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

Auto_Load_Next_Post::setup_constants()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 16
nop 0
dl 0
loc 17
rs 9.3888
c 0
b 0
f 0
1
<?php
2
/**
3
 * Plugin Name: Auto Load Next Post
4
 * Plugin URI:  https://autoloadnextpost.com
5
 * Description: Increase your pageviews on your site as readers continue reading your posts scrolling down the page.
6
 * Author:      Sébastien Dumont
7
 * Author URI:  https://sebastiendumont.com
8
 * Version:     1.4.12
9
 * Text Domain: auto-load-next-post
10
 * Domain Path: /languages/
11
 *
12
 * Copyright: © 2018 Sébastien Dumont, ([email protected])
13
 *
14
 * License: GNU General Public License v3.0
15
 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
16
 *
17
 * @package   Auto Load Next Post
18
 * @author    Sébastien Dumont
19
 * @copyright Copyright © 2018, Sébastien Dumont
20
 * @license   GNU General Public License v3.0 http://www.gnu.org/licenses/gpl-3.0.html
21
 */
22
23
/**
24
 * Main Auto Load Next Post Class
25
 *
26
 * The main instance of the plugin.
27
 *
28
 * @version 1.4.10
29
 */
30
if ( ! class_exists('Auto_Load_Next_Post') ) {
31
32
	class Auto_Load_Next_Post {
33
34
		/**
35
		 * @var Auto_Load_Next_Post - The single instance of the class
36
		 *
37
		 * @access protected
38
		 * @static
39
		 * @since  1.0.0
40
		 */
41
		protected static $_instance = null;
42
43
		/**
44
		 * Main Auto Load Next Post Instance
45
		 *
46
		 * Ensures only one instance of Auto Load Next Post is loaded or can be loaded.
47
		 *
48
		 * @access public
49
		 * @since  1.0.0
50
		 * @static
51
		 * @see    Auto_Load_Next_Post()
52
		 * @return Auto_Load_Next_Post Single instance.
53
		 */
54
		public static function instance() {
55
			if ( is_null( self::$_instance ) ) {
56
				self::$_instance = new self();
57
				self::$_instance->setup_constants();
58
				self::$_instance->includes();
59
			}
60
			return self::$_instance;
61
		} // END instance()
62
63
		/**
64
		 * Cloning is forbidden.
65
		 *
66
		 * @access public
67
		 * @since  1.0.0
68
		 * @return void
69
		 */
70
		public function __clone() {
71
			// Cloning instances of the class is forbidden
72
			_doing_it_wrong( __FUNCTION__, __( 'Cloning this object is forbidden.', 'auto-load-next-post' ), AUTO_LOAD_NEXT_POST_VERSION );
73
		} // END __clone()
74
75
		/**
76
		 * Unserializing instances of this class is forbidden.
77
		 *
78
		 * @access public
79
		 * @since  1.0.0
80
		 * @return void
81
		 */
82
		public function __wakeup() {
83
			_doing_it_wrong( __FUNCTION__, __( 'Unserializing instances of this class is forbidden.', 'auto-load-next-post' ), AUTO_LOAD_NEXT_POST_VERSION );
84
		} // END __wakeup()
85
86
		/**
87
		 * Auto_Load_Next_Post Constructor
88
		 *
89
		 * @access public
90
		 * @since  1.0.0
91
		 * @return Auto_Load_Next_Post
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
92
		 */
93
		public function __construct() {
94
			// Auto-load classes on demand
95
			if ( function_exists("__autoload") ) {
96
				spl_autoload_register("__autoload");
97
			}
98
99
			spl_autoload_register( array( $this, 'autoload' ) );
100
101
			// Load translation files.
102
			add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
103
104
			// Load Auto Load Next Post scripts on the frontend.
105
			add_action( 'wp_enqueue_scripts', array( $this, 'alnp_scripts' ) );
106
		} // END __construct()
107
108
		/**
109
		 * Auto-load Auto Load Next Post classes on demand to reduce memory consumption.
110
		 *
111
		 * @since  1.0.0
112
		 * @access public
113
		 * @param  mixed $class
114
		 * @return void
115
		 */
116
		public function autoload( $class ) {
117
			$path  = null;
118
			$file  = strtolower( 'class-' . str_replace( '_', '-', $class ) ) . '.php';
119
120
			if ( strpos( $class, 'auto_load_next_post_admin' ) === 0 ) {
121
				$path = AUTO_LOAD_NEXT_POST_FILE_PATH . '/includes/admin/';
122
			} else if ( strpos( $class, 'auto_load_next_post_' ) === 0 ) {
123
				$path = AUTO_LOAD_NEXT_POST_FILE_PATH . '/includes/';
124
			}
125
126
			if ( $path !== null && is_readable( $path . $file ) ) {
127
				include_once( $path . $file );
128
				return true;
129
			}
130
		} // END autoload()
131
132
		/**
133
		 * Setup Constants
134
		 *
135
		 * @since   1.4.3
136
		 * @version 1.4.10
137
		 * @access private
138
		 */
139
		private function setup_constants() {
140
			$this->define('AUTO_LOAD_NEXT_POST_VERSION', '1.4.10');
141
			$this->define('AUTO_LOAD_NEXT_POST_FILE', __FILE__);
142
			$this->define('AUTO_LOAD_NEXT_POST_SLUG', 'auto-load-next-post');
143
144
			$this->define('AUTO_LOAD_NEXT_POST_URL_PATH', untrailingslashit(plugins_url('/', __FILE__)));
145
			$this->define('AUTO_LOAD_NEXT_POST_FILE_PATH', untrailingslashit(plugin_dir_path(__FILE__)));
146
			$this->define('AUTO_LOAD_NEXT_POST_TEMPLATE_PATH', 'auto-load-next-post/');
147
148
			$this->define('AUTO_LOAD_NEXT_POST_WP_VERSION_REQUIRE', '4.3');
149
150
			$suffix       = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
151
			$debug_suffix = defined('ALNP_DEV_DEBUG') && ALNP_DEV_DEBUG ? '.dev' : '';
152
153
			$this->define('AUTO_LOAD_NEXT_POST_SCRIPT_MODE', $suffix);
154
			$this->define('AUTO_LOAD_NEXT_POST_DEBUG_MODE', $debug_suffix);
155
		} // END setup_constants()
156
157
		/**
158
		 * Define constant if not already set.
159
		 *
160
		 * @param  string $name
161
		 * @param  string|bool $value
162
		 * @access private
163
		 * @since  1.4.3
164
		 */
165
		private function define( $name, $value ) {
166
			if ( ! defined( $name ) ) {
167
				define( $name, $value );
168
			}
169
		} // END define()
170
171
		/*-----------------------------------------------------------------------------------*/
172
		/*  Load Files                                                                       */
173
		/*-----------------------------------------------------------------------------------*/
174
175
		/**
176
		 * Include required core files used in admin and on the frontend.
177
		 *
178
		 * @access  public
179
		 * @since   1.0.0
180
		 * @version 1.4.10
181
		 * @return  void
182
		 */
183
		public function includes() {
184
			include_once( dirname( __FILE__ ) . '/includes/auto-load-next-post-core-functions.php'); // Contains core functions for the front/back end.
185
186
			// Include admin class to handle all back-end functions.
187
			if ( is_admin() ) {
188
				include_once( dirname( __FILE__ ) . '/includes/admin/class-auto-load-next-post-admin.php'); // Admin section.
189
			}
190
191
			// Install.
192
			require_once( dirname( __FILE__ ) . '/includes/class-auto-load-next-post-install.php' );
193
194
			/**
195
			 * Auto Load Next Post is fully loaded.
196
			 */
197
			do_action( 'auto_load_next_post_loaded' );
198
		} // END includes()
199
200
		/**
201
		 * Make the plugin translation ready.
202
		 *
203
		 * Translations should be added in the WordPress language directory:
204
		 *      - WP_LANG_DIR/plugins/auto-load-next-post-LOCALE.mo
205
		 *
206
		 * @access  public
207
		 * @since   1.0.0
208
		 * @version 1.4.10
209
		 * @return  void
210
		 */
211
		public function load_plugin_textdomain() {
212
			load_plugin_textdomain( 'auto-load-next-post', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
213
		} // END load_plugin_textdomain()
214
215
		/**
216
		 * Registers and enqueues stylesheets and javascripts for the front of the site.
217
		 *
218
		 * @access  public
219
		 * @since   1.3.2
220
		 * @version 1.4.12
221
		 */
222
		public function alnp_scripts() {
223
			/**
224
			 * Load the Javascript if found as a singluar post.
225
			 */
226
			if ( is_singular() && get_post_type() == 'post' ) {
227
				$this->load_file( 'auto-load-next-post-scrollspy', '/assets/js/libs/scrollspy' . AUTO_LOAD_NEXT_POST_SCRIPT_MODE . '.js', true, array('jquery'), AUTO_LOAD_NEXT_POST_VERSION );
228
229
				// Only load History.js when not in the customizer.
230
				if ( ! is_customize_preview() ) {
231
					$this->load_file( 'auto-load-next-post-history', '/assets/js/libs/jquery.history.js', true, array('jquery'), AUTO_LOAD_NEXT_POST_VERSION );
232
				}
233
234
				$this->load_file( 'auto-load-next-post-script', '/assets/js/frontend/auto-load-next-post' . AUTO_LOAD_NEXT_POST_DEBUG_MODE.AUTO_LOAD_NEXT_POST_SCRIPT_MODE . '.js', true, array('auto-load-next-post-scrollspy'), AUTO_LOAD_NEXT_POST_VERSION );
235
236
				// Variables for JS scripts
237
				wp_localize_script( 'auto-load-next-post-script', 'auto_load_next_post_params', array(
238
					'alnp_version'              => AUTO_LOAD_NEXT_POST_VERSION,
239
					'alnp_content_container'    => get_option('auto_load_next_post_content_container'),
240
					'alnp_title_selector'       => get_option('auto_load_next_post_title_selector'),
241
					'alnp_navigation_container' => get_option('auto_load_next_post_navigation_container'),
242
					'alnp_comments_container'   => get_option('auto_load_next_post_comments_container'),
243
					'alnp_remove_comments'      => get_option('auto_load_next_post_remove_comments'),
244
					'alnp_google_analytics'     => get_option('auto_load_next_post_google_analytics'),
245
					'alnp_event_on_load'        => get_option('auto_load_next_post_on_load_event'),
246
					'alnp_event_on_entering'    => get_option('auto_load_next_post_on_entering_event'),
247
					'alnp_is_customizer'        => is_customize_preview()
248
				) );
249
			} // END if is_singular() && get_post_type()
250
		} // END alnp_scripts()
251
252
		/**
253
		 * Helper function for registering and enqueueing scripts and styles.
254
		 *
255
		 * @access public
256
		 * @since  1.0.0
257
		 * @static
258
		 * @param  string  $name      The ID to register with WordPress.
259
		 * @param  string  $file_path The path to the actual file.
260
		 * @param  bool    $is_script Optional, argument for if the incoming file_path is a JavaScript source file.
261
		 * @param  array   $support   Optional, for requiring other javascripts for the source file you are calling.
262
		 * @param  string  $version   Optional, can match the version of the plugin or version of the source file.
263
		 * @global string  $wp_version
264
		 */
265
		public static function load_file( $name, $file_path, $is_script = false, $support = array(), $version = '' ) {
266
			global $wp_version;
267
268
			$url = AUTO_LOAD_NEXT_POST_URL_PATH . $file_path; // URL to the file.
269
270
			if ( file_exists( AUTO_LOAD_NEXT_POST_FILE_PATH . $file_path ) ) {
271
				if ( $is_script ) {
272
					wp_register_script( $name, $url, $support, $version );
273
					wp_enqueue_script( $name );
274
				} else {
275
					wp_register_style( $name, $url );
276
					wp_enqueue_style( $name );
277
				} // end if
278
			} // end if
279
280
		} // END load_file()
281
282
	} // END Auto_Load_Next_Post()
283
284
} // END class exists 'Auto_Load_Next_Post'
285
286
/**
287
 * This runs the plugin if the required PHP version has been met.
288
 */
289
function run_auto_load_next_post() {
290
	return Auto_Load_Next_Post::instance();
291
} // END run_auto_load_next_post()
292
293
// Fetch the Php version checker.
294
if ( ! class_exists( 'WP_Update_Php' ) ) {
295
	require_once( dirname( __FILE__ ) . '/wp-update-php/wp-update-php.php' );
296
}
297
$updatePhp = new WP_Update_Php(
298
	array(
299
		'name' => 'Auto Load Next Post',
300
		'textdomain' => 'auto-load-next-post'
301
	),
302
	array(
303
		'minimum_version' => '5.6.0',
304
		'recommended_version' => '7.2'
305
	)
306
);
307
308
// If the miniumum version of PHP required is available then run the plugin.
309
if ( $updatePhp->does_it_meet_required_php_version() ) {
310
	add_action('plugins_loaded', 'run_auto_load_next_post', 20);
311
}
312