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_handshake', array( $this, 'handle_handshake' ) ); add_action( 'admin_post_site_sync_reset_state', array( $this, 'handle_reset_state' ) ); 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(); ?>


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'] ), ); // 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; } /** * 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( '

%s

', esc_html__( 'Site Sync settings saved.', 'site-sync' ) ); } if ( isset( $_GET['site_sync_status'] ) && 'manual_run' === $_GET['site_sync_status'] ) { printf( '

%s

', esc_html__( 'Manual sync triggered.', 'site-sync' ) ); } if ( isset( $_GET['site_sync_status'] ) && 'handshake_ok' === $_GET['site_sync_status'] ) { printf( '

%s

', 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( '

%s

', esc_html( $msg ) ); } if ( isset( $_GET['site_sync_status'] ) && 'reset_ok' === $_GET['site_sync_status'] ) { printf( '

%s

', esc_html__( 'Sync state reset. Next run will resend all items.', '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 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; } }