Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display sync trigger on sync page. #3722

Merged
merged 5 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions assets/js/sync-ui/apps/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import { useSyncSettings } from '../provider';
*/
export default () => {
const { createNotice } = useSettingsScreen();
const { isComplete, isEpio, isSyncing, logMessage, startSync, syncHistory } = useSync();
const { isComplete, isEpio, isSyncing, logMessage, startSync, syncHistory, syncTrigger } =
useSync();
const { autoIndex } = useSyncSettings();

/**
Expand All @@ -46,13 +47,13 @@ export default () => {
*/
const onInit = () => {
if (autoIndex) {
startSync({ put_mapping: true });
startSync({ put_mapping: true, trigger: syncTrigger });
logMessage(__('Starting delete and sync…', 'elasticpress'), 'info');
}
};

useEffect(onComplete, [createNotice, isComplete]);
useEffect(onInit, [autoIndex, logMessage, startSync]);
useEffect(onInit, [autoIndex, logMessage, startSync, syncTrigger]);

return (
<>
Expand Down
2 changes: 1 addition & 1 deletion assets/js/sync-ui/components/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default () => {
const { put_mapping } = args;

const putMapping = syncHistory.length ? put_mapping : true;
const syncArgs = { ...args, put_mapping: putMapping };
const syncArgs = { ...args, put_mapping: putMapping, trigger: 'manual' };

startSync(syncArgs);
logMessage(__('Starting sync…', 'elasticpress'), 'info');
Expand Down
21 changes: 18 additions & 3 deletions assets/js/sync-ui/components/previous-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,16 @@ export default ({ failures, method, stateDatetime, status, trigger }) => {
}

switch (trigger) {
case 'features':
return __('Automatic sync after settings change.', 'elasticpress');
case 'install':
return __('Automatic sync after installation.', 'elasticpress');
case 'manual':
default: {
return __('Manual sync from Sync Settings.', 'elasticpress');
}
case 'upgrade':
return __('Automatic sync after plugin update.', 'elasticpress');
default:
return null;
}
}, [method, trigger]);

Expand All @@ -101,7 +107,16 @@ export default ({ failures, method, stateDatetime, status, trigger }) => {
>
<Icon icon={isError ? error : success} />
<div className="ep-previous-sync__title">
{when} &mdash; {why}
{why
? sprintf(
/* translators: %1$s Sync date and time. %2%s sync trigger. */ __(
'%1$s — %2$s',
'elasticpress',
),
when,
why,
)
: when}
</div>
<div className="ep-previous-sync__help">{how}</div>
</div>
Expand Down
69 changes: 57 additions & 12 deletions assets/js/sync-ui/components/progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import classnames from 'classnames';
* WordPress dependencies.
*/
import { Icon } from '@wordpress/components';
import { useMemo, WPElement } from '@wordpress/element';
import { createInterpolateElement, useMemo, WPElement } from '@wordpress/element';
import { dateI18n } from '@wordpress/date';
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import { update } from '@wordpress/icons';

/**
Expand All @@ -23,8 +23,16 @@ import { useSync } from '../../sync';
* @returns {WPElement} Component.
*/
export default () => {
const { isCli, isComplete, isFailed, isPaused, itemsProcessed, itemsTotal, syncStartDateTime } =
useSync();
const {
isCli,
isComplete,
isFailed,
isPaused,
itemsProcessed,
itemsTotal,
syncStartDateTime,
syncTrigger,
} = useSync();

/**
* Sync progress label.
Expand Down Expand Up @@ -59,6 +67,43 @@ export default () => {

const now = itemsTotal ? Math.floor((itemsProcessed / itemsTotal) * 100) : 100;

const why = useMemo(() => {
if (isCli) {
/* translators: %1$s Sync start date and time. */
return __('Started manually from WP CLI at <time>%s</time>.', 'elasticpress');
}

switch (syncTrigger) {
case 'features':
/* translators: %1$s Sync start date and time. */
return __(
'Started automatically after a change to feature settings at <time>%s</time>.',
'elasticpress',
);
case 'install':
/* translators: %1$s Sync start date and time. */
return __(
'Started automatically after installing the ElasticPress plugin at <time>%s</time>.',
'elasticpress',
);
case 'manual':
/* translators: %1$s Sync start date and time. */
return __(
'Started manually from the Sync page at <time>%s</time>.',
'elasticpress',
);
case 'upgrade':
/* translators: %1$s Sync start date and time. */
return __(
'Started automatically after updating the ElasticPress plugin at <time>%s</time>.',
'elasticpress',
);
default:
/* translators: %1$s Sync start date and time. */
return __('Started on <time>%s</time>.', 'elasticpress');
}
}, [isCli, syncTrigger]);

return (
<div
className={classnames('ep-sync-progress', {
Expand All @@ -68,14 +113,14 @@ export default () => {
<Icon icon={update} />
<div className="ep-sync-progress__details">
<strong>{label}</strong>
{syncStartDateTime ? (
<>
{__('Started on', 'elasticpress')}{' '}
<time dateTime={dateI18n('c', syncStartDateTime)}>
{dateI18n('D, F d, Y H:i', syncStartDateTime)}
</time>
</>
) : null}
{syncStartDateTime
? createInterpolateElement(
sprintf(why, dateI18n('g:ia l F jS, Y', syncStartDateTime)),
{
time: <time dateTime={dateI18n('c', syncStartDateTime)} />,
},
)
: null}
</div>
<div className="ep-sync-progress__progress-bar">
<div
Expand Down
25 changes: 22 additions & 3 deletions assets/js/sync-ui/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
/**
* Window dependencies.
*/
const { autoIndex, apiUrl, indexMeta, indexables, isEpio, nonce, postTypes, syncHistory } =
window.epDash;
const {
autoIndex,
apiUrl,
indexMeta,
indexables,
isEpio,
nonce,
postTypes,
syncHistory,
syncTrigger,
} = window.epDash;

export { autoIndex, apiUrl, indexables, indexMeta, isEpio, nonce, postTypes, syncHistory };
export {
autoIndex,
apiUrl,
indexables,
indexMeta,
isEpio,
nonce,
postTypes,
syncHistory,
syncTrigger,
};
2 changes: 2 additions & 0 deletions assets/js/sync-ui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
nonce,
postTypes,
syncHistory,
syncTrigger,
} from './config';
import { SyncSettingsProvider } from './provider';
import Sync from './apps/sync';
Expand All @@ -36,6 +37,7 @@ const App = () => (
<SyncProvider
apiUrl={apiUrl}
defaultSyncHistory={syncHistory}
defaultSyncTrigger={syncTrigger}
indexMeta={indexMeta}
isEpio={isEpio}
nonce={nonce}
Expand Down
13 changes: 12 additions & 1 deletion assets/js/sync/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const Context = createContext();
* @param {string} props.apiUrl API endpoint URL.
* @param {Function} props.children Component children
* @param {Array} props.defaultSyncHistory Sync history.
* @param {Array} props.defaultSyncTrigger Sync trigger.
* @param {object|null} props.indexMeta Details of a sync in progress.
* @param {boolean} props.isEpio Whether ElasticPress.io is in use.
* @param {string} props.nonce WordPress nonce.
Expand All @@ -49,6 +50,7 @@ export const SyncProvider = ({
apiUrl,
children,
defaultSyncHistory,
defaultSyncTrigger,
indexMeta,
isEpio,
nonce,
Expand Down Expand Up @@ -77,6 +79,7 @@ export const SyncProvider = ({
itemsTotal: 100,
syncStartDateTime: null,
syncHistory: defaultSyncHistory,
syncTrigger: defaultSyncTrigger,
});

/**
Expand Down Expand Up @@ -249,6 +252,7 @@ export const SyncProvider = ({
itemsProcessed: getItemsProcessedFromIndexMeta(indexMeta),
itemsTotal: getItemsTotalFromIndexMeta(indexMeta),
syncStartDateTime: indexMeta.start_date_time,
syncTrigger: indexMeta.trigger || null,
});
},
[],
Expand Down Expand Up @@ -454,7 +458,12 @@ export const SyncProvider = ({
isSyncing: true,
});

updateState({ itemsProcessed: 0, syncStartDateTime: Date.now() });
updateState({
itemsProcessed: 0,
syncStartDateTime: Date.now(),
syncTrigger: args.trigger || null,
});

doIndex(args);
},
[doIndex],
Expand Down Expand Up @@ -523,6 +532,7 @@ export const SyncProvider = ({
itemsTotal,
syncHistory,
syncStartDateTime,
syncTrigger,
} = stateRef.current;

// eslint-disable-next-line react/jsx-no-constructed-context-values
Expand All @@ -544,6 +554,7 @@ export const SyncProvider = ({
startSync,
stopSync,
syncStartDateTime,
syncTrigger,
};

return <Context.Provider value={contextValue}>{children}</Context.Provider>;
Expand Down
8 changes: 4 additions & 4 deletions includes/classes/AdminNotices.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ protected function process_auto_activate_sync_notice() {
}

if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
$url = admin_url( 'network/admin.php?page=elasticpress-sync&do_sync' );
$url = admin_url( 'network/admin.php?page=elasticpress-sync&do_sync=features' );
} else {
$url = admin_url( 'admin.php?page=elasticpress-sync&do_sync' );
$url = admin_url( 'admin.php?page=elasticpress-sync&do_sync=features' );
}

$feature = Features::factory()->get_registered_feature( $auto_activate_sync );
Expand Down Expand Up @@ -257,9 +257,9 @@ protected function process_upgrade_sync_notice() {
}

if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
$url = admin_url( 'network/admin.php?page=elasticpress-sync&do_sync' );
$url = admin_url( 'network/admin.php?page=elasticpress-sync&do_sync=upgrade' );
} else {
$url = admin_url( 'admin.php?page=elasticpress-sync&do_sync' );
$url = admin_url( 'admin.php?page=elasticpress-sync&do_sync=upgrade' );
}

if ( defined( 'EP_DASHBOARD_SYNC' ) && ! EP_DASHBOARD_SYNC ) {
Expand Down
2 changes: 1 addition & 1 deletion includes/classes/IndexHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ protected function build_index_meta() {
'start_date_time' => $start_date_time ? $start_date_time->format( DATE_ATOM ) : false,
'starting_indices' => $starting_indices,
'messages_queue' => [],
'trigger' => 'manual',
'trigger' => ! empty( $this->args['trigger'] ) ? sanitize_text_field( $this->args['trigger'] ) : null,
'totals' => [
'total' => 0,
'synced' => 0,
Expand Down
4 changes: 4 additions & 0 deletions includes/classes/REST/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ public function get_args() {
'type' => 'boolean',
'required' => false,
],
'trigger' => [
'enum' => [ 'features', 'install', 'manual', 'upgrade' ],
'required' => false,
],
'upper_limit_object_id' => [
'description' => __( 'End of object ID range to sync.', 'elasticpress' ),
'type' => 'integer',
Expand Down
1 change: 1 addition & 0 deletions includes/classes/Screen/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function admin_enqueue_scripts() {
'nonce' => wp_create_nonce( 'wp_rest' ),
'postTypes' => array_map( fn( $post_type ) => [ $post_type, get_post_type_object( $post_type )->labels->name ], $post_types ),
'syncHistory' => $sync_history,
'syncTrigger' => ! empty( $_GET['do_sync'] ) ? sanitize_text_field( wp_unslash( $_GET['do_sync'] ) ) : null, // phpcs:ignore WordPress.Security.NonceVerification.Recommended
];

wp_localize_script( 'ep_sync_scripts', 'epDash', $data );
Expand Down
4 changes: 2 additions & 2 deletions includes/partials/install-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
$setup_url = admin_url( 'network/admin.php?page=elasticpress-settings' );
$sync_url = admin_url( 'network/admin.php?page=elasticpress-sync&do_sync' );
$sync_url = admin_url( 'network/admin.php?page=elasticpress-sync&do_sync=install' );
$dashboard_url = admin_url( 'network/admin.php?page=elasticpress' );
} else {
$setup_url = admin_url( 'admin.php?page=elasticpress-settings' );
$sync_url = admin_url( 'admin.php?page=elasticpress-sync&do_sync' );
$sync_url = admin_url( 'admin.php?page=elasticpress-sync&do_sync=install' );
$dashboard_url = admin_url( 'admin.php?page=elasticpress' );
}

Expand Down
10 changes: 8 additions & 2 deletions tests/cypress/integration/features/woocommerce.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,18 @@ describe('WooCommerce Feature', { tags: '@slow' }, () => {
*/
cy.contains('button', 'WooCommerce').click();

cy.contains('label', 'Show suggestions')
.closest('.components-base-control')
.find('input')
.as('showSuggestionsCheck');

if (!isEpIo) {
cy.get('.components-radio-control__input').first().should('be.disabled');
cy.get('@showSuggestionsCheck').should('be.disabled');
return;
}

cy.get('.components-radio-control__input').first().click();
cy.get('@showSuggestionsCheck').check();

cy.contains('button', 'Save and sync now').click();

cy.wait('@apiRequest');
Expand Down