PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/campus-academy/krowkaramel
PHP | 317 lines | 185 code | 46 blank | 86 comment | 32 complexity | 527dadab556a4148c79d4946225a5c3b MD5 | raw file
  1. <?php
  2. /**
  3. * WooCommerce Admin
  4. *
  5. * @class WC_Admin
  6. * @package WooCommerce\Admin
  7. * @version 2.6.0
  8. */
  9. if ( ! defined( 'ABSPATH' ) ) {
  10. exit; // Exit if accessed directly.
  11. }
  12. /**
  13. * WC_Admin class.
  14. */
  15. class WC_Admin {
  16. /**
  17. * Constructor.
  18. */
  19. public function __construct() {
  20. add_action( 'init', array( $this, 'includes' ) );
  21. add_action( 'current_screen', array( $this, 'conditional_includes' ) );
  22. add_action( 'admin_init', array( $this, 'buffer' ), 1 );
  23. add_action( 'admin_init', array( $this, 'preview_emails' ) );
  24. add_action( 'admin_init', array( $this, 'prevent_admin_access' ) );
  25. add_action( 'admin_init', array( $this, 'admin_redirects' ) );
  26. add_action( 'admin_footer', 'wc_print_js', 25 );
  27. add_filter( 'admin_footer_text', array( $this, 'admin_footer_text' ), 1 );
  28. add_action( 'init', array( 'WC_Site_Tracking', 'init' ) );
  29. // Disable WXR export of schedule action posts.
  30. add_filter( 'action_scheduler_post_type_args', array( $this, 'disable_webhook_post_export' ) );
  31. // Add body class for WP 5.3+ compatibility.
  32. add_filter( 'admin_body_class', array( $this, 'include_admin_body_class' ), 9999 );
  33. // Add body class for Marketplace and My Subscriptions pages.
  34. if ( isset( $_GET['page'] ) && 'wc-addons' === $_GET['page'] ) {
  35. add_filter( 'admin_body_class', array( 'WC_Admin_Addons', 'filter_admin_body_classes' ) );
  36. }
  37. }
  38. /**
  39. * Output buffering allows admin screens to make redirects later on.
  40. */
  41. public function buffer() {
  42. ob_start();
  43. }
  44. /**
  45. * Include any classes we need within admin.
  46. */
  47. public function includes() {
  48. include_once __DIR__ . '/wc-admin-functions.php';
  49. include_once __DIR__ . '/wc-meta-box-functions.php';
  50. include_once __DIR__ . '/class-wc-admin-post-types.php';
  51. include_once __DIR__ . '/class-wc-admin-taxonomies.php';
  52. include_once __DIR__ . '/class-wc-admin-menus.php';
  53. include_once __DIR__ . '/class-wc-admin-customize.php';
  54. include_once __DIR__ . '/class-wc-admin-notices.php';
  55. include_once __DIR__ . '/class-wc-admin-assets.php';
  56. include_once __DIR__ . '/class-wc-admin-api-keys.php';
  57. include_once __DIR__ . '/class-wc-admin-webhooks.php';
  58. include_once __DIR__ . '/class-wc-admin-pointers.php';
  59. include_once __DIR__ . '/class-wc-admin-importers.php';
  60. include_once __DIR__ . '/class-wc-admin-exporters.php';
  61. include_once WC_ABSPATH . 'includes/tracks/class-wc-tracks.php';
  62. include_once WC_ABSPATH . 'includes/tracks/class-wc-tracks-event.php';
  63. include_once WC_ABSPATH . 'includes/tracks/class-wc-tracks-client.php';
  64. include_once WC_ABSPATH . 'includes/tracks/class-wc-tracks-footer-pixel.php';
  65. include_once WC_ABSPATH . 'includes/tracks/class-wc-site-tracking.php';
  66. // Help Tabs.
  67. if ( apply_filters( 'woocommerce_enable_admin_help_tab', true ) ) {
  68. include_once __DIR__ . '/class-wc-admin-help.php';
  69. }
  70. // Helper.
  71. include_once __DIR__ . '/helper/class-wc-helper.php';
  72. // Marketplace suggestions & related REST API.
  73. include_once __DIR__ . '/marketplace-suggestions/class-wc-marketplace-suggestions.php';
  74. include_once __DIR__ . '/marketplace-suggestions/class-wc-marketplace-updater.php';
  75. }
  76. /**
  77. * Include admin files conditionally.
  78. */
  79. public function conditional_includes() {
  80. $screen = get_current_screen();
  81. if ( ! $screen ) {
  82. return;
  83. }
  84. switch ( $screen->id ) {
  85. case 'dashboard':
  86. case 'dashboard-network':
  87. include __DIR__ . '/class-wc-admin-dashboard-setup.php';
  88. include __DIR__ . '/class-wc-admin-dashboard.php';
  89. break;
  90. case 'options-permalink':
  91. include __DIR__ . '/class-wc-admin-permalink-settings.php';
  92. break;
  93. case 'plugins':
  94. include __DIR__ . '/plugin-updates/class-wc-plugins-screen-updates.php';
  95. break;
  96. case 'update-core':
  97. include __DIR__ . '/plugin-updates/class-wc-updates-screen-updates.php';
  98. break;
  99. case 'users':
  100. case 'user':
  101. case 'profile':
  102. case 'user-edit':
  103. include __DIR__ . '/class-wc-admin-profile.php';
  104. break;
  105. }
  106. }
  107. /**
  108. * Handle redirects to setup/welcome page after install and updates.
  109. *
  110. * The user must have access rights, and we must ignore the network/bulk plugin updaters.
  111. */
  112. public function admin_redirects() {
  113. // Don't run this fn from Action Scheduler requests, as it would clear _wc_activation_redirect transient.
  114. // That means OBW would never be shown.
  115. if ( wc_is_running_from_async_action_scheduler() ) {
  116. return;
  117. }
  118. // phpcs:disable WordPress.Security.NonceVerification.Recommended
  119. // Nonced plugin install redirects.
  120. if ( ! empty( $_GET['wc-install-plugin-redirect'] ) ) {
  121. $plugin_slug = wc_clean( wp_unslash( $_GET['wc-install-plugin-redirect'] ) );
  122. if ( current_user_can( 'install_plugins' ) && in_array( $plugin_slug, array( 'woocommerce-gateway-stripe' ), true ) ) {
  123. $nonce = wp_create_nonce( 'install-plugin_' . $plugin_slug );
  124. $url = self_admin_url( 'update.php?action=install-plugin&plugin=' . $plugin_slug . '&_wpnonce=' . $nonce );
  125. } else {
  126. $url = admin_url( 'plugin-install.php?tab=search&type=term&s=' . $plugin_slug );
  127. }
  128. wp_safe_redirect( $url );
  129. exit;
  130. }
  131. // phpcs:enable WordPress.Security.NonceVerification.Recommended
  132. }
  133. /**
  134. * Prevent any user who cannot 'edit_posts' (subscribers, customers etc) from accessing admin.
  135. */
  136. public function prevent_admin_access() {
  137. $prevent_access = false;
  138. if ( apply_filters( 'woocommerce_disable_admin_bar', true ) && ! wp_doing_ajax() && isset( $_SERVER['SCRIPT_FILENAME'] ) && basename( sanitize_text_field( wp_unslash( $_SERVER['SCRIPT_FILENAME'] ) ) ) !== 'admin-post.php' ) {
  139. $has_cap = false;
  140. $access_caps = array( 'edit_posts', 'manage_woocommerce', 'view_admin_dashboard' );
  141. foreach ( $access_caps as $access_cap ) {
  142. if ( current_user_can( $access_cap ) ) {
  143. $has_cap = true;
  144. break;
  145. }
  146. }
  147. if ( ! $has_cap ) {
  148. $prevent_access = true;
  149. }
  150. }
  151. if ( apply_filters( 'woocommerce_prevent_admin_access', $prevent_access ) ) {
  152. wp_safe_redirect( wc_get_page_permalink( 'myaccount' ) );
  153. exit;
  154. }
  155. }
  156. /**
  157. * Preview email template.
  158. */
  159. public function preview_emails() {
  160. if ( isset( $_GET['preview_woocommerce_mail'] ) ) {
  161. if ( ! ( isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'preview-mail' ) ) ) {
  162. die( 'Security check' );
  163. }
  164. // load the mailer class.
  165. $mailer = WC()->mailer();
  166. // get the preview email subject.
  167. $email_heading = __( 'HTML email template', 'woocommerce' );
  168. // get the preview email content.
  169. ob_start();
  170. include __DIR__ . '/views/html-email-template-preview.php';
  171. $message = ob_get_clean();
  172. // create a new email.
  173. $email = new WC_Email();
  174. // wrap the content with the email template and then add styles.
  175. $message = apply_filters( 'woocommerce_mail_content', $email->style_inline( $mailer->wrap_message( $email_heading, $message ) ) );
  176. // print the preview email.
  177. // phpcs:ignore WordPress.Security.EscapeOutput
  178. echo $message;
  179. // phpcs:enable
  180. exit;
  181. }
  182. }
  183. /**
  184. * Change the admin footer text on WooCommerce admin pages.
  185. *
  186. * @since 2.3
  187. * @param string $footer_text text to be rendered in the footer.
  188. * @return string
  189. */
  190. public function admin_footer_text( $footer_text ) {
  191. if ( ! current_user_can( 'manage_woocommerce' ) || ! function_exists( 'wc_get_screen_ids' ) ) {
  192. return $footer_text;
  193. }
  194. $current_screen = get_current_screen();
  195. $wc_pages = wc_get_screen_ids();
  196. // Set only WC pages.
  197. $wc_pages = array_diff( $wc_pages, array( 'profile', 'user-edit' ) );
  198. // Check to make sure we're on a WooCommerce admin page.
  199. if ( isset( $current_screen->id ) && apply_filters( 'woocommerce_display_admin_footer_text', in_array( $current_screen->id, $wc_pages, true ) ) ) {
  200. // Change the footer text.
  201. if ( ! get_option( 'woocommerce_admin_footer_text_rated' ) ) {
  202. $footer_text = sprintf(
  203. /* translators: 1: WooCommerce 2:: five stars */
  204. __( 'If you like %1$s please leave us a %2$s rating. A huge thanks in advance!', 'woocommerce' ),
  205. sprintf( '<strong>%s</strong>', esc_html__( 'WooCommerce', 'woocommerce' ) ),
  206. '<a href="https://wordpress.org/support/plugin/woocommerce/reviews?rate=5#new-post" target="_blank" class="wc-rating-link" aria-label="' . esc_attr__( 'five star', 'woocommerce' ) . '" data-rated="' . esc_attr__( 'Thanks :)', 'woocommerce' ) . '">&#9733;&#9733;&#9733;&#9733;&#9733;</a>'
  207. );
  208. wc_enqueue_js(
  209. "jQuery( 'a.wc-rating-link' ).on( 'click', function() {
  210. jQuery.post( '" . WC()->ajax_url() . "', { action: 'woocommerce_rated' } );
  211. jQuery( this ).parent().text( jQuery( this ).data( 'rated' ) );
  212. });"
  213. );
  214. } else {
  215. $footer_text = __( 'Thank you for selling with WooCommerce.', 'woocommerce' );
  216. }
  217. }
  218. return $footer_text;
  219. }
  220. /**
  221. * Check on a Jetpack install queued by the Setup Wizard.
  222. *
  223. * See: WC_Admin_Setup_Wizard::install_jetpack()
  224. */
  225. public function setup_wizard_check_jetpack() {
  226. $jetpack_active = class_exists( 'Jetpack' );
  227. wp_send_json_success(
  228. array(
  229. 'is_active' => $jetpack_active ? 'yes' : 'no',
  230. )
  231. );
  232. }
  233. /**
  234. * Disable WXR export of scheduled action posts.
  235. *
  236. * @since 3.6.2
  237. *
  238. * @param array $args Scehduled action post type registration args.
  239. *
  240. * @return array
  241. */
  242. public function disable_webhook_post_export( $args ) {
  243. $args['can_export'] = false;
  244. return $args;
  245. }
  246. /**
  247. * Include admin classes.
  248. *
  249. * @since 4.2.0
  250. * @param string $classes Body classes string.
  251. * @return string
  252. */
  253. public function include_admin_body_class( $classes ) {
  254. if ( in_array( array( 'wc-wp-version-gte-53', 'wc-wp-version-gte-55' ), explode( ' ', $classes ), true ) ) {
  255. return $classes;
  256. }
  257. $raw_version = get_bloginfo( 'version' );
  258. $version_parts = explode( '-', $raw_version );
  259. $version = count( $version_parts ) > 1 ? $version_parts[0] : $raw_version;
  260. // Add WP 5.3+ compatibility class.
  261. if ( $raw_version && version_compare( $version, '5.3', '>=' ) ) {
  262. $classes .= ' wc-wp-version-gte-53';
  263. }
  264. // Add WP 5.5+ compatibility class.
  265. if ( $raw_version && version_compare( $version, '5.5', '>=' ) ) {
  266. $classes .= ' wc-wp-version-gte-55';
  267. }
  268. return $classes;
  269. }
  270. }
  271. return new WC_Admin();