Completed
Pull Request — master (#133)
by Sébastien
01:51
created

Auto_Load_Next_Post::autoload()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 6
nop 1
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Auto_Load_Next_Post::init_hooks() 0 7 1
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.5.0-beta.1
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.5.0
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
		 * Plugin Version
45
		 *
46
		 * @access public
47
		 * @static
48
		 * @since  1.5.0
49
		 */
50
		public static $version = '1.5.0-beta.1';
51
52
		/**
53
		 * Main Auto Load Next Post Instance
54
		 *
55
		 * Ensures only one instance of Auto Load Next Post is loaded or can be loaded.
56
		 *
57
		 * @access public
58
		 * @since  1.0.0
59
		 * @static
60
		 * @see    Auto_Load_Next_Post()
61
		 * @return Auto_Load_Next_Post Single instance.
62
		 */
63
		public static function instance() {
64
			if ( is_null( self::$_instance ) ) {
65
				self::$_instance = new self();
66
			}
67
			return self::$_instance;
68
		} // END instance()
69
70
		/**
71
		 * Cloning is forbidden.
72
		 *
73
		 * @access public
74
		 * @since  1.0.0
75
		 * @return void
76
		 */
77
		public function __clone() {
78
			// Cloning instances of the class is forbidden
79
			_doing_it_wrong( __FUNCTION__, __( 'Cloning this object is forbidden.', 'auto-load-next-post' ), AUTO_LOAD_NEXT_POST_VERSION );
80
		} // END __clone()
81
82
		/**
83
		 * Unserializing instances of this class is forbidden.
84
		 *
85
		 * @access public
86
		 * @since  1.0.0
87
		 * @return void
88
		 */
89
		public function __wakeup() {
90
			_doing_it_wrong( __FUNCTION__, __( 'Unserializing instances of this class is forbidden.', 'auto-load-next-post' ), AUTO_LOAD_NEXT_POST_VERSION );
91
		} // END __wakeup()
92
93
		/**
94
		 * Auto_Load_Next_Post Constructor
95
		 *
96
		 * @access  public
97
		 * @since   1.0.0
98
		 * @version 1.5.0
99
		 * @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...
100
		 */
101
		public function __construct() {
102
			$this->setup_constants();
103
			$this->includes();
104
			$this->init_hooks();
105
106
			/**
107
			 * Auto Load Next Post is fully loaded.
108
			 */
109
			do_action( 'auto_load_next_post_loaded' );
110
		} // END __construct()
111
112
		/**
113
		 * Hooks into action hooks.
114
		 *
115
		 * @access public
116
		 * @since  1.5.0
117
		 */
118
		public function init_hooks() {
119
			// Load translation files.
120
			add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
121
122
			// Load Auto Load Next Post scripts on the frontend.
123
			add_action( 'wp_enqueue_scripts', array( $this, 'alnp_enqueue_scripts' ) );
124
		} // END init_hooks()
125
126
		/**
127
		 * Setup Constants
128
		 *
129
		 * @since   1.4.3
130
		 * @version 1.5.0
131
		 * @access  private
132
		 */
133
		private function setup_constants() {
134
			$this->define('AUTO_LOAD_NEXT_POST_VERSION', self::$version);
135
			$this->define('AUTO_LOAD_NEXT_POST_FILE', __FILE__);
136
			$this->define('AUTO_LOAD_NEXT_POST_SLUG', 'auto-load-next-post');
137
138
			$this->define('AUTO_LOAD_NEXT_POST_URL_PATH', untrailingslashit( plugins_url('/', __FILE__) ) );
139
			$this->define('AUTO_LOAD_NEXT_POST_FILE_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
140
			$this->define('AUTO_LOAD_NEXT_POST_TEMPLATE_PATH', 'auto-load-next-post/');
141
142
			$this->define('AUTO_LOAD_NEXT_POST_WP_VERSION_REQUIRE', '4.4');
143
144
			$suffix       = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
145
			$debug_suffix = defined('ALNP_DEV_DEBUG') && ALNP_DEV_DEBUG ? '.dev' : '';
146
147
			$this->define('AUTO_LOAD_NEXT_POST_SCRIPT_MODE', $suffix);
148
			$this->define('AUTO_LOAD_NEXT_POST_DEBUG_MODE', $debug_suffix);
149
		} // END setup_constants()
150
151
		/**
152
		 * Define constant if not already set.
153
		 *
154
		 * @param  string $name
155
		 * @param  string|bool $value
156
		 * @access private
157
		 * @since  1.4.3
158
		 */
159
		private function define( $name, $value ) {
160
			if ( ! defined( $name ) ) {
161
				define( $name, $value );
162
			}
163
		} // END define()
164
165
		/*-----------------------------------------------------------------------------------*/
166
		/*  Load Files                                                                       */
167
		/*-----------------------------------------------------------------------------------*/
168
169
		/**
170
		 * Include required core files used in admin and on the frontend.
171
		 *
172
		 * @access  public
173
		 * @since   1.0.0
174
		 * @version 1.5.0
175
		 * @return  void
176
		 */
177
		public function includes() {
178
			include_once( dirname( __FILE__ ) . '/includes/class-alnp-autoloader.php' ); // Autoloader.
179
			include_once( dirname( __FILE__ ) . '/includes/auto-load-next-post-conditional-functions.php' ); // Conditional functions.
180
			include_once( dirname( __FILE__ ) . '/includes/auto-load-next-post-formatting-functions.php' ); // Formatting functions.
181
			include_once( dirname( __FILE__ ) . '/includes/auto-load-next-post-core-functions.php' ); // Contains core functions for the front/back end.
182
183
			// Include theme support.
184
			$this->alnp_include_theme_support();
185
186
			// Customizer.
187
			include_once( dirname( __FILE__ ) . '/includes/customizer/class-alnp-customizer.php' );
188
			include_once( dirname( __FILE__ ) . '/includes/customizer/class-alnp-customizer-scripts.php' );
189
190
			// Include admin class to handle all back-end functions.
191
			if ( is_admin() ) {
192
				include_once( dirname( __FILE__ ) . '/includes/admin/class-alnp-admin.php' ); // Admin section.
193
			}
194
195
			// Install.
196
			require_once( dirname( __FILE__ ) . '/includes/class-alnp-install.php' );
197
		} // END includes()
198
199
		/**
200
		 * Include classes for theme support.
201
		 *
202
		 * @access public
203
		 * @since  1.5.0
204
		 */
205
		public function alnp_include_theme_support() {
206
			if ( is_alnp_active_theme( array( 'make', 'understrap', 'storefront', 'twentyseventeen', 'twentysixteen', 'twentyfifteen', 'twentyfourteen', 'twentythirteen', 'twentytwelve', 'twentyeleven', 'twentyten' ) ) ) {
207
208
				switch ( get_template() ) {
209
					case 'twentyten':
210
						include_once( dirname( __FILE__ ) . '/includes/theme-support/class-alnp-twenty-ten.php' );
211
						break;
212
					case 'twentyeleven':
213
						include_once( dirname( __FILE__ ) . '/includes/theme-support/class-alnp-twenty-eleven.php' );
214
						break;
215
					case 'twentytwelve':
216
						include_once( dirname( __FILE__ ) . '/includes/theme-support/class-alnp-twenty-twelve.php' );
217
						break;
218
					case 'twentythirteen':
219
						include_once( dirname( __FILE__ ) . '/includes/theme-support/class-alnp-twenty-thirteen.php' );
220
						break;
221
					case 'twentyfourteen':
222
						include_once( dirname( __FILE__ ) . '/includes/theme-support/class-alnp-twenty-fourteen.php' );
223
						break;
224
					case 'twentyfifteen':
225
						include_once( dirname( __FILE__ ) . '/includes/theme-support/class-alnp-twenty-fifteen.php' );
226
						break;
227
					case 'twentysixteen':
228
						include_once( dirname( __FILE__ ) . '/includes/theme-support/class-alnp-twenty-sixteen.php' );
229
						break;
230
					case 'twentyseventeen':
231
						include_once( dirname( __FILE__ ) . '/includes/theme-support/class-alnp-twenty-seventeen.php' );
232
						break;
233
					case 'storefront':
234
						include_once( dirname( __FILE__ ) . '/includes/theme-support/class-alnp-storefront.php' );
235
						break;
236
					case 'understrap':
237
						include_once( dirname( __FILE__ ) . '/includes/theme-support/class-alnp-understrap.php' );
238
						break;
239
					case 'make':
240
						include_once( dirname( __FILE__ ) . '/includes/theme-support/class-alnp-make.php' );
241
						break;
242
				} // END switch()
243
244
				include_once( dirname( __FILE__ ) . '/includes/theme-support/class-alnp-theme-support.php' );
245
246
			}
247
		} // END alnp_include_theme_support()
248
249
		/*-----------------------------------------------------------------------------------*/
250
		/*  Localization                                                                     */
251
		/*-----------------------------------------------------------------------------------*/
252
253
		/**
254
		 * Make the plugin translation ready.
255
		 *
256
		 * Translations should be added in the WordPress language directory:
257
		 *      - WP_LANG_DIR/plugins/auto-load-next-post-LOCALE.mo
258
		 *
259
		 * @access  public
260
		 * @since   1.0.0
261
		 * @version 1.4.10
262
		 * @return  void
263
		 */
264
		public function load_plugin_textdomain() {
265
			load_plugin_textdomain( 'auto-load-next-post', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
266
		} // END load_plugin_textdomain()
267
268
		/**
269
		 * Registers and enqueues stylesheets and javascripts for the front of the site.
270
		 *
271
		 * @access  public
272
		 * @since   1.3.2
273
		 * @version 1.5.0
274
		 */
275
		public function alnp_enqueue_scripts() {
276
			// Load the Javascript if found as a singluar post and the user is not a bot.
277
			if ( !alnp_is_bot() && is_singular() && get_post_type() == 'post' ) {
278
				// This helps the plugin decide to load the JavaScript in the footer or not.
279
				$load_in_footer = get_option( 'auto_load_next_post_js_footer' );
280
281
				$this->load_file( 'auto-load-next-post-scrollspy', '/assets/js/libs/scrollspy.min.js', true, array('jquery'), AUTO_LOAD_NEXT_POST_VERSION, $load_in_footer );
282
283
				// Only load History.js when not in the customizer.
284
				if ( ! is_customize_preview() ) {
285
					$this->load_file( 'auto-load-next-post-history', '/assets/js/libs/jquery.history.js', true, array('jquery'), AUTO_LOAD_NEXT_POST_VERSION, $load_in_footer );
286
				}
287
288
				$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, $load_in_footer );
289
290
				// Variables for the JavaScript
291
				wp_localize_script( 'auto-load-next-post-script', 'auto_load_next_post_params', array(
292
					'alnp_version'              => AUTO_LOAD_NEXT_POST_VERSION,
293
					'alnp_content_container'    => get_option( 'auto_load_next_post_content_container' ),
294
					'alnp_title_selector'       => get_option( 'auto_load_next_post_title_selector' ),
295
					'alnp_navigation_container' => get_option( 'auto_load_next_post_navigation_container' ),
296
					'alnp_comments_container'   => get_option( 'auto_load_next_post_comments_container' ),
297
					'alnp_remove_comments'      => get_option( 'auto_load_next_post_remove_comments' ),
298
					'alnp_google_analytics'     => get_option( 'auto_load_next_post_google_analytics' ),
299
					'alnp_is_customizer'        => $this->is_alnp_using_customizer(),
300
				) );
301
			} // END if is_singular() && get_post_type()
302
		} // END alnp_enqueue_scripts()
303
304
		/*-----------------------------------------------------------------------------------*/
305
		/*  Helper Functions                                                                 */
306
		/*-----------------------------------------------------------------------------------*/
307
308
		/**
309
		 * Checks if we are using the theme customizer.
310
		 *
311
		 * @access public
312
		 * @since  1.5.0
313
		 * @static
314
		 * @return string|boolean
315
		 */
316
		public static function is_alnp_using_customizer() {
317
			if ( is_customize_preview() ) {
318
				return "yes";
319
			}
320
321
			return false;
322
		} // END is_alnp_using_customizer()
323
324
		/**
325
		 * Helper function for registering and enqueueing scripts and styles.
326
		 *
327
		 * @access  public
328
		 * @since   1.0.0
329
		 * @version 1.5.0
330
		 * @static
331
		 * @param   string  $name      The ID to register with WordPress.
332
		 * @param   string  $file_path The path to the actual file.
333
		 * @param   bool    $is_script Optional, argument for if the incoming file_path is a JavaScript source file.
334
		 * @param   array   $support   Optional, for requiring other javascripts for the source file you are calling.
335
		 * @param   string  $version   Optional, can match the version of the plugin or version of the source file.
336
		 * @param   bool    $footer Optional, can set the JavaScript to load in the footer instead.
337
		 * @global  string  $wp_version
338
		 */
339
		public static function load_file( $name, $file_path, $is_script = false, $support = array(), $version = '', $footer = false ) {
340
			global $wp_version;
341
342
			$url = AUTO_LOAD_NEXT_POST_URL_PATH . $file_path; // URL to the file.
343
344
			if ( file_exists( AUTO_LOAD_NEXT_POST_FILE_PATH . $file_path ) ) {
345
				if ( $is_script ) {
346
					if ( !wp_script_is( $name, 'registered' ) ) {
347
						wp_register_script( $name, $url, $support, $version, $footer );
348
						wp_enqueue_script( $name );
349
					}
350
				} else {
351
					if ( !wp_style_is( $name, 'registered' ) ) {
352
						wp_register_style( $name, $url );
353
						wp_enqueue_style( $name );
354
					}
355
				} // end if
356
			} // end if
357
358
		} // END load_file()
359
360
	} // END Auto_Load_Next_Post()
361
362
} // END class exists 'Auto_Load_Next_Post'
363
364
return Auto_Load_Next_Post::instance();
365