WC_Admin

WC_Admin class.

Defined (1)

The class is defined in the following location(s).

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