BRANDY_VERSION, 'links' => array( 'dashboard' => admin_url( 'themes.php?page=' . self::PAGE_ID ), 'customize' => admin_url( 'customize.php?return=' . admin_url( '/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(), 'bss' => array( 'niches' => array_filter( NicheLoader::get_instance()->get_niches( true ), function( $niche ) { return 'default' !== $niche['id']; } ), 'is_plugin_installed' => brandy_is_starter_sites_installed(), ), ) ) ); } /** * Render dashboard page content */ public function render_theme_page() { if ( $this->is_starter_sites_screen() ) { update_option( 'brandy_first_time_installed', 'no' ); } echo '
'; } private function is_starter_sites_screen() { global $pagenow; if ( 'admin.php' != $pagenow ) { return false; } if ( ! isset( $_GET['page'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended return false; } if ( self::PAGE_ID !== $_GET['page'] ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended return false; } if ( ! isset( $_GET['action'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended return false; } if ( 'open-starter-sites' !== $_GET['action'] ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended return false; } return true; } /** * Check whether can open starter sites on dashboard page */ private function is_first_time_installed() { $option_value = get_option( 'brandy_first_time_installed', null ); if ( 'no' == $option_value ) { return false; } return true; } /** * Handle opening starter sites */ public function after_switch_theme() { if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { return; } if ( ! $this->is_first_time_installed() ) { return; } $current_niche = brandy_current_niche(); if ( 'default' === $current_niche ) { $niche_instance = new NicheService( 'default' ); $posts_count = wp_count_posts('post')->publish; $content_to_install = array('pages', 'menus'); if ($posts_count < 3) { $content_to_install[] = 'posts'; } $niche_instance->install_niche( $content_to_install ); } if ( $this->is_starter_sites_screen() ) { return; } wp_safe_redirect( admin_url( 'admin.php?page=' . self::PAGE_ID . '&action=open-starter-sites#/starter-sites' ) ); } /** * 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 { $last_content_installation = 'widgets'; if ( in_array( $last_content_installation, $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(), ) ); } } /** * Get niches data for the Brandy Sites plugin */ public function get_niches_data() { try { $niches_data = array_filter( NicheLoader::get_instance()->get_niches( true ), function( $niche ) { return 'default' !== $niche['id']; } ); wp_send_json_success( array( 'success' => true, 'niches' => $niches_data, 'is_plugin_installed' => true ) ); } catch ( \Error $err ) { wp_send_json_error( 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' ), ) ); } } }