Completed
Pull Request — master (#165)
by Sébastien
01:34
created

auto-load-next-post.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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.11
9
 * Text Domain: auto-load-next-post
10
 * Domain Path: /languages/
11
 *
12
 * Copyright: © 2019 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 © 2019, 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
if ( ! class_exists( 'Auto_Load_Next_Post' ) ) {
29
30
	class Auto_Load_Next_Post {
31
32
		/**
33
		 * @var Auto_Load_Next_Post - The single instance of the class
34
		 *
35
		 * @access protected
36
		 * @static
37
		 * @since  1.0.0
38
		 */
39
		protected static $_instance = null;
40
41
		/**
42
		 * Plugin Version
43
		 *
44
		 * @access public
45
		 * @static
46
		 * @since  1.5.0
47
		 */
48
		public static $version = '1.5.11';
49
50
		/**
51
		 * Main Auto Load Next Post Instance
52
		 *
53
		 * Ensures only one instance of Auto Load Next Post is loaded or can be loaded.
54
		 *
55
		 * @access public
56
		 * @since  1.0.0
57
		 * @static
58
		 * @see    Auto_Load_Next_Post()
59
		 * @return Auto_Load_Next_Post Single instance.
60
		 */
61
		public static function instance() {
62
			if ( is_null( self::$_instance ) ) {
63
				self::$_instance = new self();
64
			}
65
			return self::$_instance;
66
		} // END instance()
67
68
		/**
69
		 * Cloning is forbidden.
70
		 *
71
		 * @access public
72
		 * @since  1.0.0
73
		 * @return void
74
		 */
75
		public function __clone() {
76
			// Cloning instances of the class is forbidden
77
			_doing_it_wrong( __FUNCTION__, __( 'Cloning this object is forbidden.', 'auto-load-next-post' ), self::$version );
78
		} // END __clone()
79
80
		/**
81
		 * Unserializing instances of this class is forbidden.
82
		 *
83
		 * @access public
84
		 * @since  1.0.0
85
		 * @return void
86
		 */
87
		public function __wakeup() {
88
			_doing_it_wrong( __FUNCTION__, __( 'Unserializing instances of this class is forbidden.', 'auto-load-next-post' ), self::$version );
89
		} // END __wakeup()
90
91
		/**
92
		 * Auto_Load_Next_Post Constructor
93
		 *
94
		 * @access  public
95
		 * @since   1.0.0
96
		 * @version 1.5.0
97
		 * @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...
98
		 */
99
		public function __construct() {
100
			$this->setup_constants();
101
			$this->includes();
102
			$this->init_hooks();
103
104
			/**
105
			 * Auto Load Next Post is fully loaded.
106
			 */
107
			do_action( 'auto_load_next_post_loaded' );
108
		} // END __construct()
109
110
		/**
111
		 * Hooks into action hooks.
112
		 *
113
		 * @access public
114
		 * @since  1.5.0
115
		 */
116
		public function init_hooks() {
117
			// Load translation files.
118
			add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
119
120
			// Load Auto Load Next Post scripts on the frontend.
121
			add_action( 'wp_enqueue_scripts', array( $this, 'alnp_enqueue_scripts' ) );
122
		} // END init_hooks()
123
124
		/**
125
		 * Setup Constants
126
		 *
127
		 * @since   1.4.3
128
		 * @version 1.5.10
129
		 * @access  private
130
		 */
131
		private function setup_constants() {
132
			$this->define('AUTO_LOAD_NEXT_POST_VERSION', self::$version);
133
			$this->define('AUTO_LOAD_NEXT_POST_FILE', __FILE__);
134
			$this->define('AUTO_LOAD_NEXT_POST_SLUG', 'auto-load-next-post');
135
136
			$this->define('AUTO_LOAD_NEXT_POST_URL_PATH', untrailingslashit( plugins_url( '/', __FILE__ ) ) );
137
			$this->define('AUTO_LOAD_NEXT_POST_FILE_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
138
			$this->define('AUTO_LOAD_NEXT_POST_TEMPLATE_PATH', 'auto-load-next-post/');
139
			$this->define('AUTO_LOAD_NEXT_POST_3RD_PARTY', AUTO_LOAD_NEXT_POST_FILE_PATH . '/includes/3rd-party/');
140
141
			$this->define('AUTO_LOAD_NEXT_POST_WP_VERSION_REQUIRE', '4.4');
142
143
			$suffix       = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
144
			$debug_suffix = defined('ALNP_DEV_DEBUG') && ALNP_DEV_DEBUG ? '.dev' : '';
145
146
			$this->define('AUTO_LOAD_NEXT_POST_SCRIPT_MODE', $suffix);
147
			$this->define('AUTO_LOAD_NEXT_POST_DEBUG_MODE', $debug_suffix);
148
149
			$this->define('AUTO_LOAD_NEXT_POST_STORE_URL', 'https://autoloadnextpost.com/');
150
			$this->define('AUTO_LOAD_NEXT_POST_SUPPORT_URL', 'https://wordpress.org/support/plugin/auto-load-next-post');
151
			$this->define('AUTO_LOAD_NEXT_POST_REVIEW_URL', 'https://wordpress.org/plugins/auto-load-next-post/#reviews');
152
		} // END setup_constants()
153
154
		/**
155
		 * Define constant if not already set.
156
		 *
157
		 * @param  string $name
158
		 * @param  string|bool $value
159
		 * @access private
160
		 * @since  1.4.3
161
		 */
162
		private function define( $name, $value ) {
163
			if ( ! defined( $name ) ) {
164
				define( $name, $value );
165
			}
166
		} // END define()
167
168
		/**
169
		 * Include required core files used in admin and on the frontend.
170
		 *
171
		 * @access  public
172
		 * @since   1.0.0
173
		 * @version 1.5.10
174
		 * @return  void
175
		 */
176
		public function includes() {
177
			include_once( dirname( __FILE__ ) . '/includes/class-alnp-autoloader.php' ); // Autoloader.
178
			include_once( dirname( __FILE__ ) . '/includes/auto-load-next-post-conditional-functions.php' ); // Conditional functions.
179
			include_once( dirname( __FILE__ ) . '/includes/auto-load-next-post-formatting-functions.php' ); // Formatting functions.
180
			include_once( dirname( __FILE__ ) . '/includes/auto-load-next-post-themes-supported.php' ); // Handles all supported themes out of the box.
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
			alnp_include_theme_support();
185
186
			// 3rd Party support.
187
			include_once( dirname( __FILE__ ) . '/includes/3rd-party/3rd-party.php' );
188
189
			// Customizer.
190
			include_once( dirname( __FILE__ ) . '/includes/customizer/class-alnp-customizer.php' );
191
			include_once( dirname( __FILE__ ) . '/includes/customizer/class-alnp-customizer-scripts.php' );
192
193
			// Include admin class to handle all back-end functions.
194
			if ( is_admin() ) {
195
				include_once( dirname( __FILE__ ) . '/includes/admin/class-alnp-admin.php' ); // Admin section.
196
			}
197
198
			// Install.
199
			require_once( dirname( __FILE__ ) . '/includes/class-alnp-install.php' );
200
		} // END includes()
201
202
		/**
203
		 * Make the plugin translation ready.
204
		 *
205
		 * Translations should be added in the WordPress language directory:
206
		 *      - WP_LANG_DIR/plugins/auto-load-next-post-LOCALE.mo
207
		 *
208
		 * @access  public
209
		 * @since   1.0.0
210
		 * @version 1.4.10
211
		 * @return  void
212
		 */
213
		public function load_plugin_textdomain() {
214
			load_plugin_textdomain( 'auto-load-next-post', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
215
		} // END load_plugin_textdomain()
216
217
		/**
218
		 * Registers and enqueues stylesheets and javascripts for the front of the site.
219
		 *
220
		 * @access  public
221
		 * @since   1.3.2
222
		 * @version 1.5.8
223
		 */
224
		public function alnp_enqueue_scripts() {
225
			// Load the Javascript if found as a singluar post and the user is not a bot.
226
			if ( !alnp_is_bot() && is_singular() && get_post_type() == 'post' ) {
227
				// This checks to see if the JavaScript should load in the footer or not.
228
				$load_in_footer = alnp_load_js_in_footer();
229
230
				$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 );
231
232
				// Only load History.js when not in the customizer.
233
				if ( ! is_customize_preview() ) {
234
					$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 );
235
				}
236
237
				$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 );
238
239
				// Variables for the JavaScript
240
				wp_localize_script( 'auto-load-next-post-script', 'auto_load_next_post_params', array(
241
					'alnp_version'              => AUTO_LOAD_NEXT_POST_VERSION,
242
					'alnp_content_container'    => get_option( 'auto_load_next_post_content_container' ),
243
					'alnp_title_selector'       => get_option( 'auto_load_next_post_title_selector' ),
244
					'alnp_navigation_container' => get_option( 'auto_load_next_post_navigation_container' ),
245
					'alnp_comments_container'   => get_option( 'auto_load_next_post_comments_container' ),
246
					'alnp_remove_comments'      => get_option( 'auto_load_next_post_remove_comments' ),
247
					'alnp_google_analytics'     => get_option( 'auto_load_next_post_google_analytics' ),
248
					'alnp_event_on_load'        => get_option( 'auto_load_next_post_on_load_event' ),
249
					'alnp_event_on_entering'    => get_option( 'auto_load_next_post_on_entering_event' ),
250
					'alnp_is_customizer'        => $this->is_alnp_using_customizer(),
251
					'alnp_load_in_footer'       => $load_in_footer
252
				) );
253
			} // END if is_singular() && get_post_type()
254
		} // END alnp_enqueue_scripts()
255
256
		/**
257
		 * Checks if we are using the theme customizer.
258
		 *
259
		 * @access public
260
		 * @since  1.5.0
261
		 * @static
262
		 * @return string|false
263
		 */
264
		public static function is_alnp_using_customizer() {
265
			if ( is_customize_preview() ) {
266
				return "yes";
267
			}
268
269
			return false;
270
		} // END is_alnp_using_customizer()
271
272
		/**
273
		 * Helper function for registering and enqueueing scripts and styles.
274
		 *
275
		 * @access  public
276
		 * @since   1.0.0
277
		 * @version 1.5.0
278
		 * @static
279
		 * @param   string  $name      The ID to register with WordPress.
280
		 * @param   string  $file_path The path to the actual file.
281
		 * @param   bool    $is_script Optional, argument for if the incoming file_path is a JavaScript source file.
282
		 * @param   array   $support   Optional, for requiring other javascripts for the source file you are calling.
283
		 * @param   string  $version   Optional, can match the version of the plugin or version of the source file.
284
		 * @param   bool    $footer Optional, can set the JavaScript to load in the footer instead.
285
		 */
286
		public static function load_file( $name, $file_path, $is_script = false, $support = array(), $version = '', $footer = false ) {
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
					if ( !wp_script_is( $name, 'registered' ) ) {
292
						wp_register_script( $name, $url, $support, $version, $footer );
293
						wp_enqueue_script( $name );
294
					}
295
				} else {
296
					if ( !wp_style_is( $name, 'registered' ) ) {
297
						wp_register_style( $name, $url );
298
						wp_enqueue_style( $name );
299
					}
300
				} // end if
301
			} // end if
302
303
		} // END load_file()
304
305
	} // END Auto_Load_Next_Post()
306
307
} // END class exists 'Auto_Load_Next_Post'
308
309
return Auto_Load_Next_Post::instance();
310