Completed
Branch master (ea5e46)
by
unknown
02:11
created

Auto_Load_Next_Post_Install::reset_alnp()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 36

Duplication

Lines 20
Ratio 55.56 %

Importance

Changes 0
Metric Value
cc 5
nc 3
nop 0
dl 20
loc 36
rs 9.0328
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.5.11
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( 'Auto_Load_Next_Post_Install' ) ) {
19
20
	class Auto_Load_Next_Post_Install {
21
22
		/**
23
		 * Plugin version.
24
		 *
25
		 * @access private
26
		 * @static
27
		 * @since 1.4.10
28
		 * @var string
29
		 */
30
		private static $current_version;
31
32
		/**
33
		 * Constructor.
34
		 *
35
		 * @since  1.0.0
36
		 * @access public
37
		 */
38
		public function __construct() {
39
			// Resets Auto Load Next Post settings when requested.
40
			add_action( 'init', array( __CLASS__, 'reset_alnp' ), 0 );
41
42
			// Checks version of Auto Load Next Post and install/update if needed.
43
			add_action( 'init', array( __CLASS__, 'check_version' ), 5 );
44
45
			// Adds rewrite endpoint.
46
			add_action( 'init', array( __CLASS__, 'add_rewrite_endpoint' ), 10 );
47
48
			// Get plugin version.
49
			self::$current_version = get_option( 'auto_load_next_post_version' );
50
		} // END __construct()
51
52
		/**
53
		 * Check plugin version and run the updater if necessary.
54
		 *
55
		 * This check is done on all requests and runs if the versions do not match.
56
		 *
57
		 * @access  public
58
		 * @static
59
		 * @since   1.4.10
60
		 * @version 1.5.11
61
		 */
62
		public static function check_version() {
63
			if ( ! defined( 'IFRAME_REQUEST' ) && version_compare( self::$current_version, AUTO_LOAD_NEXT_POST_VERSION, '<' ) && current_user_can( 'install_plugins' ) ) {
64
				self::install();
65
				do_action( 'auto_load_next_post_updated' );
66
			}
67
		} // END check_version()
68
69
		/**
70
		 * Install Auto Load Next Post.
71
		 *
72
		 * @access  public
73
		 * @static
74
		 * @since   1.0.0
75
		 * @version 1.5.0
76
		 */
77
		public static function install() {
78
			if ( ! is_blog_installed() ) {
79
				return;
80
			}
81
82
			// Check if we are not already running this routine.
83
			if ( 'yes' === get_transient( 'alnp_installing' ) ) {
84
				return;
85
			}
86
87
			// If we made it till here nothing is running yet, lets set the transient now for five minutes.
88
			set_transient( 'alnp_installing', 'yes', MINUTE_IN_SECONDS * 5 );
89
			if ( ! defined( 'AUTO_LOAD_NEXT_POST_INSTALLING' ) ) {
90
				define( 'AUTO_LOAD_NEXT_POST_INSTALLING', true );
91
			}
92
93
			// Add default options.
94
			self::create_options();
95
96
			// Set theme selectors if current active theme supports Auto Load Next Post.
97
			self::set_theme_selectors();
98
99
			// Sets ALNP to load in the footer if the current active theme requires it.
100
			self::set_js_in_footer();
101
102
			// Set activation date.
103
			self::set_install_date();
104
105
			// Update plugin version.
106
			self::update_version();
107
108
			// Refresh rewrite rules.
109
			self::flush_rewrite_rules();
110
111
			delete_transient( 'alnp_installing' );
112
113
			do_action( 'alnp_installed' );
114
		} // END install()
115
116
		/**
117
		 * Set theme selectors for the current active theme should it
118
		 * support Auto Load Next Post and have the theme selectors set.
119
		 *
120
		 * @access private
121
		 * @static
122
		 * @since  1.5.0
123
		 */
124
		private static function set_theme_selectors() {
125
			if ( is_alnp_supported() ) {
126
				$content_container    = alnp_get_theme_support( 'content_container' );
127
				$title_selector       = alnp_get_theme_support( 'title_selector' );
128
				$navigation_container = alnp_get_theme_support( 'navigation_container' );
129
				$comments_container   = alnp_get_theme_support( 'comments_container' );
130
131
				if ( ! empty( $content_container ) ) update_option( 'auto_load_next_post_content_container', $content_container );
132
				if ( ! empty( $title_selector ) ) update_option( 'auto_load_next_post_title_selector', $title_selector );
133
				if ( ! empty( $navigation_container ) ) update_option( 'auto_load_next_post_navigation_container', $navigation_container );
134
				if ( ! empty( $comments_container ) ) update_option( 'auto_load_next_post_comments_container', $comments_container );
135
			}
136
		} // END set_theme_selectors()
137
138
		/**
139
		 * Sets Auto Load Next Post to load in the footer if the 
140
		 * current active theme requires it and lock it so the 
141
		 * user can not disable it should the theme not work any other way.
142
		 *
143
		 * @access private
144
		 * @static
145
		 * @since   1.5.0
146
		 * @version 1.5.3
147
		 */
148
		private static function set_js_in_footer() {
149
			if ( is_alnp_supported() ) {
150
				$load_js_in_footer = alnp_get_theme_support( 'load_js_in_footer' );
151
				$lock_js_in_footer = alnp_get_theme_support( 'lock_js_in_footer' );
152
153
				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 );
154
				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 );
155
			}
156
		} // END set_js_in_footer()
157
158
		/**
159
		 * Update plugin version to current.
160
		 *
161
		 * @access private
162
		 * @static
163
		 */
164
		private static function update_version() {
165
			update_option( 'auto_load_next_post_version', AUTO_LOAD_NEXT_POST_VERSION );
166
		} // END update_version()
167
168
		/**
169
		 * Set the time the plugin was installed.
170
		 *
171
		 * @access  public
172
		 * @static
173
		 * @since   1.4.4
174
		 * @version 1.5.0
175
		 */
176
		public static function set_install_date() {
177
			$install_date = get_site_option( 'auto_load_next_post_install_date' );
178
179
			// If ALNP was installed before but the install date was not converted to time then convert it.
180
			if ( ! empty( $install_date ) && !intval( $install_date ) ) {
181
				update_site_option( 'auto_load_next_post_install_date', strtotime( $install_date ) );
182
			} else {
183
				add_site_option( 'auto_load_next_post_install_date', time() );
184
			}
185
		} // END set_install_date()
186
187
		/**
188
		 * Default Options
189
		 *
190
		 * Sets up the default options defined on the settings pages.
191
		 *
192
		 * @access  public
193
		 * @static
194
		 * @since   1.0.0
195
		 * @version 1.5.1
196
		 */
197
		public static function create_options() {
198
			// Include settings so that we can run through defaults
199
			include_once( dirname( __FILE__ ) . '/admin/class-alnp-admin-settings.php' );
200
201
			$settings = Auto_Load_Next_Post_Admin_Settings::get_settings_pages();
202
203
			foreach ( $settings as $section ) {
204
				foreach ( $section->get_settings() as $value ) {
205
					if ( isset( $value['default'] ) && isset( $value['id'] ) ) {
206
						$autoload = isset( $value['autoload'] ) ? (bool) $value['autoload'] : true;
207
						add_option( $value['id'], $value['default'], '', ( $autoload ? 'yes' : 'no' ) );
208
					}
209
				}
210
			}
211
		} // END create_options()
212
213
		/**
214
		 * Add rewrite endpoint for Auto Load Next Post.
215
		 *
216
		 * @access  public
217
		 * @static
218
		 * @since   1.0.0
219
		 * @version 1.5.0
220
		 */
221
		public static function add_rewrite_endpoint() {
222
			add_rewrite_endpoint( 'alnp', EP_PERMALINK | EP_PAGES | EP_ATTACHMENT );
223
		} // END add_rewrite_endpoint()
224
225
		/**
226
		 * Flush rewrite rules.
227
		 *
228
		 * @access public
229
		 * @static
230
		 * @since  1.5.0
231
		 */
232
		public static function flush_rewrite_rules() {
233
			flush_rewrite_rules();
234
		} // END flush_rewrite_rules()
235
236
		/**
237
		 * Resets all Auto Load Next Post settings.
238
		 *
239
		 * @access public
240
		 * @static
241
		 * @since  1.5.11
242
		 * @global object $wpdb 
243
		 */
244
		public static function reset_alnp() {
245
			if ( current_user_can( 'install_plugins' ) && isset( $_GET['reset-alnp'] ) && $_GET['reset-alnp'] == 'yes' ) {
246
				global $wpdb;
247
248
				// Make sure it is only a single site we are resetting.
249 View Code Duplication
				if ( ! is_multisite() ) {
250
					// Delete options
251
					$wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE 'auto_load_next_post_%'");
252
253
					// Delete user interactions
254
					$wpdb->query("DELETE FROM $wpdb->usermeta WHERE meta_key LIKE 'auto_load_next_post_%'");
255
256
					// Delete Uninstall Data - Just to double check it has been removed.
257
					delete_option( 'auto_load_next_post_uninstall_data' );
258
259
					// Delete Install Date
260
					delete_option( 'auto_load_next_post_install_date' );
261
				}
262
				else {
263
					// Delete Uninstall Data
264
					delete_site_option( 'auto_load_next_post_uninstall_data' );
265
266
					// Delete Install Date
267
					delete_site_option( 'auto_load_next_post_install_date' );
268
				}
269
270
				// Re-install Auto Load Next Post
271
				self::install();
272
273
				wp_safe_redirect( add_query_arg( array(
274
					'page'  => 'auto-load-next-post-settings',
275
					'reset' => 'done'
276
				), admin_url( 'options-general.php' ) ) );
277
				exit;
278
			}
279
		} // END reset_alnp()
280
281
	} // END class.
282
283
} // END if class exists.
284
285
return new Auto_Load_Next_Post_Install();
286