Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Taxonomy_Provider
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 8
420
0.00% covered (danger)
0.00%
0 / 1
 get_critical_source_urls
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
56
 get_current_storage_keys
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 get_keys
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 describe_key
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 get_edit_url
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_available_taxonomies
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 get_terms
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 * Provides taxonomy support for critical CSS
4 *
5 * @package automattic/jetpack-boost
6 */
7
8namespace Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers;
9
10/**
11 * Class Taxonomy_Provider
12 *
13 * @package Automattic\Jetpack_Boost\Modules\Critical_CSS\Providers
14 */
15class Taxonomy_Provider extends Provider {
16
17    /**
18     * Provider name.
19     *
20     * @var string
21     */
22    protected static $name = 'taxonomy';
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        $results = array();
42
43        $taxonomies = self::get_available_taxonomies();
44        if ( ! empty( $context_posts ) ) {
45            $context_post_types = array_unique( wp_list_pluck( $context_posts, 'post_type' ) );
46            $context_taxonomies = get_object_taxonomies( $context_post_types, 'names' );
47            $taxonomies         = array_intersect( $taxonomies, $context_taxonomies );
48        }
49
50        foreach ( $taxonomies as $taxonomy ) {
51            $terms = self::get_terms( $taxonomy );
52
53            if ( ! $terms ) {
54                continue;
55            }
56
57            foreach ( $terms as $term ) {
58                $url = get_term_link( $term, $taxonomy );
59                if ( ! is_wp_error( $url ) && ! empty( $url ) ) {
60                    $results[ $taxonomy ][] = $url;
61                }
62            }
63        }
64
65        return $results;
66    }
67
68    // phpcs:ignore Generic.Commenting.DocComment.MissingShort
69    /** @inheritdoc */
70    public static function get_current_storage_keys() {
71        if ( ! is_category() && ! is_tax() ) {
72            return array();
73        }
74
75        if ( ! get_queried_object() ) {
76            return array();
77        }
78
79        // For example: "taxonomy_category".
80        return array( self::$name . '_' . get_queried_object()->taxonomy );
81    }
82
83    // phpcs:ignore Generic.Commenting.DocComment.MissingShort
84    /** @inheritdoc */
85    public static function get_keys() {
86        return array_keys(
87            array_filter(
88                self::get_available_taxonomies(),
89                function ( $taxonomy ) {
90                    return ! empty( Taxonomy_Provider::get_terms( $taxonomy ) );
91                }
92            )
93        );
94    }
95
96    // phpcs:ignore
97    /** @inheritdoc */
98    public static function describe_key( $provider_key ) { // phpcs:ignore Generic.Commenting.DocComment.MissingShort
99        $taxonomy = substr( $provider_key, strlen( static::$name ) + 1 );
100
101        switch ( $taxonomy ) {
102            case 'category':
103                return __( 'Category view', 'jetpack-boost' );
104
105            default:
106                return __( 'View for custom taxonomy', 'jetpack-boost' );
107        }
108    }
109
110    // phpcs:ignore Generic.Commenting.DocComment.MissingShort
111    /** @inheritdoc */
112    public static function get_edit_url( $_provider_key ) { // phpcs:ignore Generic.Commenting.DocComment.MissingShort
113        return null;
114    }
115
116    /**
117     * Which taxonomies should Critical CSS be generated for.
118     *
119     * @return array
120     */
121    public static function get_available_taxonomies() {
122        $taxonomies = get_taxonomies(
123            array(
124                'public'       => true,
125                'show_in_rest' => true,
126            ),
127            'objects'
128        );
129
130        $taxonomies = array_filter( $taxonomies, 'is_taxonomy_viewable' );
131
132        $provider_taxonomies = array();
133        // Generate a name => name array for backwards compatibility.
134        foreach ( $taxonomies as $taxonomy ) {
135            $provider_taxonomies[ $taxonomy->name ] = $taxonomy->name;
136        }
137
138        return $provider_taxonomies;
139    }
140
141    /**
142     * Get a couple sample terms for a taxonomy.
143     *
144     * @param string $taxonomy Taxonomy.
145     *
146     * @return array
147     */
148    public static function get_terms( $taxonomy ) {
149        /**
150         * Filters the WP_Term_Query args to get a sample of terms for a taxonomy
151         *
152         * @param array $args The arguments that will be used by WP_Term_Query
153         *
154         * @since   1.0.0
155         */
156        $args = apply_filters(
157            'jetpack_boost_critical_css_terms_query',
158            array(
159                'fields'                 => 'ids',
160                'taxonomy'               => $taxonomy,
161                'orderby'                => 'term_order',
162                'number'                 => static::MAX_URLS,
163                'hide_empty'             => true,
164                'hierarchical'           => false,
165                'update_term_meta_cache' => false,
166            )
167        );
168
169        return ( new \WP_Term_Query( $args ) )->terms;
170    }
171
172    // phpcs:ignore Generic.Commenting.DocComment.MissingShort
173    /** @inheritdoc */
174    public static function get_success_ratio() {
175        return static::MIN_SUCCESS_URLS / static::MAX_URLS;
176    }
177}