Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 48 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| Post_ID_Provider | |
0.00% |
0 / 48 |
|
0.00% |
0 / 10 |
420 | |
0.00% |
0 / 1 |
| get_critical_source_urls | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
42 | |||
| get_current_storage_keys | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| describe_key | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| get_edit_url | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| get_current_page_key | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| get_post_ids | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| add_post_id | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| get_keys | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_posts | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
6 | |||
| get_success_ratio | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * The Post ID provider class. |
| 4 | * |
| 5 | * @package automattic/jetpack-boost |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers; |
| 9 | |
| 10 | /** |
| 11 | * Class Post_ID_Provider |
| 12 | * |
| 13 | * @package Automattic\Jetpack_Boost\Modules\Critical_CSS\Providers |
| 14 | */ |
| 15 | class Post_ID_Provider extends Provider { |
| 16 | |
| 17 | /** |
| 18 | * Post Ids storage key. |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | const STORAGE_KEY = 'jetpack_boost_critical_css_post_ids'; |
| 23 | |
| 24 | /** |
| 25 | * Provider name. |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | protected static $name = 'post_id'; |
| 30 | |
| 31 | /** @inheritdoc */ |
| 32 | public static function get_critical_source_urls( $context_posts = array() ) { |
| 33 | $results = array(); |
| 34 | $query = self::get_posts(); |
| 35 | $context_post_ids = wp_list_pluck( $context_posts, 'ID' ); |
| 36 | |
| 37 | if ( false === $query ) { |
| 38 | return array(); |
| 39 | } |
| 40 | |
| 41 | foreach ( $query->posts as $post ) { |
| 42 | if ( empty( $context_post_ids ) || in_array( $post->ID, $context_post_ids, true ) ) { |
| 43 | $url = get_permalink( $post ); |
| 44 | if ( ! empty( $url ) ) { |
| 45 | $results[ $post->ID ] = array( $url ); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return $results; |
| 51 | } |
| 52 | |
| 53 | /** @inheritdoc */ |
| 54 | public static function get_current_storage_keys() { |
| 55 | if ( ! is_singular() ) { |
| 56 | return array(); |
| 57 | } |
| 58 | |
| 59 | // For example: "post_id_123". |
| 60 | return array( self::$name . '_' . get_the_ID() ); |
| 61 | } |
| 62 | |
| 63 | /** @inheritdoc */ |
| 64 | public static function describe_key( $provider_key ) { |
| 65 | $post_id = (int) substr( $provider_key, strlen( self::$name ) + 1 ); |
| 66 | |
| 67 | $post = get_post( $post_id ); |
| 68 | if ( ! $post ) { |
| 69 | /* translators: %d is the id of a post which cannot be found. */ |
| 70 | return sprintf( __( 'Post %d', 'jetpack-boost' ), $post_id ); |
| 71 | } |
| 72 | |
| 73 | return $post->post_title; |
| 74 | } |
| 75 | |
| 76 | /** @inheritdoc */ |
| 77 | public static function get_edit_url( $provider_key ) { |
| 78 | $post_id = (int) substr( $provider_key, strlen( self::$name ) + 1 ); |
| 79 | |
| 80 | return get_edit_post_link( $post_id, 'link' ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns a key that can be used to identify the current page, if any exists. |
| 85 | * |
| 86 | * @return string|null |
| 87 | */ |
| 88 | public static function get_current_page_key() { |
| 89 | $keys = static::get_current_storage_keys(); |
| 90 | |
| 91 | if ( count( $keys ) > 0 ) { |
| 92 | return $keys[0]; |
| 93 | } else { |
| 94 | return null; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Get post ids. |
| 100 | * |
| 101 | * @return array |
| 102 | */ |
| 103 | public static function get_post_ids() { |
| 104 | // Store the IDs somewhere. |
| 105 | return get_option( self::STORAGE_KEY, array() ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Add post id to storage. |
| 110 | * |
| 111 | * @param int $post_id Post Id. |
| 112 | * |
| 113 | * @return bool |
| 114 | */ |
| 115 | public static function add_post_id( $post_id ) { |
| 116 | $post_ids = static::get_post_ids(); |
| 117 | |
| 118 | if ( in_array( $post_id, $post_ids, true ) ) { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | $post_ids[] = (int) $post_id; |
| 123 | |
| 124 | return update_option( self::STORAGE_KEY, $post_ids ); |
| 125 | } |
| 126 | |
| 127 | /** @inheritdoc */ |
| 128 | public static function get_keys() { |
| 129 | return self::get_post_ids(); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Create a new WP_Query to gather sample posts. |
| 134 | * |
| 135 | * @return false|\WP_Query |
| 136 | */ |
| 137 | public static function get_posts() { |
| 138 | $ids = self::get_post_ids(); |
| 139 | |
| 140 | if ( ! $ids ) { |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | return new \WP_Query( |
| 145 | array( |
| 146 | 'post__in' => $ids, |
| 147 | 'posts_per_page' => count( $ids ), |
| 148 | 'post_status' => array( 'publish' ), |
| 149 | 'post_type' => 'any', |
| 150 | 'no_found_rows' => true, |
| 151 | 'ignore_sticky_posts' => true, |
| 152 | 'update_post_term_cache' => false, |
| 153 | 'update_post_meta_cache' => false, |
| 154 | ) |
| 155 | ); |
| 156 | } |
| 157 | |
| 158 | /** @inheritdoc */ |
| 159 | public static function get_success_ratio() { |
| 160 | return 1; |
| 161 | } |
| 162 | } |