Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
WP_Core_Provider
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 6
650
0.00% covered (danger)
0.00%
0 / 1
 get_critical_source_urls
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
132
 get_keys
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 get_current_storage_keys
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 get_edit_url
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 describe_key
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 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 core 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 WP_Core_Provider.
12 *
13 * @package Automattic\Jetpack_Boost\Modules\Critical_CSS\Providers
14 */
15class WP_Core_Provider extends Provider {
16
17    /**
18     * Provider name.
19     *
20     * @var string
21     */
22    protected static $name = 'core';
23
24    /** @inheritdoc */
25    public static function get_critical_source_urls( $context_posts = array() ) {
26        $urls = array();
27
28        $front_page = (int) get_option( 'page_on_front' );
29        $posts_page = (int) get_option( 'page_for_posts' );
30
31        if ( ! empty( $front_page ) && empty( $context_posts ) ) {
32            $permalink = get_permalink( $front_page );
33            if ( ! empty( $permalink ) ) {
34                $urls['front_page'] = array( $permalink );
35            }
36        }
37
38        $context_post_types = wp_list_pluck( $context_posts, 'post_type' );
39        $context_post_ids   = wp_list_pluck( $context_posts, 'ID' );
40
41        // The blog page is only in context if the context posts include a 'post' post_type.
42        // Or, if the blog page itself is in context.
43        if ( empty( $context_post_types ) || in_array( 'post', $context_post_types, true ) || in_array( $posts_page, $context_post_ids, true ) ) {
44            if ( ! empty( $posts_page ) ) {
45                $permalink = get_permalink( $posts_page );
46                if ( ! empty( $permalink ) ) {
47                    $urls['posts_page'] = array( $permalink );
48                }
49            }
50        }
51
52        if ( ! $front_page && ! isset( $urls['posts_page'] ) ) {
53            $urls['posts_page'] = array( home_url( '/' ) );
54        }
55
56        return $urls;
57    }
58
59    /** @inheritdoc */
60    public static function get_keys() {
61        $keys = array( 'posts_page' );
62
63        if ( ! empty( get_option( 'page_on_front' ) ) ) {
64            $keys[] = 'front_page';
65        }
66
67        return $keys;
68    }
69
70    /** @inheritdoc */
71    public static function get_current_storage_keys() {
72        if ( is_home() ) {
73            $key = 'posts_page';
74        } elseif ( is_front_page() ) {
75            $key = 'front_page';
76        }
77
78        if ( ! isset( $key ) ) {
79            return array();
80        }
81
82        // For example: "core_posts_page".
83        return array( self::$name . '_' . $key );
84    }
85
86    /** @inheritdoc */
87    public static function get_edit_url( $provider_key ) {
88        if ( $provider_key === 'core_front_page' ) {
89            $front_page_id = get_option( 'page_on_front' );
90            if ( ! empty( $front_page_id ) ) {
91                return get_edit_post_link( $front_page_id, 'link' );
92            }
93        }
94
95        return null;
96    }
97
98    /** @inheritdoc */
99    public static function describe_key( $provider_key ) {
100        $page = substr( $provider_key, strlen( static::$name ) + 1 );
101
102        switch ( $page ) {
103            case 'posts_page':
104                return __( 'Posts page', 'jetpack-boost' );
105
106            case 'front_page':
107                return __( 'Front page', 'jetpack-boost' );
108
109            default:
110                return $provider_key;
111        }
112    }
113
114    /** @inheritdoc */
115    public static function get_success_ratio() {
116        return 1;
117    }
118}