| 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 |
|
| 84 |
$instance['title'] = isset( $new_instance['title'] ) ? strip_tags( $new_instance['title'] ) : ''; |
| 85 |
|
| 86 |
$fields = ayg_get_editor_fields(); |
| 87 |
|
| 88 |
foreach ( $fields as $key => $value ) { |
| 89 |
foreach ( $value['fields'] as $field ) { |
| 90 |
$field_name = $field['name']; |
| 91 |
$field_type = $field['type']; |
| 92 |
|
| 93 |
if ( 'number' == $field_type ) { |
| 94 |
if ( ! empty( $new_instance[ $field_name ] ) ) { |
| 95 |
$instance[ $field_name ] = false === strpos( $new_instance[ $field_name ], '.' ) ? (int) $new_instance[ $field_name ] : (float) $new_instance[ $field_name ]; |
| 96 |
} else { |
| 97 |
$instance[ $field_name ] = 0; |
| 98 |
} |
| 99 |
} elseif ( 'checkbox' == $field_type ) { |
| 100 |
$instance[ $field_name ] = isset( $new_instance[ $field_name ] ) ? (int) $new_instance[ $field_name ] : 0; |
| 101 |
} elseif ( 'textarea' == $field_type ) { |
| 102 |
$instance[ $field_name ] = ! empty( $new_instance[ $field_name ] ) ? sanitize_textarea_field( $new_instance[ $field_name ] ) : ''; |
| 103 |
} else { |
| 104 |
$instance[ $field_name ] = ! empty( $new_instance[ $field_name ] ) ? sanitize_text_field( $new_instance[ $field_name ] ) : ''; |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
return $instance; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Generates the administration form for the widget. |
| 114 |
* |
| 115 |
* @since 2.1.0 |
| 116 |
* @param array $instance The array of keys and values for the widget. |
| 117 |
*/ |
| 118 |
public function form( $instance ) { |
| 119 |
// Define the array of defaults |
| 120 |
$fields = ayg_get_editor_fields(); |
| 121 |
|
| 122 |
$defaults = array( |
| 123 |
'title' => __( 'Automatic YouTube Gallery', 'automatic-youtube-gallery' ) |
| 124 |
); |
| 125 |
|
| 126 |
foreach ( $fields as $key => $value ) { |
| 127 |
foreach ( $value['fields'] as $field ) { |
| 128 |
$defaults[ $field['name'] ] = $field['value']; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
// Parse incoming $instance into an array and merge it with $defaults |
| 133 |
$instance = wp_parse_args( |
| 134 |
(array) $instance, |
| 135 |
$defaults |
| 136 |
); |
| 137 |
|
| 138 |
// Display the admin form |
| 139 |
include AYG_DIR . 'widget/templates/admin.php'; |
| 140 |
} |
| 141 |
|
| 142 |
} |
| 143 |
|