<?php


if (!defined('ABSPATH')) {
    exit();
}
if (!class_exists('fdbgp_cronjob')) {
    class fdbgp_cronjob
    {

        public function __construct()
        {
            //initialize Cron Jobs
            add_filter('cron_schedules', array($this, 'fdbgp_cron_schedules'));
            add_action('fdbgp_extra_data_update', array($this, 'fdbgp_cron_extra_data_autoupdater'));
        }

        /*
        |--------------------------------------------------------------------------
        |  cron custom schedules
        |--------------------------------------------------------------------------
         */

        public function fdbgp_cron_schedules($schedules)
        {

            if (!isset($schedules['every_30_days'])) {

                $schedules['every_30_days'] = array(
                    'interval' => 30 * 24 * 60 * 60, // 2,592,000 seconds
                    'display'  => __('Once every 30 days', 'sb-elementor-contact-form-db'),
                );
            }

            return $schedules;
        }

         /*
        |--------------------------------------------------------------------------
        |  cron extra data autoupdater
        |--------------------------------------------------------------------------
         */

        function fdbgp_cron_extra_data_autoupdater() {

            $settings  = get_option('cfef_usage_share_data');
            
            if (!empty($settings) || $settings === 'on') {
                fdbgp_cronjob::fdbgp_send_data();
            }
            
        }

        /*
        |--------------------------------------------------------------------------
        |  cron send data
        |--------------------------------------------------------------------------
         */ 

         public static function cpfm_get_user_info() {
                global $wpdb;
                // Server and WP environment details
                $server_info = [
                    'server_software'        => isset($_SERVER['SERVER_SOFTWARE']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_SOFTWARE'])) : 'N/A',
                    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Query is incrementally prepared above.
                    'mysql_version'          => $wpdb ? sanitize_text_field($wpdb->get_var("SELECT VERSION()")) : 'N/A',
                    'php_version'            => sanitize_text_field(phpversion() ?: 'N/A'),
                    'wp_version'             => sanitize_text_field(get_bloginfo('version') ?: 'N/A'),
                    'wp_debug'               => (defined('WP_DEBUG') && WP_DEBUG) ? 'Enabled' : 'Disabled',
                    'wp_memory_limit'        => sanitize_text_field(ini_get('memory_limit') ?: 'N/A'),
                    'wp_max_upload_size'     => sanitize_text_field(ini_get('upload_max_filesize') ?: 'N/A'),
                    'wp_permalink_structure' => sanitize_text_field(get_option('permalink_structure') ?: 'Default'),
                    'wp_multisite'           => is_multisite() ? 'Enabled' : 'Disabled',
                    'wp_language'            => sanitize_text_field(get_option('WPLANG') ?: get_locale()),
                    'wp_prefix'              => isset($wpdb->prefix) ? sanitize_key($wpdb->prefix) : 'N/A',
                ];
                // Theme details
                $theme = wp_get_theme();
                $theme_data = [
                    'name'      => sanitize_text_field($theme->get('Name')),
                    'version'   => sanitize_text_field($theme->get('Version')),
                    'theme_uri' => esc_url($theme->get('ThemeURI')),
                ];
                // Ensure plugin functions are loaded
                if ( ! function_exists('get_plugins') ) {
                    require_once ABSPATH . 'wp-admin/includes/plugin.php';
                }
                // Active plugins details
                $active_plugins = get_option('active_plugins', []);
                $plugin_data = [];
                foreach ( $active_plugins as $plugin_path ) {
                    $plugin_info = get_plugin_data(WP_PLUGIN_DIR . '/' . sanitize_text_field($plugin_path));
                    $plugin_data[] = [
                        'name'       => sanitize_text_field($plugin_info['Name']),
                        'version'    => sanitize_text_field($plugin_info['Version']),
                    'plugin_uri' => esc_url( !empty($plugin_info['PluginURI']) ? $plugin_info['PluginURI'] : $plugin_info['AuthorURI'] ),
                    ];
                }
                return [
                    'server_info'   => $server_info,
                    'extra_details' => [
                        'wp_theme'       => $theme_data,
                        'active_plugins' => $plugin_data,
                    ],
                ];
            }


         static public function fdbgp_send_data() {
 
                 $feedback_url = FDBGP_FEEDBACK_URL.'wp-json/coolplugins-feedback/v1/site';
                    
                 $extra_data_details = static::cpfm_get_user_info();

                  $server_info        = $extra_data_details['server_info'];
                  $extra_details      = $extra_data_details['extra_details'];
                  $site_url           = get_site_url();
                  $install_date       = get_option('fdbgp-install-date');
                  $uni_id      		  = '12';
			      $site_id            = $site_url . '-' . $install_date . '-' .$uni_id;
                 
                  $initial_version = get_option('formsdb_initial_version');
                  $initial_version = is_string($initial_version) ? sanitize_text_field($initial_version) : 'N/A';
                  $plugin_version = defined('FDBGP_PLUGIN_VERSION') ? FDBGP_PLUGIN_VERSION : 'N/A';
                  $admin_email = sanitize_email(get_option('admin_email') ?: 'N/A');
              
                  $post_data = array(
                      'site_id'           => sanitize_text_field(md5($site_id)),
                      'plugin_version'    => sanitize_text_field($plugin_version),
                      'plugin_name'       => "FormsDB For Elementor Forms",
                      'plugin_initial'    => sanitize_text_field($initial_version),
                      'email'             => sanitize_email($admin_email),
                      'site_url'          => esc_url_raw($site_url),
                      'server_info'       => $server_info,
                      'extra_details'     => $extra_details,
                  );
              
                  $response = wp_remote_post(esc_url($feedback_url), array(
                      'method'    => 'POST',
                      'timeout'   => 30,
                      'headers'   => array(
                          'Content-Type' => 'application/json',
                      ),
                      'body'      => wp_json_encode($post_data),
                  ));
              
                  if (is_wp_error($response)) {
                    return;
                  }
              
                  $response_body = wp_remote_retrieve_body($response);
                  $decoded = json_decode($response_body, true);
                
                  if (!wp_next_scheduled('fdbgp_extra_data_update')) {
                    wp_schedule_event(time(), 'every_30_days', 'fdbgp_extra_data_update');
                }
             
        }

    }
    
    // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
    $cron_init = new fdbgp_cronjob();
}
