Completed
Pull Request — master (#10628)
by Mike
09:21
created

WC_Admin_Notices::no_shipping_methods_notice()   B

Complexity

Conditions 9
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 7.756
cc 9
eloc 9
nc 5
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
	}
80
81
	/**
82
	 * See if a notice is being shown.
83
	 * @param  string  $name
84
	 * @return boolean
85
	 */
86
	public static function has_notice( $name ) {
87
		return in_array( $name, get_option( 'woocommerce_admin_notices', array() ) );
88
	}
89
90
	/**
91
	 * Hide a notice if the GET variable is set.
92
	 */
93
	public function hide_notices() {
94
		if ( isset( $_GET['wc-hide-notice'] ) && isset( $_GET['_wc_notice_nonce'] ) ) {
95
			if ( ! wp_verify_nonce( $_GET['_wc_notice_nonce'], 'woocommerce_hide_notices_nonce' ) ) {
96
				wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
97
			}
98
99
			if ( ! current_user_can( 'manage_woocommerce' ) ) {
100
				wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce' ) );
101
			}
102
103
			$hide_notice = sanitize_text_field( $_GET['wc-hide-notice'] );
104
			self::remove_notice( $hide_notice );
105
			do_action( 'woocommerce_hide_' . $hide_notice . '_notice' );
106
		}
107
	}
108
109
	/**
110
	 * Add notices + styles if needed.
111
	 */
112
	public function add_notices() {
113
		$notices = get_option( 'woocommerce_admin_notices', array() );
114
115
		if ( $notices ) {
116
			wp_enqueue_style( 'woocommerce-activation', plugins_url(  '/assets/css/activation.css', WC_PLUGIN_FILE ) );
117
			foreach ( $notices as $notice ) {
118
				if ( ! empty( $this->core_notices[ $notice ] ) && apply_filters( 'woocommerce_show_admin_notice', true, $notice ) ) {
119
					add_action( 'admin_notices', array( $this, $this->core_notices[ $notice ] ) );
120
				}
121
			}
122
		}
123
	}
124
125
	/**
126
	 * If we need to update, include a message with the update button.
127
	 */
128
	public function update_notice() {
129
		include( 'views/html-notice-update.php' );
130
	}
131
132
	/**
133
	 * If we have just installed, show a message with the install pages button.
134
	 */
135
	public function install_notice() {
136
		include( 'views/html-notice-install.php' );
137
	}
138
139
	/**
140
	 * Show the Theme Check notice.
141
	 */
142
	public function theme_check_notice() {
143 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...
144
			include( 'views/html-notice-theme-support.php' );
145
		} else {
146
			self::remove_notice( 'theme_support' );
147
		}
148
	}
149
150
	/**
151
	 * Show a notice highlighting bad template files.
152
	 */
153
	public function template_file_check_notice() {
154
		$core_templates = WC_Admin_Status::scan_template_files( WC()->plugin_path() . '/templates' );
155
		$outdated       = false;
156
157
		foreach ( $core_templates as $file ) {
158
159
			$theme_file = false;
160 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...
161
				$theme_file = get_stylesheet_directory() . '/' . $file;
162
			} elseif ( file_exists( get_stylesheet_directory() . '/woocommerce/' . $file ) ) {
163
				$theme_file = get_stylesheet_directory() . '/woocommerce/' . $file;
164
			} elseif ( file_exists( get_template_directory() . '/' . $file ) ) {
165
				$theme_file = get_template_directory() . '/' . $file;
166
			} elseif( file_exists( get_template_directory() . '/woocommerce/' . $file ) ) {
167
				$theme_file = get_template_directory() . '/woocommerce/' . $file;
168
			}
169
170
			if ( $theme_file !== false ) {
171
				$core_version  = WC_Admin_Status::get_file_version( WC()->plugin_path() . '/templates/' . $file );
172
				$theme_version = WC_Admin_Status::get_file_version( $theme_file );
173
174
				if ( $core_version && $theme_version && version_compare( $theme_version, $core_version, '<' ) ) {
175
					$outdated = true;
176
					break;
177
				}
178
			}
179
		}
180
181
		if ( $outdated ) {
182
			include( 'views/html-notice-template-check.php' );
183
		} else {
184
			self::remove_notice( 'template_files' );
185
		}
186
	}
187
188
	/**
189
	 * Show a notice asking users to convert to shipping zones.
190
	 */
191
	public function legacy_shipping_notice() {
192
		$maybe_load_legacy_methods = array( 'flat_rate', 'free_shipping', 'international_delivery', 'local_delivery', 'local_pickup' );
193
		$enabled                   = false;
194
195
		foreach ( $maybe_load_legacy_methods as $method ) {
196
			$options = get_option( 'woocommerce_' . $method . '_settings' );
197
			if ( $options && isset( $options['enabled'] ) && 'yes' === $options['enabled'] ) {
198
				$enabled = true;
199
			}
200
		}
201
202
		if ( $enabled ) {
203
			include( 'views/html-notice-legacy-shipping.php' );
204
		} else {
205
			self::remove_notice( 'template_files' );
206
		}
207
	}
208
209
	/**
210
	 * No shipping methods.
211
	 */
212
	public function no_shipping_methods_notice() {
213
		if ( wc_shipping_enabled() && ( empty( $_GET['page'] ) || empty( $_GET['tab'] ) || 'wc-settings' !== $_GET['page'] || 'shipping' !== $_GET['tab'] ) ) {
214
			global $wpdb;
215
216
			$product_count = wp_count_posts( 'product' );
217
			$method_count  = absint( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_shipping_zone_methods" ) );
218
219
			if ( $product_count->publish > 0 && 0 === $method_count ) {
220
				include( 'views/html-notice-no-shipping-methods.php' );
221
			}
222
223
			if ( $method_count > 0 ) {
224
				self::remove_notice( 'no_shipping_methods' );
225
			}
226
		}
227
	}
228
}
229
230
new WC_Admin_Notices();
231