WP_Site_Health::get_test_persistent_object_cache()publicWP 6.1.0

Tests if the site uses persistent object cache and recommends to use it if not.

Method of the class: WP_Site_Health{}

Return

Array. The test result.

Usage

$WP_Site_Health = new WP_Site_Health();
$WP_Site_Health->get_test_persistent_object_cache();

Changelog

Since 6.1.0 Introduced.

WP_Site_Health::get_test_persistent_object_cache() code WP 6.6.2

public function get_test_persistent_object_cache() {
	/**
	 * Filters the action URL for the persistent object cache health check.
	 *
	 * @since 6.1.0
	 *
	 * @param string $action_url Learn more link for persistent object cache health check.
	 */
	$action_url = apply_filters(
		'site_status_persistent_object_cache_url',
		/* translators: Localized Support reference. */
		__( 'https://developer.wordpress.org/advanced-administration/performance/optimization/#persistent-object-cache' )
	);

	$result = array(
		'test'        => 'persistent_object_cache',
		'status'      => 'good',
		'badge'       => array(
			'label' => __( 'Performance' ),
			'color' => 'blue',
		),
		'label'       => __( 'A persistent object cache is being used' ),
		'description' => sprintf(
			'<p>%s</p>',
			__( 'A persistent object cache makes your site&#8217;s database more efficient, resulting in faster load times because WordPress can retrieve your site&#8217;s content and settings much more quickly.' )
		),
		'actions'     => sprintf(
			'<p><a href="%s" target="_blank" rel="noopener">%s<span class="screen-reader-text"> %s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a></p>',
			esc_url( $action_url ),
			__( 'Learn more about persistent object caching.' ),
			/* translators: Hidden accessibility text. */
			__( '(opens in a new tab)' )
		),
	);

	if ( wp_using_ext_object_cache() ) {
		return $result;
	}

	if ( ! $this->should_suggest_persistent_object_cache() ) {
		$result['label'] = __( 'A persistent object cache is not required' );

		return $result;
	}

	$available_services = $this->available_object_cache_services();

	$notes = __( 'Your hosting provider can tell you if a persistent object cache can be enabled on your site.' );

	if ( ! empty( $available_services ) ) {
		$notes .= ' ' . sprintf(
			/* translators: Available object caching services. */
			__( 'Your host appears to support the following object caching services: %s.' ),
			implode( ', ', $available_services )
		);
	}

	/**
	 * Filters the second paragraph of the health check's description
	 * when suggesting the use of a persistent object cache.
	 *
	 * Hosts may want to replace the notes to recommend their preferred object caching solution.
	 *
	 * Plugin authors may want to append notes (not replace) on why object caching is recommended for their plugin.
	 *
	 * @since 6.1.0
	 *
	 * @param string   $notes              The notes appended to the health check description.
	 * @param string[] $available_services The list of available persistent object cache services.
	 */
	$notes = apply_filters( 'site_status_persistent_object_cache_notes', $notes, $available_services );

	$result['status']       = 'recommended';
	$result['label']        = __( 'You should use a persistent object cache' );
	$result['description'] .= sprintf(
		'<p>%s</p>',
		wp_kses(
			$notes,
			array(
				'a'      => array( 'href' => true ),
				'code'   => true,
				'em'     => true,
				'strong' => true,
			)
		)
	);

	return $result;
}