PluginProbe
Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels / 2.3.5
Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels v2.3.5
2.9.1 2.9.0 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.4.0 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 2.0.0 2.1.0 2.2.0 2.3.2 2.3.3 2.3.5 2.3.6 2.3.8 2.3.9 2.4.3 All 37 releases
automatic-youtube-gallery / admin / settings.php

settings.php in Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels 2.3.5, at admin/settings.php

656 lines 23.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Plugin Settings.
5 *
6 * @link https://plugins360.com
7 * @since 1.0.0
8 *
9 * @package Automatic_YouTube_Gallery
10 */
11
12 // Exit if accessed directly
13 if ( ! defined( 'WPINC' ) ) {
14 die;
15 }
16
17 /**
18 * AYG_Admin_Settings class.
19 *
20 * @since 1.0.0
21 */
22 class AYG_Admin_Settings {
23
24 /**
25 * Settings tabs array.
26 *
27 * @since 1.0.0
28 * @access protected
29 * @var array
30 */
31 protected $tabs = array();
32
33 /**
34 * Settings sections array.
35 *
36 * @since 1.0.0
37 * @access protected
38 * @var array
39 */
40 protected $sections = array();
41
42 /**
43 * Settings fields array
44 *
45 * @since 1.0.0
46 * @access protected
47 * @var array
48 */
49 protected $fields = array();
50
51 /**
52 * Add "Settings" menu.
53 *
54 * @since 1.0.0
55 */
56 public function admin_menu() {
57 add_submenu_page(
58 'automatic-youtube-gallery',
59 __( 'Settings', 'automatic-youtube-gallery' ),
60 __( 'Settings', 'automatic-youtube-gallery' ),
61 'manage_options',
62 'automatic-youtube-gallery-settings',
63 array( $this, 'display_settings_form' )
64 );
65 }
66
67 /**
68 * Display settings form.
69 *
70 * @since 1.0.0
71 */
72 public function display_settings_form() {
73 $gallery_settings = get_option( 'ayg_gallery_settings' );
74
75 $active_tab = isset( $_GET['tab'] ) && array_key_exists( $_GET['tab'], $this->tabs ) ? sanitize_text_field( $_GET['tab'] ) : 'general';
76 $active_theme = $gallery_settings['theme'];
77 $pagination_type = $gallery_settings['pagination_type'];
78
79 require_once AYG_DIR . 'admin/templates/settings.php';
80 }
81
82 /**
83 * Initiate settings.
84 *
85 * @since 1.0.0
86 */
87 public function admin_init() {
88 $this->tabs = $this->get_tabs();
89 $this->sections = $this->get_sections();
90 $this->fields = $this->get_fields();
91
92 // Initialize settings
93 $this->initialize_settings();
94 }
95
96 /**
97 * Get settings tabs.
98 *
99 * @since 1.0.0
100 * @return array $tabs Setting tabs array.
101 */
102 public function get_tabs() {
103 $tabs = array(
104 'general' => __( 'General', 'automatic-youtube-gallery' ),
105 'gallery' => __( 'Gallery', 'automatic-youtube-gallery' ),
106 'player' => __( 'Player', 'automatic-youtube-gallery' ),
107 'livestream' => __( 'Livestream', 'automatic-youtube-gallery' ),
108 'privacy' => __( 'GDPR Compliance', 'automatic-youtube-gallery' )
109 );
110
111 return apply_filters( 'ayg_settings_tabs', $tabs );
112 }
113
114 /**
115 * Get settings sections.
116 *
117 * @since 1.0.0
118 * @return array $sections Setting sections array.
119 */
120 public function get_sections() {
121 $sections = array(
122 array(
123 'id' => 'ayg_general_settings',
124 'title' => __( 'General Settings', 'automatic-youtube-gallery' ),
125 'description' => '',
126 'tab' => 'general'
127 ),
128 array(
129 'id' => 'ayg_gallery_settings',
130 'title' => __( 'Gallery Settings', 'automatic-youtube-gallery' ),
131 'description' => '',
132 'tab' => 'gallery'
133 ),
134 array(
135 'id' => 'ayg_player_settings',
136 'title' => __( 'Player Settings', 'automatic-youtube-gallery' ),
137 'description' => '',
138 'tab' => 'player'
139 ),
140 array(
141 'id' => 'ayg_livestream_settings',
142 'title' => __( 'Livestream Settings', 'automatic-youtube-gallery' ),
143 'description' => '',
144 'tab' => 'livestream'
145 ),
146 array(
147 'id' => 'ayg_privacy_settings',
148 'title' => __( 'GDPR Compliance', 'automatic-youtube-gallery' ),
149 'description' => __( 'These options will help with privacy restrictions such as GDPR and the EU Cookie Law.', 'automatic-youtube-gallery' ),
150 'tab' => 'privacy'
151 ),
152 );
153
154 return apply_filters( 'ayg_settings_sections', $sections );
155 }
156
157 /**
158 * Get settings fields.
159 *
160 * @since 1.0.0
161 * @return array $fields Setting fields array.
162 */
163 public function get_fields() {
164 $fields = array(
165 'ayg_general_settings' => array(
166 array(
167 'name' => 'api_key',
168 'label' => __( 'Youtube API Key', 'automatic-youtube-gallery' ),
169 'description' => sprintf(
170 __( 'Follow <a href="%s" target="_blank">this guide</a> to get your own API key.', 'automatic-youtube-gallery' ),
171 'https://plugins360.com/automatic-youtube-gallery/how-to-get-youtube-api-key/'
172 ),
173 'type' => 'text',
174 'sanitize_callback' => 'sanitize_text_field'
175 ),
176 array(
177 'name' => 'development_mode',
178 'label' => __( 'Development Mode', 'automatic-youtube-gallery' ),
179 'description' => __( 'Does not cache API results when checked. We strongly recommend disabling this option when your site is live.', 'automatic-youtube-gallery' ),
180 'type' => 'checkbox',
181 'sanitize_callback' => 'intval'
182 ),
183 ),
184 'ayg_gallery_settings' => ayg_get_gallery_settings_fields(),
185 'ayg_player_settings' => ayg_get_player_settings_fields(),
186 'ayg_livestream_settings' => array(
187 array(
188 'name' => 'fallback_message',
189 'label' => __( 'Fallback Message', 'automatic-youtube-gallery' ),
190 'description' => __( 'Enter your Custom HTML message that should be displayed when there is no video streaming live.', 'automatic-youtube-gallery' ),
191 'type' => 'wysiwyg',
192 'sanitize_callback' => 'wp_kses_post'
193 )
194 ),
195 'ayg_privacy_settings' => array(
196 array(
197 'name' => 'cookie_consent',
198 'label' => __( 'Cookie Consent', 'automatic-youtube-gallery' ),
199 'description' => __( 'Ask for viewer consent to store YouTube cookies before showing videos.', 'automatic-youtube-gallery' ),
200 'type' => 'checkbox',
201 'sanitize_callback' => 'intval'
202 ),
203 array(
204 'name' => 'consent_message',
205 'label' => __( 'Consent Message', 'automatic-youtube-gallery' ),
206 'description' => '',
207 'type' => 'wysiwyg',
208 'sanitize_callback' => 'wp_kses_post'
209 ),
210 array(
211 'name' => 'button_label',
212 'label' => __( 'Button Label', 'automatic-youtube-gallery' ),
213 'description' => '',
214 'type' => 'text',
215 'sanitize_callback' => 'sanitize_text_field'
216 )
217 )
218 );
219
220 return apply_filters( 'ayg_settings_fields', $fields );
221 }
222
223 /**
224 * Initialize and registers the settings sections and fields to WordPress.
225 *
226 * @since 1.0.0
227 */
228 public function initialize_settings() {
229 // Register settings sections & fields
230 foreach ( $this->sections as $section ) {
231
232 $page_hook = "ayg_{$section['tab']}_settings";
233
234 // Sections
235 if ( false == get_option( $section['id'] ) ) {
236 add_option( $section['id'] );
237 }
238
239 if ( isset( $section['description'] ) && ! empty( $section['description'] ) ) {
240 $callback = array( $this, 'settings_section_callback' );
241 } elseif ( isset( $section['callback'] ) ) {
242 $callback = $section['callback'];
243 } else {
244 $callback = null;
245 }
246
247 add_settings_section( $section['id'], $section['title'], $callback, $page_hook );
248
249 // Fields
250 $fields = $this->fields[ $section['id'] ];
251
252 foreach ( $fields as $option ) {
253 $name = $option['name'];
254 $type = isset( $option['type'] ) ? $option['type'] : 'text';
255 $label = isset( $option['label'] ) ? $option['label'] : '';
256 $callback = isset( $option['callback'] ) ? $option['callback'] : array( $this, 'callback_' . $type );
257 $args = array(
258 'id' => $name,
259 'class' => isset( $option['class'] ) ? $option['class'] : $name,
260 'label_for' => "{$section['id']}[{$name}]",
261 'description' => isset( $option['description'] ) ? $option['description'] : '',
262 'name' => $label,
263 'section' => $section['id'],
264 'size' => isset( $option['size'] ) ? $option['size'] : null,
265 'options' => isset( $option['options'] ) ? $option['options'] : '',
266 'sanitize_callback' => isset( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : '',
267 'type' => $type,
268 'placeholder' => isset( $option['placeholder'] ) ? $option['placeholder'] : '',
269 'min' => isset( $option['min'] ) ? $option['min'] : '',
270 'max' => isset( $option['max'] ) ? $option['max'] : '',
271 'step' => isset( $option['step'] ) ? $option['step'] : ''
272 );
273
274 add_settings_field( "{$section['id']}[{$name}]", $label, $callback, $page_hook, $section['id'], $args );
275 }
276
277 // Creates our settings in the options table
278 register_setting( $page_hook, $section['id'], array( $this, 'sanitize_options' ) );
279 }
280 }
281
282 /**
283 * Displays a section description.
284 *
285 * @since 1.0.0
286 * @param array $args Settings section args.
287 */
288 public function settings_section_callback( $args ) {
289 foreach ( $this->sections as $section ) {
290 if ( $section['id'] == $args['id'] ) {
291 printf( '<div class="inside">%s</div>', wp_kses_post( $section['description'] ) );
292 break;
293 }
294 }
295 }
296
297 /**
298 * Displays a text field for a settings field.
299 *
300 * @since 1.0.0
301 * @param array $args Settings field args.
302 */
303 public function callback_text( $args ) {
304 $value = esc_attr( $this->get_option( $args['id'], $args['section'], '' ) );
305 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
306 $type = isset( $args['type'] ) ? $args['type'] : 'text';
307 $placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="' . $args['placeholder'] . '"';
308
309 $html = sprintf( '<input type="%1$s" class="%2$s-text" id="%3$s[%4$s]" name="%3$s[%4$s]" value="%5$s"%6$s/>', $type, $size, $args['section'], $args['id'], $value, $placeholder );
310 $html .= $this->get_field_description( $args );
311
312 echo $html;
313 }
314
315 /**
316 * Displays a url field for a settings field.
317 *
318 * @since 1.0.0
319 * @param array $args Settings field args.
320 */
321 public function callback_url( $args ) {
322 $this->callback_text( $args );
323 }
324
325 /**
326 * Displays a number field for a settings field.
327 *
328 * @since 1.0.0
329 * @param array $args Settings field args.
330 */
331 public function callback_number( $args ) {
332 $value = esc_attr( $this->get_option( $args['id'], $args['section'], 0 ) );
333 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
334 $type = isset( $args['type'] ) ? $args['type'] : 'number';
335 $placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="' . $args['placeholder'] . '"';
336 $min = empty( $args['min'] ) ? '' : ' min="' . $args['min'] . '"';
337 $max = empty( $args['max'] ) ? '' : ' max="' . $args['max'] . '"';
338 $step = empty( $args['max'] ) ? '' : ' step="' . $args['step'] . '"';
339
340 $html = sprintf( '<input type="%1$s" class="%2$s-number" id="%3$s[%4$s]" name="%3$s[%4$s]" value="%5$s"%6$s%7$s%8$s%9$s/>', $type, $size, $args['section'], $args['id'], $value, $placeholder, $min, $max, $step );
341 $html .= $this->get_field_description( $args );
342
343 echo $html;
344 }
345
346 /**
347 * Displays a checkbox for a settings field.
348 *
349 * @since 1.0.0
350 * @param array $args Settings field args.
351 */
352 public function callback_checkbox( $args ) {
353 $value = esc_attr( $this->get_option( $args['id'], $args['section'], 0 ) );
354
355 $html = '<fieldset>';
356 $html .= sprintf( '<label for="%1$s[%2$s]">', $args['section'], $args['id'] );
357 $html .= sprintf( '<input type="hidden" name="%1$s[%2$s]" value="0" />', $args['section'], $args['id'] );
358 $html .= sprintf( '<input type="checkbox" class="checkbox" id="%1$s[%2$s]" name="%1$s[%2$s]" value="1" %3$s />', $args['section'], $args['id'], checked( $value, 1, false ) );
359 $html .= sprintf( '%1$s</label>', $args['description'] );
360 $html .= '</fieldset>';
361
362 echo $html;
363 }
364
365 /**
366 * Displays a multicheckbox for a settings field.
367 *
368 * @since 1.0.0
369 * @param array $args Settings field args.
370 */
371 public function callback_multicheck( $args ) {
372 $value = $this->get_option( $args['id'], $args['section'], array() );
373
374 $html = '<fieldset>';
375 $html .= sprintf( '<input type="hidden" name="%1$s[%2$s]" value="" />', $args['section'], $args['id'] );
376 foreach ( $args['options'] as $key => $label ) {
377 $checked = in_array( $key, $value ) ? 'checked="checked"' : '';
378 $html .= sprintf( '<label for="%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key );
379 $html .= sprintf( '<input type="checkbox" class="checkbox" id="%1$s[%2$s][%3$s]" name="%1$s[%2$s][%3$s]" value="%3$s" %4$s />', $args['section'], $args['id'], $key, $checked );
380 $html .= sprintf( '%1$s</label><br>', $label );
381 }
382 $html .= $this->get_field_description( $args );
383 $html .= '</fieldset>';
384
385 echo $html;
386 }
387
388 /**
389 * Displays a radio button for a settings field.
390 *
391 * @since 1.0.0
392 * @param array $args Settings field args.
393 */
394 public function callback_radio( $args ) {
395 $value = $this->get_option( $args['id'], $args['section'], '' );
396
397 $html = '<fieldset>';
398 foreach ( $args['options'] as $key => $label ) {
399 $html .= sprintf( '<label for="%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key );
400 $html .= sprintf( '<input type="radio" class="radio" id="%1$s[%2$s][%3$s]" name="%1$s[%2$s]" value="%3$s" %4$s />', $args['section'], $args['id'], $key, checked( $value, $key, false ) );
401 $html .= sprintf( '%1$s</label><br>', $label );
402 }
403 $html .= $this->get_field_description( $args );
404 $html .= '</fieldset>';
405
406 echo $html;
407 }
408
409 /**
410 * Displays a selectbox for a settings field.
411 *
412 * @since 1.0.0
413 * @param array $args Settings field args.
414 */
415 public function callback_select( $args ) {
416 $value = esc_attr( $this->get_option( $args['id'], $args['section'], '' ) );
417 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
418
419 $html = sprintf( '<select class="%1$s" name="%2$s[%3$s]" id="%2$s[%3$s]">', $size, $args['section'], $args['id'] );
420 foreach ( $args['options'] as $key => $label ) {
421 $html .= sprintf( '<option value="%s"%s>%s</option>', $key, selected( $value, $key, false ), $label );
422 }
423 $html .= sprintf( '</select>' );
424 $html .= $this->get_field_description( $args );
425
426 echo $html;
427 }
428
429 /**
430 * Displays a textarea for a settings field.
431 *
432 * @since 1.0.0
433 * @param array $args Settings field args.
434 */
435 public function callback_textarea( $args ) {
436 $value = esc_textarea( $this->get_option( $args['id'], $args['section'], '' ) );
437 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
438 $placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="'.$args['placeholder'].'"';
439
440 $html = sprintf( '<textarea rows="5" cols="55" class="%1$s-text" id="%2$s[%3$s]" name="%2$s[%3$s]"%4$s>%5$s</textarea>', $size, $args['section'], $args['id'], $placeholder, $value );
441 $html .= $this->get_field_description( $args );
442
443 echo $html;
444 }
445
446 /**
447 * Displays the html for a settings field.
448 *
449 * @since 1.0.0
450 * @param array $args Settings field args.
451 */
452 public function callback_html( $args ) {
453 echo $this->get_field_description( $args );
454 }
455
456 /**
457 * Displays a rich text textarea for a settings field.
458 *
459 * @since 1.0.0
460 * @param array $args Settings field args.
461 */
462 public function callback_wysiwyg( $args ) {
463 $value = $this->get_option( $args['id'], $args['section'], '' );
464 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : '500px';
465
466 echo '<div style="max-width: ' . $size . ';">';
467 $editor_settings = array(
468 'teeny' => true,
469 'textarea_name' => $args['section'] . '[' . $args['id'] . ']',
470 'textarea_rows' => 10
471 );
472 if ( isset( $args['options'] ) && is_array( $args['options'] ) ) {
473 $editor_settings = array_merge( $editor_settings, $args['options'] );
474 }
475 wp_editor( $value, $args['section'] . '-' . $args['id'], $editor_settings );
476 echo '</div>';
477 echo $this->get_field_description( $args );
478 }
479
480 /**
481 * Displays a file upload field for a settings field.
482 *
483 * @since 1.0.0
484 * @param array $args Settings field args.
485 */
486 public function callback_file( $args ) {
487 $value = esc_attr( $this->get_option( $args['id'], $args['section'], '' ) );
488 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
489 $id = $args['section'] . '[' . $args['id'] . ']';
490 $label = isset( $args['options']['button_label'] ) ? $args['options']['button_label'] : __( 'Choose File', 'automatic-youtube-gallery' );
491
492 $html = sprintf( '<input type="text" class="%1$s-text ayg-settings-url" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s"/>', $size, $args['section'], $args['id'], $value );
493 $html .= '<input type="button" class="button ayg-settings-browse" value="' . $label . '" />';
494 $html .= $this->get_field_description( $args );
495
496 echo $html;
497 }
498
499 /**
500 * Displays a password field for a settings field.
501 *
502 * @since 1.0.0
503 * @param array $args Settings field args.
504 */
505 public function callback_password( $args ) {
506 $value = esc_attr( $this->get_option( $args['id'], $args['section'], '' ) );
507 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
508
509 $html = sprintf( '<input type="password" class="%1$s-text" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s"/>', $size, $args['section'], $args['id'], $value );
510 $html .= $this->get_field_description( $args );
511
512 echo $html;
513 }
514
515 /**
516 * Displays a color picker field for a settings field.
517 *
518 * @since 1.0.0
519 * @param array $args Settings field args.
520 */
521 public function callback_color( $args ) {
522 $value = esc_attr( $this->get_option( $args['id'], $args['section'], '#ffffff' ) );
523 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
524
525 $html = sprintf( '<input type="text" class="%1$s-text ayg-color-picker" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s" data-default-color="%5$s" />', $size, $args['section'], $args['id'], $value, '#ffffff' );
526 $html .= $this->get_field_description( $args );
527
528 echo $html;
529 }
530
531 /**
532 * Displays a select box for creating the pages select box.
533 *
534 * @since 1.0.0
535 * @param array $args Settings field args.
536 */
537 public function callback_pages( $args ) {
538 $dropdown_args = array(
539 'show_option_none' => '-- ' . __( 'Select a page', 'automatic-youtube-gallery' ) . ' --',
540 'option_none_value' => -1,
541 'selected' => esc_attr($this->get_option($args['id'], $args['section'], -1 ) ),
542 'name' => $args['section'] . '[' . $args['id'] . ']',
543 'id' => $args['section'] . '[' . $args['id'] . ']',
544 'echo' => 0
545 );
546
547 $html = wp_dropdown_pages( $dropdown_args );
548 $html .= $this->get_field_description( $args );
549
550 echo $html;
551 }
552
553 /**
554 * Get field description for display.
555 *
556 * @since 1.0.0
557 * @param array $args Settings field args.
558 */
559 public function get_field_description( $args ) {
560 if ( ! empty( $args['description'] ) ) {
561 $description = sprintf( '<p class="description">%s</p>', $args['description'] );
562 } else {
563 $description = '';
564 }
565
566 return $description;
567 }
568
569 /**
570 * Sanitize callback for Settings API.
571 *
572 * @since 1.0.0
573 * @param array $options The unsanitized collection of options.
574 * @return The collection of sanitized values.
575 */
576 public function sanitize_options( $options ) {
577 if ( ! $options ) {
578 return $options;
579 }
580
581 foreach ( $options as $option_slug => $option_value ) {
582 $sanitize_callback = $this->get_sanitize_callback( $option_slug );
583
584 // If callback is set, call it
585 if ( $sanitize_callback ) {
586 $options[ $option_slug ] = call_user_func( $sanitize_callback, $option_value );
587 continue;
588 }
589 }
590
591 return $options;
592 }
593
594 /**
595 * Get sanitization callback for given option slug.
596 *
597 * @since 1.0.0
598 * @param string $slug Option slug.
599 * @return mixed String or bool false.
600 */
601 public function get_sanitize_callback( $slug = '' ) {
602 if ( empty( $slug ) ) {
603 return false;
604 }
605
606 // Iterate over registered fields and see if we can find proper callback
607 foreach ( $this->fields as $section => $options ) {
608 foreach ( $options as $option ) {
609 if ( $option['name'] != $slug ) {
610 continue;
611 }
612
613 // Return the callback name
614 return isset( $option['sanitize_callback'] ) && is_callable( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : false;
615 }
616 }
617
618 return false;
619 }
620
621 /**
622 * Get the value of a settings field
623 *
624 * @since 1.0.0
625 * @param string $option Settings field name.
626 * @param string $section The section name this field belongs to
627 * @param string $default Default text if it's not found.
628 * @return string
629 */
630 public function get_option( $option, $section, $default = '' ) {
631 $options = get_option( $section );
632
633 if ( ! empty( $options[ $option ] ) ) {
634 return $options[ $option ];
635 }
636
637 return $default;
638 }
639
640 /**
641 * Dump our plugin transients.
642 *
643 * @since 1.3.0
644 */
645 public function ajax_callback_delete_cache() {
646 check_ajax_referer( 'ayg_ajax_nonce', 'security' );
647
648 if ( current_user_can( 'manage_options' ) ) {
649 ayg_delete_cache();
650 }
651
652 wp_die();
653 }
654
655 }
656