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