PluginProbe
Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels / 1.6.4
Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels v1.6.4
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 1.6.4, at admin/settings.php

621 lines 21.6 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
78 require_once AYG_DIR . 'admin/templates/settings.php';
79 }
80
81 /**
82 * Initiate settings.
83 *
84 * @since 1.0.0
85 */
86 public function admin_init() {
87 $this->tabs = $this->get_tabs();
88 $this->sections = $this->get_sections();
89 $this->fields = $this->get_fields();
90
91 // Initialize settings
92 $this->initialize_settings();
93 }
94
95 /**
96 * Get settings tabs.
97 *
98 * @since 1.0.0
99 * @return array $tabs Setting tabs array.
100 */
101 public function get_tabs() {
102 $tabs = array(
103 'general' => __( 'General', 'automatic-youtube-gallery' ),
104 'gallery' => __( 'Gallery', 'automatic-youtube-gallery' ),
105 'player' => __( 'Player', 'automatic-youtube-gallery' ),
106 'livestream' => __( 'Live Stream', 'automatic-youtube-gallery' )
107 );
108
109 return apply_filters( 'ayg_settings_tabs', $tabs );
110 }
111
112 /**
113 * Get settings sections.
114 *
115 * @since 1.0.0
116 * @return array $sections Setting sections array.
117 */
118 public function get_sections() {
119 $sections = array(
120 array(
121 'id' => 'ayg_general_settings',
122 'title' => __( 'General Settings', 'automatic-youtube-gallery' ),
123 'tab' => 'general'
124 ),
125 array(
126 'id' => 'ayg_gallery_settings',
127 'title' => __( 'Gallery Settings', 'automatic-youtube-gallery' ),
128 'tab' => 'gallery'
129 ),
130 array(
131 'id' => 'ayg_player_settings',
132 'title' => __( 'Player Settings', 'automatic-youtube-gallery' ),
133 'tab' => 'player'
134 ),
135 array(
136 'id' => 'ayg_livestream_settings',
137 'title' => __( 'Live Stream', 'automatic-youtube-gallery' ),
138 'tab' => 'livestream'
139 )
140 );
141
142 return apply_filters( 'ayg_settings_sections', $sections );
143 }
144
145 /**
146 * Get settings fields.
147 *
148 * @since 1.0.0
149 * @return array $fields Setting fields array.
150 */
151 public function get_fields() {
152 $fields = array(
153 'ayg_general_settings' => array(
154 array(
155 'name' => 'api_key',
156 'label' => __( 'Youtube API Key', 'automatic-youtube-gallery' ),
157 'description' => sprintf(
158 __( 'Follow <a href="%s" target="_blank">this guide</a> to get your own API key.', 'automatic-youtube-gallery' ),
159 'https://plugins360.com/automatic-youtube-gallery/how-to-get-youtube-api-key/'
160 ),
161 'type' => 'text',
162 'sanitize_callback' => 'sanitize_text_field'
163 )
164 ),
165 'ayg_gallery_settings' => ayg_get_gallery_settings_fields(),
166 'ayg_player_settings' => ayg_get_player_settings_fields(),
167 'ayg_livestream_settings' => array(
168 array(
169 'name' => 'fallback_message',
170 'label' => __( 'Fallback Message', 'automatic-youtube-gallery' ),
171 'description' => __( 'Enter your Custom HTML message that should be displayed when there is no video streaming live.', 'automatic-youtube-gallery' ),
172 'type' => 'wysiwyg',
173 'sanitize_callback' => 'wp_kses_post'
174 )
175 )
176 );
177
178 return apply_filters( 'ayg_settings_fields', $fields );
179 }
180
181 /**
182 * Initialize and registers the settings sections and fields to WordPress.
183 *
184 * @since 1.0.0
185 */
186 public function initialize_settings() {
187 // Register settings sections & fields
188 foreach ( $this->sections as $section ) {
189
190 $page_hook = "ayg_{$section['tab']}_settings";
191
192 // Sections
193 if ( false == get_option( $section['id'] ) ) {
194 add_option( $section['id'] );
195 }
196
197 if ( isset( $section['description'] ) && ! empty( $section['description'] ) ) {
198 $callback = array( $this, 'settings_section_callback' );
199 } elseif ( isset( $section['callback'] ) ) {
200 $callback = $section['callback'];
201 } else {
202 $callback = null;
203 }
204
205 add_settings_section( $section['id'], $section['title'], $callback, $page_hook );
206
207 // Fields
208 $fields = $this->fields[ $section['id'] ];
209
210 foreach ( $fields as $option ) {
211 $name = $option['name'];
212 $type = isset( $option['type'] ) ? $option['type'] : 'text';
213 $label = isset( $option['label'] ) ? $option['label'] : '';
214 $callback = isset( $option['callback'] ) ? $option['callback'] : array( $this, 'callback_' . $type );
215 $args = array(
216 'id' => $name,
217 'class' => isset( $option['class'] ) ? $option['class'] : $name,
218 'label_for' => "{$section['id']}[{$name}]",
219 'description' => isset( $option['description'] ) ? $option['description'] : '',
220 'name' => $label,
221 'section' => $section['id'],
222 'size' => isset( $option['size'] ) ? $option['size'] : null,
223 'options' => isset( $option['options'] ) ? $option['options'] : '',
224 'sanitize_callback' => isset( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : '',
225 'type' => $type,
226 'placeholder' => isset( $option['placeholder'] ) ? $option['placeholder'] : '',
227 'min' => isset( $option['min'] ) ? $option['min'] : '',
228 'max' => isset( $option['max'] ) ? $option['max'] : '',
229 'step' => isset( $option['step'] ) ? $option['step'] : ''
230 );
231
232 add_settings_field( "{$section['id']}[{$name}]", $label, $callback, $page_hook, $section['id'], $args );
233 }
234
235 // Creates our settings in the options table
236 register_setting( $page_hook, $section['id'], array( $this, 'sanitize_options' ) );
237 }
238 }
239
240 /**
241 * Displays a section description.
242 *
243 * @since 1.0.0
244 * @param array $args Settings section args.
245 */
246 public function settings_section_callback( $args ) {
247 foreach ( $this->sections as $section ) {
248 if ( $section['id'] == $args['id'] ) {
249 printf( '<div class="inside">%s</div>', sanitize_text_field( $section['description'] ) );
250 break;
251 }
252 }
253 }
254
255 /**
256 * Displays a text field for a settings field.
257 *
258 * @since 1.0.0
259 * @param array $args Settings field args.
260 */
261 public function callback_text( $args ) {
262 $value = esc_attr( $this->get_option( $args['id'], $args['section'], '' ) );
263 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
264 $type = isset( $args['type'] ) ? $args['type'] : 'text';
265 $placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="' . $args['placeholder'] . '"';
266
267 $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 );
268 $html .= $this->get_field_description( $args );
269
270 echo $html;
271 }
272
273 /**
274 * Displays a url field for a settings field.
275 *
276 * @since 1.0.0
277 * @param array $args Settings field args.
278 */
279 public function callback_url( $args ) {
280 $this->callback_text( $args );
281 }
282
283 /**
284 * Displays a number field for a settings field.
285 *
286 * @since 1.0.0
287 * @param array $args Settings field args.
288 */
289 public function callback_number( $args ) {
290 $value = esc_attr( $this->get_option( $args['id'], $args['section'], 0 ) );
291 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
292 $type = isset( $args['type'] ) ? $args['type'] : 'number';
293 $placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="' . $args['placeholder'] . '"';
294 $min = empty( $args['min'] ) ? '' : ' min="' . $args['min'] . '"';
295 $max = empty( $args['max'] ) ? '' : ' max="' . $args['max'] . '"';
296 $step = empty( $args['max'] ) ? '' : ' step="' . $args['step'] . '"';
297
298 $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 );
299 $html .= $this->get_field_description( $args );
300
301 echo $html;
302 }
303
304 /**
305 * Displays a checkbox for a settings field.
306 *
307 * @since 1.0.0
308 * @param array $args Settings field args.
309 */
310 public function callback_checkbox( $args ) {
311 $value = esc_attr( $this->get_option( $args['id'], $args['section'], 0 ) );
312
313 $html = '<fieldset>';
314 $html .= sprintf( '<label for="%1$s[%2$s]">', $args['section'], $args['id'] );
315 $html .= sprintf( '<input type="hidden" name="%1$s[%2$s]" value="0" />', $args['section'], $args['id'] );
316 $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 ) );
317 $html .= sprintf( '%1$s</label>', $args['description'] );
318 $html .= '</fieldset>';
319
320 echo $html;
321 }
322
323 /**
324 * Displays a multicheckbox for a settings field.
325 *
326 * @since 1.0.0
327 * @param array $args Settings field args.
328 */
329 public function callback_multicheck( $args ) {
330 $value = $this->get_option( $args['id'], $args['section'], array() );
331
332 $html = '<fieldset>';
333 $html .= sprintf( '<input type="hidden" name="%1$s[%2$s]" value="" />', $args['section'], $args['id'] );
334 foreach ( $args['options'] as $key => $label ) {
335 $checked = in_array( $key, $value ) ? 'checked="checked"' : '';
336 $html .= sprintf( '<label for="%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key );
337 $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 );
338 $html .= sprintf( '%1$s</label><br>', $label );
339 }
340 $html .= $this->get_field_description( $args );
341 $html .= '</fieldset>';
342
343 echo $html;
344 }
345
346 /**
347 * Displays a radio button for a settings field.
348 *
349 * @since 1.0.0
350 * @param array $args Settings field args.
351 */
352 public function callback_radio( $args ) {
353 $value = $this->get_option( $args['id'], $args['section'], '' );
354
355 $html = '<fieldset>';
356 foreach ( $args['options'] as $key => $label ) {
357 $html .= sprintf( '<label for="%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key );
358 $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 ) );
359 $html .= sprintf( '%1$s</label><br>', $label );
360 }
361 $html .= $this->get_field_description( $args );
362 $html .= '</fieldset>';
363
364 echo $html;
365 }
366
367 /**
368 * Displays a selectbox for a settings field.
369 *
370 * @since 1.0.0
371 * @param array $args Settings field args.
372 */
373 public function callback_select( $args ) {
374 $value = esc_attr( $this->get_option( $args['id'], $args['section'], '' ) );
375 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
376
377 $html = sprintf( '<select class="%1$s" name="%2$s[%3$s]" id="%2$s[%3$s]">', $size, $args['section'], $args['id'] );
378 foreach ( $args['options'] as $key => $label ) {
379 $html .= sprintf( '<option value="%s"%s>%s</option>', $key, selected( $value, $key, false ), $label );
380 }
381 $html .= sprintf( '</select>' );
382 $html .= $this->get_field_description( $args );
383
384 echo $html;
385 }
386
387 /**
388 * Displays a textarea for a settings field.
389 *
390 * @since 1.0.0
391 * @param array $args Settings field args.
392 */
393 public function callback_textarea( $args ) {
394 $value = esc_textarea( $this->get_option( $args['id'], $args['section'], '' ) );
395 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
396 $placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="'.$args['placeholder'].'"';
397
398 $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 );
399 $html .= $this->get_field_description( $args );
400
401 echo $html;
402 }
403
404 /**
405 * Displays the html for a settings field.
406 *
407 * @since 1.0.0
408 * @param array $args Settings field args.
409 */
410 public function callback_html( $args ) {
411 echo $this->get_field_description( $args );
412 }
413
414 /**
415 * Displays a rich text textarea for a settings field.
416 *
417 * @since 1.0.0
418 * @param array $args Settings field args.
419 */
420 public function callback_wysiwyg( $args ) {
421 $value = $this->get_option( $args['id'], $args['section'], '' );
422 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : '500px';
423
424 echo '<div style="max-width: ' . $size . ';">';
425 $editor_settings = array(
426 'teeny' => true,
427 'textarea_name' => $args['section'] . '[' . $args['id'] . ']',
428 'textarea_rows' => 10
429 );
430 if ( isset( $args['options'] ) && is_array( $args['options'] ) ) {
431 $editor_settings = array_merge( $editor_settings, $args['options'] );
432 }
433 wp_editor( $value, $args['section'] . '-' . $args['id'], $editor_settings );
434 echo '</div>';
435 echo $this->get_field_description( $args );
436 }
437
438 /**
439 * Displays a file upload field for a settings field.
440 *
441 * @since 1.0.0
442 * @param array $args Settings field args.
443 */
444 public function callback_file( $args ) {
445 $value = esc_attr( $this->get_option( $args['id'], $args['section'], '' ) );
446 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
447 $id = $args['section'] . '[' . $args['id'] . ']';
448 $label = isset( $args['options']['button_label'] ) ? $args['options']['button_label'] : __( 'Choose File', 'automatic-youtube-gallery' );
449
450 $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 );
451 $html .= '<input type="button" class="button ayg-settings-browse" value="' . $label . '" />';
452 $html .= $this->get_field_description( $args );
453
454 echo $html;
455 }
456
457 /**
458 * Displays a password field for a settings field.
459 *
460 * @since 1.0.0
461 * @param array $args Settings field args.
462 */
463 public function callback_password( $args ) {
464 $value = esc_attr( $this->get_option( $args['id'], $args['section'], '' ) );
465 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
466
467 $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 );
468 $html .= $this->get_field_description( $args );
469
470 echo $html;
471 }
472
473 /**
474 * Displays a color picker field for a settings field.
475 *
476 * @since 1.0.0
477 * @param array $args Settings field args.
478 */
479 public function callback_color( $args ) {
480 $value = esc_attr( $this->get_option( $args['id'], $args['section'], '#ffffff' ) );
481 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
482
483 $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' );
484 $html .= $this->get_field_description( $args );
485
486 echo $html;
487 }
488
489 /**
490 * Displays a select box for creating the pages select box.
491 *
492 * @since 1.0.0
493 * @param array $args Settings field args.
494 */
495 public function callback_pages( $args ) {
496 $dropdown_args = array(
497 'show_option_none' => '-- ' . __( 'Select a page', 'automatic-youtube-gallery' ) . ' --',
498 'option_none_value' => -1,
499 'selected' => esc_attr($this->get_option($args['id'], $args['section'], -1 ) ),
500 'name' => $args['section'] . '[' . $args['id'] . ']',
501 'id' => $args['section'] . '[' . $args['id'] . ']',
502 'echo' => 0
503 );
504
505 $html = wp_dropdown_pages( $dropdown_args );
506 $html .= $this->get_field_description( $args );
507
508 echo $html;
509 }
510
511 /**
512 * Get field description for display.
513 *
514 * @since 1.0.0
515 * @param array $args Settings field args.
516 */
517 public function get_field_description( $args ) {
518 if ( ! empty( $args['description'] ) ) {
519 $description = sprintf( '<p class="description">%s</p>', $args['description'] );
520 } else {
521 $description = '';
522 }
523
524 return $description;
525 }
526
527 /**
528 * Sanitize callback for Settings API.
529 *
530 * @since 1.0.0
531 * @param array $options The unsanitized collection of options.
532 * @return The collection of sanitized values.
533 */
534 public function sanitize_options( $options ) {
535 if ( ! $options ) {
536 return $options;
537 }
538
539 foreach ( $options as $option_slug => $option_value ) {
540 $sanitize_callback = $this->get_sanitize_callback( $option_slug );
541
542 // If callback is set, call it
543 if ( $sanitize_callback ) {
544 $options[ $option_slug ] = call_user_func( $sanitize_callback, $option_value );
545 continue;
546 }
547 }
548
549 return $options;
550 }
551
552 /**
553 * Get sanitization callback for given option slug.
554 *
555 * @since 1.0.0
556 * @param string $slug Option slug.
557 * @return mixed String or bool false.
558 */
559 public function get_sanitize_callback( $slug = '' ) {
560 if ( empty( $slug ) ) {
561 return false;
562 }
563
564 // Iterate over registered fields and see if we can find proper callback
565 foreach ( $this->fields as $section => $options ) {
566 foreach ( $options as $option ) {
567 if ( $option['name'] != $slug ) {
568 continue;
569 }
570
571 // Return the callback name
572 return isset( $option['sanitize_callback'] ) && is_callable( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : false;
573 }
574 }
575
576 return false;
577 }
578
579 /**
580 * Get the value of a settings field
581 *
582 * @since 1.0.0
583 * @param string $option Settings field name.
584 * @param string $section The section name this field belongs to
585 * @param string $default Default text if it's not found.
586 * @return string
587 */
588 public function get_option( $option, $section, $default = '' ) {
589 $options = get_option( $section );
590
591 if ( ! empty( $options[ $option ] ) ) {
592 return $options[ $option ];
593 }
594
595 return $default;
596 }
597
598 /**
599 * Dump our plugin transients.
600 *
601 * @since 1.3.0
602 */
603 public function ajax_callback_delete_cache() {
604 check_ajax_referer( 'ayg_admin_ajax_nonce', 'security' );
605
606 // Get the current list of transients
607 $transient_keys = get_option( 'ayg_transient_keys', array() );
608
609 // For each key, delete that transient
610 foreach ( $transient_keys as $key ) {
611 delete_transient( $key );
612 }
613
614 // Reset our DB value
615 update_option( 'ayg_transient_keys', array() );
616
617 wp_die();
618 }
619
620 }
621