[...], 'time' => int ] */ public static function get( $fresh = false ) { $cached = $fresh ? false : get_transient( self::CACHE ); if ( is_array( $cached ) && isset( $cached['groups'] ) ) { return $cached; } $result = array( 'groups' => self::run(), 'time' => time(), ); set_transient( self::CACHE, $result, HOUR_IN_SECONDS ); return $result; } public static function reset() { delete_transient( self::CACHE ); } /** * @return array[] Groups: [ 'title' => ..., 'checks' => [ [ label, status ok|warn|fail|info, detail ] ] ] */ public static function run() { return array( array( 'title' => 'WordPress', 'checks' => self::wordpress() ), array( 'title' => 'Server', 'checks' => self::server() ), array( 'title' => 'Background processing', 'checks' => self::background() ), array( 'title' => 'Privacy', 'checks' => CFSP_Private::diagnostics() ), array( 'title' => 'Deploy', 'checks' => self::deploy() ), ); } public static function check( $label, $status, $detail = '' ) { return array( 'label' => $label, 'status' => $status, 'detail' => $detail ); } private static function wordpress() { global $wp_version; $out = array(); $out[] = self::check( 'WordPress version', version_compare( $wp_version, '5.8', '>=' ) ? 'ok' : 'fail', $wp_version ); $out[] = get_option( 'permalink_structure' ) ? self::check( 'Pretty permalinks', 'ok', get_option( 'permalink_structure' ) ) : self::check( 'Pretty permalinks', 'fail', 'Plain permalinks (?p=123) can\'t be turned into static files. Settings → Permalinks → choose "Post name".' ); $out[] = self::check( 'Site URL', 'info', home_url() ); $out[] = CFSP_Settings::destination_url() || 'relative' === CFSP_Settings::get( 'url_mode' ) ? self::check( 'Static site URL', 'ok', 'relative' === CFSP_Settings::get( 'url_mode' ) ? 'Relative URLs (' . CFSP_Settings::get( 'relative_path' ) . ')' : CFSP_Settings::destination_url() ) : self::check( 'Static site URL', 'warn', 'Not set. Go to Export → Link rewriting.' ); if ( CFSP_Settings::destination_url() && wp_parse_url( CFSP_Settings::destination_url(), PHP_URL_HOST ) === wp_parse_url( home_url(), PHP_URL_HOST ) ) { $out[] = self::check( 'Separate hostnames', 'warn', 'WordPress and the static site use the same hostname. Move WordPress to its own subdomain (e.g. wp.example.com).' ); } $out[] = self::check( 'Search engine visibility', '1' === (string) get_option( 'blog_public' ) ? 'info' : 'warn', '1' === (string) get_option( 'blog_public' ) ? 'Visible: robots.txt allows indexing.' : 'Discouraged: the exported robots.txt / meta tags will block search engines.' ); return $out; } private static function server() { $out = array(); $out[] = self::check( 'PHP version', version_compare( PHP_VERSION, '7.4', '>=' ) ? 'ok' : 'fail', PHP_VERSION ); foreach ( array( 'curl' => 'HTTP requests', 'json' => 'API calls', 'mbstring' => 'UTF-8 checks', 'zip' => 'ZIP download (Tools)' ) as $ext => $why ) { $out[] = self::check( 'PHP extension: ' . $ext, extension_loaded( $ext ) ? 'ok' : ( 'zip' === $ext || 'mbstring' === $ext ? 'warn' : 'fail' ), extension_loaded( $ext ) ? 'Loaded' : 'Missing. Needed for ' . $why ); } $memory = wp_convert_hr_to_bytes( ini_get( 'memory_limit' ) ); $out[] = self::check( 'Memory limit', ( $memory < 0 || $memory >= 128 * MB_IN_BYTES ) ? 'ok' : 'warn', ini_get( 'memory_limit' ) ); $max = (int) ini_get( 'max_execution_time' ); $out[] = self::check( 'Max execution time', ( 0 === $max || $max >= (int) CFSP_Settings::get( 'batch_seconds' ) + 15 ) ? 'ok' : 'warn', $max ? $max . 's (batch length is ' . CFSP_Settings::get( 'batch_seconds' ) . 's)' : 'Unlimited' ); $dir = CFSP_Settings::export_dir(); wp_mkdir_p( $dir ); $out[] = self::check( 'Export directory writable', wp_is_writable( $dir ) ? 'ok' : 'fail', $dir ); $free = function_exists( 'disk_free_space' ) ? @disk_free_space( $dir ) : false; // phpcs:ignore WordPress.PHP.NoSilencedErrors if ( false !== $free ) { $out[] = self::check( 'Free disk space', $free > 500 * MB_IN_BYTES ? 'ok' : 'warn', size_format( $free ) ); } return $out; } /** * A 403 that came from something in front of WordPress (Cloudflare's WAF or bot protection, a * firewall), not from WordPress itself. * * @param array|WP_Error $response */ public static function is_proxy_block( $response ) { if ( is_wp_error( $response ) || 403 !== (int) wp_remote_retrieve_response_code( $response ) ) { return false; } $body = (string) wp_remote_retrieve_body( $response ); return '' !== (string) wp_remote_retrieve_header( $response, 'cf-mitigated' ) || ( 'cloudflare' === strtolower( (string) wp_remote_retrieve_header( $response, 'server' ) ) && ! preg_match( '#]+name=["\']?generator["\']?[^>]+WordPress#i', $body ) && false === stripos( $body, '/wp-content/' ) ) || false !== stripos( $body, 'Attention Required! | Cloudflare' ) || false !== stripos( $body, 'cf-error-details' ); } /** This server's own public address, as the web server reports it ('' if private or unknown). */ private static function public_ip() { $ip = isset( $_SERVER['SERVER_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_ADDR'] ) ) : ''; return filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) ? $ip : ''; } private static function background() { $out = array(); // The server's own public address: the IP a firewall or Cloudflare Access "Bypass" policy // needs so the exporter can reach WordPress. (A server with IPv6 may also call out over IPv6.) $ip = self::public_ip(); $out[] = self::check( 'Server IP', 'info', $ip ? $ip . '. If WordPress sits behind a firewall or Cloudflare Access, allow this IP (and the server\'s IPv6 address, if it has one).' : 'Not reported by the web server. Your host\'s control panel shows the server\'s public IP.' ); // Redirects are not followed: a redirect here usually means a login wall or a wrong Site URL. $start = microtime( true ); $response = wp_remote_get( home_url( '/' ), array( 'timeout' => 15, 'redirection' => 0, 'sslverify' => (bool) CFSP_Settings::get( 'sslverify' ), 'headers' => CFSP_Private::export_headers(), ) ); $ms = round( ( microtime( true ) - $start ) * 1000 ); if ( is_wp_error( $response ) ) { $out[] = self::check( 'Loopback: fetch homepage', 'fail', $response->get_error_message() . '. The server must be able to request its own pages.' ); } else { $code = wp_remote_retrieve_response_code( $response ); $location = (string) wp_remote_retrieve_header( $response, 'location' ); if ( CFSP_Exporter::is_login_wall( $location ) ) { $out[] = self::check( 'Loopback: fetch homepage', 'fail', 'Blocked by Cloudflare Access: the server gets the login page. Add ' . ( $ip ? $ip : "this server's IP" ) . ' to a Bypass policy for this hostname.' ); } elseif ( $code >= 300 && $code < 400 ) { $out[] = self::check( 'Loopback: fetch homepage', 'warn', 'HTTP ' . $code . ' redirect to ' . $location . '. Check the WordPress Address and Site Address.' ); } elseif ( 403 === (int) $code && self::is_proxy_block( $response ) ) { $direct_on = CFSP_Exporter::direct_enabled(); $out[] = self::check( 'Loopback: fetch homepage', $direct_on ? 'warn' : 'fail', 'HTTP 403 in ' . $ms . ' ms from Cloudflare (or another firewall in front of WordPress): it refuses this server\'s requests to its own site. ' . ( $direct_on ? 'Exports still work, because pages are fetched directly on this server (see below). ' : 'Turn on direct fetch under Advanced, or ' ) . 'To clear it, allow ' . ( $ip ? $ip : "this server's IP" ) . ' (and its IPv6 address) in Cloudflare: Security, WAF, Tools, an IP Access Rule set to Allow.' ); } else { $out[] = self::check( 'Loopback: fetch homepage', 200 === $code ? ( $ms > 3000 ? 'warn' : 'ok' ) : 'fail', 'HTTP ' . $code . ' in ' . $ms . ' ms' ); } } if ( CFSP_Settings::get( 'direct_fetch' ) ) { $start = microtime( true ); $direct = wp_remote_get( home_url( '/' ), array( 'timeout' => 15, 'redirection' => 0, 'sslverify' => false, 'cfsp_direct' => true, 'headers' => CFSP_Private::export_headers(), ) ); $ms = round( ( microtime( true ) - $start ) * 1000 ); if ( is_wp_error( $direct ) ) { $out[] = self::check( 'Direct fetch from this server', 'warn', 'Not possible at ' . CFSP_Exporter::direct_ip() . ' (' . $direct->get_error_message() . '). Pages are fetched the normal way, which is slower. If this server answers on another address, enter it under Advanced.' ); } else { $paused = get_transient( 'cfsp_direct_off' ) ? ' It had been paused after an earlier failure; resumed now.' : ''; delete_transient( 'cfsp_direct_off' ); $out[] = self::check( 'Direct fetch from this server', 'ok', 'Working: HTTP ' . wp_remote_retrieve_response_code( $direct ) . ' in ' . $ms . ' ms via ' . CFSP_Exporter::direct_ip() . ', without leaving the server.' . $paused ); } } // Same route the real background calls take (straight to this server when direct fetch is on). $worker_args = CFSP_Exporter::loopback_args( array( 'timeout' => 15, 'redirection' => 0, 'sslverify' => false, 'body' => array( 'action' => 'cfsp_worker', 'key' => 'diagnostics', ), ) ); $worker = wp_remote_post( admin_url( 'admin-ajax.php' ), $worker_args ); $code = is_wp_error( $worker ) ? 0 : (int) wp_remote_retrieve_response_code( $worker ); // The worker refuses the wrong key with an empty 403. A 403 with a page in it came from // something in front of WordPress (a firewall or proxy rule), so the worker never ran. $reachable = 403 === $code && '' === trim( (string) wp_remote_retrieve_body( $worker ) ); $route = empty( $worker_args['cfsp_direct'] ) ? '' : ' (directly on this server)'; if ( ! is_wp_error( $worker ) && CFSP_Exporter::is_login_wall( (string) wp_remote_retrieve_header( $worker, 'location' ) ) ) { $out[] = self::check( 'Loopback: background worker', 'fail', 'Blocked by Cloudflare Access. Add ' . ( $ip ? $ip : "this server's IP" ) . ' to a Bypass policy for this hostname.' ); } elseif ( $reachable ) { $out[] = self::check( 'Loopback: background worker', 'ok', 'admin-ajax.php is reachable' . $route . '.' ); } elseif ( 403 === $code ) { $out[] = self::check( 'Loopback: background worker', 'fail', 'Blocked: a firewall or proxy rule answered HTTP 403' . $route . ', so background jobs only move while the Activity page is open. Allow ' . ( $ip ? $ip : "this server's IP" ) . ' in your firewall (for Cloudflare: Security, WAF, an IP Access rule set to Allow)' . ( $route ? '.' : ', or turn on direct fetch under Advanced.' ) ); } else { $out[] = self::check( 'Loopback: background worker', 'warn', 'Not reachable' . $route . ', so jobs fall back to WP-Cron (slower). ' . ( is_wp_error( $worker ) ? $worker->get_error_message() : 'HTTP ' . $code ) ); } if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) { $out[] = self::check( 'WP-Cron', 'info', 'DISABLE_WP_CRON is set. Make sure a real cron job calls wp-cron.php every minute.' ); } else { $out[] = self::check( 'WP-Cron', 'ok', 'Enabled. On low-traffic sites a real cron job makes scheduled pushes punctual.' ); } $missed = count( CFSP_Job::missed_posts( 100 ) ); $out[] = $missed ? self::check( 'Scheduled posts', 'warn', sprintf( '%d scheduled post(s) are past their publish time. WP-Cron isn\'t running often enough: set up the external cron in Automation.', $missed ) ) : self::check( 'Scheduled posts', 'ok', 'No missed schedules.' ); $next = wp_next_scheduled( 'cfsp_watchdog' ); if ( ! $next ) { $out[] = self::check( 'Watchdog event', 'warn', 'Not scheduled. Deactivate and reactivate the plugin.' ); } elseif ( $next < time() - 10 * MINUTE_IN_SECONDS ) { $out[] = self::check( 'Watchdog event', 'warn', 'Overdue by ' . human_time_diff( $next, time() ) . ', so WP-Cron is not running. The static site gets no WordPress traffic to trigger it, and this server\'s requests to itself may be blocked. Set up the external cron in Automation.' ); } else { $out[] = self::check( 'Watchdog event', 'ok', $next > time() ? 'Next run in ' . human_time_diff( time(), $next ) : 'Due now.' ); } return $out; } private static function deploy() { $out = array(); $target = CFSP_Settings::get( 'target' ); $labels = array( 'github' => 'GitHub', 'cloudflare' => 'Cloudflare Pages', 'local' => 'Local directory / ZIP' ); $out[] = self::check( 'Target', 'info', $labels[ $target ] ); if ( 'github' === $target ) { $auth = CFSP_GitHub_Auth::get(); $out[] = CFSP_Settings::get( 'gh_token' ) ? self::check( 'GitHub account', 'ok', $auth ? '@' . $auth['login'] : 'Token from wp-config.php' ) : self::check( 'GitHub account', 'fail', 'Not connected. Go to Destination → Connect GitHub.' ); if ( CFSP_Settings::get( 'gh_token' ) && CFSP_Settings::get( 'gh_repo' ) ) { $repo = ( new CFSP_GitHub() )->get_repo(); $out[] = is_wp_error( $repo ) ? self::check( 'Repository', 'fail', $repo->get_error_message() ) : self::check( 'Repository', empty( $repo['permissions']['push'] ) ? 'fail' : 'ok', $repo['full_name'] . ( empty( $repo['permissions']['push'] ) ? ' (no write access)' : ' (write access)' ) ); } elseif ( CFSP_Settings::get( 'gh_token' ) ) { $out[] = self::check( 'Repository', 'fail', 'No repository selected.' ); } } elseif ( 'cloudflare' === $target ) { if ( CFSP_Settings::is_configured() ) { $project = ( new CFSP_Cloudflare() )->get_project(); $out[] = is_wp_error( $project ) ? self::check( 'Cloudflare project', 'fail', $project->get_error_message() ) : self::check( 'Cloudflare project', 'ok', $project['name'] ); } else { $out[] = self::check( 'Cloudflare credentials', 'fail', 'Account ID, API token and project name are required.' ); } } if ( CFSP_Settings::get( 'r2_enabled' ) ) { if ( ! CFSP_R2::is_configured() ) { $out[] = self::check( 'Media offload (R2)', 'fail', 'Enabled, but the R2 connection is incomplete. Check Media Offload.' ); } else { $read = ( new CFSP_R2() )->test_read(); $out[] = is_wp_error( $read ) ? self::check( 'Media offload (R2)', 'fail', $read->get_error_message() ) : self::check( 'Media offload (R2)', 'ok', 'Bucket "' . CFSP_Settings::get( 'r2_bucket' ) . '" reachable. ' . number_format_i18n( CFSP_Media::totals()['files'] ) . ' files uploaded.' ); if ( ! CFSP_Settings::get( 'r2_public_url' ) ) { $out[] = self::check( 'Media public URL', 'warn', 'Not set, so exported pages keep pointing at WordPress for images.' ); } } } $files = CFSP_Deployer::count_files( true ); $out[] = self::check( 'Exported files', $files ? 'info' : 'warn', $files ? number_format_i18n( $files ) . ' files' . ( $files > 20000 ? ', over the 20,000 file limit of the Cloudflare Pages Free plan' : '' ) : 'Nothing exported yet.' ); return $out; } }