Completed
Pull Request — master (#131)
by
unknown
01:57
created

Auto_Load_Next_Post_Install   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 141
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A check_version() 0 6 3
A install() 0 29 4
A update_version() 0 3 1
A set_install_date() 0 3 1
B create_options() 0 15 7
A add_rewrite_endpoint() 0 6 1
1
<?php
2
/**
3
 * Auto Load Next Post - Installation related functions and actions.
4
 *
5
 * @class    Auto_Load_Next_Post_Install
6
 * @author   Sébastien Dumont
7
 * @category Classes
8
 * @package  Auto Load Next Post
9
 * @license  GPL-2.0+
10
 * @since    1.0.0
11
 * @version  1.4.10
12
 */
13
14
// Exit if accessed directly.
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
if ( ! class_exists( 'Auto_Load_Next_Post_Install' ) ) {
20
21
	class Auto_Load_Next_Post_Install {
22
23
		/**
24
		 * Plugin version.
25
		 *
26
		 * @access private
27
		 * @static
28
		 * @since 1.4.10
29
		 * @var string
30
		 */
31
		private static $current_version;
32
33
		/**
34
		 * Constructor.
35
		 *
36
		 * @since  1.0.0
37
		 * @access public
38
		 */
39
		public function __construct() {
40
			add_action( 'init', array( __CLASS__, 'add_rewrite_endpoint' ), 0 );
41
			add_action( 'init', array( __CLASS__, 'check_version' ), 5 );
42
43
			// Get plugin version.
44
			self::$current_version = get_option( 'auto_load_next_post_version', null );
45
		} // END __construct()
46
47
		/**
48
		 * Check plugin version and run the updater if necessary.
49
		 *
50
		 * This check is done on all requests and runs if the versions do not match.
51
		 *
52
		 * @access public
53
		 * @static
54
		 * @since  1.4.10
55
		 */
56
		public static function check_version() {
57
			if ( ! defined( 'IFRAME_REQUEST' ) && version_compare( self::$current_version, AUTO_LOAD_NEXT_POST_VERSION, '<' ) ) {
58
				self::install();
59
				do_action( 'auto_load_next_post_updated' );
60
			}
61
		} // END check_version()
62
63
		/**
64
		 * Install Auto Load Next Post.
65
		 *
66
		 * @access  public
67
		 * @static
68
		 * @since   1.0.0
69
		 * @version 1.4.10
70
		 */
71
		public static function install() {
72
			if ( ! is_blog_installed() ) {
73
				return;
74
			}
75
76
			// Check if we are not already running this routine.
77
			if ( 'yes' === get_transient( 'alnp_installing' ) ) {
78
				return;
79
			}
80
81
			// If we made it till here nothing is running yet, lets set the transient now for five minutes.
82
			set_transient( 'alnp_installing', 'yes', MINUTE_IN_SECONDS * 5 );
83
			if ( ! defined( 'AUTO_LOAD_NEXT_POST_INSTALLING' ) ) {
84
				define( 'AUTO_LOAD_NEXT_POST_INSTALLING', true );
85
			}
86
87
			// Add default options.
88
			self::create_options();
89
90
			// Update plugin version.
91
			self::update_version();
92
93
			// Set activation date.
94
			self::set_install_date();
95
96
			delete_transient( 'alnp_installing' );
97
98
			do_action( 'alnp_installed' );
99
		} // END install()
100
101
		/**
102
		 * Update plugin version to current.
103
		 *
104
		 * @access private
105
		 * @static
106
		 */
107
		private static function update_version() {
108
			update_option( 'auto_load_next_post_version', AUTO_LOAD_NEXT_POST_VERSION );
109
		} // END update_version()
110
111
		/**
112
		 * Set the time the plugin was installed.
113
		 *
114
		 * @access  public
115
		 * @static
116
		 * @since   1.4.4
117
		 * @version 1.4.10
118
		 */
119
		public static function set_install_date() {
120
			add_site_option( 'auto_load_next_post_install_date', time() );
121
		} // END set_install_date()
122
123
		/**
124
		 * Default Options
125
		 *
126
		 * Sets up the default options defined on the settings pages.
127
		 *
128
		 * @access public
129
		 * @static
130
		 * @since  1.0.0
131
		 */
132
		public static function create_options() {
133
			// Include settings so that we can run through defaults
134
			include_once( dirname( __FILE__ ) . '/admin/class-auto-load-next-post-admin-settings.php' );
135
136
			$settings = Auto_Load_Next_Post_Admin_Settings::get_settings_pages();
137
138
			foreach ( $settings as $section ) {
139
				foreach ( $section->get_settings() as $value ) {
140
					if ( isset( $value['default'] ) && isset( $value['id'] ) ) {
141
						$autoload = isset( $value['autoload'] ) ? (bool) $value['autoload'] : true;
142
						add_option( $value['id'], $value['default'], '', ( $autoload ? 'yes' : 'no' ) );
143
					}
144
				}
145
			}
146
		} // END create_options()
147
148
		/**
149
		 * Runs when the plugin is initialized.
150
		 *
151
		 * @access public
152
		 * @since  1.0.0
153
		 */
154
		public static function add_rewrite_endpoint() {
155
			add_rewrite_endpoint( 'partial', EP_PERMALINK );
156
157
			// Refresh permalinks
158
			flush_rewrite_rules();
159
		} // END add_rewrite_endpoint()
160
161
	} // END if class.
162
163
} // END if class exists.
164
165
return new Auto_Load_Next_Post_Install();
166