2013-07-24 16:01:36 +00:00
< ? php
/**
2015-11-03 13:53:50 +00:00
* WooCommerce Admin
2013-07-24 16:01:36 +00:00
*
2014-11-30 06:52:32 +00:00
* @ class WC_Admin
* @ author WooThemes
* @ category Admin
* @ package WooCommerce / Admin
2015-01-30 11:26:01 +00:00
* @ version 2.3
2013-07-24 16:01:36 +00:00
*/
2014-09-20 19:52:30 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ; // Exit if accessed directly
}
/**
* WC_Admin class .
*/
2013-07-24 16:01:36 +00:00
class WC_Admin {
/**
2015-11-03 12:28:01 +00:00
* Constructor .
2013-07-24 16:01:36 +00:00
*/
public function __construct () {
2013-11-07 09:53:24 +00:00
add_action ( 'init' , array ( $this , 'includes' ) );
2015-07-06 09:49:31 +00:00
add_action ( 'current_screen' , array ( $this , 'conditional_includes' ) );
2016-04-28 11:36:49 +00:00
add_action ( 'admin_init' , array ( $this , 'buffer' ), 1 );
2013-08-06 15:56:15 +00:00
add_action ( 'admin_init' , array ( $this , 'preview_emails' ) );
2015-07-04 22:11:12 +00:00
add_action ( 'admin_init' , array ( $this , 'prevent_admin_access' ) );
add_action ( 'admin_init' , array ( $this , 'admin_redirects' ) );
2015-07-06 09:49:31 +00:00
add_action ( 'admin_footer' , 'wc_print_js' , 25 );
2015-01-29 12:41:39 +00:00
add_filter ( 'admin_footer_text' , array ( $this , 'admin_footer_text' ), 1 );
2013-07-24 16:01:36 +00:00
}
2016-04-28 11:36:49 +00:00
/**
* Output buffering allows admin screens to make redirects later on .
*/
public function buffer () {
ob_start ();
}
2013-07-24 16:01:36 +00:00
/**
* Include any classes we need within admin .
*/
public function includes () {
2014-01-21 11:24:48 +00:00
include_once ( 'wc-admin-functions.php' );
include_once ( 'wc-meta-box-functions.php' );
2014-02-05 15:02:30 +00:00
include_once ( 'class-wc-admin-post-types.php' );
include_once ( 'class-wc-admin-taxonomies.php' );
2015-04-29 10:32:03 +00:00
include_once ( 'class-wc-admin-menus.php' );
include_once ( 'class-wc-admin-notices.php' );
include_once ( 'class-wc-admin-assets.php' );
2015-07-04 22:11:12 +00:00
include_once ( 'class-wc-admin-api-keys.php' );
2015-04-29 10:32:03 +00:00
include_once ( 'class-wc-admin-webhooks.php' );
2015-04-30 12:30:03 +00:00
include_once ( 'class-wc-admin-pointers.php' );
2013-07-24 16:01:36 +00:00
2015-04-29 10:32:03 +00:00
// Help Tabs
if ( apply_filters ( 'woocommerce_enable_admin_help_tab' , true ) ) {
include_once ( 'class-wc-admin-help.php' );
}
2013-07-24 16:01:36 +00:00
2015-04-29 10:32:03 +00:00
// Setup/welcome
if ( ! empty ( $_GET [ 'page' ] ) ) {
switch ( $_GET [ 'page' ] ) {
case 'wc-setup' :
2015-04-29 09:47:57 +00:00
include_once ( 'class-wc-admin-setup-wizard.php' );
2015-10-09 10:56:13 +00:00
break ;
2014-06-04 10:16:19 +00:00
}
2013-12-05 15:34:37 +00:00
}
2013-10-22 17:20:33 +00:00
2013-07-24 16:01:36 +00:00
// Importers
2014-06-04 10:16:19 +00:00
if ( defined ( 'WP_LOAD_IMPORTERS' ) ) {
2015-01-20 15:23:34 +00:00
include_once ( 'class-wc-admin-importers.php' );
2014-06-04 10:16:19 +00:00
}
2013-07-24 16:01:36 +00:00
}
2013-08-06 15:56:15 +00:00
/**
2015-11-03 12:28:01 +00:00
* Include admin files conditionally .
2013-08-06 15:56:15 +00:00
*/
2015-07-06 09:49:31 +00:00
public function conditional_includes () {
2016-02-05 12:16:41 +00:00
if ( ! $screen = get_current_screen () ) {
return ;
}
2013-07-24 16:01:36 +00:00
switch ( $screen -> id ) {
case 'dashboard' :
2013-07-25 14:00:23 +00:00
include ( 'class-wc-admin-dashboard.php' );
2013-07-24 16:01:36 +00:00
break ;
2014-06-04 10:16:19 +00:00
case 'options-permalink' :
include ( 'class-wc-admin-permalink-settings.php' );
break ;
2013-07-24 16:01:36 +00:00
case 'users' :
case 'user' :
case 'profile' :
2013-11-23 18:41:10 +00:00
case 'user-edit' :
2013-07-24 18:55:02 +00:00
include ( 'class-wc-admin-profile.php' );
2013-07-24 16:01:36 +00:00
break ;
}
}
2015-04-29 09:47:57 +00:00
/**
2015-04-29 10:24:29 +00:00
* Handle redirects to setup / welcome page after install and updates .
*
* Transient must be present , the user must have access rights , and we must ignore the network / bulk plugin updaters .
2015-04-29 09:47:57 +00:00
*/
public function admin_redirects () {
2015-10-23 11:24:04 +00:00
if ( ! get_transient ( '_wc_activation_redirect' ) ) {
2015-04-29 09:47:57 +00:00
return ;
}
2015-07-04 22:11:12 +00:00
delete_transient ( '_wc_activation_redirect' );
2015-12-22 11:03:31 +00:00
if ( ( ! empty ( $_GET [ 'page' ] ) && in_array ( $_GET [ 'page' ], array ( 'wc-setup' ) ) ) || is_network_admin () || isset ( $_GET [ 'activate-multi' ] ) || ! current_user_can ( 'manage_woocommerce' ) || apply_filters ( 'woocommerce_prevent_automatic_wizard_redirect' , false ) ) {
2015-07-04 22:11:12 +00:00
return ;
}
2015-04-29 09:47:57 +00:00
2015-07-04 22:11:12 +00:00
// If the user needs to install, send them to the setup wizard
2015-04-29 10:24:29 +00:00
if ( WC_Admin_Notices :: has_notice ( 'install' ) ) {
wp_safe_redirect ( admin_url ( 'index.php?page=wc-setup' ) );
2015-04-29 09:47:57 +00:00
exit ;
}
}
2013-08-06 15:56:15 +00:00
/**
2015-11-03 12:28:01 +00:00
* Prevent any user who cannot 'edit_posts' ( subscribers , customers etc ) from accessing admin .
2013-08-06 15:56:15 +00:00
*/
public function prevent_admin_access () {
$prevent_access = false ;
2015-07-04 22:11:12 +00:00
if ( 'yes' === get_option ( 'woocommerce_lock_down_admin' , 'yes' ) && ! is_ajax () && basename ( $_SERVER [ " SCRIPT_FILENAME " ] ) !== 'admin-post.php' && ! current_user_can ( 'edit_posts' ) && ! current_user_can ( 'manage_woocommerce' ) ) {
2013-08-06 15:56:15 +00:00
$prevent_access = true ;
}
$prevent_access = apply_filters ( 'woocommerce_prevent_admin_access' , $prevent_access );
if ( $prevent_access ) {
2015-02-15 19:13:22 +00:00
wp_safe_redirect ( wc_get_page_permalink ( 'myaccount' ) );
2013-08-06 15:56:15 +00:00
exit ;
}
}
/**
2015-11-03 12:28:01 +00:00
* Preview email template .
2014-09-02 19:50:19 +00:00
*
* @ return string
2013-08-06 15:56:15 +00:00
*/
public function preview_emails () {
2014-11-30 06:52:32 +00:00
2013-08-06 15:56:15 +00:00
if ( isset ( $_GET [ 'preview_woocommerce_mail' ] ) ) {
2014-06-17 21:40:54 +00:00
if ( ! wp_verify_nonce ( $_REQUEST [ '_wpnonce' ], 'preview-mail' ) ) {
2013-08-06 15:56:15 +00:00
die ( 'Security check' );
2014-06-17 21:40:54 +00:00
}
2013-08-06 15:56:15 +00:00
2014-12-16 18:32:54 +00:00
// load the mailer class
$mailer = WC () -> mailer ();
2013-08-06 15:56:15 +00:00
2014-12-16 18:32:54 +00:00
// get the preview email subject
$email_heading = __ ( 'HTML Email Template' , 'woocommerce' );
2013-08-06 15:56:15 +00:00
2014-12-16 18:32:54 +00:00
// get the preview email content
ob_start ();
2013-08-06 15:56:15 +00:00
include ( 'views/html-email-template-preview.php' );
$message = ob_get_clean ();
2014-12-16 18:32:54 +00:00
// create a new email
$email = new WC_Email ();
// wrap the content with the email template and then add styles
$message = $email -> style_inline ( $mailer -> wrap_message ( $email_heading , $message ) );
// print the preview email
echo $message ;
2013-08-06 15:56:15 +00:00
exit ;
}
}
2015-01-29 00:13:13 +00:00
/**
2015-11-03 12:28:01 +00:00
* Change the admin footer text on WooCommerce admin pages .
2015-01-29 00:13:13 +00:00
*
* @ since 2.3
2015-01-29 12:41:39 +00:00
* @ param string $footer_text
2015-01-29 00:13:13 +00:00
* @ return string
*/
public function admin_footer_text ( $footer_text ) {
2015-07-04 22:11:12 +00:00
if ( ! current_user_can ( 'manage_woocommerce' ) ) {
return ;
2015-04-28 14:06:38 +00:00
}
2015-01-29 12:41:39 +00:00
$current_screen = get_current_screen ();
2015-02-17 18:24:11 +00:00
$wc_pages = wc_get_screen_ids ();
// Set only wc pages
2015-02-18 17:06:11 +00:00
$wc_pages = array_flip ( $wc_pages );
2015-04-28 14:06:38 +00:00
if ( isset ( $wc_pages [ 'profile' ] ) ) {
unset ( $wc_pages [ 'profile' ] );
}
if ( isset ( $wc_pages [ 'user-edit' ] ) ) {
unset ( $wc_pages [ 'user-edit' ] );
}
2015-02-18 17:06:11 +00:00
$wc_pages = array_flip ( $wc_pages );
2015-02-17 18:24:11 +00:00
2015-01-29 12:41:39 +00:00
// Check to make sure we're on a WooCommerce admin page
2015-02-17 18:24:11 +00:00
if ( isset ( $current_screen -> id ) && apply_filters ( 'woocommerce_display_admin_footer_text' , in_array ( $current_screen -> id , $wc_pages ) ) ) {
2015-01-29 12:41:39 +00:00
// Change the footer text
2015-02-19 11:38:35 +00:00
if ( ! get_option ( 'woocommerce_admin_footer_text_rated' ) ) {
2015-08-05 19:17:52 +00:00
$footer_text = sprintf ( __ ( 'If you like <strong>WooCommerce</strong> please leave us a %s★★★★★%s rating. A huge thank you from WooThemes in advance!' , 'woocommerce' ), '<a href="https://wordpress.org/support/view/plugin-reviews/woocommerce?filter=5#postform" target="_blank" class="wc-rating-link" data-rated="' . esc_attr__ ( 'Thanks :)' , 'woocommerce' ) . '">' , '</a>' );
2015-02-19 11:38:35 +00:00
wc_enqueue_js ( "
2015-04-28 14:06:38 +00:00
jQuery ( 'a.wc-rating-link' ) . click ( function () {
2015-02-19 11:38:35 +00:00
jQuery . post ( '" . WC()->ajax_url() . "' , { action : 'woocommerce_rated' } );
2015-04-28 14:06:38 +00:00
jQuery ( this ) . parent () . text ( jQuery ( this ) . data ( 'rated' ) );
2015-02-19 11:38:35 +00:00
});
" );
} else {
$footer_text = __ ( 'Thank you for selling with WooCommerce.' , 'woocommerce' );
}
2015-01-29 00:13:13 +00:00
}
return $footer_text ;
}
2013-07-24 16:01:36 +00:00
}
2014-06-17 21:40:54 +00:00
return new WC_Admin ();