Completed
Branch master (8062bc)
by
unknown
03:42 queued 01:52
created

Auto_Load_Next_Post::setup_constants()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 16
nop 0
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Plugin Name: Auto Load Next Post
4
 * Plugin URI:  https://autoloadnextpost.com
5
 * Description: Gain more post views on your site as readers continue reading your posts scrolling down the page.
6
 * Version:     1.4.8
7
 * Author:      Sébastien Dumont
8
 * Author URI:  https://sebastiendumont.com
9
 *
10
 * Text Domain: auto-load-next-post
11
 * Domain Path: languages
12
 *
13
 * Auto Load Next Post is distributed under the terms of the
14
 * GNU General Public License as published by the Free Software Foundation,
15
 * either version 2 of the License, or any later version.
16
 *
17
 * Auto Load Next Post is distributed in the hope that it will
18
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with Auto Load Next Post.
24
 * If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 * @package  Auto_Load_Next_Post
27
 * @category Core
28
 * @author   Sébastien Dumont
29
 */
30
if ( ! defined('ABSPATH')) {
31
	exit; // Exit if accessed directly.
32
}
33
34
if ( ! class_exists('Auto_Load_Next_Post') ) {
35
36
/**
37
 * Main Auto Load Next Post Class
38
 *
39
 * @class   Auto_Load_Next_Post
40
 * @version 1.4.8
41
 */
42
final class Auto_Load_Next_Post {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
43
44
	/**
45
	 * The single instance of the class
46
	 *
47
	 * @since  1.0.0
48
	 * @access private
49
	 * @var    Auto_Load_Next_Post
50
	 */
51
	private static $_instance = null;
52
53
	/**
54
	 * Main Auto Load Next Post Instance
55
	 *
56
	 * Ensures only one instance of Auto Load Next Post is loaded or can be loaded.
57
	 *
58
	 * @since  1.0.0
59
	 * @access public
60
	 * @static
61
	 * @see    Auto_Load_Next_Post()
62
	 * @return Auto Load Next Post - Main instance.
63
	 */
64
	public static function instance() {
65
		if (is_null(self::$_instance)) {
66
			self::$_instance = new Auto_Load_Next_Post;
67
			self::$_instance->setup_constants();
68
			self::$_instance->load_plugin_textdomain();
69
			self::$_instance->includes();
70
		}
71
		return self::$_instance;
72
	} // END instance()
73
74
	/**
75
	 * Throw error on object clone
76
	 *
77
	 * The whole idea of the singleton design pattern is that there is a single
78
	 * object therefore, we don't want the object to be cloned.
79
	 *
80
	 * @since  1.0.0
81
	 * @access public
82
	 * @return void
83
	 */
84
	public function __clone() {
85
		// Cloning instances of the class is forbidden
86
		_doing_it_wrong(__FUNCTION__, __('Cheatin&#8217; huh?', 'auto-load-next-post'), AUTO_LOAD_NEXT_POST_VERSION);
87
	} // END __clone()
88
89
	/**
90
	 * Disable unserializing of the class
91
	 *
92
	 * @since  1.0.0
93
	 * @access public
94
	 * @return void
95
	 */
96
	public function __wakeup() {
97
		// Unserializing instances of the class is forbidden
98
		_doing_it_wrong(__FUNCTION__, __('Cheatin&#8217; huh?', 'auto-load-next-post'), AUTO_LOAD_NEXT_POST_VERSION);
99
	} // END __wakeup()
100
101
	/**
102
	 * Constructor
103
	 *
104
	 * @since  1.0.0
105
	 * @access public
106
	 */
107
	public function __construct() {
108
		// Auto-load classes on demand
109
		if (function_exists("__autoload")) {
110
			spl_autoload_register("__autoload");
111
		}
112
113
		spl_autoload_register(array($this, 'autoload'));
114
115
		// Hooks
116
		add_action('init', array($this, 'init_auto_load_next_post'), 0);
117
		add_action('wp_enqueue_scripts', array($this, 'front_scripts_and_styles'));
118
	} // END __construct()
119
120
	/**
121
	 * Auto-load Auto Load Next Post classes on demand to reduce memory consumption.
122
	 *
123
	 * @since  1.0.0
124
	 * @access public
125
	 * @param  mixed $class
126
	 * @return void
127
	 */
128
	public function autoload($class) {
129
		$path  = null;
130
		$file  = strtolower('class-'.str_replace('_', '-', $class)).'.php';
131
132
		if (strpos($class, 'auto_load_next_post_admin') === 0) {
133
			$path = AUTO_LOAD_NEXT_POST_FILE_PATH.'/includes/admin/';
134
		} else if (strpos($class, 'auto_load_next_post_') === 0) {
135
			$path = AUTO_LOAD_NEXT_POST_FILE_PATH.'/includes/';
136
		}
137
138
		if ($path !== null && is_readable($path.$file)) {
139
			include_once($path.$file);
140
			return true;
141
		}
142
	} // END autoload()
143
144
	/**
145
	 * Setup Constants
146
	 *
147
	 * @since   1.4.3
148
	 * @version 1.4.8
149
	 * @access private
150
	 */
151
	private function setup_constants() {
152
		$this->define('AUTO_LOAD_NEXT_POST_VERSION', '1.4.8');
153
		$this->define('AUTO_LOAD_NEXT_POST_FILE', __FILE__);
154
		$this->define('AUTO_LOAD_NEXT_POST_SLUG', 'auto-load-next-post');
155
156
		$this->define('AUTO_LOAD_NEXT_POST_URL_PATH', untrailingslashit(plugins_url('/', __FILE__)));
157
		$this->define('AUTO_LOAD_NEXT_POST_FILE_PATH', untrailingslashit(plugin_dir_path(__FILE__)));
158
		$this->define('AUTO_LOAD_NEXT_POST_TEMPLATE_PATH', 'auto-load-next-post/');
159
160
		$this->define('AUTO_LOAD_NEXT_POST_WP_VERSION_REQUIRE', '4.0');
161
162
		$suffix       = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
163
		$debug_suffix = defined('ALNP_DEV_DEBUG') && ALNP_DEV_DEBUG ? '.dev' : '';
164
165
		$this->define('AUTO_LOAD_NEXT_POST_SCRIPT_MODE', $suffix);
166
		$this->define('AUTO_LOAD_NEXT_POST_DEBUG_MODE', $debug_suffix);
167
	} // END setup_constants()
168
169
	/**
170
	 * Define constant if not already set.
171
	 *
172
	 * @param  string $name
173
	 * @param  string|bool $value
174
	 * @access private
175
	 * @since  1.4.3
176
	 */
177
	private function define($name, $value) {
178
		if ( ! defined($name)) {
179
			define($name, $value);
180
		}
181
	} // END define()
182
183
	/**
184
	 * Include required core files used in admin and on the frontend.
185
	 *
186
	 * @since  1.0.0
187
	 * @access public
188
	 * @return void
189
	 */
190
	public function includes() {
191
		include_once('includes/auto-load-next-post-core-functions.php'); // Contains core functions for the front/back end.
192
193
		if (is_admin()) {
194
			include_once('includes/admin/class-auto-load-next-post-admin.php'); // Admin section.
195
		}
196
	} // END includes()
197
198
	/**
199
	 * Runs when the plugin is initialized.
200
	 *
201
	 * @since  1.0.0
202
	 * @access public
203
	 */
204
	public function init_auto_load_next_post() {
205
		add_rewrite_endpoint('partial', EP_PERMALINK);
206
207
		// Refresh permalinks
208
		flush_rewrite_rules();
209
	} // END init_auto_load_next_post()
210
211
	/**
212
	 * Load Localisation files.
213
	 *
214
	 * Note: the first-loaded translation file overrides any following ones if the same translation is present.
215
	 *
216
	 * Locales found in:
217
	 *      - WP_LANG_DIR/auto-load-next-post/auto-load-next-post-LOCALE.mo
218
	 *      - WP_LANG_DIR/plugins/auto-load-next-post-LOCALE.mo
219
	 * @since   1.0.0
220
	 * @version 1.4.8
221
	 */
222
	public function load_plugin_textdomain() {
223
		$locale = apply_filters( 'plugin_locale', get_locale(), 'auto-load-next-post' );
224
		load_textdomain( 'auto-load-next-post', WP_LANG_DIR . '/auto-load-next-post/auto-load-next-post-' . $locale . '.mo' );
225
		load_plugin_textdomain( 'auto-load-next-post', false, plugin_basename( dirname( __FILE__ ) ) . '/languages' );
226
	} // END load_plugin_textdomain()
227
228
	/**
229
	 * Registers and enqueues stylesheets and javascripts for the front of the site.
230
	 *
231
	 * @since  1.3.2
232
	 * @access public
233
	 */
234
	public function front_scripts_and_styles() {
235
		/**
236
		 * Load the Javascript if found as a singluar post.
237
		 */
238
		if (supports_alnp() && is_singular() && get_post_type() == 'post') {
239
			$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);
240
			$this->load_file('auto-load-next-post-history', '/assets/js/libs/jquery.history.js', true, array('jquery'), AUTO_LOAD_NEXT_POST_VERSION);
241
			$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);
242
243
			// Variables for JS scripts
244
			wp_localize_script('auto-load-next-post-script', 'auto_load_next_post_params', array(
245
				'alnp_content_container'    => get_option('auto_load_next_post_content_container'),
246
				'alnp_title_selector'       => get_option('auto_load_next_post_title_selector'),
247
				'alnp_navigation_container' => get_option('auto_load_next_post_navigation_container'),
248
				'alnp_comments_container'   => get_option('auto_load_next_post_comments_container'),
249
				'alnp_remove_comments'      => get_option('auto_load_next_post_remove_comments'),
250
				'alnp_google_analytics'     => get_option('auto_load_next_post_google_analytics'),
251
			));
252
		} // END if is_singular() && get_post_type()
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
253
	} // END front_scripts_and_styles()
254
255
	/**
256
	 * Helper function for registering and enqueueing scripts and styles.
257
	 *
258
	 * @since  1.0.0
259
	 * @access public
260
	 * @static
261
	 * @param  string  $name      The ID to register with WordPress.
262
	 * @param  string  $file_path The path to the actual file.
263
	 * @param  bool    $is_script Optional, argument for if the incoming file_path is a JavaScript source file.
264
	 * @param  array   $support   Optional, for requiring other javascripts for the source file you are calling.
265
	 * @param  string  $version   Optional, can match the version of the plugin or version of the source file.
266
	 * @global string  $wp_version
267
	 */
268
	public static function load_file($name, $file_path, $is_script = false, $support = array(), $version = '') {
269
		global $wp_version;
270
271
		$url = AUTO_LOAD_NEXT_POST_URL_PATH.$file_path; // URL to the file.
272
273
		if (file_exists(AUTO_LOAD_NEXT_POST_FILE_PATH.$file_path)) {
274
			if ($is_script) {
275
				wp_register_script($name, $url, $support, $version);
276
				wp_enqueue_script($name);
277
			} else {
278
				wp_register_style($name, $url);
279
				wp_enqueue_style($name);
280
			} // end if
281
		} // end if
282
283
	} // END load_file()
284
285
} // END Auto_Load_Next_Post()
286
287
} // END class exists 'Auto_Load_Next_Post'
288
289
/**
290
 * This runs the plugin if the required PHP version has been met.
291
 */
292
function run_auto_load_next_post() {
293
	return Auto_Load_Next_Post::instance();
294
} // END run_auto_load_next_post()
295
296
// Fetch the Php version checker.
297
if ( ! class_exists('WP_Update_Php')) {
298
	require_once('wp-update-php/wp-update-php.php');
299
}
300
$updatePhp = new WP_Update_Php(
301
	array(
302
		'name' => 'Auto Load Next Post',
303
		'textdomain' => 'auto-load-next-post'
304
	),
305
	array(
306
		'minimum_version' => '5.3.0',
307
		'recommended_version' => '5.4.7'
308
	)
309
);
310
311
// If the miniumum version of PHP required is available then run the plugin.
312
if ($updatePhp->does_it_meet_required_php_version()) {
313
	add_action('plugins_loaded', 'run_auto_load_next_post', 20);
314
}
315