Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Archive_Provider
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 7
240
0.00% covered (danger)
0.00%
0 / 1
 get_critical_source_urls
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 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 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 get_post_types
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
6
 get_success_ratio
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Archive provider class.
4 *
5 * @package automattic/jetpack-boost
6 */
7
8namespace Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers;
9
10/**
11 * Class Archive_Provider
12 *
13 * @package Automattic\Jetpack_Boost\Modules\Critical_CSS\Providers
14 */
15class Archive_Provider extends Provider {
16
17    /**
18     * Provider name.
19     *
20     * @var string
21     */
22    protected static $name = 'archive';
23
24    /** @inheritdoc */
25    public static function get_critical_source_urls( $context_posts = array() ) {
26        $links              = array();
27        $context_post_types = wp_list_pluck( $context_posts, 'post_type' );
28
29        $post_types = self::get_post_types();
30        if ( ! empty( $context_post_types ) ) {
31            $post_types = array_intersect( $post_types, $context_post_types );
32        }
33        foreach ( $post_types as $post_type ) {
34            $link = get_post_type_archive_link( $post_type );
35
36            if ( ! empty( $link ) ) {
37                $links[ $post_type ][] = $link;
38            }
39        }
40
41        return $links;
42    }
43
44    /** @inheritdoc */
45    public static function get_current_storage_keys() {
46        if ( ! is_archive() ) {
47            return array();
48        }
49
50        // For example: "archive_post".
51        return array( self::$name . '_' . get_post_type() );
52    }
53
54    /** @inheritdoc */
55    public static function get_keys() {
56        return self::get_post_types();
57    }
58
59    /** @inheritdoc */
60    public static function get_edit_url( $_provider_key ) {
61        return null;
62    }
63
64    /** @inheritdoc */
65    public static function describe_key( $provider_key ) {
66        $post_type = substr( $provider_key, strlen( static::$name ) + 1 );
67
68        switch ( $post_type ) {
69            case 'post':
70                return __( 'Post archive view', 'jetpack-boost' );
71
72            case 'page':
73                return __( 'Page archive view', 'jetpack-boost' );
74
75            default:
76                return __( 'Archive page for custom post type', 'jetpack-boost' );
77        }
78    }
79
80    /**
81     * Get post types that need Critical CSS.
82     *
83     * @return mixed|void
84     */
85    public static function get_post_types() {
86        $post_types = get_post_types(
87            array(
88                'public'      => true,
89                'has_archive' => true,
90            ),
91            'objects'
92        );
93        unset( $post_types['attachment'] );
94
95        $post_types = array_filter( $post_types, 'is_post_type_viewable' );
96
97        $provider_post_types = array();
98        // Generate a name => name array for backwards compatibility.
99        foreach ( $post_types as $post_type ) {
100            $provider_post_types[ $post_type->name ] = $post_type->name;
101        }
102
103        /**
104         * Filters the post types used for Critical CSS
105         *
106         * @param array $post_types The array of post types to be used
107         *
108         * @since   1.0.0
109         */
110        return apply_filters(
111            'jetpack_boost_critical_css_post_types_archives',
112            apply_filters_deprecated(
113                'jetpack_boost_critical_css_post_types',
114                array(
115                    $provider_post_types,
116                ),
117                '3.4.0',
118                'jetpack_boost_critical_css_post_types_archives'
119            ),
120            $post_types
121        );
122    }
123
124    /** @inheritdoc */
125    public static function get_success_ratio() {
126        return 1;
127    }
128}