attach_ajax_actions(); } if ( $this->is_dashboard_page() ) { add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_static' ] ); } if ( $this->is_dashboard_page() ) { add_action( 'admin_print_scripts', function () { global $wp_filter; if ( is_user_admin() ) { if ( isset( $wp_filter['user_admin_notices'] ) ) { unset( $wp_filter['user_admin_notices'] ); } } elseif ( isset( $wp_filter['admin_notices'] ) ) { unset( $wp_filter['admin_notices'] ); } if ( isset( $wp_filter['all_admin_notices'] ) ) { unset( $wp_filter['all_admin_notices'] ); } } ); } } /** * Load system status data */ public function load_system_status() { $system_status = new CT_System_Status(); wp_send_json_success( $system_status->output() ); } /** * Compute data for jed * * @param string $domain Domain to load translations for. */ public function get_jed_locale_data( $domain ) { $translations = get_translations_for_domain( $domain ); $locale = [ '' => [ 'domain' => $domain, 'lang' => is_admin() ? get_user_locale() : get_locale(), ], ]; if ( ! empty( $translations->headers['Plural-Forms'] ) ) { $locale['']['plural_forms'] = $translations->headers['Plural-Forms']; } foreach ( $translations->entries as $msgid => $entry ) { $locale[ $msgid ] = $entry->translations; } return $locale; } /** * Enqueue statics for theme */ public function enqueue_static() { $theme = wp_get_theme(); if ( ! wp_script_is( 'wp-i18n' ) ) { wp_register_script( 'wp-i18n', get_template_directory_uri() . '/admin/dashboard/static/bundle/i18n.js', [], $theme->get( 'Version' ), false ); } wp_enqueue_script( 'wp-i18n' ); $locale_data_ct = $this->get_jed_locale_data( 'ct' ); wp_add_inline_script( 'wp-i18n', 'wp.i18n.setLocaleData( ' . wp_json_encode( $locale_data_ct ) . ', "ct" );' ); $dependencies = [ 'underscore', 'wp-util', ]; wp_enqueue_script( 'ct-dashboard-scripts', get_template_directory_uri() . '/admin/dashboard/static/bundle/main.js', $dependencies, $theme->get( 'Version' ), true ); if ( defined( 'WP_DEBUG' ) ) { wp_localize_script( 'ct-dashboard-scripts', 'WP_DEBUG', [ 'debug' => true ] ); } $manager = new CT_Plugin_Manager(); $plugins_config = $manager->get_config(); wp_localize_script( 'ct-dashboard-scripts', 'ctDashboardLocalizations', [ 'ajax_url' => admin_url( 'admin-ajax.php' ), 'customizer_url' => admin_url( '/customize.php?autofocus' ), 'theme_version' => $theme->get( 'Version' ), 'theme_name' => $theme->get( 'Name' ), 'clean_install_plugins' => $plugins_config, ] ); wp_enqueue_style( 'ct-dashboard-styles', get_template_directory_uri() . '/admin/dashboard/static/bundle/main.css', [], $theme->get( 'Version' ) ); } /** * Add admin menu page */ public function setup_framework_page() { $welcome_page_options = [ 'title' => __( 'Blocksy', 'ct' ), 'menu-title' => __( 'Blocksy', 'ct' ), 'permision' => 'activate_plugins', 'top-level-handle' => $this->page_slug, 'callback' => [ $this, 'welcome_page_template' ], 'icon-url' => get_template_directory_uri() . '/admin/dashboard/static/img/navigation.svg', 'position' => 2, ]; $this->menu_page( $welcome_page_options['title'], $welcome_page_options['menu-title'], $welcome_page_options['permision'], $welcome_page_options['top-level-handle'], $welcome_page_options['callback'], $welcome_page_options['icon-url'], $welcome_page_options['position'] ); $this->submenu_page( $welcome_page_options['top-level-handle'], __( 'Dashboard', 'ct' ), __( 'Dashboard', 'ct' ), $welcome_page_options['permision'], $welcome_page_options['top-level-handle'] ); } public function menu_page($page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '', $position = null) { global $menu, $admin_page_hooks, $_registered_pages, $_parent_pages; $menu_slug = plugin_basename( $menu_slug ); $admin_page_hooks[$menu_slug] = sanitize_title( $menu_title ); $hookname = get_plugin_page_hookname( $menu_slug, '' ); if ( !empty( $function ) && !empty( $hookname ) && current_user_can( $capability ) ) add_action( $hookname, $function ); if ( empty($icon_url) ) { $icon_url = 'dashicons-admin-generic'; $icon_class = 'menu-icon-generic '; } else { $icon_url = set_url_scheme( $icon_url ); $icon_class = ''; } $new_menu = array( $menu_title, $capability, $menu_slug, $page_title, 'menu-top ' . $icon_class . $hookname, $hookname, $icon_url ); if ( null === $position ) { $menu[] = $new_menu; } elseif ( isset( $menu[ "$position" ] ) ) { $position = $position + substr( base_convert( md5( $menu_slug . $menu_title ), 16, 10 ) , -5 ) * 0.00001; $menu[ "$position" ] = $new_menu; } else { $menu[ $position ] = $new_menu; } $_registered_pages[$hookname] = true; // No parent as top level $_parent_pages[$menu_slug] = false; return $hookname; } public function submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { global $submenu, $menu, $_wp_real_parent_file, $_wp_submenu_nopriv, $_registered_pages, $_parent_pages; $menu_slug = plugin_basename( $menu_slug ); $parent_slug = plugin_basename( $parent_slug); if ( isset( $_wp_real_parent_file[$parent_slug] ) ) $parent_slug = $_wp_real_parent_file[$parent_slug]; if ( !current_user_can( $capability ) ) { $_wp_submenu_nopriv[$parent_slug][$menu_slug] = true; return false; } /* * If the parent doesn't already have a submenu, add a link to the parent * as the first item in the submenu. If the submenu file is the same as the * parent file someone is trying to link back to the parent manually. In * this case, don't automatically add a link back to avoid duplication. */ if (!isset( $submenu[$parent_slug] ) && $menu_slug != $parent_slug ) { foreach ( (array)$menu as $parent_menu ) { if ( $parent_menu[2] == $parent_slug && current_user_can( $parent_menu[1] ) ) $submenu[$parent_slug][] = array_slice( $parent_menu, 0, 4 ); } } $submenu[$parent_slug][] = array ( $menu_title, $capability, $menu_slug, $page_title ); $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug); if (!empty ( $function ) && !empty ( $hookname )) add_action( $hookname, $function ); $_registered_pages[$hookname] = true; /* * Backward-compatibility for plugins using add_management page. * See wp-admin/admin.php for redirect from edit.php to tools.php */ if ( 'tools.php' == $parent_slug ) $_registered_pages[get_plugin_page_hookname( $menu_slug, 'edit.php')] = true; // No parent as top level. $_parent_pages[$menu_slug] = $parent_slug; return $hookname; } /** * Redirect */ public function redirect_after_theme_switch() { if ( ! ct_is_dashboard_page() ) { wp_safe_redirect( admin_url( '/admin.php?page=' . $this->page_slug, 'http' ) ); exit; } } /** * Render templates for welcome page */ public function welcome_page_template() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html( __( 'You do not have sufficient permissions to access this page.', 'ct' ) ) ); } echo '
'; } } new CT_Dashboard_Page();