Completed
Push — master ( fb62b9...43af20 )
by Mike
08:19
created

WC_Admin::buffer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 19 and the first side effect is on line 13.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * WooCommerce Admin
4
 *
5
 * @class       WC_Admin
6
 * @author      WooThemes
7
 * @category    Admin
8
 * @package     WooCommerce/Admin
9
 * @version     2.3
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
	 * 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
		if ( ! get_transient( '_wc_activation_redirect' ) ) {
108
			return;
109
		}
110
111
		delete_transient( '_wc_activation_redirect' );
112
113
		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 ) ) {
114
			return;
115
		}
116
117
		// If the user needs to install, send them to the setup wizard
118
		if ( WC_Admin_Notices::has_notice( 'install' ) ) {
119
			wp_safe_redirect( admin_url( 'index.php?page=wc-setup' ) );
120
			exit;
121
		}
122
	}
123
124
	/**
125
	 * Prevent any user who cannot 'edit_posts' (subscribers, customers etc) from accessing admin.
126
	 */
127
	public function prevent_admin_access() {
128
		$prevent_access = false;
129
130
		if ( 'yes' === get_option( 'woocommerce_lock_down_admin', 'yes' ) && ! is_ajax() && basename( $_SERVER["SCRIPT_FILENAME"] ) !== 'admin-post.php' && ! current_user_can( 'edit_posts' ) && ! current_user_can( 'manage_woocommerce' ) ) {
131
			$prevent_access = true;
132
		}
133
134
		$prevent_access = apply_filters( 'woocommerce_prevent_admin_access', $prevent_access );
135
136
		if ( $prevent_access ) {
137
			wp_safe_redirect( wc_get_page_permalink( 'myaccount' ) );
138
			exit;
139
		}
140
	}
141
142
	/**
143
	 * Preview email template.
144
	 *
145
	 * @return string
146
	 */
147
	public function preview_emails() {
148
149
		if ( isset( $_GET['preview_woocommerce_mail'] ) ) {
150
			if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'preview-mail') ) {
151
				die( 'Security check' );
152
			}
153
154
			// load the mailer class
155
			$mailer        = WC()->mailer();
156
157
			// get the preview email subject
158
			$email_heading = __( 'HTML Email Template', 'woocommerce' );
159
160
			// get the preview email content
161
			ob_start();
162
			include( 'views/html-email-template-preview.php' );
163
			$message       = ob_get_clean();
164
165
			// create a new email
166
			$email         = new WC_Email();
167
168
			// wrap the content with the email template and then add styles
169
			$message       = $email->style_inline( $mailer->wrap_message( $email_heading, $message ) );
170
171
			// print the preview email
172
			echo $message;
173
			exit;
174
		}
175
	}
176
177
	/**
178
	 * Change the admin footer text on WooCommerce admin pages.
179
	 *
180
	 * @since  2.3
181
	 * @param  string $footer_text
182
	 * @return string
183
	 */
184
	public function admin_footer_text( $footer_text ) {
185
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
186
			return;
187
		}
188
		$current_screen = get_current_screen();
189
		$wc_pages       = wc_get_screen_ids();
190
191
		// Set only wc pages
192
		$wc_pages = array_flip( $wc_pages );
193
		if ( isset( $wc_pages['profile'] ) ) {
194
			unset( $wc_pages['profile'] );
195
		}
196
		if ( isset( $wc_pages['user-edit'] ) ) {
197
			unset( $wc_pages['user-edit'] );
198
		}
199
		$wc_pages = array_flip( $wc_pages );
200
201
		// Check to make sure we're on a WooCommerce admin page
202
		if ( isset( $current_screen->id ) && apply_filters( 'woocommerce_display_admin_footer_text', in_array( $current_screen->id, $wc_pages ) ) ) {
203
			// Change the footer text
204
			if ( ! get_option( 'woocommerce_admin_footer_text_rated' ) ) {
205
				$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>' );
206
				wc_enqueue_js( "
207
					jQuery( 'a.wc-rating-link' ).click( function() {
208
						jQuery.post( '" . WC()->ajax_url() . "', { action: 'woocommerce_rated' } );
209
						jQuery( this ).parent().text( jQuery( this ).data( 'rated' ) );
210
					});
211
				" );
212
			} else {
213
				$footer_text = __( 'Thank you for selling with WooCommerce.', 'woocommerce' );
214
			}
215
		}
216
217
		return $footer_text;
218
	}
219
}
220
221
return new WC_Admin();
222