niche_service = NicheService::get_instance();
add_action( 'admin_menu', array( $this, 'add_theme_page' ) );
add_action( 'wp_ajax_brandy_install_niche', array( $this, 'install_niche' ) );
add_action( 'wp_ajax_brandy_install_plugin', array( $this, 'install_plugin' ) );
add_action( 'wp_ajax_brandy_activate_plugin', array( $this, 'activate_plugin' ) );
add_action( 'wp_ajax_brandy_get_plugins_information', array( $this, 'get_plugins_information' ) );
$this->open_wizard();
}
/**
* Add dashboard menu page
*/
public function add_theme_page() {
$page_title = __( 'Brandy', 'brandy' );
// $icon = 'data:image/svg+xml;base64,' . \base64_encode(
// '
//
// '
// );
$icon = '';
$page_id = add_menu_page(
$page_title,
$page_title,
self::CAP,
self::PAGE_ID,
array( $this, 'render_theme_page' ),
$icon,
self::POSITION
);
// $page_id = add_submenu_page(
// 'themes.php',
// $page_title,
// $page_title,
// self::CAP,
// self::PAGE_ID,
// array( $this, 'render_theme_page' ),
// null
// );
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/themes.php?page=' . self::PAGE_ID ),
),
'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/',
),
'suggested_plugins' => PluginService::get_suggested_plugins(),
'required_plugins' => PluginService::get_required_plugins(),
'i18n' => I18n::get_translations(),
'bts' => array(
'niches' => isset( $this->niche_service->niches ) ? $this->niche_service->niches : array(),
'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;
}
/**
* Handle opening wizard
*/
public function open_wizard() {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
}
if ( ! $this->can_open_wizard() ) {
return;
}
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 = $this->niche_service->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(),
)
);
}
}
/**
* 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 ),
)
);
}
}