open_wizard(); } /** * Add dashboard menu page */ public function add_theme_page() { $page_title = __( 'Brandy', 'brandy' ); // $icon = ''; $icon = 'data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIGlkPSJMYXllcl8xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB4PSIwcHgiIHk9IjBweCINCgkJICB2aWV3Qm94PSIwIDAgMjAgMjAiIHN0eWxlPSJlbmFibGUtYmFja2dyb3VuZDpuZXcgMCAwIDIwIDIwOyIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSI+DQoJCTxzdHlsZSB0eXBlPSJ0ZXh0L2NzcyI+DQoJCSAuc3Qwe2ZpbGw6IzlBQTBBNTt9DQoJCTwvc3R5bGU+DQoJCTxnPg0KCQkgPHBhdGggY2xhc3M9InN0MCIgZD0iTTIuOSwxNC43VjMuNkMyLjksMi4yLDQuMSwxLDUuNSwxSDEwYzEsMCwyLDAuMiwyLjksMC43YzEuMSwwLjYsMi4zLDEuOCwyLjcsNGMwLjEsMC45LDAsMS44LTAuNCwyLjYNCgkJICAgICBjLTAuNCwwLjctMSwxLjYtMi4yLDIuMmMwLDAtMi4zLDEuNC00LjgsMkM4LjIsMTIuNSwzLjgsMTMuNiwyLjksMTQuN3oiLz4NCgkJIDxwYXRoIGNsYXNzPSJzdDAiIGQ9Ik0xNC43LDEwLjNjMCwwLTIuMywxLjgtNS45LDIuOGMwLDAtMy45LDEuMy00LjcsMS42YzAsMC0xLjQsMC43LTEuMiwyLjNjMCwwLDAuMiwxLjgsMi4yLDEuOWg3LjQNCgkJICAgICBjMCwwLDIuNCwwLjEsMy44LTJjMC42LTAuOSwwLjgtMS45LDAuOC0yLjlDMTcsMTIuOSwxNi41LDExLjQsMTQuNywxMC4zeiIvPg0KCQk8L2c+DQoJCTwvc3ZnPg=='; $page_id = add_menu_page( $page_title, $page_title, self::CAP, self::PAGE_ID, array( $this, 'render_theme_page' ), $icon, self::POSITION ); add_action( 'load-' . $page_id, array( $this, 'load_page_dependencies' ) ); } /** * Load page dependencies */ public function load_page_dependencies() { ThemeDashboardVite::enqueue_vite( 'main.tsx', '3005' ); wp_localize_script( 'module/brandy-theme-dashboard/main.tsx', 'brandyDashboardData', array_merge( array( 'version' => BRANDY_VERSION, 'links' => array( 'dashboard' => admin_url( 'themes.php?page=' . self::PAGE_ID ), 'customize' => admin_url( 'customize.php?return=/wp-admin/admin.php?page=' . self::PAGE_ID ), 'home_page' => home_url(), ), 'ajax' => array( 'path' => admin_url( 'admin-ajax.php' ), 'nonces' => array( 'install_niche' => wp_create_nonce( self::INSTALL_NICHE_NONCE_ACTION ), 'install_plugin' => wp_create_nonce( self::INSTALL_PLUGIN_NONCE_ACTION ), 'get_plugins_information' => wp_create_nonce( self::GET_PLUGINS_INFORMATION_NONCE_ACTION ), ), ), 'urls' => array( 'images' => BRANDY_TEMPLATE_URL . '/assets/images/', 'admin' => admin_url(), ), 'suggested_plugins' => PluginService::get_suggested_plugins(), 'required_plugins' => PluginService::get_required_plugins(), 'i18n' => I18n::get_translations(), 'bts' => array( 'niches' => NicheLoader::get_instance()->get_niches( true ), 'is_plugin_installed' => is_bts_installed(), ), ), is_elementor_installed() ? array( 'elementor' => array( 'version' => ELEMENTOR_VERSION, ), ) : array() ) ); } /** * Render dashboard page content */ public function render_theme_page() { global $pagenow; $is_theme_page = 'themes.php' == $pagenow; $is_dashboard_page = isset( $_GET['page'] ) && self::PAGE_ID === $_GET['page']; //phpcs:ignore. $is_wizard_screen = isset( $_GET['action'] ) && 'open-wizard' === $_GET['action']; //phpcs:ignore. if ( $is_theme_page && $is_dashboard_page && $is_wizard_screen ) { update_option( 'brandy_first_time_installed', 'no' ); } echo '
'; } /** * Check whether can open wizard on dashboard page */ private function can_open_wizard() { $option_value = get_option( 'brandy_first_time_installed', null ); if ( 'no' == $option_value ) { return false; } return true; } /** * Add sample posts when site doesn't have enough posts to show * * @since 1.1.5 */ private function add_sample_posts_for_empty_site() { require_once ABSPATH . 'wp-admin/includes/media.php'; require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/image.php'; $sample_posts = ImportService::read_posts_from_json( BRANDY_TEMPLATE_DIR . '/inc/Niches/WooCommerce/sample-data/sample-posts.json' ); $sample_posts = array_slice( $sample_posts, 0, 4 ); $query = new \WP_Query( array( 'post_type' => 'post', 'post_name__in' => array_map( function( $p ) { return $p['name']; }, $sample_posts ), ) ); $existed_posts_name = array_map( function( $p ) { return $p->post_name; }, $query->have_posts() ? $query->posts : array() ); $not_existed_posts = array_filter( $sample_posts, function( $post_info ) use ( $existed_posts_name ) { return ! in_array( $post_info['name'], $existed_posts_name, true ); } ); foreach ( $not_existed_posts as $post_info ) { $comment_status = isset( $post_info['comment_status'] ) ? ( empty( $post_info['comment_status'] ) ? 'closed' : 'open' ) : 'open'; $comments = isset( $post_info['comments'] ) ? $post_info['comments'] : array(); $post_args = array( 'post_title' => $post_info['title'], 'post_type' => 'post', 'post_name' => $post_info['name'], 'post_content' => $post_info['content'], 'post_category' => $post_info['post_category'], 'tags_input' => $post_info['tags_input'], 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_author' => 1, 'menu_order' => 0, 'comment_status' => $comment_status, ); $inserted_id = wp_insert_post( $post_args ); if ( 0 === $inserted_id || is_wp_error( $inserted_id ) ) { continue; } if ( ! empty( $post_info['post_meta'] ) ) { foreach ( $post_info['post_meta'] as $key => $value ) { update_post_meta( $inserted_id, $key, $value ); } } foreach ( $comments as $comment ) { wp_insert_comment( array( 'comment_post_ID' => $inserted_id, 'comment_content' => $comment, ) ); } ImportService::add_featured_image( $inserted_id, $post_info['featured_image'] ?? '', $post_info['name'] ?? '' ); } } /** * Handle opening wizard */ public function open_wizard() { if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { return; } if ( ! $this->can_open_wizard() ) { return; } $site_posts = get_posts( array( 'post_status' => 'publish', 'posts_per_page' => 4, ) ); if ( count( $site_posts ) < 4 ) { $this->add_sample_posts_for_empty_site(); } global $pagenow; if ( 'themes.php' == $pagenow && isset( $_GET['page'] ) && self::PAGE_ID === $_GET['page'] ) { //phpcs:ignore. return; } wp_safe_redirect( admin_url( 'themes.php?page=' . self::PAGE_ID . '&action=open-wizard#/wizard' ) ); } /** * Callback for calling install niche AJAX */ public function install_niche() { $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : ''; if ( ! wp_verify_nonce( $nonce, self::INSTALL_NICHE_NONCE_ACTION ) ) { throw new Error( __( 'Invalid nonce', 'brandy' ) ); } $niche = isset( $_POST['niche'] ) ? $_POST['niche'] : ''; if ( empty( $niche ) ) { throw new Error( __( 'Invalid data', 'brandy' ) ); } $builder = isset( $_POST['builder'] ) ? $_POST['builder'] : 'gutenberg'; $content_list = isset( $_POST['content_list'] ) ? $_POST['content_list'] : array(); if ( empty( $niche ) ) { throw new Error( __( 'Invalid data', 'brandy' ) ); } try { $result = NicheService::get_instance()->install_niche( $niche, $content_list, $builder ); wp_send_json_success( array( 'message' => __( 'Import successfully', 'brandy' ), 'import_data' => $result, ) ); } catch ( \Error $err ) { wp_send_json_success( array( 'success' => false, 'message' => $err->getMessage(), ) ); } finally { if ( in_array( BRANDY_LAST_SITE_CONTENT_INSTALATION, $content_list, true ) ) { do_action( "brandy_{$niche}_installed" ); } } } /** * Callback for calling install plugin AJAX */ public function install_plugin() { $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : ''; if ( ! wp_verify_nonce( $nonce, self::INSTALL_PLUGIN_NONCE_ACTION ) ) { throw new Error( __( 'Invalid nonce', 'brandy' ) ); } $plugin = isset( $_POST['plugin'] ) ? $_POST['plugin'] : ''; if ( empty( $plugin ) || empty( $plugin['download_link'] ) ) { throw new Error( __( 'Invalid data', 'brandy' ) ); } try { PluginService::install_and_activate_plugin( $plugin ); wp_send_json_success( array( 'message' => __( 'Install successfully', 'brandy' ), 'plugin' => $plugin, ) ); } catch ( \Error $err ) { wp_send_json_success( array( 'success' => false, 'message' => $err->getMessage(), ) ); } } /** * Callback for calling activate plugin AJAX */ public function activate_plugin() { $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : ''; if ( ! wp_verify_nonce( $nonce, self::INSTALL_PLUGIN_NONCE_ACTION ) ) { throw new Error( __( 'Invalid nonce', 'brandy' ) ); } $plugin = isset( $_POST['plugin'] ) ? $_POST['plugin'] : ''; if ( empty( $plugin ) ) { throw new Error( __( 'Invalid data', 'brandy' ) ); } try { PluginService::install_and_activate_plugin( $plugin ); wp_send_json_success( array( 'message' => __( 'Activate successfully', 'brandy' ), 'plugin' => $plugin, ) ); } catch ( \Error $err ) { wp_send_json_success( array( 'success' => false, 'message' => $err->getMessage(), ) ); } } /** * Callback for calling get plugins information AJAX */ public function get_plugins_information() { try { $nonce = isset( $_GET['nonce'] ) ? sanitize_text_field( $_GET['nonce'] ) : ''; if ( ! wp_verify_nonce( $nonce, self::GET_PLUGINS_INFORMATION_NONCE_ACTION ) ) { throw new Error( __( 'Invalid nonce', 'brandy' ) ); } $plugins_information = PluginService::get_plugins_information(); wp_send_json_success( array( 'success' => true, 'plugins_information' => $plugins_information, ) ); } catch ( \Error $err ) { wp_send_json_success( array( 'success' => false, 'message' => $err->getMessage(), ) ); } } public function add_dashboard_to_admin_bar( $wp_admin_bar ) { if ( ! user_can( get_current_user_id(), self::CAP ) ) { return; } // Add an option to visit the store. $wp_admin_bar->add_node( array( 'parent' => 'site-name', 'id' => 'brandy-dashboard', 'title' => __( 'Brandy Dashboard', 'brandy' ), 'href' => admin_url( 'admin.php?page=' . self::PAGE_ID ), ) ); } }