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