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

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