ALNP_Admin_Notices   A
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 250
Duplicated Lines 4.8 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 12
loc 250
rs 9.44
c 0
b 0
f 0
wmc 37
lcom 1
cbo 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A check_wp() 0 15 3
B dont_bug_me() 12 35 10
D add_notices() 0 72 17
A upgrade_warning() 0 3 1
A requirement_wp_notice() 0 3 1
A theme_ready_notice() 0 3 1
A setup_wizard_notice() 0 3 1
A beta_notice() 0 3 1
A plugin_review_notice() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Display notices in the WordPress admin.
4
 *
5
 * @since    1.3.2
6
 * @version  1.6.0
7
 * @author   Sébastien Dumont
8
 * @category Admin
9
 * @package  Auto Load Next Post/Admin/Notices
10
 * @license  GPL-2.0+
11
 */
12
13
// Exit if accessed directly.
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
if ( ! class_exists( 'ALNP_Admin_Notices' ) ) {
19
20
	class ALNP_Admin_Notices {
21
22
		/**
23
		 * Activation date.
24
		 *
25
		 * @access public
26
		 * @static
27
		 * @since  1.4.10
28
		 * @var    string
29
		 */
30
		public static $install_date;
31
32
		/**
33
		 * Constructor
34
		 *
35
		 * @access  public
36
		 * @since   1.3.2
37
		 * @version 1.5.11
38
		 */
39
		public function __construct() {
40
			self::$install_date = get_site_option( 'auto_load_next_post_install_date', time() );
41
42
			// Check WordPress enviroment.
43
			add_action( 'admin_init', array( $this, 'check_wp' ), 12 );
44
45
			// Don't bug the user if they don't want to see any notices.
46
			add_action( 'admin_init', array( $this, 'dont_bug_me' ), 15 );
47
48
			// Display other admin notices when required. All are dismissable.
49
			add_action( 'admin_print_styles', array( $this, 'add_notices' ), 0 );
50
		} // END __construct()
51
52
		/**
53
		 * Checks that the WordPress version meets the plugin requirement.
54
		 *
55
		 * @access  public
56
		 * @since   1.0.0
57
		 * @version 1.5.11
58
		 * @global  string $wp_version - The version of WordPress
59
		 * @return  bool
60
		 */
61
		public function check_wp() {
62
			global $wp_version;
63
64
			// If the current user can not install plugins then return nothing!
65
			if ( ! current_user_can( 'install_plugins' ) ) {
66
				return false;
67
			}
68
69
			if ( ! version_compare( $wp_version, AUTO_LOAD_NEXT_POST_WP_VERSION_REQUIRE, '>=' ) ) {
70
				add_action( 'admin_notices', array( $this, 'requirement_wp_notice' ) );
71
				return false;
72
			}
73
74
			return true;
75
		} // END check_wp()
76
77
		/**
78
		 * Don't bug the user if they don't want to see any notices.
79
		 *
80
		 * @access  public
81
		 * @since   1.5.0
82
		 * @version 1.6.0
83
		 * @global  $current_user
84
		 */
85
		public function dont_bug_me() {
86
			global $current_user;
87
88
			$user_hidden_notice = false;
89
90
			// If the user is allowed to install plugins and requested to hide the review notice then hide it for that user.
91 View Code Duplication
			if ( ! empty( $_GET['hide_auto_load_next_post_review_notice'] ) && current_user_can( 'install_plugins' ) ) {
92
				add_user_meta( $current_user->ID, 'auto_load_next_post_hide_review_notice', '1', true );
93
				$user_hidden_notice = true;
94
			}
95
96
			// If the user is allowed to install plugins and requested to hide the upgrade notice then hide it for that user.
97 View Code Duplication
			if ( ! empty( $_GET['hide_auto_load_next_post_upgrade_notice'] ) && current_user_can( 'install_plugins' ) ) {
98
				add_user_meta( $current_user->ID, 'auto_load_next_post_hide_upgrade_notice', '1', true );
99
				$user_hidden_notice = true;
100
			}
101
102
			// If the user is allowed to install plugins and requested to hide the setup notice then hide it for that user.
103 View Code Duplication
			if ( ! empty( $_GET['hide_auto_load_next_post_setup_notice'] ) && current_user_can( 'install_plugins' ) ) {
104
				add_user_meta( $current_user->ID, 'auto_load_next_post_hide_setup_notice', '1', true );
105
				$user_hidden_notice = true;
106
			}
107
108
			// If the user is allowed to install plugins and requested to hide the beta notice then hide it for that user.
109
			if ( ! empty( $_GET['hide_auto_load_next_post_beta_notice'] ) && current_user_can( 'install_plugins' ) ) {
110
				set_transient( 'alnp_beta_notice_hidden', 'hidden', WEEK_IN_SECONDS );
111
				$user_hidden_notice = true;
112
			}
113
114
			if ( $user_hidden_notice ) {
115
				// Redirect to the plugins page.
116
				wp_safe_redirect( admin_url( 'plugins.php' ) );
117
				exit;
118
			}
119
		} // END dont_bug_me()
120
121
		/**
122
		 * Checks if the theme supports the plugin and display the plugin review
123
		 * notice after 7 days or more from the time the plugin was installed.
124
		 *
125
		 * @access  public
126
		 * @since   1.3.2
127
		 * @version 1.6.0
128
		 * @global  $current_user
129
		 * @return  void|bool
130
		 */
131
		public function add_notices() {
132
			global $current_user;
133
134
			// If the current user can not install plugins then return nothing!
135
			if ( ! current_user_can( 'install_plugins' ) ) {
136
				return false;
137
			}
138
139
			$screen    = get_current_screen();
140
			$screen_id = $screen ? $screen->id : '';
141
142
			// Notices should only show on the main dashboard and on the plugins screen.
143
			if ( ! in_array( $screen_id, alnp_get_admin_screens() ) ) {
144
				return false;
145
			}
146
147
			// Is admin review notice hidden?
148
			$hide_review_notice = get_user_meta( $current_user->ID, 'auto_load_next_post_hide_review_notice', true );
149
150
			// Check if we need to display the review plugin notice.
151
			if ( empty( $hide_review_notice ) ) {
152
				// If it has been a week or more since activating the plugin then display the review notice.
153
				if ( ( intval( time() - self::$install_date ) ) > WEEK_IN_SECONDS ) {
154
					add_action( 'admin_notices', array( $this, 'plugin_review_notice' ) );
155
				}
156
			}
157
158
			// Is admin setup wizard notice hidden?
159
			$hide_setup_wizard_notice = get_user_meta( $current_user->ID, 'auto_load_next_post_hide_setup_notice', true );
160
161
			// Check if we need to show setup wizard notice.
162
			if ( empty( $hide_setup_wizard_notice ) && ! is_alnp_supported() ) {
163
				// Notify users of the Setup Wizard.
164
				add_action( 'admin_notices', array( $this, 'setup_wizard_notice' ) );
165
			}
166
167
			// Is this version of Auto Load Next Post a beta/pre-release?
168
			if ( is_alnp_beta() && empty( get_transient( 'alnp_beta_notice_hidden' ) ) ) {
169
				add_action( 'admin_notices', array( $this, 'beta_notice' ) );
170
			}
171
172
			$template = get_option( 'template' );
173
174
			// Checks if the theme supports Auto Load Next Post and not provided via a plugin.
175
			if ( is_alnp_supported() ) {
176
				$plugin_supported = alnp_get_theme_support( 'plugin_support' );
177
178
				// Return if theme is supported via plugin.
179
				if ( ! empty( $plugin_supported ) && $plugin_supported == 'yes' ) {
180
					update_option( 'auto_load_next_post_theme_supported', $template );
181
					return false;
182
				}
183
184
				// If supported theme does not match active theme then show notice.
185
				if ( get_option( 'auto_load_next_post_theme_supported' ) !== $template ) {
186
					add_action( 'admin_notices', array( $this, 'theme_ready_notice' ) );
187
					update_option( 'auto_load_next_post_theme_supported', $template );
188
				}
189
			} else {
190
				// If theme not supported then delete option.
191
				delete_option( 'auto_load_next_post_theme_supported' );
192
			}
193
194
			// Upgrade warning notice that will disappear once the new release is installed.
195
			$upgrade_version = '1.6.0';
196
197
			$user_hidden_upgrade = get_user_meta( $current_user->ID, 'auto_load_next_post_hide_upgrade_notice', '1' );
198
199
			if ( ! is_alnp_beta() && version_compare( AUTO_LOAD_NEXT_POST_VERSION, $upgrade_version, '<' ) && empty( $user_hidden_upgrade ) ) {
200
				add_action( 'admin_notices', array( $this, 'upgrade_warning' ) );
201
			}
202
		} // END add_notices()
203
204
		/**
205
		 * Shows an upgrade warning notice if the installed version is less
206
		 * than the new release coming soon.
207
		 *
208
		 * @access  public
209
		 * @since   1.4.13
210
		 * @version 1.5.13
211
		 */
212
		public function upgrade_warning() {
213
			include_once( dirname( __FILE__ ) . '/views/html-notice-upgrade-warning.php' );
214
		} // END upgrade_warning()
215
216
		/**
217
		 * Show the WordPress requirement notice.
218
		 *
219
		 * @access public
220
		 * @since  1.4.3
221
		 */
222
		public function requirement_wp_notice() {
223
			include( dirname( __FILE__ ) . '/views/html-notice-requirement-wp.php' );
224
		} // END requirement_wp_notice()
225
226
		/**
227
		 * Show the theme ready notice.
228
		 *
229
		 * @access public
230
		 * @since  1.5.0
231
		 */
232
		public function theme_ready_notice() {
233
			include( dirname( __FILE__ ) . '/views/html-notice-theme-ready.php' );
234
		} // END theme_ready_notice()
235
236
		/**
237
		 * Show setup wizard notice.
238
		 *
239
		 * @access public
240
		 * @since  1.6.0
241
		 */
242
		public function setup_wizard_notice() {
243
			include( dirname( __FILE__ ) . '/views/html-notice-setup-wizard.php' );
244
		}
245
246
		/**
247
		 * Show the beta notice.
248
		 *
249
		 * @access public
250
		 * @since  1.5.0
251
		 */
252
		public function beta_notice() {
253
			include( dirname( __FILE__ ) . '/views/html-notice-trying-beta.php' );
254
		} // END beta_notice()
255
256
		/**
257
		 * Show the plugin review notice.
258
		 *
259
		 * @access  public
260
		 * @since   1.4.4
261
		 * @version 1.4.10
262
		 */
263
		public function plugin_review_notice() {
264
			$install_date = self::$install_date;
265
266
			include( dirname( __FILE__ ) . '/views/html-notice-please-review.php' );
267
		} // END plugin_review_notice()
268
269
	} // END class.
270
271
} // END if class exists.
272
273
return new ALNP_Admin_Notices();
274