tabs ) ? sanitize_text_field( $_GET['tab'] ) : 'general';
$active_theme = $gallery_settings['theme'];
require_once AYG_DIR . 'admin/templates/settings.php';
}
/**
* Initiate settings.
*
* @since 1.0.0
*/
public function admin_init() {
$this->tabs = $this->get_tabs();
$this->sections = $this->get_sections();
$this->fields = $this->get_fields();
// Initialize settings
$this->initialize_settings();
}
/**
* Get settings tabs.
*
* @since 1.0.0
* @return array $tabs Setting tabs array.
*/
public function get_tabs() {
$tabs = array(
'general' => __( 'General', 'automatic-youtube-gallery' ),
'gallery' => __( 'Gallery', 'automatic-youtube-gallery' ),
'player' => __( 'Player', 'automatic-youtube-gallery' )
);
return apply_filters( 'ayg_settings_tabs', $tabs );
}
/**
* Get settings sections.
*
* @since 1.0.0
* @return array $sections Setting sections array.
*/
public function get_sections() {
$sections = array(
array(
'id' => 'ayg_general_settings',
'title' => __( 'General Settings', 'automatic-youtube-gallery' ),
'tab' => 'general'
),
array(
'id' => 'ayg_gallery_settings',
'title' => __( 'Gallery Settings', 'automatic-youtube-gallery' ),
'tab' => 'gallery'
),
array(
'id' => 'ayg_player_settings',
'title' => __( 'Player Settings', 'automatic-youtube-gallery' ),
'tab' => 'player'
)
);
return apply_filters( 'ayg_settings_sections', $sections );
}
/**
* Get settings fields.
*
* @since 1.0.0
* @return array $fields Setting fields array.
*/
public function get_fields() {
$fields = array(
'ayg_general_settings' => array(
array(
'name' => 'api_key',
'label' => __( 'Youtube API Key', 'automatic-youtube-gallery' ),
'description' => sprintf(
__( 'Follow this guide to get your own API key.', 'automatic-youtube-gallery' ),
'https://plugins360.com/automatic-youtube-gallery/how-to-get-youtube-api-key/'
),
'type' => 'text',
'sanitize_callback' => 'sanitize_text_field'
)
),
'ayg_gallery_settings' => ayg_get_gallery_settings_fields(),
'ayg_player_settings' => ayg_get_player_settings_fields()
);
return apply_filters( 'ayg_settings_fields', $fields );
}
/**
* Initialize and registers the settings sections and fields to WordPress.
*
* @since 1.0.0
*/
public function initialize_settings() {
// Register settings sections & fields
foreach ( $this->sections as $section ) {
$page_hook = "ayg_{$section['tab']}_settings";
// Sections
if ( false == get_option( $section['id'] ) ) {
add_option( $section['id'] );
}
if ( isset( $section['description'] ) && ! empty( $section['description'] ) ) {
$callback = array( $this, 'settings_section_callback' );
} elseif ( isset( $section['callback'] ) ) {
$callback = $section['callback'];
} else {
$callback = null;
}
add_settings_section( $section['id'], $section['title'], $callback, $page_hook );
// Fields
$fields = $this->fields[ $section['id'] ];
foreach ( $fields as $option ) {
$name = $option['name'];
$type = isset( $option['type'] ) ? $option['type'] : 'text';
$label = isset( $option['label'] ) ? $option['label'] : '';
$callback = isset( $option['callback'] ) ? $option['callback'] : array( $this, 'callback_' . $type );
$args = array(
'id' => $name,
'class' => isset( $option['class'] ) ? $option['class'] : $name,
'label_for' => "{$section['id']}[{$name}]",
'description' => isset( $option['description'] ) ? $option['description'] : '',
'name' => $label,
'section' => $section['id'],
'size' => isset( $option['size'] ) ? $option['size'] : null,
'options' => isset( $option['options'] ) ? $option['options'] : '',
'sanitize_callback' => isset( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : '',
'type' => $type,
'placeholder' => isset( $option['placeholder'] ) ? $option['placeholder'] : '',
'min' => isset( $option['min'] ) ? $option['min'] : '',
'max' => isset( $option['max'] ) ? $option['max'] : '',
'step' => isset( $option['step'] ) ? $option['step'] : ''
);
add_settings_field( "{$section['id']}[{$name}]", $label, $callback, $page_hook, $section['id'], $args );
}
// Creates our settings in the options table
register_setting( $page_hook, $section['id'], array( $this, 'sanitize_options' ) );
}
}
/**
* Displays a section description.
*
* @since 1.0.0
* @param array $args Settings section args.
*/
public function settings_section_callback( $args ) {
foreach ( $this->sections as $section ) {
if ( $section['id'] == $args['id'] ) {
printf( '
%s
', sanitize_text_field( $section['description'] ) );
break;
}
}
}
/**
* Displays a text field for a settings field.
*
* @since 1.0.0
* @param array $args Settings field args.
*/
public function callback_text( $args ) {
$value = esc_attr( $this->get_option( $args['id'], $args['section'], '' ) );
$size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
$type = isset( $args['type'] ) ? $args['type'] : 'text';
$placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="' . $args['placeholder'] . '"';
$html = sprintf( '', $type, $size, $args['section'], $args['id'], $value, $placeholder );
$html .= $this->get_field_description( $args );
echo $html;
}
/**
* Displays a url field for a settings field.
*
* @since 1.0.0
* @param array $args Settings field args.
*/
public function callback_url( $args ) {
$this->callback_text( $args );
}
/**
* Displays a number field for a settings field.
*
* @since 1.0.0
* @param array $args Settings field args.
*/
public function callback_number( $args ) {
$value = esc_attr( $this->get_option( $args['id'], $args['section'], 0 ) );
$size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
$type = isset( $args['type'] ) ? $args['type'] : 'number';
$placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="' . $args['placeholder'] . '"';
$min = empty( $args['min'] ) ? '' : ' min="' . $args['min'] . '"';
$max = empty( $args['max'] ) ? '' : ' max="' . $args['max'] . '"';
$step = empty( $args['max'] ) ? '' : ' step="' . $args['step'] . '"';
$html = sprintf( '', $type, $size, $args['section'], $args['id'], $value, $placeholder, $min, $max, $step );
$html .= $this->get_field_description( $args );
echo $html;
}
/**
* Displays a checkbox for a settings field.
*
* @since 1.0.0
* @param array $args Settings field args.
*/
public function callback_checkbox( $args ) {
$value = esc_attr( $this->get_option( $args['id'], $args['section'], 0 ) );
$html = '';
echo $html;
}
/**
* Displays a multicheckbox for a settings field.
*
* @since 1.0.0
* @param array $args Settings field args.
*/
public function callback_multicheck( $args ) {
$value = $this->get_option( $args['id'], $args['section'], array() );
$html = '';
echo $html;
}
/**
* Displays a radio button for a settings field.
*
* @since 1.0.0
* @param array $args Settings field args.
*/
public function callback_radio( $args ) {
$value = $this->get_option( $args['id'], $args['section'], '' );
$html = '';
echo $html;
}
/**
* Displays a selectbox for a settings field.
*
* @since 1.0.0
* @param array $args Settings field args.
*/
public function callback_select( $args ) {
$value = esc_attr( $this->get_option( $args['id'], $args['section'], '' ) );
$size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
$html = sprintf( '' );
$html .= $this->get_field_description( $args );
echo $html;
}
/**
* Displays a textarea for a settings field.
*
* @since 1.0.0
* @param array $args Settings field args.
*/
public function callback_textarea( $args ) {
$value = esc_textarea( $this->get_option( $args['id'], $args['section'], '' ) );
$size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
$placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="'.$args['placeholder'].'"';
$html = sprintf( '', $size, $args['section'], $args['id'], $placeholder, $value );
$html .= $this->get_field_description( $args );
echo $html;
}
/**
* Displays the html for a settings field.
*
* @since 1.0.0
* @param array $args Settings field args.
*/
public function callback_html( $args ) {
echo $this->get_field_description( $args );
}
/**
* Displays a rich text textarea for a settings field.
*
* @since 1.0.0
* @param array $args Settings field args.
*/
public function callback_wysiwyg( $args ) {
$value = $this->get_option( $args['id'], $args['section'], '' );
$size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : '500px';
echo '';
$editor_settings = array(
'teeny' => true,
'textarea_name' => $args['section'] . '[' . $args['id'] . ']',
'textarea_rows' => 10
);
if ( isset( $args['options'] ) && is_array( $args['options'] ) ) {
$editor_settings = array_merge( $editor_settings, $args['options'] );
}
wp_editor( $value, $args['section'] . '-' . $args['id'], $editor_settings );
echo '
';
echo $this->get_field_description( $args );
}
/**
* Displays a file upload field for a settings field.
*
* @since 1.0.0
* @param array $args Settings field args.
*/
public function callback_file( $args ) {
$value = esc_attr( $this->get_option( $args['id'], $args['section'], '' ) );
$size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
$id = $args['section'] . '[' . $args['id'] . ']';
$label = isset( $args['options']['button_label'] ) ? $args['options']['button_label'] : __( 'Choose File', 'automatic-youtube-gallery' );
$html = sprintf( '', $size, $args['section'], $args['id'], $value );
$html .= '';
$html .= $this->get_field_description( $args );
echo $html;
}
/**
* Displays a password field for a settings field.
*
* @since 1.0.0
* @param array $args Settings field args.
*/
public function callback_password( $args ) {
$value = esc_attr( $this->get_option( $args['id'], $args['section'], '' ) );
$size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
$html = sprintf( '', $size, $args['section'], $args['id'], $value );
$html .= $this->get_field_description( $args );
echo $html;
}
/**
* Displays a color picker field for a settings field.
*
* @since 1.0.0
* @param array $args Settings field args.
*/
public function callback_color( $args ) {
$value = esc_attr( $this->get_option( $args['id'], $args['section'], '#ffffff' ) );
$size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
$html = sprintf( '', $size, $args['section'], $args['id'], $value, '#ffffff' );
$html .= $this->get_field_description( $args );
echo $html;
}
/**
* Displays a select box for creating the pages select box.
*
* @since 1.0.0
* @param array $args Settings field args.
*/
public function callback_pages( $args ) {
$dropdown_args = array(
'show_option_none' => '-- ' . __( 'Select a page', 'automatic-youtube-gallery' ) . ' --',
'option_none_value' => -1,
'selected' => esc_attr($this->get_option($args['id'], $args['section'], -1 ) ),
'name' => $args['section'] . '[' . $args['id'] . ']',
'id' => $args['section'] . '[' . $args['id'] . ']',
'echo' => 0
);
$html = wp_dropdown_pages( $dropdown_args );
$html .= $this->get_field_description( $args );
echo $html;
}
/**
* Get field description for display.
*
* @since 1.0.0
* @param array $args Settings field args.
*/
public function get_field_description( $args ) {
if ( ! empty( $args['description'] ) ) {
$description = sprintf( '%s
', $args['description'] );
} else {
$description = '';
}
return $description;
}
/**
* Sanitize callback for Settings API.
*
* @since 1.0.0
* @param array $options The unsanitized collection of options.
* @return The collection of sanitized values.
*/
public function sanitize_options( $options ) {
if ( ! $options ) {
return $options;
}
foreach ( $options as $option_slug => $option_value ) {
$sanitize_callback = $this->get_sanitize_callback( $option_slug );
// If callback is set, call it
if ( $sanitize_callback ) {
$options[ $option_slug ] = call_user_func( $sanitize_callback, $option_value );
continue;
}
}
return $options;
}
/**
* Get sanitization callback for given option slug.
*
* @since 1.0.0
* @param string $slug Option slug.
* @return mixed String or bool false.
*/
public function get_sanitize_callback( $slug = '' ) {
if ( empty( $slug ) ) {
return false;
}
// Iterate over registered fields and see if we can find proper callback
foreach ( $this->fields as $section => $options ) {
foreach ( $options as $option ) {
if ( $option['name'] != $slug ) {
continue;
}
// Return the callback name
return isset( $option['sanitize_callback'] ) && is_callable( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : false;
}
}
return false;
}
/**
* Get the value of a settings field
*
* @since 1.0.0
* @param string $option Settings field name.
* @param string $section The section name this field belongs to
* @param string $default Default text if it's not found.
* @return string
*/
public function get_option( $option, $section, $default = '' ) {
$options = get_option( $section );
if ( ! empty( $options[ $option ] ) ) {
return $options[ $option ];
}
return $default;
}
/**
* Dump our plugin transients.
*
* @since 1.3.0
*/
public function ajax_callback_delete_cache() {
check_ajax_referer( 'ayg_admin_ajax_nonce', 'security' );
// Get the current list of transients
$transient_keys = get_option( 'ayg_transient_keys', array() );
// For each key, delete that transient
foreach ( $transient_keys as $key ) {
delete_transient( $key );
}
// Reset our DB value
update_option( 'ayg_transient_keys', array() );
wp_die();
}
}