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