Completed
Branch master (c7ddc0)
by
unknown
02:02
created

Auto_Load_Next_Post_Admin_Notices::add_notices()   C

Complexity

Conditions 13
Paths 55

Size

Total Lines 53

Duplication

Lines 9
Ratio 16.98 %

Importance

Changes 0
Metric Value
cc 13
nc 55
nop 0
dl 9
loc 53
rs 6.6166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Display notices in the WordPress admin.
4
 *
5
 * @since    1.3.2
6
 * @version  1.5.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( 'Auto_Load_Next_Post_Admin_Notices' ) ) {
19
20
	class Auto_Load_Next_Post_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.0
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_notices', 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
		 * @global string $wp_version
58
		 * @return bool
59
		 */
60
		public function check_wp() {
61
			global $wp_version;
62
63
			if ( ! version_compare( $wp_version, AUTO_LOAD_NEXT_POST_WP_VERSION_REQUIRE, '>=' ) ) {
64
				add_action( 'admin_notices', array( $this, 'requirement_wp_notice' ) );
65
				return false;
66
			}
67
68
			return true;
69
		} // END check_wp()
70
71
		/**
72
		 * Don't bug the user if they don't want to see any notices.
73
		 *
74
		 * @access public
75
		 * @since  1.5.0
76
		 * @global $current_user
77
		 */
78
		public function dont_bug_me() {
79
			global $current_user;
80
81
			$user_hidden_notice = false;
82
83
			// If the user is allowed to install plugins and requested to hide the review notice then hide it for that user.
84 View Code Duplication
			if ( ! empty( $_GET['hide_auto_load_next_post_review_notice'] ) && current_user_can( 'install_plugins' ) ) {
85
				add_user_meta( $current_user->ID, 'auto_load_next_post_hide_review_notice', '1', true );
86
				$user_hidden_notice = true;
87
			}
88
89
			// If the user is allowed to install plugins and requested to hide the welcome notice then hide it for that user.
90 View Code Duplication
			if ( ! empty( $_GET['hide_auto_load_next_post_welcome_notice'] ) && current_user_can( 'install_plugins' ) ) {
91
				add_user_meta( $current_user->ID, 'auto_load_next_post_hide_welcome_notice', '1', true );
92
				$user_hidden_notice = true;
93
			}
94
95
			if ( ! empty( $_GET['hide_auto_load_next_post_beta_notice'] ) && current_user_can( 'install_plugins' ) ) {
96
				set_transient( 'alnp_beta_notice_hidden', 'hidden', WEEK_IN_SECONDS );
97
				$user_hidden_notice = true;
98
			}
99
100
			if ( $user_hidden_notice ) {
101
				// Redirect to the plugins page.
102
				wp_safe_redirect( admin_url( 'plugins.php' ) ); exit;
103
			}
104
		} // END dont_bug_me()
105
106
		/**
107
		 * Checks if the theme supports the plugin and display the plugin review
108
		 * notice after 7 days or more from the time the plugin was installed.
109
		 *
110
		 * @access  public
111
		 * @since   1.3.2
112
		 * @version 1.5.10
113
		 * @global  $current_user
114
		 */
115
		public function add_notices() {
116
			global $current_user;
117
118
			$template = get_option( 'template' );
119
120
			// Checks if the theme supports Auto Load Next Post and not provided via a plugin.
121
			if ( is_alnp_supported() ) {
122
				$plugin_supported = alnp_get_theme_support( 'plugin_support' );
123
124
				// Return if theme is supported via plugin.
125
				if ( ! empty( $plugin_supported ) && $plugin_supported == 'yes' ) {
126
					update_option( 'auto_load_next_post_theme_supported', $template );
127
					return false;
128
				}
129
130
				// If supported theme does not match active theme then show notice.
131
				if ( get_option( 'auto_load_next_post_theme_supported' ) !== $template ) {
132
					add_action( 'admin_notices', array( $this, 'theme_ready_notice' ) );
133
					update_option( 'auto_load_next_post_theme_supported', $template );
134
				}
135
			}
136
			else {
137
				// If theme not supported then delete option.
138
				delete_option( 'auto_load_next_post_theme_supported' );
139
			}
140
141
			// Is admin review notice hidden?
142
			$hide_review_notice = get_user_meta( $current_user->ID, 'auto_load_next_post_hide_review_notice', true );
143
144
			// Check if we need to display the review plugin notice.
145 View Code Duplication
			if ( current_user_can( 'install_plugins' ) && empty( $hide_review_notice ) ) {
146
				// If it has been a week or more since activating the plugin then display the review notice.
147
				if ( ( intval( time() - self::$install_date ) ) > WEEK_IN_SECONDS ) {
148
					add_action( 'admin_notices', array( $this, 'plugin_review_notice' ) );
149
				}
150
			}
151
152
			// Is admin welcome notice hidden?
153
			$hide_welcome_notice = get_user_meta( $current_user->ID, 'auto_load_next_post_hide_welcome_notice', true );
154
155
			// Check if we need to display the welcome notice.
156
			if ( current_user_can( 'install_plugins' ) && empty( $hide_welcome_notice ) ) {
157
				// If the user has just installed the plugin for the first time then welcome the user.
158 View Code Duplication
				if ( ( intval( time() - strtotime( self::$install_date ) ) / WEEK_IN_SECONDS ) % 52 <= 2 ) {
159
					add_action( 'admin_notices', array( $this, 'welcome_notice' ) );
160
				}
161
			}
162
163
			// Is this version of Auto Load Next Post a beta release?
164
			if ( is_alnp_beta() && empty( get_transient( 'alnp_beta_notice_hidden' ) ) ) {
165
				add_action( 'admin_notices', array( $this, 'beta_notice' ) );
166
			}
167
		} // END add_notices()
168
169
		/**
170
		 * Show the WordPress requirement notice.
171
		 *
172
		 * @access public
173
		 * @since  1.4.3
174
		 */
175
		public function requirement_wp_notice() {
176
			include( dirname( __FILE__ ) . '/views/html-notice-requirement-wp.php' );
177
		} // END requirement_wp_notice()
178
179
		/**
180
		 * Show the theme ready notice.
181
		 *
182
		 * @access public
183
		 * @since  1.5.0
184
		 */
185
		public function theme_ready_notice() {
186
			include( dirname( __FILE__ ) . '/views/html-notice-theme-ready.php' );
187
		} // END theme_ready_notice()
188
189
		/**
190
		 * Show the welcome notice.
191
		 *
192
		 * @access public
193
		 * @since  1.5.0
194
		 */
195
		public function welcome_notice() {
196
			include( dirname( __FILE__ ) . '/views/html-notice-welcome.php' );
197
		} // END welcome_notice()
198
199
		/**
200
		 * Show the beta notice.
201
		 *
202
		 * @access public
203
		 * @since  1.5.0
204
		 */
205
		public function beta_notice() {
206
			include( dirname( __FILE__ ) . '/views/html-notice-trying-beta.php' );
207
		} // END beta_notice()
208
209
		/**
210
		 * Show the plugin review notice.
211
		 *
212
		 * @access  public
213
		 * @since   1.4.4
214
		 * @version 1.4.10
215
		 */
216
		public function plugin_review_notice() {
217
			$install_date = self::$install_date;
218
219
			include( dirname( __FILE__ ) . '/views/html-notice-please-review.php' );
220
		} // END plugin_review_notice()
221
222
	} // END class.
223
224
} // END if class exists.
225
226
return new Auto_Load_Next_Post_Admin_Notices();
227