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