hooks(); } return self::$instance; } /** * Register WordPress hooks. * * @return void */ private function hooks() { add_action( 'rest_api_init', array( new RestController(), 'register_routes' ) ); if ( is_admin() ) { ( new AdminPage() )->hooks(); } // Step engine continuation: loopback (no cookies) and cron safety net. add_action( 'wp_ajax_sitezip_run', array( $this, 'handle_loopback' ) ); add_action( 'admin_notices', array( '\\SiteZip\\Restore\\HealthCheck', 'notice' ) ); add_action( 'wp_ajax_sitezip_resume', array( '\\SiteZip\\Security\\ResumeLogin', 'handle' ) ); add_action( 'wp_ajax_nopriv_sitezip_resume', array( '\\SiteZip\\Security\\ResumeLogin', 'handle' ) ); add_action( 'wp_ajax_nopriv_sitezip_run', array( $this, 'handle_loopback' ) ); add_action( 'wp_ajax_sitezip_ping', array( __CLASS__, 'pong' ) ); add_action( 'wp_ajax_nopriv_sitezip_ping', array( __CLASS__, 'pong' ) ); add_action( 'sitezip_run_job', array( $this, 'handle_cron' ), 10, 1 ); } /** * Continue a job from a non-blocking loopback request. * * Authorised by a per-job random runner token rather than a user * capability, because the loopback request carries no cookies. * * @return void */ public function handle_loopback() { // phpcs:disable WordPress.Security.NonceVerification.Missing -- authorised by the per-job runner token below. $job_id = isset( $_POST['job'] ) ? absint( $_POST['job'] ) : 0; $token = isset( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : ''; // phpcs:enable WordPress.Security.NonceVerification.Missing $store = new JobStore(); $job = $store->get( $job_id ); if ( ! $job || '' === $token || ! hash_equals( (string) $job['runner_token'], $token ) ) { status_header( 403 ); wp_die( '', '', array( 'response' => 403 ) ); } ( new JobRunner() )->run( $job_id ); wp_die( '', '', array( 'response' => 200 ) ); } /** * Answer the loopback check (see JobRunner::loopback_ok()). * * @return void */ public static function pong() { wp_die( 'sitezip-pong', '', array( 'response' => 200 ) ); } /** * Cron safety net for a stalled job. * * @param int $job_id Job identifier. * @return void */ public function handle_cron( $job_id ) { ( new JobRunner() )->run( absint( $job_id ) ); } /** * Create tables on activation. * * @return void */ public static function activate() { JobStore::install(); } /** * On deactivation, stop any running job, and delete everything if the * administrator chose "Deactivate and delete all data". * * @return void */ public static function deactivate() { wp_unschedule_hook( 'sitezip_run_job' ); // phpcs:disable WordPress.Security.NonceVerification.Recommended -- verified immediately below. $purge = isset( $_GET['sitezip_purge'], $_GET['sitezip_nonce'] ) && '1' === $_GET['sitezip_purge'] && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['sitezip_nonce'] ) ), self::PURGE_NONCE ) && Capabilities::current_user_can(); // phpcs:enable WordPress.Security.NonceVerification.Recommended if ( $purge ) { self::purge(); return; } // Keep the data, but do not leave a half-finished job behind. ( new JobRunner() )->cancel(); } /** * Delete every backup file, the job table, options and leftovers. * * @return void */ public static function purge() { global $wpdb; ( new JobRunner() )->cancel(); RestoreRunner::drop_temp_tables(); // phpcs:disable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL, PluginCheck.Security.DirectDB -- table name built from the trusted prefix. $wpdb->query( 'DROP TABLE IF EXISTS ' . JobStore::table() ); $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'sitezip\\_%' OR option_name LIKE '\\_transient\\_sitezip\\_%' OR option_name LIKE '\\_transient\\_timeout\\_sitezip\\_%'" ); // phpcs:enable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL wp_cache_flush(); self::delete_backups_folder(); } /** * Remove the backups folder and everything in it. * * @return void */ private static function delete_backups_folder() { $dir = PathGuard::find_backups_dir(); if ( ! $dir ) { return; } $items = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( $dir, \FilesystemIterator::SKIP_DOTS ), \RecursiveIteratorIterator::CHILD_FIRST ); foreach ( $items as $item ) { if ( $item->isDir() && ! $item->isLink() ) { @rmdir( $item->getPathname() ); } else { @unlink( $item->getPathname() ); } } @rmdir( $dir ); } }