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

Auto_Load_Next_Post_Customizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Auto Load Next Post: Theme Customizer
4
 *
5
 * @since    1.5.0
6
 * @author   Sébastien Dumont
7
 * @category Classes
8
 * @package  Auto Load Next Post/Classes/Customizer
9
 * @license  GPL-2.0+
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
if ( !class_exists( 'Auto_Load_Next_Post_Customizer' ) ) {
18
19
	class Auto_Load_Next_Post_Customizer {
20
21
		/**
22
		 * Constructor.
23
		 *
24
		 * @since  1.5.0
25
		 * @access public
26
		 */
27
		public function __construct() {
28
			add_action( 'customize_register', array( $this, 'alnp_init_customizer' ), 50 );
29
			add_filter( 'customize_loaded_components', array( $this, 'alnp_remove_widgets_panel' ) );
30
		}
31
32
		/**
33
		 * Initialize the Customizer.
34
		 *
35
		 * @access public
36
		 * @since  1.5.0
37
		 * @param  WP_Customize_Manager $wp_customize The Customizer object.
38
		 */
39
		public function alnp_init_customizer( $wp_customize ) {
40
			/**
41
			 * Dont add settings to the customizer if the user does
42
			 * not have permission to make changes to the theme.
43
			 */
44
			if ( ! current_user_can( 'edit_theme_options' ) ) {
45
				return;
46
			}
47
48
			// Auto Load Next Post Panel.
49
			$panel = array( 'panel' => 'alnp' );
50
51
			/**
52
			 * Add the main panel and sections.
53
			 *
54
			 * Only shows if viewing a singular post.
55
			 */
56
			$wp_customize->add_panel(
57
				'alnp', array(
58
					'title'           => esc_html__( 'Auto Load Next Post', 'auto-load-next-post' ),
59
					'capability'      => 'edit_theme_options',
60
					'description'     => esc_html__( 'Auto Load Next Post increases your pageviews by engaging the site viewers to keep reading your content rather than increasing your bounce rate.', 'auto-load-next-post' ),
61
					'priority'        => 160,
62
					'active_callback' => array( $this, 'is_page_alnp_ready' )
63
				)
64
			);
65
66
			// Get the sections.
67
			$sections = $this->alnp_get_customizer_sections();
68
69
			// Add each section.
70
			foreach ( $sections as $section => $args ) {
71
				/**
72
				 * If we are not only viewing Auto Load Next Post customizer sections
73
				 * then move them under our own panel.
74
				 */
75
				if ( ! $this->alnp_is_customizer() ) {
76
					$args = array_merge( $args, $panel );
77
				}
78
79
				$wp_customize->add_section( $section, $args );
80
			}
81
82
			// Get plugin settings.
83
			$settings = $this->alnp_get_customizer_settings();
84
85
			// Add each setting.
86
			foreach ( $settings as $setting => $args ) {
87
				$wp_customize->add_setting( $setting, $args );
88
			}
89
90
			/**
91
			 * Let other plugins register extra customizer options for Auto Load Next Post.
92
			 *
93
			 * @since 1.5.0
94
			 * @param WP_Customize_Manager $wp_customize The Customizer object.
95
			 */
96
			do_action( 'alnp_customizer_register', $wp_customize );
97
98
			$controls = $this->alnp_get_customizer_controls();
99
100
			foreach ( $controls as $control => $args ) {
101
				$wp_customize->add_control( new $args['class']( $wp_customize, $control, $args ) );
102
			}
103
104
			if ( $this->alnp_is_customizer() ) {
105
				$this->alnp_remove_default_customizer_panels( $wp_customize ); // Remove controls from the customizer.
106
			}
107
108
			// Load custom controllers.
109
			/*include_once( dirname( __FILE__ ) . '/class-alnp-display-video-controller.php' );
110
111
			// Video Help - Coming Soon
112
			$wp_customize->add_setting( 'alnp_video_theme_selectors', array(
113
				'default' => '', // The video ID
114
				'type'    => 'theme_mod',
115
			) );
116
117
			$wp_customize->add_control( new Auto_Load_Next_Post_Display_Video_Controller( $wp_customize, 'alnp_video_theme_selectors', array(
118
				'label'    => __( 'Video: How to find your theme selectors', 'auto-load-next-post' ),
119
				'section'  => 'auto_load_next_post_theme_selectors',
120
				'settings' => 'alnp_video_theme_selectors',
121
				'type'     => 'alnp-display-video',
122
				'priority' => 1,
123
			) ) );*/
124
125
			/**
126
			 * If Auto Load Next Post Pro is not installed, display a new section
127
			 * to tell users about the pro version, what comes with it
128
			 * and link to product page.
129
			 */
130
			if ( ! is_alnp_pro_version_installed() ) {
131
				include_once( dirname( __FILE__ ) . '/class-alnp-pro-preview-controller.php' );
132
133
				$preview_args = array(
134
					'title'    => esc_html__( 'More?', 'auto-load-next-post' ),
135
					'priority' => 999,
136
				);
137
138
				if ( ! $this->alnp_is_customizer() ) {
139
					$preview_args = array_merge( $preview_args, $panel );
140
				}
141
142
				$wp_customize->add_section( 'alnp_pro_preview', $preview_args );
143
144
				$wp_customize->add_setting( 'alnp_pro_preview', array(
145
					'default' => null,
146
				) );
147
148
				$wp_customize->add_control( new Auto_Load_Next_Post_Pro_Preview_Controller( $wp_customize, 'alnp_pro_preview', array(
149
					'label'    => __( 'Looking for more options?', 'auto-load-next-post' ),
150
					'section'  => 'alnp_pro_preview',
151
					'settings' => 'alnp_pro_preview',
152
					'priority' => 1,
153
				) ) );
154
			}
155
		} // END alnp_init_customizer()
156
157
		/**
158
		 * Removes the core 'Navigation Menu' and 'Widgets' panel from the Customizer.
159
		 *
160
		 * @since  1.5.0
161
		 * @param  array $components Core Customizer components list.
162
		 * @return array (Maybe) modified components list.
163
		 */
164
		public function alnp_remove_widgets_panel( $components ) {
165
			if ( $this->alnp_is_customizer() ) {
166
				foreach( $components as $key => $component ) {
167
					if ( $component == 'widgets' ) unset( $components[ 'widgets' ] );
168
					if ( $component == 'nav_menus' ) unset( $components[ 'nav_menus' ] );
169
				}
170
			}
171
172
			return $components;
173
		} // END alnp_remove_widgets_panel()
174
175
		/**
176
		 * Remove any unwanted default conrols.
177
		 *
178
		 * @since  1.5.0
179
		 * @global $wp_cutomize
180
		 * @param  object $wp_customize
181
		 * @return boolean
182
		 */
183
		public function alnp_remove_default_customizer_panels( $wp_customize ) {
184
			global $wp_customize;
185
186
			$wp_customize->remove_section("themes");
187
			$wp_customize->remove_control("active_theme");
188
			$wp_customize->remove_section("title_tagline");
189
			$wp_customize->remove_section("colors");
190
			$wp_customize->remove_section("header_image");
191
			$wp_customize->remove_section("background_image");
192
			$wp_customize->remove_section("static_front_page");
193
			$wp_customize->remove_section("custom_css");
194
195
			return true;
196
		} // END alnp_remove_default_customizer_panels()
197
198
		/**
199
		 * Are we looking at the Auto Load Next Post customizer?
200
		 *
201
		 * @since  1.5.0
202
		 * @return boolean
203
		 */
204
		public function alnp_is_customizer() {
205
			return isset( $_GET['alnp-customizer'] ) && $_GET['alnp-customizer'] === 'yes';
206
		} // END alnp_is_customizer()
207
208
		/**
209
		 * Get Customizer sections for Auto Load Next Post.
210
		 *
211
		 * @since  1.5.0
212
		 * @return array
213
		 */
214
		public function alnp_get_customizer_sections() {
215
			/**
216
			 * Filter Customizer sections for Auto Load Next Post.
217
			 *
218
			 * @param array $sections Customizer sections to add.
219
			 */
220
			return apply_filters( 'auto_load_next_post_get_customizer_sections', array(
221
				'auto_load_next_post_theme_selectors' => array(
222
					'capability'  => 'edit_theme_options',
223
					'title'       => __( 'Theme Selectors', 'auto-load-next-post' ),
224
					'description' => sprintf( __( 'Set the theme selectors below according to the theme. %1$sHow to find my theme selectors?%2$s', 'auto-load-next-post' ), '<a href="' . esc_url( 'https://autoloadnextpost.com/documentation/find-theme-selectors/?utm_source=wpcustomizer&utm_campaign=plugin-settings-theme-selectors' ) . '" target="_blank">', '</a>' ),
225
				),
226
				'auto_load_next_post_misc' => array(
227
					'capability'  => 'edit_theme_options',
228
					'title'       => __( 'Misc Settings', 'auto-load-next-post' ),
229
					'description' => sprintf( __( 'Here you can set if you want to track pageviews, remove comments and load %s javascript in the footer.', 'auto-load-next-post' ), esc_html__( 'Auto Load Next Post', 'auto-load-next-post' ) ),
230
				),
231
			) );
232
		} // END alnp_get_customizer_sections()
233
234
		/**
235
		 * Get Customizer settings for Auto Load Next Post.
236
		 *
237
		 * @since  1.5.0
238
		 * @return array
239
		 */
240
		public function alnp_get_customizer_settings() {
241
			$settings = $this->alnp_get_settings();
242
243
			/**
244
			 * Filter Customizer settings for Auto Load Next Post.
245
			 *
246
			 * @param array $settings Customizer settings to add.
247
			 */
248
			return apply_filters( 'auto_load_next_post_get_customizer_settings', array(
249
				'auto_load_next_post_content_container' => array(
250
					'capability'        => 'edit_theme_options',
251
					'default'           => $settings['alnp_content_container'],
252
					'sanitize_callback' => 'wp_filter_post_kses',
253
					'validate_callback' => 'alnp_validate_content_container_selector',
254
					'transport'         => 'postMessage',
255
					'type'              => 'option',
256
				),
257
				'auto_load_next_post_title_selector' => array(
258
					'capability'        => 'edit_theme_options',
259
					'default'           => $settings['alnp_title_selector'],
260
					'sanitize_callback' => 'wp_filter_post_kses',
261
					'validate_callback' => 'alnp_validate_post_title_selector',
262
					'transport'         => 'postMessage',
263
					'type'              => 'option',
264
				),
265
				'auto_load_next_post_navigation_container' => array(
266
					'capability'        => 'edit_theme_options',
267
					'default'           => $settings['alnp_navigation_container'],
268
					'sanitize_callback' => 'wp_filter_post_kses',
269
					'validate_callback' => 'alnp_validate_post_navigation_selector',
270
					'transport'         => 'postMessage',
271
					'type'              => 'option',
272
				),
273
				'auto_load_next_post_previous_post_selector' => array(
274
					'capability'        => 'edit_theme_options',
275
					'default'           => $settings['alnp_previous_post_selector'],
276
					'sanitize_callback' => 'wp_filter_post_kses',
277
					'transport'         => 'postMessage',
278
					'type'              => 'option',
279
				),
280
				'auto_load_next_post_comments_container' => array(
281
					'capability'        => 'edit_theme_options',
282
					'default'           => $settings['alnp_comments_container'],
283
					'sanitize_callback' => 'wp_filter_post_kses',
284
					'transport'         => 'postMessage',
285
					'type'              => 'option',
286
				),
287
				'auto_load_next_post_remove_comments' => array(
288
					'capability'        => 'edit_theme_options',
289
					'default'           => $settings['alnp_remove_comments'],
290
					'transport'         => 'postMessage',
291
					'type'              => 'option',
292
				),
293
				'auto_load_next_post_google_analytics' => array(
294
					'capability'        => 'edit_theme_options',
295
					'default'           => $settings['alnp_google_analytics'],
296
					'transport'         => 'postMessage',
297
					'type'              => 'option',
298
				),
299
				'auto_load_next_post_js_footer' => array(
300
					'capability'        => 'edit_theme_options',
301
					'default'           => $settings['alnp_js_footer'],
302
					'transport'         => 'postMessage',
303
					'type'              => 'option',
304
				),
305
			) );
306
		} // END alnp_get_customizer_settings()
307
308
		/**
309
		 * Get Customizer controls for Auto Load Next Post.
310
		 *
311
		 * @since  1.5.0
312
		 * @global int $blog_id
313
		 * @return array
314
		 */
315
		public function alnp_get_customizer_controls() {
316
			global $blog_id;
317
318
			/**
319
			 * Filter Customizer controls for Auto Load Next Post.
320
			 *
321
			 * @param array $controls Customizer controls to add.
322
			 */
323
			return apply_filters( 'auto_load_next_post_get_customizer_controls', array(
324
				'alnp_content_container' => array(
325
					'class'       => 'WP_Customize_Control',
326
					'description' => __( 'The primary container where the post content is loaded in. Default: <code>main.site-main</code>', 'auto-load-next-post' ),
327
					'label'       => esc_html__( 'Content Container', 'auto-load-next-post' ),
328
					'section'     => 'auto_load_next_post_theme_selectors',
329
					'settings'    => 'auto_load_next_post_content_container',
330
					'type'        => 'text',
331
				),
332
				'alnp_title_selector' => array(
333
					'class'       => 'WP_Customize_Control',
334
					'description' => __( 'Used to identify which article the user is reading and track should Google Analytics or other analytics be enabled. Default: <code>h1.entry-title</code>', 'auto-load-next-post' ),
335
					'label'       => esc_html__( 'Post Title Selector', 'auto-load-next-post' ),
336
					'section'     => 'auto_load_next_post_theme_selectors',
337
					'settings'    => 'auto_load_next_post_title_selector',
338
					'type'        => 'text',
339
				),
340
				'alnp_navigation_container' => array(
341
					'class'       => 'WP_Customize_Control',
342
					'description' => __( 'Used to identify which post to load next if any. Default: <code>nav.post-navigation</code>', 'auto-load-next-post' ),
343
					'label'       => esc_html__( 'Post Navigation Container', 'auto-load-next-post' ),
344
					'section'     => 'auto_load_next_post_theme_selectors',
345
					'settings'    => 'auto_load_next_post_navigation_container',
346
					'type'        => 'text',
347
				),
348
				'alnp_comments_container' => array(
349
					'class'       => 'WP_Customize_Control',
350
					'description' => sprintf( __( 'Used to remove comments if enabled under <strong>%1$sMisc%2$s</strong> settings. Default: <code>div#comments</code>', 'auto-load-next-post' ), '<a href="javascript:wp.customize.section( \'auto_load_next_post_misc\' ).focus();">', '</a>' ),
351
					'label'       => esc_html__( 'Comments Container', 'auto-load-next-post' ),
352
					'section'     => 'auto_load_next_post_theme_selectors',
353
					'settings'    => 'auto_load_next_post_comments_container',
354
					'type'        => 'text',
355
				),
356
				'alnp_remove_comments' => array(
357
					'class'       => 'WP_Customize_Control',
358
					'description' => esc_html__( 'Enable to remove comments when each post loads including the initial post.', 'auto-load-next-post' ),
359
					'label'       => esc_html__( 'Remove Comments', 'auto-load-next-post' ),
360
					'section'     => 'auto_load_next_post_misc',
361
					'settings'    => 'auto_load_next_post_remove_comments',
362
					'type'        => 'checkbox',
363
				),
364
				'alnp_google_analytics' => array(
365
					'class'       => 'WP_Customize_Control',
366
					'description' => esc_html__( 'Enable to track each post the visitor is reading. This will count as a pageview. You must already have Google Analytics setup.', 'auto-load-next-post' ),
367
					'label'       => esc_html__( 'Update Google Analytics', 'auto-load-next-post' ),
368
					'section'     => 'auto_load_next_post_misc',
369
					'settings'    => 'auto_load_next_post_google_analytics',
370
					'type'        => 'checkbox',
371
				),
372
				'alnp_js_footer' => array(
373
					'class'       => 'WP_Customize_Control',
374
					'description' => esc_html__( 'Enable to load Auto Load Next Post in the footer instead of the header. Can be useful to optimize your site.', 'auto-load-next-post' ),
375
					'label'       => esc_html__( 'JavaScript in Footer?', 'auto-load-next-post' ),
376
					'section'     => 'auto_load_next_post_misc',
377
					'settings'    => 'auto_load_next_post_google_analytics',
378
					'type'        => 'checkbox',
379
				),
380
			) );
381
		} // END alnp_get_customizer_controls()
382
383
		/**
384
		 * Validates the content container theme selector to not be empty.
385
		 *
386
		 * @since  1.5.0
387
		 * @param  WP_Error $validity Validity.
388
		 * @param  string   $value    Value, normally pre-sanitized.
389
		 * @return WP_Error $validity
390
		 */
391
		public function alnp_validate_content_container_selector( $validity, $value ) {
392
			if ( empty( $value ) ) {
393
				$validity->add( 'required', esc_html__( 'The content container selector is empty. Will not know where to load posts without it.', 'auto-load-next-post' ) );
394
			}
395
396
			return $validity;
397
		} // END alnp_validate_content_container_selector()
398
399
		/**
400
		 * Validates the post title theme selector to not be empty.
401
		 *
402
		 * @since  1.5.0
403
		 * @param  WP_Error $validity Validity.
404
		 * @param  string   $value    Value, normally pre-sanitized.
405
		 * @return WP_Error $validity
406
		 */
407
		public function alnp_validate_post_title_selector( $validity, $value ) {
408
			if ( empty( $value ) ) {
409
				$validity->add( 'required', esc_html__( 'The post title selector is empty. Will not be able to identify which article the user is reading.', 'auto-load-next-post' ) );
410
			}
411
412
			return $validity;
413
		} // END alnp_validate_post_title_selector()
414
415
		/**
416
		 * Validates the post navigation theme selector to not be empty.
417
		 *
418
		 * @since  1.5.0
419
		 * @param  WP_Error $validity Validity.
420
		 * @param  string   $value    Value, normally pre-sanitized.
421
		 * @return WP_Error $validity
422
		 */
423
		public function alnp_validate_post_navigation_selector( $validity, $value ) {
424
			if ( empty( $value ) ) {
425
				$validity->add( 'required', esc_html__( 'The post navigation container selector is empty. Required so ALNP can look up the next post to load.', 'auto-load-next-post' ) );
426
			}
427
428
			return $validity;
429
		} // END alnp_validate_post_navigation_selector()
430
431
		/**
432
		 * Return Auto Load Next Post settings.
433
		 *
434
		 * @since  1.5.0
435
		 * @return array $args
436
		 */
437
		public function alnp_get_settings() {
438
			$args = array(
439
				'alnp_content_container'      => get_option( 'auto_load_next_post_content_container' ),
440
				'alnp_title_selector'         => get_option( 'auto_load_next_post_title_selector' ),
441
				'alnp_navigation_container'   => get_option( 'auto_load_next_post_navigation_container' ),
442
				'alnp_previous_post_selector' => get_option( 'auto_load_next_post_previous_post_selector' ),
443
				'alnp_comments_container'     => get_option( 'auto_load_next_post_comments_container' ),
444
				'alnp_remove_comments'        => get_option( 'auto_load_next_post_remove_comments' ),
445
				'alnp_google_analytics'       => get_option( 'auto_load_next_post_google_analytics' ),
446
				'alnp_js_footer'              => get_option( 'auto_load_next_post_js_footer' )
447
			);
448
449
			return $args;
450
		} // END alnp_get_settings()
451
452
		/**
453
		 * Returns true or false if the page is ready for Auto Load Next Post
454
		 * panel to show in the customizer.
455
		 *
456
		 * @since  1.5.0
457
		 * @return boolean
458
		 */
459
		public function is_page_alnp_ready() {
460
			if ( is_front_page() && is_home() ) {
461
				return false;
462
			} elseif ( is_front_page() ) {
463
				return false;
464
			} elseif ( is_home() ) {
465
				return true;
466
			}
467
			elseif ( is_singular( apply_filters( 'alnp_customizer_posts_ready', array( 'post' ) ) ) ) {
468
				return true;
469
			}
470
471
			return true;
472
		} // END is_page_alnp_ready()
473
474
		/**
475
		 * Returns the permalink of a random page
476
		 *
477
		 * @since  1.5.0
478
		 * @static
479
		 * @param  string $post_type - Default is post.
480
		 * @return int|boolean
481
		 */
482
		public static function alnp_get_random_page_permalink( $post_type = 'post' ) {
483
			$args = array(
484
				'post_type'      => $post_type,
485
				'post_status'    => 'publish',
486
				'orderby'        => 'rand',
487
				'posts_per_page' => 1
488
			);
489
490
			$query = new WP_Query( $args );
491
492
			if ( $query->have_posts() ) {
493
				while ( $query->have_posts() ) : $query->the_post();
494
					$id = get_the_ID();
495
496
					return get_permalink( $id );
497
				endwhile;
498
			}
499
			else {
500
				return false;
501
			}
502
		} // END alnp_get_random_page_permalink()
503
504
	} // END Class
505
506
} // END if class
507
508
new Auto_Load_Next_Post_Customizer();
509