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