Skip to content

Commit

Permalink
Merge pull request #3162 from 10up/feature/new-epio-page
Browse files Browse the repository at this point in the history
Create a new ElasticPress.io page with client messages
  • Loading branch information
felipeelia authored Feb 28, 2023
2 parents e3d4c63 + 5bc8b21 commit 316bece
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 19 deletions.
4 changes: 4 additions & 0 deletions assets/css/status-report.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
margin-bottom: 1rem;
}

& .components-notice {
margin: 0;
}

& table {
table-layout: fixed;

Expand Down
11 changes: 9 additions & 2 deletions assets/js/status-report/components/reports.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ import Report from './reports/report';
* @returns {WPElement} Reports component.
*/
export default ({ reports }) => {
return Object.entries(reports).map(([key, { actions, groups, title }]) => (
<Report actions={actions} groups={groups} id={key} key={key} title={title} />
return Object.entries(reports).map(([key, { actions, groups, messages, title }]) => (
<Report
actions={actions}
groups={groups}
id={key}
key={key}
messages={messages}
title={title}
/>
));
};
13 changes: 10 additions & 3 deletions assets/js/status-report/components/reports/report.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/**
* WordPress dependencies.
*/
import { Button, Panel, PanelBody, PanelHeader } from '@wordpress/components';
import { WPElement } from '@wordpress/element';
import { Button, Notice, Panel, PanelBody, PanelHeader } from '@wordpress/components';
import { safeHTML } from '@wordpress/dom';
import { RawHTML, WPElement } from '@wordpress/element';
import { decodeEntities } from '@wordpress/html-entities';

/**
Expand All @@ -17,10 +18,11 @@ import Value from './report/value';
* @param {Array} props.actions Report actions.
* @param {object} props.groups Report groups.
* @param {string} props.id Report ID.
* @param {string} props.messages Report messages.
* @param {string} props.title Report title.
* @returns {WPElement} Report component.
*/
export default ({ actions, groups, id, title }) => {
export default ({ actions, groups, id, messages, title }) => {
if (groups.length < 1) {
return null;
}
Expand All @@ -41,6 +43,11 @@ export default ({ actions, groups, id, title }) => {
</Button>
))}
</PanelHeader>
{messages.map(({ message, type }) => (
<Notice status={type} isDismissible={false}>
<RawHTML>{safeHTML(message)}</RawHTML>
</Notice>
))}
{groups.map(({ fields, title }) => (
<PanelBody key={title} title={decodeEntities(title)} initialOpen={false}>
<table
Expand Down
56 changes: 56 additions & 0 deletions includes/classes/ElasticPressIo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Class for interacting with ElasticPress.io
*
* @since 4.5.0
* @package elasticpress
*/

namespace ElasticPress;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* ElasticPressIo class
*
* @package ElasticPress
*/
class ElasticPressIo {
/**
* Return singleton instance of class
*
* @return object
*/
public static function factory() {
static $instance = false;

if ( ! $instance ) {
$instance = new self();
}

return $instance;
}

/**
* Get messages from ElasticPress.io.
*
* @return array ElasticPress.io messages.
*/
public function get_endpoint_messages() {
$transient = 'ep_elasticpress_io_messages';
$messages = get_transient( $transient );

if ( false !== $messages ) {
return $messages;
}

$response = \ElasticPress\Elasticsearch::factory()->remote_request( 'endpoint-messages' );
$messages = (array) json_decode( wp_remote_retrieve_body( $response ), true );

set_transient( $transient, $messages, HOUR_IN_SECONDS );

return $messages;
}
}
66 changes: 55 additions & 11 deletions includes/classes/Screen/StatusReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class StatusReport {
*/
public function setup() {
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
add_action( 'admin_head', array( $this, 'admin_menu_count' ), 11 );
}

/**
Expand Down Expand Up @@ -77,24 +78,23 @@ public function admin_enqueue_scripts() {
public function get_reports() : array {
$reports = [];

$reports['wordpress'] = new \ElasticPress\StatusReport\WordPress();
$reports['indexable'] = new \ElasticPress\StatusReport\IndexableContent();
$reports['elasticpress'] = new \ElasticPress\StatusReport\ElasticPress();

/* this filter is documented in elasticpress.php */
$query_logger = apply_filters( 'ep_query_logger', new \ElasticPress\QueryLogger() );

if ( $query_logger ) {
$reports['failed-queries'] = new \ElasticPress\StatusReport\FailedQueries( $query_logger );
}

$reports['indices'] = new \ElasticPress\StatusReport\Indices();

if ( Utils\is_epio() ) {
$reports['autosuggest'] = new \ElasticPress\StatusReport\ElasticPressIo();
}

$reports['last-sync'] = new \ElasticPress\StatusReport\LastSync();
$reports['features'] = new \ElasticPress\StatusReport\Features();
$reports['wordpress'] = new \ElasticPress\StatusReport\WordPress();
$reports['indexable'] = new \ElasticPress\StatusReport\IndexableContent();
$reports['elasticpress'] = new \ElasticPress\StatusReport\ElasticPress();
$reports['indices'] = new \ElasticPress\StatusReport\Indices();
$reports['last-sync'] = new \ElasticPress\StatusReport\LastSync();
$reports['features'] = new \ElasticPress\StatusReport\Features();

/**
* Filter the reports executed in the Status Report page.
Expand Down Expand Up @@ -164,9 +164,10 @@ protected function get_formatted_reports() : array {
$this->formatted_reports = array_map(
function( $report ) {
return [
'actions' => $report->get_actions(),
'groups' => $report->get_groups(),
'title' => $report->get_title(),
'actions' => $report->get_actions(),
'groups' => $report->get_groups(),
'messages' => $report->get_messages(),
'title' => $report->get_title(),
];
},
$reports
Expand Down Expand Up @@ -217,4 +218,47 @@ protected function render_value( $value ) {

return (string) $value;
}

/**
* Display a badge in the admin menu if there's admin notices from
* ElasticPress.io.
*
* @return void
*/
public function admin_menu_count() {
global $menu, $submenu;

$messages = \ElasticPress\ElasticPressIo::factory()->get_endpoint_messages();

if ( empty( $messages ) ) {
return;
}

$count = count( $messages );
$title = sprintf(
/* translators: %d: Number of messages. */
_n( '%s message from ElasticPress.io', '%s messages from ElasticPress.io', $count, 'elasticpress' ),
$count
);

foreach ( $menu as $key => $value ) {
if ( 'elasticpress' === $value[2] ) {
$menu[ $key ][0] .= sprintf(
' <span class="update-plugins"><span aria-hidden="true">%1$s</span><span class="screen-reader-text">%2$s</span></span>',
esc_html( $count ),
esc_attr( $title )
);
}
}

foreach ( $submenu['elasticpress'] as $key => $value ) {
if ( 'elasticpress-status-report' === $value[2] ) {
$submenu['elasticpress'][ $key ][0] .= sprintf(
' <span class="menu-counter"><span aria-hidden="true">%1$s</span><span class="screen-reader-text">%2$s</span></span>',
esc_html( $count ),
esc_attr( $title )
);
}
}
}
}
13 changes: 13 additions & 0 deletions includes/classes/StatusReport/ElasticPressIo.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,19 @@ protected function get_instant_results_field() : array {
];
}

/**
* Return the report messages.
*
* @return array
* @since 4.5.0
*/
public function get_messages() : array {
$messages = \ElasticPress\ElasticPressIo::factory()->get_endpoint_messages();
$messages = array_values( $messages );

return $messages;
}

/**
* Process the ElasticPress.io Orders Search template.
*
Expand Down
10 changes: 10 additions & 0 deletions includes/classes/StatusReport/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,14 @@ abstract public function get_groups() : array;
public function get_actions() : array {
return [];
}

/**
* Return the report messages.
*
* @return array
* @since 4.5.0
*/
public function get_messages() : array {
return [];
}
}
6 changes: 3 additions & 3 deletions tests/php/TestStatusReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testGetReports() {

$reports = $status_report->get_reports();
$this->assertSame(
[ 'wordpress', 'indexable', 'elasticpress', 'failed-queries', 'indices', 'last-sync', 'features' ],
[ 'failed-queries', 'wordpress', 'indexable', 'elasticpress', 'indices', 'last-sync', 'features' ],
array_keys( $reports )
);
}
Expand All @@ -46,7 +46,7 @@ public function testGetReportsFilter() {

$reports = $status_report->get_reports();
$this->assertSame(
[ 'wordpress', 'indexable', 'elasticpress', 'failed-queries', 'indices', 'last-sync', 'features', 'custom' ],
[ 'failed-queries', 'wordpress', 'indexable', 'elasticpress', 'indices', 'last-sync', 'features', 'custom' ],
array_keys( $reports )
);
}
Expand All @@ -63,7 +63,7 @@ public function testGetReportsSkipped() {

$reports = $status_report->get_reports();
$this->assertSame(
[ 'elasticpress', 'failed-queries', 'indices', 'last-sync', 'features' ],
[ 'failed-queries', 'elasticpress', 'indices', 'last-sync', 'features' ],
array_keys( $reports )
);
}
Expand Down

0 comments on commit 316bece

Please sign in to comment.