hook = add_menu_page( __( 'Static Export', 'aouwal-static-site-export' ), __( 'Static Export', 'aouwal-static-site-export' ), 'manage_options', 'aouwal-static-site-export', array( $this, 'render' ), 'dashicons-download', 81 ); } /** * Register settings. * * @return void */ public function register_settings() { register_setting( 'aouwal_settings_group', AOUWAL_Settings::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( 'AOUWAL_Settings', 'sanitize' ), 'default' => AOUWAL_Settings::defaults(), ) ); } /** * Enqueue admin assets on the plugin page. * * @param string $hook_suffix Current admin page. * @return void */ public function assets( $hook_suffix ) { if ( $hook_suffix !== $this->hook ) { return; } wp_enqueue_style( 'aouwal-admin', AOUWAL_PLUGIN_URL . 'admin/assets/admin.css', array(), AOUWAL_VERSION ); wp_enqueue_script( 'aouwal-admin', AOUWAL_PLUGIN_URL . 'admin/assets/export.js', array( 'jquery' ), AOUWAL_VERSION, true ); wp_localize_script( 'aouwal-admin', 'AOUWAL', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'aouwal_ajax' ), 'batch' => (int) AOUWAL_Settings::get( 'batch_size', 5 ), 'i18n' => array( 'starting' => __( 'Discovering URLs…', 'aouwal-static-site-export' ), 'crawling' => __( 'Processing', 'aouwal-static-site-export' ), 'finalizing' => __( 'Packaging export…', 'aouwal-static-site-export' ), 'done' => __( 'Export complete.', 'aouwal-static-site-export' ), 'error' => __( 'An error occurred.', 'aouwal-static-site-export' ), 'confirm' => __( 'Start a new static export?', 'aouwal-static-site-export' ), 'download' => __( 'Download export ZIP', 'aouwal-static-site-export' ), /* translators: %s: number of discovered URLs. */ 'discovered' => __( 'Discovered %s URLs.', 'aouwal-static-site-export' ), ), ) ); } /** * Render the admin page. * * @return void */ public function render() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', 'aouwal-static-site-export' ) ); } $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'settings'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $tabs = array( 'settings' => __( 'Settings', 'aouwal-static-site-export' ), 'export' => __( 'Export', 'aouwal-static-site-export' ), 'deploy' => __( 'Deploy', 'aouwal-static-site-export' ), ); if ( ! isset( $tabs[ $tab ] ) ) { $tab = 'settings'; } echo '
'; echo '
'; echo ''; echo ''; echo '' . esc_html__( 'Aouwal Static Site Export', 'aouwal-static-site-export' ) . ''; printf( '%1$s %2$s', esc_html__( 'by', 'aouwal-static-site-export' ), esc_html__( 'Abdul Aouwal', 'aouwal-static-site-export' ) ); echo ''; echo 'abdulaouwal.com'; echo '
'; if ( isset( $_GET['settings-updated'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended echo '

' . esc_html__( 'Settings saved.', 'aouwal-static-site-export' ) . '

'; } echo ''; switch ( $tab ) { case 'export': require AOUWAL_PLUGIN_DIR . 'admin/views/export.php'; break; case 'deploy': require AOUWAL_PLUGIN_DIR . 'admin/views/deploy.php'; break; default: require AOUWAL_PLUGIN_DIR . 'admin/views/settings.php'; break; } echo '
'; } /** * AJAX: start export. * * @return void */ public function ajax_start() { check_ajax_referer( 'aouwal_ajax', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'Permission denied.', 'aouwal-static-site-export' ) ), 403 ); } $exporter = new AOUWAL_Exporter(); $result = $exporter->start(); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ) ); } wp_send_json_success( $result ); } /** * AJAX: process a batch. * * @return void */ public function ajax_batch() { check_ajax_referer( 'aouwal_ajax', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'Permission denied.', 'aouwal-static-site-export' ) ), 403 ); } $job_id = isset( $_POST['job_id'] ) ? sanitize_text_field( wp_unslash( $_POST['job_id'] ) ) : ''; $batch = (int) AOUWAL_Settings::get( 'batch_size', 5 ); $exporter = new AOUWAL_Exporter(); $result = $exporter->process_batch( $job_id, max( 1, $batch ) ); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ) ); } wp_send_json_success( $result ); } /** * AJAX: finalize export. * * @return void */ public function ajax_finalize() { check_ajax_referer( 'aouwal_ajax', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'Permission denied.', 'aouwal-static-site-export' ) ), 403 ); } $job_id = isset( $_POST['job_id'] ) ? sanitize_text_field( wp_unslash( $_POST['job_id'] ) ) : ''; $exporter = new AOUWAL_Exporter(); $result = $exporter->finalize( $job_id ); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ) ); } wp_send_json_success( $result ); } } new AOUWAL_Admin();