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
|
|
|
add_action( 'wp_ajax_setup_wizard_check_jetpack', array( $this, 'setup_wizard_check_jetpack' ) ); |
32
|
|
|
add_action( 'init', array( 'WC_Site_Tracking', 'init' ) ); |
33
|
|
|
|
34
|
|
|
// Disable WXR export of schedule action posts. |
35
|
|
|
add_filter( 'action_scheduler_post_type_args', array( $this, 'disable_webhook_post_export' ) ); |
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
|
|
|
/** |
46
|
|
|
* Include any classes we need within admin. |
47
|
|
|
*/ |
48
|
|
|
public function includes() { |
49
|
|
|
include_once dirname( __FILE__ ) . '/wc-admin-functions.php'; |
50
|
|
|
include_once dirname( __FILE__ ) . '/wc-meta-box-functions.php'; |
51
|
|
|
include_once dirname( __FILE__ ) . '/class-wc-admin-post-types.php'; |
52
|
|
|
include_once dirname( __FILE__ ) . '/class-wc-admin-taxonomies.php'; |
53
|
|
|
include_once dirname( __FILE__ ) . '/class-wc-admin-menus.php'; |
54
|
|
|
include_once dirname( __FILE__ ) . '/class-wc-admin-customize.php'; |
55
|
|
|
include_once dirname( __FILE__ ) . '/class-wc-admin-notices.php'; |
56
|
|
|
include_once dirname( __FILE__ ) . '/class-wc-admin-assets.php'; |
57
|
|
|
include_once dirname( __FILE__ ) . '/class-wc-admin-api-keys.php'; |
58
|
|
|
include_once dirname( __FILE__ ) . '/class-wc-admin-webhooks.php'; |
59
|
|
|
include_once dirname( __FILE__ ) . '/class-wc-admin-pointers.php'; |
60
|
|
|
include_once dirname( __FILE__ ) . '/class-wc-admin-importers.php'; |
61
|
|
|
include_once dirname( __FILE__ ) . '/class-wc-admin-exporters.php'; |
62
|
|
|
|
63
|
|
|
include_once WC_ABSPATH . 'includes/tracks/class-wc-tracks.php'; |
64
|
|
|
include_once WC_ABSPATH . 'includes/tracks/class-wc-tracks-event.php'; |
65
|
|
|
include_once WC_ABSPATH . 'includes/tracks/class-wc-tracks-client.php'; |
66
|
|
|
include_once WC_ABSPATH . 'includes/tracks/class-wc-tracks-footer-pixel.php'; |
67
|
|
|
include_once WC_ABSPATH . 'includes/tracks/class-wc-site-tracking.php'; |
68
|
|
|
|
69
|
|
|
// Help Tabs. |
70
|
|
|
if ( apply_filters( 'woocommerce_enable_admin_help_tab', true ) ) { |
71
|
|
|
include_once dirname( __FILE__ ) . '/class-wc-admin-help.php'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Setup/welcome. |
75
|
|
|
if ( ! empty( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification |
76
|
|
|
switch ( $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification |
|
|
|
|
77
|
|
|
case 'wc-setup': |
78
|
|
|
include_once dirname( __FILE__ ) . '/class-wc-admin-setup-wizard.php'; |
79
|
|
|
break; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Importers. |
84
|
|
|
if ( defined( 'WP_LOAD_IMPORTERS' ) ) { |
85
|
|
|
include_once dirname( __FILE__ ) . '/class-wc-admin-importers.php'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Helper. |
89
|
|
|
include_once dirname( __FILE__ ) . '/helper/class-wc-helper.php'; |
90
|
|
|
|
91
|
|
|
// Marketplace suggestions & related REST API. |
92
|
|
|
include_once dirname( __FILE__ ) . '/marketplace-suggestions/class-wc-marketplace-suggestions.php'; |
93
|
|
|
include_once dirname( __FILE__ ) . '/marketplace-suggestions/class-wc-marketplace-updater.php'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Include admin files conditionally. |
98
|
|
|
*/ |
99
|
|
|
public function conditional_includes() { |
100
|
|
|
$screen = get_current_screen(); |
101
|
|
|
|
102
|
|
|
if ( ! $screen ) { |
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
switch ( $screen->id ) { |
107
|
|
|
case 'dashboard': |
108
|
|
|
case 'dashboard-network': |
109
|
|
|
include 'class-wc-admin-dashboard.php'; |
110
|
|
|
break; |
111
|
|
|
case 'options-permalink': |
112
|
|
|
include 'class-wc-admin-permalink-settings.php'; |
113
|
|
|
break; |
114
|
|
|
case 'plugins': |
115
|
|
|
include 'plugin-updates/class-wc-plugins-screen-updates.php'; |
116
|
|
|
break; |
117
|
|
|
case 'update-core': |
118
|
|
|
include 'plugin-updates/class-wc-updates-screen-updates.php'; |
119
|
|
|
break; |
120
|
|
|
case 'users': |
121
|
|
|
case 'user': |
122
|
|
|
case 'profile': |
123
|
|
|
case 'user-edit': |
124
|
|
|
include 'class-wc-admin-profile.php'; |
125
|
|
|
break; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Handle redirects to setup/welcome page after install and updates. |
131
|
|
|
* |
132
|
|
|
* For setup wizard, transient must be present, the user must have access rights, and we must ignore the network/bulk plugin updaters. |
133
|
|
|
*/ |
134
|
|
|
public function admin_redirects() { |
135
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.NoNonceVerification |
136
|
|
|
// Nonced plugin install redirects (whitelisted). |
137
|
|
|
if ( ! empty( $_GET['wc-install-plugin-redirect'] ) ) { |
138
|
|
|
$plugin_slug = wc_clean( wp_unslash( $_GET['wc-install-plugin-redirect'] ) ); |
|
|
|
|
139
|
|
|
|
140
|
|
|
if ( current_user_can( 'install_plugins' ) && in_array( $plugin_slug, array( 'woocommerce-gateway-stripe' ), true ) ) { |
141
|
|
|
$nonce = wp_create_nonce( 'install-plugin_' . $plugin_slug ); |
142
|
|
|
$url = self_admin_url( 'update.php?action=install-plugin&plugin=' . $plugin_slug . '&_wpnonce=' . $nonce ); |
143
|
|
|
} else { |
144
|
|
|
$url = admin_url( 'plugin-install.php?tab=search&type=term&s=' . $plugin_slug ); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
wp_safe_redirect( $url ); |
148
|
|
|
exit; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
// Setup wizard redirect. |
152
|
|
|
if ( get_transient( '_wc_activation_redirect' ) && apply_filters( 'woocommerce_enable_setup_wizard', true ) ) { |
153
|
|
|
$do_redirect = true; |
154
|
|
|
$current_page = isset( $_GET['page'] ) ? wc_clean( wp_unslash( $_GET['page'] ) ) : false; |
|
|
|
|
155
|
|
|
|
156
|
|
|
// On these pages, or during these events, postpone the redirect. |
157
|
|
|
if ( wp_doing_ajax() || is_network_admin() || ! current_user_can( 'manage_woocommerce' ) ) { |
158
|
|
|
$do_redirect = false; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
// On these pages, or during these events, disable the redirect. |
162
|
|
|
if ( 'wc-setup' === $current_page || ! WC_Admin_Notices::has_notice( 'install' ) || apply_filters( 'woocommerce_prevent_automatic_wizard_redirect', false ) || isset( $_GET['activate-multi'] ) ) { |
163
|
|
|
delete_transient( '_wc_activation_redirect' ); |
164
|
|
|
$do_redirect = false; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if ( $do_redirect ) { |
168
|
|
|
delete_transient( '_wc_activation_redirect' ); |
169
|
|
|
wp_safe_redirect( admin_url( 'index.php?page=wc-setup' ) ); |
170
|
|
|
exit; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
// phpcs:enable WordPress.Security.NonceVerification.NoNonceVerification |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Prevent any user who cannot 'edit_posts' (subscribers, customers etc) from accessing admin. |
178
|
|
|
*/ |
179
|
|
|
public function prevent_admin_access() { |
180
|
|
|
$prevent_access = false; |
181
|
|
|
|
182
|
|
|
if ( apply_filters( 'woocommerce_disable_admin_bar', true ) && ! is_ajax() && isset( $_SERVER['SCRIPT_FILENAME'] ) && basename( sanitize_text_field( wp_unslash( $_SERVER['SCRIPT_FILENAME'] ) ) ) !== 'admin-post.php' ) { |
|
|
|
|
183
|
|
|
$has_cap = false; |
184
|
|
|
$access_caps = array( 'edit_posts', 'manage_woocommerce', 'view_admin_dashboard' ); |
185
|
|
|
|
186
|
|
|
foreach ( $access_caps as $access_cap ) { |
187
|
|
|
if ( current_user_can( $access_cap ) ) { |
188
|
|
|
$has_cap = true; |
189
|
|
|
break; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
if ( ! $has_cap ) { |
194
|
|
|
$prevent_access = true; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if ( apply_filters( 'woocommerce_prevent_admin_access', $prevent_access ) ) { |
199
|
|
|
wp_safe_redirect( wc_get_page_permalink( 'myaccount' ) ); |
200
|
|
|
exit; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Preview email template. |
206
|
|
|
*/ |
207
|
|
|
public function preview_emails() { |
208
|
|
|
|
209
|
|
|
if ( isset( $_GET['preview_woocommerce_mail'] ) ) { |
210
|
|
|
if ( ! ( isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'preview-mail' ) ) ) { |
|
|
|
|
211
|
|
|
die( 'Security check' ); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
// load the mailer class. |
215
|
|
|
$mailer = WC()->mailer(); |
216
|
|
|
|
217
|
|
|
// get the preview email subject. |
218
|
|
|
$email_heading = __( 'HTML email template', 'woocommerce' ); |
219
|
|
|
|
220
|
|
|
// get the preview email content. |
221
|
|
|
ob_start(); |
222
|
|
|
include 'views/html-email-template-preview.php'; |
223
|
|
|
$message = ob_get_clean(); |
224
|
|
|
|
225
|
|
|
// create a new email. |
226
|
|
|
$email = new WC_Email(); |
227
|
|
|
|
228
|
|
|
// wrap the content with the email template and then add styles. |
229
|
|
|
$message = apply_filters( 'woocommerce_mail_content', $email->style_inline( $mailer->wrap_message( $email_heading, $message ) ) ); |
230
|
|
|
|
231
|
|
|
// print the preview email. |
232
|
|
|
// phpcs:ignore WordPress.Security.EscapeOutput |
233
|
|
|
echo $message; |
234
|
|
|
// phpcs:enable |
235
|
|
|
exit; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Change the admin footer text on WooCommerce admin pages. |
241
|
|
|
* |
242
|
|
|
* @since 2.3 |
243
|
|
|
* @param string $footer_text text to be rendered in the footer. |
244
|
|
|
* @return string |
245
|
|
|
*/ |
246
|
|
|
public function admin_footer_text( $footer_text ) { |
247
|
|
|
if ( ! current_user_can( 'manage_woocommerce' ) || ! function_exists( 'wc_get_screen_ids' ) ) { |
248
|
|
|
return $footer_text; |
249
|
|
|
} |
250
|
|
|
$current_screen = get_current_screen(); |
251
|
|
|
$wc_pages = wc_get_screen_ids(); |
252
|
|
|
|
253
|
|
|
// Set only WC pages. |
254
|
|
|
$wc_pages = array_diff( $wc_pages, array( 'profile', 'user-edit' ) ); |
255
|
|
|
|
256
|
|
|
// Check to make sure we're on a WooCommerce admin page. |
257
|
|
|
if ( isset( $current_screen->id ) && apply_filters( 'woocommerce_display_admin_footer_text', in_array( $current_screen->id, $wc_pages, true ) ) ) { |
258
|
|
|
// Change the footer text. |
259
|
|
|
if ( ! get_option( 'woocommerce_admin_footer_text_rated' ) ) { |
260
|
|
|
$footer_text = sprintf( |
261
|
|
|
/* translators: 1: WooCommerce 2:: five stars */ |
262
|
|
|
__( 'If you like %1$s please leave us a %2$s rating. A huge thanks in advance!', 'woocommerce' ), |
263
|
|
|
sprintf( '<strong>%s</strong>', esc_html__( 'WooCommerce', 'woocommerce' ) ), |
264
|
|
|
'<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>' |
265
|
|
|
); |
266
|
|
|
wc_enqueue_js( |
267
|
|
|
"jQuery( 'a.wc-rating-link' ).click( function() { |
268
|
|
|
jQuery.post( '" . WC()->ajax_url() . "', { action: 'woocommerce_rated' } ); |
269
|
|
|
jQuery( this ).parent().text( jQuery( this ).data( 'rated' ) ); |
270
|
|
|
});" |
271
|
|
|
); |
272
|
|
|
} else { |
273
|
|
|
$footer_text = __( 'Thank you for selling with WooCommerce.', 'woocommerce' ); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
return $footer_text; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Check on a Jetpack install queued by the Setup Wizard. |
282
|
|
|
* |
283
|
|
|
* See: WC_Admin_Setup_Wizard::install_jetpack() |
284
|
|
|
*/ |
285
|
|
|
public function setup_wizard_check_jetpack() { |
286
|
|
|
$jetpack_active = class_exists( 'Jetpack' ); |
287
|
|
|
|
288
|
|
|
wp_send_json_success( |
289
|
|
|
array( |
290
|
|
|
'is_active' => $jetpack_active ? 'yes' : 'no', |
291
|
|
|
) |
292
|
|
|
); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Disable WXR export of scheduled action posts. |
297
|
|
|
* |
298
|
|
|
* @since 3.6.2 |
299
|
|
|
* |
300
|
|
|
* @param array $args Scehduled action post type registration args. |
301
|
|
|
* |
302
|
|
|
* @return array |
303
|
|
|
*/ |
304
|
|
|
public function disable_webhook_post_export( $args ) { |
305
|
|
|
$args['can_export'] = false; |
306
|
|
|
return $args; |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
return new WC_Admin(); |
311
|
|
|
|