PluginProbe
ElasticPress / 5.0.0
ElasticPress v5.0.0
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / includes / classes / StatusReport / IndexableContent.php

IndexableContent.php in ElasticPress 5.0.0, at includes/classes/StatusReport/IndexableContent.php

193 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 Report {
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() : 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 &mdash; %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
97 foreach ( $post_types as $post_type ) {
98 $post_type_obj = get_post_type_object( $post_type );
99 $post_type_label = $post_type_obj ? $post_type_obj->labels->name : $post_type;
100
101 $post_count = (array) wp_count_posts( $post_type );
102
103 $post_count = array_reduce(
104 array_keys( $post_count ),
105 function ( $count, $post_status ) use ( $post_count, $post_stati ) {
106 if ( in_array( $post_status, $post_stati, true ) ) {
107 $count += $post_count[ $post_status ];
108 }
109 return $count;
110 },
111 0
112 );
113
114 $fields[ $post_type . '_count' ] = [
115 'label' => sprintf( '%s (%s)', $post_type_label, $post_type ),
116 'value' => number_format_i18n( $post_count ),
117 ];
118 }
119
120 return $fields;
121 }
122
123 /**
124 * Process the count of meta keys.
125 *
126 * @return array
127 */
128 protected function get_post_meta_fields() : array {
129 $post_indexable = \ElasticPress\Indexables::factory()->get( 'post' );
130 $post_types = $post_indexable->get_indexable_post_types();
131
132 $force_refresh = ! empty( $_GET['force_refresh'] ); // phpcs:ignore WordPress.Security.NonceVerification
133
134 $fields = [];
135 $all_keys = [];
136 $post_count_limit = 88000;
137
138 foreach ( $post_types as $post_type ) {
139 $post_type_obj = get_post_type_object( $post_type );
140
141 $meta_keys_post_type = $post_indexable->get_indexable_meta_keys_per_post_type( $post_type, $force_refresh );
142 $all_keys = array_merge( $all_keys, $meta_keys_post_type );
143
144 $post_count = array_sum( (array) wp_count_posts( $post_type ) );
145 $limited = $post_count > $post_count_limit;
146
147 $post_type_label = $post_type_obj ?
148 sprintf( '%s (%s) Meta Keys', $post_type_obj->labels->singular_name, $post_type ) :
149 $post_type;
150
151 $value = count( $meta_keys_post_type );
152
153 $description = $limited
154 ? sprintf(
155 /* translators: %1$s: Post count limit (defaults to 80,000). %2$s: Post type name. */
156 _n(
157 'For performance reasons the reported count is based on the first %1$s %2$s only. The actual number may be higher.',
158 'For performance reasons the reported count is based on the first %1$s %2$s only. The actual number may be higher.',
159 $post_count_limit,
160 'elasticpress'
161 ),
162 number_format_i18n( $post_count_limit ),
163 // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingle,WordPress.WP.I18n.NonSingularStringLiteralPlural
164 _n( $post_type_obj->labels->singular_name, $post_type_obj->labels->name, $post_count_limit )
165 ) : '';
166
167 $fields[ $post_type . '_meta_keys' ] = [
168 'label' => $post_type_label,
169 'description' => $description,
170 'value' => number_format_i18n( $value ),
171 ];
172 }
173
174 if ( $limited ) {
175 $all_keys = $post_indexable->get_predicted_indexable_meta_keys( $force_refresh );
176 } else {
177 $all_keys = array_unique( $all_keys );
178 }
179
180 $fields['total-all-post-types'] = [
181 'label' => __( 'Total Distinct Meta Keys', 'elasticpress' ),
182 'value' => count( $all_keys ),
183 ];
184
185 $fields['distinct-meta-keys'] = [
186 'label' => __( 'Distinct Meta Keys', 'elasticpress' ),
187 'value' => wp_sprintf( '%l', $all_keys ),
188 ];
189
190 return $fields;
191 }
192 }
193