Completed
Push — master ( 856b95...479a13 )
by Mike
08:36
created

WC_Admin::includes()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
dl 0
loc 31
rs 8.439
c 0
b 0
f 0
eloc 20
nc 12
nop 0
1
<?php
2
/**
3
 * WooCommerce Admin
4
 *
5
 * @class    WC_Admin
6
 * @author   WooThemes
7
 * @category Admin
8
 * @package  WooCommerce/Admin
9
 * @version  2.6.0
10
 */
11
12
if ( ! defined( 'ABSPATH' ) ) {
13
	exit; // Exit if accessed directly
14
}
15
16
/**
17
 * WC_Admin class.
18
 */
19
class WC_Admin {
20
21
	/**
22
	 * Constructor.
23
	 */
24
	public function __construct() {
25
		add_action( 'init', array( $this, 'includes' ) );
26
		add_action( 'current_screen', array( $this, 'conditional_includes' ) );
27
		add_action( 'admin_init', array( $this, 'buffer' ), 1 );
28
		add_action( 'admin_init', array( $this, 'preview_emails' ) );
29
		add_action( 'admin_init', array( $this, 'prevent_admin_access' ) );
30
		add_action( 'admin_init', array( $this, 'admin_redirects' ) );
31
		add_action( 'admin_footer', 'wc_print_js', 25 );
32
		add_filter( 'admin_footer_text', array( $this, 'admin_footer_text' ), 1 );
33
	}
34
35
	/**
36
	 * Output buffering allows admin screens to make redirects later on.
37
	 */
38
	public function buffer() {
39
		ob_start();
40
	}
41
42
	/**
43
	 * Include any classes we need within admin.
44
	 */
45
	public function includes() {
46
		include_once( 'wc-admin-functions.php' );
47
		include_once( 'wc-meta-box-functions.php' );
48
		include_once( 'class-wc-admin-post-types.php' );
49
		include_once( 'class-wc-admin-taxonomies.php' );
50
		include_once( 'class-wc-admin-menus.php' );
51
		include_once( 'class-wc-admin-notices.php' );
52
		include_once( 'class-wc-admin-assets.php' );
53
		include_once( 'class-wc-admin-api-keys.php' );
54
		include_once( 'class-wc-admin-webhooks.php' );
55
		include_once( 'class-wc-admin-pointers.php' );
56
57
		// Help Tabs
58
		if ( apply_filters( 'woocommerce_enable_admin_help_tab', true ) ) {
59
			include_once( 'class-wc-admin-help.php' );
60
		}
61
62
		// Setup/welcome
63
		if ( ! empty( $_GET['page'] ) ) {
64
			switch ( $_GET['page'] ) {
65
				case 'wc-setup' :
66
					include_once( 'class-wc-admin-setup-wizard.php' );
67
				break;
68
			}
69
		}
70
71
		// Importers
72
		if ( defined( 'WP_LOAD_IMPORTERS' ) ) {
73
			include_once( 'class-wc-admin-importers.php' );
74
		}
75
	}
76
77
	/**
78
	 * Include admin files conditionally.
79
	 */
80
	public function conditional_includes() {
81
		if ( ! $screen = get_current_screen() ) {
82
			return;
83
		}
84
85
		switch ( $screen->id ) {
86
			case 'dashboard' :
87
				include( 'class-wc-admin-dashboard.php' );
88
			break;
89
			case 'options-permalink' :
90
				include( 'class-wc-admin-permalink-settings.php' );
91
			break;
92
			case 'users' :
93
			case 'user' :
94
			case 'profile' :
95
			case 'user-edit' :
96
				include( 'class-wc-admin-profile.php' );
97
			break;
98
		}
99
	}
100
101
	/**
102
	 * Handle redirects to setup/welcome page after install and updates.
103
	 *
104
	 * For setup wizard, transient must be present, the user must have access rights, and we must ignore the network/bulk plugin updaters.
105
	 */
106
	public function admin_redirects() {
107
		// Nonced plugin install redirects (whitelisted)
108
		if ( ! empty( $_GET['wc-install-plugin-redirect'] ) ) {
109
			$plugin_slug = wc_clean( $_GET['wc-install-plugin-redirect'] );
110
111
			if ( current_user_can( 'install_plugins' ) && in_array( $plugin_slug, array( 'woocommerce-gateway-stripe' ) ) ) {
112
				$nonce = wp_create_nonce( 'install-plugin_' . $plugin_slug );
113
				$url   = self_admin_url( 'update.php?action=install-plugin&plugin=' . $plugin_slug . '&_wpnonce=' . $nonce );
114
			} else {
115
				$url = admin_url( 'plugin-install.php?tab=search&type=term&s=' . $plugin_slug );
116
			}
117
118
			wp_safe_redirect( $url );
119
			exit;
120
		}
121
122
		// Setup wizard redirect
123
		if ( get_transient( '_wc_activation_redirect' ) ) {
124
			delete_transient( '_wc_activation_redirect' );
125
126
			if ( ( ! empty( $_GET['page'] ) && in_array( $_GET['page'], array( 'wc-setup' ) ) ) || is_network_admin() || isset( $_GET['activate-multi'] ) || ! current_user_can( 'manage_woocommerce' ) || apply_filters( 'woocommerce_prevent_automatic_wizard_redirect', false ) ) {
127
				return;
128
			}
129
130
			// If the user needs to install, send them to the setup wizard
131
			if ( WC_Admin_Notices::has_notice( 'install' ) ) {
132
				wp_safe_redirect( admin_url( 'index.php?page=wc-setup' ) );
133
				exit;
134
			}
135
		}
136
	}
137
138
	/**
139
	 * Prevent any user who cannot 'edit_posts' (subscribers, customers etc) from accessing admin.
140
	 */
141
	public function prevent_admin_access() {
142
		$prevent_access = false;
143
144
		if ( 'yes' === get_option( 'woocommerce_lock_down_admin', 'yes' ) && ! is_ajax() && basename( $_SERVER["SCRIPT_FILENAME"] ) !== 'admin-post.php' ) {
145
			$has_cap     = false;
146
			$access_caps = array( 'edit_posts', 'manage_woocommerce', 'view_admin_dashboard' );
147
148
			foreach ( $access_caps as $access_cap ) {
149
				if ( current_user_can( $access_cap ) ) {
150
					$has_cap = true;
151
					break;
152
				}
153
			}
154
155
			if ( ! $has_cap ) {
156
				$prevent_access = true;
157
			}
158
		}
159
160
		if ( apply_filters( 'woocommerce_prevent_admin_access', $prevent_access ) ) {
161
			wp_safe_redirect( wc_get_page_permalink( 'myaccount' ) );
162
			exit;
163
		}
164
	}
165
166
	/**
167
	 * Preview email template.
168
	 *
169
	 * @return string
170
	 */
171
	public function preview_emails() {
172
173
		if ( isset( $_GET['preview_woocommerce_mail'] ) ) {
174
			if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'preview-mail') ) {
175
				die( 'Security check' );
176
			}
177
178
			// load the mailer class
179
			$mailer        = WC()->mailer();
180
181
			// get the preview email subject
182
			$email_heading = __( 'HTML Email Template', 'woocommerce' );
183
184
			// get the preview email content
185
			ob_start();
186
			include( 'views/html-email-template-preview.php' );
187
			$message       = ob_get_clean();
188
189
			// create a new email
190
			$email         = new WC_Email();
191
192
			// wrap the content with the email template and then add styles
193
			$message       = apply_filters( 'woocommerce_mail_content', $email->style_inline( $mailer->wrap_message( $email_heading, $message ) ) );
194
195
			// print the preview email
196
			echo $message;
197
			exit;
198
		}
199
	}
200
201
	/**
202
	 * Change the admin footer text on WooCommerce admin pages.
203
	 *
204
	 * @since  2.3
205
	 * @param  string $footer_text
206
	 * @return string
207
	 */
208
	public function admin_footer_text( $footer_text ) {
209
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
210
			return;
211
		}
212
		$current_screen = get_current_screen();
213
		$wc_pages       = wc_get_screen_ids();
214
215
		// Set only wc pages
216
		$wc_pages = array_flip( $wc_pages );
217
		if ( isset( $wc_pages['profile'] ) ) {
218
			unset( $wc_pages['profile'] );
219
		}
220
		if ( isset( $wc_pages['user-edit'] ) ) {
221
			unset( $wc_pages['user-edit'] );
222
		}
223
		$wc_pages = array_flip( $wc_pages );
224
225
		// Check to make sure we're on a WooCommerce admin page
226
		if ( isset( $current_screen->id ) && apply_filters( 'woocommerce_display_admin_footer_text', in_array( $current_screen->id, $wc_pages ) ) ) {
227
			// Change the footer text
228
			if ( ! get_option( 'woocommerce_admin_footer_text_rated' ) ) {
229
				$footer_text = sprintf( __( 'If you like <strong>WooCommerce</strong> please leave us a %s&#9733;&#9733;&#9733;&#9733;&#9733;%s rating. A huge thank you from WooThemes in advance!', 'woocommerce' ), '<a href="https://wordpress.org/support/view/plugin-reviews/woocommerce?filter=5#postform" target="_blank" class="wc-rating-link" data-rated="' . esc_attr__( 'Thanks :)', 'woocommerce' ) . '">', '</a>' );
230
				wc_enqueue_js( "
231
					jQuery( 'a.wc-rating-link' ).click( function() {
232
						jQuery.post( '" . WC()->ajax_url() . "', { action: 'woocommerce_rated' } );
233
						jQuery( this ).parent().text( jQuery( this ).data( 'rated' ) );
234
					});
235
				" );
236
			} else {
237
				$footer_text = __( 'Thank you for selling with WooCommerce.', 'woocommerce' );
238
			}
239
		}
240
241
		return $footer_text;
242
	}
243
}
244
245
return new WC_Admin();
246