PluginProbe
ElasticPress / 4.6.0
ElasticPress v4.6.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 4.6.0, at includes/classes/StatusReport/IndexableContent.php

197 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();
50 foreach ( $sites as $site ) {
51 if ( ! Utils\is_site_indexable( $site['blog_id'] ) ) {
52 continue;
53 }
54
55 switch_to_blog( $site['blog_id'] );
56
57 $groups[] = $this->get_indexable_content_group();
58
59 restore_current_blog();
60 }
61 } else {
62 $groups = [ $this->get_indexable_content_group() ];
63 }
64
65 return $groups;
66 }
67
68 /**
69 * Process the indexable content.
70 *
71 * @return array
72 */
73 protected function get_indexable_content_group() : array {
74 $post_counts = $this->get_post_count_group();
75 $meta_counts = $this->get_post_meta_fields();
76
77 $fields = array_merge( $post_counts, $meta_counts );
78
79 return [
80 'title' => sprintf(
81 /* translators: %1%s: Site name. %2$s: Site URL. */
82 __( '%1$s &mdash; %2$s', 'ep' ),
83 get_option( 'blogname' ),
84 site_url()
85 ),
86 'fields' => $fields,
87 ];
88 }
89
90 /**
91 * Process the count of all indexable post types and status
92 *
93 * @return array
94 */
95 protected function get_post_count_group() : array {
96 $post_indexable = \ElasticPress\Indexables::factory()->get( 'post' );
97 $post_types = $post_indexable->get_indexable_post_types();
98
99 $post_stati = $post_indexable->get_indexable_post_status();
100
101 foreach ( $post_types as $post_type ) {
102 $post_type_obj = get_post_type_object( $post_type );
103 $post_type_label = $post_type_obj ? $post_type_obj->labels->name : $post_type;
104
105 $post_count = (array) wp_count_posts( $post_type );
106
107 $post_count = array_reduce(
108 array_keys( $post_count ),
109 function ( $count, $post_status ) use ( $post_count, $post_stati ) {
110 if ( in_array( $post_status, $post_stati, true ) ) {
111 $count += $post_count[ $post_status ];
112 }
113 return $count;
114 },
115 0
116 );
117
118 $fields[ $post_type . '_count' ] = [
119 'label' => sprintf( '%s (%s)', $post_type_label, $post_type ),
120 'value' => number_format_i18n( $post_count ),
121 ];
122 }
123
124 return $fields;
125 }
126
127 /**
128 * Process the count of meta keys.
129 *
130 * @return array
131 */
132 protected function get_post_meta_fields() : array {
133 $post_indexable = \ElasticPress\Indexables::factory()->get( 'post' );
134 $post_types = $post_indexable->get_indexable_post_types();
135
136 $force_refresh = ! empty( $_GET['force_refresh'] ); // phpcs:ignore WordPress.Security.NonceVerification
137
138 $fields = [];
139 $all_keys = [];
140 $post_count_limit = 88000;
141
142 foreach ( $post_types as $post_type ) {
143 $post_type_obj = get_post_type_object( $post_type );
144
145 $meta_keys_post_type = $post_indexable->get_indexable_meta_keys_per_post_type( $post_type, $force_refresh );
146 $all_keys = array_merge( $all_keys, $meta_keys_post_type );
147
148 $post_count = array_sum( (array) wp_count_posts( $post_type ) );
149 $limited = $post_count > $post_count_limit;
150
151 $post_type_label = $post_type_obj ?
152 sprintf( '%s (%s) Meta Keys', $post_type_obj->labels->singular_name, $post_type ) :
153 $post_type;
154
155 $value = count( $meta_keys_post_type );
156
157 $description = $limited
158 ? sprintf(
159 /* translators: %1$s: Post count limit (defaults to 80,000). %2$s: Post type name. */
160 _n(
161 'For performance reasons the reported count is based on the first %1$s %2$s only. The actual number may be higher.',
162 'For performance reasons the reported count is based on the first %1$s %2$s only. The actual number may be higher.',
163 $post_count_limit,
164 'elasticpress'
165 ),
166 number_format_i18n( $post_count_limit ),
167 // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingle,WordPress.WP.I18n.NonSingularStringLiteralPlural
168 _n( $post_type_obj->labels->singular_name, $post_type_obj->labels->name, $post_count_limit )
169 ) : '';
170
171 $fields[ $post_type . '_meta_keys' ] = [
172 'label' => $post_type_label,
173 'description' => $description,
174 'value' => number_format_i18n( $value ),
175 ];
176 }
177
178 if ( $limited ) {
179 $all_keys = $post_indexable->get_predicted_indexable_meta_keys( $force_refresh );
180 } else {
181 $all_keys = array_unique( $all_keys );
182 }
183
184 $fields['total-all-post-types'] = [
185 'label' => __( 'Total Distinct Meta Keys', 'elasticpress' ),
186 'value' => count( $all_keys ),
187 ];
188
189 $fields['distinct-meta-keys'] = [
190 'label' => __( 'Distinct Meta Keys', 'elasticpress' ),
191 'value' => wp_sprintf( '%l', $all_keys ),
192 ];
193
194 return $fields;
195 }
196 }
197