woocommerce/includes/admin/class-wc-admin.php

233 lines
6.8 KiB
PHP
Raw Normal View History

<?php
/**
* WooCommerce Admin.
*
* @class WC_Admin
* @author WooThemes
* @category Admin
* @package WooCommerce/Admin
* @version 2.3
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* WC_Admin class.
*/
class WC_Admin {
/**
* Constructor
*/
public function __construct() {
2013-11-07 09:53:24 +00:00
add_action( 'init', array( $this, 'includes' ) );
add_action( 'current_screen', array( $this, 'conditonal_includes' ) );
2015-04-29 09:47:57 +00:00
add_action( 'admin_init', array( $this, 'admin_redirects' ) );
add_action( 'admin_init', array( $this, 'prevent_admin_access' ) );
add_action( 'admin_init', array( $this, 'preview_emails' ) );
add_action( 'admin_footer', 'wc_print_js', 25 );
add_filter( 'admin_footer_text', array( $this, 'admin_footer_text' ), 1 );
}
/**
* Include any classes we need within admin.
*/
public function includes() {
// Functions
include_once( 'wc-admin-functions.php' );
include_once( 'wc-meta-box-functions.php' );
// Classes
include_once( 'class-wc-admin-post-types.php' );
include_once( 'class-wc-admin-taxonomies.php' );
// Classes we only need during non-ajax requests
if ( ! is_ajax() ) {
include_once( 'class-wc-admin-menus.php' );
include_once( 'class-wc-admin-notices.php' );
include_once( 'class-wc-admin-assets.php' );
include_once( 'class-wc-admin-webhooks.php' );
// Help
if ( apply_filters( 'woocommerce_enable_admin_help_tab', true ) ) {
include_once( 'class-wc-admin-help.php' );
}
2015-04-28 12:19:16 +00:00
2015-04-29 09:47:57 +00:00
// Setup/welcome
if ( ! empty( $_GET['page'] ) && current_user_can( 'manage_woocommerce' ) ) {
2015-04-29 09:59:59 +00:00
if ( apply_filters( 'woocommerce_enable_setup_wizard', true ) && 'wc-setup' === $_GET['page'] ) {
2015-04-29 09:47:57 +00:00
include_once( 'class-wc-admin-setup-wizard.php' );
} elseif ( 'wc-about' === $_GET['page'] || 'wc-credits' === $_GET['page'] || 'wc-translators' === $_GET['page'] ) {
include_once( 'class-wc-admin-welcome.php' );
}
2015-04-28 12:19:16 +00:00
}
}
// Importers
if ( defined( 'WP_LOAD_IMPORTERS' ) ) {
include_once( 'class-wc-admin-importers.php' );
}
}
/**
* Include admin files conditionally
*/
public function conditonal_includes() {
$screen = get_current_screen();
switch ( $screen->id ) {
case 'dashboard' :
2013-07-25 14:00:23 +00:00
include( 'class-wc-admin-dashboard.php' );
break;
case 'options-permalink' :
include( 'class-wc-admin-permalink-settings.php' );
break;
case 'users' :
case 'user' :
case 'profile' :
case 'user-edit' :
2013-07-24 18:55:02 +00:00
include( 'class-wc-admin-profile.php' );
break;
}
}
2015-04-29 09:47:57 +00:00
/**
* Handle redirects to setup/welcome page
*/
public function admin_redirects() {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
if ( ! get_transient( '_wc_setup_redirect' ) && ! get_transient( '_wc_activation_redirect' ) ) {
return;
}
// Bail if activating from network, or bulk, or within an iFrame
if ( is_network_admin() || isset( $_GET['activate-multi'] ) || defined( 'IFRAME_REQUEST' ) ) {
return;
}
// Don't affect upgrade plugin screen
if ( isset( $_GET['action'] ) && 'upgrade-plugin' === $_GET['action'] ) {
return;
}
// Prevent loop
if ( ! empty( $_GET['page'] ) && ( $_GET['page'] === 'wc-about' || $_GET['page'] === 'wc-setup' ) ) {
return;
}
2015-04-29 09:59:59 +00:00
if ( apply_filters( 'woocommerce_enable_setup_wizard', true ) && get_transient( '_wc_setup_redirect' ) ) {
2015-04-29 09:47:57 +00:00
delete_transient( '_wc_setup_redirect' );
wp_redirect( admin_url( 'index.php?page=wc-setup' ) );
exit;
}
if ( ! WC_Admin_Notices::has_notice( 'update' ) && get_transient( '_wc_activation_redirect' ) ) {
delete_transient( '_wc_activation_redirect' );
wp_redirect( admin_url( 'index.php?page=wc-about' ) );
exit;
}
}
/**
* Prevent any user who cannot 'edit_posts' (subscribers, customers etc) from accessing admin
*/
public function prevent_admin_access() {
$prevent_access = false;
if ( 'yes' === get_option( 'woocommerce_lock_down_admin', 'yes' ) && ! is_ajax() && ! ( current_user_can( 'edit_posts' ) || current_user_can( 'manage_woocommerce' ) ) && basename( $_SERVER["SCRIPT_FILENAME"] ) !== 'admin-post.php' ) {
$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' ) );
exit;
}
}
/**
* Preview email template
2014-09-02 19:50:19 +00:00
*
* @return string
*/
public function preview_emails() {
if ( isset( $_GET['preview_woocommerce_mail'] ) ) {
2014-06-17 21:40:54 +00:00
if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'preview-mail') ) {
die( 'Security check' );
2014-06-17 21:40:54 +00:00
}
// load the mailer class
$mailer = WC()->mailer();
// get the preview email subject
$email_heading = __( 'HTML Email Template', 'woocommerce' );
// get the preview email content
ob_start();
include( 'views/html-email-template-preview.php' );
$message = ob_get_clean();
// 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;
exit;
}
}
/**
* Change the admin footer text on WooCommerce admin pages
*
* @since 2.3
* @param string $footer_text
* @return string
*/
public function admin_footer_text( $footer_text ) {
$current_screen = get_current_screen();
$wc_pages = wc_get_screen_ids();
// Set only wc pages
$wc_pages = array_flip( $wc_pages );
unset( $wc_pages['profile'] );
unset( $wc_pages['user-edit'] );
$wc_pages = array_flip( $wc_pages );
// Add the dashboard pages
$wc_pages[] = 'dashboard_page_wc-about';
$wc_pages[] = 'dashboard_page_wc-credits';
$wc_pages[] = 'dashboard_page_wc-translators';
// Check to make sure we're on a WooCommerce admin page
if ( isset( $current_screen->id ) && apply_filters( 'woocommerce_display_admin_footer_text', in_array( $current_screen->id, $wc_pages ) ) ) {
// Change the footer text
if ( ! get_option( 'woocommerce_admin_footer_text_rated' ) ) {
$footer_text = sprintf( __( 'If you like <strong>WooCommerce</strong> please leave us a %s&#9733;&#9733;&#9733;&#9733;&#9733;%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="' . __( 'Thanks :)', 'woocommerce' ) . '">', '</a>' );
wc_enqueue_js( "
jQuery('a.wc-rating-link').click(function() {
jQuery.post( '" . WC()->ajax_url() . "', { action: 'woocommerce_rated' } );
jQuery(this).parent().text( jQuery(this).data( 'rated' ) );
});
" );
} else {
$footer_text = __( 'Thank you for selling with WooCommerce.', 'woocommerce' );
}
}
return $footer_text;
}
}
2014-06-17 21:40:54 +00:00
return new WC_Admin();