1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
<?php /** * WooCommerce Admin * * @class WC_Admin * @package WooCommerce\Admin * @version 2.6.0 */
declare(strict_types=1);
use Automattic\WooCommerce\Admin\PageController; use Automattic\WooCommerce\Internal\Admin\EmailPreview\EmailPreview;
if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. }
/** * WC_Admin class. */ class WC_Admin {
/** * Constructor. */ public function __construct() { add_action( 'init', array( $this, 'includes' ) );
// Hook in early (priority 1) to make sure the PageController's hooks are added before any WC admin pages or // menus logic is run, including the enqueuing of assets via \Automattic\WooCommerce\Internal\Admin\WCAdminAssets. // While it may not sound like it, the admin_menu action is triggered quite early, // before the admin_init or admin_enqueue_scripts action. // @see https://developer.wordpress.org/apis/hooks/action-reference/#actions-run-during-an-admin-page-request. add_action( 'admin_menu', array( $this, 'init_page_controller' ), 1 );
add_action( 'current_screen', array( $this, 'conditional_includes' ) ); add_action( 'admin_init', array( $this, 'buffer' ), 1 ); add_action( 'admin_init', array( $this, 'preview_emails' ) ); add_action( 'admin_init', array( $this, 'preview_email_editor_dummy_content' ) ); add_action( 'admin_init', array( $this, 'prevent_admin_access' ) ); add_action( 'admin_init', array( $this, 'admin_redirects' ) ); add_action( 'admin_footer', 'wc_print_js', 25 ); add_filter( 'admin_footer_text', array( $this, 'admin_footer_text' ), 1 ); add_filter( 'update_footer', array( $this, 'update_footer_version' ), 20 );
// Disable WXR export of schedule action posts. add_filter( 'action_scheduler_post_type_args', array( $this, 'disable_webhook_post_export' ) );
// Add body class for WP 5.3+ compatibility. add_filter( 'admin_body_class', array( $this, 'include_admin_body_class' ), 9999 );
// Add body class for Marketplace and My Subscriptions pages. if ( isset( $_GET['page'] ) && 'wc-addons' === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended add_filter( 'admin_body_class', array( 'WC_Admin_Addons', 'filter_admin_body_classes' ) ); } }
/** * Output buffering allows admin screens to make redirects later on. */ public function buffer() { ob_start(); }
/** * Include any classes we need within admin. */ public function includes() { include_once __DIR__ . '/wc-admin-functions.php'; include_once __DIR__ . '/wc-meta-box-functions.php'; include_once __DIR__ . '/class-wc-admin-post-types.php'; include_once __DIR__ . '/class-wc-admin-taxonomies.php'; include_once __DIR__ . '/class-wc-admin-menus.php'; include_once __DIR__ . '/class-wc-admin-customize.php'; include_once __DIR__ . '/class-wc-admin-notices.php'; include_once __DIR__ . '/class-wc-admin-assets.php'; include_once __DIR__ . '/class-wc-admin-api-keys.php'; include_once __DIR__ . '/class-wc-admin-webhooks.php'; include_once __DIR__ . '/class-wc-admin-pointers.php'; include_once __DIR__ . '/class-wc-admin-importers.php'; include_once __DIR__ . '/class-wc-admin-exporters.php';
// Help Tabs. /** * Filter to enable/disable admin help tab. * * @since 3.6.0 */ if ( apply_filters( 'woocommerce_enable_admin_help_tab', true ) ) { include_once __DIR__ . '/class-wc-admin-help.php'; }
// Helper. include_once __DIR__ . '/helper/class-wc-helper.php';
// Marketplace suggestions & related REST API. include_once __DIR__ . '/marketplace-suggestions/class-wc-marketplace-suggestions.php'; include_once __DIR__ . '/marketplace-suggestions/class-wc-marketplace-updater.php'; }
/** * Initialize the admin page controller logic. */ public function init_page_controller() { // We only need to make sure the controller is instantiated since the hooking is done in the constructor. PageController::get_instance(); }
/** * Include admin files conditionally. */ public function conditional_includes() { $screen = get_current_screen();
if ( ! $screen ) { return; }
switch ( $screen->id ) { case 'dashboard': case 'dashboard-network': include __DIR__ . '/class-wc-admin-dashboard-setup.php'; include __DIR__ . '/class-wc-admin-dashboard.php'; break; case 'options-permalink': include __DIR__ . '/class-wc-admin-permalink-settings.php'; break; case 'plugins': include __DIR__ . '/plugin-updates/class-wc-plugins-screen-updates.php'; break; case 'update-core': include __DIR__ . '/plugin-updates/class-wc-updates-screen-updates.php'; break; case 'users': case 'user': case 'profile': case 'user-edit': include __DIR__ . '/class-wc-admin-profile.php'; break; } }
/** * Handle redirects: * 1. Nonced plugin install redirects. * * The user must have access rights, and we must ignore the network/bulk plugin updaters. */ public function admin_redirects() { // Don't run this fn from Action Scheduler requests. if ( wc_is_running_from_async_action_scheduler() ) { return; }
// phpcs:disable WordPress.Security.NonceVerification.Recommended // Nonced plugin install redirects. if ( ! empty( $_GET['wc-install-plugin-redirect'] ) ) { $plugin_slug = wc_clean( wp_unslash( $_GET['wc-install-plugin-redirect'] ) );
if ( current_user_can( 'install_plugins' ) && in_array( $plugin_slug, array( 'woocommerce-gateway-stripe' ), true ) ) { $nonce = wp_create_nonce( 'install-plugin_' . $plugin_slug ); $url = self_admin_url( 'update.php?action=install-plugin&plugin=' . $plugin_slug . '&_wpnonce=' . $nonce ); } else { $url = admin_url( 'plugin-install.php?tab=search&type=term&s=' . $plugin_slug ); }
wp_safe_redirect( $url ); exit; } // phpcs:enable WordPress.Security.NonceVerification.Recommended }
/** * Prevent any user who cannot 'edit_posts' (subscribers, customers etc) from accessing admin. */ public function prevent_admin_access() { $prevent_access = false;
// Do not interfere with admin-post or admin-ajax requests. $exempted_paths = array( 'admin-post.php', 'admin-ajax.php' );
if ( /** * This filter is documented in ../wc-user-functions.php * * @since 3.6.0 */ apply_filters( 'woocommerce_disable_admin_bar', true ) && isset( $_SERVER['SCRIPT_FILENAME'] ) && ! in_array( basename( sanitize_text_field( wp_unslash( $_SERVER['SCRIPT_FILENAME'] ) ) ), $exempted_paths, true ) ) { $has_cap = false; $access_caps = array( 'edit_posts', 'manage_woocommerce', 'view_admin_dashboard' );
foreach ( $access_caps as $access_cap ) { if ( current_user_can( $access_cap ) ) { $has_cap = true; break; } }
if ( ! $has_cap ) { $prevent_access = true; } }
/** * Filter to prevent admin access. * * @since 3.6.0 */ if ( apply_filters( 'woocommerce_prevent_admin_access', $prevent_access ) ) { wp_safe_redirect( wc_get_page_permalink( 'myaccount' ) ); exit; } }
/** * Preview email template. */ public function preview_emails() {
if ( isset( $_GET['preview_woocommerce_mail'] ) ) { if ( ! ( isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'preview-mail' ) ) ) { die( 'Security check' ); }
$email_preview = wc_get_container()->get( EmailPreview::class );
if ( isset( $_GET['type'] ) ) { $type_param = sanitize_text_field( wp_unslash( $_GET['type'] ) ); try { $email_preview->set_email_type( $type_param ); } catch ( InvalidArgumentException $e ) { wp_die( esc_html__( 'Invalid email type.', 'woocommerce' ), 400 ); } }
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { $message = $email_preview->render(); $message = $email_preview->ensure_links_open_in_new_tab( $message ); } else { // Start output buffering to prevent partial renders with PHP notices or warnings. ob_start(); try { $message = $email_preview->render(); $message = $email_preview->ensure_links_open_in_new_tab( $message ); } catch ( Throwable $e ) { ob_end_clean(); wp_die( esc_html__( 'There was an error rendering the email preview. This doesn\'t affect actual email delivery. Please contact the extension author for assistance.', 'woocommerce' ), 404 ); } ob_end_clean(); }
// print the preview email. // phpcs:ignore WordPress.Security.EscapeOutput echo $message; // phpcs:enable exit; } }
/** * Preview email editor placeholder dummy content. */ public function preview_email_editor_dummy_content() { $message = ''; if ( ! isset( $_GET['preview_woocommerce_mail_editor_content'] ) ) { return; }
if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'preview-mail' ) ) { die( 'Security check' ); }
/** * Email preview instance for rendering dummy content. * * @var EmailPreview $email_preview - email preview instance */ $email_preview = wc_get_container()->get( EmailPreview::class );
$type_param = EmailPreview::DEFAULT_EMAIL_TYPE; if ( isset( $_GET['type'] ) ) { $type_param = sanitize_text_field( wp_unslash( $_GET['type'] ) ); }
try { $message = $email_preview->generate_placeholder_content( $type_param ); } catch ( \Exception $e ) { // Catch other potential errors during content generation. wp_die( esc_html__( 'There was an error rendering the email preview.', 'woocommerce' ), 404 ); }
// Print the placeholder content. // phpcs:ignore WordPress.Security.EscapeOutput echo $message; exit; }
/** * Change the admin footer text on WooCommerce admin pages. * * @since 2.3 * * @param string $footer_text Footer text to be rendered. * @return string */ public function admin_footer_text( $footer_text ) { if ( ! current_user_can( 'manage_woocommerce' ) || ! function_exists( 'wc_get_screen_ids' ) ) { return $footer_text; } $current_screen = get_current_screen(); $wc_pages = array_merge( wc_get_screen_ids(), array( 'woocommerce_page_wc-admin' ) );
// Set only WC pages. $wc_pages = array_diff( $wc_pages, array( 'profile', 'user-edit' ) );
/** * Filter to determine if admin footer text should be displayed. * * @since 2.3 */ if ( isset( $current_screen->id ) && apply_filters( 'woocommerce_display_admin_footer_text', in_array( $current_screen->id, $wc_pages, true ) ) ) { // Change the footer text. if ( ! get_option( 'woocommerce_admin_footer_text_rated' ) ) { $footer_text = sprintf( /* translators: 1: WooCommerce 2:: five stars */ __( 'If you like %1$s please leave us a %2$s rating. A huge thanks in advance!', 'woocommerce' ), sprintf( '<strong>%s</strong>', esc_html__( 'WooCommerce', 'woocommerce' ) ), '<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>' ); wc_enqueue_js( "jQuery( 'a.wc-rating-link' ).on( 'click', function() { jQuery.post( '" . WC()->ajax_url() . "', { action: 'woocommerce_rated' } ); jQuery( this ).parent().text( jQuery( this ).data( 'rated' ) ); });" ); } else { $footer_text = __( 'Thank you for selling with WooCommerce.', 'woocommerce' ); } }
return '<span id="footer-thankyou">' . $footer_text . '</span>'; }
/** * Update the footer version text. * * @since $VID:$ * * @param string $version The current version string. * @return string */ public function update_footer_version( $version ) { if ( ! function_exists( 'wc_get_screen_ids' ) ) { return $version; } $current_screen = get_current_screen(); $wc_pages = array_merge( wc_get_screen_ids(), array( 'woocommerce_page_wc-admin' ) );
// Set only WC pages. $wc_pages = array_diff( $wc_pages, array( 'profile', 'user-edit' ) );
// Check to make sure we're on a WooCommerce admin page. /** * Filter to determine if update footer text should be displayed. * * @since 2.3 */ if ( isset( $current_screen->id ) && apply_filters( 'woocommerce_display_update_footer_text', in_array( $current_screen->id, $wc_pages, true ) ) ) { // Replace WordPress version with WooCommerce version. $version = sprintf( /* translators: %s: WooCommerce version */ __( 'Version %s', 'woocommerce' ), esc_html( WC()->version ) ); }
return $version; }
/** * Check on a Jetpack install queued by the Setup Wizard. * * See: WC_Admin_Setup_Wizard::install_jetpack() */ public function setup_wizard_check_jetpack() { $jetpack_active = class_exists( 'Jetpack' );
wp_send_json_success( array( 'is_active' => $jetpack_active ? 'yes' : 'no', ) ); }
/** * Disable WXR export of scheduled action posts. * * @since 3.6.2 * * @param array $args Scheduled action post type registration args. * * @return array */ public function disable_webhook_post_export( $args ) { $args['can_export'] = false; return $args; }
/** * Include admin classes. * * @since 4.2.0 * @param string $classes Body classes string. * @return string */ public function include_admin_body_class( $classes ) { if ( in_array( array( 'wc-wp-version-gte-53', 'wc-wp-version-gte-55' ), explode( ' ', $classes ), true ) ) { return $classes; }
$raw_version = get_bloginfo( 'version' ); $version_parts = explode( '-', $raw_version ); $version = count( $version_parts ) > 1 ? $version_parts[0] : $raw_version;
// Add WP 5.3+ compatibility class. if ( $raw_version && version_compare( $version, '5.3', '>=' ) ) { $classes .= ' wc-wp-version-gte-53'; }
// Add WP 5.5+ compatibility class. if ( $raw_version && version_compare( $version, '5.5', '>=' ) ) { $classes .= ' wc-wp-version-gte-55'; }
return $classes; } }
return new WC_Admin();
|