ALNP_Install::install()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 41
rs 9.264
c 0
b 0
f 0
1
<?php
2
/**
3
 * Auto Load Next Post - Installation related functions and actions.
4
 *
5
 * @since    1.0.0
6
 * @version  1.6.0
7
 * @author   Sébastien Dumont
8
 * @category Classes
9
 * @package  Auto Load Next Post/Classes/Install
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_Install' ) ) {
19
20
	class ALNP_Install {
21
22
		/**
23
		 * Constructor.
24
		 *
25
		 * @since   1.0.0
26
		 * @version 1.6.0
27
		 * @access  public
28
		 */
29
		public function __construct() {
30
			// Resets Auto Load Next Post settings when requested.
31
			add_action( 'init', array( __CLASS__, 'reset_alnp' ), 0 );
32
33
			// Checks version of Auto Load Next Post and install/update if needed.
34
			add_action( 'init', array( __CLASS__, 'check_version' ), 5 );
35
36
			// Adds rewrite endpoint.
37
			add_action( 'init', array( __CLASS__, 'add_rewrite_endpoint' ), 10 );
38
39
			// Redirect to Getting Started page once activated.
40
			add_action( 'activated_plugin', array( __CLASS__, 'redirect_getting_started') );
41
		} // END __construct()
42
43
		/**
44
		 * Check plugin version and run the updater if necessary.
45
		 *
46
		 * This check is done on all requests and runs if the versions do not match.
47
		 *
48
		 * @access  public
49
		 * @static
50
		 * @since   1.4.10
51
		 * @version 1.6.0
52
		 */
53
		public static function check_version() {
54
			// Check if we are not already running this routine.
55
			if ( 'yes' === get_transient( 'alnp_resetting' ) ) {
56
				return;
57
			}
58
59
			if ( ! defined( 'IFRAME_REQUEST' ) && version_compare( get_option( 'auto_load_next_post_version' ), AUTO_LOAD_NEXT_POST_VERSION, '<' ) && current_user_can( 'install_plugins' ) ) {
60
				self::install();
61
				do_action( 'auto_load_next_post_updated' );
62
			}
63
		} // END check_version()
64
65
		/**
66
		 * Install Auto Load Next Post.
67
		 *
68
		 * @access  public
69
		 * @static
70
		 * @since   1.0.0
71
		 * @version 1.6.0
72
		 */
73
		public static function install() {
74
			if ( ! is_blog_installed() ) {
75
				return;
76
			}
77
78
			// Check if we are not already running this routine.
79
			if ( 'yes' === get_transient( 'alnp_installing' ) ) {
80
				return;
81
			}
82
83
			// If we made it till here nothing is running yet, lets set the transient now for two minutes.
84
			set_transient( 'alnp_installing', 'yes', MINUTE_IN_SECONDS * 2 );
85
			if ( ! defined( 'AUTO_LOAD_NEXT_POST_INSTALLING' ) ) {
86
				define( 'AUTO_LOAD_NEXT_POST_INSTALLING', true );
87
			}
88
89
			// Add default options.
90
			self::create_options();
91
92
			// Set theme selectors if current active theme supports Auto Load Next Post.
93
			self::set_theme_selectors();
94
95
			// Sets ALNP to load in the footer if the current active theme requires it.
96
			self::set_js_in_footer();
97
98
			// Set the template directory if the current active theme requires it.
99
			self::set_template_directory();
100
101
			// Set activation date.
102
			self::set_install_date();
103
104
			// Update plugin version.
105
			self::update_version();
106
107
			// Refresh rewrite rules.
108
			self::flush_rewrite_rules();
109
110
			delete_transient( 'alnp_installing' );
111
112
			do_action( 'alnp_installed' );
113
		} // END install()
114
115
		/**
116
		 * Set theme selectors for the current active theme should it
117
		 * support Auto Load Next Post and have the theme selectors set.
118
		 *
119
		 * @access private
120
		 * @static
121
		 * @since  1.5.0
122
		 */
123
		private static function set_theme_selectors() {
124
			if ( is_alnp_supported() ) {
125
				$content_container    = alnp_get_theme_support( 'content_container' );
126
				$title_selector       = alnp_get_theme_support( 'title_selector' );
127
				$navigation_container = alnp_get_theme_support( 'navigation_container' );
128
				$comments_container   = alnp_get_theme_support( 'comments_container' );
129
130
				if ( ! empty( $content_container ) ) update_option( 'auto_load_next_post_content_container', $content_container );
131
				if ( ! empty( $title_selector ) ) update_option( 'auto_load_next_post_title_selector', $title_selector );
132
				if ( ! empty( $navigation_container ) ) update_option( 'auto_load_next_post_navigation_container', $navigation_container );
133
				if ( ! empty( $comments_container ) ) update_option( 'auto_load_next_post_comments_container', $comments_container );
134
			}
135
		} // END set_theme_selectors()
136
137
		/**
138
		 * Sets Auto Load Next Post to load in the footer if the 
139
		 * current active theme requires it and lock it so the 
140
		 * user can not disable it should the theme not work any other way.
141
		 *
142
		 * @access private
143
		 * @static
144
		 * @since   1.5.0
145
		 * @version 1.5.3
146
		 */
147
		private static function set_js_in_footer() {
148
			if ( is_alnp_supported() ) {
149
				$load_js_in_footer = alnp_get_theme_support( 'load_js_in_footer' );
150
				$lock_js_in_footer = alnp_get_theme_support( 'lock_js_in_footer' );
151
152
				if ( ! empty( $load_js_in_footer ) && $load_js_in_footer == 'yes' ) update_option( 'auto_load_next_post_load_js_in_footer', $load_js_in_footer );
153
				if ( ! empty( $lock_js_in_footer ) && $lock_js_in_footer == 'yes' ) update_option( 'auto_load_next_post_lock_js_in_footer', $lock_js_in_footer );
154
			}
155
		} // END set_js_in_footer()
156
157
		/**
158
		 * Sets the template directory for the current active theme should it
159
		 * support Auto Load Next Post and have the template directory specified.
160
		 *
161
		 * @access private
162
		 * @static
163
		 * @since  1.6.0
164
		 */
165
		private static function set_template_directory() {
166
			if ( is_alnp_supported() ) {
167
				$directory = alnp_get_theme_support( 'directory_post' );
168
169
				if ( ! empty( $directory ) ) update_option( 'auto_load_next_post_directory_post', $directory );
170
			}
171
		} // END set_template_directory()
172
173
		/**
174
		 * Update plugin version to current.
175
		 *
176
		 * @access private
177
		 * @static
178
		 */
179
		private static function update_version() {
180
			update_option( 'auto_load_next_post_version', AUTO_LOAD_NEXT_POST_VERSION );
181
		} // END update_version()
182
183
		/**
184
		 * Set the time the plugin was installed.
185
		 *
186
		 * @access  public
187
		 * @static
188
		 * @since   1.4.4
189
		 * @version 1.5.0
190
		 */
191
		public static function set_install_date() {
192
			$install_date = get_site_option( 'auto_load_next_post_install_date' );
193
194
			// If ALNP was installed before but the install date was not converted to time then convert it.
195
			if ( ! empty( $install_date ) && ! intval( $install_date ) ) {
196
				update_site_option( 'auto_load_next_post_install_date', strtotime( $install_date ) );
197
			} else {
198
				add_site_option( 'auto_load_next_post_install_date', time() );
199
			}
200
		} // END set_install_date()
201
202
		/**
203
		 * Default Options
204
		 *
205
		 * Sets up the default options defined on the settings pages.
206
		 *
207
		 * @access  public
208
		 * @static
209
		 * @since   1.0.0
210
		 * @version 1.5.1
211
		 */
212
		public static function create_options() {
213
			// Include settings so that we can run through defaults
214
			include_once( dirname( __FILE__ ) . '/admin/class-alnp-admin-settings.php' );
215
216
			$settings = ALNP_Admin_Settings::get_settings_pages();
217
218
			foreach ( $settings as $section ) {
219
				foreach ( $section->get_settings() as $value ) {
220
					if ( isset( $value['default'] ) && isset( $value['id'] ) ) {
221
						$autoload = isset( $value['autoload'] ) ? (bool) $value['autoload'] : true;
222
						add_option( $value['id'], $value['default'], '', ( $autoload ? 'yes' : 'no' ) );
223
					}
224
				}
225
			}
226
		} // END create_options()
227
228
		/**
229
		 * Add rewrite endpoint for Auto Load Next Post.
230
		 *
231
		 * @access  public
232
		 * @static
233
		 * @since   1.0.0
234
		 * @version 1.5.0
235
		 */
236
		public static function add_rewrite_endpoint() {
237
			add_rewrite_endpoint( 'alnp', EP_PERMALINK | EP_PAGES | EP_ATTACHMENT );
238
		} // END add_rewrite_endpoint()
239
240
		/**
241
		 * Flush rewrite rules.
242
		 *
243
		 * @access public
244
		 * @static
245
		 * @since  1.5.0
246
		 */
247
		public static function flush_rewrite_rules() {
248
			flush_rewrite_rules();
249
		} // END flush_rewrite_rules()
250
251
		/**
252
		 * Resets all Auto Load Next Post settings.
253
		 *
254
		 * @access  public
255
		 * @static
256
		 * @since   1.5.11
257
		 * @version 1.6.0
258
		 * @global  object $wpdb 
259
		 */
260
		public static function reset_alnp() {
261
			if ( current_user_can( 'install_plugins' ) && isset( $_GET['reset-alnp'] ) && $_GET['reset-alnp'] == 'yes' ) {
262
				global $wpdb;
263
264
				// If we made it till here nothing is running yet, lets set the transient now for two minutes.
265
				set_transient( 'alnp_resetting', 'yes', MINUTE_IN_SECONDS * 2 );
266
	
267
				// Make sure it is only a single site we are resetting.
268
				if ( ! is_multisite() ) {
269
					// Delete options
270
					$wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE 'auto_load_next_post_%'");
271
272
					// Delete user interactions
273
					$wpdb->query("DELETE FROM $wpdb->usermeta WHERE meta_key LIKE 'auto_load_next_post_%'");
274
				}
275
				else {
276
					// Delete Uninstall Data
277
					delete_site_option( 'auto_load_next_post_uninstall_data' );
278
279
					// Delete Install Date
280
					delete_site_option( 'auto_load_next_post_install_date' );
281
				}
282
283
				// Re-install Auto Load Next Post
284
				self::install();
285
286
				wp_safe_redirect( add_query_arg( array(
287
					'page'  => 'auto-load-next-post',
288
					'reset' => 'done'
289
				), admin_url( 'options-general.php' ) ) );
290
				exit;
291
			}
292
		} // END reset_alnp()
293
294
		/**
295
		 * Redirects to the Getting Started page upon plugin activation.
296
		 *
297
		 * @access public
298
		 * @static
299
		 * @since  1.6.0
300
		 * @param  string $plugin The activate plugin name.
301
		 */
302
		public static function redirect_getting_started( $plugin ) {
303
			// Prevent redirect if plugin name does not match.
304
			if ( $plugin !== plugin_basename( AUTO_LOAD_NEXT_POST_FILE ) ) {
305
				return;
306
			}
307
308
			$getting_started = add_query_arg( array(
309
				'page' => 'auto-load-next-post',
310
				'view' => 'getting-started'
311
			), admin_url( 'options-general.php' ) );
312
313
			/**
314
			 * Should Auto Load Next Post be installed via WP-CLI,
315
			 * display a link to the Getting Started page.
316
			 */
317
			if ( defined( 'WP_CLI' ) && WP_CLI ) {
318
				WP_CLI::log(
319
					WP_CLI::colorize(
320
						'%y' . sprintf( '🎉 %1$s %2$s', __( 'Get started with %3$s here:', 'auto-load-next-post' ), $getting_started, esc_html__( 'Auto Load Next Post', 'auto-load-next-post' ) ) . '%n'
321
					)
322
				);
323
				return;
324
			}
325
326
			// If activated on a Multisite, don't redirect.
327
			if ( is_multisite() ) {
328
				return;
329
			}
330
331
			wp_safe_redirect( $getting_started );
332
			exit;
333
		} // END redirect_getting_started()
334
	} // END class.
335
336
} // END if class exists.
337
338
return new ALNP_Install();
339