rules = $rules; $this->settings = $settings; add_action( 'admin_menu', array( $this, 'menu' ) ); add_action( 'admin_init', array( $this, 'redirect_parent' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'menu_css' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'assets' ) ); add_filter( 'plugin_action_links_' . plugin_basename( HIDEXO_LITE_FILE ), array( $this, 'action_links' ) ); } /** * Add the menu. "Hidexo Lite" opens with one sub-menu entry, "All Hider", * which shows the rules and options. */ public function menu() { add_menu_page( __( 'Hidexo Lite', 'hidexo-lite' ), __( 'Hidexo Lite', 'hidexo-lite' ), Hidexo_Lite_Plugin::capability(), 'hidexo-lite', '', 'dashicons-hidden', 58 ); $this->hook = add_submenu_page( 'hidexo-lite', __( 'All Hider', 'hidexo-lite' ), __( 'All Hider', 'hidexo-lite' ), Hidexo_Lite_Plugin::capability(), self::PAGE, array( $this, 'render' ) ); } /** * WordPress adds the parent menu title ("Hidexo Lite") as the first sub-menu entry. * Hide that duplicate so only "All Hider" is listed under Hidexo Lite. */ public function menu_css() { wp_add_inline_style( 'admin-menu', '#adminmenu #toplevel_page_hidexo-lite .wp-submenu li.wp-first-item{display:none}' ); } /** * Old links to the parent page go to All Hider. */ public function redirect_parent() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only redirect, no state change. if ( isset( $_GET['page'] ) && 'hidexo-lite' === sanitize_key( wp_unslash( $_GET['page'] ) ) && current_user_can( Hidexo_Lite_Plugin::capability() ) ) { wp_safe_redirect( admin_url( 'admin.php?page=' . self::PAGE ) ); exit; } } /** * Plugin list links. * * @param array $links Links. * @return array */ public function action_links( $links ) { array_unshift( $links, '' . esc_html__( 'All Hider', 'hidexo-lite' ) . '' ); return $links; } /** * Pages offered in the picker's page selector. * * @return array */ private function pages() { $out = array( array( 'label' => __( 'Homepage', 'hidexo-lite' ), 'url' => home_url( '/' ), ), ); $ids = get_posts( array( 'post_type' => array( 'page', 'post' ), 'post_status' => 'publish', 'posts_per_page' => 30, 'orderby' => 'modified', 'order' => 'DESC', 'fields' => 'ids', 'no_found_rows' => true, 'ignore_sticky_posts' => true, ) ); foreach ( $ids as $pid ) { $title = get_the_title( $pid ); $out[] = array( 'label' => ( '' !== $title ? $title : __( '(no title)', 'hidexo-lite' ) ), 'url' => get_permalink( $pid ), ); } return $out; } /** * Enqueue dashboard assets. * * @param string $hook Current hook. */ public function assets( $hook ) { if ( $hook !== $this->hook ) { return; } wp_enqueue_style( 'hidexo-lite-admin', HIDEXO_LITE_URL . 'admin/css/admin.css', array(), HIDEXO_LITE_VERSION ); wp_enqueue_script( 'hidexo-lite-admin', HIDEXO_LITE_URL . 'admin/js/admin.js', array( 'wp-i18n' ), HIDEXO_LITE_VERSION, true ); wp_set_script_translations( 'hidexo-lite-admin', 'hidexo-lite' ); $snapshot = array( 'rules' => array_map( array( $this->rules, 'public_view' ), $this->rules->all() ), 'active' => $this->rules->count_active(), 'hidden' => $this->rules->hidden_element_count(), ); wp_add_inline_script( 'hidexo-lite-admin', 'window.HidexoAdmin = ' . wp_json_encode( array( 'restUrl' => esc_url_raw( rest_url( Hidexo_Lite_REST_API::NS . '/' ) ), 'nonce' => wp_create_nonce( 'wp_rest' ), 'pickerNonce' => wp_create_nonce( 'hidexo_picker' ), 'checkNonce' => wp_create_nonce( 'hidexo_check' ), 'homeUrl' => home_url( '/' ), 'homeOrigin' => Hidexo_Lite_Picker::origin( home_url( '/' ) ), 'pages' => $this->pages(), 'snapshot' => $snapshot, 'settings' => $this->settings->get(), ) ) . ';', 'before' ); } /** * Render the dashboard. */ public function render() { if ( ! current_user_can( Hidexo_Lite_Plugin::capability() ) ) { return; } include HIDEXO_LITE_PATH . 'admin/views/dashboard.php'; } }