Completed
Pull Request — master (#155)
by Sébastien
01:38
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.7
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.7';
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.5
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
140
			$this->define('AUTO_LOAD_NEXT_POST_WP_VERSION_REQUIRE', '4.4');
141
142
			$suffix       = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
143
			$debug_suffix = defined('ALNP_DEV_DEBUG') && ALNP_DEV_DEBUG ? '.dev' : '';
144
145
			$this->define('AUTO_LOAD_NEXT_POST_SCRIPT_MODE', $suffix);
146
			$this->define('AUTO_LOAD_NEXT_POST_DEBUG_MODE', $debug_suffix);
147
148
			$this->define('AUTO_LOAD_NEXT_POST_STORE_URL', 'https://autoloadnextpost.com/');
149
			$this->define('AUTO_LOAD_NEXT_POST_SUPPORT_URL', 'https://wordpress.org/support/plugin/auto-load-next-post');
150
			$this->define('AUTO_LOAD_NEXT_POST_REVIEW_URL', 'https://wordpress.org/plugins/auto-load-next-post/#reviews');
151
		} // END setup_constants()
152
153
		/**
154
		 * Define constant if not already set.
155
		 *
156
		 * @param  string $name
157
		 * @param  string|bool $value
158
		 * @access private
159
		 * @since  1.4.3
160
		 */
161
		private function define( $name, $value ) {
162
			if ( ! defined( $name ) ) {
163
				define( $name, $value );
164
			}
165
		} // END define()
166
167
		/**
168
		 * Include required core files used in admin and on the frontend.
169
		 *
170
		 * @access  public
171
		 * @since   1.0.0
172
		 * @version 1.5.5
173
		 * @return  void
174
		 */
175
		public function includes() {
176
			include_once( dirname( __FILE__ ) . '/includes/class-alnp-autoloader.php' ); // Autoloader.
177
			include_once( dirname( __FILE__ ) . '/includes/auto-load-next-post-conditional-functions.php' ); // Conditional functions.
178
			include_once( dirname( __FILE__ ) . '/includes/auto-load-next-post-formatting-functions.php' ); // Formatting functions.
179
			include_once( dirname( __FILE__ ) . '/includes/auto-load-next-post-themes-supported.php' ); // Handles all supported themes out of the box.
180
			include_once( dirname( __FILE__ ) . '/includes/auto-load-next-post-core-functions.php' ); // Contains core functions for the front/back end.
181
182
			// Include theme support.
183
			alnp_include_theme_support();
184
185
			// Customizer.
186
			include_once( dirname( __FILE__ ) . '/includes/customizer/class-alnp-customizer.php' );
187
			include_once( dirname( __FILE__ ) . '/includes/customizer/class-alnp-customizer-scripts.php' );
188
189
			// Include admin class to handle all back-end functions.
190
			if ( is_admin() ) {
191
				include_once( dirname( __FILE__ ) . '/includes/admin/class-alnp-admin.php' ); // Admin section.
192
			}
193
194
			// Install.
195
			require_once( dirname( __FILE__ ) . '/includes/class-alnp-install.php' );
196
		} // END includes()
197
198
		/**
199
		 * Make the plugin translation ready.
200
		 *
201
		 * Translations should be added in the WordPress language directory:
202
		 *      - WP_LANG_DIR/plugins/auto-load-next-post-LOCALE.mo
203
		 *
204
		 * @access  public
205
		 * @since   1.0.0
206
		 * @version 1.4.10
207
		 * @return  void
208
		 */
209
		public function load_plugin_textdomain() {
210
			load_plugin_textdomain( 'auto-load-next-post', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
211
		} // END load_plugin_textdomain()
212
213
		/**
214
		 * Registers and enqueues stylesheets and javascripts for the front of the site.
215
		 *
216
		 * @access  public
217
		 * @since   1.3.2
218
		 * @version 1.5.7
219
		 */
220
		public function alnp_enqueue_scripts() {
221
			// Load the Javascript if found as a singluar post and the user is not a bot.
222
			if ( !alnp_is_bot() && is_singular() && get_post_type() == 'post' ) {
223
				// This checks to see if the JavaScript should load in the footer or not.
224
				$load_in_footer = alnp_load_js_in_footer();
225
226
				$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 );
227
228
				// Only load History.js when not in the customizer.
229
				if ( ! is_customize_preview() ) {
230
					$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 );
231
				}
232
233
				$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 );
234
235
				// Variables for the JavaScript
236
				wp_localize_script( 'auto-load-next-post-script', 'auto_load_next_post_params', array(
237
					'alnp_version'              => AUTO_LOAD_NEXT_POST_VERSION,
238
					'alnp_content_container'    => get_option( 'auto_load_next_post_content_container' ),
239
					'alnp_title_selector'       => get_option( 'auto_load_next_post_title_selector' ),
240
					'alnp_navigation_container' => get_option( 'auto_load_next_post_navigation_container' ),
241
					'alnp_comments_container'   => get_option( 'auto_load_next_post_comments_container' ),
242
					'alnp_remove_comments'      => get_option( 'auto_load_next_post_remove_comments' ),
243
					'alnp_google_analytics'     => get_option( 'auto_load_next_post_google_analytics' ),
244
					'alnp_event_on_load'        => get_option( 'auto_load_next_post_on_load_event' ),
245
					'alnp_event_on_entering'    => get_option( 'auto_load_next_post_on_entering_event' ),
246
					'alnp_is_customizer'        => $this->is_alnp_using_customizer(),
247
				) );
248
			} // END if is_singular() && get_post_type()
249
		} // END alnp_enqueue_scripts()
250
251
		/**
252
		 * Checks if we are using the theme customizer.
253
		 *
254
		 * @access public
255
		 * @since  1.5.0
256
		 * @static
257
		 * @return string|boolean
258
		 */
259
		public static function is_alnp_using_customizer() {
260
			if ( is_customize_preview() ) {
261
				return "yes";
262
			}
263
264
			return false;
265
		} // END is_alnp_using_customizer()
266
267
		/**
268
		 * Helper function for registering and enqueueing scripts and styles.
269
		 *
270
		 * @access  public
271
		 * @since   1.0.0
272
		 * @version 1.5.0
273
		 * @static
274
		 * @param   string  $name      The ID to register with WordPress.
275
		 * @param   string  $file_path The path to the actual file.
276
		 * @param   bool    $is_script Optional, argument for if the incoming file_path is a JavaScript source file.
277
		 * @param   array   $support   Optional, for requiring other javascripts for the source file you are calling.
278
		 * @param   string  $version   Optional, can match the version of the plugin or version of the source file.
279
		 * @param   bool    $footer Optional, can set the JavaScript to load in the footer instead.
280
		 * @global  string  $wp_version
281
		 */
282
		public static function load_file( $name, $file_path, $is_script = false, $support = array(), $version = '', $footer = false ) {
283
			global $wp_version;
284
285
			$url = AUTO_LOAD_NEXT_POST_URL_PATH . $file_path; // URL to the file.
286
287
			if ( file_exists( AUTO_LOAD_NEXT_POST_FILE_PATH . $file_path ) ) {
288
				if ( $is_script ) {
289
					if ( !wp_script_is( $name, 'registered' ) ) {
290
						wp_register_script( $name, $url, $support, $version, $footer );
291
						wp_enqueue_script( $name );
292
					}
293
				} else {
294
					if ( !wp_style_is( $name, 'registered' ) ) {
295
						wp_register_style( $name, $url );
296
						wp_enqueue_style( $name );
297
					}
298
				} // end if
299
			} // end if
300
301
		} // END load_file()
302
303
	} // END Auto_Load_Next_Post()
304
305
} // END class exists 'Auto_Load_Next_Post'
306
307
return Auto_Load_Next_Post::instance();
308