Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 86 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Site_Urls | |
0.00% |
0 / 86 |
|
0.00% |
0 / 6 |
182 | |
0.00% |
0 / 1 |
| get | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
2 | |||
| get_wp_core_urls | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
12 | |||
| get_post_urls | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
6 | |||
| cleanup_post_urls | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| get_public_post_types | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
2 | |||
| get_post_group | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack_Boost\Lib; |
| 4 | |
| 5 | class Site_Urls { |
| 6 | |
| 7 | public static function get( $limit = 100 ) { |
| 8 | $core_urls = self::get_wp_core_urls(); |
| 9 | $post_urls = self::cleanup_post_urls( |
| 10 | self::get_post_urls( $limit ), |
| 11 | wp_list_pluck( |
| 12 | $core_urls, |
| 13 | 'url' |
| 14 | ) |
| 15 | ); |
| 16 | |
| 17 | $urls = array_slice( |
| 18 | array_merge( |
| 19 | $core_urls, |
| 20 | $post_urls |
| 21 | ), |
| 22 | 0, |
| 23 | $limit |
| 24 | ); |
| 25 | |
| 26 | /** |
| 27 | * Filters the list of site URLs used by the Image Size Analysis. |
| 28 | * |
| 29 | * @since 3.7.0 |
| 30 | * |
| 31 | * @param array $urls An array of URLs. |
| 32 | * @param int $limit The maximum number of URLs that should be returned. |
| 33 | */ |
| 34 | return apply_filters( 'jetpack_boost_site_urls', $urls, $limit ); |
| 35 | } |
| 36 | |
| 37 | private static function get_wp_core_urls() { |
| 38 | $urls = array(); |
| 39 | |
| 40 | $front_page = get_option( 'page_on_front' ); |
| 41 | if ( ! empty( $front_page ) ) { |
| 42 | $urls['core_front_page'] = array( |
| 43 | 'url' => get_permalink( $front_page ), |
| 44 | 'modified' => get_post_modified_time( 'Y-m-d H:i:s', false, $front_page ), |
| 45 | 'group' => 'core_front_page', |
| 46 | ); |
| 47 | } else { |
| 48 | $urls['core_front_page'] = array( |
| 49 | 'url' => home_url( '/' ), |
| 50 | 'modified' => current_time( 'Y-m-d H:i:s' ), |
| 51 | 'group' => 'core_front_page', |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | $posts_page = get_option( 'page_for_posts' ); |
| 56 | if ( ! empty( $posts_page ) ) { |
| 57 | $urls['core_posts_page'] = array( |
| 58 | 'url' => get_permalink( $posts_page ), |
| 59 | 'modified' => get_post_modified_time( 'Y-m-d H:i:s', false, $posts_page ), |
| 60 | 'group' => 'other', |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | return $urls; |
| 65 | } |
| 66 | |
| 67 | private static function get_post_urls( $limit ) { |
| 68 | global $wpdb; |
| 69 | |
| 70 | $public_post_types = self::get_public_post_types(); |
| 71 | $post_types_placeholders = implode( |
| 72 | ',', |
| 73 | array_fill( |
| 74 | 0, |
| 75 | count( $public_post_types ), |
| 76 | '%s' |
| 77 | ) |
| 78 | ); |
| 79 | |
| 80 | $prepare_values = $public_post_types; |
| 81 | $prepare_values[] = $limit; |
| 82 | |
| 83 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 84 | $results = $wpdb->get_results( |
| 85 | $wpdb->prepare( |
| 86 | // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 87 | "SELECT ID, post_modified FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types_placeholders}) ORDER BY post_modified DESC LIMIT 0, %d", |
| 88 | $prepare_values |
| 89 | ) |
| 90 | ); |
| 91 | |
| 92 | $urls = array(); |
| 93 | foreach ( $results as $result ) { |
| 94 | $urls[ 'post_id_' . $result->ID ] = array( |
| 95 | 'url' => get_permalink( $result->ID ), |
| 96 | 'modified' => get_post_modified_time( 'Y-m-d H:i:s', false, $result ), |
| 97 | 'group' => self::get_post_group( $result ), |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | return $urls; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Removes duplicate URLs from the $post_urls list |
| 106 | * based on the additional URLs. |
| 107 | * |
| 108 | * @param array $post_urls List of URLs to cleanup. |
| 109 | * @param array $additional_urls List of URLs to lookup while cleaning. |
| 110 | * |
| 111 | * @return array |
| 112 | */ |
| 113 | private static function cleanup_post_urls( $post_urls, $additional_urls ) { |
| 114 | $clean = array(); |
| 115 | |
| 116 | foreach ( $post_urls as $key => $item ) { |
| 117 | if ( in_array( $item['url'], $additional_urls, true ) ) { |
| 118 | continue; |
| 119 | } |
| 120 | |
| 121 | $clean[ $key ] = $item; |
| 122 | } |
| 123 | |
| 124 | return $clean; |
| 125 | } |
| 126 | |
| 127 | private static function get_public_post_types() { |
| 128 | $post_types = get_post_types( |
| 129 | array( |
| 130 | 'public' => true, |
| 131 | ) |
| 132 | ); |
| 133 | unset( $post_types['attachment'] ); |
| 134 | |
| 135 | return array_values( |
| 136 | array_filter( |
| 137 | $post_types, |
| 138 | 'is_post_type_viewable' |
| 139 | ) |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Returns the group for the post. |
| 145 | * |
| 146 | * @param \WP_Post $p Post object. |
| 147 | * |
| 148 | * @return string |
| 149 | */ |
| 150 | private static function get_post_group( $p ) { |
| 151 | $post_type = get_post_type( $p->ID ); |
| 152 | if ( 'post' === $post_type || 'page' === $post_type ) { |
| 153 | return 'singular_' . $post_type; |
| 154 | } |
| 155 | |
| 156 | return 'other'; |
| 157 | } |
| 158 | } |