This commit is contained in:
2026-07-02 15:54:39 -06:00
commit 9883323161
17470 changed files with 4470592 additions and 0 deletions
@@ -0,0 +1,211 @@
<?php
namespace Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers;
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Critical_CSS_Storage;
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers\Archive_Provider;
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers\Cornerstone_Provider;
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers\Post_ID_Provider;
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers\Provider;
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers\Singular_Post_Provider;
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers\Taxonomy_Provider;
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers\WP_Core_Provider;
class Source_Providers {
/**
* Variable used to cache the CSS string during the page request.
* This is here because `get_critical_css` is called multiple
* times in `style_loader_tag` hook (on each CSS file).
*
* @var null|false|string
*/
protected $request_cached_css;
/**
* Stores the Critical CSS key used for rendering the current page if any.
*
* @var null|string
*/
protected $current_critical_css_key;
/**
* List of all the Critical CSS Types.
*
* The order is important because searching for critical CSS will stop as soon as a value is found.
* So finding Critical CSS by post ID is attempted before searching for a common Singular Post critical CSS.
*
* @var Provider[]
*/
protected $providers = array(
Cornerstone_Provider::class,
Post_ID_Provider::class,
WP_Core_Provider::class,
Singular_Post_Provider::class,
Archive_Provider::class,
Taxonomy_Provider::class,
);
public function get_providers() {
return $this->providers;
}
/**
* Returns the Provider which controls a given key.
*/
public function get_provider_for_key( $key ) {
foreach ( $this->providers as $provider ) {
if ( $provider::owns_key( $key ) ) {
return $provider;
}
}
return null;
}
/**
* Get all critical CSS storage keys that are available for the current request.
* Caches the result.
*
* @return array
*/
public function get_current_request_css_keys() {
static $keys = null;
if ( null !== $keys ) {
return $keys;
}
$keys = array();
foreach ( $this->providers as $provider ) {
$provider_keys = $provider::get_current_storage_keys();
if ( empty( $provider_keys ) ) {
continue;
}
$keys = array_merge( $keys, $provider_keys );
}
return $keys;
}
/**
* Get critical CSS for the current request.
*
* @return string|false
*/
public function get_current_request_css() {
if ( null !== $this->request_cached_css ) {
return $this->request_cached_css;
}
$storage = new Critical_CSS_Storage();
$data = $storage->get_css( $this->get_current_request_css_keys() );
if ( false === $data ) {
return false;
}
$this->request_cached_css = $data['css'];
$this->current_critical_css_key = $data['key'];
return $this->request_cached_css;
}
public function get_current_critical_css_key() {
return $this->current_critical_css_key;
}
/**
* Get providers sources.
*
* @return array
*/
public function get_provider_sources( $context_posts = array() ) {
$sources = array();
$flat_core_and_cornerstone_urls = array();
$wp_core_provider_urls = WP_Core_Provider::get_critical_source_urls( $context_posts );
foreach ( $wp_core_provider_urls as $urls ) {
$flat_core_and_cornerstone_urls = array_merge( $flat_core_and_cornerstone_urls, $urls );
}
$cornerstone_provider_urls = Cornerstone_Provider::get_critical_source_urls( $context_posts );
foreach ( $cornerstone_provider_urls as $urls ) {
$flat_core_and_cornerstone_urls = array_merge( $flat_core_and_cornerstone_urls, $urls );
}
$flat_core_and_cornerstone_urls = array_values( array_unique( $flat_core_and_cornerstone_urls ) );
foreach ( $this->get_providers() as $provider ) {
$provider_name = $provider::get_provider_name();
// For each provider,
// Gather a list of URLs that are going to be used as Critical CSS source.
foreach ( $provider::get_critical_source_urls( $context_posts ) as $group => $urls ) {
if ( empty( $urls ) ) {
continue;
}
// This removes core and cornerstone URLs from the list of URLs,
// so they don't belong to two separate groups.
if ( ! in_array( $provider, array( WP_Core_Provider::class, Cornerstone_Provider::class ), true ) ) {
$urls = array_values( array_diff( $urls, $flat_core_and_cornerstone_urls ) );
}
if ( empty( $urls ) ) {
continue;
}
$urls = $this->make_absolute_urls( $urls );
$key = $provider_name . '_' . $group;
// For each provider
// Track the state and errors in a state array.
$sources[] = array(
'key' => $key,
'label' => $provider::describe_key( $key ),
/**
* Filters the URLs used by Critical CSS for each provider.
*
* @param array $urls The list of URLs to be used to generate critical CSS
* @param string $provider The provider name.
* @since 1.0.0
*/
'urls' => apply_filters( 'jetpack_boost_critical_css_urls', $urls, $provider ),
'success_ratio' => $provider::get_success_ratio(),
);
}
}
/**
* Filters the list of Critical CSS source providers.
*
* @param array $sources The list of Critical CSS source providers.
* @since 3.6.0
*/
return apply_filters( 'jetpack_boost_critical_css_providers', $sources );
}
/**
* Make URLs absolute.
*
* @param array $urls The list of URLs to make absolute.
*
* @return array
*/
private function make_absolute_urls( $urls ) {
$absolute_urls = array();
foreach ( $urls as $url ) {
if ( class_exists( '\WP_Http' ) && method_exists( '\WP_Http', 'make_absolute_url' ) ) {
$absolute_urls[] = \WP_Http::make_absolute_url( $url, home_url() );
continue;
}
if ( stripos( $url, home_url() ) === 0 ) {
$absolute_urls[] = $url;
} else {
$absolute_urls[] = home_url( $url );
}
}
return $absolute_urls;
}
}
@@ -0,0 +1,128 @@
<?php
/**
* Archive provider class.
*
* @package automattic/jetpack-boost
*/
namespace Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers;
/**
* Class Archive_Provider
*
* @package Automattic\Jetpack_Boost\Modules\Critical_CSS\Providers
*/
class Archive_Provider extends Provider {
/**
* Provider name.
*
* @var string
*/
protected static $name = 'archive';
/** @inheritdoc */
public static function get_critical_source_urls( $context_posts = array() ) {
$links = array();
$context_post_types = wp_list_pluck( $context_posts, 'post_type' );
$post_types = self::get_post_types();
if ( ! empty( $context_post_types ) ) {
$post_types = array_intersect( $post_types, $context_post_types );
}
foreach ( $post_types as $post_type ) {
$link = get_post_type_archive_link( $post_type );
if ( ! empty( $link ) ) {
$links[ $post_type ][] = $link;
}
}
return $links;
}
/** @inheritdoc */
public static function get_current_storage_keys() {
if ( ! is_archive() ) {
return array();
}
// For example: "archive_post".
return array( self::$name . '_' . get_post_type() );
}
/** @inheritdoc */
public static function get_keys() {
return self::get_post_types();
}
/** @inheritdoc */
public static function get_edit_url( $_provider_key ) {
return null;
}
/** @inheritdoc */
public static function describe_key( $provider_key ) {
$post_type = substr( $provider_key, strlen( static::$name ) + 1 );
switch ( $post_type ) {
case 'post':
return __( 'Post archive view', 'jetpack-boost' );
case 'page':
return __( 'Page archive view', 'jetpack-boost' );
default:
return __( 'Archive page for custom post type', 'jetpack-boost' );
}
}
/**
* Get post types that need Critical CSS.
*
* @return mixed|void
*/
public static function get_post_types() {
$post_types = get_post_types(
array(
'public' => true,
'has_archive' => true,
),
'objects'
);
unset( $post_types['attachment'] );
$post_types = array_filter( $post_types, 'is_post_type_viewable' );
$provider_post_types = array();
// Generate a name => name array for backwards compatibility.
foreach ( $post_types as $post_type ) {
$provider_post_types[ $post_type->name ] = $post_type->name;
}
/**
* Filters the post types used for Critical CSS
*
* @param array $post_types The array of post types to be used
*
* @since 1.0.0
*/
return apply_filters(
'jetpack_boost_critical_css_post_types_archives',
apply_filters_deprecated(
'jetpack_boost_critical_css_post_types',
array(
$provider_post_types,
),
'3.4.0',
'jetpack_boost_critical_css_post_types_archives'
),
$post_types
);
}
/** @inheritdoc */
public static function get_success_ratio() {
return 1;
}
}
@@ -0,0 +1,129 @@
<?php
/**
* Provider for the cornerstone pages
*
* @package automattic/jetpack-boost
*/
namespace Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers;
use Automattic\Jetpack_Boost\Lib\Cornerstone\Cornerstone_Utils;
/**
* Class Cornerstone_Provider
*
* @package Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers
*/
class Cornerstone_Provider extends Provider {
/**
* @var string
*/
protected static $name = 'cornerstone';
/**
* Get the providers for cornerstone pages.
*
* @param array $_context_posts Context posts, not used. Cornerstone pages are always available.
* @return array
*/
public static function get_critical_source_urls( $_context_posts = array() ) {
$urls = Cornerstone_Utils::get_list();
$groups = array();
foreach ( $urls as $url ) {
$groups[ self::get_hash_for_url( $url ) ] = array( $url );
}
return $groups;
}
public static function get_provider_key( $url ) {
return self::$name . '_' . self::get_hash_for_url( $url );
}
/**
* Get the current storage keys for cornerstone pages.
*
* @return array
*/
public static function get_current_storage_keys() {
$current_url = self::get_request_url();
return array( self::get_provider_key( $current_url ) );
}
public static function get_request_url() {
global $wp;
// If pretty parmalinks are enabled, use the request. Otherwise, use the query vars.
if ( get_option( 'permalink_structure' ) ) {
return home_url( $wp->request );
}
return add_query_arg( $wp->query_vars, home_url() );
}
/**
* Get the keys for cornerstone pages.
*
* @return array
*/
public static function get_keys() {
$urls = Cornerstone_Utils::get_list();
return array_map( array( __CLASS__, 'get_hash_for_url' ), $urls );
}
/**
* @inheritdoc
*/
public static function get_hash_for_url( $url ) {
// Remove the home_url from the beginning of the URL.
$home_url = home_url();
if ( stripos( $url, $home_url ) === 0 ) {
$url = substr( $url, strlen( $home_url ) );
}
$url = ltrim( $url, '/' );
$url = untrailingslashit( $url );
$hash = hash( 'md5', $url );
return substr( $hash, 0, 8 );
}
/**
* @inheritdoc
*/
public static function describe_key( $_key ) {
return __( 'Cornerstone page', 'jetpack-boost' );
}
/**
* @inheritdoc
*/
public static function get_edit_url( $key ) {
$hash = substr( $key, strlen( self::$name ) + 1 );
$source_urls = self::get_critical_source_urls();
if ( ! isset( $source_urls[ $hash ] ) ) {
return null;
}
$post_id = url_to_postid( $source_urls[ $hash ][0] );
if ( ! $post_id ) {
return null;
}
return get_edit_post_link( $post_id );
}
/**
* @inheritdoc
*/
public static function get_success_ratio() {
return 1;
}
}
@@ -0,0 +1,162 @@
<?php
/**
* The Post ID provider class.
*
* @package automattic/jetpack-boost
*/
namespace Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers;
/**
* Class Post_ID_Provider
*
* @package Automattic\Jetpack_Boost\Modules\Critical_CSS\Providers
*/
class Post_ID_Provider extends Provider {
/**
* Post Ids storage key.
*
* @var string
*/
const STORAGE_KEY = 'jetpack_boost_critical_css_post_ids';
/**
* Provider name.
*
* @var string
*/
protected static $name = 'post_id';
/** @inheritdoc */
public static function get_critical_source_urls( $context_posts = array() ) {
$results = array();
$query = self::get_posts();
$context_post_ids = wp_list_pluck( $context_posts, 'ID' );
if ( false === $query ) {
return array();
}
foreach ( $query->posts as $post ) {
if ( empty( $context_post_ids ) || in_array( $post->ID, $context_post_ids, true ) ) {
$url = get_permalink( $post );
if ( ! empty( $url ) ) {
$results[ $post->ID ] = array( $url );
}
}
}
return $results;
}
/** @inheritdoc */
public static function get_current_storage_keys() {
if ( ! is_singular() ) {
return array();
}
// For example: "post_id_123".
return array( self::$name . '_' . get_the_ID() );
}
/** @inheritdoc */
public static function describe_key( $provider_key ) {
$post_id = (int) substr( $provider_key, strlen( self::$name ) + 1 );
$post = get_post( $post_id );
if ( ! $post ) {
/* translators: %d is the id of a post which cannot be found. */
return sprintf( __( 'Post %d', 'jetpack-boost' ), $post_id );
}
return $post->post_title;
}
/** @inheritdoc */
public static function get_edit_url( $provider_key ) {
$post_id = (int) substr( $provider_key, strlen( self::$name ) + 1 );
return get_edit_post_link( $post_id, 'link' );
}
/**
* Returns a key that can be used to identify the current page, if any exists.
*
* @return string|null
*/
public static function get_current_page_key() {
$keys = static::get_current_storage_keys();
if ( count( $keys ) > 0 ) {
return $keys[0];
} else {
return null;
}
}
/**
* Get post ids.
*
* @return array
*/
public static function get_post_ids() {
// Store the IDs somewhere.
return get_option( self::STORAGE_KEY, array() );
}
/**
* Add post id to storage.
*
* @param int $post_id Post Id.
*
* @return bool
*/
public static function add_post_id( $post_id ) {
$post_ids = static::get_post_ids();
if ( in_array( $post_id, $post_ids, true ) ) {
return false;
}
$post_ids[] = (int) $post_id;
return update_option( self::STORAGE_KEY, $post_ids );
}
/** @inheritdoc */
public static function get_keys() {
return self::get_post_ids();
}
/**
* Create a new WP_Query to gather sample posts.
*
* @return false|\WP_Query
*/
public static function get_posts() {
$ids = self::get_post_ids();
if ( ! $ids ) {
return false;
}
return new \WP_Query(
array(
'post__in' => $ids,
'posts_per_page' => count( $ids ),
'post_status' => array( 'publish' ),
'post_type' => 'any',
'no_found_rows' => true,
'ignore_sticky_posts' => true,
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
)
);
}
/** @inheritdoc */
public static function get_success_ratio() {
return 1;
}
}
@@ -0,0 +1,88 @@
<?php
/**
* Abstract Critical CSS provider class.
*
* @package automattic/jetpack-boost
*/
namespace Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers;
/**
* Class Provider
*
* @package Automattic\Jetpack_Boost\Modules\Critical_CSS\Providers
*/
abstract class Provider {
/**
* The name of the provider.
*
* @since 1.0.0
* @access protected
* @var string $name The name of the provider
*/
protected static $name;
/**
* Each provider must return a list of URLs to generate CSS from.
*
* @param \WP_Post[] $context_posts The posts to generate CSS from.
* @return array
*/
abstract public static function get_critical_source_urls( $context_posts = array() );
/**
* What key should this provider look for during the current request?
* Used in the front-end to determine where the CSS
* might be stored for the current request.
*
* @return array
*/
abstract public static function get_current_storage_keys();
/**
* Returns a list of all keys that this provider can provide, regardless
* of the current URL.
*/
abstract public static function get_keys();
/**
* Get a human-displayable string describing the given provider key.
*
* @param string $provider_key the key to describe.
*/
abstract public static function describe_key( $provider_key );
/**
* Get the URL of the edit page for the given provider key.
*
* @param string $provider_key the key to edit.
*/
abstract public static function get_edit_url( $provider_key );
/**
* Returns true if the key looks like it belongs to this provider.
*
* @param string $key The key.
*/
public static function owns_key( $key ) {
return strncmp( static::$name, $key, strlen( static::$name ) ) === 0;
}
/**
* Get the name of the provider.
*
* @return string
*/
public static function get_provider_name() {
return static::$name;
}
/**
* Returns the ratio of valid urls from the provider source urls
* for the Critical CSS generation to be considered successful.
*
* @return float|int
*/
abstract public static function get_success_ratio();
}
@@ -0,0 +1,178 @@
<?php
/**
* Critical CSS Provider for singular posts.
*
* @package automattic/jetpack-boost
*/
namespace Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers;
/**
* Class Singular_Post_Provider
*
* @package Automattic\Jetpack_Boost\Modules\Critical_CSS\Providers
*/
class Singular_Post_Provider extends Provider {
/**
* Provider name.
*
* @var string
*/
protected static $name = 'singular';
/**
* Max number of posts to query.
*
* @var integer
*/
const MAX_URLS = 10;
/**
* Minimum number of posts to have Critical CSS generated in order for the whole process to be successful.
*
* @var integer
*/
const MIN_SUCCESS_URLS = 5;
/** @inheritdoc */
public static function get_critical_source_urls( $context_posts = array() ) {
$links = array();
$context_post_types = wp_list_pluck( $context_posts, 'post_type' );
$post_types = self::get_post_types();
if ( ! empty( $context_post_types ) ) {
$post_types = array_intersect( $post_types, $context_post_types );
}
foreach ( $post_types as $post_type ) {
$query = self::post_type_query( $post_type );
foreach ( $query->posts as $post ) {
$url = get_permalink( $post );
if ( ! empty( $url ) ) {
$links[ $post_type ][] = $url;
}
}
}
return $links;
}
/** @inheritdoc */
public static function get_current_storage_keys() {
if ( ! is_singular() ) {
return array();
}
// For example: "singular_post".
return array( self::$name . '_' . get_post_type() );
}
/** @inheritdoc */
public static function get_keys() {
return array_keys( self::get_post_types() );
}
/** @inheritdoc */
public static function get_edit_url( $_provider_key ) {
return null;
}
/** @inheritdoc */
public static function describe_key( $provider_key ) {
$post_type = substr( $provider_key, strlen( static::$name ) + 1 );
switch ( $post_type ) {
case 'post':
return __( 'Single post view', 'jetpack-boost' );
case 'page':
return __( 'Single page view', 'jetpack-boost' );
case 'product':
return __( 'Single product view', 'jetpack-boost' );
default:
return __( 'Custom post type', 'jetpack-boost' );
}
}
/**
* Get post types that need Critical CSS.
*
* @return mixed|void
*/
public static function get_post_types() {
$post_types = get_post_types(
array(
'public' => true,
),
'objects'
);
unset( $post_types['attachment'] );
$post_types = array_filter( $post_types, 'is_post_type_viewable' );
$provider_post_types = array();
// Generate a name => name array for backwards compatibility.
foreach ( $post_types as $post_type ) {
$provider_post_types[ $post_type->name ] = $post_type->name;
}
/**
* Filters the post types used for Critical CSS
*
* @param array $post_types The array of post types to be used
*
* @since 1.0.0
*/
return apply_filters(
'jetpack_boost_critical_css_post_types_singular',
apply_filters_deprecated(
'jetpack_boost_critical_css_post_types',
array(
$provider_post_types,
),
'3.4.0',
'jetpack_boost_critical_css_post_types_singular'
),
$post_types
);
}
/**
* Create a new WP_Query to gather sample posts.
*
* @param string $post_type post type.
*
* @return \WP_Query
*/
public static function post_type_query( $post_type ) {
/**
* Filters the WP_Query parameters used to gather sample posts
*
* @param array $args The arguments that will be used by WP_Query
*
* @since 1.0.0
*/
$args = apply_filters(
'jetpack_boost_critical_css_post_type_query',
array(
'orderby' => 'ID',
'post_type' => $post_type,
'posts_per_page' => static::MAX_URLS,
'post_status' => array( 'publish' ),
'no_found_rows' => true,
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
)
);
return new \WP_Query( $args );
}
/** @inheritdoc */
public static function get_success_ratio() {
return static::MIN_SUCCESS_URLS / static::MAX_URLS;
}
}
@@ -0,0 +1,171 @@
<?php
/**
* Provides taxonomy support for critical CSS
*
* @package automattic/jetpack-boost
*/
namespace Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers;
/**
* Class Taxonomy_Provider
*
* @package Automattic\Jetpack_Boost\Modules\Critical_CSS\Providers
*/
class Taxonomy_Provider extends Provider {
/**
* Provider name.
*
* @var string
*/
protected static $name = 'taxonomy';
/**
* Max number of posts to query.
*
* @var integer
*/
const MAX_URLS = 10;
/**
* Minimum number of posts to have Critical CSS generated in order for the whole process to be successful.
*
* @var integer
*/
const MIN_SUCCESS_URLS = 5;
/** @inheritdoc */
public static function get_critical_source_urls( $context_posts = array() ) {
$results = array();
$taxonomies = self::get_available_taxonomies();
if ( ! empty( $context_posts ) ) {
$context_post_types = array_unique( wp_list_pluck( $context_posts, 'post_type' ) );
$context_taxonomies = get_object_taxonomies( $context_post_types, 'names' );
$taxonomies = array_intersect( $taxonomies, $context_taxonomies );
}
foreach ( $taxonomies as $taxonomy ) {
$terms = self::get_terms( $taxonomy );
if ( ! $terms ) {
continue;
}
foreach ( $terms as $term ) {
$url = get_term_link( $term, $taxonomy );
if ( ! is_wp_error( $url ) && ! empty( $url ) ) {
$results[ $taxonomy ][] = $url;
}
}
}
return $results;
}
/** @inheritdoc */
public static function get_current_storage_keys() {
if ( ! is_category() && ! is_tax() ) {
return array();
}
if ( ! get_queried_object() ) {
return array();
}
// For example: "taxonomy_category".
return array( self::$name . '_' . get_queried_object()->taxonomy );
}
/** @inheritdoc */
public static function get_keys() {
return array_keys(
array_filter(
self::get_available_taxonomies(),
function ( $taxonomy ) {
return ! empty( Taxonomy_Provider::get_terms( $taxonomy ) );
}
)
);
}
/** @inheritdoc */
public static function describe_key( $provider_key ) {
$taxonomy = substr( $provider_key, strlen( static::$name ) + 1 );
switch ( $taxonomy ) {
case 'category':
return __( 'Category view', 'jetpack-boost' );
default:
return __( 'View for custom taxonomy', 'jetpack-boost' );
}
}
/** @inheritdoc */
public static function get_edit_url( $_provider_key ) {
return null;
}
/**
* Which taxonomies should Critical CSS be generated for.
*
* @return array
*/
public static function get_available_taxonomies() {
$taxonomies = get_taxonomies(
array(
'public' => true,
'show_in_rest' => true,
),
'objects'
);
$taxonomies = array_filter( $taxonomies, 'is_taxonomy_viewable' );
$provider_taxonomies = array();
// Generate a name => name array for backwards compatibility.
foreach ( $taxonomies as $taxonomy ) {
$provider_taxonomies[ $taxonomy->name ] = $taxonomy->name;
}
return $provider_taxonomies;
}
/**
* Get a couple sample terms for a taxonomy.
*
* @param string $taxonomy Taxonomy.
*
* @return array
*/
public static function get_terms( $taxonomy ) {
/**
* Filters the WP_Term_Query args to get a sample of terms for a taxonomy
*
* @param array $args The arguments that will be used by WP_Term_Query
*
* @since 1.0.0
*/
$args = apply_filters(
'jetpack_boost_critical_css_terms_query',
array(
'fields' => 'ids',
'taxonomy' => $taxonomy,
'orderby' => 'term_order',
'number' => static::MAX_URLS,
'hide_empty' => true,
'hierarchical' => false,
'update_term_meta_cache' => false,
)
);
return ( new \WP_Term_Query( $args ) )->terms;
}
/** @inheritdoc */
public static function get_success_ratio() {
return static::MIN_SUCCESS_URLS / static::MAX_URLS;
}
}
@@ -0,0 +1,118 @@
<?php
/**
* Provides core support for critical CSS
*
* @package automattic/jetpack-boost
*/
namespace Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers;
/**
* Class WP_Core_Provider.
*
* @package Automattic\Jetpack_Boost\Modules\Critical_CSS\Providers
*/
class WP_Core_Provider extends Provider {
/**
* Provider name.
*
* @var string
*/
protected static $name = 'core';
/** @inheritdoc */
public static function get_critical_source_urls( $context_posts = array() ) {
$urls = array();
$front_page = (int) get_option( 'page_on_front' );
$posts_page = (int) get_option( 'page_for_posts' );
if ( ! empty( $front_page ) && empty( $context_posts ) ) {
$permalink = get_permalink( $front_page );
if ( ! empty( $permalink ) ) {
$urls['front_page'] = array( $permalink );
}
}
$context_post_types = wp_list_pluck( $context_posts, 'post_type' );
$context_post_ids = wp_list_pluck( $context_posts, 'ID' );
// The blog page is only in context if the context posts include a 'post' post_type.
// Or, if the blog page itself is in context.
if ( empty( $context_post_types ) || in_array( 'post', $context_post_types, true ) || in_array( $posts_page, $context_post_ids, true ) ) {
if ( ! empty( $posts_page ) ) {
$permalink = get_permalink( $posts_page );
if ( ! empty( $permalink ) ) {
$urls['posts_page'] = array( $permalink );
}
}
}
if ( ! $front_page && ! isset( $urls['posts_page'] ) ) {
$urls['posts_page'] = array( home_url( '/' ) );
}
return $urls;
}
/** @inheritdoc */
public static function get_keys() {
$keys = array( 'posts_page' );
if ( ! empty( get_option( 'page_on_front' ) ) ) {
$keys[] = 'front_page';
}
return $keys;
}
/** @inheritdoc */
public static function get_current_storage_keys() {
if ( is_home() ) {
$key = 'posts_page';
} elseif ( is_front_page() ) {
$key = 'front_page';
}
if ( ! isset( $key ) ) {
return array();
}
// For example: "core_posts_page".
return array( self::$name . '_' . $key );
}
/** @inheritdoc */
public static function get_edit_url( $provider_key ) {
if ( $provider_key === 'core_front_page' ) {
$front_page_id = get_option( 'page_on_front' );
if ( ! empty( $front_page_id ) ) {
return get_edit_post_link( $front_page_id, 'link' );
}
}
return null;
}
/** @inheritdoc */
public static function describe_key( $provider_key ) {
$page = substr( $provider_key, strlen( static::$name ) + 1 );
switch ( $page ) {
case 'posts_page':
return __( 'Posts page', 'jetpack-boost' );
case 'front_page':
return __( 'Front page', 'jetpack-boost' );
default:
return $provider_key;
}
}
/** @inheritdoc */
public static function get_success_ratio() {
return 1;
}
}