Completed
Push — master ( c01a09...075891 )
by
unknown
29s queued 11s
created

WC_Admin::includes()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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