wp_cron_preload_cache()WPSCache 1.0

Hooks from the function

Return

null. Nothing (null).

Usage

wp_cron_preload_cache();

wp_cron_preload_cache() code WPSCache 1.11.0

function wp_cron_preload_cache() {
	global $wpdb, $wp_cache_preload_interval, $wp_cache_preload_posts, $wp_cache_preload_email_me, $wp_cache_preload_email_volume, $cache_path, $wp_cache_preload_taxonomies;

	// check if stop_preload.txt exists and preload should be stopped.
	// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
	if ( @file_exists( $cache_path . 'stop_preload.txt' ) ) {
		wp_cache_debug( 'wp_cron_preload_cache: preload cancelled. Aborting preload.' );
		wpsc_reset_preload_settings();
		return true;
	}

	/*
	 * The mutex file is used to prevent multiple preload processes from running at the same time.
	 * If the mutex file is found, the preload process will wait 3-8 seconds and then check again.
	 * If the mutex file is still found, the preload process will abort.
	 * If the mutex file is not found, the preload process will create the mutex file and continue.
	 * The mutex file is deleted at the end of the preload process.
	 * The mutex file is deleted if it is more than 10 minutes old.
	 * The mutex file should only be deleted by the preload process that created it.
	 * If the mutex file is deleted by another process, another preload process may start.
	 */
	$mutex = $cache_path . "preload_mutex.tmp";
	if ( @file_exists( $mutex ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
		sleep( 3 + wp_rand( 1, 5 ) );
		// check again just in case another preload process is still running.
		if ( @file_exists( $mutex ) && @filemtime( $mutex ) > ( time() - 600 ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
			wp_cache_debug( 'wp_cron_preload_cache: preload mutex found and less than 600 seconds old. Aborting preload.', 1 );
			return true;
		} else {
			wp_cache_debug( 'wp_cron_preload_cache: old preload mutex found and deleted. Preload continues.', 1 );
			@unlink( $mutex );
		}
	}
	$fp = @fopen( $mutex, 'w' );
	@fclose( $fp );

	$counter = get_option( 'preload_cache_counter' );
	$c = $counter[ 'c' ];

	if ( $wp_cache_preload_email_volume == 'none' && $wp_cache_preload_email_me == 1 ) {
		$wp_cache_preload_email_me = 0;
		wp_cache_setting( 'wp_cache_preload_email_me', 0 );
	}

	$just_started_preloading = false;

	/*
	 * Preload taxonomies first.
	 *
	 */
	if ( isset( $wp_cache_preload_taxonomies ) && $wp_cache_preload_taxonomies ) {
		wp_cache_debug( 'wp_cron_preload_cache: doing taxonomy preload.', 5 );
		$taxonomies = apply_filters(
			'wp_cache_preload_taxonomies',
			array(
				'post_tag' => 'tag',
				'category' => 'category',
			)
		);

		$preload_more_taxonomies = false;

		foreach ( $taxonomies as $taxonomy => $path ) {
			$taxonomy_filename = $cache_path . 'taxonomy_' . $taxonomy . '.txt';

			// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
			if ( false === @file_exists( $taxonomy_filename ) ) {

				if ( ! $just_started_preloading && $wp_cache_preload_email_me ) {
					// translators: 1: site url
					wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] Cache Preload Started', 'wp-super-cache' ), home_url(), '' ), ' ' );
				}

				$just_started_preloading = true;
				$out                     = '';
				$records                 = get_terms( $taxonomy );
				foreach ( $records as $term ) {
					$out .= get_term_link( $term ) . "\n";
				}
				// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen
				$fp = fopen( $taxonomy_filename, 'w' );
				if ( $fp ) {
					// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite
					fwrite( $fp, $out );
					// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose
					fclose( $fp );
				}
				$details = explode( "\n", $out );
			} else {
				// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
				$details = explode( "\n", file_get_contents( $taxonomy_filename ) );
			}
			if ( count( $details ) > 0 && $details[0] !== '' ) {
				$rows = array_splice( $details, 0, WPSC_PRELOAD_POST_COUNT );
				if ( $wp_cache_preload_email_me && $wp_cache_preload_email_volume === 'many' ) {
					// translators: 1: Site URL, 2: Taxonomy name, 3: Number of posts done, 4: Number of posts to preload
					wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] Refreshing %2$s taxonomy from %3$d to %4$d', 'wp-super-cache' ), home_url(), $taxonomy, $c, ( $c + WPSC_PRELOAD_POST_COUNT ) ), 'Refreshing: ' . print_r( $rows, 1 ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
				}

				foreach ( (array) $rows as $url ) {
					set_time_limit( 60 );
					if ( $url === '' ) {
						continue;
					}

					$url_info = wp_parse_url( $url );
					$dir      = get_supercache_dir() . $url_info['path'];
					wp_cache_debug( "wp_cron_preload_cache: delete $dir" );
					wpsc_delete_files( $dir );
					prune_super_cache( trailingslashit( $dir ) . 'feed/', true );
					prune_super_cache( trailingslashit( $dir ) . 'page/', true );

					wpsc_update_active_preload( 'taxonomies', $taxonomy, $url );

					wp_remote_get(
						$url,
						array(
							'timeout'  => 60,
							'blocking' => true,
						)
					);
					wp_cache_debug( "wp_cron_preload_cache: fetched $url" );
					sleep( 1 );

					if ( ! wpsc_is_preload_active() ) {
						wp_cache_debug( 'wp_cron_preload_cache: cancelling preload process.' );
						wpsc_reset_preload_settings();

						if ( $wp_cache_preload_email_me ) {
							// translators: Home URL of website
							wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] Cache Preload Stopped', 'wp-super-cache' ), home_url(), '' ), ' ' );
						}
						wpsc_update_idle_preload( time() );
						return true;
					}
				}
				// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen
				$fp = fopen( $taxonomy_filename, 'w' );
				if ( $fp ) {
					// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite
					fwrite( $fp, implode( "\n", $details ) );
					// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose
					fclose( $fp );
				}
			}

			if (
				$preload_more_taxonomies === false &&
				count( $details ) > 0 &&
				$details[0] !== ''
			) {
				$preload_more_taxonomies = true;
			}
		}

		if ( $preload_more_taxonomies === true ) {
			wpsc_schedule_next_preload();
			return true;
		}
	} elseif ( $c === 0 && $wp_cache_preload_email_me ) {
		// translators: Home URL of website
		wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] Cache Preload Started', 'wp-super-cache' ), home_url(), '' ), ' ' );
	}

	/*
	 *
	 * Preload posts now.
	 *
	 * The preload_cache_counter has two values:
	 * c = the number of posts we've preloaded after this loop.
	 * t = the time we started preloading in the current loop.
	 *
	 * $c is set to the value of preload_cache_counter['c'] at the start of the function
	 * before it is incremented by WPSC_PRELOAD_POST_COUNT here.
	 * The time is used to check if preloading has stalled in check_up_on_preloading().
	 */

	update_option(
		'preload_cache_counter',
		array(
			'c' => ( $c + WPSC_PRELOAD_POST_COUNT ),
			't' => time(),
		)
	);

	if ( $wp_cache_preload_posts == 'all' || $c < $wp_cache_preload_posts ) {
		$types = wpsc_get_post_types();
		$posts = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE ( post_type IN ( $types ) ) AND post_status = 'publish' ORDER BY ID DESC LIMIT %d," . WPSC_PRELOAD_POST_COUNT, $c ) ); // phpcs:ignore
		wp_cache_debug( 'wp_cron_preload_cache: got ' . WPSC_PRELOAD_POST_COUNT . ' posts from position ' . $c );
	} else {
		wp_cache_debug( "wp_cron_preload_cache: no more posts to get. Limit ($wp_cache_preload_posts) reached.", 5 );
		$posts = false;
	}
	if ( !isset( $wp_cache_preload_email_volume ) )
		$wp_cache_preload_email_volume = 'medium';

	if ( $posts ) {
		if ( get_option( 'show_on_front' ) == 'page' ) {
			$page_on_front = get_option( 'page_on_front' );
			$page_for_posts = get_option( 'page_for_posts' );
		} else {
			$page_on_front = $page_for_posts = 0;
		}
		if ( $wp_cache_preload_email_me && $wp_cache_preload_email_volume === 'many' ) {
			/* translators: 1: home url, 2: start post id, 3: end post id */
			wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] Refreshing posts from %2$d to %3$d', 'wp-super-cache' ), home_url(), $c, ( $c + WPSC_PRELOAD_POST_COUNT ) ), ' ' );
		}
		$msg = '';
		$count = $c + 1;

		foreach( $posts as $post_id ) {
			set_time_limit( 60 );
			if ( $page_on_front != 0 && ( $post_id == $page_on_front || $post_id == $page_for_posts ) )
				continue;
			$url = get_permalink( $post_id );

			if ( ! is_string( $url ) ) {
					wp_cache_debug( "wp_cron_preload_cache: skipped $post_id. Expected a URL, received: " . gettype( $url ) );
					continue;
			}

			if ( wp_cache_is_rejected( $url ) ) {
				wp_cache_debug( "wp_cron_preload_cache: skipped $url per rejected strings setting" );
				continue;
			}
			clear_post_supercache( $post_id );

			wpsc_update_active_preload( 'posts', $count, $url );

			if ( ! wpsc_is_preload_active() ) {
				wp_cache_debug( 'wp_cron_preload_cache: cancelling preload process.' );
				wpsc_reset_preload_settings();

				if ( $wp_cache_preload_email_me ) {
					// translators: Home URL of website
					wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] Cache Preload Stopped', 'wp-super-cache' ), home_url(), '' ), ' ' );
				}

				wpsc_update_idle_preload( time() );
				return true;
			}

			$msg .= "$url\n";
			wp_remote_get( $url, array('timeout' => 60, 'blocking' => true ) );
			wp_cache_debug( "wp_cron_preload_cache: fetched $url", 5 );
			++$count;
			sleep( 1 );
		}

		if ( $wp_cache_preload_email_me && ( $wp_cache_preload_email_volume === 'medium' || $wp_cache_preload_email_volume === 'many' ) ) {
			// translators: 1: home url, 2: number of posts refreshed
			wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] %2$d posts refreshed', 'wp-super-cache' ), home_url(), ( $c + WPSC_PRELOAD_POST_COUNT ) ), __( 'Refreshed the following posts:', 'wp-super-cache' ) . "\n$msg" );
		}

		wpsc_schedule_next_preload();

		wpsc_delete_files( get_supercache_dir() );
	} else {
		$msg = '';
		wpsc_reset_preload_counter();
		if ( (int)$wp_cache_preload_interval && defined( 'DOING_CRON' ) ) {
			if ( $wp_cache_preload_email_me )
				$msg = sprintf( __( 'Scheduling next preload refresh in %d minutes.', 'wp-super-cache' ), (int)$wp_cache_preload_interval );
			wp_cache_debug( "wp_cron_preload_cache: no more posts. scheduling next preload in $wp_cache_preload_interval minutes.", 5 );
			wp_schedule_single_event( time() + ( (int)$wp_cache_preload_interval * 60 ), 'wp_cache_full_preload_hook' );
		}
		global $file_prefix, $cache_max_time;
		if ( $wp_cache_preload_interval > 0 ) {
			$cache_max_time = (int)$wp_cache_preload_interval * 60; // fool the GC into expiring really old files
		} else {
			$cache_max_time = 86400; // fool the GC into expiring really old files
		}
		if ( $wp_cache_preload_email_me )
			wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] Cache Preload Completed', 'wp-super-cache' ), home_url() ), __( "Cleaning up old supercache files.", 'wp-super-cache' ) . "\n" . $msg );
		if ( $cache_max_time > 0 ) { // GC is NOT disabled
			wp_cache_debug( "wp_cron_preload_cache: clean expired cache files older than $cache_max_time seconds.", 5 );
			wp_cache_phase2_clean_expired( $file_prefix, true ); // force cleanup of old files.
		}

		wpsc_reset_preload_settings();
		wpsc_update_idle_preload( time() );
	}
	@unlink( $mutex );
}