Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 62 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| Singular_Post_Provider | |
0.00% |
0 / 62 |
|
0.00% |
0 / 8 |
342 | |
0.00% |
0 / 1 |
| get_critical_source_urls | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
30 | |||
| get_current_storage_keys | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| get_keys | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_edit_url | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| describe_key | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
30 | |||
| get_post_types | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
6 | |||
| post_type_query | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
2 | |||
| get_success_ratio | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Critical CSS Provider for singular posts. |
| 4 | * |
| 5 | * @package automattic/jetpack-boost |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers; |
| 9 | |
| 10 | /** |
| 11 | * Class Singular_Post_Provider |
| 12 | * |
| 13 | * @package Automattic\Jetpack_Boost\Modules\Critical_CSS\Providers |
| 14 | */ |
| 15 | class Singular_Post_Provider extends Provider { |
| 16 | |
| 17 | /** |
| 18 | * Provider name. |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | protected static $name = 'singular'; |
| 23 | |
| 24 | /** |
| 25 | * Max number of posts to query. |
| 26 | * |
| 27 | * @var integer |
| 28 | */ |
| 29 | const MAX_URLS = 10; |
| 30 | |
| 31 | /** |
| 32 | * Minimum number of posts to have Critical CSS generated in order for the whole process to be successful. |
| 33 | * |
| 34 | * @var integer |
| 35 | */ |
| 36 | const MIN_SUCCESS_URLS = 5; |
| 37 | |
| 38 | /** @inheritdoc */ |
| 39 | public static function get_critical_source_urls( $context_posts = array() ) { |
| 40 | $links = array(); |
| 41 | $context_post_types = wp_list_pluck( $context_posts, 'post_type' ); |
| 42 | |
| 43 | $post_types = self::get_post_types(); |
| 44 | if ( ! empty( $context_post_types ) ) { |
| 45 | $post_types = array_intersect( $post_types, $context_post_types ); |
| 46 | } |
| 47 | foreach ( $post_types as $post_type ) { |
| 48 | $query = self::post_type_query( $post_type ); |
| 49 | |
| 50 | foreach ( $query->posts as $post ) { |
| 51 | $url = get_permalink( $post ); |
| 52 | if ( ! empty( $url ) ) { |
| 53 | $links[ $post_type ][] = $url; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return $links; |
| 59 | } |
| 60 | |
| 61 | /** @inheritdoc */ |
| 62 | public static function get_current_storage_keys() { |
| 63 | if ( ! is_singular() ) { |
| 64 | return array(); |
| 65 | } |
| 66 | |
| 67 | // For example: "singular_post". |
| 68 | return array( self::$name . '_' . get_post_type() ); |
| 69 | } |
| 70 | |
| 71 | /** @inheritdoc */ |
| 72 | public static function get_keys() { |
| 73 | return array_keys( self::get_post_types() ); |
| 74 | } |
| 75 | |
| 76 | /** @inheritdoc */ |
| 77 | public static function get_edit_url( $_provider_key ) { |
| 78 | return null; |
| 79 | } |
| 80 | |
| 81 | /** @inheritdoc */ |
| 82 | public static function describe_key( $provider_key ) { |
| 83 | $post_type = substr( $provider_key, strlen( static::$name ) + 1 ); |
| 84 | |
| 85 | switch ( $post_type ) { |
| 86 | case 'post': |
| 87 | return __( 'Single post view', 'jetpack-boost' ); |
| 88 | |
| 89 | case 'page': |
| 90 | return __( 'Single page view', 'jetpack-boost' ); |
| 91 | |
| 92 | case 'product': |
| 93 | return __( 'Single product view', 'jetpack-boost' ); |
| 94 | |
| 95 | default: |
| 96 | return __( 'Custom post type', 'jetpack-boost' ); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Get post types that need Critical CSS. |
| 102 | * |
| 103 | * @return mixed|void |
| 104 | */ |
| 105 | public static function get_post_types() { |
| 106 | $post_types = get_post_types( |
| 107 | array( |
| 108 | 'public' => true, |
| 109 | ), |
| 110 | 'objects' |
| 111 | ); |
| 112 | unset( $post_types['attachment'] ); |
| 113 | |
| 114 | $post_types = array_filter( $post_types, 'is_post_type_viewable' ); |
| 115 | |
| 116 | $provider_post_types = array(); |
| 117 | // Generate a name => name array for backwards compatibility. |
| 118 | foreach ( $post_types as $post_type ) { |
| 119 | $provider_post_types[ $post_type->name ] = $post_type->name; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Filters the post types used for Critical CSS |
| 124 | * |
| 125 | * @param array $post_types The array of post types to be used |
| 126 | * |
| 127 | * @since 1.0.0 |
| 128 | */ |
| 129 | return apply_filters( |
| 130 | 'jetpack_boost_critical_css_post_types_singular', |
| 131 | apply_filters_deprecated( |
| 132 | 'jetpack_boost_critical_css_post_types', |
| 133 | array( |
| 134 | $provider_post_types, |
| 135 | ), |
| 136 | '3.4.0', |
| 137 | 'jetpack_boost_critical_css_post_types_singular' |
| 138 | ), |
| 139 | $post_types |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Create a new WP_Query to gather sample posts. |
| 145 | * |
| 146 | * @param string $post_type post type. |
| 147 | * |
| 148 | * @return \WP_Query |
| 149 | */ |
| 150 | public static function post_type_query( $post_type ) { |
| 151 | /** |
| 152 | * Filters the WP_Query parameters used to gather sample posts |
| 153 | * |
| 154 | * @param array $args The arguments that will be used by WP_Query |
| 155 | * |
| 156 | * @since 1.0.0 |
| 157 | */ |
| 158 | $args = apply_filters( |
| 159 | 'jetpack_boost_critical_css_post_type_query', |
| 160 | array( |
| 161 | 'orderby' => 'ID', |
| 162 | 'post_type' => $post_type, |
| 163 | 'posts_per_page' => static::MAX_URLS, |
| 164 | 'post_status' => array( 'publish' ), |
| 165 | 'no_found_rows' => true, |
| 166 | 'update_post_term_cache' => false, |
| 167 | 'update_post_meta_cache' => false, |
| 168 | ) |
| 169 | ); |
| 170 | |
| 171 | return new \WP_Query( $args ); |
| 172 | } |
| 173 | |
| 174 | /** @inheritdoc */ |
| 175 | public static function get_success_ratio() { |
| 176 | return static::MIN_SUCCESS_URLS / static::MAX_URLS; |
| 177 | } |
| 178 | } |