PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/admin/class-wc-admin.php

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