false, 'posts' => array(), 'urls' => array(), 'deletes' => array(), 'reasons' => array(), ) ); } /* ------------------------------------------------------------------ * * Starting jobs * ------------------------------------------------------------------ */ /** * Queue a changed post. Its affected URLs are computed when the push runs, * so terms/meta saved after the status transition (block editor) are included. */ public static function enqueue_post( $post_id, array $deletes = array(), $reason = '' ) { $pending = self::get_pending(); $pending['posts'][ $post_id ] = true; update_option( self::PENDING_OPTION, $pending, false ); self::enqueue_changes( array(), $deletes, $reason ); } /** Queue changed URLs / removed files. They're pushed after the configured delay. */ public static function enqueue_changes( array $urls, array $deletes = array(), $reason = '', $full = false ) { $pending = self::get_pending(); foreach ( $urls as $u ) { $pending['urls'][ $u ] = true; } foreach ( $deletes as $d ) { $pending['deletes'][ $d ] = true; } if ( $reason ) { $pending['reasons'][] = $reason; $pending['reasons'] = array_slice( array_unique( $pending['reasons'] ), -20 ); } if ( $full ) { $pending['full'] = true; } update_option( self::PENDING_OPTION, $pending, false ); if ( ! wp_next_scheduled( 'cfsp_run_pending' ) ) { wp_schedule_single_event( time() + (int) CFSP_Settings::get( 'push_delay' ), 'cfsp_run_pending' ); } } public static function start_from_pending() { if ( self::is_active() ) { return; // Picked up automatically when the current job finishes. } $pending = self::get_pending(); delete_option( self::PENDING_OPTION ); if ( $pending['full'] ) { self::start_full( 'Auto: ' . implode( ', ', $pending['reasons'] ) ); return; } foreach ( array_keys( $pending['posts'] ) as $post_id ) { foreach ( CFSP_URLs::for_post( $post_id ) as $url ) { $pending['urls'][ $url ] = true; } CFSP_Auto_Push::remember_file( $post_id ); } if ( ! $pending['urls'] && ! $pending['deletes'] ) { return; } $urls = array_keys( $pending['urls'] ); self::create( array( 'type' => 'partial', 'phase' => 'crawl', 'queue' => $urls, 'roots' => $urls, 'deletes' => array_keys( $pending['deletes'] ), 'reason' => 'Auto: ' . implode( ', ', $pending['reasons'] ), ) ); } public static function start_full( $reason = 'Manual full export' ) { if ( self::is_active() ) { $pending = self::get_pending(); $pending['full'] = true; update_option( self::PENDING_OPTION, $pending, false ); return false; } delete_option( self::PENDING_OPTION ); self::create( array( 'type' => 'full', 'phase' => 'crawl', 'queue' => CFSP_URLs::full(), 'reason' => $reason, ) ); return true; } /** * Export and push only these pages (e.g. after editing the home template). * Accepts WordPress URLs, static-site URLs or paths; anything on another host is ignored. * * @return int|WP_Error Number of pages queued. */ public static function start_urls( array $lines ) { if ( self::is_active() ) { return new WP_Error( 'cfsp_busy', 'A job is already running. Try again when it has finished.' ); } $home_host = strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) ); $dest = CFSP_Settings::destination_url(); $dest_host = $dest ? strtolower( (string) wp_parse_url( $dest, PHP_URL_HOST ) ) : ''; $dest_path = $dest ? rtrim( (string) wp_parse_url( $dest, PHP_URL_PATH ), '/' ) : ''; $urls = array(); $want_404 = false; foreach ( $lines as $line ) { $line = trim( (string) $line ); if ( '' === $line ) { continue; } if ( preg_match( '#^(?:https?://[^/]+)?/?404(?:\.html|/)?$#i', $line ) ) { $want_404 = true; continue; } // Static file names back to their WordPress address: ".../feed.xml" is WordPress's ".../feed/". $line = preg_replace( '#(^|/)feed\.xml$#i', '$1feed/', $line ); if ( preg_match( '#^https?://#i', $line ) ) { $host = strtolower( (string) wp_parse_url( $line, PHP_URL_HOST ) ); $path = (string) wp_parse_url( $line, PHP_URL_PATH ); if ( $host === $dest_host && $host !== $home_host ) { // A static-site address: same path on WordPress. if ( '' !== $dest_path && 0 === strpos( $path, $dest_path ) ) { $path = substr( $path, strlen( $dest_path ) ); } $line = home_url( '/' . ltrim( $path, '/' ) ); } elseif ( $host !== $home_host ) { continue; } } else { $line = home_url( '/' . ltrim( $line, '/' ) ); } $url = CFSP_Exporter::normalize( $line ); if ( $url && ! CFSP_Exporter::is_excluded( $url ) ) { $urls[ $url ] = true; } } $urls = array_slice( array_keys( $urls ), 0, 500 ); if ( ! $urls && ! $want_404 ) { return new WP_Error( 'cfsp_no_urls', 'None of those addresses belong to this site.' ); } $labels = $urls; if ( $want_404 ) { $labels[] = '404 page'; } self::create( array( 'type' => 'partial', 'phase' => 'crawl', 'queue' => $urls, 'roots' => $urls, 'force_404' => $want_404, 'manual' => true, 'reason' => 1 === count( $labels ) ? 'Manual: ' . $labels[0] : sprintf( 'Manual: %d pages', count( $labels ) ), ) ); return count( $labels ); } public static function start_deploy_only() { if ( self::is_active() ) { return false; } self::create( array( 'type' => 'deploy', 'phase' => 'deploy_prepare', 'queue' => array(), 'reason' => 'Manual deploy', ) ); return true; } public static function cancel() { $job = self::get(); if ( $job && self::is_active( $job ) ) { $job['phase'] = 'cancelled'; $job['message'] = 'Cancelled by user.'; self::save( $job ); CFSP_Logger::log( 'Job cancelled.' ); } self::release_lock(); } private static function create( array $args ) { $job = array_merge( array( 'id' => wp_generate_password( 8, false, false ), 'queue' => array(), 'seen' => array(), 'roots' => array(), 'deletes' => array(), 'written' => array(), 'processed' => 0, 'errors' => 0, 'retries' => 0, 'started' => time(), 'message' => '', ), $args ); foreach ( $job['queue'] as $u ) { $job['seen'][ $u ] = 1; } $job['total'] = count( $job['queue'] ); self::save( $job ); CFSP_Logger::log( sprintf( 'Started %s job (%d URLs). %s', $job['type'], $job['total'], $job['reason'] ) ); self::spawn(); } /* ------------------------------------------------------------------ * * Running * ------------------------------------------------------------------ */ /** Fire a non-blocking request to process the next batch. */ public static function spawn() { wp_remote_post( admin_url( 'admin-ajax.php' ), CFSP_Exporter::loopback_args( array( 'blocking' => false, 'timeout' => 0.01, 'sslverify' => false, 'body' => array( 'action' => 'cfsp_worker', 'key' => get_option( 'cfsp_worker_key' ), ), ) ) ); } public static function handle_worker() { // Background worker called by this server itself (not a form): it is authenticated with the // site's secret worker key instead of a nonce, because nonces are tied to a logged-in user. $key = isset( $_POST['key'] ) ? sanitize_text_field( wp_unslash( $_POST['key'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing if ( ! $key || ! hash_equals( (string) get_option( 'cfsp_worker_key' ), $key ) ) { wp_die( '', '', array( 'response' => 403 ) ); } ignore_user_abort( true ); self::run_batch(); wp_die( '', '', array( 'response' => 200 ) ); } /** Scheduled posts whose publish time has passed but are still waiting (WP-Cron didn't run). */ public static function missed_posts( $limit = 20 ) { return array_map( 'intval', get_posts( array( 'post_type' => 'any', 'post_status' => 'future', 'date_query' => array( array( 'column' => 'post_date_gmt', 'before' => gmdate( 'Y-m-d H:i:s' ), 'inclusive' => true, ), ), 'orderby' => 'date', 'order' => 'ASC', 'numberposts' => (int) $limit, 'fields' => 'ids', ) ) ); } /** * Publish scheduled posts that WordPress missed. Once the public site is static, the WordPress * install gets almost no traffic, so WP-Cron (which only runs on page views) can fall behind. * Throttled to once a minute; publishing triggers the normal automatic push. */ public static function publish_missed() { if ( ! CFSP_Settings::get( 'rescue_missed' ) || get_transient( 'cfsp_missed_check' ) ) { return 0; } set_transient( 'cfsp_missed_check', 1, MINUTE_IN_SECONDS ); $count = 0; foreach ( self::missed_posts() as $post_id ) { wp_publish_post( $post_id ); if ( 'publish' === get_post_status( $post_id ) ) { CFSP_Logger::log( sprintf( 'Published missed scheduled post "%s".', get_the_title( $post_id ) ) ); $count++; } } return $count; } /** Cron fallback: keeps jobs moving if loopback requests are blocked. */ public static function watchdog() { self::publish_missed(); $job = self::get(); if ( $job && self::is_active( $job ) && $job['updated'] < time() - 45 && ( $job['wait_until'] ?? 0 ) <= time() ) { self::run_batch(); } // Pending changes whose single event got lost. $pending = self::get_pending(); if ( ( $pending['urls'] || $pending['posts'] || $pending['deletes'] || $pending['full'] ) && ! wp_next_scheduled( 'cfsp_run_pending' ) && ! self::is_active() ) { self::start_from_pending(); } } private static function acquire_lock( $ttl ) { global $wpdb; $now = time(); // An atomic lock: INSERT IGNORE succeeds for exactly one request, which add_option() can't guarantee. // phpcs:disable WordPress.DB.DirectDatabaseQuery $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name = %s AND CAST(option_value AS UNSIGNED) < %d", self::LOCK, $now ) ); $inserted = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->options} (option_name, option_value, autoload) VALUES (%s, %s, 'no')", self::LOCK, (string) ( $now + $ttl ) ) ); // phpcs:enable wp_cache_delete( self::LOCK, 'options' ); return 1 === (int) $inserted; } private static function release_lock() { global $wpdb; $wpdb->delete( $wpdb->options, array( 'option_name' => self::LOCK ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Paired with acquire_lock(). wp_cache_delete( self::LOCK, 'options' ); } /** * Process one time-boxed batch of the current job. * * @param bool $chain Spawn the next batch when done (false for CLI, which loops itself). */ public static function run_batch( $chain = true ) { $seconds = (int) CFSP_Settings::get( 'batch_seconds' ); if ( ! self::acquire_lock( $seconds + 120 ) ) { return false; } if ( function_exists( 'set_time_limit' ) ) { @set_time_limit( $seconds + 120 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors, Squiz.PHP.DiscouragedFunctions.Discouraged -- A batch runs up to "Batch length" seconds plus uploads; hosts may disable the function, hence the check and @. } wp_raise_memory_limit( 'admin' ); $job = self::get(); if ( ! $job || ! self::is_active( $job ) ) { self::release_lock(); return false; } if ( ! empty( $job['wait_until'] ) && $job['wait_until'] > time() ) { self::release_lock(); // Rate limited; the watchdog resumes it later. return false; } unset( $job['wait_until'] ); $deadline = time() + $seconds; try { switch ( $job['phase'] ) { case 'crawl': $job = self::crawl( $job, $deadline ); break; case 'deploy_prepare': case 'deploy_upload': case 'deploy_finalize': $job = self::deploy_step( $job, $deadline ); break; } } catch ( Throwable $e ) { $job['phase'] = 'failed'; $job['message'] = $e->getMessage(); CFSP_Logger::error( 'Job failed: ' . $e->getMessage() ); } // The job may have been cancelled while this batch was running, or cancelled and replaced by a // new one: then leave the stored job alone, so it isn't marked as running again. $current = self::get(); if ( $current && ( $current['id'] !== $job['id'] || 'cancelled' === $current['phase'] ) ) { self::release_lock(); return false; } self::save( $job ); self::release_lock(); if ( self::is_active( $job ) ) { if ( $chain ) { self::spawn(); } } else { self::after_finish( $job ); } return true; } private static function crawl( $job, $deadline ) { // Remove files for deleted / unpublished / renamed content first. foreach ( $job['deletes'] as $file ) { if ( CFSP_Exporter::delete_file( $file ) ) { CFSP_Logger::log( 'Removed ' . $file ); } } $job['deletes'] = array(); $parallel = max( 1, (int) CFSP_Settings::get( 'fetch_concurrency' ) ); // Addresses the user typed are never deleted on a 404 or redirect (see CFSP_Exporter::$protect). CFSP_Exporter::$protect = ! empty( $job['manual'] ) ? array_fill_keys( (array) $job['roots'], 1 ) : array(); // Related lists are shared between posts with the same terms for the whole job. CFSP_Related::$job = (string) $job['id']; $batch_start = microtime( true ); while ( $job['queue'] && time() < $deadline ) { $batch = array_splice( $job['queue'], 0, $parallel ); $results = CFSP_Exporter::export_many( $batch ); foreach ( $batch as $i => $url ) { $result = $results[ $i ]; $job['processed']++; if ( ! empty( $result['blocked'] ) ) { // Every other page would fail the same way; stop before anything is pruned or pushed. throw new RuntimeException( esc_html( $result['error'] ) ); } if ( ! $result['ok'] ) { $job['errors']++; CFSP_Logger::error( sprintf( 'Failed %s: %s', $url, $result['error'] ) ); // Keep the previously exported copy rather than pruning it on a temporary error. $file = CFSP_Exporter::url_to_file( $url ); if ( $file ) { $job['written'][ $file ] = 1; } continue; } if ( $result['file'] ) { $job['written'][ $result['file'] ] = 1; } if ( $result['removed'] && 'partial' === $job['type'] ) { CFSP_Logger::log( 'Removed (404) ' . $url ); } foreach ( $result['links'] as $link ) { if ( isset( $job['seen'][ $link ] ) ) { continue; } $follow = 'full' === $job['type'] ? self::should_follow_full( $link, $url ) : self::should_follow_partial( $link, $url, $job ); if ( $follow ) { $job['seen'][ $link ] = 1; $job['queue'][] = $link; $job['total']++; } } } } $timing = isset( $job['timing'] ) ? $job['timing'] : array(); $job['timing'] = array( 'batches' => ( $timing['batches'] ?? 0 ) + 1, 'crawl' => ( $timing['crawl'] ?? 0 ) + ( microtime( true ) - $batch_start ), 'fetch' => ( $timing['fetch'] ?? 0 ) + CFSP_Exporter::$timing['fetch'], 'export' => ( $timing['export'] ?? 0 ) + CFSP_Exporter::$timing['total'], 'related' => ( $timing['related'] ?? 0 ) + CFSP_Related::$seconds, ); CFSP_Exporter::$timing = array( 'fetch' => 0.0, 'total' => 0.0, ); CFSP_Related::$seconds = 0.0; if ( ! $job['queue'] ) { $want_404 = CFSP_Settings::crawler( 'not_found' ) || ! CFSP_Settings::get( 'enhanced_crawl' ); if ( 'full' === $job['type'] ) { if ( $want_404 && CFSP_Exporter::export_404() ) { $job['written']['404.html'] = 1; } $pruned = self::prune( $job['written'] ); if ( $pruned ) { CFSP_Logger::log( sprintf( 'Pruned %d stale file(s).', $pruned ) ); } } elseif ( ! empty( $job['force_404'] ) || ( $want_404 && ! CFSP_Exporter::file_exists( '404.html' ) ) ) { CFSP_Exporter::export_404(); if ( ! empty( $job['force_404'] ) ) { CFSP_Logger::log( 'Exported the 404 page.' ); } } $job['written'] = array(); $job['seen'] = array(); // Static search index (after pruning, which doesn't know about it). $indexed = CFSP_Search::build(); CFSP_Feeds::build(); if ( $indexed ) { CFSP_Logger::log( sprintf( 'Search index: %d entries.', $indexed ) ); } $llms = CFSP_LLMs::build(); if ( $llms ) { CFSP_Logger::log( sprintf( 'llms.txt: %d links.', $llms ) ); } CFSP_Logger::log( sprintf( 'Export finished: %d URLs, %d errors.', $job['processed'], $job['errors'] ) ); if ( ! empty( $job['timing'] ) && $job['processed'] > 0 ) { $t = $job['timing']; CFSP_Logger::log( sprintf( 'Export timing: %1$s in %2$d batches. Waiting for WordPress to render pages %3$s, processing pages %4$s (related posts %5$s), job bookkeeping %6$s.', self::duration( $t['crawl'] ), $t['batches'], self::duration( $t['fetch'] ), self::duration( max( 0, $t['export'] - $t['fetch'] ) ), self::duration( $t['related'] ), self::duration( max( 0, $t['crawl'] - $t['export'] ) ) ) ); } if ( CFSP_Settings::has_remote() ) { $job['phase'] = 'deploy_prepare'; } else { $job['phase'] = 'done'; $job['message'] = 'local' === CFSP_Settings::get( 'target' ) ? 'Export complete. Download the ZIP from Tools.' : 'Export complete. No deploy target is configured, so nothing was pushed.'; } } return $job; } /** "1 h 12 min", "4 min 5 s", "12 s" for the timing summary. */ private static function duration( $seconds ) { $seconds = (int) round( $seconds ); if ( $seconds >= HOUR_IN_SECONDS ) { return sprintf( '%d h %d min', intdiv( $seconds, HOUR_IN_SECONDS ), intdiv( $seconds % HOUR_IN_SECONDS, MINUTE_IN_SECONDS ) ); } if ( $seconds >= MINUTE_IN_SECONDS ) { return sprintf( '%d min %d s', intdiv( $seconds, MINUTE_IN_SECONDS ), $seconds % MINUTE_IN_SECONDS ); } return sprintf( '%d s', $seconds ); } /** Full export: follow every link, or only assets / sitemaps / pagination when link following is off. */ private static function should_follow_full( $link, $from ) { if ( CFSP_Settings::get( 'follow_links' ) || CFSP_Exporter::is_asset( $link ) ) { return true; } return CFSP_Settings::crawler( 'pagination' ) && preg_match( '#/page/\d+/?$#', $link ); } /** In partial mode only follow new assets, pagination of changed listings and sitemap children. */ private static function should_follow_partial( $link, $from, $job ) { if ( CFSP_Exporter::is_asset( $link ) ) { if ( 'xml' === CFSP_Exporter::extension( $link ) && 'xml' === CFSP_Exporter::extension( $from ) ) { return true; // Sub-sitemaps listed in the sitemap index. } $file = CFSP_Exporter::url_to_file( $link, false ); return $file && ! CFSP_Exporter::file_exists( $file ); } // Automatic pushes may stop at a page number; pushes of specific pages refresh every page. $depth = empty( $job['manual'] ) ? (int) CFSP_Settings::get( 'page_depth' ) : 0; foreach ( $job['roots'] as $root ) { $root = trailingslashit( preg_replace( '#/page/\d+/?$#', '/', $root ) ); if ( 0 === strpos( $link, $root ) && preg_match( '#^page/(\d+)/?$#', substr( $link, strlen( $root ) ), $m ) ) { return $depth < 1 || (int) $m[1] <= $depth; } } return false; } /** Delete files that weren't produced by the full export (deleted posts, old slugs, ...). */ private static function prune( array $written ) { $root = CFSP_Settings::export_dir(); $count = 0; $it = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $root, FilesystemIterator::SKIP_DOTS ) ); $stale = array(); foreach ( $it as $file ) { if ( $file->isFile() ) { $rel = ltrim( str_replace( '\\', '/', substr( $file->getPathname(), strlen( $root ) ) ), '/' ); if ( ! isset( $written[ $rel ] ) ) { $stale[] = $rel; } } } foreach ( $stale as $rel ) { CFSP_Exporter::delete_file( $rel ); $count++; } return $count; } private static function deploy_step( $job, $deadline ) { if ( 'deploy_prepare' === $job['phase'] ) { $result = CFSP_Deployer::prepare( $job ); if ( is_wp_error( $result ) ) { return self::deploy_error( $job, $result ); } CFSP_Logger::log( sprintf( '%s: %d files in site, %d new/changed/deleted to send.', CFSP_Deployer::target_label(), $result['files'], $result['missing'] ) ); $job['upload_total'] = $result['missing']; $job['phase'] = 'deploy_upload'; $job['retries'] = 0; } if ( 'deploy_upload' === $job['phase'] && time() < $deadline ) { $remaining = CFSP_Deployer::upload( $deadline ); if ( is_wp_error( $remaining ) ) { return self::deploy_error( $job, $remaining ); } $job['upload_remaining'] = $remaining; if ( 0 === $remaining ) { $job['phase'] = 'deploy_finalize'; $job['retries'] = 0; } } if ( 'deploy_finalize' === $job['phase'] && time() < $deadline ) { $result = CFSP_Deployer::finalize(); if ( is_wp_error( $result ) ) { return self::deploy_error( $job, $result ); } $url = $result['url'] ?? ''; if ( ! empty( $result['nochange'] ) ) { CFSP_Logger::log( 'No changes, nothing to push.' ); $job['phase'] = 'done'; $job['message'] = 'Already up to date.'; return $job; } CFSP_Logger::log( sprintf( 'Pushed to %s: %s', CFSP_Deployer::target_label(), $url ) ); update_option( self::LAST_OPTION, array( 'time' => time(), 'url' => $url, 'id' => $result['id'] ?? '', ), false ); $job['phase'] = 'done'; $job['message'] = 'Pushed: ' . $url; } return $job; } private static function deploy_error( $job, WP_Error $error ) { if ( 'cfsp_rate_limited' === $error->get_error_code() ) { $wait = (int) ( $error->get_error_data()['wait'] ?? 60 ); $job['wait_until'] = time() + $wait; CFSP_Logger::log( sprintf( 'Rate limited by %s. Resuming in %d s.', CFSP_Deployer::target_label(), $wait ) ); return $job; } $job['retries']++; if ( 'cfsp_restart' === $error->get_error_code() && $job['retries'] < 3 ) { CFSP_Logger::log( $error->get_error_message() ); $job['phase'] = 'deploy_prepare'; return $job; } CFSP_Logger::error( sprintf( 'Deploy error (attempt %d/3): %s', $job['retries'], $error->get_error_message() ) ); if ( $job['retries'] >= 3 ) { $job['phase'] = 'failed'; $job['message'] = $error->get_error_message(); } return $job; } private static function after_finish( $job ) { self::record_history( $job ); self::notify( $job ); if ( 'failed' === $job['phase'] ) { do_action( 'cfsp_job_failed', $job ); } elseif ( 'done' === $job['phase'] ) { do_action( 'cfsp_job_done', $job ); } // Changes that came in while we were busy. $pending = self::get_pending(); if ( $pending['full'] || $pending['posts'] || $pending['urls'] || $pending['deletes'] ) { wp_clear_scheduled_hook( 'cfsp_run_pending' ); self::start_from_pending(); } } /* ------------------------------------------------------------------ * * History, notifications, schedule * ------------------------------------------------------------------ */ const HISTORY_OPTION = 'cfsp_history'; public static function history() { $h = get_option( self::HISTORY_OPTION, array() ); return is_array( $h ) ? $h : array(); } private static function record_history( $job ) { $history = self::history(); $last = get_option( self::LAST_OPTION ); $history[] = array( 'time' => time(), 'type' => $job['type'], 'status' => $job['phase'], 'reason' => $job['reason'] ?? '', 'urls' => (int) $job['processed'], 'errors' => (int) $job['errors'], 'duration' => time() - (int) $job['started'], 'message' => $job['message'], 'url' => ( 'done' === $job['phase'] && is_array( $last ) && $last['time'] >= $job['started'] ) ? $last['url'] : '', ); update_option( self::HISTORY_OPTION, array_slice( $history, -30 ), false ); } private static function notify( $job ) { $failed = 'failed' === $job['phase']; if ( ! ( $failed ? CFSP_Settings::get( 'notify_failure' ) : ( 'done' === $job['phase'] && CFSP_Settings::get( 'notify_success' ) ) ) ) { return; } $to = CFSP_Settings::get( 'notify_email' ) ? CFSP_Settings::get( 'notify_email' ) : get_option( 'admin_email' ); $subject = sprintf( '[%s] Static push %s', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), $failed ? 'FAILED' : 'completed' ); $body = sprintf( "%s job %s.\n\nReason: %s\nURLs exported: %d (errors: %d)\nMessage: %s\n\nActivity log: %s", ucfirst( $job['type'] ), $failed ? 'failed' : 'finished', $job['reason'] ?? '', $job['processed'], $job['errors'], $job['message'], admin_url( 'admin.php?page=static-push#/activity' ) ); // No em dashes in notifications, even from post titles or API errors (U+2014 shown as ", "). $plain = function ( $text ) { return preg_replace( '/\s*\x{2014}\s*/u', ', ', $text ); }; wp_mail( $to, $plain( $subject ), $plain( $body ) ); } public static function scheduled_full() { self::start_full( 'Scheduled rebuild' ); } /** Keep the scheduled full rebuild in sync with Automation → Scheduled rebuild. */ public static function reschedule() { $recurrence = CFSP_Settings::get( 'schedule' ); $current = wp_get_schedule( 'cfsp_scheduled_full' ); if ( 'off' === $recurrence ) { wp_clear_scheduled_hook( 'cfsp_scheduled_full' ); } elseif ( $current !== $recurrence ) { wp_clear_scheduled_hook( 'cfsp_scheduled_full' ); wp_schedule_event( time() + HOUR_IN_SECONDS, $recurrence, 'cfsp_scheduled_full' ); } } /** Debug helpers. */ public static function force_unlock() { self::release_lock(); } }