Completed
Pull Request — master (#10632)
by Mike
13:10
created

WC_Admin_Notices::reset_admin_notices()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 3
Ratio 23.08 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 3
loc 13
rs 8.8571
cc 6
eloc 7
nc 4
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 18 and the first side effect is on line 12.

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
 * Display notices in admin
4
 *
5
 * @author      WooThemes
6
 * @category    Admin
7
 * @package     WooCommerce/Admin
8
 * @version     2.3.0
9
 */
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
/**
16
 * WC_Admin_Notices Class.
17
 */
18
class WC_Admin_Notices {
19
20
	/**
21
	 * Array of notices - name => callback.
22
	 * @var array
23
	 */
24
	private $core_notices = array(
25
		'install'             => 'install_notice',
26
		'update'              => 'update_notice',
27
		'template_files'      => 'template_file_check_notice',
28
		'theme_support'       => 'theme_check_notice',
29
		'legacy_shipping'     => 'legacy_shipping_notice',
30
		'no_shipping_methods' => 'no_shipping_methods_notice',
31
		'simplify_commerce'   => 'simplify_commerce_notice',
32
	);
33
34
	/**
35
	 * Constructor.
36
	 */
37
	public function __construct() {
38
		add_action( 'switch_theme', array( $this, 'reset_admin_notices' ) );
39
		add_action( 'woocommerce_installed', array( $this, 'reset_admin_notices' ) );
40
		add_action( 'wp_loaded', array( $this, 'hide_notices' ) );
41
42
		if ( current_user_can( 'manage_woocommerce' ) ) {
43
			add_action( 'admin_print_styles', array( $this, 'add_notices' ) );
44
		}
45
	}
46
47
	/**
48
	 * Remove all notices.
49
	 */
50
	public static function remove_all_notices() {
51
		delete_option( 'woocommerce_admin_notices' );
52
	}
53
54
	/**
55
	 * Reset notices for themes when switched or a new version of WC is installed.
56
	 */
57
	public function reset_admin_notices() {
58 View Code Duplication
		if ( ! current_theme_supports( 'woocommerce' ) && ! in_array( get_option( 'template' ), wc_get_core_supported_themes() ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
			self::add_notice( 'theme_support' );
60
		}
61
62
		$simplify_options = get_option( 'woocommerce_simplify_commerce_settings', array() );
63
64
		if ( ! class_exists( 'WC_Gateway_Simplify_Commerce_Loader' ) && ! empty( $simplify_options['enabled'] ) && 'yes' === $simplify_options['enabled'] ) {
65
			WC_Admin_Notices::add_notice( 'simplify_commerce' );
66
		}
67
68
		self::add_notice( 'template_files' );
69
	}
70
71
	/**
72
	 * Show a notice.
73
	 * @param string $name
74
	 */
75
	public static function add_notice( $name ) {
76
		$notices = array_unique( array_merge( get_option( 'woocommerce_admin_notices', array() ), array( $name ) ) );
77
		update_option( 'woocommerce_admin_notices', $notices );
78
	}
79
80
	/**
81
	 * Remove a notice from being displayed.
82
	 * @param  string $name
83
	 */
84
	public static function remove_notice( $name ) {
85
		$notices = array_diff( get_option( 'woocommerce_admin_notices', array() ), array( $name ) );
86
		update_option( 'woocommerce_admin_notices', $notices );
87
		delete_option( 'woocommerce_admin_notice_' . $name );
88
	}
89
90
	/**
91
	 * See if a notice is being shown.
92
	 * @param  string  $name
93
	 * @return boolean
94
	 */
95
	public static function has_notice( $name ) {
96
		return in_array( $name, get_option( 'woocommerce_admin_notices', array() ) );
97
	}
98
99
	/**
100
	 * Hide a notice if the GET variable is set.
101
	 */
102
	public function hide_notices() {
103
		if ( isset( $_GET['wc-hide-notice'] ) && isset( $_GET['_wc_notice_nonce'] ) ) {
104
			if ( ! wp_verify_nonce( $_GET['_wc_notice_nonce'], 'woocommerce_hide_notices_nonce' ) ) {
105
				wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
106
			}
107
108
			if ( ! current_user_can( 'manage_woocommerce' ) ) {
109
				wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce' ) );
110
			}
111
112
			$hide_notice = sanitize_text_field( $_GET['wc-hide-notice'] );
113
			self::remove_notice( $hide_notice );
114
			do_action( 'woocommerce_hide_' . $hide_notice . '_notice' );
115
		}
116
	}
117
118
	/**
119
	 * Add notices + styles if needed.
120
	 */
121
	public function add_notices() {
122
		$notices = get_option( 'woocommerce_admin_notices', array() );
123
124
		if ( $notices ) {
125
			wp_enqueue_style( 'woocommerce-activation', plugins_url(  '/assets/css/activation.css', WC_PLUGIN_FILE ) );
126
			foreach ( $notices as $notice ) {
127
				if ( ! empty( $this->core_notices[ $notice ] ) && apply_filters( 'woocommerce_show_admin_notice', true, $notice ) ) {
128
					add_action( 'admin_notices', array( $this, $this->core_notices[ $notice ] ) );
129
				} else {
130
					add_action( 'admin_notices', array( $this, 'output_custom_notices' ) );
131
				}
132
			}
133
		}
134
	}
135
136
	/**
137
	 * Add a custom notice.
138
	 * @param string $name
139
	 * @param string $notice_html
140
	 */
141
	public static function add_custom_notice( $name, $notice_html ) {
142
		self::add_notice( $name );
143
		update_option( 'woocommerce_admin_notice_' . $name, wp_kses_post( $notice_html ) );
144
	}
145
146
	/**
147
	 * Output any stored custom notices.
148
	 */
149
	public function output_custom_notices() {
150
		$notices = get_option( 'woocommerce_admin_notices', array() );
151
		if ( $notices ) {
152
			foreach ( $notices as $notice ) {
153
				if ( empty( $this->core_notices[ $notice ] ) ) {
154
					$notice_html = get_option( 'woocommerce_admin_notice_' . $notice );
155
156
					if ( $notice_html ) {
157
						include( 'views/html-notice-custom.php' );
158
					}
159
				}
160
			}
161
		}
162
	}
163
164
	/**
165
	 * If we need to update, include a message with the update button.
166
	 */
167
	public function update_notice() {
168
		include( 'views/html-notice-update.php' );
169
	}
170
171
	/**
172
	 * If we have just installed, show a message with the install pages button.
173
	 */
174
	public function install_notice() {
175
		include( 'views/html-notice-install.php' );
176
	}
177
178
	/**
179
	 * Show the Theme Check notice.
180
	 */
181
	public function theme_check_notice() {
182 View Code Duplication
		if ( ! current_theme_supports( 'woocommerce' ) && ! in_array( get_option( 'template' ), wc_get_core_supported_themes() ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
183
			include( 'views/html-notice-theme-support.php' );
184
		} else {
185
			self::remove_notice( 'theme_support' );
186
		}
187
	}
188
189
	/**
190
	 * Show a notice highlighting bad template files.
191
	 */
192
	public function template_file_check_notice() {
193
		$core_templates = WC_Admin_Status::scan_template_files( WC()->plugin_path() . '/templates' );
194
		$outdated       = false;
195
196
		foreach ( $core_templates as $file ) {
197
198
			$theme_file = false;
199 View Code Duplication
			if ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
200
				$theme_file = get_stylesheet_directory() . '/' . $file;
201
			} elseif ( file_exists( get_stylesheet_directory() . '/woocommerce/' . $file ) ) {
202
				$theme_file = get_stylesheet_directory() . '/woocommerce/' . $file;
203
			} elseif ( file_exists( get_template_directory() . '/' . $file ) ) {
204
				$theme_file = get_template_directory() . '/' . $file;
205
			} elseif( file_exists( get_template_directory() . '/woocommerce/' . $file ) ) {
206
				$theme_file = get_template_directory() . '/woocommerce/' . $file;
207
			}
208
209
			if ( $theme_file !== false ) {
210
				$core_version  = WC_Admin_Status::get_file_version( WC()->plugin_path() . '/templates/' . $file );
211
				$theme_version = WC_Admin_Status::get_file_version( $theme_file );
212
213
				if ( $core_version && $theme_version && version_compare( $theme_version, $core_version, '<' ) ) {
214
					$outdated = true;
215
					break;
216
				}
217
			}
218
		}
219
220
		if ( $outdated ) {
221
			include( 'views/html-notice-template-check.php' );
222
		} else {
223
			self::remove_notice( 'template_files' );
224
		}
225
	}
226
227
	/**
228
	 * Show a notice asking users to convert to shipping zones.
229
	 */
230
	public function legacy_shipping_notice() {
231
		$maybe_load_legacy_methods = array( 'flat_rate', 'free_shipping', 'international_delivery', 'local_delivery', 'local_pickup' );
232
		$enabled                   = false;
233
234
		foreach ( $maybe_load_legacy_methods as $method ) {
235
			$options = get_option( 'woocommerce_' . $method . '_settings' );
236
			if ( $options && isset( $options['enabled'] ) && 'yes' === $options['enabled'] ) {
237
				$enabled = true;
238
			}
239
		}
240
241
		if ( $enabled ) {
242
			include( 'views/html-notice-legacy-shipping.php' );
243
		} else {
244
			self::remove_notice( 'template_files' );
245
		}
246
	}
247
248
	/**
249
	 * No shipping methods.
250
	 */
251
	public function no_shipping_methods_notice() {
252
		if ( wc_shipping_enabled() && ( empty( $_GET['page'] ) || empty( $_GET['tab'] ) || 'wc-settings' !== $_GET['page'] || 'shipping' !== $_GET['tab'] ) ) {
253
			global $wpdb;
254
255
			$product_count = wp_count_posts( 'product' );
256
			$method_count  = absint( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_shipping_zone_methods" ) );
257
258
			if ( $product_count->publish > 0 && 0 === $method_count ) {
259
				include( 'views/html-notice-no-shipping-methods.php' );
260
			}
261
262
			if ( $method_count > 0 ) {
263
				self::remove_notice( 'no_shipping_methods' );
264
			}
265
		}
266
	}
267
268
	/**
269
	 * Simplify Commerce is being removed from core.
270
	 */
271
	public function simplify_commerce_notice() {
272
		if ( class_exists( 'WC_Gateway_Simplify_Commerce_Loader' ) ) {
273
			self::remove_notice( 'simplify_commerce' );
274
			return;
275
		}
276
		if ( empty( $_GET['action'] ) ) {
277
			include( 'views/html-notice-simplify-commerce.php' );
278
		}
279
	}
280
}
281
282
new WC_Admin_Notices();
283