Completed
Branch dev (58959f)
by
unknown
02:36
created

auto-load-next-post.php ➔ run_auto_load_next_post()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 2
b 1
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/*
3
 * Plugin Name:       Auto Load Next Post
4
 * Plugin URI:        https://autoloadnextpost.com
5
 * Description:       Auto loads the next post as you scroll down to the end of a post. Replaces the URL in the address bar and the page title when viewing the next post.
6
 * Version:           1.4.7
7
 * Author:            Sébastien Dumont
8
 * Author URI:        https://sebastiendumont.com
9
 * License:           GPL-2.0+
10
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
11
 * Text Domain:       auto-load-next-post
12
 * Domain Path:       languages
13
 * Network:           false
14
 * GitHub Plugin URI: https://github.com/seb86/Auto-Load-Next-Post
15
 *
16
 * Auto Load Next Post is distributed under the terms of the
17
 * GNU General Public License as published by the Free Software Foundation,
18
 * either version 2 of the License, or any later version.
19
 *
20
 * Auto Load Next Post is distributed in the hope that it will
21
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
 * GNU General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU General Public License
26
 * along with Auto Load Next Post.
27
 * If not, see <http://www.gnu.org/licenses/>.
28
 *
29
 * @package Auto_Load_Next_Post
30
 * @author  Sébastien Dumont
31
 */
32
if ( ! defined('ABSPATH')) {
33
	exit;
34
}
35
// Exit if accessed directly
36
37
if ( ! class_exists('Auto_Load_Next_Post')) {
38
39
/**
40
 * Main Auto Load Next Post Class
41
 *
42
 * @since 1.0.0
43
 */
44
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...
45
46
	/**
47
	 * The single instance of the class
48
	 *
49
	 * @since  1.0.0
50
	 * @access private
51
	 * @var    object
52
	 */
53
	private static $_instance = null;
54
55
	/**
56
	 * Main Auto Load Next Post Instance
57
	 *
58
	 * Ensures only one instance of Auto Load Next Post is loaded or can be loaded.
59
	 *
60
	 * @since  1.0.0
61
	 * @access public static
62
	 * @see    Auto_Load_Next_Post()
63
	 * @return Auto Load Next Post instance
64
	 */
65
	public static function instance() {
66
		if (is_null(self::$_instance)) {
67
			self::$_instance = new Auto_Load_Next_Post;
68
			self::$_instance->setup_constants();
69
			self::$_instance->load_plugin_textdomain();
70
			self::$_instance->includes();
71
		}
72
		return self::$_instance;
73
	} // END instance()
74
75
	/**
76
	 * Throw error on object clone
77
	 *
78
	 * The whole idea of the singleton design pattern is that there is a single
79
	 * object therefore, we don't want the object to be cloned.
80
	 *
81
	 * @since  1.0.0
82
	 * @access public
83
	 * @return void
84
	 */
85
	public function __clone() {
86
		// Cloning instances of the class is forbidden
87
		_doing_it_wrong(__FUNCTION__, __('Cheatin’ huh?', 'auto-load-next-post'), AUTO_LOAD_NEXT_POST_VERSION);
88
	} // END __clone()
89
90
	/**
91
	 * Disable unserializing of the class
92
	 *
93
	 * @since  1.0.0
94
	 * @access public
95
	 * @return void
96
	 */
97
	public function __wakeup() {
98
		// Unserializing instances of the class is forbidden
99
		_doing_it_wrong(__FUNCTION__, __('Cheatin’ huh?', 'auto-load-next-post'), AUTO_LOAD_NEXT_POST_VERSION);
100
	} // END __wakeup()
101
102
	/**
103
	 * Constructor
104
	 *
105
	 * @since  1.0.0
106
	 * @access public
107
	 */
108
	public function __construct() {
109
		// Auto-load classes on demand
110
		if (function_exists("__autoload")) {
111
			spl_autoload_register("__autoload");
112
		}
113
114
		spl_autoload_register(array($this, 'autoload'));
115
116
		// Hooks
117
		add_action('init', array($this, 'init_auto_load_next_post'), 0);
118
		add_action('wp_enqueue_scripts', array($this, 'front_scripts_and_styles'));
119
	} // END __construct()
120
121
	/**
122
	 * Auto-load Auto Load Next Post classes on demand to reduce memory consumption.
123
	 *
124
	 * @since  1.0.0
125
	 * @access public
126
	 * @param  mixed $class
127
	 * @return void
128
	 */
129
	public function autoload($class) {
130
		$path  = null;
131
		$file  = strtolower('class-'.str_replace('_', '-', $class)).'.php';
132
133
		if (strpos($class, 'auto_load_next_post_admin') === 0) {
134
			$path = AUTO_LOAD_NEXT_POST_FILE_PATH.'/includes/admin/';
135
		} else if (strpos($class, 'auto_load_next_post_') === 0) {
136
			$path = AUTO_LOAD_NEXT_POST_FILE_PATH.'/includes/';
137
		}
138
139
		if ($path !== null && is_readable($path.$file)) {
140
			include_once($path.$file);
141
			return true;
142
		}
143
	} // END autoload()
144
145
	/**
146
	 * Setup Constants
147
	 *
148
	 * @since  1.4.3
149
	 * @access private
150
	 */
151
	private function setup_constants() {
152
		$this->define('AUTO_LOAD_NEXT_POST_VERSION', '1.4.7');
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
		$this->define('AUTO_LOAD_NEXT_POST_SCRIPT_MODE', $suffix);
164
	} // END setup_constants()
165
166
	/**
167
	 * Define constant if not already set.
168
	 *
169
	 * @param  string $name
170
	 * @param  string|bool $value
171
	 * @access private
172
	 * @since  1.4.3
173
	 */
174
	private function define($name, $value) {
175
		if ( ! defined($name)) {
176
			define($name, $value);
177
		}
178
	}
179
180
	/**
181
	 * Include required core files used in admin and on the frontend.
182
	 *
183
	 * @since  1.0.0
184
	 * @access public
185
	 * @return void
186
	 */
187
	public function includes() {
188
		include_once('includes/auto-load-next-post-core-functions.php'); // Contains core functions for the front/back end.
189
190
		if (is_admin()) {
191
			include_once('includes/admin/class-auto-load-next-post-admin.php'); // Admin section
192
		}
193
	} // END includes()
194
195
	/**
196
	 * Runs when the plugin is initialized.
197
	 *
198
	 * @since  1.0.0
199
	 * @access public
200
	 */
201
	public function init_auto_load_next_post() {
202
		add_rewrite_endpoint('partial', EP_PERMALINK);
203
204
		// Refresh permalinks
205
		flush_rewrite_rules();
206
	} // END init_auto_load_next_post()
207
208
	/**
209
	 * Load Localisation files.
210
	 *
211
	 * Note: the first-loaded translation file overrides any
212
	 * following ones if the same translation is present.
213
	 *
214
	 * @since  1.0.0
215
	 * @access public
216
	 * @filter auto_load_next_post_languages_directory
217
	 * @filter plugin_locale
218
	 * @return void
219
	 */
220
	public function load_plugin_textdomain() {
221
		// Set filter for plugin's languages directory
222
		$lang_dir = dirname(plugin_basename(AUTO_LOAD_NEXT_POST_FILE)).'/languages/';
223
		$lang_dir = apply_filters('auto_load_next_post_languages_directory', $lang_dir);
224
225
		// Traditional WordPress plugin locale filter
226
		$locale = apply_filters('plugin_locale', get_locale(), 'auto-load-next-post');
227
		$mofile = sprintf('%1$s-%2$s.mo', 'auto-load-next-post', $locale);
228
229
		// Setup paths to current locale file
230
		$mofile_local  = $lang_dir.$mofile;
231
		$mofile_global = WP_LANG_DIR.'/auto-load-next-post/'.$mofile;
232
233
		if (file_exists($mofile_global)) {
234
			// Look in global /wp-content/languages/auto-load-next-post/ folder
235
			load_textdomain('auto-load-next-post', $mofile_global);
236
		} else if (file_exists($mofile_local)) {
237
			// Look in local /wp-content/plugins/auto-load-next-post/languages/ folder
238
			load_textdomain('auto-load-next-post', $mofile_local);
239
		} else {
240
			// Load the default language files
241
			load_plugin_textdomain('auto-load-next-post', false, $lang_dir);
242
		}
243
	} // END load_plugin_textdomain()
244
245
	/**
246
	 * Registers and enqueues stylesheets and javascripts for the front of the site.
247
	 *
248
	 * @since  1.3.2
249
	 * @access public
250
	 */
251
	public function front_scripts_and_styles() {
252
		/**
253
		 * Load the Javascript if found as a singluar post.
254
		 */
255
		if (supports_alnp() && is_singular() && get_post_type() == 'post') {
256
			$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);
257
			$this->load_file('auto-load-next-post-history', '/assets/js/libs/jquery.history.js', true, array('jquery'), AUTO_LOAD_NEXT_POST_VERSION);
258
			$this->load_file('auto-load-next-post-script', '/assets/js/frontend/auto-load-next-post'.AUTO_LOAD_NEXT_POST_SCRIPT_MODE.'.js', true, array('auto-load-next-post-scrollspy'), AUTO_LOAD_NEXT_POST_VERSION);
259
260
			// Variables for JS scripts
261
			wp_localize_script('auto-load-next-post-script', 'auto_load_next_post_params', array(
262
				'alnp_content_container'    => get_option('auto_load_next_post_content_container'),
263
				'alnp_title_selector'       => get_option('auto_load_next_post_title_selector'),
264
				'alnp_navigation_container' => get_option('auto_load_next_post_navigation_container'),
265
				'alnp_comments_container'   => get_option('auto_load_next_post_comments_container'),
266
				'alnp_remove_comments'      => get_option('auto_load_next_post_remove_comments'),
267
				'alnp_google_analytics'     => get_option('auto_load_next_post_google_analytics'),
268
			));
269
		} // 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...
270
	} // END register_scripts_and_styles()
271
272
	/**
273
	 * Helper function for registering and enqueueing scripts and styles.
274
	 *
275
	 * @since  1.0.0
276
	 * @access public static
277
	 * @param  string  $name      The ID to register with WordPress.
278
	 * @param  string  $file_path The path to the actual file.
279
	 * @param  bool    $is_script Optional, argument for if the incoming file_path is a JavaScript source file.
280
	 * @param  array   $support   Optional, for requiring other javascripts for the source file you are calling.
281
	 * @param  string  $version   Optional, can match the version of the plugin or version of the source file.
282
	 * @global string  $wp_version
283
	 */
284
	public static function load_file($name, $file_path, $is_script = false, $support = array(), $version = '') {
285
		global $wp_version;
286
287
		$url = AUTO_LOAD_NEXT_POST_URL_PATH.$file_path; // URL to the file.
288
289
		if (file_exists(AUTO_LOAD_NEXT_POST_FILE_PATH.$file_path)) {
290
			if ($is_script) {
291
				wp_register_script($name, $url, $support, $version);
292
				wp_enqueue_script($name);
293
			} else {
294
				wp_register_style($name, $url);
295
				wp_enqueue_style($name);
296
			} // end if
297
		} // end if
298
299
	} // END load_file()
300
301
} // END Auto_Load_Next_Post()
302
303
} // END class exists 'Auto_Load_Next_Post'
304
305
/**
306
 * This runs the plugin if the required PHP version has been met.
307
 */
308
function run_auto_load_next_post() {
309
	return Auto_Load_Next_Post::instance();
310
} // END run_auto_load_next_post()
311
312
// Fetch the Php version checker.
313
if ( ! class_exists('WP_Update_Php')) {
314
	require_once('wp-update-php/wp-update-php.php');
315
}
316
$updatePhp = new WP_Update_Php(
317
	array(
318
		'name' => 'Auto Load Next Post',
319
		'textdomain' => 'auto-load-next-post'
320
	),
321
	array(
322
		'minimum_version' => '5.3.0',
323
		'recommended_version' => '5.4.7'
324
	)
325
);
326
327
// If the miniumum version of PHP required is available then run the plugin.
328
if ($updatePhp->does_it_meet_required_php_version()) {
329
	add_action('plugins_loaded', 'run_auto_load_next_post', 20);
330
}
331