mapper = $mapper; } /** * Initializes admin hooks. */ public function init() { add_action( 'admin_menu', array( $this, 'register_admin_menu' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); // AJAX endpoints. add_action( 'wp_ajax_crowbridge_add_mapping', array( $this, 'ajax_add_mapping' ) ); add_action( 'wp_ajax_crowbridge_reindex', array( $this, 'ajax_reindex' ) ); add_action( 'wp_ajax_crowbridge_reindex_all', array( $this, 'ajax_reindex_all' ) ); add_action( 'wp_ajax_crowbridge_unlink', array( $this, 'ajax_unlink' ) ); // Plugin list action & meta links. add_filter( 'plugin_action_links_' . plugin_basename( CROWBRIDGE_PLUGIN_FILE ), array( $this, 'filter_action_links' ) ); add_filter( 'plugin_row_meta', array( $this, 'filter_row_meta' ), 10, 2 ); } /** * Adds quick settings link on Plugins screen. * * @param array $links * @return array */ public function filter_action_links( $links ) { $dashboard_link = sprintf( '%s', esc_url( admin_url( 'tools.php?page=crowbridge' ) ), esc_html__( 'Dashboard', 'crowbridge' ) ); array_unshift( $links, $dashboard_link ); return $links; } /** * Adds CyberCrow links on Plugins screen. * * @param array $links * @param string $file * @return array */ public function filter_row_meta( $links, $file ) { if ( plugin_basename( CROWBRIDGE_PLUGIN_FILE ) === $file ) { $links[] = sprintf( '%s', esc_url( 'https://cybercrow.com' ), esc_html__( 'CyberCrow Inc.', 'crowbridge' ) ); $links[] = sprintf( '%s', esc_url( 'https://cybercrow.com/donate' ), esc_html__( 'Support & Donate ♥', 'crowbridge' ) ); } return $links; } /** * Registers Tools > CrowBridge admin menu. */ public function register_admin_menu() { add_management_page( __( 'CrowBridge — Static Content Search', 'crowbridge' ), __( 'CrowBridge', 'crowbridge' ), 'edit_pages', 'crowbridge', array( $this, 'render_dashboard' ) ); } /** * Enqueues stylesheet and JavaScript for the CrowBridge screen. * * @param string $hook */ public function enqueue_assets( $hook ) { if ( 'tools_page_crowbridge' !== $hook ) { return; } wp_enqueue_style( 'crowbridge-admin-css', CROWBRIDGE_PLUGIN_URL . 'assets/css/admin.css', array(), CROWBRIDGE_VERSION ); wp_enqueue_script( 'crowbridge-admin-js', CROWBRIDGE_PLUGIN_URL . 'assets/js/admin.js', array( 'jquery' ), CROWBRIDGE_VERSION, true ); wp_localize_script( 'crowbridge-admin-js', 'crowbridgeData', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'crowbridge_admin_nonce' ), 'strings' => array( 'confirmUnlink' => __( 'Are you sure you want to remove this mapping? The page content will remain, but automatic redirects and indexing will stop.', 'crowbridge' ), 'confirmReindexAll'=> __( 'Re-index all static sources now? This will refresh content for all mapped pages.', 'crowbridge' ), 'reindexing' => __( 'Re-indexing...', 'crowbridge' ), 'reindexingAll' => __( 'Re-indexing all pages...', 'crowbridge' ), 'adding' => __( 'Adding and indexing...', 'crowbridge' ), 'unlinking' => __( 'Removing link...', 'crowbridge' ), 'success' => __( 'Operation completed successfully.', 'crowbridge' ), ), ) ); } /** * Renders the dashboard HTML. */ public function render_dashboard() { if ( ! current_user_can( 'edit_pages' ) ) { wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'crowbridge' ) ); } $mapped_pages = $this->mapper->get_mapped_pages(); $unmapped_pages = $this->mapper->get_unmapped_pages(); ?>

Add New) to hold the searchable index.', 'crowbridge' ); ?>

Add New.', 'crowbridge' ); ?>

()

| (ID: • slug: /)
__( 'Permission denied.', 'crowbridge' ) ) ); } $source = isset( $_POST['source'] ) ? sanitize_text_field( wp_unslash( $_POST['source'] ) ) : ''; $page_id = isset( $_POST['page_id'] ) ? absint( $_POST['page_id'] ) : 0; if ( empty( $source ) || empty( $page_id ) ) { wp_send_json_error( array( 'message' => __( 'Please provide both a source path/URL and a placeholder WordPress Page.', 'crowbridge' ) ) ); } $result = $this->mapper->map_page( $page_id, $source ); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ) ); } wp_send_json_success( array( 'message' => __( 'Static content successfully indexed and mapped to WordPress Page!', 'crowbridge' ) ) ); } /** * AJAX: Re-indexes a single page. */ public function ajax_reindex() { check_ajax_referer( 'crowbridge_admin_nonce', 'nonce' ); if ( ! current_user_can( 'edit_pages' ) ) { wp_send_json_error( array( 'message' => __( 'Permission denied.', 'crowbridge' ) ) ); } $page_id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; if ( empty( $page_id ) ) { wp_send_json_error( array( 'message' => __( 'Invalid page ID.', 'crowbridge' ) ) ); } $result = $this->mapper->reindex_page( $page_id ); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ) ); } wp_send_json_success( array( 'message' => __( 'Content successfully re-indexed.', 'crowbridge' ) ) ); } /** * AJAX: Bulk re-indexes all mapped pages. */ public function ajax_reindex_all() { check_ajax_referer( 'crowbridge_admin_nonce', 'nonce' ); if ( ! current_user_can( 'edit_pages' ) ) { wp_send_json_error( array( 'message' => __( 'Permission denied.', 'crowbridge' ) ) ); } $summary = $this->mapper->reindex_all(); $msg = sprintf( /* translators: 1: success count, 2: failed count */ __( 'Re-index completed: %1$d succeeded, %2$d failed.', 'crowbridge' ), $summary['success'], $summary['failed'] ); if ( ! empty( $summary['errors'] ) ) { $msg .= ' ' . implode( ' ', $summary['errors'] ); wp_send_json_error( array( 'message' => $msg ) ); } wp_send_json_success( array( 'message' => $msg ) ); } /** * AJAX: Unlinks a page from CrowBridge. */ public function ajax_unlink() { check_ajax_referer( 'crowbridge_admin_nonce', 'nonce' ); if ( ! current_user_can( 'edit_pages' ) ) { wp_send_json_error( array( 'message' => __( 'Permission denied.', 'crowbridge' ) ) ); } $page_id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; if ( empty( $page_id ) ) { wp_send_json_error( array( 'message' => __( 'Invalid page ID.', 'crowbridge' ) ) ); } $this->mapper->unlink_page( $page_id ); wp_send_json_success( array( 'message' => __( 'Mapping removed successfully.', 'crowbridge' ) ) ); } }