*/ private static $hooks = array(); /** * Register hooks. * * @return void */ public static function register() { add_action( 'admin_menu', array( __CLASS__, 'menu' ) ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'assets' ) ); add_filter( 'plugin_row_meta', array( __CLASS__, 'plugin_row_meta' ), 10, 2 ); add_filter( 'admin_footer_text', array( __CLASS__, 'admin_footer_text' ) ); } /** * Screens: slug suffix => [title, capability, callback]. * * @return array */ public static function screens() { $screens = array( '' => array( __( 'Dashboard', 'linkcortex' ), 'edit_posts', array( Dashboard::class, 'render' ) ), '-suggestions' => array( __( 'Suggestions', 'linkcortex' ), 'edit_posts', array( Suggestions_Page::class, 'render' ) ), '-keywords' => array( __( 'Keywords', 'linkcortex' ), 'edit_others_posts', array( Keywords_Page::class, 'render' ) ), '-settings' => array( __( 'Settings', 'linkcortex' ), 'manage_options', array( Settings_Page::class, 'render' ) ), ); foreach ( $screens as $suffix => $screen ) { if ( ! is_callable( $screen[2] ) ) { unset( $screens[ $suffix ] ); } } return $screens; } /** * Build the menu. * * @return void */ public static function menu() { $screens = self::screens(); $first = reset( $screens ); self::$hooks['dashboard'] = add_menu_page( __( 'LinkCortex', 'linkcortex' ), __( 'LinkCortex', 'linkcortex' ), 'edit_posts', self::SLUG, $first[2], 'dashicons-admin-links', 58 ); foreach ( $screens as $suffix => $screen ) { $hook = add_submenu_page( self::SLUG, $screen[0], $screen[0], $screen[1], self::SLUG . $suffix, $screen[2] ); if ( $hook && is_array( $screen[2] ) && method_exists( $screen[2][0], 'load' ) ) { add_action( 'load-' . $hook, array( $screen[2][0], 'load' ) ); } if ( $hook ) { self::$hooks[ '' === $suffix ? 'dashboard' : ltrim( $suffix, '-' ) ] = $hook; } } } /** * Whether the current screen belongs to this plugin. * * @param string $hook_suffix Hook suffix. * @return bool */ public static function is_ours( $hook_suffix ) { return in_array( $hook_suffix, self::$hooks, true ); } /** * URL of one of our screens. * * @param string $screen dashboard|suggestions|keywords|settings. * @param array $args Extra query args. * @return string */ public static function url( $screen = 'dashboard', array $args = array() ) { $page = 'dashboard' === $screen ? self::SLUG : self::SLUG . '-' . $screen; return add_query_arg( array_merge( array( 'page' => $page ), $args ), admin_url( 'admin.php' ) ); } /** * Enqueue shared admin CSS/JS on our screens only. * * @param string $hook_suffix Hook suffix. * @return void */ public static function assets( $hook_suffix ) { if ( ! self::is_ours( $hook_suffix ) ) { return; } wp_enqueue_style( 'ailsug-admin', AILSUG_URL . 'assets/css/admin.css', array(), AILSUG_VERSION ); wp_style_add_data( 'ailsug-admin', 'rtl', 'replace' ); wp_enqueue_script( 'ailsug-admin', AILSUG_URL . 'assets/js/admin.js', array( 'wp-i18n' ), AILSUG_VERSION, true ); wp_set_script_translations( 'ailsug-admin', 'linkcortex', AILSUG_DIR . 'languages' ); wp_enqueue_script( 'ailsug-cost', AILSUG_URL . 'assets/js/cost.js', array( 'ailsug-admin', 'wp-i18n' ), AILSUG_VERSION, true ); wp_set_script_translations( 'ailsug-cost', 'linkcortex', AILSUG_DIR . 'languages' ); wp_localize_script( 'ailsug-admin', 'ailsugAdmin', array( 'restUrl' => esc_url_raw( rest_url( 'linkcortex/v1/' ) ), 'nonce' => wp_create_nonce( 'wp_rest' ), 'screen' => array_search( $hook_suffix, self::$hooks, true ), 'currency2' => (string) \AILSUG\Settings::get( 'currency2' ), 'currency2Rate' => (float) \AILSUG\Settings::get( 'currency2_rate' ), ) ); } /** * Plugin row meta links. * * @param string[] $links Link array. * @param string $file Plugin basename. * @return string[] */ public static function plugin_row_meta( array $links, $file ) { return $links; } /** * Friendly footer credit on this plugin's admin screens. * * @param string $text Existing footer text. * @return string */ public static function admin_footer_text( $text ) { $screen = get_current_screen(); if ( $screen && in_array( $screen->id, self::$hooks, true ) ) { return sprintf( /* translators: %s: plugin name. */ __( 'Thank you for using %s.', 'linkcortex' ), '' . esc_html__( 'LinkCortex', 'linkcortex' ) . '' ); } return $text; } }