| 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 |
* Initialize class |
| 23 |
*/ |
| 24 |
public function setup() { |
| 25 |
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Enqueue script. |
| 30 |
* |
| 31 |
* @return void |
| 32 |
*/ |
| 33 |
public function admin_enqueue_scripts() { |
| 34 |
if ( 'status-report' !== \ElasticPress\Screen::factory()->get_current_screen() ) { |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
$script_deps = Utils\get_asset_info( 'status-report-script', 'dependencies' ); |
| 39 |
|
| 40 |
wp_enqueue_script( |
| 41 |
'ep_admin_status_report_scripts', |
| 42 |
EP_URL . 'dist/js/status-report-script.js', |
| 43 |
array_merge( $script_deps, [ 'clipboard' ] ), |
| 44 |
Utils\get_asset_info( 'status-report-script', 'version' ), |
| 45 |
true |
| 46 |
); |
| 47 |
|
| 48 |
$reports = $this->get_reports(); |
| 49 |
$reports = array_map( |
| 50 |
function( $report ) { |
| 51 |
return [ |
| 52 |
'actions' => $report->get_actions(), |
| 53 |
'groups' => $report->get_groups(), |
| 54 |
'title' => $report->get_title(), |
| 55 |
]; |
| 56 |
}, |
| 57 |
$reports |
| 58 |
); |
| 59 |
|
| 60 |
wp_localize_script( |
| 61 |
'ep_admin_status_report_scripts', |
| 62 |
'epStatusReport', |
| 63 |
$reports |
| 64 |
); |
| 65 |
|
| 66 |
$style_deps = Utils\get_asset_info( 'status-report-styles', 'dependencies' ); |
| 67 |
|
| 68 |
wp_enqueue_style( |
| 69 |
'ep_status_report_styles', |
| 70 |
EP_URL . 'dist/css/status-report-styles.css', |
| 71 |
array_merge( $style_deps, [ 'wp-edit-post' ] ), |
| 72 |
Utils\get_asset_info( 'status-report-styles', 'version' ) |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Return all reports available |
| 78 |
* |
| 79 |
* @return array |
| 80 |
*/ |
| 81 |
public function get_reports() : array { |
| 82 |
$reports = []; |
| 83 |
|
| 84 |
$reports['wordpress'] = new \ElasticPress\StatusReport\WordPress(); |
| 85 |
$reports['indexable'] = new \ElasticPress\StatusReport\IndexableContent(); |
| 86 |
$reports['elasticpress'] = new \ElasticPress\StatusReport\ElasticPress(); |
| 87 |
|
| 88 |
/* this filter is documented in elasticpress.php */ |
| 89 |
$query_logger = apply_filters( 'ep_query_logger', new \ElasticPress\QueryLogger() ); |
| 90 |
if ( $query_logger ) { |
| 91 |
$reports['failed-queries'] = new \ElasticPress\StatusReport\FailedQueries( $query_logger ); |
| 92 |
} |
| 93 |
|
| 94 |
$reports['indices'] = new \ElasticPress\StatusReport\Indices(); |
| 95 |
|
| 96 |
if ( Utils\is_epio() ) { |
| 97 |
$reports['autosuggest'] = new \ElasticPress\StatusReport\ElasticPressIo(); |
| 98 |
} |
| 99 |
|
| 100 |
$reports['last-sync'] = new \ElasticPress\StatusReport\LastSync(); |
| 101 |
$reports['features'] = new \ElasticPress\StatusReport\Features(); |
| 102 |
|
| 103 |
/** |
| 104 |
* Filter the reports executed in the Status Report page. |
| 105 |
* |
| 106 |
* @since 4.4.0 |
| 107 |
* @hook ep_status_report_reports |
| 108 |
* @param {array<Report>} $reports Array of reports |
| 109 |
* @return {array<Report>} New array of reports |
| 110 |
*/ |
| 111 |
$filtered_reports = apply_filters( 'ep_status_report_reports', $reports ); |
| 112 |
|
| 113 |
$skipped_reports = ! empty( $_GET['ep-skip-reports'] ) ? (array) $_GET['ep-skip-reports'] : []; // phpcs:ignore WordPress.Security.NonceVerification |
| 114 |
$skipped_reports = array_map( 'sanitize_text_field', $skipped_reports ); |
| 115 |
|
| 116 |
$filtered_reports = array_filter( |
| 117 |
$filtered_reports, |
| 118 |
function( $report_slug ) use ( $skipped_reports ) { |
| 119 |
return ! in_array( $report_slug, $skipped_reports, true ); |
| 120 |
}, |
| 121 |
ARRAY_FILTER_USE_KEY |
| 122 |
); |
| 123 |
|
| 124 |
return $filtered_reports; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Render all reports (HTML and Copy & Paste button) |
| 129 |
*/ |
| 130 |
public function render_reports() { |
| 131 |
$reports = $this->get_reports(); |
| 132 |
|
| 133 |
$copy_paste_output = []; |
| 134 |
|
| 135 |
foreach ( $reports as $report ) { |
| 136 |
$title = $report->get_title(); |
| 137 |
$groups = $report->get_groups(); |
| 138 |
|
| 139 |
$copy_paste_output[] = $this->render_copy_paste_report( $title, $groups ); |
| 140 |
} |
| 141 |
|
| 142 |
?> |
| 143 |
<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> |
| 144 |
<p class="ep-copy-button-wrapper"> |
| 145 |
<button class="button" data-clipboard-text="<?php echo esc_attr( implode( "\n\n", $copy_paste_output ) ); ?>" id="ep-copy-report" type="button"> |
| 146 |
<?php esc_html_e( 'Copy status report to clipboard', 'elasticpress' ); ?> |
| 147 |
</button> |
| 148 |
<span class="ep-copy-button-wrapper__success"> |
| 149 |
<?php esc_html_e( 'Copied!', 'elasticpress' ); ?> |
| 150 |
</span> |
| 151 |
</p> |
| 152 |
<?php |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Render the copy & paste report |
| 157 |
* |
| 158 |
* @param string $title Report title |
| 159 |
* @param array $groups Report groups |
| 160 |
* @return string |
| 161 |
*/ |
| 162 |
protected function render_copy_paste_report( string $title, array $groups ) : string { |
| 163 |
$output = "## {$title} ##\n\n"; |
| 164 |
|
| 165 |
foreach ( $groups as $group ) { |
| 166 |
$output .= "### {$group['title']} ###\n"; |
| 167 |
foreach ( $group['fields'] as $slug => $field ) { |
| 168 |
$value = $field['value'] ?? ''; |
| 169 |
|
| 170 |
$output .= "{$slug}: "; |
| 171 |
$output .= $this->render_value( $value ); |
| 172 |
$output .= "\n"; |
| 173 |
} |
| 174 |
$output .= "\n"; |
| 175 |
} |
| 176 |
|
| 177 |
return $output; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Render a value based on its type |
| 182 |
* |
| 183 |
* @param mixed $value The value |
| 184 |
* @return string |
| 185 |
*/ |
| 186 |
protected function render_value( $value ) { |
| 187 |
if ( is_array( $value ) || is_object( $value ) ) { |
| 188 |
return var_export( $value, true ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions |
| 189 |
} |
| 190 |
|
| 191 |
if ( is_bool( $value ) ) { |
| 192 |
return $value ? 'true' : 'false'; |
| 193 |
} |
| 194 |
|
| 195 |
return (string) $value; |
| 196 |
} |
| 197 |
} |
| 198 |
|