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

WC_Admin_Notices::reset_admin_notices()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 3
Ratio 50 %
Metric Value
dl 3
loc 6
rs 9.4285
cc 3
eloc 4
nc 2
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
	);
32
33
	/**
34
	 * Constructor.
35
	 */
36
	public function __construct() {
37
		add_action( 'switch_theme', array( $this, 'reset_admin_notices' ) );
38
		add_action( 'woocommerce_installed', array( $this, 'reset_admin_notices' ) );
39
		add_action( 'wp_loaded', array( $this, 'hide_notices' ) );
40
41
		if ( current_user_can( 'manage_woocommerce' ) ) {
42
			add_action( 'admin_print_styles', array( $this, 'add_notices' ) );
43
		}
44
	}
45
46
	/**
47
	 * Remove all notices.
48
	 */
49
	public static function remove_all_notices() {
50
		delete_option( 'woocommerce_admin_notices' );
51
	}
52
53
	/**
54
	 * Reset notices for themes when switched or a new version of WC is installed.
55
	 */
56
	public function reset_admin_notices() {
57 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...
58
			self::add_notice( 'theme_support' );
59
		}
60
		self::add_notice( 'template_files' );
61
	}
62
63
	/**
64
	 * Show a notice.
65
	 * @param string $name
66
	 */
67
	public static function add_notice( $name ) {
68
		$notices = array_unique( array_merge( get_option( 'woocommerce_admin_notices', array() ), array( $name ) ) );
69
		update_option( 'woocommerce_admin_notices', $notices );
70
	}
71
72
	/**
73
	 * Remove a notice from being displayed.
74
	 * @param  string $name
75
	 */
76
	public static function remove_notice( $name ) {
77
		$notices = array_diff( get_option( 'woocommerce_admin_notices', array() ), array( $name ) );
78
		update_option( 'woocommerce_admin_notices', $notices );
79
		delete_option( 'woocommerce_admin_notice_' . $name );
80
	}
81
82
	/**
83
	 * See if a notice is being shown.
84
	 * @param  string  $name
85
	 * @return boolean
86
	 */
87
	public static function has_notice( $name ) {
88
		return in_array( $name, get_option( 'woocommerce_admin_notices', array() ) );
89
	}
90
91
	/**
92
	 * Hide a notice if the GET variable is set.
93
	 */
94
	public function hide_notices() {
95
		if ( isset( $_GET['wc-hide-notice'] ) && isset( $_GET['_wc_notice_nonce'] ) ) {
96
			if ( ! wp_verify_nonce( $_GET['_wc_notice_nonce'], 'woocommerce_hide_notices_nonce' ) ) {
97
				wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
98
			}
99
100
			if ( ! current_user_can( 'manage_woocommerce' ) ) {
101
				wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce' ) );
102
			}
103
104
			$hide_notice = sanitize_text_field( $_GET['wc-hide-notice'] );
105
			self::remove_notice( $hide_notice );
106
			do_action( 'woocommerce_hide_' . $hide_notice . '_notice' );
107
		}
108
	}
109
110
	/**
111
	 * Add notices + styles if needed.
112
	 */
113
	public function add_notices() {
114
		$notices = get_option( 'woocommerce_admin_notices', array() );
115
116
		if ( $notices ) {
117
			wp_enqueue_style( 'woocommerce-activation', plugins_url(  '/assets/css/activation.css', WC_PLUGIN_FILE ) );
118
			foreach ( $notices as $notice ) {
119
				if ( ! empty( $this->core_notices[ $notice ] ) && apply_filters( 'woocommerce_show_admin_notice', true, $notice ) ) {
120
					add_action( 'admin_notices', array( $this, $this->core_notices[ $notice ] ) );
121
				} else {
122
					add_action( 'admin_notices', array( $this, 'output_custom_notices' ) );
123
				}
124
			}
125
		}
126
	}
127
128
	/**
129
	 * Add a custom notice.
130
	 * @param string $name
131
	 * @param string $notice_html
132
	 */
133
	public static function add_custom_notice( $name, $notice_html ) {
134
		self::add_notice( $name );
135
		update_option( 'woocommerce_admin_notice_' . $name, wp_kses_post( $notice_html ) );
136
	}
137
138
	/**
139
	 * Output any stored custom notices.
140
	 */
141
	public function output_custom_notices() {
142
		$notices = get_option( 'woocommerce_admin_notices', array() );
143
		if ( $notices ) {
144
			foreach ( $notices as $notice ) {
145
				if ( empty( $this->core_notices[ $notice ] ) ) {
146
					$notice_html = get_option( 'woocommerce_admin_notice_' . $notice );
147
148
					if ( $notice_html ) {
149
						include( 'views/html-notice-custom.php' );
150
					}
151
				}
152
			}
153
		}
154
	}
155
156
	/**
157
	 * If we need to update, include a message with the update button.
158
	 */
159
	public function update_notice() {
160
		include( 'views/html-notice-update.php' );
161
	}
162
163
	/**
164
	 * If we have just installed, show a message with the install pages button.
165
	 */
166
	public function install_notice() {
167
		include( 'views/html-notice-install.php' );
168
	}
169
170
	/**
171
	 * Show the Theme Check notice.
172
	 */
173
	public function theme_check_notice() {
174 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...
175
			include( 'views/html-notice-theme-support.php' );
176
		} else {
177
			self::remove_notice( 'theme_support' );
178
		}
179
	}
180
181
	/**
182
	 * Show a notice highlighting bad template files.
183
	 */
184
	public function template_file_check_notice() {
185
		$core_templates = WC_Admin_Status::scan_template_files( WC()->plugin_path() . '/templates' );
186
		$outdated       = false;
187
188
		foreach ( $core_templates as $file ) {
189
190
			$theme_file = false;
191 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...
192
				$theme_file = get_stylesheet_directory() . '/' . $file;
193
			} elseif ( file_exists( get_stylesheet_directory() . '/woocommerce/' . $file ) ) {
194
				$theme_file = get_stylesheet_directory() . '/woocommerce/' . $file;
195
			} elseif ( file_exists( get_template_directory() . '/' . $file ) ) {
196
				$theme_file = get_template_directory() . '/' . $file;
197
			} elseif( file_exists( get_template_directory() . '/woocommerce/' . $file ) ) {
198
				$theme_file = get_template_directory() . '/woocommerce/' . $file;
199
			}
200
201
			if ( $theme_file !== false ) {
202
				$core_version  = WC_Admin_Status::get_file_version( WC()->plugin_path() . '/templates/' . $file );
203
				$theme_version = WC_Admin_Status::get_file_version( $theme_file );
204
205
				if ( $core_version && $theme_version && version_compare( $theme_version, $core_version, '<' ) ) {
206
					$outdated = true;
207
					break;
208
				}
209
			}
210
		}
211
212
		if ( $outdated ) {
213
			include( 'views/html-notice-template-check.php' );
214
		} else {
215
			self::remove_notice( 'template_files' );
216
		}
217
	}
218
219
	/**
220
	 * Show a notice asking users to convert to shipping zones.
221
	 */
222
	public function legacy_shipping_notice() {
223
		$maybe_load_legacy_methods = array( 'flat_rate', 'free_shipping', 'international_delivery', 'local_delivery', 'local_pickup' );
224
		$enabled                   = false;
225
226
		foreach ( $maybe_load_legacy_methods as $method ) {
227
			$options = get_option( 'woocommerce_' . $method . '_settings' );
228
			if ( $options && isset( $options['enabled'] ) && 'yes' === $options['enabled'] ) {
229
				$enabled = true;
230
			}
231
		}
232
233
		if ( $enabled ) {
234
			include( 'views/html-notice-legacy-shipping.php' );
235
		} else {
236
			self::remove_notice( 'template_files' );
237
		}
238
	}
239
240
	/**
241
	 * No shipping methods.
242
	 */
243
	public function no_shipping_methods_notice() {
244
		if ( wc_shipping_enabled() && ( empty( $_GET['page'] ) || empty( $_GET['tab'] ) || 'wc-settings' !== $_GET['page'] || 'shipping' !== $_GET['tab'] ) ) {
245
			global $wpdb;
246
247
			$product_count = wp_count_posts( 'product' );
248
			$method_count  = absint( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_shipping_zone_methods" ) );
249
250
			if ( $product_count->publish > 0 && 0 === $method_count ) {
251
				include( 'views/html-notice-no-shipping-methods.php' );
252
			}
253
254
			if ( $method_count > 0 ) {
255
				self::remove_notice( 'no_shipping_methods' );
256
			}
257
		}
258
	}
259
}
260
261
new WC_Admin_Notices();
262