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

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