Completed
Branch master (11b54f)
by Sébastien
01:41
created

Auto_Load_Next_Post::front_scripts_and_styles()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 2
nop 0
dl 0
loc 22
rs 8.9197
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
 * Version:     1.4.10
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.10
41
 */
42
final class Auto_Load_Next_Post {
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.10
149
	 * @access private
150
	 */
151
	private function setup_constants() {
152
		$this->define('AUTO_LOAD_NEXT_POST_VERSION', '1.4.10');
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.3');
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
	 * @version 1.4.10
233
	 * @access  public
234
	 */
235
	public function front_scripts_and_styles() {
236
		/**
237
		 * Load the Javascript if found as a singluar post.
238
		 */
239
		if (supports_alnp() && is_singular() && get_post_type() == 'post') {
240
			$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);
241
			$this->load_file('auto-load-next-post-history', '/assets/js/libs/jquery.history.js', true, array('jquery'), AUTO_LOAD_NEXT_POST_VERSION);
242
			$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);
243
244
			// Variables for JS scripts
245
			wp_localize_script('auto-load-next-post-script', 'auto_load_next_post_params', array(
246
				'alnp_version'              => AUTO_LOAD_NEXT_POST_VERSION,
247
				'alnp_content_container'    => get_option('auto_load_next_post_content_container'),
248
				'alnp_title_selector'       => get_option('auto_load_next_post_title_selector'),
249
				'alnp_navigation_container' => get_option('auto_load_next_post_navigation_container'),
250
				'alnp_comments_container'   => get_option('auto_load_next_post_comments_container'),
251
				'alnp_remove_comments'      => get_option('auto_load_next_post_remove_comments'),
252
				'alnp_google_analytics'     => get_option('auto_load_next_post_google_analytics'),
253
				'alnp_is_customizer'        => is_customize_preview()
254
			));
255
		} // END if is_singular() && get_post_type()
256
	} // END front_scripts_and_styles()
257
258
	/**
259
	 * Helper function for registering and enqueueing scripts and styles.
260
	 *
261
	 * @since  1.0.0
262
	 * @access public
263
	 * @static
264
	 * @param  string  $name      The ID to register with WordPress.
265
	 * @param  string  $file_path The path to the actual file.
266
	 * @param  bool    $is_script Optional, argument for if the incoming file_path is a JavaScript source file.
267
	 * @param  array   $support   Optional, for requiring other javascripts for the source file you are calling.
268
	 * @param  string  $version   Optional, can match the version of the plugin or version of the source file.
269
	 * @global string  $wp_version
270
	 */
271
	public static function load_file($name, $file_path, $is_script = false, $support = array(), $version = '') {
272
		global $wp_version;
273
274
		$url = AUTO_LOAD_NEXT_POST_URL_PATH.$file_path; // URL to the file.
275
276
		if (file_exists(AUTO_LOAD_NEXT_POST_FILE_PATH.$file_path)) {
277
			if ($is_script) {
278
				wp_register_script($name, $url, $support, $version);
279
				wp_enqueue_script($name);
280
			} else {
281
				wp_register_style($name, $url);
282
				wp_enqueue_style($name);
283
			} // end if
284
		} // end if
285
286
	} // END load_file()
287
288
} // END Auto_Load_Next_Post()
289
290
} // END class exists 'Auto_Load_Next_Post'
291
292
/**
293
 * This runs the plugin if the required PHP version has been met.
294
 */
295
function run_auto_load_next_post() {
296
	return Auto_Load_Next_Post::instance();
297
} // END run_auto_load_next_post()
298
299
// Fetch the Php version checker.
300
if ( ! class_exists('WP_Update_Php')) {
301
	require_once('wp-update-php/wp-update-php.php');
302
}
303
$updatePhp = new WP_Update_Php(
304
	array(
305
		'name' => 'Auto Load Next Post',
306
		'textdomain' => 'auto-load-next-post'
307
	),
308
	array(
309
		'minimum_version' => '5.3.0',
310
		'recommended_version' => '5.4.7'
311
	)
312
);
313
314
// If the miniumum version of PHP required is available then run the plugin.
315
if ($updatePhp->does_it_meet_required_php_version()) {
316
	add_action('plugins_loaded', 'run_auto_load_next_post', 20);
317
}
318