Completed
Push — master ( 2be24c...e22d32 )
by Mike
09:49
created

WC_Admin_Notices::legacy_shipping_notice()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 17
rs 8.8571
cc 6
eloc 11
nc 6
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
	);
31
32
	/**
33
	 * Constructor.
34
	 */
35
	public function __construct() {
36
		add_action( 'switch_theme', array( $this, 'reset_admin_notices' ) );
37
		add_action( 'woocommerce_installed', array( $this, 'reset_admin_notices' ) );
38
		add_action( 'wp_loaded', array( $this, 'hide_notices' ) );
39
40
		if ( current_user_can( 'manage_woocommerce' ) ) {
41
			add_action( 'admin_print_styles', array( $this, 'add_notices' ) );
42
		}
43
	}
44
45
	/**
46
	 * Remove all notices.
47
	 */
48
	public static function remove_all_notices() {
49
		delete_option( 'woocommerce_admin_notices' );
50
	}
51
52
	/**
53
	 * Reset notices for themes when switched or a new version of WC is installed.
54
	 */
55
	public function reset_admin_notices() {
56 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...
57
			self::add_notice( 'theme_support' );
58
		}
59
		self::add_notice( 'template_files' );
60
	}
61
62
	/**
63
	 * Show a notice.
64
	 * @param  string $name
65
	 */
66
	public static function add_notice( $name ) {
67
		$notices = array_unique( array_merge( get_option( 'woocommerce_admin_notices', array() ), array( $name ) ) );
68
		update_option( 'woocommerce_admin_notices', $notices );
69
	}
70
71
	/**
72
	 * Remove a notice from being displayed.
73
	 * @param  string $name
74
	 */
75
	public static function remove_notice( $name ) {
76
		$notices = array_diff( get_option( 'woocommerce_admin_notices', array() ), array( $name ) );
77
		update_option( 'woocommerce_admin_notices', $notices );
78
	}
79
80
	/**
81
	 * See if a notice is being shown.
82
	 * @param  string  $name
83
	 * @return boolean
84
	 */
85
	public static function has_notice( $name ) {
86
		return in_array( $name, get_option( 'woocommerce_admin_notices', array() ) );
87
	}
88
89
	/**
90
	 * Hide a notice if the GET variable is set.
91
	 */
92
	public function hide_notices() {
93
		if ( isset( $_GET['wc-hide-notice'] ) && isset( $_GET['_wc_notice_nonce'] ) ) {
94
			if ( ! wp_verify_nonce( $_GET['_wc_notice_nonce'], 'woocommerce_hide_notices_nonce' ) ) {
95
				wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
96
			}
97
98
			if ( ! current_user_can( 'manage_woocommerce' ) ) {
99
				wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce' ) );
100
			}
101
102
			$hide_notice = sanitize_text_field( $_GET['wc-hide-notice'] );
103
			self::remove_notice( $hide_notice );
104
			do_action( 'woocommerce_hide_' . $hide_notice . '_notice' );
105
		}
106
	}
107
108
	/**
109
	 * Add notices + styles if needed.
110
	 */
111
	public function add_notices() {
112
		$notices = get_option( 'woocommerce_admin_notices', array() );
113
114
		if ( $notices ) {
115
			wp_enqueue_style( 'woocommerce-activation', plugins_url(  '/assets/css/activation.css', WC_PLUGIN_FILE ) );
116
			foreach ( $notices as $notice ) {
117
				if ( ! empty( $this->core_notices[ $notice ] ) && apply_filters( 'woocommerce_show_admin_notice', true, $notice ) ) {
118
					add_action( 'admin_notices', array( $this, $this->core_notices[ $notice ] ) );
119
				}
120
			}
121
		}
122
	}
123
124
	/**
125
	 * If we need to update, include a message with the update button.
126
	 */
127
	public function update_notice() {
128
		include( 'views/html-notice-update.php' );
129
	}
130
131
	/**
132
	 * If we have just installed, show a message with the install pages button.
133
	 */
134
	public function install_notice() {
135
		include( 'views/html-notice-install.php' );
136
	}
137
138
	/**
139
	 * Show the Theme Check notice.
140
	 */
141
	public function theme_check_notice() {
142 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...
143
			include( 'views/html-notice-theme-support.php' );
144
		} else {
145
			self::remove_notice( 'theme_support' );
146
		}
147
	}
148
149
	/**
150
	 * Show a notice highlighting bad template files.
151
	 */
152
	public function template_file_check_notice() {
153
		$core_templates = WC_Admin_Status::scan_template_files( WC()->plugin_path() . '/templates' );
154
		$outdated       = false;
155
156
		foreach ( $core_templates as $file ) {
157
158
			$theme_file = false;
159 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...
160
				$theme_file = get_stylesheet_directory() . '/' . $file;
161
			} elseif ( file_exists( get_stylesheet_directory() . '/woocommerce/' . $file ) ) {
162
				$theme_file = get_stylesheet_directory() . '/woocommerce/' . $file;
163
			} elseif ( file_exists( get_template_directory() . '/' . $file ) ) {
164
				$theme_file = get_template_directory() . '/' . $file;
165
			} elseif( file_exists( get_template_directory() . '/woocommerce/' . $file ) ) {
166
				$theme_file = get_template_directory() . '/woocommerce/' . $file;
167
			}
168
169
			if ( $theme_file !== false ) {
170
				$core_version  = WC_Admin_Status::get_file_version( WC()->plugin_path() . '/templates/' . $file );
171
				$theme_version = WC_Admin_Status::get_file_version( $theme_file );
172
173
				if ( $core_version && $theme_version && version_compare( $theme_version, $core_version, '<' ) ) {
174
					$outdated = true;
175
					break;
176
				}
177
			}
178
		}
179
180
		if ( $outdated ) {
181
			include( 'views/html-notice-template-check.php' );
182
		} else {
183
			self::remove_notice( 'template_files' );
184
		}
185
	}
186
187
	/**
188
	 * Show a notice asking users to convert to shipping zones.
189
	 */
190
	public function legacy_shipping_notice() {
191
		$maybe_load_legacy_methods = array( 'flat_rate', 'free_shipping', 'international_delivery', 'local_delivery', 'local_pickup' );
192
		$enabled                   = false;
193
194
		foreach ( $maybe_load_legacy_methods as $method ) {
195
			$options = get_option( 'woocommerce_' . $method . '_settings' );
196
			if ( $options && isset( $options['enabled'] ) && 'yes' === $options['enabled'] ) {
197
				$enabled = true;
198
			}
199
		}
200
201
		if ( $enabled ) {
202
			include( 'views/html-notice-legacy-shipping.php' );
203
		} else {
204
			self::remove_notice( 'template_files' );
205
		}
206
	}
207
}
208
209
new WC_Admin_Notices();
210