theme_name = $theme->get( 'Name' );
$this->theme_version = $theme->get( 'Version' );
$this->theme_slug = $theme->get_template();
$this->page_slug = $this->theme_slug . '-dashboard';
$this->review_url = 'https://wordpress.org/support/theme/' . $this->theme_slug . '/reviews/?rate=5#new-post';
$template_link = add_query_arg(
array(
'page' => $this->page_slug,
'section' => 'getting-started',
),
admin_url( 'themes.php' )
);
$this->redirect_template_url = $template_link;
// Define Tabs Sections.
$this->tab_sections = array(
'getting-started' => __( 'Getting Started', 'blogmarks' ),
'recommended-plugins' => __( 'Recommended Plugins', 'blogmarks' ),
'starter-templates' => __( 'Starter Templates', 'blogmarks' ),
'free-vs-pro' => __( 'Free vs Pro', 'blogmarks' ),
);
// List of Plugins used on starter setup.
$this->starter_template_plugins = array(
array(
'name' => __( 'One Click Demo Import', 'blogmarks' ),
'desc' => __( 'Import your content, widgets and theme settings with one click.', 'blogmarks' ),
'slug' => 'one-click-demo-import',
'path' => 'one-click-demo-import/one-click-demo-import.php',
'status' => self::get_plugin_status( 'one-click-demo-import/one-click-demo-import.php' ),
'icon' => 'https://ps.w.org/one-click-demo-import/assets/icon-256x256.png',
),
);
// And any extra list of Recommended Plugins.
$this->recommended_plugins = array_merge(
$this->starter_template_plugins,
array(
array(
'name' => __( 'MailChimp for WordPress', 'blogmarks' ),
'desc' => __( 'Adds various highly effective sign-up methods to your site.', 'blogmarks' ),
'slug' => 'mailchimp-for-wp',
'path' => 'mailchimp-for-wp/mailchimp-for-wp.php',
'status' => self::get_plugin_status( 'mailchimp-for-wp/mailchimp-for-wp.php' ),
'icon' => 'https://ps.w.org/mailchimp-for-wp/assets/icon-256x256.png',
),
)
);
$this->errors = array(
'permission' => __( 'Sorry, you do not have permission to perform this action.', 'blogmarks' ),
'nonce' => __( 'Could not verifry the nonce.', 'blogmarks' ),
'default' => __( 'Oops! something went wrong.', 'blogmarks' ),
'invalid' => __( 'Not found!', 'blogmarks' ),
);
if ( current_user_can( 'manage_options' ) ) {
// Create admin notices.
add_action( 'admin_notices', array( $this, 'display_admin_notice' ), 99 );
// Create a settings page.
add_action( 'admin_menu', array( $this, 'add_menu' ) );
add_action( 'admin_bar_menu', array( $this, 'add_bar_menu' ), 999 );
}
add_action( 'admin_init', array( $this, 'init_reminder' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'wp_ajax_blogmarks_dismiss_notice', array( $this, 'dismiss_notice' ) );
add_action( 'wp_ajax_blogmarks_install_plugin', 'wp_ajax_install_plugin' );
add_action( 'wp_ajax_blogmarks_activate_plugin', array( $this, 'activate_plugin' ) );
add_action( 'switch_theme', array( $this, 'reset_notices' ) );
add_action( 'after_switch_theme', array( $this, 'reset_notices' ) );
}
/**
* Update the reminder time.
*
* @since 1.0.0
*/
public function init_reminder() {
if ( ! get_option( 'blogmarks_reminder_time' ) ) {
update_option( 'blogmarks_reminder_time', time() + 14 * DAY_IN_SECONDS );
}
}
/**
* Delete the saved options.
*
* @since 1.0.0
*/
public function reset_notices() {
delete_option( 'blogmarks_reminder_time' );
delete_option( 'blogmarks_dismissed_notices' );
}
/**
* Generate proper data for starter plugin installation.
*
* @since 1.0.0
*/
public function starter_template_button() {
$plugins_active = true;
$btn_html = '';
$data = array();
if ( $this->starter_template_plugins && is_array( $this->starter_template_plugins ) ) :
foreach ( $this->starter_template_plugins as $plugin_data ) :
if ( 'activated' === $plugin_data['status'] ) :
$data_action = '';
else :
$plugins_active = false;
if ( 'install' === $plugin_data['status'] ) {
$data_action = 'blogmarks_install_plugin';
} else {
$data_action = 'blogmarks_activate_plugin';
}
endif;
$data_slug = $plugin_data['slug'];
$data_path = $plugin_data['path'];
$btn_html .= "
";
endforeach;
endif;
$data['plugins_active'] = $plugins_active;
$data['btn_html'] = $btn_html;
return $data;
}
/**
* Dislpay admin notices.
*
* @since 1.0.0
*/
public function display_admin_notice() {
if ( $this->can_display_notice() && ! $this->is_dismissed( 'welcome' ) ) {
$this->display_welcome_notice();
}
$reminder_time = get_option( 'blogmarks_reminder_time' );
if ( $this->can_display_notice() && ! $this->is_dismissed( 'review' ) && ! empty( $reminder_time ) && time() > $reminder_time ) {
$this->display_review_notice();
}
}
/**
* Display welcome notice.
*
* @since 1.0.0
*/
public function display_welcome_notice() {
$user = wp_get_current_user();
$btn_data = $this->starter_template_button();
?>
display_name ) ); ?>
theme_name
);
?>
theme_version ); ?>
theme_name
);
?>
theme_name
);
?>
review_url ) . '">',
''
);
?>
id,
array(
'dashboard',
'themes',
)
);
}
/**
* Has a notice been dismissed?
*
* @since 1.0.0
*
* @param string $notice Notice name.
* @return bool
*/
public static function is_dismissed( $notice ) {
$dismissed = get_option( 'blogmarks_dismissed_notices', array() );
return in_array( $notice, $dismissed );
}
/**
* Stores a dismissed notice in database
*
* @since 1.0.0
*
* @param string $notice Notice to be Dismissed.
* @return void
*/
public static function dismiss( $notice ) {
$dismissed = get_option( 'blogmarks_dismissed_notices', array() );
if ( ! in_array( $notice, $dismissed ) ) {
$dismissed[] = $notice;
update_option( 'blogmarks_dismissed_notices', array_unique( $dismissed ) );
}
}
/**
* Add theme dashboard page.
*
* @since 1.0.0
*/
public function add_menu() {
add_theme_page(
__( 'Theme Dashboard', 'blogmarks' ),
__( 'Theme Dashboard', 'blogmarks' ),
'manage_options',
$this->page_slug,
array( $this, 'render_main_page' ),
1
);
}
/**
* Add link in menu bar
*
* @since 1.0.0
*/
public function add_bar_menu() {
global $wp_admin_bar;
if ( ! is_super_admin() || ! is_admin_bar_showing() ) {
return;
}
$wp_admin_bar->add_menu(
array(
'parent' => 'site-name',
'id' => $this->page_slug,
'title' => esc_html__( 'Theme Dashboard', 'blogmarks' ),
'href' => admin_url( "themes.php?page=$this->page_slug" ),
)
);
}
/**
* Dashboard Page
*
* @since 1.0.0
*/
public function render_main_page() {
$tabs = $this->tab_sections;
?>
errors[ $type ] ) ) {
$type = 'default';
}
return $this->errors[ $type ];
}
/**
* Acticate plugin callback.
*
* @since 1.0.0
*/
public function activate_plugin() {
$response_data = array( 'message' => $this->get_error_msg( 'permission' ) );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( $response_data );
}
if ( empty( $_POST ) ) {
$response_data = array( 'message' => $this->get_error_msg( 'invalid' ) );
wp_send_json_error( $response_data );
}
if ( ! check_ajax_referer( 'blogmarks-plugin-activate', 'nonce', false ) ) {
$response_data = array( 'message' => $this->get_error_msg( 'nonce' ) );
wp_send_json_error( $response_data );
}
if ( ! current_user_can( 'install_plugins' ) || ! isset( $_POST['path'] ) || ! sanitize_text_field( wp_unslash( $_POST['path'] ) ) ) {
wp_send_json_error(
array(
'success' => false,
'message' => __( 'Please specify a plugin', 'blogmarks' ),
)
);
}
$plugin_path = ( isset( $_POST['path'] ) ) ? sanitize_text_field( wp_unslash( $_POST['path'] ) ) : '';
$activate = activate_plugin( $plugin_path, '', false, true );
if ( is_wp_error( $activate ) ) {
wp_send_json_error(
array(
'success' => false,
'message' => $activate->get_error_message(),
)
);
}
wp_send_json_success(
array(
'success' => true,
'message' => __( 'Plugin Activated Successfully', 'blogmarks' ),
)
);
}
/**
* Enqueue scripts for dashboard page.
*
* @since 1.0.0
*
* @param string $hook Page hook.
*/
public function enqueue_scripts( $hook ) {
if ( ! in_array(
$hook,
array(
'index.php',
'themes.php',
'appearance_page_blogmarks-dashboard',
)
) ) {
return;
}
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_enqueue_style( 'blogmarks-dashboard-style', get_template_directory_uri() . '/include/dashboard/css/style' . $min . '.css', array(), $this->theme_version );
wp_enqueue_script( 'blogmarks-dashboard-script', get_template_directory_uri() . '/include/dashboard/js/build.js', array( 'jquery' ), $this->theme_version, true );
wp_localize_script(
'blogmarks-dashboard-script',
'blogmarksDashboard',
array(
'notice_dismiss_nonce' => wp_create_nonce( 'blogmarks-dismiss-nonce' ),
'plugin_install_nonce' => wp_create_nonce( 'updates' ),
'plugin_activate_nonce' => wp_create_nonce( 'blogmarks-plugin-activate' ),
'plugin_installing_text' => __( 'Installing', 'blogmarks' ),
'plugin_installed_text' => __( 'Installed', 'blogmarks' ),
'plugin_activating_text' => __( 'Activating', 'blogmarks' ),
'plugin_activated_text' => __( 'Activated', 'blogmarks' ),
'plugin_activate_text' => __( 'Activate', 'blogmarks' ),
)
);
}
/**
* Dismiss notice callback
*
* @since 1.0.0
*/
public function dismiss_notice() {
$response_data = array( 'message' => $this->get_error_msg( 'permission' ) );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( $response_data );
}
if ( empty( $_POST ) ) {
$response_data = array( 'message' => $this->get_error_msg( 'invalid' ) );
wp_send_json_error( $response_data );
}
if ( ! check_ajax_referer( 'blogmarks-dismiss-nonce', 'nonce', false ) ) {
$response_data = array( 'message' => $this->get_error_msg( 'nonce' ) );
wp_send_json_error( $response_data );
}
$dismiss = isset( $_POST['dismiss'] ) ? sanitize_text_field( $_POST['dismiss'] ) : '';
if ( ! $dismiss ) {
wp_send_json_error(
array(
'success' => false,
'message' => __( 'No dismissable notice found.', 'blogmarks' ),
)
);
}
$dismiss_type = isset( $_POST['dismiss_type'] ) ? sanitize_text_field( $_POST['dismiss_type'] ) : '';
if ( $dismiss_type && 'remind' === $dismiss_type ) {
$reminder_time = time() + 14 * DAY_IN_SECONDS;
update_option( 'blogmarks_reminder_time', $reminder_time );
} else {
self::dismiss( $dismiss );
}
wp_send_json_success(
array(
'success' => true,
'message' => __( 'Notice Dismissed Successfully', 'blogmarks' ),
)
);
}
}
}
Blogmarks_Dashboard::get_instance();