| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Automatic YouTube Gallery Widget. |
| 5 |
* |
| 6 |
* @link https://plugins360.com |
| 7 |
* @since 2.1.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_Widget class. |
| 19 |
* |
| 20 |
* @since 2.1.0 |
| 21 |
*/ |
| 22 |
class AYG_Widget extends WP_Widget { |
| 23 |
|
| 24 |
/** |
| 25 |
* Get things going. |
| 26 |
* |
| 27 |
* @since 2.1.0 |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
$widget_slug = 'ayg-widget'; |
| 31 |
|
| 32 |
parent::__construct( |
| 33 |
$widget_slug, |
| 34 |
__( 'Automatic YouTube Gallery', 'automatic-youtube-gallery' ), |
| 35 |
array( |
| 36 |
'classname' => $widget_slug, |
| 37 |
'description' => __( 'Displays automated YouTube video gallery.', 'automatic-youtube-gallery' ) |
| 38 |
) |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Outputs the content of the widget. |
| 44 |
* |
| 45 |
* @since 2.1.0 |
| 46 |
* @param array $args The array of form elements. |
| 47 |
* @param array $instance The current instance of the widget. |
| 48 |
*/ |
| 49 |
public function widget( $args, $instance ) { |
| 50 |
// Process output |
| 51 |
echo $args['before_widget']; |
| 52 |
|
| 53 |
if ( ! empty( $instance['title'] ) ) { |
| 54 |
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title']; |
| 55 |
} |
| 56 |
|
| 57 |
// Deprecated since v2.5.8. Retained for backward compatibility. |
| 58 |
if ( isset( $args['widget_id'] ) ) { |
| 59 |
$instance['deprecated_uid'] = md5( $args['widget_id'] ); |
| 60 |
} |
| 61 |
|
| 62 |
// If popup is enabled and type is video, force theme to popup |
| 63 |
if ( isset( $instance['popup'] ) && ! empty( $instance['popup'] ) ) { |
| 64 |
if ( isset( $instance['type'] ) && 'video' == $instance['type'] ) { |
| 65 |
$instance['theme'] = 'popup'; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
echo ayg_build_gallery( $instance ); |
| 70 |
|
| 71 |
echo $args['after_widget']; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Processes the widget's options to be saved. |
| 76 |
* |
| 77 |
* @since 2.1.0 |
| 78 |
* @param array $new_instance The new instance of values to be generated via the update. |
| 79 |
* @param array $old_instance The previous instance of values before the update. |
| 80 |
*/ |
| 81 |
public function update( $new_instance, $old_instance ) { |
| 82 |
$instance = array(); |
| 83 |
$instance['title'] = isset( $new_instance['title'] ) ? strip_tags( $new_instance['title'] ) : ''; |
| 84 |
|
| 85 |
$fields = ayg_get_editor_fields(); |
| 86 |
|
| 87 |
foreach ( $fields as $section ) { |
| 88 |
foreach ( $section['fields'] as $field ) { |
| 89 |
$name = $field['name']; |
| 90 |
|
| 91 |
if ( 'checkbox' === $field['type'] ) { |
| 92 |
$instance[ $name ] = ! empty( $new_instance[ $name ] ) ? 1 : 0; |
| 93 |
} elseif ( ! isset( $new_instance[ $name ] ) ) { |
| 94 |
continue; |
| 95 |
} elseif ( 'select' === $field['type'] && isset( $field['options'] ) ) { |
| 96 |
$raw = sanitize_text_field( $new_instance[ $name ] ); |
| 97 |
$instance[ $name ] = array_key_exists( $raw, $field['options'] ) ? $raw : sanitize_text_field( $field['value'] ); |
| 98 |
} else { |
| 99 |
$sanitize_callback = ! empty( $field['sanitize_callback'] ) ? $field['sanitize_callback'] : 'sanitize_text_field'; |
| 100 |
$instance[ $name ] = call_user_func( $sanitize_callback, $new_instance[ $name ] ); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
return $instance; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Generates the administration form for the widget. |
| 110 |
* |
| 111 |
* @since 2.1.0 |
| 112 |
* @param array $instance The array of keys and values for the widget. |
| 113 |
*/ |
| 114 |
public function form( $instance ) { |
| 115 |
// Define the array of defaults |
| 116 |
$fields = ayg_get_editor_fields(); |
| 117 |
|
| 118 |
$defaults = array( |
| 119 |
'title' => __( 'Automatic YouTube Gallery', 'automatic-youtube-gallery' ) |
| 120 |
); |
| 121 |
|
| 122 |
foreach ( $fields as $key => $value ) { |
| 123 |
foreach ( $value['fields'] as $field ) { |
| 124 |
$defaults[ $field['name'] ] = $field['value']; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
// Parse incoming $instance into an array and merge it with $defaults |
| 129 |
$instance = wp_parse_args( |
| 130 |
(array) $instance, |
| 131 |
$defaults |
| 132 |
); |
| 133 |
|
| 134 |
// Display the admin form |
| 135 |
include AYG_DIR . 'widget/templates/admin.php'; |
| 136 |
} |
| 137 |
|
| 138 |
} |
| 139 |
|