Files
WP-Site-Sync/includes/class-admin.php
2025-12-14 21:57:25 -06:00

613 lines
26 KiB
PHP

<?php
namespace SiteSync;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handles the WordPress admin interface and actions for the Site Sync plugin.
*/
class Admin {
/**
* Settings instance for managing plugin options.
*
* @var Settings
*/
private $settings;
/**
* State instance for managing sync state.
*
* @var State
*/
private $state;
/**
* Sync_Engine instance for handling sync operations.
*
* @var Sync_Engine
*/
private $engine;
/**
* Admin constructor.
*
* @param Settings $settings Settings instance for managing plugin options.
* @param State $state State instance for managing sync state.
* @param Sync_Engine $engine Sync_Engine instance for handling sync operations.
*/
public function __construct( Settings $settings, State $state, Sync_Engine $engine ) {
$this->settings = $settings;
$this->state = $state;
$this->engine = $engine;
}
/**
* Registers all WordPress admin hooks for the Site Sync plugin.
*
* @return void
*/
public function hooks(): void {
add_action( 'admin_menu', array( $this, 'register_menu' ) );
add_action( 'admin_post_site_sync_save_settings', array( $this, 'handle_save_settings' ) );
add_action( 'admin_post_site_sync_manual', array( $this, 'handle_manual_sync' ) );
add_action( 'admin_post_site_sync_manual_push', array( $this, 'handle_manual_push' ) );
add_action( 'admin_post_site_sync_manual_pull', array( $this, 'handle_manual_pull' ) );
add_action( 'admin_post_site_sync_handshake', array( $this, 'handle_handshake' ) );
add_action( 'admin_post_site_sync_reset_state', array( $this, 'handle_reset_state' ) );
add_action( 'admin_post_site_sync_clear_logs', array( $this, 'handle_clear_logs' ) );
add_action( 'admin_notices', array( $this, 'maybe_render_notices' ) );
}
/**
* Registers the Site Sync menu page in the WordPress admin.
*
* @return void
*/
public function register_menu(): void {
add_menu_page(
__( 'Site Sync', 'site-sync' ),
__( 'Site Sync', 'site-sync' ),
'manage_options',
'site-sync',
array( $this, 'render_settings_page' ),
'dashicons-update',
65
);
}
/**
* Renders the Site Sync settings page in the WordPress admin.
*
* @return void
*/
public function render_settings_page(): void {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have permission to access this page.', 'site-sync' ) );
}
$settings = $this->settings->ensure_defaults();
$schedules = $this->get_schedule_choices();
$state = $this->state->get();
?>
<div class="wrap">
<h1><?php esc_html_e( 'Site Sync', 'site-sync' ); ?></h1>
<p><?php esc_html_e( 'Configure the connection to your peer site and schedule syncs.', 'site-sync' ); ?></p>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
<?php wp_nonce_field( 'site_sync_save_settings', 'site_sync_nonce' ); ?>
<input type="hidden" name="action" value="site_sync_save_settings">
<table class="form-table" role="presentation">
<tr>
<th scope="row">
<label for="site_uuid"><?php esc_html_e( 'Site ID', 'site-sync' ); ?></label>
</th>
<td>
<input type="text" id="site_uuid" name="site_uuid" value="<?php echo esc_attr( $settings['site_uuid'] ); ?>" class="regular-text" readonly>
<p class="description"><?php esc_html_e( 'Share this ID with the peer so it can identify this site.', 'site-sync' ); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label for="shared_key"><?php esc_html_e( 'Shared Secret', 'site-sync' ); ?></label>
</th>
<td>
<input type="text" id="shared_key" name="shared_key" value="<?php echo esc_attr( $settings['shared_key'] ); ?>" class="regular-text" autocomplete="off">
<p class="description"><?php esc_html_e( 'Used to sign requests to and from the peer.', 'site-sync' ); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label for="peer_site_key"><?php esc_html_e( 'Peer Site ID', 'site-sync' ); ?></label>
</th>
<td>
<input type="text" id="peer_site_key" name="peer_site_key" value="<?php echo esc_attr( $settings['peer_site_key'] ); ?>" class="regular-text">
<p class="description"><?php esc_html_e( 'Site ID provided by your peer instance.', 'site-sync' ); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label for="peer_url"><?php esc_html_e( 'Peer Site URL', 'site-sync' ); ?></label>
</th>
<td>
<input type="url" id="peer_url" name="peer_url" value="<?php echo esc_attr( $settings['peer_url'] ); ?>" class="regular-text" placeholder="https://peer-site.test">
<p class="description"><?php esc_html_e( 'Base URL to reach the peer (will call its REST endpoints).', 'site-sync' ); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label for="post_meta_keys"><?php esc_html_e( 'Post Meta Keys', 'site-sync' ); ?></label>
</th>
<td>
<input type="text" id="post_meta_keys" name="post_meta_keys" value="<?php echo esc_attr( implode( ', ', $settings['post_meta_keys'] ?? array() ) ); ?>" class="regular-text" placeholder="_thumbnail_id, custom_key">
<p class="description"><?php esc_html_e( 'Comma or space separated meta keys to sync (defaults to _thumbnail_id). Also filterable via site_sync/post_meta_keys.', 'site-sync' ); ?></p>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e( 'Sync Schedule', 'site-sync' ); ?></th>
<td>
<select name="sync_interval" id="sync_interval">
<?php foreach ( $schedules as $key => $label ) : ?>
<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $settings['sync_interval'], $key ); ?>>
<?php echo esc_html( $label ); ?>
</option>
<?php endforeach; ?>
</select>
<p class="description"><?php esc_html_e( 'How often WP-Cron should attempt a sync.', 'site-sync' ); ?></p>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e( 'Enable Sync', 'site-sync' ); ?></th>
<td>
<label>
<input type="checkbox" name="enabled" value="1" <?php checked( $settings['enabled'] ); ?>>
<?php esc_html_e( 'Allow scheduled sync jobs to run.', 'site-sync' ); ?>
</label>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e( 'Directions', 'site-sync' ); ?></th>
<td>
<fieldset>
<label>
<input type="checkbox" name="enable_push" value="1" <?php checked( $settings['enable_push'] ); ?>>
<?php esc_html_e( 'Include outbound push (send this site\'s changes).', 'site-sync' ); ?>
</label>
<br>
<label>
<input type="checkbox" name="enable_pull" value="1" <?php checked( $settings['enable_pull'] ); ?>>
<?php esc_html_e( 'Include inbound pull (receive peer changes).', 'site-sync' ); ?>
</label>
<p class="description"><?php esc_html_e( 'Uncheck to disable that direction during scheduled and combined manual syncs.', 'site-sync' ); ?></p>
</fieldset>
</td>
</tr>
</table>
<?php submit_button( __( 'Save Settings', 'site-sync' ) ); ?>
</form>
<hr>
<h2><?php esc_html_e( 'Manual Sync', 'site-sync' ); ?></h2>
<p><?php esc_html_e( 'Trigger an immediate sync cycle using the current settings.', 'site-sync' ); ?></p>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
<?php wp_nonce_field( 'site_sync_manual', 'site_sync_manual_nonce' ); ?>
<input type="hidden" name="action" value="site_sync_manual">
<?php submit_button( __( 'Sync Now', 'site-sync' ), 'secondary' ); ?>
</form>
<p><?php esc_html_e( 'Or run one direction at a time:', 'site-sync' ); ?></p>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="margin-right:12px; display:inline-block;">
<?php wp_nonce_field( 'site_sync_manual_push', 'site_sync_manual_push_nonce' ); ?>
<input type="hidden" name="action" value="site_sync_manual_push">
<?php submit_button( __( 'Push Only', 'site-sync' ), 'secondary', 'submit', false ); ?>
</form>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="display:inline-block;">
<?php wp_nonce_field( 'site_sync_manual_pull', 'site_sync_manual_pull_nonce' ); ?>
<input type="hidden" name="action" value="site_sync_manual_pull">
<?php submit_button( __( 'Pull Only', 'site-sync' ), 'secondary', 'submit', false ); ?>
</form>
<h2><?php esc_html_e( 'Maintenance', 'site-sync' ); ?></h2>
<p><?php esc_html_e( 'Reset checkpoints to force a full resend on the next sync. Use if the destination was emptied or fell out of sync.', 'site-sync' ); ?></p>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
<?php wp_nonce_field( 'site_sync_reset_state', 'site_sync_reset_state_nonce' ); ?>
<input type="hidden" name="action" value="site_sync_reset_state">
<?php submit_button( __( 'Reset Sync State', 'site-sync' ), 'delete' ); ?>
</form>
<p><?php esc_html_e( 'Clear the stored recent logs shown below.', 'site-sync' ); ?></p>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
<?php wp_nonce_field( 'site_sync_clear_logs', 'site_sync_clear_logs_nonce' ); ?>
<input type="hidden" name="action" value="site_sync_clear_logs">
<?php submit_button( __( 'Clear Logs', 'site-sync' ), 'secondary' ); ?>
</form>
<h2><?php esc_html_e( 'Status', 'site-sync' ); ?></h2>
<table class="widefat striped">
<tbody>
<tr>
<th><?php esc_html_e( 'Last Run', 'site-sync' ); ?></th>
<td><?php echo esc_html( $state['last_run'] ?? __( 'Never', 'site-sync' ) ); ?></td>
</tr>
<tr>
<th><?php esc_html_e( 'Last Error', 'site-sync' ); ?></th>
<td><?php echo esc_html( $state['last_error'] ?? __( 'None', 'site-sync' ) ); ?></td>
</tr>
<tr>
<th><?php esc_html_e( 'Last Sent', 'site-sync' ); ?></th>
<td>
<?php
$sent = $state['last_counts']['sent'] ?? array();
printf(
/* translators: 1: posts count, 2: terms count, 3: media count, 4: tombstones count */
esc_html__( 'Posts: %1$d, Terms: %2$d, Media: %3$d, Deletes: %4$d', 'site-sync' ),
(int) ( $sent['posts'] ?? 0 ),
(int) ( $sent['terms'] ?? 0 ),
(int) ( $sent['media'] ?? 0 ),
(int) ( $sent['tombstones'] ?? 0 )
);
?>
</td>
</tr>
<tr>
<th><?php esc_html_e( 'Last Applied (Inbound)', 'site-sync' ); ?></th>
<td>
<?php
$recv = $state['last_counts']['received'] ?? array();
printf(
/* translators: 1: applied count, 2: skipped count, 3: errors count */
esc_html__( 'Applied: %1$d, Skipped: %2$d, Errors: %3$d', 'site-sync' ),
(int) ( $recv['applied'] ?? 0 ),
(int) ( $recv['skipped'] ?? 0 ),
(int) ( $recv['errors'] ?? 0 )
);
?>
</td>
</tr>
</tbody>
</table>
<h2><?php esc_html_e( 'Connectivity Test', 'site-sync' ); ?></h2>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
<?php wp_nonce_field( 'site_sync_handshake', 'site_sync_handshake_nonce' ); ?>
<input type="hidden" name="action" value="site_sync_handshake">
<?php submit_button( __( 'Run Handshake', 'site-sync' ), 'secondary' ); ?>
</form>
<h2><?php esc_html_e( 'Recent Logs', 'site-sync' ); ?></h2>
<table class="widefat striped">
<thead>
<tr>
<th><?php esc_html_e( 'Time', 'site-sync' ); ?></th>
<th><?php esc_html_e( 'Level', 'site-sync' ); ?></th>
<th><?php esc_html_e( 'Message', 'site-sync' ); ?></th>
<th><?php esc_html_e( 'Context', 'site-sync' ); ?></th>
</tr>
</thead>
<tbody>
<?php
$logs = \SiteSync\Logger::recent( 20 );
if ( empty( $logs ) ) :
?>
<tr><td colspan="4"><?php esc_html_e( 'No logs yet.', 'site-sync' ); ?></td></tr>
<?php
else :
foreach ( $logs as $log ) :
?>
<tr>
<td><?php echo esc_html( $log['ts'] ); ?></td>
<td><?php echo esc_html( $log['level'] ); ?></td>
<td><?php echo esc_html( $log['message'] ); ?></td>
<td><code><?php echo esc_html( $log['context'] ? wp_json_encode( $log['context'] ) : '' ); ?></code></td>
</tr>
<?php
endforeach;
endif;
?>
</tbody>
</table>
</div>
<?php
}
/**
* Handles saving the Site Sync plugin settings from the admin form submission.
*
* @return void
*/
public function handle_save_settings(): void {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have permission to do this.', 'site-sync' ) );
}
check_admin_referer( 'site_sync_save_settings', 'site_sync_nonce' );
// phpcs:disable
$data = array(
'site_uuid' => isset( $_POST['site_uuid'] ) ? wp_unslash( $_POST['site_uuid'] ) : '',
'shared_key' => isset( $_POST['shared_key'] ) ? wp_unslash( $_POST['shared_key'] ) : '',
'peer_site_key' => isset( $_POST['peer_site_key'] ) ? wp_unslash( $_POST['peer_site_key'] ) : '',
'peer_url' => isset( $_POST['peer_url'] ) ? wp_unslash( $_POST['peer_url'] ) : '',
'post_meta_keys' => isset( $_POST['post_meta_keys'] ) ? wp_unslash( $_POST['post_meta_keys'] ) : '',
'sync_interval' => isset( $_POST['sync_interval'] ) ? wp_unslash( $_POST['sync_interval'] ) : '',
'enabled' => isset( $_POST['enabled'] ),
'enable_push' => isset( $_POST['enable_push'] ),
'enable_pull' => isset( $_POST['enable_pull'] ),
);
// phpcs:enable
$this->settings->update( $data );
$settings = $this->settings->get_settings();
Cron::configure( $settings['enabled'], $settings['sync_interval'] );
$redirect = add_query_arg(
array(
'page' => 'site-sync',
'site_sync_status' => 'saved',
),
admin_url( 'admin.php' )
);
wp_safe_redirect( $redirect );
exit;
}
/**
* Handles the manual sync action from the admin form submission.
*
* @return void
*/
public function handle_manual_sync(): void {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have permission to do this.', 'site-sync' ) );
}
check_admin_referer( 'site_sync_manual', 'site_sync_manual_nonce' );
$settings = $this->settings->get_settings();
do_action( 'site_sync/manual_trigger', $settings );
$redirect = add_query_arg(
array(
'page' => 'site-sync',
'site_sync_status' => 'manual_run',
),
admin_url( 'admin.php' )
);
wp_safe_redirect( $redirect );
exit;
}
/**
* Handles the manual push-only action from the admin form submission.
*
* @return void
*/
public function handle_manual_push(): void {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have permission to do this.', 'site-sync' ) );
}
check_admin_referer( 'site_sync_manual_push', 'site_sync_manual_push_nonce' );
$settings = $this->settings->get_settings();
do_action( 'site_sync/manual_push', $settings );
$redirect = add_query_arg(
array(
'page' => 'site-sync',
'site_sync_status' => 'manual_push_run',
),
admin_url( 'admin.php' )
);
wp_safe_redirect( $redirect );
exit;
}
/**
* Handles the manual pull-only action from the admin form submission.
*
* @return void
*/
public function handle_manual_pull(): void {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have permission to do this.', 'site-sync' ) );
}
check_admin_referer( 'site_sync_manual_pull', 'site_sync_manual_pull_nonce' );
$settings = $this->settings->get_settings();
do_action( 'site_sync/manual_pull', $settings );
$redirect = add_query_arg(
array(
'page' => 'site-sync',
'site_sync_status' => 'manual_pull_run',
),
admin_url( 'admin.php' )
);
wp_safe_redirect( $redirect );
exit;
}
/**
* Renders admin notices for Site Sync actions based on query parameters.
*
* @return void
*/
public function maybe_render_notices(): void {
if ( empty( $_GET['page'] ) || 'site-sync' !== $_GET['page'] ) {
return;
}
if ( isset( $_GET['site_sync_status'] ) && 'saved' === $_GET['site_sync_status'] ) {
printf(
'<div class="notice notice-success is-dismissible"><p>%s</p></div>',
esc_html__( 'Site Sync settings saved.', 'site-sync' )
);
}
if ( isset( $_GET['site_sync_status'] ) && 'manual_run' === $_GET['site_sync_status'] ) {
printf(
'<div class="notice notice-info is-dismissible"><p>%s</p></div>',
esc_html__( 'Manual sync triggered.', 'site-sync' )
);
}
if ( isset( $_GET['site_sync_status'] ) && 'manual_push_run' === $_GET['site_sync_status'] ) {
printf(
'<div class="notice notice-info is-dismissible"><p>%s</p></div>',
esc_html__( 'Manual push triggered.', 'site-sync' )
);
}
if ( isset( $_GET['site_sync_status'] ) && 'manual_pull_run' === $_GET['site_sync_status'] ) {
printf(
'<div class="notice notice-info is-dismissible"><p>%s</p></div>',
esc_html__( 'Manual pull triggered.', 'site-sync' )
);
}
if ( isset( $_GET['site_sync_status'] ) && 'handshake_ok' === $_GET['site_sync_status'] ) {
printf(
'<div class="notice notice-success is-dismissible"><p>%s</p></div>',
esc_html__( 'Handshake succeeded.', 'site-sync' )
);
}
if ( isset( $_GET['site_sync_status'] ) && 'handshake_fail' === $_GET['site_sync_status'] ) {
$msg = isset( $_GET['site_sync_msg'] ) ? sanitize_text_field( wp_unslash( $_GET['site_sync_msg'] ) ) : __( 'Handshake failed.', 'site-sync' );
printf(
'<div class="notice notice-error is-dismissible"><p>%s</p></div>',
esc_html( $msg )
);
}
if ( isset( $_GET['site_sync_status'] ) && 'reset_ok' === $_GET['site_sync_status'] ) {
printf(
'<div class="notice notice-success is-dismissible"><p>%s</p></div>',
esc_html__( 'Sync state reset. Next run will resend all items.', 'site-sync' )
);
}
if ( isset( $_GET['site_sync_status'] ) && 'logs_cleared' === $_GET['site_sync_status'] ) {
printf(
'<div class="notice notice-success is-dismissible"><p>%s</p></div>',
esc_html__( 'Recent logs cleared.', 'site-sync' )
);
}
}
/**
* Handles the handshake action from the admin form submission.
*
* @return void
*/
public function handle_handshake(): void {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have permission to do this.', 'site-sync' ) );
}
check_admin_referer( 'site_sync_handshake', 'site_sync_handshake_nonce' );
$result = $this->engine->test_handshake();
if ( is_wp_error( $result ) ) {
$redirect = add_query_arg(
array(
'page' => 'site-sync',
'site_sync_status' => 'handshake_fail',
'site_sync_msg' => rawurlencode( $result->get_error_message() ),
),
admin_url( 'admin.php' )
);
} else {
$redirect = add_query_arg(
array(
'page' => 'site-sync',
'site_sync_status' => 'handshake_ok',
),
admin_url( 'admin.php' )
);
}
wp_safe_redirect( $redirect );
exit;
}
/**
* Handles clearing the recent logs from the admin form submission.
*
* @return void
*/
public function handle_clear_logs(): void {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have permission to do this.', 'site-sync' ) );
}
check_admin_referer( 'site_sync_clear_logs', 'site_sync_clear_logs_nonce' );
Logger::clear();
$redirect = add_query_arg(
array(
'page' => 'site-sync',
'site_sync_status' => 'logs_cleared',
),
admin_url( 'admin.php' )
);
wp_safe_redirect( $redirect );
exit;
}
/**
* Handles the reset state action from the admin form submission.
*
* @return void
*/
public function handle_reset_state(): void {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have permission to do this.', 'site-sync' ) );
}
check_admin_referer( 'site_sync_reset_state', 'site_sync_reset_state_nonce' );
$this->state->reset();
$redirect = add_query_arg(
array(
'page' => 'site-sync',
'site_sync_status' => 'reset_ok',
),
admin_url( 'admin.php' )
);
wp_safe_redirect( $redirect );
exit;
}
/**
* Returns available schedule choices for the sync interval.
*
* @return array
*/
private function get_schedule_choices(): array {
$choices = array(
'site_sync_5min' => __( 'Every 5 minutes', 'site-sync' ),
);
// Add core schedules to give admins flexibility.
$choices['hourly'] = __( 'Hourly', 'site-sync' );
$choices['twicedaily'] = __( 'Twice Daily', 'site-sync' );
$choices['daily'] = __( 'Daily', 'site-sync' );
return $choices;
}
}