class-wc-admin.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * WooCommerce Admin
  4. *
  5. * @class WC_Admin
  6. * @author WooThemes
  7. * @category Admin
  8. * @package WooCommerce/Admin
  9. * @version 2.6.0
  10. */
  11. if ( ! defined( 'ABSPATH' ) ) {
  12. exit; // Exit if accessed directly
  13. }
  14. /**
  15. * WC_Admin class.
  16. */
  17. class WC_Admin {
  18. /**
  19. * Constructor.
  20. */
  21. public function __construct() {
  22. add_action( 'init', array( $this, 'includes' ) );
  23. add_action( 'current_screen', array( $this, 'conditional_includes' ) );
  24. add_action( 'admin_init', array( $this, 'buffer' ), 1 );
  25. add_action( 'admin_init', array( $this, 'preview_emails' ) );
  26. add_action( 'admin_init', array( $this, 'prevent_admin_access' ) );
  27. add_action( 'admin_init', array( $this, 'admin_redirects' ) );
  28. add_action( 'admin_footer', 'wc_print_js', 25 );
  29. add_filter( 'admin_footer_text', array( $this, 'admin_footer_text' ), 1 );
  30. add_action( 'wp_ajax_setup_wizard_check_jetpack', array( $this, 'setup_wizard_check_jetpack' ) );
  31. }
  32. /**
  33. * Output buffering allows admin screens to make redirects later on.
  34. */
  35. public function buffer() {
  36. ob_start();
  37. }
  38. /**
  39. * Include any classes we need within admin.
  40. */
  41. public function includes() {
  42. include_once dirname( __FILE__ ) . '/wc-admin-functions.php';
  43. include_once dirname( __FILE__ ) . '/wc-meta-box-functions.php';
  44. include_once dirname( __FILE__ ) . '/class-wc-admin-post-types.php';
  45. include_once dirname( __FILE__ ) . '/class-wc-admin-taxonomies.php';
  46. include_once dirname( __FILE__ ) . '/class-wc-admin-menus.php';
  47. include_once dirname( __FILE__ ) . '/class-wc-admin-customize.php';
  48. include_once dirname( __FILE__ ) . '/class-wc-admin-notices.php';
  49. include_once dirname( __FILE__ ) . '/class-wc-admin-assets.php';
  50. include_once dirname( __FILE__ ) . '/class-wc-admin-api-keys.php';
  51. include_once dirname( __FILE__ ) . '/class-wc-admin-webhooks.php';
  52. include_once dirname( __FILE__ ) . '/class-wc-admin-pointers.php';
  53. include_once dirname( __FILE__ ) . '/class-wc-admin-importers.php';
  54. include_once dirname( __FILE__ ) . '/class-wc-admin-exporters.php';
  55. // Help Tabs
  56. if ( apply_filters( 'woocommerce_enable_admin_help_tab', true ) ) {
  57. include_once dirname( __FILE__ ) . '/class-wc-admin-help.php';
  58. }
  59. // Setup/welcome
  60. if ( ! empty( $_GET['page'] ) ) {
  61. switch ( $_GET['page'] ) {
  62. case 'wc-setup':
  63. include_once dirname( __FILE__ ) . '/class-wc-admin-setup-wizard.php';
  64. break;
  65. }
  66. }
  67. // Importers
  68. if ( defined( 'WP_LOAD_IMPORTERS' ) ) {
  69. include_once dirname( __FILE__ ) . '/class-wc-admin-importers.php';
  70. }
  71. // Helper
  72. include_once dirname( __FILE__ ) . '/helper/class-wc-helper-options.php';
  73. include_once dirname( __FILE__ ) . '/helper/class-wc-helper-api.php';
  74. include_once dirname( __FILE__ ) . '/helper/class-wc-helper-updater.php';
  75. include_once dirname( __FILE__ ) . '/helper/class-wc-helper-plugin-info.php';
  76. include_once dirname( __FILE__ ) . '/helper/class-wc-helper-compat.php';
  77. include_once dirname( __FILE__ ) . '/helper/class-wc-helper.php';
  78. }
  79. /**
  80. * Include admin files conditionally.
  81. */
  82. public function conditional_includes() {
  83. if ( ! $screen = get_current_screen() ) {
  84. return;
  85. }
  86. switch ( $screen->id ) {
  87. case 'dashboard':
  88. case 'dashboard-network':
  89. include 'class-wc-admin-dashboard.php';
  90. break;
  91. case 'options-permalink':
  92. include 'class-wc-admin-permalink-settings.php';
  93. break;
  94. case 'plugins':
  95. include 'plugin-updates/class-wc-plugins-screen-updates.php';
  96. break;
  97. case 'update-core':
  98. include 'plugin-updates/class-wc-updates-screen-updates.php';
  99. break;
  100. case 'users':
  101. case 'user':
  102. case 'profile':
  103. case 'user-edit':
  104. include 'class-wc-admin-profile.php';
  105. break;
  106. }
  107. }
  108. /**
  109. * Handle redirects to setup/welcome page after install and updates.
  110. *
  111. * For setup wizard, transient must be present, the user must have access rights, and we must ignore the network/bulk plugin updaters.
  112. */
  113. public function admin_redirects() {
  114. // Nonced plugin install redirects (whitelisted)
  115. if ( ! empty( $_GET['wc-install-plugin-redirect'] ) ) {
  116. $plugin_slug = wc_clean( $_GET['wc-install-plugin-redirect'] );
  117. if ( current_user_can( 'install_plugins' ) && in_array( $plugin_slug, array( 'woocommerce-gateway-stripe' ) ) ) {
  118. $nonce = wp_create_nonce( 'install-plugin_' . $plugin_slug );
  119. $url = self_admin_url( 'update.php?action=install-plugin&plugin=' . $plugin_slug . '&_wpnonce=' . $nonce );
  120. } else {
  121. $url = admin_url( 'plugin-install.php?tab=search&type=term&s=' . $plugin_slug );
  122. }
  123. wp_safe_redirect( $url );
  124. exit;
  125. }
  126. // Setup wizard redirect
  127. if ( get_transient( '_wc_activation_redirect' ) ) {
  128. delete_transient( '_wc_activation_redirect' );
  129. 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 ) ) {
  130. return;
  131. }
  132. // If the user needs to install, send them to the setup wizard
  133. if ( WC_Admin_Notices::has_notice( 'install' ) ) {
  134. wp_safe_redirect( admin_url( 'index.php?page=wc-setup' ) );
  135. exit;
  136. }
  137. }
  138. }
  139. /**
  140. * Prevent any user who cannot 'edit_posts' (subscribers, customers etc) from accessing admin.
  141. */
  142. public function prevent_admin_access() {
  143. $prevent_access = false;
  144. if ( 'yes' === get_option( 'woocommerce_lock_down_admin', 'yes' ) && ! is_ajax() && basename( $_SERVER['SCRIPT_FILENAME'] ) !== 'admin-post.php' ) {
  145. $has_cap = false;
  146. $access_caps = array( 'edit_posts', 'manage_woocommerce', 'view_admin_dashboard' );
  147. foreach ( $access_caps as $access_cap ) {
  148. if ( current_user_can( $access_cap ) ) {
  149. $has_cap = true;
  150. break;
  151. }
  152. }
  153. if ( ! $has_cap ) {
  154. $prevent_access = true;
  155. }
  156. }
  157. if ( apply_filters( 'woocommerce_prevent_admin_access', $prevent_access ) ) {
  158. wp_safe_redirect( wc_get_page_permalink( 'myaccount' ) );
  159. exit;
  160. }
  161. }
  162. /**
  163. * Preview email template.
  164. */
  165. public function preview_emails() {
  166. if ( isset( $_GET['preview_woocommerce_mail'] ) ) {
  167. if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'preview-mail' ) ) {
  168. die( 'Security check' );
  169. }
  170. // load the mailer class
  171. $mailer = WC()->mailer();
  172. // get the preview email subject
  173. $email_heading = __( 'HTML email template', 'woocommerce' );
  174. // get the preview email content
  175. ob_start();
  176. include 'views/html-email-template-preview.php';
  177. $message = ob_get_clean();
  178. // create a new email
  179. $email = new WC_Email();
  180. // wrap the content with the email template and then add styles
  181. $message = apply_filters( 'woocommerce_mail_content', $email->style_inline( $mailer->wrap_message( $email_heading, $message ) ) );
  182. // print the preview email
  183. echo $message;
  184. exit;
  185. }
  186. }
  187. /**
  188. * Change the admin footer text on WooCommerce admin pages.
  189. *
  190. * @since 2.3
  191. * @param string $footer_text
  192. * @return string
  193. */
  194. public function admin_footer_text( $footer_text ) {
  195. if ( ! current_user_can( 'manage_woocommerce' ) || ! function_exists( 'wc_get_screen_ids' ) ) {
  196. return $footer_text;
  197. }
  198. $current_screen = get_current_screen();
  199. $wc_pages = wc_get_screen_ids();
  200. // Set only WC pages.
  201. $wc_pages = array_diff( $wc_pages, array( 'profile', 'user-edit' ) );
  202. // Check to make sure we're on a WooCommerce admin page.
  203. if ( isset( $current_screen->id ) && apply_filters( 'woocommerce_display_admin_footer_text', in_array( $current_screen->id, $wc_pages ) ) ) {
  204. // Change the footer text
  205. if ( ! get_option( 'woocommerce_admin_footer_text_rated' ) ) {
  206. $footer_text = sprintf(
  207. /* translators: 1: WooCommerce 2:: five stars */
  208. __( 'If you like %1$s please leave us a %2$s rating. A huge thanks in advance!', 'woocommerce' ),
  209. sprintf( '<strong>%s</strong>', esc_html__( 'WooCommerce', 'woocommerce' ) ),
  210. '<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>'
  211. );
  212. wc_enqueue_js(
  213. "jQuery( 'a.wc-rating-link' ).click( function() {
  214. jQuery.post( '" . WC()->ajax_url() . "', { action: 'woocommerce_rated' } );
  215. jQuery( this ).parent().text( jQuery( this ).data( 'rated' ) );
  216. });"
  217. );
  218. } else {
  219. $footer_text = __( 'Thank you for selling with WooCommerce.', 'woocommerce' );
  220. }
  221. }
  222. return $footer_text;
  223. }
  224. /**
  225. * Check on a Jetpack install queued by the Setup Wizard.
  226. *
  227. * See: WC_Admin_Setup_Wizard::install_jetpack()
  228. */
  229. public function setup_wizard_check_jetpack() {
  230. $jetpack_active = class_exists( 'Jetpack' );
  231. wp_send_json_success(
  232. array(
  233. 'is_active' => $jetpack_active ? 'yes' : 'no',
  234. )
  235. );
  236. }
  237. }
  238. return new WC_Admin();