*/ public static function get_pages() { $pages = array( 'dashboard' => array( 'slug' => self::MENU_SLUG, 'title' => __( 'Dashboard', 'camelplug-video-player' ), 'menu' => __( 'Dashboard', 'camelplug-video-player' ), // Tab label in the dashboard navigation. 'nav' => __( 'Welcome', 'camelplug-video-player' ), ), // Demo, Feature Comparison, Pricing and Documentation are reached from the dashboard navigation only. 'demo' => array( 'slug' => 'camelplug-video-player-demo', 'title' => __( 'Demo', 'camelplug-video-player' ), 'menu' => __( 'Demo', 'camelplug-video-player' ), 'sidebar' => false, ), 'compare' => array( 'slug' => 'camelplug-video-player-compare', 'title' => __( 'Feature Comparison', 'camelplug-video-player' ), 'menu' => __( 'Feature Comparison', 'camelplug-video-player' ), 'sidebar' => false, ), 'pricing' => array( 'slug' => 'camelplug-video-player-pricing', 'title' => __( 'Pricing', 'camelplug-video-player' ), 'menu' => __( 'Pricing', 'camelplug-video-player' ), 'sidebar' => false, ), 'help' => array( 'slug' => 'camelplug-video-player-help', 'title' => __( 'Documentation', 'camelplug-video-player' ), 'menu' => __( 'Documentation', 'camelplug-video-player' ), 'sidebar' => false, ), ); // Pro users have nothing to buy: no Pricing page (and so no "Buy Now" button). if ( cvp_is_premium() ) { unset( $pages['pricing'] ); } return $pages; } /** * Adds the top-level menu and its sub pages. */ public function register_menu() { $this->hooks[] = add_menu_page( __( 'Video Player', 'camelplug-video-player' ), __( 'Video Player', 'camelplug-video-player' ), self::CAPABILITY, self::MENU_SLUG, array( $this, 'render_page' ), 'dashicons-format-video', 58 ); foreach ( self::get_pages() as $page ) { $in_sidebar = ! isset( $page['sidebar'] ) || $page['sidebar']; /* translators: %s: page name, e.g. "Demo". */ $page_title = sprintf( __( '%s ‹ Video Player', 'camelplug-video-player' ), $page['title'] ); // An empty parent registers the page without a sidebar entry. $hook = add_submenu_page( $in_sidebar ? self::MENU_SLUG : '', $page_title, $page['menu'], self::CAPABILITY, $page['slug'], array( $this, 'render_page' ) ); $this->hooks[] = $hook; if ( ! $in_sidebar && $hook ) { // WordPress files pages with an empty parent under `$submenu['']`, where // get_admin_page_parent() would find them and reset the parent we set // below. The page stays registered (and accessible) without that entry. global $submenu; if ( isset( $submenu[''] ) ) { $submenu[''] = array_filter( // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited $submenu[''], function ( $item ) use ( $page ) { return $page['slug'] !== $item[2]; } ); } add_action( 'load-' . $hook, function () use ( $page_title ) { // Hidden pages have no menu entry to take their title from. $GLOBALS['title'] = $page_title; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited // Keep the Video Player menu open and Dashboard highlighted. add_filter( 'parent_file', array( $this, 'menu_parent' ) ); add_filter( 'submenu_file', array( $this, 'menu_parent' ) ); } ); } } // The WordPress list of all saved shortcodes. add_submenu_page( self::MENU_SLUG, __( 'Shortcodes', 'camelplug-video-player' ), __( 'Shortcodes', 'camelplug-video-player' ), self::CAPABILITY, 'edit.php?post_type=' . CVP_Shortcode::POST_TYPE ); // Opens the editor of a new shortcode, pre-filled with the Video Player block. add_submenu_page( self::MENU_SLUG, __( 'Add New Shortcode', 'camelplug-video-player' ), __( 'Add New Shortcode', 'camelplug-video-player' ), self::CAPABILITY, 'post-new.php?post_type=' . CVP_Shortcode::POST_TYPE ); } /** * Whether the current screen edits a shortcode post. * * @return bool */ private function is_shortcode_editor() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; return $screen && CVP_Shortcode::POST_TYPE === $screen->post_type; } /** * Keeps the Video Player menu open in the shortcode list and editor. * * @param string $parent_file Parent menu file. * @return string Parent menu file. */ public function shortcode_editor_parent( $parent_file ) { return $this->is_shortcode_editor() ? self::MENU_SLUG : $parent_file; } /** * Highlights "Add New Shortcode" while creating a shortcode, and * "Shortcodes" on the list and while editing an existing one. * * @param string|null $submenu_file Submenu file. * @return string|null Submenu file. */ public function shortcode_editor_submenu( $submenu_file ) { if ( ! $this->is_shortcode_editor() ) { return $submenu_file; } global $pagenow; $type = CVP_Shortcode::POST_TYPE; return 'post-new.php' === $pagenow ? 'post-new.php?post_type=' . $type : 'edit.php?post_type=' . $type; } /** * Menu entry to highlight while a sidebar-less page is open. * * @return string Dashboard menu slug. */ public function menu_parent() { return self::MENU_SLUG; } /** * Id of the dashboard page being viewed. * * @return string Page id (`dashboard`, `demo`, `compare`, `pricing`, `help`). */ private function current_page() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only menu routing. $slug = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; foreach ( self::get_pages() as $id => $page ) { if ( $page['slug'] === $slug ) { return $id; } } return 'dashboard'; } /** * Prints the mount point of the dashboard app. */ public function render_page() { if ( ! current_user_can( self::CAPABILITY ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to access this page.', 'camelplug-video-player' ) ); } printf( '
', esc_attr( $this->current_page() ), esc_html__( 'Please enable JavaScript to use the Video Player dashboard.', 'camelplug-video-player' ) ); } /** * Loads the dashboard app on the dashboard pages only. * * @param string $hook_suffix Current admin page hook. */ public function enqueue_assets( $hook_suffix ) { if ( ! in_array( $hook_suffix, $this->hooks, true ) ) { return; } $asset_file = CAMELPLUG_VIDEO_PLAYER_DIR_PATH . 'build/admin-dashboard.asset.php'; if ( ! file_exists( $asset_file ) ) { return; } $asset = include $asset_file; wp_enqueue_script( self::HANDLE, CAMELPLUG_VIDEO_PLAYER_DIR_URL . 'build/admin-dashboard.js', array_merge( $asset['dependencies'], array( 'wp-core-data' ) ), $asset['version'], true ); wp_enqueue_style( self::HANDLE, CAMELPLUG_VIDEO_PLAYER_DIR_URL . 'build/admin-dashboard.css', array( 'wp-components' ), $asset['version'] ); wp_add_inline_script( self::HANDLE, 'window.cvpAdminDashboard = ' . wp_json_encode( $this->get_app_data() ) . ';', 'before' ); wp_set_script_translations( self::HANDLE, 'camelplug-video-player', CAMELPLUG_VIDEO_PLAYER_DIR_PATH . 'languages' ); } /** * License state from Freemius, for the dashboard's License card. * * @return array License data. */ private function get_license_data() { $fs = function_exists( 'cvp_fs' ) ? cvp_fs() : null; if ( ! is_object( $fs ) ) { return array( 'status' => 'unknown' ); } $license = $fs->_get_license(); $status = 'free'; if ( cvp_is_premium() ) { $status = $fs->is_trial() ? 'trial' : 'active'; } elseif ( is_object( $license ) && $license->is_expired() ) { $status = 'expired'; } return array( 'status' => $status, 'isRegistered' => $fs->is_registered(), 'expires' => ( is_object( $license ) && ! empty( $license->expiration ) ) ? mysql2date( get_option( 'date_format' ), $license->expiration ) : '', 'isLifetime' => is_object( $license ) && $license->is_lifetime(), // Freemius' own screens: license activation (Pro code) and account (change / deactivate the license). 'activationUrl' => $fs->get_activation_url( array( 'require_license' => 'true' ) ), 'accountUrl' => $fs->is_registered() ? $fs->get_account_url() : '', ); } /** * Data the dashboard app needs. * * @return array App data. */ private function get_app_data() { $pages = array(); foreach ( self::get_pages() as $id => $page ) { $pages[] = array( 'id' => $id, 'label' => isset( $page['nav'] ) ? $page['nav'] : $page['menu'], 'url' => admin_url( 'admin.php?page=' . $page['slug'] ), ); } return array( 'page' => $this->current_page(), 'pages' => $pages, 'version' => CAMELPLUG_VIDEO_PLAYER_VERSION, 'slug' => 'camelplug-video-player', 'isPremium' => cvp_is_premium(), 'checkoutUrl' => CVP_Premium::CHECKOUT_URL, 'newPageUrl' => admin_url( 'post-new.php?post_type=page' ), 'mediaUrl' => admin_url( 'upload.php' ), 'license' => $this->get_license_data(), ); } /** * Adds "Dashboard" (and "Go Pro" for free sites) next to the plugin on the Plugins screen. * * @param string[] $links Existing action links. * @return string[] Action links. */ public function action_links( $links ) { if ( current_user_can( self::CAPABILITY ) ) { array_unshift( $links, sprintf( '%s', esc_url( admin_url( 'admin.php?page=' . self::MENU_SLUG ) ), esc_html__( 'Dashboard', 'camelplug-video-player' ) ) ); // "Go Pro": the plugin's Pricing page (not for Pro users, who have no Pricing page). if ( ! cvp_is_premium() ) { $links['go_pro'] = sprintf( '%s', esc_url( CVP_Premium::pricing_url() ), esc_html__( 'Go Pro', 'camelplug-video-player' ) ); } } return $links; } } }