2017-02-09 18:39:28 +00:00
< ? php
/**
* Storefront NUX Admin Class
*
* @ package storefront
* @ since 2.0 . 0
*/
if ( ! defined ( 'ABSPATH' ) ) {
exit ;
}
if ( ! class_exists ( 'Storefront_NUX_Admin' ) ) :
/**
* The Storefront NUX Admin class
*/
class Storefront_NUX_Admin {
/**
* Setup class .
*
2017-03-08 16:41:29 +00:00
* @ since 2.2 . 0
2017-02-09 18:39:28 +00:00
*/
public function __construct () {
2018-06-22 15:11:39 +01:00
add_action ( 'admin_enqueue_scripts' , array ( $this , 'enqueue_scripts' ) );
add_action ( 'admin_notices' , array ( $this , 'admin_notices' ), 99 );
add_action ( 'wp_ajax_storefront_dismiss_notice' , array ( $this , 'dismiss_nux' ) );
add_action ( 'admin_post_storefront_starter_content' , array ( $this , 'redirect_customizer' ) );
add_action ( 'init' , array ( $this , 'log_fresh_site_state' ) );
add_filter ( 'admin_body_class' , array ( $this , 'admin_body_class' ) );
2017-02-21 02:08:32 +02:00
}
2017-02-09 18:39:28 +00:00
/**
* Enqueue scripts .
*
2017-03-08 16:41:29 +00:00
* @ since 2.2 . 0
2017-02-09 18:39:28 +00:00
*/
public function enqueue_scripts () {
global $wp_customize , $storefront_version ;
if ( isset ( $wp_customize ) || true === ( bool ) get_option ( 'storefront_nux_dismissed' ) ) {
return ;
}
2017-08-03 12:57:59 +01:00
$suffix = ( defined ( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min' ;
2018-02-08 18:29:45 +00:00
wp_enqueue_style ( 'storefront-admin-nux' , get_template_directory_uri () . '/assets/css/admin/admin.css' , '' , $storefront_version );
2017-02-09 18:39:28 +00:00
2017-08-03 12:57:59 +01:00
wp_enqueue_script ( 'storefront-admin-nux' , get_template_directory_uri () . '/assets/js/admin/admin' . $suffix . '.js' , array ( 'jquery' ), $storefront_version , 'all' );
2017-02-09 18:39:28 +00:00
$storefront_nux = array (
2018-06-22 15:11:39 +01:00
'nonce' => wp_create_nonce ( 'storefront_notice_dismiss' ),
2017-02-09 18:39:28 +00:00
);
wp_localize_script ( 'storefront-admin-nux' , 'storefrontNUX' , $storefront_nux );
}
/**
* Output admin notices .
*
2017-03-08 16:41:29 +00:00
* @ since 2.2 . 0
2017-02-09 18:39:28 +00:00
*/
public function admin_notices () {
2017-02-14 11:37:00 +00:00
global $pagenow ;
2018-04-03 13:04:33 +01:00
2017-02-09 18:39:28 +00:00
if ( true === ( bool ) get_option ( 'storefront_nux_dismissed' ) ) {
return ;
}
2018-04-03 13:04:33 +01:00
// Coming from the WooCommerce Wizard?
2018-04-03 13:08:37 +01:00
if ( wp_get_referer () && 'index.php?page=wc-setup&step=next_steps' === basename ( wp_get_referer () ) && 'post-new.php' === $pagenow ) {
2018-04-03 13:04:33 +01:00
return ;
}
2017-02-09 18:39:28 +00:00
?>
< div class = " notice notice-info sf-notice-nux is-dismissible " >
< span class = " sf-icon " >
2017-02-26 18:43:20 +02:00
< ? php echo '<img src="' . esc_url ( get_template_directory_uri () ) . '/assets/images/admin/storefront-icon.svg" alt="Storefront" width="250" />' ; ?>
2017-02-09 18:39:28 +00:00
</ span >
< div class = " notice-content " >
2017-02-21 02:08:32 +02:00
< ? php if ( ! storefront_is_woocommerce_activated () && current_user_can ( 'install_plugins' ) && current_user_can ( 'activate_plugins' ) ) : ?>
2017-02-14 11:37:00 +00:00
< h2 >< ? php esc_attr_e ( 'Thanks for installing Storefront, you rock! 🤘' , 'storefront' ); ?> </h2>
< p >< ? php esc_attr_e ( 'To enable eCommerce features you need to install the WooCommerce plugin.' , 'storefront' ); ?> </p>
2017-02-21 02:08:32 +02:00
< p >< ? php Storefront_Plugin_Install :: install_plugin_button ( 'woocommerce' , 'woocommerce.php' , 'WooCommerce' , array ( 'sf-nux-button' ), __ ( 'WooCommerce activated' , 'storefront' ), __ ( 'Activate WooCommerce' , 'storefront' ), __ ( 'Install WooCommerce' , 'storefront' ) ); ?> </p>
2017-02-09 18:39:28 +00:00
< ? php endif ; ?>
< ? php if ( storefront_is_woocommerce_activated () ) : ?>
2017-02-14 11:37:00 +00:00
< h2 >< ? php esc_html_e ( 'Design your store 🎨' , 'storefront' ); ?> </h2>
< p >
< ? php
if ( true === ( bool ) get_option ( 'storefront_nux_fresh_site' ) && 'post-new.php' === $pagenow ) {
echo esc_attr__ ( 'Before you add your first product let\'s design your store. We\'ll add some example products for you. When you\'re ready let\'s get started by adding your logo.' , 'storefront' );
} else {
echo esc_attr__ ( 'You\'ve set up WooCommerce, now it\'s time to give it some style! Let\'s get started by entering the Customizer and adding your logo.' , 'storefront' );
2018-06-22 15:11:39 +01:00
}
?>
2017-02-14 11:37:00 +00:00
</ p >
2017-02-09 18:39:28 +00:00
< form action = " <?php echo esc_url( admin_url( 'admin-post.php' ) ); ?> " method = " post " >
2017-09-08 09:34:40 +02:00
< input type = " hidden " name = " action " value = " storefront_starter_content " >
< ? php wp_nonce_field ( 'storefront_starter_content' ); ?>
2017-02-09 18:39:28 +00:00
< ? php if ( true === ( bool ) get_option ( 'storefront_nux_fresh_site' ) ) : ?>
< input type = " hidden " name = " homepage " value = " on " >
2017-02-18 02:03:28 +02:00
< ? php endif ; ?>
< ? php if ( true === ( bool ) get_option ( 'storefront_nux_fresh_site' ) && true === $this -> _is_woocommerce_empty () ) : ?>
2017-02-09 18:39:28 +00:00
< input type = " hidden " name = " products " value = " on " >
< ? php endif ; ?>
< ? php if ( false === ( bool ) get_option ( 'storefront_nux_fresh_site' ) ) : ?>
< label >
< input type = " checkbox " name = " homepage " checked >
< ? php
2018-06-22 15:11:39 +01:00
if ( 'page' === get_option ( 'show_on_front' ) ) {
esc_attr_e ( 'Apply the Storefront homepage template' , 'storefront' );
} else {
esc_attr_e ( 'Create a homepage using Storefront\'s homepage template' , 'storefront' );
}
2017-02-09 18:39:28 +00:00
?>
</ label >
2017-02-18 02:03:28 +02:00
< ? php if ( true === $this -> _is_woocommerce_empty () ) : ?>
2017-02-09 18:39:28 +00:00
< label >
< input type = " checkbox " name = " products " checked >
< ? php esc_attr_e ( 'Add example products' , 'storefront' ); ?>
</ label >
2017-02-18 02:03:28 +02:00
< ? php endif ; ?>
2017-02-09 18:39:28 +00:00
< ? php endif ; ?>
2017-02-14 13:16:02 +00:00
< input type = " submit " name = " storefront-guided-tour " class = " sf-nux-button " value = " <?php esc_attr_e( 'Let \ 's go!', 'storefront' ); ?> " >
2017-02-09 18:39:28 +00:00
</ form >
< ? php endif ; ?>
</ div >
</ div >
2018-06-22 15:11:39 +01:00
< ? php
}
2017-02-09 18:39:28 +00:00
/**
* AJAX dismiss notice .
*
2017-03-08 16:41:29 +00:00
* @ since 2.2 . 0
2017-02-09 18:39:28 +00:00
*/
public function dismiss_nux () {
2018-11-21 19:12:40 +00:00
if ( ! isset ( $_POST [ 'nonce' ] ) || ! wp_verify_nonce ( sanitize_key ( wp_unslash ( $_POST [ 'nonce' ] ) ), 'storefront_notice_dismiss' ) || ! current_user_can ( 'manage_options' ) ) { // WPCS: input var ok.
2017-02-09 18:39:28 +00:00
die ();
}
update_option ( 'storefront_nux_dismissed' , true );
}
/**
* Redirects to the customizer with the correct variables .
*
2017-03-08 16:41:29 +00:00
* @ since 2.2 . 0
2017-02-09 18:39:28 +00:00
*/
public function redirect_customizer () {
2017-09-08 09:34:40 +02:00
check_admin_referer ( 'storefront_starter_content' );
2017-02-09 18:39:28 +00:00
if ( current_user_can ( 'manage_options' ) ) {
2017-09-08 09:34:40 +02:00
2017-02-09 18:39:28 +00:00
// Dismiss notice.
update_option ( 'storefront_nux_dismissed' , true );
}
2017-09-08 09:44:44 +02:00
$args = array ( 'sf_starter_content' => '1' );
2017-02-09 18:39:28 +00:00
$tasks = array ();
2018-11-21 19:12:40 +00:00
if ( ! empty ( $_REQUEST [ 'homepage' ] ) && 'on' === sanitize_text_field ( wp_unslash ( $_REQUEST [ 'homepage' ] ) ) ) { // WPCS: input var ok.
2017-02-09 18:39:28 +00:00
if ( current_user_can ( 'edit_pages' ) && 'page' === get_option ( 'show_on_front' ) ) {
$this -> _assign_page_template ( get_option ( 'page_on_front' ), 'template-homepage.php' );
} else {
$tasks [] = 'homepage' ;
}
}
2018-11-21 19:12:40 +00:00
if ( ! empty ( $_REQUEST [ 'products' ] ) && 'on' === sanitize_text_field ( wp_unslash ( $_REQUEST [ 'products' ] ) ) ) { // WPCS: input var ok.
2017-02-09 18:39:28 +00:00
$tasks [] = 'products' ;
}
if ( ! empty ( $tasks ) ) {
$args [ 'sf_tasks' ] = implode ( ',' , $tasks );
2017-05-22 20:12:59 +01:00
if ( current_user_can ( 'manage_options' ) ) {
2017-09-08 09:34:40 +02:00
2017-05-22 20:12:59 +01:00
// Make sure the fresh_site flag is set to true.
update_option ( 'fresh_site' , true );
2017-05-22 22:41:26 +01:00
if ( current_user_can ( 'edit_pages' ) && true === ( bool ) get_option ( 'storefront_nux_fresh_site' ) ) {
2017-05-22 20:12:59 +01:00
$this -> _set_woocommerce_pages_full_width ();
}
}
2017-02-09 18:39:28 +00:00
}
2017-02-10 15:47:43 +00:00
// Redirect to the Storefront Welcome screen when exiting the Customizer.
2018-11-21 17:32:12 +00:00
$args [ 'return' ] = rawurlencode ( admin_url ( 'themes.php?page=storefront-welcome' ) );
2017-02-10 15:47:43 +00:00
2017-02-09 18:39:28 +00:00
wp_safe_redirect ( add_query_arg ( $args , admin_url ( 'customize.php' ) ) );
die ();
}
/**
* Get WooCommerce page ids .
*
2017-03-08 16:41:29 +00:00
* @ since 2.2 . 0
2017-02-09 18:39:28 +00:00
*/
public static function get_woocommerce_pages () {
$woocommerce_pages = array ();
2018-06-22 15:11:39 +01:00
$wc_pages_options = apply_filters (
'storefront_page_option_names' , array (
'woocommerce_cart_page_id' ,
'woocommerce_checkout_page_id' ,
'woocommerce_myaccount_page_id' ,
'woocommerce_shop_page_id' ,
'woocommerce_terms_page_id' ,
)
);
2017-02-09 18:39:28 +00:00
foreach ( $wc_pages_options as $option ) {
$page_id = get_option ( $option );
if ( ! empty ( $page_id ) ) {
$page_id = intval ( $page_id );
if ( null !== get_post ( $page_id ) ) {
$woocommerce_pages [ $option ] = $page_id ;
}
}
}
return $woocommerce_pages ;
}
/**
* Update Storefront fresh site flag .
*
2017-03-08 16:41:29 +00:00
* @ since 2.2 . 0
2017-02-09 18:39:28 +00:00
*/
public function log_fresh_site_state () {
2017-05-24 15:08:02 +01:00
if ( null === get_option ( 'storefront_nux_fresh_site' , null ) ) {
2017-02-09 18:39:28 +00:00
update_option ( 'storefront_nux_fresh_site' , get_option ( 'fresh_site' ) );
}
}
2017-03-08 16:20:38 +00:00
/**
* Add custom classes to the list of admin body classes .
*
2017-03-08 16:41:29 +00:00
* @ since 2.2 . 0
2017-03-08 16:20:38 +00:00
* @ param string $classes Classes for the admin body element .
* @ return string
*/
public function admin_body_class ( $classes ) {
if ( true === ( bool ) get_option ( 'storefront_nux_dismissed' ) ) {
return $classes ;
}
$classes .= ' sf-nux ' ;
return $classes ;
}
2017-02-09 18:39:28 +00:00
/**
* Check if WooCommerce is installed .
*
2017-03-08 16:41:29 +00:00
* @ since 2.2 . 0
2017-02-09 18:39:28 +00:00
*/
private function _is_woocommerce_installed () {
if ( file_exists ( WP_PLUGIN_DIR . '/woocommerce' ) ) {
$plugins = get_plugins ( '/woocommerce' );
if ( ! empty ( $plugins ) ) {
$keys = array_keys ( $plugins );
$plugin_file = 'woocommerce/' . $keys [ 0 ];
2018-06-22 15:11:39 +01:00
$url = wp_nonce_url (
add_query_arg (
array (
'action' => 'activate' ,
'plugin' => $plugin_file ,
), admin_url ( 'plugins.php' )
), 'activate-plugin_' . $plugin_file
);
2017-02-09 18:39:28 +00:00
return $url ;
}
}
return false ;
}
/**
* Set WooCommerce pages to use the full width template .
*
2017-03-08 16:41:29 +00:00
* @ since 2.2 . 0
2017-02-09 18:39:28 +00:00
*/
private function _set_woocommerce_pages_full_width () {
$wc_pages = $this -> get_woocommerce_pages ();
foreach ( $wc_pages as $option => $page_id ) {
$this -> _assign_page_template ( $page_id , 'template-fullwidth.php' );
}
}
/**
* Given a page id assign a given page template to it .
*
2017-03-08 16:41:29 +00:00
* @ since 2.2 . 0
2018-06-22 15:11:39 +01:00
* @ param int $page_id Page id .
* @ param string $template Template file name .
* @ return void | bool Returns false if $page_id or $template is empty .
2017-02-09 18:39:28 +00:00
*/
private function _assign_page_template ( $page_id , $template ) {
if ( empty ( $page_id ) || empty ( $template ) || '' === locate_template ( $template ) ) {
return false ;
}
update_post_meta ( $page_id , '_wp_page_template' , $template );
}
2017-02-18 02:03:28 +02:00
/**
* Check if WooCommerce is empty .
*
2017-03-08 16:41:29 +00:00
* @ since 2.2 . 0
2017-02-18 02:03:28 +02:00
* @ return bool
*/
private function _is_woocommerce_empty () {
$products = wp_count_posts ( 'product' );
if ( 0 < $products -> publish ) {
return false ;
}
return true ;
}
2017-02-09 18:39:28 +00:00
}
endif ;
2018-02-08 18:29:45 +00:00
return new Storefront_NUX_Admin ();