| 1 |
<?php |
| 2 |
/** |
| 3 |
* Indexable Content report class |
| 4 |
* |
| 5 |
* @since 4.4.0 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress\StatusReport; |
| 10 |
|
| 11 |
use ElasticPress\Utils; |
| 12 |
|
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
/** |
| 16 |
* IndexableContent report class |
| 17 |
* |
| 18 |
* @package ElasticPress |
| 19 |
*/ |
| 20 |
class IndexableContent extends AjaxReport { |
| 21 |
|
| 22 |
/** |
| 23 |
* Return the report title |
| 24 |
* |
| 25 |
* @return string |
| 26 |
*/ |
| 27 |
public function get_title(): string { |
| 28 |
return __( 'Indexable Content', 'elasticpress' ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Return the report fields |
| 33 |
* |
| 34 |
* @return array |
| 35 |
*/ |
| 36 |
public function get_groups_ajax(): array { |
| 37 |
return $this->get_indexable_content_groups(); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Process the indexable content for all sites. |
| 42 |
* |
| 43 |
* @return array |
| 44 |
*/ |
| 45 |
protected function get_indexable_content_groups(): array { |
| 46 |
$groups = []; |
| 47 |
|
| 48 |
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { |
| 49 |
$sites = Utils\get_sites( 0, true ); |
| 50 |
foreach ( $sites as $site ) { |
| 51 |
switch_to_blog( $site['blog_id'] ); |
| 52 |
|
| 53 |
$groups[] = $this->get_indexable_content_group(); |
| 54 |
|
| 55 |
restore_current_blog(); |
| 56 |
} |
| 57 |
} else { |
| 58 |
$groups = [ $this->get_indexable_content_group() ]; |
| 59 |
} |
| 60 |
|
| 61 |
return $groups; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Process the indexable content. |
| 66 |
* |
| 67 |
* @return array |
| 68 |
*/ |
| 69 |
protected function get_indexable_content_group(): array { |
| 70 |
$post_counts = $this->get_post_count_group(); |
| 71 |
$meta_counts = $this->get_post_meta_fields(); |
| 72 |
|
| 73 |
$fields = array_merge( $post_counts, $meta_counts ); |
| 74 |
|
| 75 |
return [ |
| 76 |
'title' => sprintf( |
| 77 |
/* translators: %1%s: Site name. %2$s: Site URL. */ |
| 78 |
__( '%1$s — %2$s', 'ep' ), |
| 79 |
get_option( 'blogname' ), |
| 80 |
site_url() |
| 81 |
), |
| 82 |
'fields' => $fields, |
| 83 |
]; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Process the count of all indexable post types and status |
| 88 |
* |
| 89 |
* @return array |
| 90 |
*/ |
| 91 |
protected function get_post_count_group(): array { |
| 92 |
$post_indexable = \ElasticPress\Indexables::factory()->get( 'post' ); |
| 93 |
$post_types = $post_indexable->get_indexable_post_types(); |
| 94 |
|
| 95 |
$post_stati = $post_indexable->get_indexable_post_status(); |
| 96 |
$fields = []; |
| 97 |
|
| 98 |
foreach ( $post_types as $post_type ) { |
| 99 |
$post_type_obj = get_post_type_object( $post_type ); |
| 100 |
$post_type_label = $post_type_obj ? $post_type_obj->labels->name : $post_type; |
| 101 |
|
| 102 |
$post_count = (array) wp_count_posts( $post_type ); |
| 103 |
|
| 104 |
$post_count = array_reduce( |
| 105 |
array_keys( $post_count ), |
| 106 |
function ( $count, $post_status ) use ( $post_count, $post_stati ) { |
| 107 |
if ( in_array( $post_status, $post_stati, true ) ) { |
| 108 |
$count += $post_count[ $post_status ]; |
| 109 |
} |
| 110 |
return $count; |
| 111 |
}, |
| 112 |
0 |
| 113 |
); |
| 114 |
|
| 115 |
$fields[ $post_type . '_count' ] = [ |
| 116 |
'label' => sprintf( '%s (%s)', $post_type_label, $post_type ), |
| 117 |
'value' => number_format_i18n( $post_count ), |
| 118 |
]; |
| 119 |
} |
| 120 |
|
| 121 |
return $fields; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Process the count of meta keys. |
| 126 |
* |
| 127 |
* @return array |
| 128 |
*/ |
| 129 |
protected function get_post_meta_fields(): array { |
| 130 |
$post_indexable = \ElasticPress\Indexables::factory()->get( 'post' ); |
| 131 |
$post_types = $post_indexable->get_indexable_post_types(); |
| 132 |
|
| 133 |
$force_refresh = ! empty( $_GET['force_refresh'] ); // phpcs:ignore WordPress.Security.NonceVerification |
| 134 |
|
| 135 |
$fields = []; |
| 136 |
$all_keys = []; |
| 137 |
$post_count_limit = 88000; |
| 138 |
$limited = false; |
| 139 |
|
| 140 |
foreach ( $post_types as $post_type ) { |
| 141 |
$post_type_obj = get_post_type_object( $post_type ); |
| 142 |
|
| 143 |
$meta_keys_post_type = $post_indexable->get_indexable_meta_keys_per_post_type( $post_type, $force_refresh ); |
| 144 |
$all_keys = array_merge( $all_keys, $meta_keys_post_type ); |
| 145 |
|
| 146 |
$post_count = array_sum( (array) wp_count_posts( $post_type ) ); |
| 147 |
$limited = $post_count > $post_count_limit; |
| 148 |
|
| 149 |
$post_type_label = $post_type_obj ? |
| 150 |
sprintf( '%s (%s) Meta Keys', $post_type_obj->labels->singular_name, $post_type ) : |
| 151 |
$post_type; |
| 152 |
|
| 153 |
$value = count( $meta_keys_post_type ); |
| 154 |
|
| 155 |
$description = $limited |
| 156 |
? sprintf( |
| 157 |
/* translators: %1$s: Post count limit (defaults to 80,000). %2$s: Post type name. */ |
| 158 |
_n( |
| 159 |
'For performance reasons the reported count is based on the first %1$s %2$s only. The actual number may be higher.', |
| 160 |
'For performance reasons the reported count is based on the first %1$s %2$s only. The actual number may be higher.', |
| 161 |
$post_count_limit, |
| 162 |
'elasticpress' |
| 163 |
), |
| 164 |
number_format_i18n( $post_count_limit ), |
| 165 |
// phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingle,WordPress.WP.I18n.NonSingularStringLiteralPlural |
| 166 |
_n( $post_type_obj->labels->singular_name, $post_type_obj->labels->name, $post_count_limit ) |
| 167 |
) : ''; |
| 168 |
|
| 169 |
$fields[ $post_type . '_meta_keys' ] = [ |
| 170 |
'label' => $post_type_label, |
| 171 |
'description' => $description, |
| 172 |
'value' => number_format_i18n( $value ), |
| 173 |
]; |
| 174 |
} |
| 175 |
|
| 176 |
if ( $limited ) { |
| 177 |
$all_keys = $post_indexable->get_predicted_indexable_meta_keys( $force_refresh ); |
| 178 |
} else { |
| 179 |
$all_keys = array_unique( $all_keys ); |
| 180 |
} |
| 181 |
|
| 182 |
$fields['total-all-post-types'] = [ |
| 183 |
'label' => __( 'Total Distinct Meta Keys', 'elasticpress' ), |
| 184 |
'value' => count( $all_keys ), |
| 185 |
]; |
| 186 |
|
| 187 |
$fields['distinct-meta-keys'] = [ |
| 188 |
'label' => __( 'Distinct Meta Keys', 'elasticpress' ), |
| 189 |
'value' => wp_sprintf( '%l', $all_keys ), |
| 190 |
]; |
| 191 |
|
| 192 |
return $fields; |
| 193 |
} |
| 194 |
} |
| 195 |
|