init_hooks(); } /** * Initialize hooks. */ private function init_hooks() { add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); add_action( 'in_admin_header', array( $this, 'render_header' ) ); add_filter( 'admin_body_class', array( $this, 'body_class' ) ); } /** * Slugs of the plugin's own admin pages. * * @return string[] */ public static function page_slugs() { return array( 'afridirect-payment', 'afridirect-payment-methods', 'afridirect-payment-requests', 'afridirect-standalone-payments', 'afridirect-woo-payments', 'afridirect-settings', ); } /** * Admin URL for one of the plugin pages. * * @param string $slug Page slug. * @return string */ public static function page_url( $slug ) { return admin_url( 'admin.php?page=' . $slug ); } /** * The "page" query argument, sanitised. Read-only; used to recognise our screens. * * @return string */ private function current_page() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only screen detection, nothing is processed. return isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; } /** * Post type of the current admin screen, if any. * * @return string */ private function current_post_type() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; return ( $screen && ! empty( $screen->post_type ) ) ? $screen->post_type : ''; } /** * Whether the current screen belongs to this plugin. * * @return bool */ public function is_plugin_screen() { if ( in_array( $this->current_page(), self::page_slugs(), true ) ) { return true; } return in_array( $this->current_post_type(), array( 'afridirect_form', 'afridirect_payment' ), true ); } /** * Enqueue the design system on our screens only. */ public function enqueue_assets() { if ( ! $this->is_plugin_screen() ) { return; } wp_enqueue_style( 'afridirect-admin-ui', AFRIDIRECT_PAYMENT_PLUGIN_URL . 'admin/css/ui.css', array(), afridirect_payment_asset_ver( 'admin/css/ui.css' ) ); wp_enqueue_script( 'afridirect-admin-ui', AFRIDIRECT_PAYMENT_PLUGIN_URL . 'admin/js/ui.js', array(), afridirect_payment_asset_ver( 'admin/js/ui.js' ), true ); wp_localize_script( 'afridirect-admin-ui', 'afridirectAdminUi', array( 'copied' => __( 'Copied!', 'afridirect-payment' ), ) ); } /** * Add a scoping class to the admin body on our screens. * * @param string $classes Existing body classes. * @return string */ public function body_class( $classes ) { if ( $this->is_plugin_screen() ) { $classes .= ' afd-admin'; } return $classes; } /** * Navigation items for the header. * * @return array[] */ private function nav_items() { $page = $this->current_page(); $post_type = $this->current_post_type(); $items = array( array( 'label' => __( 'Dashboard', 'afridirect-payment' ), 'url' => self::page_url( 'afridirect-payment' ), 'active' => 'afridirect-payment' === $page, ), array( 'label' => __( 'Methods', 'afridirect-payment' ), 'url' => self::page_url( 'afridirect-payment-methods' ), 'active' => 'afridirect-payment-methods' === $page, ), array( 'label' => __( 'Forms', 'afridirect-payment' ), 'url' => admin_url( 'edit.php?post_type=afridirect_form' ), 'active' => 'afridirect_form' === $post_type, ), array( 'label' => __( 'Requests', 'afridirect-payment' ), 'url' => self::page_url( 'afridirect-payment-requests' ), 'active' => 'afridirect-payment-requests' === $page, ), array( 'label' => __( 'Payments', 'afridirect-payment' ), 'url' => self::page_url( 'afridirect-standalone-payments' ), 'active' => 'afridirect-standalone-payments' === $page || 'afridirect_payment' === $post_type, ), ); if ( class_exists( 'WooCommerce' ) ) { $items[] = array( 'label' => __( 'WooCommerce', 'afridirect-payment' ), 'url' => self::page_url( 'afridirect-woo-payments' ), 'active' => 'afridirect-woo-payments' === $page, ); } $items[] = array( 'label' => __( 'Settings', 'afridirect-payment' ), 'url' => self::page_url( 'afridirect-settings' ), 'active' => 'afridirect-settings' === $page, ); return $items; } /** * Render the branded header with navigation. */ public function render_header() { if ( ! $this->is_plugin_screen() ) { return; } ?>
'