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

776 lines 31.0 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 require_once AYG_DIR . 'admin/templates/settings.php';
74 }
75
76 /**
77 * Initiate settings.
78 *
79 * @since 1.0.0
80 */
81 public function admin_init() {
82 $this->tabs = $this->get_tabs();
83 $this->sections = $this->get_sections();
84 $this->fields = $this->get_fields();
85
86 // Initialize settings
87 $this->initialize_settings();
88 }
89
90 /**
91 * Get settings tabs.
92 *
93 * @since 1.0.0
94 * @return array $tabs Setting tabs array.
95 */
96 public function get_tabs() {
97 $tabs = array(
98 'general' => __( 'General', 'automatic-youtube-gallery' ),
99 'gallery' => __( 'Gallery', 'automatic-youtube-gallery' ),
100 'player' => __( 'Player', 'automatic-youtube-gallery' ),
101 'livestream' => __( 'Livestream', 'automatic-youtube-gallery' ),
102 'privacy' => __( 'GDPR Compliance', 'automatic-youtube-gallery' )
103 );
104
105 return apply_filters( 'ayg_settings_tabs', $tabs );
106 }
107
108 /**
109 * Get settings sections.
110 *
111 * @since 1.0.0
112 * @return array $sections Setting sections array.
113 */
114 public function get_sections() {
115 $sections = array(
116 array(
117 'id' => 'ayg_general_settings',
118 'title' => __( 'General Settings', 'automatic-youtube-gallery' ),
119 'description' => '',
120 'tab' => 'general',
121 'page' => 'ayg_general_settings'
122 ),
123 array(
124 'id' => 'ayg_strings_settings',
125 'title' => __( 'Button & Link Labels', 'automatic-youtube-gallery' ),
126 'description' => '',
127 'tab' => 'general',
128 'page' => 'ayg_strings_settings'
129 ),
130 array(
131 'id' => 'ayg_gallery_settings',
132 'title' => __( 'Gallery Settings', 'automatic-youtube-gallery' ),
133 'description' => '',
134 'tab' => 'gallery',
135 'page' => 'ayg_gallery_settings'
136 ),
137 array(
138 'id' => 'ayg_player_settings',
139 'title' => __( 'Player Settings', 'automatic-youtube-gallery' ),
140 'description' => '',
141 'tab' => 'player',
142 'page' => 'ayg_player_settings'
143 ),
144 array(
145 'id' => 'ayg_livestream_settings',
146 'title' => __( 'Livestream Settings', 'automatic-youtube-gallery' ),
147 'description' => '',
148 'tab' => 'livestream',
149 'page' => 'ayg_livestream_settings'
150 ),
151 array(
152 'id' => 'ayg_privacy_settings',
153 'title' => __( 'GDPR Compliance', 'automatic-youtube-gallery' ),
154 'description' => '',
155 'tab' => 'privacy',
156 'page' => 'ayg_privacy_settings'
157 ),
158 );
159
160 return apply_filters( 'ayg_settings_sections', $sections );
161 }
162
163 /**
164 * Get settings fields.
165 *
166 * @since 1.0.0
167 * @return array $fields Setting fields array.
168 */
169 public function get_fields() {
170 // General Settings
171 $fields['ayg_general_settings'] = array(
172 array(
173 'name' => 'api_key',
174 'label' => __( 'YouTube API Key', 'automatic-youtube-gallery' ),
175 'description' => sprintf(
176 __( 'A free YouTube Data API key is required to fetch your videos. <a href="%s" target="_blank" rel="noopener noreferrer">Get your API key</a> in just a few minutes.', 'automatic-youtube-gallery' ),
177 'https://plugins360.com/automatic-youtube-gallery/how-to-get-youtube-api-key/'
178 ),
179 'type' => 'text',
180 'sanitize_callback' => 'sanitize_text_field'
181 ),
182 array(
183 'name' => 'force_load_assets',
184 'label' => __( 'Force Load Plugin Assets', 'automatic-youtube-gallery' ),
185 'description' => __( 'Force-load the plugin\'s CSS and/or JavaScript files on all front-end pages. Enable this option only if layouts do not render correctly due to page builders or theme conflicts.', 'automatic-youtube-gallery' ),
186 'type' => 'multicheck',
187 'options' => array(
188 'css' => __( 'Force load CSS', 'automatic-youtube-gallery' ),
189 'js' => __( 'Force load JavaScript', 'automatic-youtube-gallery' ),
190 ),
191 'sanitize_callback' => 'ayg_sanitize_array'
192 ),
193 array(
194 'name' => 'lazyload',
195 'label' => __( 'Lazy Load Images / Videos', 'automatic-youtube-gallery' ),
196 'description' => __( 'Check this option to lazy load images and videos added by the plugin to enhance page load speed and performance. If you experience any issues with content display, try disabling this option.', 'automatic-youtube-gallery' ),
197 'type' => 'checkbox',
198 'sanitize_callback' => 'intval'
199 ),
200 array(
201 'name' => 'development_mode',
202 'label' => __( 'Development Mode', 'automatic-youtube-gallery' ),
203 'description' => __( 'Check this option to stop caching API results. We strongly recommend disabling this option when your site is live.', 'automatic-youtube-gallery' ),
204 'type' => 'checkbox',
205 'sanitize_callback' => 'intval'
206 )
207 );
208
209 // Strings Settings
210 $fields['ayg_strings_settings'] = array(
211 array(
212 'name' => 'more_button_label',
213 'label' => __( 'More Button Label', 'automatic-youtube-gallery' ),
214 'description' => __( 'Text for the "Load More" button when pagination type is set to "More Button".', 'automatic-youtube-gallery' ),
215 'type' => 'text',
216 'sanitize_callback' => 'sanitize_text_field'
217 ),
218 array(
219 'name' => 'previous_button_label',
220 'label' => __( 'Previous Button Label', 'automatic-youtube-gallery' ),
221 'description' => __( 'Text for the "Previous" button when pagination type is set to "Pager".', 'automatic-youtube-gallery' ),
222 'type' => 'text',
223 'sanitize_callback' => 'sanitize_text_field'
224 ),
225 array(
226 'name' => 'next_button_label',
227 'label' => __( 'Next Button Label', 'automatic-youtube-gallery' ),
228 'description' => __( 'Text for the "Next" button when pagination type is set to "Pager".', 'automatic-youtube-gallery' ),
229 'type' => 'text',
230 'sanitize_callback' => 'sanitize_text_field'
231 ),
232 array(
233 'name' => 'show_more_label',
234 'label' => __( 'Show More Label', 'automatic-youtube-gallery' ),
235 'description' => __( 'Text for the "Show More" link that expands the video description below the player.', 'automatic-youtube-gallery' ),
236 'type' => 'text',
237 'sanitize_callback' => 'sanitize_text_field'
238 ),
239 array(
240 'name' => 'show_less_label',
241 'label' => __( 'Show Less Label', 'automatic-youtube-gallery' ),
242 'description' => __( 'Text for the "Show Less" link that collapses the video description below the player.', 'automatic-youtube-gallery' ),
243 'type' => 'text',
244 'sanitize_callback' => 'sanitize_text_field'
245 )
246 );
247
248 // Gallery Settings
249 $gallery_settings = ayg_get_gallery_settings_fields();
250
251 $gallery_settings[] = array(
252 'name' => 'scroll_top_offset',
253 'label' => __( 'Page Scroll Top Offset', 'automatic-youtube-gallery' ),
254 'description' => __( 'Set the top offset in pixels for scrolling to the video player after a thumbnail is clicked. Enter <code>-1</code> to disable automatic scrolling.', 'automatic-youtube-gallery' ),
255 'type' => 'text',
256 'sanitize_callback' => 'ayg_sanitize_int'
257 );
258
259 $fields['ayg_gallery_settings'] = $gallery_settings;
260
261 // Player Settings
262 $player_settings = array(
263 array(
264 'name' => 'player_type',
265 'label' => __( 'Player Type', 'automatic-youtube-gallery' ),
266 'description' => __( 'Choose how videos are embedded. "Native YouTube Embed" uses YouTube\'s standard iframe, while "Custom Video Player" provides a lightweight, styled player with more control over its appearance.', 'automatic-youtube-gallery' ),
267 'type' => 'radio',
268 'options' => array(
269 'youtube' => __( 'Native YouTube Embed', 'automatic-youtube-gallery' ),
270 'custom' => __( 'Custom Video Player', 'automatic-youtube-gallery' )
271 ),
272 'sanitize_callback' => 'sanitize_text_field'
273 ),
274 array(
275 'name' => 'player_color',
276 'label' => __( 'Player Color', 'automatic-youtube-gallery' ),
277 'description' => __( 'Set the theme color for your video player.', 'automatic-youtube-gallery' ),
278 'type' => 'color',
279 'sanitize_callback' => 'sanitize_text_field'
280 )
281 );
282
283 $player_settings = array_merge( $player_settings, ayg_get_player_settings_fields() );
284
285 $player_settings[] = array(
286 'name' => 'privacy_enhanced_mode',
287 'label' => __( 'Privacy Enhanced Mode', 'automatic-youtube-gallery' ),
288 'description' => __( "Check this option to prevent YouTube from leaving tracking cookies on your visitor's browsers unless they actually play the videos. Please uncheck this option if you see errors while testing your playlist embeds or watching your videos on mobile.", 'automatic-youtube-gallery' ),
289 'type' => 'checkbox',
290 'sanitize_callback' => 'intval'
291 );
292
293 $player_settings[] = array(
294 'name' => 'origin',
295 'label' => __( 'Extra Player Security', 'automatic-youtube-gallery' ),
296 'description' => __( 'Check this option to add site origin information with each embed code as an extra security measure. In YouTube\'s own words, checking this option "protects against malicious third-party JavaScript being injected into your page and hijacking control of your YouTube player."', 'automatic-youtube-gallery' ),
297 'type' => 'checkbox',
298 'sanitize_callback' => 'intval'
299 );
300
301 $fields['ayg_player_settings'] = $player_settings;
302
303 // Livestream Settings
304 $fields['ayg_livestream_settings'] = array(
305 array(
306 'name' => 'fallback_message',
307 'label' => __( 'Fallback Message', 'automatic-youtube-gallery' ),
308 'description' => __( 'Enter your Custom HTML message that should be displayed when there is no video streaming live.', 'automatic-youtube-gallery' ),
309 'type' => 'wysiwyg',
310 'sanitize_callback' => 'wp_kses_post'
311 )
312 );
313
314 // Privacy Settings
315 $fields['ayg_privacy_settings'] = array(
316 array(
317 'name' => 'cookie_consent',
318 'label' => __( 'Cookie Consent', 'automatic-youtube-gallery' ),
319 'description' => __( 'Check this option to ask for viewer consent to store YouTube cookies before showing videos.', 'automatic-youtube-gallery' ),
320 'type' => 'checkbox',
321 'sanitize_callback' => 'intval'
322 ),
323 array(
324 'name' => 'consent_message',
325 'label' => __( 'Consent Message', 'automatic-youtube-gallery' ),
326 'description' => __( 'The message shown to visitors asking for their permission before YouTube videos and cookies are loaded.', 'automatic-youtube-gallery' ),
327 'type' => 'wysiwyg',
328 'sanitize_callback' => 'wp_kses_post'
329 ),
330 array(
331 'name' => 'button_label',
332 'label' => __( 'Consent Button Label', 'automatic-youtube-gallery' ),
333 'description' => __( 'Text for the button visitors click to accept and load the video (e.g. "Accept" or "Load video").', 'automatic-youtube-gallery' ),
334 'type' => 'text',
335 'sanitize_callback' => 'sanitize_text_field'
336 )
337 );
338
339 return apply_filters( 'ayg_settings_fields', $fields );
340 }
341
342 /**
343 * Initialize and registers the settings sections and fields to WordPress.
344 *
345 * @since 1.0.0
346 */
347 public function initialize_settings() {
348 // Register settings sections & fields
349 foreach ( $this->sections as $section ) {
350 $page_hook = isset( $section['page'] ) ? $section['page'] : $section['id'];
351
352 // Sections
353 if ( false == get_option( $section['id'] ) ) {
354 add_option( $section['id'] );
355 }
356
357 if ( isset( $section['description'] ) && ! empty( $section['description'] ) ) {
358 $callback = array( $this, 'settings_section_callback' );
359 } elseif ( isset( $section['callback'] ) ) {
360 $callback = $section['callback'];
361 } else {
362 $callback = null;
363 }
364
365 add_settings_section( $section['id'], $section['title'], $callback, $page_hook );
366
367 // Fields
368 $fields = $this->fields[ $section['id'] ];
369
370 foreach ( $fields as $option ) {
371 $name = $option['name'];
372 $type = isset( $option['type'] ) ? $option['type'] : 'text';
373 $label = isset( $option['label'] ) ? $option['label'] : '';
374 $callback = isset( $option['callback'] ) ? $option['callback'] : array( $this, 'callback_' . $type );
375 $args = array(
376 'id' => $name,
377 'class' => isset( $option['class'] ) ? $option['class'] : $name,
378 'label_for' => "{$section['id']}[{$name}]",
379 'description' => isset( $option['description'] ) ? $option['description'] : '',
380 'name' => $label,
381 'section' => $section['id'],
382 'size' => isset( $option['size'] ) ? $option['size'] : null,
383 'options' => isset( $option['options'] ) ? $option['options'] : '',
384 'sanitize_callback' => isset( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : '',
385 'type' => $type,
386 'placeholder' => isset( $option['placeholder'] ) ? $option['placeholder'] : '',
387 'min' => isset( $option['min'] ) ? $option['min'] : '',
388 'max' => isset( $option['max'] ) ? $option['max'] : '',
389 'step' => isset( $option['step'] ) ? $option['step'] : ''
390 );
391
392 add_settings_field( "{$section['id']}[{$name}]", $label, $callback, $page_hook, $section['id'], $args );
393 }
394
395 // Creates our settings in the options table
396 register_setting( $page_hook, $section['id'], array( $this, 'sanitize_options' ) );
397 }
398 }
399
400 /**
401 * Displays a section description.
402 *
403 * @since 1.0.0
404 * @param array $args Settings section args.
405 */
406 public function settings_section_callback( $args ) {
407 foreach ( $this->sections as $section ) {
408 if ( $section['id'] == $args['id'] ) {
409 printf( '<div class="inside">%s</div>', wp_kses_post( $section['description'] ) );
410 break;
411 }
412 }
413 }
414
415 /**
416 * Displays a text field for a settings field.
417 *
418 * @since 1.0.0
419 * @param array $args Settings field args.
420 */
421 public function callback_text( $args ) {
422 $value = esc_attr( $this->get_option( $args['id'], $args['section'], '' ) );
423 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular-text';
424 $type = isset( $args['type'] ) ? $args['type'] : 'text';
425 $placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="' . $args['placeholder'] . '"';
426
427 $html = sprintf( '<input type="%1$s" class="%2$s" id="%3$s[%4$s]" name="%3$s[%4$s]" value="%5$s"%6$s/>', $type, $size, $args['section'], $args['id'], $value, $placeholder );
428 $html .= $this->get_field_description( $args );
429
430 echo $html;
431 }
432
433 /**
434 * Displays a url field for a settings field.
435 *
436 * @since 1.0.0
437 * @param array $args Settings field args.
438 */
439 public function callback_url( $args ) {
440 $this->callback_text( $args );
441 }
442
443 /**
444 * Displays a number field for a settings field.
445 *
446 * @since 1.0.0
447 * @param array $args Settings field args.
448 */
449 public function callback_number( $args ) {
450 $value = esc_attr( $this->get_option( $args['id'], $args['section'], 0 ) );
451 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular-text';
452 $type = isset( $args['type'] ) ? $args['type'] : 'number';
453 $placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="' . $args['placeholder'] . '"';
454 $min = empty( $args['min'] ) ? '' : ' min="' . $args['min'] . '"';
455 $max = empty( $args['max'] ) ? '' : ' max="' . $args['max'] . '"';
456 $step = empty( $args['max'] ) ? '' : ' step="' . $args['step'] . '"';
457
458 $html = sprintf( '<input type="%1$s" class="%2$s" 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 );
459 $html .= $this->get_field_description( $args );
460
461 echo $html;
462 }
463
464 /**
465 * Displays a checkbox for a settings field.
466 *
467 * @since 1.0.0
468 * @param array $args Settings field args.
469 */
470 public function callback_checkbox( $args ) {
471 $value = esc_attr( $this->get_option( $args['id'], $args['section'], 0 ) );
472
473 $html = '<fieldset>';
474 $html .= sprintf( '<label for="%1$s[%2$s]">', $args['section'], $args['id'] );
475 $html .= sprintf( '<input type="hidden" name="%1$s[%2$s]" value="0" />', $args['section'], $args['id'] );
476 $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 ) );
477 $html .= sprintf( '%1$s</label>', $args['description'] );
478 $html .= '</fieldset>';
479
480 echo $html;
481 }
482
483 /**
484 * Displays a multicheckbox for a settings field.
485 *
486 * @since 1.0.0
487 * @param array $args Settings field args.
488 */
489 public function callback_multicheck( $args ) {
490 $value = $this->get_option( $args['id'], $args['section'], array() );
491
492 $html = '<fieldset>';
493 $html .= sprintf( '<input type="hidden" name="%1$s[%2$s]" value="" />', $args['section'], $args['id'] );
494 foreach ( $args['options'] as $key => $label ) {
495 $checked = in_array( $key, $value ) ? 'checked="checked"' : '';
496 $html .= sprintf( '<label for="%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key );
497 $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 );
498 $html .= sprintf( '%1$s</label><br>', $label );
499 }
500 $html .= $this->get_field_description( $args );
501 $html .= '</fieldset>';
502
503 echo $html;
504 }
505
506 /**
507 * Displays a radio button for a settings field.
508 *
509 * @since 1.0.0
510 * @param array $args Settings field args.
511 */
512 public function callback_radio( $args ) {
513 $value = $this->get_option( $args['id'], $args['section'], '' );
514
515 $html = '<fieldset>';
516 foreach ( $args['options'] as $key => $label ) {
517 $html .= sprintf( '<label for="%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key );
518 $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 ) );
519 $html .= sprintf( '%1$s</label><br>', $label );
520 }
521 $html .= $this->get_field_description( $args );
522 $html .= '</fieldset>';
523
524 echo $html;
525 }
526
527 /**
528 * Displays a selectbox for a settings field.
529 *
530 * @since 1.0.0
531 * @param array $args Settings field args.
532 */
533 public function callback_select( $args ) {
534 $value = esc_attr( $this->get_option( $args['id'], $args['section'], '' ) );
535 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular-text';
536
537 $html = sprintf( '<select class="%1$s" name="%2$s[%3$s]" id="%2$s[%3$s]">', $size, $args['section'], $args['id'] );
538 foreach ( $args['options'] as $key => $label ) {
539 $html .= sprintf( '<option value="%s"%s>%s</option>', $key, selected( $value, $key, false ), $label );
540 }
541 $html .= sprintf( '</select>' );
542 $html .= $this->get_field_description( $args );
543
544 echo $html;
545 }
546
547 /**
548 * Displays a textarea for a settings field.
549 *
550 * @since 1.0.0
551 * @param array $args Settings field args.
552 */
553 public function callback_textarea( $args ) {
554 $value = esc_textarea( $this->get_option( $args['id'], $args['section'], '' ) );
555 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular-text';
556 $placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="'.$args['placeholder'].'"';
557
558 $html = sprintf( '<textarea rows="5" cols="55" class="%1$s" id="%2$s[%3$s]" name="%2$s[%3$s]"%4$s>%5$s</textarea>', $size, $args['section'], $args['id'], $placeholder, $value );
559 $html .= $this->get_field_description( $args );
560
561 echo $html;
562 }
563
564 /**
565 * Displays the html for a settings field.
566 *
567 * @since 1.0.0
568 * @param array $args Settings field args.
569 */
570 public function callback_html( $args ) {
571 echo $this->get_field_description( $args );
572 }
573
574 /**
575 * Displays a rich text textarea for a settings field.
576 *
577 * @since 1.0.0
578 * @param array $args Settings field args.
579 */
580 public function callback_wysiwyg( $args ) {
581 $value = $this->get_option( $args['id'], $args['section'], '' );
582 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : '500px';
583
584 echo '<div style="max-width: ' . $size . ';">';
585 $editor_settings = array(
586 'teeny' => true,
587 'textarea_name' => $args['section'] . '[' . $args['id'] . ']',
588 'textarea_rows' => 10
589 );
590 if ( isset( $args['options'] ) && is_array( $args['options'] ) ) {
591 $editor_settings = array_merge( $editor_settings, $args['options'] );
592 }
593 wp_editor( $value, $args['section'] . '-' . $args['id'], $editor_settings );
594 echo '</div>';
595 echo $this->get_field_description( $args );
596 }
597
598 /**
599 * Displays a file upload field for a settings field.
600 *
601 * @since 1.0.0
602 * @param array $args Settings field args.
603 */
604 public function callback_file( $args ) {
605 $value = esc_attr( $this->get_option( $args['id'], $args['section'], '' ) );
606 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular-text';
607 $id = $args['section'] . '[' . $args['id'] . ']';
608 $label = isset( $args['options']['button_label'] ) ? $args['options']['button_label'] : __( 'Choose File', 'automatic-youtube-gallery' );
609
610 $html = sprintf( '<input type="text" class="%1$s ayg-settings-url" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s"/>', $size, $args['section'], $args['id'], $value );
611 $html .= '<input type="button" class="button ayg-button-upload-media" value="' . $label . '" />';
612 $html .= $this->get_field_description( $args );
613
614 echo $html;
615 }
616
617 /**
618 * Displays a password field for a settings field.
619 *
620 * @since 1.0.0
621 * @param array $args Settings field args.
622 */
623 public function callback_password( $args ) {
624 $value = esc_attr( $this->get_option( $args['id'], $args['section'], '' ) );
625 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular-text';
626
627 $html = sprintf( '<input type="password" class="%1$s" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s"/>', $size, $args['section'], $args['id'], $value );
628 $html .= $this->get_field_description( $args );
629
630 echo $html;
631 }
632
633 /**
634 * Displays a color picker field for a settings field.
635 *
636 * @since 1.0.0
637 * @param array $args Settings field args.
638 */
639 public function callback_color( $args ) {
640 $value = esc_attr( $this->get_option( $args['id'], $args['section'], '#ffffff' ) );
641 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular-text';
642
643 $html = sprintf( '<input type="text" class="%1$s 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' );
644 $html .= $this->get_field_description( $args );
645
646 echo $html;
647 }
648
649 /**
650 * Displays a select box for creating the pages select box.
651 *
652 * @since 1.0.0
653 * @param array $args Settings field args.
654 */
655 public function callback_pages( $args ) {
656 $dropdown_args = array(
657 'show_option_none' => '-- ' . __( 'Select a page', 'automatic-youtube-gallery' ) . ' --',
658 'option_none_value' => -1,
659 'selected' => esc_attr($this->get_option($args['id'], $args['section'], -1 ) ),
660 'name' => $args['section'] . '[' . $args['id'] . ']',
661 'id' => $args['section'] . '[' . $args['id'] . ']',
662 'echo' => 0
663 );
664
665 $html = wp_dropdown_pages( $dropdown_args );
666 $html .= $this->get_field_description( $args );
667
668 echo $html;
669 }
670
671 /**
672 * Get field description for display.
673 *
674 * @since 1.0.0
675 * @param array $args Settings field args.
676 */
677 public function get_field_description( $args ) {
678 if ( ! empty( $args['description'] ) ) {
679 $description = sprintf( '<p class="description">%s</p>', $args['description'] );
680 } else {
681 $description = '';
682 }
683
684 return $description;
685 }
686
687 /**
688 * Sanitize callback for Settings API.
689 *
690 * @since 1.0.0
691 * @param array $options The unsanitized collection of options.
692 * @return The collection of sanitized values.
693 */
694 public function sanitize_options( $options ) {
695 if ( ! $options ) {
696 return $options;
697 }
698
699 foreach ( $options as $option_slug => $option_value ) {
700 $sanitize_callback = $this->get_sanitize_callback( $option_slug );
701
702 // If callback is set, call it
703 if ( $sanitize_callback ) {
704 $options[ $option_slug ] = call_user_func( $sanitize_callback, $option_value );
705 continue;
706 }
707 }
708
709 return $options;
710 }
711
712 /**
713 * Get sanitization callback for given option slug.
714 *
715 * @since 1.0.0
716 * @param string $slug Option slug.
717 * @return mixed String or bool false.
718 */
719 public function get_sanitize_callback( $slug = '' ) {
720 if ( empty( $slug ) ) {
721 return false;
722 }
723
724 // Iterate over registered fields and see if we can find proper callback
725 foreach ( $this->fields as $section => $options ) {
726 foreach ( $options as $option ) {
727 if ( $option['name'] != $slug ) {
728 continue;
729 }
730
731 // Return the callback name
732 return isset( $option['sanitize_callback'] ) && is_callable( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : false;
733 }
734 }
735
736 return false;
737 }
738
739 /**
740 * Get the value of a settings field
741 *
742 * @since 1.0.0
743 * @param string $option Settings field name.
744 * @param string $section The section name this field belongs to
745 * @param string $default Default text if it's not found.
746 * @return string
747 */
748 public function get_option( $option, $section, $default = '' ) {
749 $options = get_option( $section );
750
751 if ( ! empty( $options[ $option ] ) ) {
752 return $options[ $option ];
753 }
754
755 return $default;
756 }
757
758 /**
759 * Dump our plugin transients.
760 *
761 * @since 1.3.0
762 */
763 public function ajax_callback_delete_cache() {
764 check_ajax_referer( 'ayg_ajax_nonce', 'security' );
765
766 if ( ! current_user_can( 'manage_options' ) ) {
767 wp_send_json_error( array( 'message' => __( 'Permission denied.', 'automatic-youtube-gallery' ) ) );
768 }
769
770 ayg_delete_cache();
771
772 wp_send_json_success();
773 }
774
775 }
776