' with __return_true. * Then, you can hook to 'vphq_custom_admin_page_pre_render-' to register/queue js/css files or take actions before your page gets rendered. * By default, the rendered template will be VPHQ_ROOT . '/custom-pages/' . . '.php', but you can also hook to 'vphq_custom_admin_page_template-' to change that. * * Alternatively, you can add your page in the array $simple_pages in register_simple_admin_pages and it will be automatically registered. * Available options there are : * - has_css : boolean, whether to register a css file for this page. The css file should be located in /assets/css/custom-page-.css * - has_js : boolean, whether to register a js file for this page. The js file should be located in /assets/js/custom-page-/index.js * - has_menu : boolean, whether to show the side menu when the page is rendered. * - on_include/on_render : callable, a function to call before rendering the page (both are called one after the other). */ class VideoPressHQCustomAdminPage { private const QUERY_VAR = 'vp-custom-page'; private const ADMIN_PAGE_CSS_KEY = 'custom-page-admin-style'; private const EDIT_HELPERS_JS_KEY = 'custom-page-edit-helpers-scripts'; private const EDIT_HELPERS_CSS_KEY = 'custom-page-edit-helpers-style'; private const EDIT_PLAYLIST_CSS_KEY = 'custom-page-edit-playlist-style'; private const EDIT_PLAYLIST_JS_KEY = 'custom-page-edit-playlist-scripts'; public static function register() { $current_user = wp_get_current_user(); // No need to register this for non-logged in users. if ( ! $current_user || $current_user->ID <= 0 ) { return; } add_action( 'template_include', array( __CLASS__, 'template_include' ) ); self::register_simple_admin_pages(); } private static function register_simple_admin_pages() { $simple_pages = array( 'dashboard' => array( 'has_css' => false ), 'appearance' => array( 'has_css' => true, 'has_js' => true, 'on_include' => function() { wp_register_style( 'coloris-color-picker-css', get_template_directory_uri() . '/assets/css/coloris.min.css' ); wp_register_script( 'coloris-color-picker-js', get_template_directory_uri() . '/assets/js/coloris.min.js' ); self::register_edit_helpers(); }, 'on_render' => function() { wp_enqueue_style( 'coloris-color-picker-css' ); wp_enqueue_script( 'coloris-color-picker-js' ); self::include_edit_helpers(); }, ), 'manage-comments' => array( 'has_css' => true ), 'manage-categories' => array( 'has_css' => true, 'has_js' => true, 'localize' => array( 'manageCategoriesTranslations' => array( 'script' => self::get_custom_page_js_handle( 'manage-categories' ), 'values' => array( 'confirmDelete' => __( 'Do you really want to delete this category? This action cannot be undone.', 'videopress-hq' ), ), ) ), ), 'manage-playlists' => array( 'has_css' => true ), 'manage-videos' => array( 'has_css' => true, 'has_js' => true, ), 'edit' => array( 'on_include' => function () { self::register_edit_helpers(); VideoPressHQCustomPageEdit::on_include(); }, 'on_render' => function () { self::include_edit_helpers(); VideoPressHQCustomPageEdit::on_render(); }, ), 'edit-playlist' => array( 'on_include' => array( __CLASS__, 'edit_playlists_on_include' ), 'on_render' => array( __CLASS__, 'edit_playlists_on_render' ), ), 'onboarding-upload' => array( 'has_menu' => false, 'has_css' => true, 'on_include' => array( VideoPressHQUploader::class, 'on_include' ), 'on_render' => array( VideoPressHQUploader::class, 'on_render' ), ), 'onboarding' => array( 'has_menu' => false, 'has_css' => true, 'has_js' => true, 'on_include' => array( __CLASS__, 'onboarding_on_include' ), 'on_render' => function () { self::include_edit_helpers(); }, ), 'onboarding-clear' => array(), 'onboarding-customize' => array( 'has_menu' => false, 'has_css' => true, 'has_js' => true, 'on_include' => array( __CLASS__, 'onboarding_customize_on_include' ), 'on_render' => function () { self::include_edit_helpers(); }, ), ); foreach ( $simple_pages as $page => $props ) { add_filter( 'vphq_custom_admin_page_exists-' . $page, '__return_true' ); if ( isset( $props['on_include'] ) ) { add_action( 'vphq_custom_admin_page_pre_render-' . $page, $props['on_include'] ); } if ( isset( $props['on_render'] ) ) { add_action( 'vphq_custom_admin_page_pre_render-' . $page, $props['on_render'] ); } if ( isset( $props['has_css'] ) && $props['has_css'] ) { add_action( 'vphq_custom_admin_page_pre_render-' . $page, function () use ( $page ) { wp_enqueue_style( self::get_custom_page_css_handle( $page ), get_template_directory_uri() . '/assets/css/custom-page-' . $page . '.css' ); } ); } if ( isset( $props['has_js'] ) && $props['has_js'] ) { add_action( 'vphq_custom_admin_page_pre_render-' . $page, function () use ( $page ) { wp_enqueue_script( self::get_custom_page_js_handle( $page ), get_template_directory_uri() . '/assets/js/custom-page-' . $page . '/index.js', array( VideoPressHQ::GLOBAL_FUNCTIONS_SCRIPT_ID ) ); } ); } if ( isset( $props['localize'] ) ) { foreach( $props['localize'] as $localize_key => $localize_data ) { add_action( 'vphq_custom_admin_page_pre_render-' . $page, function () use ( $localize_key, $localize_data ) { wp_localize_script( $localize_data['script'], $localize_key, $localize_data['values'] ); } ); } } if ( isset( $props['has_menu'] ) && ! $props['has_menu'] ) { add_action( 'vphq_custom_admin_page_pre_render-' . $page, function () use ( $page ) { add_filter( VideoPressHQAdmin::FILTER_ADMIN_HAS_MENU, '__return_false' ); } ); } } } public static function template_include( $template ) { $custom_page = trim( sanitize_text_field( get_query_var( self::QUERY_VAR ) ) ); if ( empty( $custom_page ) ) { return $template; } $custom_page_exists = apply_filters( 'vphq_custom_admin_page_exists-' . $custom_page, false ); if ( ! $custom_page_exists ) { return $template; } wp_register_style( self::ADMIN_PAGE_CSS_KEY, get_template_directory_uri() . '/assets/css/custom-page-admin.css' ); return VPHQ_ROOT . '/custom-pages/template.php'; } public static function get_custom_page_css_handle( $custom_page_name ) { return 'custom-page-' . $custom_page_name . '-css'; } public static function get_custom_page_js_handle( $custom_page_name ) { return 'custom-page-' . $custom_page_name . '-js'; } public static function render_page() { $custom_page = trim( sanitize_text_field( get_query_var( self::QUERY_VAR ) ) ); if ( empty( $custom_page ) ) { /// @todo: 404 ? return ''; } $custom_page_exists = apply_filters( 'vphq_custom_admin_page_exists-' . $custom_page, false ); if ( ! $custom_page_exists ) { /// @todo 404 ? return ''; } wp_register_script( self::get_custom_page_js_handle( 'admin-template' ), get_template_directory_uri() . '/assets/js/custom-page-admin-template/index.js', array( VideoPressHQ::GLOBAL_FUNCTIONS_SCRIPT_ID ) ); do_action( 'vphq_custom_admin_page_pre_render-' . $custom_page ); wp_enqueue_style( 'dashicons' ); wp_enqueue_style( self::ADMIN_PAGE_CSS_KEY ); wp_enqueue_style( self::get_custom_page_css_handle( $custom_page ) ); wp_enqueue_script( self::get_custom_page_js_handle( 'admin-template' ) ); wp_enqueue_script( self::get_custom_page_js_handle( $custom_page ) ); $page_template = VPHQ_ROOT . '/custom-pages/' . $custom_page . '.php'; $page_template = apply_filters( 'vphq_custom_admin_page_template-' . $custom_page, $page_template ); if ( empty( $page_template ) ) { /// @todo 404 ? return ''; } ob_start(); block_template_part( 'header' ); include VPHQ_ROOT . '/custom-pages/admin-template.php'; block_template_part( 'footer' ); return ob_get_clean(); } private static function register_edit_helpers() { wp_register_style( self::EDIT_HELPERS_CSS_KEY, get_template_directory_uri() . '/assets/css/edit-helpers.css' ); wp_register_script( self::EDIT_HELPERS_JS_KEY, get_template_directory_uri() . '/assets/js/edit-helpers.js' ); wp_localize_script( self::EDIT_HELPERS_JS_KEY, 'videopressHqEditHelpers', array( 'ajaxUrl' => vphq_get_admin_url( 'admin-ajax.php' ), 'templateDirUri' => get_template_directory_uri(), 'loadingText' => __( 'Please wait' ), ) ); } private static function include_edit_helpers() { wp_enqueue_script( self::EDIT_HELPERS_JS_KEY ); wp_enqueue_style( self::EDIT_HELPERS_CSS_KEY ); } public static function edit_playlists_on_include() { self::register_edit_helpers(); wp_register_style( self::EDIT_PLAYLIST_CSS_KEY, get_template_directory_uri() . '/assets/css/custom-page-edit-playlist.css' ); wp_register_script( self::EDIT_PLAYLIST_JS_KEY, get_template_directory_uri() . '/assets/js/custom-page-edit-playlist/index.js', array( VideoPressHQ::GLOBAL_FUNCTIONS_SCRIPT_ID, self::EDIT_HELPERS_JS_KEY ) ); wp_localize_script( self::EDIT_PLAYLIST_JS_KEY, 'editPlaylistTr', array( 'confirmDelete' => __( 'Do you really want to delete this playlist? This action cannot be undone.' ), 'managePlaylistsUri' => vphq_admin_page_link( 'playlists' ), 'addVideoText' => __( 'Add to playlist' ), 'viewVideoText' => __( 'View' ), ) ); } public static function edit_playlists_on_render() { self::include_edit_helpers(); wp_enqueue_script( self::EDIT_PLAYLIST_JS_KEY, array( VideoPressHQ::GLOBAL_FUNCTIONS_SCRIPT_ID, self::EDIT_HELPERS_JS_KEY ) ); wp_enqueue_style( self::EDIT_PLAYLIST_CSS_KEY ); } public static function onboarding_on_include() { self::register_edit_helpers(); wp_register_script( self::get_custom_page_js_handle( 'onboarding' ), get_template_directory_uri() . '/assets/js/custom-page-onboarding/index.js', array( VideoPressHQ::GLOBAL_FUNCTIONS_SCRIPT_ID, self::EDIT_HELPERS_JS_KEY ) ); wp_localize_script( self::get_custom_page_js_handle( 'onboarding' ), 'onboardingTranslations', array( 'loadingText' => array( __( 'Analyzing description...' ), __( 'Generating channel info...' ), __( 'Generating theme...' ), __( 'Customizing theme...' ), __( 'Preparing channel...' ), __( 'Almost ready...' ), ), 'redirectUrl' => vphq_admin_page_link( 'onboarding/customize' ), ) ); } public static function onboarding_customize_on_include() { self::register_edit_helpers(); wp_register_script( self::get_custom_page_js_handle( 'onboarding-customize' ), get_template_directory_uri() . '/assets/js/custom-page-onboarding-customize/index.js', array( VideoPressHQ::GLOBAL_FUNCTIONS_SCRIPT_ID, self::EDIT_HELPERS_JS_KEY ) ); wp_localize_script( self::get_custom_page_js_handle( 'onboarding-customize' ), 'onboardingTranslations', array( 'redirectUrl' => vphq_admin_page_link( 'onboarding/upload' ), ) ); } }