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

274 lines
8.8 KiB
PHP
Raw Normal View History

<?php
/**
2015-11-03 13:53:50 +00:00
* WooCommerce Admin
*
2016-05-20 21:02:57 +00:00
* @class WC_Admin
* @author WooThemes
* @category Admin
* @package WooCommerce/Admin
* @version 2.6.0
*/
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' ) );
2015-07-06 09:49:31 +00:00
add_action( 'current_screen', array( $this, 'conditional_includes' ) );
add_action( 'admin_init', array( $this, 'buffer' ), 1 );
add_action( 'admin_init', array( $this, 'preview_emails' ) );
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 );
add_filter( 'admin_footer_text', array( $this, 'admin_footer_text' ), 1 );
add_action( 'wp_ajax_setup_wizard_check_jetpack', array( $this, 'setup_wizard_check_jetpack' ) );
}
/**
* Output buffering allows admin screens to make redirects later on.
*/
public function buffer() {
ob_start();
}
/**
* Include any classes we need within admin.
*/
public function includes() {
include_once( dirname( __FILE__ ) . '/wc-admin-functions.php' );
include_once( dirname( __FILE__ ) . '/wc-meta-box-functions.php' );
include_once( dirname( __FILE__ ) . '/class-wc-admin-post-types.php' );
include_once( dirname( __FILE__ ) . '/class-wc-admin-taxonomies.php' );
include_once( dirname( __FILE__ ) . '/class-wc-admin-menus.php' );
include_once( dirname( __FILE__ ) . '/class-wc-admin-customize.php' );
include_once( dirname( __FILE__ ) . '/class-wc-admin-notices.php' );
include_once( dirname( __FILE__ ) . '/class-wc-admin-assets.php' );
include_once( dirname( __FILE__ ) . '/class-wc-admin-api-keys.php' );
include_once( dirname( __FILE__ ) . '/class-wc-admin-webhooks.php' );
include_once( dirname( __FILE__ ) . '/class-wc-admin-pointers.php' );
include_once( dirname( __FILE__ ) . '/class-wc-admin-importers.php' );
include_once( dirname( __FILE__ ) . '/class-wc-admin-exporters.php' );
2015-04-29 10:32:03 +00:00
// Help Tabs
if ( apply_filters( 'woocommerce_enable_admin_help_tab', true ) ) {
include_once( dirname( __FILE__ ) . '/class-wc-admin-help.php' );
2015-04-29 10:32:03 +00:00
}
2015-04-29 10:32:03 +00:00
// Setup/welcome
if ( ! empty( $_GET['page'] ) ) {
switch ( $_GET['page'] ) {
case 'wc-setup' :
include_once( dirname( __FILE__ ) . '/class-wc-admin-setup-wizard.php' );
break;
}
}
// Importers
if ( defined( 'WP_LOAD_IMPORTERS' ) ) {
include_once( dirname( __FILE__ ) . '/class-wc-admin-importers.php' );
}
// Helper
include_once( dirname( __FILE__ ) . '/helper/class-wc-helper-options.php' );
include_once( dirname( __FILE__ ) . '/helper/class-wc-helper-api.php' );
include_once( dirname( __FILE__ ) . '/helper/class-wc-helper-updater.php' );
include_once( dirname( __FILE__ ) . '/helper/class-wc-helper-plugin-info.php' );
Helper: Various Improvements (#15519) * Helper: Improve messaging and CTAs * Helper: Update copy on connect flow * Helper: Improve messaging around plugins without a subscription * removes toggle from extensions that cannot be activated * Helper: Add new `WC_Helper_Compat` class for legacy helper compat This new class removes some of the actions from the legacy helper (updater) plugin, attempts to migrate an existing legacy connection to a new one, attempts to deactivate the old helper plugin. * Helper: Improve messaging around maxed out subscriptions * Helper: Enable `WC_Tracker` when connected to WooCommerce.com * Helper: adds styles for manual licence form row and other visual improvements * Helper: removes disabled toggles * Helper: adds styles for connection component * Helper: redesigned connection module * Helper: rotates the connection module chevron on mouse hover * Helper: adds some fixes to the key form on mobile and other updates to meet coding standards * Helper: Only mark action as primary if there's a CTA button. * Helper: Better handling for woothemes_updater_notice Remove the updater notice built into extensions and themes, even if the legacy Helper plugin is deactivated. * Helper: Navigate existing Helper users to the new place. Adds a Dashboard menu item and direct users of the old helper plugin to the new Extensions screen. * Helper: Do not show the back-compat menu item for new users * Helper: Avoid notice in API authentication if credentials are missing * Helper: Hide the back-compat menu item when users see the new screen Also sets the user_id for the connection if one was not previously set.
2017-06-14 13:12:53 +00:00
include_once( dirname( __FILE__ ) . '/helper/class-wc-helper-compat.php' );
include_once( dirname( __FILE__ ) . '/helper/class-wc-helper.php' );
}
/**
* Include admin files conditionally.
*/
2015-07-06 09:49:31 +00:00
public function conditional_includes() {
if ( ! $screen = get_current_screen() ) {
return;
}
switch ( $screen->id ) {
case 'dashboard' :
case 'dashboard-network' :
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;
2017-07-11 14:52:40 +00:00
case 'plugins' :
include ( 'plugin-updates/class-wc-plugins-screen-updates.php' );
break;
2017-08-01 22:04:11 +00:00
case 'update-core' :
include( 'plugin-updates/class-wc-updates-screen-updates.php' );
2017-07-11 14:52:40 +00:00
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
/**
2015-04-29 10:24:29 +00:00
* Handle redirects to setup/welcome page after install and updates.
*
* For setup wizard, 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() {
// Nonced plugin install redirects (whitelisted)
if ( ! empty( $_GET['wc-install-plugin-redirect'] ) ) {
$plugin_slug = wc_clean( $_GET['wc-install-plugin-redirect'] );
2015-04-29 09:47:57 +00:00
if ( current_user_can( 'install_plugins' ) && in_array( $plugin_slug, array( 'woocommerce-gateway-stripe' ) ) ) {
$nonce = wp_create_nonce( 'install-plugin_' . $plugin_slug );
$url = self_admin_url( 'update.php?action=install-plugin&plugin=' . $plugin_slug . '&_wpnonce=' . $nonce );
} else {
$url = admin_url( 'plugin-install.php?tab=search&type=term&s=' . $plugin_slug );
}
wp_safe_redirect( $url );
exit;
}
2015-04-29 09:47:57 +00:00
// Setup wizard redirect
if ( get_transient( '_wc_activation_redirect' ) ) {
delete_transient( '_wc_activation_redirect' );
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 ) ) {
return;
}
// If the user needs to install, send them to the setup wizard
if ( WC_Admin_Notices::has_notice( 'install' ) ) {
wp_safe_redirect( admin_url( 'index.php?page=wc-setup' ) );
exit;
}
2015-04-29 09:47:57 +00:00
}
}
/**
* 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() && basename( $_SERVER["SCRIPT_FILENAME"] ) !== 'admin-post.php' ) {
$has_cap = false;
$access_caps = array( 'edit_posts', 'manage_woocommerce', 'view_admin_dashboard' );
foreach ( $access_caps as $access_cap ) {
if ( current_user_can( $access_cap ) ) {
$has_cap = true;
break;
}
}
if ( ! $has_cap ) {
$prevent_access = true;
}
}
if ( apply_filters( 'woocommerce_prevent_admin_access', $prevent_access ) ) {
2015-02-15 19:13:22 +00:00
wp_safe_redirect( wc_get_page_permalink( 'myaccount' ) );
exit;
}
}
/**
* Preview email template.
*/
public function preview_emails() {
if ( isset( $_GET['preview_woocommerce_mail'] ) ) {
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 = apply_filters( 'woocommerce_mail_content', $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 ) {
if ( ! current_user_can( 'manage_woocommerce' ) || ! function_exists( 'wc_get_screen_ids' ) ) {
return $footer_text;
}
$current_screen = get_current_screen();
$wc_pages = wc_get_screen_ids();
// Set only WC pages.
$wc_pages = array_diff( $wc_pages, array( 'profile', 'user-edit' ) );
// 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(
/* translators: 1: WooCommerce 2:: five stars */
__( 'If you like %1$s please leave us a %2$s rating. A huge thanks in advance!', 'woocommerce' ),
2017-10-04 09:48:22 +00:00
sprintf( '<strong>%s</strong>', esc_html__( 'WooCommerce', 'woocommerce' ) ),
'<a href="https://wordpress.org/support/plugin/woocommerce/reviews?rate=5#new-post" target="_blank" class="wc-rating-link" data-rated="' . esc_attr__( 'Thanks :)', 'woocommerce' ) . '">&#9733;&#9733;&#9733;&#9733;&#9733;</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;
}
/**
* Check on a Jetpack install queued by the Setup Wizard.
*
* See: WC_Admin_Setup_Wizard::install_jetpack()
*/
public function setup_wizard_check_jetpack() {
$jetpack_active = class_exists( 'Jetpack' );
wp_send_json_success( array(
'is_active' => $jetpack_active ? 'yes' : 'no',
) );
}
}
2014-06-17 21:40:54 +00:00
return new WC_Admin();