Files
roi-theme/wp-content/plugins/wp-database-tools/includes/class-wp-database-tools-scanner.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

306 lines
9.8 KiB
PHP
Executable File

<?php
/**
* Database functions
*
* @link https://raiolanetworks.es
* @since 1.0.0
*
* @package Wp_Database_Tools
* @subpackage Wp_Database_Tools/includes
*/
/**
* Contains the functionalities related to database management.
*
* This class defines all code necessary to manage the database functionalities.
*
* @since 1.0.0
* @package Wp_Database_Tools
* @subpackage Wp_Database_Tools/includes
* @author Raiola Networks <info@raiolanetworks.es>
*/
class Wp_Database_Tools_Scanner {
/**
* The general data of database.
*
* @since 1.0.0
* @access protected
* @var array $data The general data of database.
*/
protected $path;
/**
* The license manager.
*
* @since 1.0.0
* @access protected
* @var array $data The general data of database.
*/
protected $license;
/**
* The license manager.
*
* @since 1.0.0
* @access protected
* @var array $data The general data of database.
*/
protected $plugins;
/**
* The license manager.
*
* @since 1.0.0
* @access protected
* @var array $data The general data of database.
*/
protected $themes;
public function __construct( $license, $plugins, $themes ) {
include_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wp-database-tools-logger.php';
$this->license = $license;
$this->plugins = $plugins->get_plugins();
$this->themes = $themes->get_themes();
}
/**
* Saves the current status of the scanner in a transient.
*
* @param string $status Current status of scanner error|active|success.
* @param string $message Message to return on sidebar.
* @since 1.0.0
*/
public function set_status_scanner( $status, $message ) {
$scanner_status_data = array(
'status' => $status,
'message' => $message,
'time' => date( 'Y-m-d H:i:s' ),
);
set_transient( WPDBT_PREFIX . 'scanner_last_status', wp_json_encode( $scanner_status_data ) );
set_transient( WPDBT_PREFIX . 'scanner_status', wp_json_encode( $scanner_status_data ) );
}
/**
* We check the global variables and create a request to wp-cron
* to check if it is working correctly.
*
* @since 1.0.1
*/
public function wpdt_test_cron_spawn() {
global $wp_version;
$cron_check = get_transient( WPDBT_PREFIX . 'cron_check' );
if ( 'success' === $cron_check || 'error' === $cron_check || 'warning' === $cron_check ) {
return true;
}
if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) {
$message = esc_html__( 'DISABLE_WP_CRON is disabled, the cron is not active', 'wp-database-tools' );
set_transient( WPDBT_PREFIX . 'cron_check', 'error', 600 );
set_transient( WPDBT_PREFIX . 'cron_check_date', gmdate( 'Y-m-d H:i:s' ), 600 );
set_transient( WPDBT_PREFIX . 'cron_check_message', $message, 600 );
}
if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
$message = esc_html__( 'ALTERNATE_WP_CRON is enabled, we cannot determine the functioning of the cron', 'wp-database-tools' );
set_transient( WPDBT_PREFIX . 'cron_check', 'warning', 600 );
set_transient( WPDBT_PREFIX . 'cron_check_date', gmdate( 'Y-m-d H:i:s' ), 600 );
set_transient( WPDBT_PREFIX . 'cron_check_message', $message, 600 );
}
$sslverify = version_compare( $wp_version, '4.0', '<' );
$doing_wp_cron = sprintf( '%.22F', microtime( true ) );
$cron_request = apply_filters(
'cron_request',
array(
'url' => site_url( 'wp-cron.php?doing_wp_cron=' . $doing_wp_cron ),
'key' => $doing_wp_cron,
'args' => array(
'timeout' => 3,
'blocking' => true,
'sslverify' => apply_filters( 'https_local_ssl_verify', $sslverify ),
),
)
);
$cron_request['args']['blocking'] = true;
$result = wp_remote_post( $cron_request['url'], $cron_request['args'] );
if ( wp_remote_retrieve_response_code( $result ) >= 300 ) {
Wp_Database_Tools_Logger::error( 'Error execute cronjob response code ' . wp_remote_retrieve_response_code( $result ) );
$message = sprintf( __( 'Error execute cronjob response code %s', 'wp-database-tools' ), wp_remote_retrieve_response_code( $result ) );
set_transient( WPDBT_PREFIX . 'cron_check', 'error', 600 );
set_transient( WPDBT_PREFIX . 'cron_check_date', gmdate( 'Y-m-d H:i:s' ), 600 );
set_transient( WPDBT_PREFIX . 'cron_check_message', $message, 600 );
} else {
Wp_Database_Tools_Logger::info( 'Success execute cronjob response' . json_encode( $result ) );
$message = esc_html__( 'The cronjob is working correctly', 'wp-database-tools' );
set_transient( WPDBT_PREFIX . 'cron_check', 'success', 1000 );
set_transient( WPDBT_PREFIX . 'cron_check_date', gmdate( 'Y-m-d H:i:s' ), 1000 );
set_transient( WPDBT_PREFIX . 'cron_check_message', $message, 1000 );
}
return true;
}
/**
* Method executed from WordPress cron.
*
* @since 1.0.0
* @author Patricia Álvarez <patriciaaf@raiolanetworks.es>
*/
public function cron_scanner( $tables, $options, $cronjobs, $transients ) {
Wp_Database_Tools_Logger::info( 'CRON SCANNER: start send data API' );
if ( $this->plugins == null ) {
Wp_Database_Tools_Logger::error( 'CRON SCANNER: plugins data null' );
}
if ( $this->themes == null ) {
Wp_Database_Tools_Logger::error( 'CRON SCANNER: themes data null' );
}
$this->set_status_scanner( 'active', __( 'Performing scanner', 'wp-database-tools' ) );
$tables = $tables['data'];
if ( null === $tables ) {
Wp_Database_Tools_Logger::error( 'CRON SCANNER: tables data null' );
} else {
$count_data = is_array( $tables ) ? count( $tables ) : 'null';
Wp_Database_Tools_Logger::info( 'CRON SCANNER: tables data ' . $count_data );
}
$options = $options['data'];
if ( null === $options ) {
Wp_Database_Tools_Logger::error( 'CRON SCANNER: options data null' );
} else {
$count_data = is_array( $options ) ? count( $options ) : 'null';
Wp_Database_Tools_Logger::info( 'CRON SCANNER: options data ' . $count_data );
}
$transients = $transients['data'];
if ( null === $transients ) {
Wp_Database_Tools_Logger::error( 'CRON SCANNER: transients data null' );
} else {
$count_data = is_array( $transients ) ? count( $transients ) : 'null';
Wp_Database_Tools_Logger::info( 'CRON SCANNER: transients data ' . $count_data );
}
$cronjobs = $cronjobs['data'];
if ( null === $cronjobs ) {
Wp_Database_Tools_Logger::error( 'CRON SCANNER: cronjobs data null' );
} else {
$count_data = is_array( $cronjobs ) ? count( $cronjobs ) : 'null';
Wp_Database_Tools_Logger::info( 'CRON SCANNER: cronjobs data ' . $count_data );
}
$domain = ( WPDBT_ENV === 'DEV' ) ? WPDBT_DEV_DOMAIN : WPDBT_PROD_DOMAIN;
$url = $domain . '/api/v2/scanner';
$args = array(
'timeout' => 1800,
'headers' => array(
'content-type' => 'application/json',
),
'body' => wp_json_encode(
array(
'tables' => wp_json_encode( $tables ),
'options' => wp_json_encode( $options ),
'transients' => wp_json_encode( $transients ),
'cronjobs' => wp_json_encode( $cronjobs ),
'plugins' => wp_json_encode( $this->plugins ),
'themes' => wp_json_encode( $this->themes ),
'license' => $this->license->get_license_key_encrypt() ?? 'invalid',
'license_url' => $this->license->get_license_url(),
'comercial' => 'true',
'version' => WPDBT_VERSION,
'force_execute_scanner' => get_option( WPDBT_PREFIX . 'force_execute_scanner' ) ?? 'NO',
)
),
);
$this->set_status_scanner( 'active', __( 'Getting results', 'wp-database-tools' ) );
$response = wp_remote_post( $url, $args );
$code = wp_remote_retrieve_response_code( $response );
// Error check.
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
$this->set_status_scanner( 'error', $response->get_error_message() );
Wp_Database_Tools_Logger::error( 'SCANNER: something went wrong: ' . $error_message );
} else {
// format response.
$response_data = wp_remote_retrieve_body( $response );
$response_data = json_decode( $response_data, true );
$PATH_API_DATA = plugin_dir_path( __DIR__ ) . 'data/api/';
$KEYS = array( 'tables', 'options', 'cronjobs', 'plugins', 'themes', 'cores', 'marketplaces', 'authors', 'transients' );
if ( isset( $response_data['license'] ) && $code == 200 ) {
// update the execute scanner to NO.
update_option( WPDBT_PREFIX . 'force_execute_scanner', 'NO' );
if ( $response_data['license'] === 'valid' ) {
$this->license->set_license_key( $response_data['license'] );
foreach ( $KEYS as $KEY ) {
if ( $response_data['data'][ $KEY ] ) {
file_put_contents( $PATH_API_DATA . $KEY . '.json', wp_json_encode( mb_convert_encoding( $response_data['data'][ $KEY ], 'UTF-8' ) ) );
}
}
$this->set_status_scanner( 'success', __( 'Scanner finished', 'wp-database-tools' ) );
} else {
// Set data empty for invalid license.
$this->license->set_license_key( $response_data['license'] );
// Save empty data.
foreach ( $KEYS as $KEY ) {
if ( $response_data['data'][ $KEY ] ) {
file_put_contents( $PATH_API_DATA . $KEY . '.json', '' );
}
}
Wp_Database_Tools_Logger::error( 'SCANNER: licencia no válida' );
$this->set_status_scanner( 'error', __( 'Invalid license', 'wp-database-tools' ) );
}
} else {
$error_message = __( 'Scanner fault', 'wp-database-tools' );
if ( isset( $response_data['message'] ) ) {
$error_message = $response_data['message'];
} elseif ( isset( $response['response']['message'] ) ) {
$error_message = $response['response']['message'];
}
Wp_Database_Tools_Logger::error( json_encode( $response ) );
$this->set_status_scanner( 'error', $error_message );
}
}
}
}