Files
roi-theme/wp-content/plugins/wp-database-tools/admin/class-wp-database-tools-admin.php
root a22573bf0b Commit inicial - WordPress Análisis de Precios Unitarios
- WordPress core y plugins
- Tema Twenty Twenty-Four configurado
- Plugin allow-unfiltered-html.php simplificado
- .gitignore configurado para excluir wp-config.php y uploads

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-03 21:04:30 -06:00

1106 lines
37 KiB
PHP
Executable File

<?php
/**
* The admin-specific functionality of the plugin.
*
* @link https://raiolanetworks.es
* @since 1.0.0
*
* @package Wp_Database_Tools
* @subpackage Wp_Database_Tools/admin
*/
/**
* The admin-specific functionality of the plugin.
*
* Defines the plugin name, version, and two examples hooks for how to
* enqueue the admin-specific stylesheet and JavaScript.
*
* @package Wp_Database_Tools
* @subpackage Wp_Database_Tools/admin
* @author Raiola Networks <info@raiolanetworks.es>
*/
class Wp_Database_Tools_Admin {
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $plugin_name The ID of this plugin.
*/
private $plugin_name;
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
/**
* Databse object.
*
* @since 1.0.0
* @access private
* @var Wp_Database_Tools_Database $database Class manage database functions.
*/
private $database;
/**
* The class responsible for logic of manage license.
*
* @since 1.0.0
* @access protected
* @var Wp_Database_Tools_License $license Manage the license functionalities.
*/
protected $license;
/**
* The class responsible for logic of manage cronjobs.
*
* @since 1.0.0
* @access protected
* @var Wp_Database_Tools_Cronjobs $cronjobs Manage the cronjobs functionalities.
*/
protected $cronjobs;
/**
* The class responsible for logic of manage scanner.
*
* @since 1.0.0
* @access protected
* @var Wp_Database_Tools_Scanner $scanner Manage the scanner functionalities.
*/
protected $scanner;
/**
* The class responsible for logic of manage feedback.
*
* @since 1.0.0
* @access protected
* @var Wp_Database_Tools_Feedback $feedback Manage the feedback functionalities.
*/
protected $feedback;
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @param string $plugin_name The name of this plugin.
* @param string $version The version of this plugin.
* @param Wp_Database_Tools_Database $database Database class.
* @param Wp_Database_Tools_License $license Licence class.
* @param Wp_Database_Tools_Scanner $scanner Scanner class.
* @param Wp_Database_Tools_Cronjobs $cronjobs Cronjobs class.
* @param Wp_Database_Tools_Feedback $feedback Feedbacl class.
*/
public function __construct( $plugin_name, $version, $database, $license, $scanner, $cronjobs, $feedback ) {
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wp-database-tools-logger.php';
$this->plugin_name = $plugin_name;
$this->version = $version;
$this->database = $database;
$this->license = $license;
$this->scanner = $scanner;
$this->cronjobs = $cronjobs;
$this->feedback = $feedback;
}
/**
* Register the stylesheets for the admin area.
*
* @since 1.0.0
*/
public function enqueue_styles() {
wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wp-database-tools-admin.css', array(), $this->version, 'all' );
wp_enqueue_style( $this->plugin_name . '-choices.min.css', plugin_dir_url( __FILE__ ) . 'css/choices.min.css', array(), $this->version, 'all' );
}
/**
* Register the JavaScript for the admin area.
*
* @since 1.0.0
*/
public function enqueue_scripts() {
if ( str_contains( get_current_screen()->base, 'wp-db-tools' ) || str_contains( get_current_screen()->base, 'wp-database-tools' ) ) {
$settings_page = $this->get_settings_pages();
if ( 'YES' === $settings_page['force_execute_scanner'] ) {
// If not is dashboard and license.
if ( 'toplevel_page_wp-database-tools' !== get_current_screen()->base && 'wp-db-tools_page_license' !== get_current_screen()->base ) {
$name_script = 'wp-database-tools-force-scanner-status.js';
wp_enqueue_script( $this->plugin_name . $name_script, plugin_dir_url( __FILE__ ) . 'js/admin-ajax/' . $name_script, array( 'jquery' ), $this->version, true );
wp_register_script( $this->plugin_name . $name_script, plugin_dir_url( __FILE__ ) . 'js/admin-ajax/' . $name_script, array( 'jquery' ), $this->version, true );
wp_localize_script(
$this->plugin_name . $name_script,
'ajax_var_check_force_scanner',
array(
'url' => admin_url( 'admin-ajax.php?script=check_force_scanner' ),
'nonce' => wp_create_nonce( 'nonce_check_force_scanner' ),
'action' => 'check_force_scanner',
'image_error' => plugin_dir_url( __DIR__ ) . 'admin/img/eye-scanner-red.jpg',
'translations' => array(
'errors' => array(
'cron' => __( 'There seem to be problems running the cronjobs for this installation', 'wp-database-tools' ),
'attempts' => __( 'The number of attempts has been exceeded', 'wp-database-tools' ),
),
'success' => array(
'scanner' => __( "The scan was successful. Let's refresh your website", 'wp-database-tools' ),
),
),
)
);
}
}
if ( 'toplevel_page_wp-database-tools' !== get_current_screen()->base && 'wp-db-tools_page_license' !== get_current_screen()->base && 'YES' !== $settings_page['force_execute_scanner'] ) {
// Vars.
wp_enqueue_script( $this->plugin_name . '-admin-vars.js', array(), plugin_dir_url( __FILE__ ) . 'js/wp-database-tools-admin-vars.js', array( 'jquery' ), $this->version, true );
wp_register_script( $this->plugin_name . '-admin-vars.js', plugin_dir_url( __FILE__ ) . 'js/wp-database-tools-admin-vars.js', array( 'jquery' ), $this->version, true );
wp_localize_script(
$this->plugin_name . '-admin-vars.js',
'wpdbt_admin_vars',
array(
'data' => $this->set_data_wpdbt_admin_vars(),
)
);
// Libraries.
wp_enqueue_script( $this->plugin_name . '-modal-js', plugin_dir_url( __FILE__ ) . 'js/libraries/micromodal.min.js', array(), $this->version, true );
wp_enqueue_script( $this->plugin_name . '-choices.min.js', plugin_dir_url( __FILE__ ) . 'js/libraries/choices.min.js', array(), '6.5.3', true );
wp_enqueue_script( $this->plugin_name . '-list-js', plugin_dir_url( __FILE__ ) . 'js/libraries/list.min.js', array(), '2.3.1', true );
// Main.
wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/wp-database-tools-admin.js', array( 'jquery' ), $this->version, true );
// Templates.
wp_enqueue_script( $this->plugin_name . '-templates.js', plugin_dir_url( __FILE__ ) . 'js/templates.js', array( 'jquery' ), $this->version, true );
// Admin ajax.
wp_enqueue_script( $this->plugin_name . '-scanner-js', array(), plugin_dir_url( __FILE__ ) . 'js/admin-ajax/wp-database-tools-scanner.js', array( $this->plugin_name . '-admin-vars.js' ), $this->version, true );
wp_enqueue_script( $this->plugin_name . '-feedback-js', array(), plugin_dir_url( __FILE__ ) . 'js/admin-ajax/wp-database-tools-feedback.js', array( $this->plugin_name . '-admin-vars.js' ), $this->version, true );
wp_enqueue_script( $this->plugin_name . '-remove-all-transients', array(), plugin_dir_url( __FILE__ ) . 'js/admin-ajax/wp-database-tools-remove-all-transients.js', array( $this->plugin_name . '-admin-vars.js' ), $this->version, true );
// Register admin ajax.
wp_register_script( $this->plugin_name . '-scanner-js', plugin_dir_url( __FILE__ ) . 'js/admin-ajax/wp-database-tools-scanner.js', array( 'jquery' ), $this->version, true );
wp_register_script( $this->plugin_name . '-feedback-js', plugin_dir_url( __FILE__ ) . 'js/admin-ajax/wp-database-tools-feedback.js', array( 'jquery' ), $this->version, true );
wp_register_script( $this->plugin_name . '-remove-all-transients', plugin_dir_url( __FILE__ ) . 'js/admin-ajax/wp-database-tools-remove-all-transients.js', array( 'jquery' ), $this->version, true );
// Localiza admin ajax.
// Scanner.
wp_localize_script(
$this->plugin_name . '-scanner-js',
'ajax_var_scanner',
array(
'url' => admin_url( 'admin-ajax.php?script=init_scanner' ),
'nonce' => wp_create_nonce( 'nonce_scanner' ),
'action' => 'init_scanner',
)
);
// Save feedback.
wp_localize_script(
$this->plugin_name . '-feedback-js',
'ajax_var',
array(
'url' => admin_url( 'admin-ajax.php?script=feedback-user' ),
'nonce' => wp_create_nonce( 'feedback-user' ),
'action' => 'save_feedback_user',
)
);
// Remove all transients.
wp_localize_script(
$this->plugin_name . '-remove-all-transients',
'ajax_var',
array(
'url' => admin_url( 'admin-ajax.php?script=remove-all-transients' ),
'nonce' => wp_create_nonce( 'remove-all-transients' ),
'action' => 'remove_all_transients',
)
);
// List.
if ( 'wp-db-tools_page_general' === get_current_screen()->base ) {
wp_enqueue_script( $this->plugin_name . '-main-list-common', plugin_dir_url( __FILE__ ) . 'js/main-list-common.js', array( $this->plugin_name . '-list-js' ), $this->version, true );
wp_enqueue_script( $this->plugin_name . '-main-list-js', plugin_dir_url( __FILE__ ) . 'js/main-list-general.js', array( $this->plugin_name . '-list-js' ), $this->version, true );
} elseif ( 'wp-db-tools_page_license' !== get_current_screen()->base ) {
wp_enqueue_script( $this->plugin_name . '-main-list-common', plugin_dir_url( __FILE__ ) . 'js/main-list-common.js', array( $this->plugin_name . '-list-js' ), $this->version, true );
wp_enqueue_script( $this->plugin_name . '-templates-item-list-js', plugin_dir_url( __FILE__ ) . 'js/templates/item-list.js', array( $this->plugin_name . '-list-js' ), $this->version, true );
wp_enqueue_script( $this->plugin_name . '-main-list-js', plugin_dir_url( __FILE__ ) . 'js/main-list.js', array( $this->plugin_name . '-templates-item-list-js' ), $this->version, true );
wp_enqueue_script( $this->plugin_name . '-loading-status.js', plugin_dir_url( __FILE__ ) . 'js/admin-ajax/wp-database-tools-loading-status.js', array( $this->plugin_name . '-loading.js' ), $this->version, true );
wp_enqueue_script( $this->plugin_name . '-loading.js', plugin_dir_url( __FILE__ ) . 'js/admin-ajax/wp-database-tools-loading.js', array( 'jquery' ), $this->version, true );
// Save status.
wp_localize_script(
$this->plugin_name . '-loading-status.js',
'ajax_var_load_status',
array(
'url' => admin_url( 'admin-ajax.php?script=load_data_status' ),
'nonce' => wp_create_nonce( 'load_data_status' ),
'action' => 'load_data_status',
)
);
// Loading.
wp_localize_script(
$this->plugin_name . '-loading.js',
'ajax_var_load_data',
array(
'url' => admin_url( 'admin-ajax.php?script=load_data' ),
'nonce' => wp_create_nonce( 'load_data' ),
'action' => 'load_data',
)
);
}
}
}
}
/**
* Return an array with the data to be used in the js files.
*
* @return array $data
*/
public function set_data_wpdbt_admin_vars() {
$key = WPDBT_PREFIX . 'admin_nonce';
if ( ! isset( $_REQUEST[ $key ] ) ) {
Wp_Database_Tools_Logger::error( 'Nonce error' );
wp_die( esc_html( __( 'Missing nonce', 'wp-database-tools' ) ) );
}
if ( ! wp_verify_nonce( sanitize_key( $_REQUEST[ $key ] ), $key ) ) {
Wp_Database_Tools_Logger::error( 'Nonce error' );
wp_die( esc_html( __( 'Nonce error', 'wp-database-tools' ) ) );
}
global $wpdb;
$license_key = $this->license->get_license_key_encrypt();
$api_domain = ( WPDBT_ENV === 'DEV' ) ? WPDBT_DEV_DOMAIN : WPDBT_PROD_DOMAIN;
$current_page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
if ( isset( $_GET['details'] ) ) {
$current_page = 'details';
}
$data = array(
'version' => WPDBT_VERSION,
'current_page' => $current_page,
'license_url' => home_url(),
'license_key' => $license_key,
'api_domain' => $api_domain,
'prefix' => $wpdb->prefix,
'admin_url' => plugin_dir_url( __DIR__ ) . 'admin/',
'pagination' => WPDBT_LIST_PAGINATION[0],
'translations' => array(
'thank_vote' => __( 'Thank you for your cooperation. Your choice has been registered', 'wp-database-tools' ),
'marketplace' => __( 'Marketplace', 'wp-database-tools' ),
'users_feedback' => __( 'Users feedback', 'wp-database-tools' ),
'reliability' => __( 'Reliability', 'wp-database-tools' ),
'votes' => __( 'Votes', 'wp-database-tools' ),
'likes' => __( 'Likes', 'wp-database-tools' ),
'dislikes' => __( 'Dislikes', 'wp-database-tools' ),
'rating' => __( 'Rating', 'wp-database-tools' ),
'website' => __( 'Website', 'wp-database-tools' ),
'download' => __( 'Download', 'wp-database-tools' ),
'remove' => __( 'Remove', 'wp-database-tools' ),
'edit' => __( 'Edit', 'wp-database-tools' ),
'empty' => __( 'Empty', 'wp-database-tools' ),
'optimize' => __( 'Optimize', 'wp-database-tools' ),
'repair' => __( 'Repair', 'wp-database-tools' ),
'details' => __( 'Details', 'wp-database-tools' ),
'refreshing' => __( 'Refreshing page', 'wp-database-tools' ),
'multiple' => __( 'Multiple', 'wp-database-tools' ),
'active' => __( 'Active', 'wp-database-tools' ),
'inactive' => __( 'Inactive', 'wp-database-tools' ),
'uninstalled' => __( 'Uninstalled', 'wp-database-tools' ),
'unknown' => __( 'Unknown', 'wp-database-tools' ),
'uncategorized' => __( 'Uncategorized', 'wp-database-tools' ),
),
);
return $data;
}
/**
* Register the menu for the admin area.
*
* @since 1.0.0
*/
public function add_admin_menu() {
$this->create_request_nonce();
add_menu_page( $this->plugin_name, 'WP DB Tools', 'manage_options', $this->plugin_name, array( $this, 'display_plugin_admin_page' ), '/wp-content/plugins/' . $this->plugin_name . '/admin/img/wp-database-tools-icon-admin.png' );
add_submenu_page( $this->plugin_name, __( 'Dashboard', 'wp-database-tools' ), __( 'Dashboard', 'wp-database-tools' ), 'manage_options', $this->plugin_name, array( $this, 'display_plugin_admin_page' ) );
add_submenu_page( $this->plugin_name, __( 'General', 'wp-database-tools' ), __( 'General', 'wp-database-tools' ), 'manage_options', 'general', array( $this, 'display_plugin_admin_page_general' ) );
add_submenu_page( $this->plugin_name, __( 'Tables', 'wp-database-tools' ), __( 'Tables', 'wp-database-tools' ), 'manage_options', 'tables', array( $this, 'display_plugin_admin_page_tables' ) );
add_submenu_page( $this->plugin_name, __( 'Options', 'wp-database-tools' ), __( 'Options', 'wp-database-tools' ), 'manage_options', 'options', array( $this, 'display_plugin_admin_page_options' ) );
add_submenu_page( $this->plugin_name, __( 'Transients', 'wp-database-tools' ), __( 'Transients', 'wp-database-tools' ), 'manage_options', 'transients', array( $this, 'display_plugin_admin_page_transients' ) );
add_submenu_page( $this->plugin_name, __( 'Cronjobs', 'wp-database-tools' ), __( 'Cronjobs', 'wp-database-tools' ), 'manage_options', 'cronjobs', array( $this, 'display_plugin_admin_page_cronjobs' ) );
add_submenu_page( $this->plugin_name, __( 'License', 'wp-database-tools' ), __( 'License', 'wp-database-tools' ), 'manage_options', 'license', array( $this, 'display_plugin_admin_page_license' ) );
}
/**
* Create a nonce for processing data
*
* @since 1.1.0
*/
public function create_request_nonce() {
$key = WPDBT_PREFIX . 'admin_nonce';
$_REQUEST[ $key ] = wp_create_nonce( $key );
}
/**
* Callback to WP Database Tools admin menu.
*
* @since 1.0.0
*/
public function display_plugin_admin_page() {
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/' . $this->plugin_name . '-admin-display.php';
}
/**
* Callback to General admin menu.
*
* @since 1.0.0
*/
public function display_plugin_admin_page_general() {
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/' . $this->plugin_name . '-admin-display-general.php';
}
/**
* Callback to Tables admin menu.
*
* @since 1.0.0
*/
public function display_plugin_admin_page_tables() {
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/' . $this->plugin_name . '-admin-display-tables.php';
}
/**
* Callback to Options admin menu.
*
* @since 1.0.0
*/
public function display_plugin_admin_page_options() {
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/' . $this->plugin_name . '-admin-display-options.php';
}
/**
* Callback to Transients admin menu.
*
* @since 1.0.0
*/
public function display_plugin_admin_page_transients() {
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/' . $this->plugin_name . '-admin-display-transients.php';
}
/**
* Callback to License admin menu.
*
* @since 1.0.0
*/
public function display_plugin_admin_page_cronjobs() {
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/' . $this->plugin_name . '-admin-display-cronjobs.php';
}
/**
* Callback to License admin menu.
*
* @since 1.0.0
*/
public function display_plugin_admin_page_scheduled() {
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/' . $this->plugin_name . '-admin-display-scheduled.php';
}
/**
* Callback to License admin menu.
*
* @since 1.0.0
*/
public function display_plugin_admin_page_license() {
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/' . $this->plugin_name . '-admin-display-license.php';
}
/**
* Active form license
*
* @since 1.0.0
*/
public function active_plugin() {
$this->license->form_action_license();
}
/**
* Call action forms
*
* @since 1.0.0
*/
public function process_general() {
$this->database->action_form_general();
}
/**
* Call tables action forms
*
* @since 1.0.0
*/
public function process_tables() {
$this->database->action_form_tables();
}
/**
* Call options action forms
*
* @since 1.0.0
*/
public function process_options() {
$this->database->action_form_options();
}
/**
* Call options action forms
*
* @since 1.0.0
*/
public function process_transients() {
$this->database->action_form_transients();
}
/**
* Call cronjobs action forms
*
* @since 1.0.0
*/
public function process_cronjobs() {
$this->database->action_form_cronjobs();
}
/**
* Register notifications
*
* @since 1.0.0
*/
public function custom_notices() {
$key = WPDBT_PREFIX . 'admin_nonce';
if ( ! isset( $_REQUEST[ $key ] ) ) {
Wp_Database_Tools_Logger::error( 'Nonce error' );
wp_die( esc_html( __( 'Missing nonce', 'wp-database-tools' ) ) );
}
if ( ! wp_verify_nonce( sanitize_key( $_REQUEST[ $key ] ), $key ) ) {
Wp_Database_Tools_Logger::error( 'Nonce error' );
wp_die( esc_html( __( 'Nonce error', 'wp-database-tools' ) ) );
}
// Pugin activation.
$sl_activation = isset( $_GET[ WPDBT_PREFIX . 'sl_activation' ] ) ? sanitize_text_field( wp_unslash( $_GET[ WPDBT_PREFIX . 'sl_activation' ] ) ) : '';
$message = isset( $_GET['message'] ) ? sanitize_text_field( wp_unslash( $_GET['message'] ) ) : '';
$allowed_tags = array(
'br' => array(),
'p' => array(),
'strong' => array(),
'span' => array(),
);
if ( ! empty( $sl_activation ) && ! empty( $message ) ) {
switch ( $_GET[ WPDBT_PREFIX . 'sl_activation' ] ) {
case 'false':
$message = urldecode( $message );?>
<div class="wpdb error notice-error is-dismissible">
<p><?php echo wp_kses( $message, $allowed_tags ); ?></p>
</div>
<?php
break;
case 'true':
$message = urldecode( $message );
?>
<div class="wpdb notice notice-success is-dismissible">
<p><?php echo wp_kses( $message, $allowed_tags ); ?></p>
</div>
<?php
break;
}
}
// General actions.
if ( isset( $_GET[ WPDBT_PREFIX . 'notice' ] ) ) {
$pages = array( 'general', 'cronjobs', 'tables', 'options', 'transients' );
if ( in_array( $_GET[ WPDBT_PREFIX . 'notice' ], $pages, true ) ) {
if ( ! empty( get_transient( WPDBT_PREFIX . 'errors' ) ) ) {
$message = get_transient( WPDBT_PREFIX . 'errors' );
?>
<div class="wpdb notice notice-error is-dismissible">
<p><?php echo wp_kses( $message, $allowed_tags ); ?></p>
</div>
<?php
}
if ( ! empty( get_transient( WPDBT_PREFIX . 'successes' ) ) ) {
$message = get_transient( WPDBT_PREFIX . 'successes' );
?>
<div class="wpdb notice notice-success is-dismissible">
<p><?php echo wp_kses( $message, $allowed_tags ); ?></p>
</div>
<?php
}
if ( ! empty( get_transient( WPDBT_PREFIX . 'warning' ) ) ) {
$message = get_transient( WPDBT_PREFIX . 'warning' );
?>
<div class="wpdb notice notice-warning is-dismissible">
<p><?php echo esc_html( __( 'Some records have been retained because they do not comply with the date range', 'wp-database-tools' ) ); ?></p>
</div>
<?php
}
set_transient( WPDBT_PREFIX . 'errors', '' );
set_transient( WPDBT_PREFIX . 'successes', '' );
set_transient( WPDBT_PREFIX . 'warning', '' );
}
}
}
/**
* Prepares some variables used in the pages
*
* @since 1.0.0
*/
public function get_settings_pages() {
$key = WPDBT_PREFIX . 'admin_nonce';
if ( ! isset( $_REQUEST[ $key ] ) ) {
Wp_Database_Tools_Logger::error( 'Nonce error' );
wp_die( esc_html( __( 'Missing nonce', 'wp-database-tools' ) ) );
}
if ( ! wp_verify_nonce( sanitize_key( $_REQUEST[ $key ] ), $key ) ) {
Wp_Database_Tools_Logger::error( 'Nonce error' );
wp_die( esc_html( __( 'Nonce error', 'wp-database-tools' ) ) );
}
$settings =
array(
'title' => 'WP Database Tools',
'admin_url' => plugin_dir_url( __DIR__ ) . 'admin/',
);
// CONFIG INFO PLUGIN.
$domain = ( WPDBT_ENV === 'DEV' ) ? WPDBT_DEV_DOMAIN : WPDBT_PROD_DOMAIN;
$url = $domain . '/api/v2/config';
$response = wp_remote_get( $url );
if ( is_wp_error( $response ) ) {
$settings['config'] = false;
} else {
$config = wp_remote_retrieve_body( $response );
$settings['config'] = json_decode( $config );
}
$settings['bulk_actions'] = $this->set_bulk_actions();
// SCANNER STATUS.
$settings['scanner'] = json_decode( get_transient( WPDBT_PREFIX . 'scanner_last_status' ) );
$settings['force_execute_scanner'] = get_option( WPDBT_PREFIX . 'force_execute_scanner' );
// Current page.
$current_page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
$settings['current_page'] = $current_page;
// Force execute cron scanner.
if ( 'YES' === $settings['force_execute_scanner'] ) {
spawn_cron();
}
$settings['filters'] =
array(
'name' => ( isset( $_GET['name'] ) ) ? sanitize_text_field( wp_unslash( $_GET['name'] ) ) : '',
'cat' => ( isset( $_GET['category'] ) ) ? sanitize_text_field( wp_unslash( $_GET['category'] ) ) : 'all',
'status' => ( isset( $_GET['status'] ) ) ? sanitize_text_field( wp_unslash( $_GET['status'] ) ) : 'all',
'autoload' => ( isset( $_GET['autoload'] ) ) ? sanitize_text_field( wp_unslash( $_GET['autoload'] ) ) : 'all',
);
if ( get_admin_page_title() === __( 'General', 'wp-database-tools' ) ) {
$this->database->init_general();
}
if ( get_admin_page_title() === __( 'Dashboard', 'wp-database-tools' ) ) {
// Change.
global $wp_version;
$settings['dashboard']['info'] =
array(
array(
'data' => $this->database->get_size()['format'],
'label' => __( 'Database Size', 'wp-database-tools' ),
'icon' => WPDBT_ADMIN_URL . 'img/icon-db.svg',
),
array(
'data' => $this->database->get_tables_obj()->get_count(),
'label' => __( 'Total Tables', 'wp-database-tools' ),
'icon' => WPDBT_ADMIN_URL . 'img/icon-db.svg',
),
array(
'data' => $this->database->get_options_obj()->get_count(),
'label' => __( 'Total Options', 'wp-database-tools' ),
'icon' => WPDBT_ADMIN_URL . 'img/icon-db.svg',
),
array(
'data' => count( wp_load_alloptions() ),
'label' => __( 'Total Autoload', 'wp-database-tools' ),
'icon' => WPDBT_ADMIN_URL . 'img/icon-db.svg',
),
array(
'data' => $this->database->get_transients_obj()->get_count(),
'label' => __( 'Total Transient', 'wp-database-tools' ),
'icon' => WPDBT_ADMIN_URL . 'img/icon-db.svg',
),
array(
'data' => $this->database->get_cronjobs_obj()->get_count(),
'label' => __( 'Total Cronjobs', 'wp-database-tools' ),
'icon' => WPDBT_ADMIN_URL . 'img/icon-db.svg',
),
array(
'data' => phpversion(),
'label' => __( 'PHP Version', 'wp-database-tools' ),
'icon' => WPDBT_ADMIN_URL . 'img/icon-db.svg',
),
array(
'data' => $wp_version,
'label' => __( 'WordPress Version', 'wp-database-tools' ),
'icon' => WPDBT_ADMIN_URL . 'img/icon-db.svg',
),
);
}
return $settings;
}
/**
* Set an array with the bulk actions to be applied depending on each page.
*
* @since 1.0.0
*/
public function set_bulk_actions() {
$key = WPDBT_PREFIX . 'admin_nonce';
if ( ! isset( $_REQUEST[ $key ] ) ) {
Wp_Database_Tools_Logger::error( 'Nonce error' );
wp_die( esc_html( __( 'Missing nonce', 'wp-database-tools' ) ) );
}
if ( ! wp_verify_nonce( sanitize_key( $_REQUEST[ $key ] ), $key ) ) {
Wp_Database_Tools_Logger::error( 'Nonce error' );
wp_die( esc_html( __( 'Nonce error', 'wp-database-tools' ) ) );
}
$bulk_actions =
array(
'delete' => array(
'label' => 'Delete',
'txt' => __( 'Remove', 'wp-database-tools' ),
),
'empty' => array(
'label' => 'Empty',
'txt' => __( 'Empty', 'wp-database-tools' ),
),
'edit' => array(
'label' => 'Edit',
'txt' => __( 'Edit', 'wp-database-tools' ),
),
'repair' => array(
'label' => 'Repair',
'txt' => __( 'Repair', 'wp-database-tools' ),
),
'optimize' => array(
'label' => 'Optimize',
'txt' => __( 'Optimize', 'wp-database-tools' ),
),
'uncategorized' => array(
'label' => 'Category',
'txt' => __( 'Category', 'wp-database-tools' ),
),
);
$page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
if ( 'tables' === $page ) {
unset( $bulk_actions['edit'] );
}
if ( 'options' === $page ) {
unset( $bulk_actions['empty'] );
unset( $bulk_actions['optimize'] );
unset( $bulk_actions['repair'] );
}
if ( 'transients' === $page ) {
unset( $bulk_actions['empty'] );
unset( $bulk_actions['optimize'] );
unset( $bulk_actions['repair'] );
}
if ( 'cronjobs' === $page ) {
unset( $bulk_actions['empty'] );
unset( $bulk_actions['optimize'] );
unset( $bulk_actions['repair'] );
unset( $bulk_actions['edit'] );
}
if ( 'general' === $page ) {
unset( $bulk_actions['empty'] );
unset( $bulk_actions['optimize'] );
unset( $bulk_actions['repair'] );
unset( $bulk_actions['uncategorized'] );
if ( isset( $_GET['details'] ) ) {
unset( $bulk_actions['edit'] );
}
}
return $bulk_actions;
}
/**
* This method is defined in the WordPress cronjob call.
*
* @since 1.0.0
*/
public function cron_scanner() {
Wp_Database_Tools_Logger::info( 'INIT CRON SCANNER: init process' );
// Check transients data.
$key = 'transients';
delete_transient( $key . '_data_loop' );
do {
$this->database->init_transients();
$status_loading = get_transient( WPDBT_PREFIX . 'status_loading' );
} while ( false === $status_loading['cache'] && false === $status_loading['data_loop']['is_finish'] );
$transients = $this->database->get_transients_data();
$transients_count = isset( $transients['data'] ) ? count( $transients['data'] ) : 'no data';
Wp_Database_Tools_Logger::info( 'INIT CRON SCANNER: ending transients data ' . $transients_count );
delete_transient( $key . '_data_loop' );
// Check the tables data.
$key = 'tables';
delete_transient( $key . '_data_loop' );
do {
$this->database->init_tables();
$status_loading = get_transient( WPDBT_PREFIX . 'status_loading' );
} while ( false === $status_loading['cache'] && false === $status_loading['data_loop']['is_finish'] );
$tables = $this->database->get_tables_data();
$tables_count = isset( $tables['data'] ) ? count( $tables['data'] ) : 'no data';
Wp_Database_Tools_Logger::info( 'INIT CRON SCANNER: ending tables data ' . $tables_count );
delete_transient( $key . '_data_loop' );
// Check options data.
$key = 'options';
delete_transient( $key . '_data_loop' );
do {
$this->database->init_options();
$status_loading = get_transient( WPDBT_PREFIX . 'status_loading' );
} while ( false === $status_loading['cache'] && false === $status_loading['data_loop']['is_finish'] );
$options = $this->database->get_options_data();
$options_count = isset( $options['data'] ) ? count( $options['data'] ) : 'no data';
Wp_Database_Tools_Logger::info( 'INIT CRON SCANNER: ending options data ' . $options_count );
delete_transient( $key . '_data_loop' );
// Check cronjobs data.
$key = 'cronjobs';
delete_transient( $key . '_data_loop' );
do {
$this->database->init_cronjobs();
$status_loading = get_transient( WPDBT_PREFIX . 'status_loading' );
} while ( false === $status_loading['cache'] && false === $status_loading['data_loop']['is_finish'] );
$cronjobs = $this->database->get_cronjobs_data();
$cronjobs_count = isset( $cronjobs['data'] ) ? count( $cronjobs['data'] ) : 'no data';
Wp_Database_Tools_Logger::info( 'INIT CRON SCANNER: ending cronjobs data ' . $cronjobs_count );
delete_transient( $key . '_data_loop' );
Wp_Database_Tools_Logger::info( 'INIT CRON SCANNER: finish process' );
$this->scanner->cron_scanner( $tables, $options, $cronjobs, $transients );
}
/**
* Method executed from ajax.
* It is executed recursively until the state of the scanner is success|error.
*
* Invoked from /admin/js/scanner.js
*
* @since 1.0.0
*/
public function init_scanner() {
Wp_Database_Tools_Logger::info( 'INIT SCANNER: start scanner' );
// Check if cronjobs is working.
$cron_check = get_transient( WPDBT_PREFIX . 'cron_check' ) ?? 'NO';
if ( 'error' === $cron_check ) {
$scanner_status_data = array(
'status' => 'error',
'message' => __( 'There seem to be problems running the cronjobs for this installation', 'wp-database-tools' ),
'time' => gmdate( 'Y-m-d H:i:s' ),
);
delete_transient( WPDBT_PREFIX . 'scanner_status' );
set_transient( WPDBT_PREFIX . 'scanner_last_status', wp_json_encode( $scanner_status_data ) );
wp_send_json( $scanner_status_data );
wp_die();
}
// Check if nonce is misssing.
if ( ! isset( $_POST['nonce'] ) ) {
Wp_Database_Tools_Logger::error( 'Nonce error' );
wp_die( esc_html( __( 'Missing nonce', 'wp-database-tools' ) ) );
}
// Check nonce verify.
if ( ! wp_verify_nonce( sanitize_key( $_POST['nonce'] ), 'nonce_scanner' ) ) {
Wp_Database_Tools_Logger::error( 'Nonce error' );
wp_die( esc_html( __( 'Missing nonce', 'wp-database-tools' ) ) );
}
if ( ! isset( $_POST['first_call'] ) ) {
Wp_Database_Tools_Logger::error( 'first_call missing' );
wp_die( esc_html( __( 'First_call missing', 'wp-database-tools' ) ) );
}
$first_call = sanitize_text_field( wp_unslash( $_POST['first_call'] ) );
Wp_Database_Tools_Logger::info( 'INIT SCANNER: fisrt call ' . wp_json_encode( $first_call ) );
$scanner_status = get_transient( WPDBT_PREFIX . 'scanner_status' );
Wp_Database_Tools_Logger::info( 'INIT SCANNER: status scanner ' . wp_json_encode( $scanner_status ) );
if ( 'true' === $first_call ) {
delete_transient( WPDBT_PREFIX . 'scanner_status' );
wp_die();
}
if ( ! empty( $scanner_status ) ) {
$decode_data = json_decode( $scanner_status );
if ( 'success' === $decode_data->status || 'error' === $decode_data->status ) {
wp_send_json( $scanner_status );
delete_transient( WPDBT_PREFIX . 'scanner_status' );
wp_die();
}
$scanner_status = get_transient( WPDBT_PREFIX . 'scanner_status' );
wp_send_json( $scanner_status );
wp_die();
}
if ( empty( $scanner_status ) || null === $scanner_status || false === $scanner_status ) {
Wp_Database_Tools_Logger::info( 'INIT SCANNER: ajax init cron' );
// schedule cron job.
if ( ! wp_next_scheduled( 'wp_database_tools_scanner_database_single' ) ) {
wp_schedule_single_event( time() - 3600, 'wp_database_tools_scanner_database_single' );
}
spawn_cron();
$scanner_status_data = array(
'status' => 'active',
'message' => __( 'Performing scanner', 'wp-database-tools' ),
'time' => gmdate( 'Y-m-d H:i:s' ),
);
set_transient( WPDBT_PREFIX . 'scanner_status', wp_json_encode( $scanner_status_data ) );
wp_send_json( $scanner_status );
wp_die();
}
Wp_Database_Tools_Logger::info( 'INIT SCANNER: finish scanner' );
wp_die();
}
/**
* Called from admin ajax gets the state of the transient containing
* the percentage of the data load.
*
* Invoked from /admin/js/scanner.js
*
* @since 1.0.0
*/
public function load_data_status() {
if ( ! isset( $_POST['nonce'] ) ) {
Wp_Database_Tools_Logger::error( 'Nonce error' );
wp_die( esc_html( __( 'Missing nonce', 'wp-database-tools' ) ) );
}
if ( ! wp_verify_nonce( sanitize_key( $_POST['nonce'] ), 'load_data_status' ) ) {
Wp_Database_Tools_Logger::error( 'Nonce error' );
wp_die( esc_html( __( 'Missing nonce', 'wp-database-tools' ) ) );
}
$load_status = get_transient( WPDBT_PREFIX . 'status_loading' );
echo wp_json_encode( $load_status );
wp_die();
}
/**
* Returns the formatted data from their corresponding classes.
* Returns a json that will be used to load the list of values from js.
*
* Invoked from /admin/js/scanner.js
*
* @since 1.0.0
*/
public function load_data() {
if ( ! isset( $_POST['nonce'] ) ) {
Wp_Database_Tools_Logger::error( 'Missing nonce.' );
wp_die( esc_html( __( 'Missing nonce', 'wp-database-tools' ) ) );
}
if ( ! wp_verify_nonce( sanitize_key( $_POST['nonce'] ), 'load_data' ) ) {
Wp_Database_Tools_Logger::error( 'Nonce error' );
wp_die( esc_html( __( 'Error nonce', 'wp-database-tools' ) ) );
}
if ( ! isset( $_POST['page'] ) ) {
Wp_Database_Tools_Logger::error( 'Missing page.' );
wp_die( esc_html( __( 'Missing page', 'wp-database-tools' ) ) );
}
$page = sanitize_key( $_POST['page'] );
$data = array();
// OPTIONS.
if ( 'options' === $page ) {
// This init the method check data.
$this->database->init_options();
// Return the data.
$data = $this->database->get_options_data();
// Return data loop.
$data['status_loading'] = get_transient( WPDBT_PREFIX . 'status_loading' );
echo wp_json_encode( $data );
wp_die();
}
// TRANSIENTS.
if ( 'transients' === $page ) {
// This init the method check data.
$this->database->init_transients();
// Return the data.
$data = $this->database->get_transients_data();
// Return data loop.
$data['status_loading'] = get_transient( WPDBT_PREFIX . 'status_loading' );
echo wp_json_encode( $data );
wp_die();
}
// TABLES.
if ( 'tables' === $page ) {
// This init the method check data.
$this->database->init_tables();
// Return the data.
$data = $this->database->get_tables_data();
// Return data loop.
$data['status_loading'] = get_transient( WPDBT_PREFIX . 'status_loading' );
echo wp_json_encode( $data );
wp_die();
}
// CRONJOBS.
if ( 'cronjobs' === $page ) {
// This init the method check data.
$this->database->init_cronjobs();
// Return the data.
$data = $this->database->get_cronjobs_data();
// Return data loop.
$data['status_loading'] = get_transient( WPDBT_PREFIX . 'status_loading' );
echo wp_json_encode( $data );
wp_die();
}
}
/**
* Check if it is necessary to force a first scan in case
* there is no data saved as API response.
* This first scan is done partly to mitigate the performance
* of a first load without API data because if there is no data,
* it must be processed by the matching algorithm.
*
* @since 1.0.0
*/
public function check_force_scanner() {
if ( ! isset( $_POST['nonce'] ) ) {
Wp_Database_Tools_Logger::error( 'Nonce error' );
wp_die( esc_html( __( 'Missing nonce', 'wp-database-tools' ) ) );
}
if ( ! wp_verify_nonce( sanitize_key( $_POST['nonce'] ), 'nonce_check_force_scanner' ) ) {
Wp_Database_Tools_Logger::error( 'Nonce error' );
wp_die( esc_html( __( 'Verify nonce', 'wp-database-tools' ) ) );
}
spawn_cron();
$response = array(
'cron_status' => get_transient( WPDBT_PREFIX . 'cron_check' ) ?? 'NO',
'force_scanner' => get_option( WPDBT_PREFIX . 'force_execute_scanner' ),
);
wp_send_json( wp_json_encode( $response ) );
wp_die();
}
/**
* Return database object
*
* @access public
* @since 1.0.0
* @return Wp_Database_Tools_Database
*/
public function get_database() {
return $this->database;
}
}