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'] ?? '' ); } } private function add_sample_pages_for_empty_site() { $sample_pages = array( array( 'title' => 'Home', 'name' => 'homepage', 'content' => '', 'page_display' => 'front_page', ), array( 'title' => 'Blog', 'name' => 'blog', 'content' => '', 'page_display' => 'blogs_page', ), array( 'title' => 'About us', 'name' => 'about-us', 'content' => '', 'page_template' => 'about-us', 'page_display' => 'about_us', ), ); $query = new \WP_Query( array( 'post_type' => 'page', 'post_name__in' => array_map( function( $p ) { return $p['name']; }, $sample_pages ), ) ); $existed_pages_name = array(); $imported_pages = array(); if ( $query->have_posts() ) { foreach ( $query->posts as $p ) { $same_pages = array_filter( $sample_pages, function( $item ) use ( $p ) { return $p->post_name === $item['name']; } ); $find_page = end( $same_pages ); $data = array( 'id' => $p->ID, 'name' => $p->post_name, 'title' => $p->post_title, ); if ( $find_page ) { $data['post_meta'] = $find_page['post_meta'] ?? array(); } $imported_pages[] = $data; $existed_pages_name[] = $p->post_name; } } $not_existed_pages = array_filter( $sample_pages, function( $page_info ) use ( $existed_pages_name ) { return ! in_array( $page_info['name'], $existed_pages_name, true ); } ); $imported_pages = array(); foreach ( $not_existed_pages as $page_info ) { $post_args = array_merge( array( 'post_title' => $page_info['title'], 'post_type' => 'page', 'post_name' => $page_info['name'], 'post_content' => $page_info['content'], 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_author' => 1, 'menu_order' => 0, ), isset( $page_info['page_template'] ) ? array( 'page_template' => $page_info['page_template'], ) : array() ); $new_post_id = wp_insert_post( $post_args ); $inserted_pages[] = $new_post_id; $imported_pages[] = array( 'id' => $new_post_id, 'title' => $page_info['title'], 'name' => $page_info['name'], 'post_meta' => $page_info['post_meta'] ?? array(), ); if ( 0 === $new_post_id || is_wp_error( $new_post_id ) ) { continue; } } foreach ( $imported_pages as $data ) { $matching_items = array_values( array_filter( $sample_pages, function( $item ) use ( $data ) { return $item['name'] == $data['name']; } ) ); if ( count( $matching_items ) < 1 ) { continue; } $page_info = $matching_items[0]; $new_post_id = $data['id']; if ( ! empty( $page_info['page_display'] ) && 'front_page' === $page_info['page_display'] ) { update_option( 'show_on_front', 'page' ); update_option( 'page_on_front', $new_post_id ); } if ( ! empty( $page_info['page_display'] ) && 'blogs_page' === $page_info['page_display'] ) { update_option( 'show_on_front', 'page' ); update_option( 'page_for_posts', $new_post_id ); } if ( ! empty( $page_info['page_display'] ) && 'about_us' === $page_info['page_display'] ) { update_option( 'page_for_about_us', $new_post_id ); } if ( ! empty( $page_info['page_display'] ) && 'contact_us' === $page_info['page_display'] ) { update_option( 'page_for_contact_us', $new_post_id ); } } } private function add_sample_menus() { $menu_info = array( 'name' => __( 'Brandy sample menu', 'brandy' ), 'locations' => array( 'primary-menu', 'header-menu-1' ), 'items' => array( array( 'title' => __( 'Home', 'brandy' ), 'status' => 'publish', 'url' => home_url(), ), array( 'title' => __( 'Blogs', 'brandy' ), 'status' => 'publish', 'object_id' => brandy_get_blog_page_id(), 'item_object' => 'page', 'item_type' => 'post_type', ), array( 'title' => __( 'About us', 'brandy' ), 'status' => 'publish', 'object_id' => brandy_get_about_us_page_id(), 'item_object' => 'page', 'item_type' => 'post_type', ), ), ); $menu_exists = wp_get_nav_menu_object( $menu_info['name'] ); if ( $menu_exists ) { return; } $menu_id = wp_create_nav_menu( $menu_info['name'] ); if ( is_wp_error( $menu_id ) ) { return; } $parent_menus = array(); foreach ( $menu_info['items'] as $menu_item ) { if ( isset( $menu_item['parent'] ) ) { continue; } $id = wp_update_nav_menu_item( $menu_id, 0, empty( $menu_item['object_id'] ) ? array( 'menu-item-title' => $menu_item['title'] ?? 'Sample item', 'menu-item-url' => $menu_item['url'] ?? '#', 'menu-item-status' => $menu_item['status'] ?? 'publish', ) : array( 'menu-item-type' => isset( $menu_item['item_type'] ) ? $menu_item['item_type'] : '', 'menu-item-object-id' => $menu_item['object_id'], 'menu-item-object' => $menu_item['item_object'], 'menu-item-status' => $menu_item['status'], ) ); if ( ! is_wp_error( $id ) ) { $parent_menus[ $menu_item['key'] ] = $id; } } } /** * 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(); } $this->add_sample_pages_for_empty_site(); $this->add_sample_menus(); 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 = ( new NicheService( $niche ) )->install_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->remove_node( 'plugins' ); $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 ), ) ); if ( current_user_can( 'activate_plugins' ) ) { $wp_admin_bar->add_node( array( 'parent' => 'site-name', 'id' => 'plugins', 'title' => __( 'Plugins', 'brandy' ), 'href' => admin_url( 'plugins.php' ), ) ); } } }