| 1 |
<?php |
| 2 |
/** |
| 3 |
* ElasticPress Status Report class |
| 4 |
* |
| 5 |
* @since 4.4.0 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress\Screen; |
| 10 |
|
| 11 |
use \ElasticPress\Utils; |
| 12 |
|
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
/** |
| 16 |
* Status Report class |
| 17 |
* |
| 18 |
* @package ElasticPress |
| 19 |
*/ |
| 20 |
class StatusReport { |
| 21 |
/** |
| 22 |
* The formatted/processed reports. |
| 23 |
* |
| 24 |
* @since 4.5.0 |
| 25 |
* @var array |
| 26 |
*/ |
| 27 |
protected $formatted_reports = []; |
| 28 |
|
| 29 |
/** |
| 30 |
* Initialize class |
| 31 |
*/ |
| 32 |
public function setup() { |
| 33 |
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); |
| 34 |
add_action( 'admin_head', array( $this, 'admin_menu_count' ), 11 ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Enqueue script. |
| 39 |
* |
| 40 |
* @return void |
| 41 |
*/ |
| 42 |
public function admin_enqueue_scripts() { |
| 43 |
if ( 'status-report' !== \ElasticPress\Screen::factory()->get_current_screen() ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
$script_deps = Utils\get_asset_info( 'status-report-script', 'dependencies' ); |
| 48 |
|
| 49 |
wp_enqueue_script( |
| 50 |
'ep_admin_status_report_scripts', |
| 51 |
EP_URL . 'dist/js/status-report-script.js', |
| 52 |
array_merge( $script_deps, [ 'clipboard' ] ), |
| 53 |
Utils\get_asset_info( 'status-report-script', 'version' ), |
| 54 |
true |
| 55 |
); |
| 56 |
|
| 57 |
wp_localize_script( |
| 58 |
'ep_admin_status_report_scripts', |
| 59 |
'epStatusReport', |
| 60 |
$this->get_formatted_reports() |
| 61 |
); |
| 62 |
|
| 63 |
$style_deps = Utils\get_asset_info( 'status-report-styles', 'dependencies' ); |
| 64 |
|
| 65 |
wp_enqueue_style( |
| 66 |
'ep_status_report_styles', |
| 67 |
EP_URL . 'dist/css/status-report-styles.css', |
| 68 |
array_merge( $style_deps, [ 'wp-edit-post' ] ), |
| 69 |
Utils\get_asset_info( 'status-report-styles', 'version' ) |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Return all reports available |
| 75 |
* |
| 76 |
* @return array |
| 77 |
*/ |
| 78 |
public function get_reports() : array { |
| 79 |
$reports = []; |
| 80 |
|
| 81 |
/* this filter is documented in elasticpress.php */ |
| 82 |
$query_logger = apply_filters( 'ep_query_logger', new \ElasticPress\QueryLogger() ); |
| 83 |
|
| 84 |
if ( $query_logger ) { |
| 85 |
$reports['failed-queries'] = new \ElasticPress\StatusReport\FailedQueries( $query_logger ); |
| 86 |
} |
| 87 |
|
| 88 |
if ( Utils\is_epio() ) { |
| 89 |
$reports['autosuggest'] = new \ElasticPress\StatusReport\ElasticPressIo(); |
| 90 |
} |
| 91 |
|
| 92 |
$reports['wordpress'] = new \ElasticPress\StatusReport\WordPress(); |
| 93 |
$reports['indexable'] = new \ElasticPress\StatusReport\IndexableContent(); |
| 94 |
$reports['elasticpress'] = new \ElasticPress\StatusReport\ElasticPress(); |
| 95 |
$reports['indices'] = new \ElasticPress\StatusReport\Indices(); |
| 96 |
$reports['last-sync'] = new \ElasticPress\StatusReport\LastSync(); |
| 97 |
$reports['features'] = new \ElasticPress\StatusReport\Features(); |
| 98 |
|
| 99 |
/** |
| 100 |
* Filter the reports executed in the Status Report page. |
| 101 |
* |
| 102 |
* @since 4.4.0 |
| 103 |
* @hook ep_status_report_reports |
| 104 |
* @param {array<Report>} $reports Array of reports |
| 105 |
* @return {array<Report>} New array of reports |
| 106 |
*/ |
| 107 |
$filtered_reports = apply_filters( 'ep_status_report_reports', $reports ); |
| 108 |
|
| 109 |
$skipped_reports = ! empty( $_GET['ep-skip-reports'] ) ? (array) $_GET['ep-skip-reports'] : []; // phpcs:ignore WordPress.Security.NonceVerification |
| 110 |
$skipped_reports = array_map( 'sanitize_text_field', $skipped_reports ); |
| 111 |
|
| 112 |
$filtered_reports = array_filter( |
| 113 |
$filtered_reports, |
| 114 |
function( $report_slug ) use ( $skipped_reports ) { |
| 115 |
return ! in_array( $report_slug, $skipped_reports, true ); |
| 116 |
}, |
| 117 |
ARRAY_FILTER_USE_KEY |
| 118 |
); |
| 119 |
|
| 120 |
return $filtered_reports; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Render all reports (HTML and Copy & Paste button) |
| 125 |
*/ |
| 126 |
public function render_reports() { |
| 127 |
$reports = $this->get_formatted_reports(); |
| 128 |
|
| 129 |
$copy_paste_output = []; |
| 130 |
|
| 131 |
foreach ( $reports as $report ) { |
| 132 |
$title = $report['title']; |
| 133 |
$groups = $report['groups']; |
| 134 |
|
| 135 |
$copy_paste_output[] = $this->render_copy_paste_report( $title, $groups ); |
| 136 |
} |
| 137 |
|
| 138 |
?> |
| 139 |
<p><?php esc_html_e( 'This screen provides a list of information related to ElasticPress and synced content that can be helpful during troubleshooting. This list can also be copy/pasted and shared as needed.', 'elasticpress' ); ?></p> |
| 140 |
<p class="ep-copy-button-wrapper"> |
| 141 |
<a download="elasticpress-report.txt" href="data:text/plain;charset=utf-8,<?php echo rawurlencode( implode( "\n\n", $copy_paste_output ) ); ?>" class="button button-primary" id="ep-download-report"> |
| 142 |
<?php esc_html_e( 'Download report', 'elasticpress' ); ?> |
| 143 |
</a> |
| 144 |
<button class="button" data-clipboard-text="<?php echo esc_attr( implode( "\n\n", $copy_paste_output ) ); ?>" id="ep-copy-report" type="button"> |
| 145 |
<?php esc_html_e( 'Copy status report to clipboard', 'elasticpress' ); ?> |
| 146 |
</button> |
| 147 |
<span class="ep-copy-button-wrapper__success"> |
| 148 |
<?php esc_html_e( 'Copied!', 'elasticpress' ); ?> |
| 149 |
</span> |
| 150 |
</p> |
| 151 |
<?php |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Process and format the reports, then store them in the `formatted_reports` attribute. |
| 156 |
* |
| 157 |
* @since 4.5.0 |
| 158 |
* @return array |
| 159 |
*/ |
| 160 |
protected function get_formatted_reports() : array { |
| 161 |
if ( empty( $this->formatted_reports ) ) { |
| 162 |
$reports = $this->get_reports(); |
| 163 |
|
| 164 |
$this->formatted_reports = array_map( |
| 165 |
function( $report ) { |
| 166 |
return [ |
| 167 |
'actions' => $report->get_actions(), |
| 168 |
'groups' => $report->get_groups(), |
| 169 |
'messages' => $report->get_messages(), |
| 170 |
'title' => $report->get_title(), |
| 171 |
]; |
| 172 |
}, |
| 173 |
$reports |
| 174 |
); |
| 175 |
} |
| 176 |
return $this->formatted_reports; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Render the copy & paste report |
| 181 |
* |
| 182 |
* @param string $title Report title |
| 183 |
* @param array $groups Report groups |
| 184 |
* @return string |
| 185 |
*/ |
| 186 |
protected function render_copy_paste_report( string $title, array $groups ) : string { |
| 187 |
$output = "## {$title} ##\n\n"; |
| 188 |
|
| 189 |
foreach ( $groups as $group ) { |
| 190 |
$output .= "### {$group['title']} ###\n"; |
| 191 |
foreach ( $group['fields'] as $slug => $field ) { |
| 192 |
$value = $field['value'] ?? ''; |
| 193 |
|
| 194 |
$output .= "{$slug}: "; |
| 195 |
$output .= $this->render_value( $value ); |
| 196 |
$output .= "\n"; |
| 197 |
} |
| 198 |
$output .= "\n"; |
| 199 |
} |
| 200 |
|
| 201 |
return $output; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Render a value based on its type |
| 206 |
* |
| 207 |
* @param mixed $value The value |
| 208 |
* @return string |
| 209 |
*/ |
| 210 |
protected function render_value( $value ) { |
| 211 |
if ( is_array( $value ) || is_object( $value ) ) { |
| 212 |
return var_export( $value, true ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions |
| 213 |
} |
| 214 |
|
| 215 |
if ( is_bool( $value ) ) { |
| 216 |
return $value ? 'true' : 'false'; |
| 217 |
} |
| 218 |
|
| 219 |
return (string) $value; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Display a badge in the admin menu if there's admin notices from |
| 224 |
* ElasticPress.io. |
| 225 |
* |
| 226 |
* @return void |
| 227 |
*/ |
| 228 |
public function admin_menu_count() { |
| 229 |
global $menu, $submenu; |
| 230 |
|
| 231 |
$messages = \ElasticPress\ElasticPressIo::factory()->get_endpoint_messages(); |
| 232 |
|
| 233 |
if ( empty( $messages ) ) { |
| 234 |
return; |
| 235 |
} |
| 236 |
|
| 237 |
$count = count( $messages ); |
| 238 |
$title = sprintf( |
| 239 |
/* translators: %d: Number of messages. */ |
| 240 |
_n( '%s message from ElasticPress.io', '%s messages from ElasticPress.io', $count, 'elasticpress' ), |
| 241 |
$count |
| 242 |
); |
| 243 |
|
| 244 |
foreach ( $menu as $key => $value ) { |
| 245 |
if ( 'elasticpress' === $value[2] ) { |
| 246 |
$menu[ $key ][0] .= sprintf( |
| 247 |
' <span class="update-plugins"><span aria-hidden="true">%1$s</span><span class="screen-reader-text">%2$s</span></span>', |
| 248 |
esc_html( $count ), |
| 249 |
esc_attr( $title ) |
| 250 |
); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
if ( ! isset( $submenu['elasticpress'] ) ) { |
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
foreach ( $submenu['elasticpress'] as $key => $value ) { |
| 259 |
if ( 'elasticpress-status-report' === $value[2] ) { |
| 260 |
$submenu['elasticpress'][ $key ][0] .= sprintf( |
| 261 |
' <span class="menu-counter"><span aria-hidden="true">%1$s</span><span class="screen-reader-text">%2$s</span></span>', |
| 262 |
esc_html( $count ), |
| 263 |
esc_attr( $title ) |
| 264 |
); |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|