tabs ) ? sanitize_text_field( $_GET['tab'] ) : 'general'; $active_theme = $gallery_settings['theme']; $pagination_type = $gallery_settings['pagination_type']; 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' ), 'livestream' => __( 'Livestream', 'automatic-youtube-gallery' ), 'privacy' => __( 'GDPR Compliance', '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' ), 'description' => '', 'tab' => 'general' ), array( 'id' => 'ayg_gallery_settings', 'title' => __( 'Gallery Settings', 'automatic-youtube-gallery' ), 'description' => '', 'tab' => 'gallery' ), array( 'id' => 'ayg_player_settings', 'title' => __( 'Player Settings', 'automatic-youtube-gallery' ), 'description' => '', 'tab' => 'player' ), array( 'id' => 'ayg_livestream_settings', 'title' => __( 'Livestream Settings', 'automatic-youtube-gallery' ), 'description' => '', 'tab' => 'livestream' ), array( 'id' => 'ayg_privacy_settings', 'title' => __( 'GDPR Compliance', 'automatic-youtube-gallery' ), 'description' => __( 'These options will help with privacy restrictions such as GDPR and the EU Cookie Law.', 'automatic-youtube-gallery' ), 'tab' => 'privacy' ), ); 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' ), array( 'name' => 'development_mode', 'label' => __( 'Development Mode', 'automatic-youtube-gallery' ), 'description' => __( 'Does not cache API results when checked. We strongly recommend disabling this option when your site is live.', 'automatic-youtube-gallery' ), 'type' => 'checkbox', 'sanitize_callback' => 'intval' ), ), 'ayg_gallery_settings' => ayg_get_gallery_settings_fields(), 'ayg_player_settings' => ayg_get_player_settings_fields(), 'ayg_livestream_settings' => array( array( 'name' => 'fallback_message', 'label' => __( 'Fallback Message', 'automatic-youtube-gallery' ), 'description' => __( 'Enter your Custom HTML message that should be displayed when there is no video streaming live.', 'automatic-youtube-gallery' ), 'type' => 'wysiwyg', 'sanitize_callback' => 'wp_kses_post' ) ), 'ayg_privacy_settings' => array( array( 'name' => 'cookie_consent', 'label' => __( 'Cookie Consent', 'automatic-youtube-gallery' ), 'description' => __( 'Ask for viewer consent to store YouTube cookies before showing videos.', 'automatic-youtube-gallery' ), 'type' => 'checkbox', 'sanitize_callback' => 'intval' ), array( 'name' => 'consent_message', 'label' => __( 'Consent Message', 'automatic-youtube-gallery' ), 'description' => '', 'type' => 'wysiwyg', 'sanitize_callback' => 'wp_kses_post' ), array( 'name' => 'button_label', 'label' => __( 'Button Label', 'automatic-youtube-gallery' ), 'description' => '', 'type' => 'text', 'sanitize_callback' => 'sanitize_text_field' ) ) ); 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
', $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_ajax_nonce', 'security' ); ayg_delete_cache(); wp_die(); } }