| 1 | <?php |
| 2 | /** |
| 3 | * Widget API: WP_Media_Widget class |
| 4 | * |
| 5 | * @package WordPress |
| 6 | * @subpackage Widgets |
| 7 | * @since 4.4.0 |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Core class that implements a media widget. |
| 12 | * |
| 13 | * @since 4.4.0 |
| 14 | * |
| 15 | * @see WP_Widget |
| 16 | */ |
| 17 | class WP_Media_Widget extends WP_Widget { |
| 18 | /** |
| 19 | * Constructor. |
| 20 | * |
| 21 | * @since 4.4.0 |
| 22 | * @access public |
| 23 | * |
| 24 | * @param string $id_base Optional Base ID for the widget, lowercase and unique. If left empty, |
| 25 | * a portion of the widget's class name will be used Has to be unique. |
| 26 | * @param string $name Optional. Name for the widget displayed on the configuration page. |
| 27 | * Default empty. |
| 28 | * @param array $widget_options Optional. Widget options. See wp_register_sidebar_widget() for |
| 29 | * information on accepted arguments. Default empty array. |
| 30 | * @param array $control_options Optional. Widget control options. See wp_register_widget_control() |
| 31 | * for information on accepted arguments. Default empty array. |
| 32 | */ |
| 33 | public function __construct( $id_base = '', $name = '', $widget_options = array(), $control_options = array() ) { |
| 34 | $widget_opts = wp_parse_args( $widget_options, array( |
| 35 | 'classname' => 'widget_media', |
| 36 | 'description' => __( 'Display media in your sidebar.' ) |
| 37 | ) ); |
| 38 | |
| 39 | $control_opts = wp_parse_args( $control_options, array() ); |
| 40 | |
| 41 | parent::__construct( |
| 42 | $id_base ? $id_base : 'wp-media-widget', |
| 43 | $name ? $name : __( 'Media' ), |
| 44 | $widget_opts, |
| 45 | $control_opts |
| 46 | ); |
| 47 | |
| 48 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) ); |
| 49 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Displays the widget on the front-end. |
| 54 | * |
| 55 | * @since 4.4.0 |
| 56 | * @access public |
| 57 | * |
| 58 | * @see WP_Widget::widget() |
| 59 | * |
| 60 | * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. |
| 61 | * @param array $instance Saved setting from the database. |
| 62 | */ |
| 63 | public function widget( $args, $instance ) { |
| 64 | extract( $instance ); |
| 65 | |
| 66 | echo $args['before_widget']; |
| 67 | if ( ! empty( $instance['title'] ) ) { |
| 68 | echo $args['before_title'] . $instance['title'] . $args['after_title']; |
| 69 | } |
| 70 | if ( ! empty( $instance['description'] ) ) { |
| 71 | echo '<p class="attachment-description align' . $instance['align'] . '">' . $instance['description'] . '</p>'; |
| 72 | } |
| 73 | if ( ! empty( $instance['link'] ) ) { |
| 74 | if ( 'file' == $instance['link'] ) { |
| 75 | $url = wp_get_attachment_url( $instance['id'] ); |
| 76 | $selectedLink = $url; |
| 77 | } else if ( 'post' == $instance['link'] ) { |
| 78 | $url = get_attachment_link( $instance['id'] ); |
| 79 | $selectedLink = $url; |
| 80 | } else { |
| 81 | $selectedLink = ''; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // Build the media output. |
| 86 | $media_output = ''; |
| 87 | |
| 88 | if ( ! empty( $instance['id'] ) ) { |
| 89 | if ( $attachment = get_post( $instance['id'] ) ) { |
| 90 | // Image. |
| 91 | if ( wp_attachment_is_image( $attachment ) ) { |
| 92 | |
| 93 | if ( ! empty( $selectedLink ) ) { |
| 94 | $media_output .= '<a href="' . $selectedLink . '"'; |
| 95 | if ( 'on' == $instance['target'] ) { |
| 96 | $media_output .= ' target="_blank"'; |
| 97 | } |
| 98 | $media_output .= '>'; |
| 99 | } |
| 100 | |
| 101 | $media_output .= wp_get_attachment_image( $instance['id'], $instance['size'], false, array( |
| 102 | 'data-id' => esc_attr( $args['widget_id'] ), |
| 103 | 'class' => 'image align' . $instance['align'], |
| 104 | 'title' => $instance['title'], |
| 105 | ) ); |
| 106 | |
| 107 | if ( ! empty( $selectedLink ) ) { |
| 108 | $media_output .= '</a>'; |
| 109 | } |
| 110 | |
| 111 | // Audio. |
| 112 | } elseif ( wp_attachment_is( 'audio', $attachment ) ) { |
| 113 | $media_output .= wp_audio_shortcode( array( |
| 114 | 'src' => wp_get_attachment_url( $attachment->ID ) |
| 115 | ) ); |
| 116 | |
| 117 | // Video. |
| 118 | } elseif( wp_attachment_is( 'video', $attachment ) ) { |
| 119 | $media_output .= wp_video_shortcode( array( |
| 120 | 'src' => wp_get_attachment_url( $attachment->ID ) |
| 121 | ) ); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | echo $media_output; |
| 127 | echo $args['after_widget']; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Sanitizes the widget form values as they are saved. |
| 132 | * |
| 133 | * @since 4.4.0 |
| 134 | * @access public |
| 135 | * |
| 136 | * @see WP_Widget::update() |
| 137 | * |
| 138 | * @param array $new_instance Values just sent to be saved. |
| 139 | * @param array $old_instance Previously saved values from database. |
| 140 | * @return array Updated safe values to be saved. |
| 141 | */ |
| 142 | function update( $new_instance, $old_instance ) { |
| 143 | $instance = $old_instance; |
| 144 | |
| 145 | // ID, title, target. |
| 146 | $instance['id'] = (int) $new_instance['id']; |
| 147 | $instance['title'] = sanitize_text_field( $new_instance['title'] ); |
| 148 | $instance['target'] = isset( $new_instance['target'] ) ? sanitize_text_field( $new_instance['target'] ) : ''; |
| 149 | |
| 150 | // Everything else. |
| 151 | $instance['align'] = sanitize_text_field( $new_instance['align'] ); |
| 152 | $instance['size'] = sanitize_text_field( $new_instance['size'] ); |
| 153 | $instance['link'] = sanitize_text_field( $new_instance['link'] ); |
| 154 | return $instance; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Renders an image attachment preview. |
| 159 | * |
| 160 | * @since 4.4.0 |
| 161 | * @access public |
| 162 | * |
| 163 | * @param WP_Post $attachment Attachment object. |
| 164 | * @param string $widget_id Widget ID. |
| 165 | * @param array $instance Current widget instance arguments. |
| 166 | */ |
| 167 | public function render_image( $attachment, $widget_id, $instance ) { |
| 168 | // If an image id is saved for this widget, display the image using `wp_get_attachment_image()`. |
| 169 | echo '<p class="media-widget-admin-preview">', |
| 170 | wp_get_attachment_image( $attachment->ID, $instance['size'], false, array( |
| 171 | 'data-id' => esc_attr( $widget_id ), |
| 172 | 'class' => 'image align' . esc_attr( $instance['align'] ), |
| 173 | 'title' => esc_attr( $instance['title'] ), |
| 174 | ) ), |
| 175 | '</p>'; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Renders an audio attachment preview. |
| 180 | * |
| 181 | * @since 4.4.0 |
| 182 | * @access public |
| 183 | * |
| 184 | * @param WP_Post $attachment Attachment object. |
| 185 | * @param string $widget_id Widget ID. |
| 186 | * @param array $instance Current widget instance arguments. |
| 187 | */ |
| 188 | public function render_audio( $attachment, $widget_id, $instance ) { |
| 189 | echo '<p class="media-widget-admin-preview">', |
| 190 | wp_audio_shortcode( array( |
| 191 | 'src' => wp_get_attachment_url( $attachment->ID ) |
| 192 | ) ), |
| 193 | '</p>'; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Renders a video attachment preview. |
| 198 | * |
| 199 | * @since 4.4.0 |
| 200 | * @access public |
| 201 | * |
| 202 | * @param WP_Post $attachment Attachment object. |
| 203 | * @param string $widget_id Widget ID. |
| 204 | * @param array $instance Current widget instance arguments. |
| 205 | */ |
| 206 | public function render_video( $attachment, $widget_id, $instance ) { |
| 207 | echo '<p class="media-widget-admin-preview">', |
| 208 | wp_video_shortcode( array( |
| 209 | 'src' => wp_get_attachment_url( $attachment->ID ) |
| 210 | ) ), |
| 211 | '</p>'; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Outputs the settings update form. |
| 216 | * |
| 217 | * @since 4.4.0 |
| 218 | * @access public |
| 219 | * |
| 220 | * @param array $saved_instance Current settings. |
| 221 | * @return string Default return is 'noform'. |
| 222 | */ |
| 223 | public function form( $saved_instance ) { |
| 224 | $defaults = array( |
| 225 | 'title' => '', |
| 226 | 'target' => '', |
| 227 | // Attachment props. |
| 228 | 'id' => '', |
| 229 | 'align' => '', |
| 230 | 'size' => '', |
| 231 | 'link' => '', |
| 232 | ); |
| 233 | $instance = wp_parse_args( (array) $saved_instance, $defaults ); |
| 234 | $widget_id = $this->id; |
| 235 | ?> |
| 236 | <div class="<?php echo esc_attr( $widget_id ); ?> media-widget-preview"> |
| 237 | <p> |
| 238 | <label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e('Title:'); ?></label> |
| 239 | <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" |
| 240 | name="<?php echo $this->get_field_name('title'); ?>" |
| 241 | type="text" value="<?php echo esc_html( $instance['title'] ); ?>" /> |
| 242 | </p> |
| 243 | |
| 244 | <p> |
| 245 | <?php _e( 'Add an image, gallery, video, or audio to your sidebar.' ); ?> |
| 246 | </p> |
| 247 | |
| 248 | <p> |
| 249 | <a data-id="<?php echo esc_attr( $widget_id ); ?>" class="button select-media widefat"><?php |
| 250 | esc_html_e( 'Select Media' ); |
| 251 | ?></a> |
| 252 | </p> |
| 253 | |
| 254 | <?php |
| 255 | if ( ! empty( $instance['id'] ) ) { |
| 256 | |
| 257 | if ( $attachment = get_post( $instance['id'] ) ) { |
| 258 | // Image. |
| 259 | if ( wp_attachment_is_image( $attachment ) ) { |
| 260 | $this->render_image( $attachment, $widget_id, $instance ); |
| 261 | |
| 262 | // Audio. |
| 263 | } elseif ( wp_attachment_is( 'audio', $attachment ) ) { |
| 264 | $this->render_audio( $attachment, $widget_id, $instance ); |
| 265 | |
| 266 | // Video. |
| 267 | } elseif( wp_attachment_is( 'video', $attachment ) ) { |
| 268 | $this->render_video( $attachment, $widget_id, $instance ); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | ?> |
| 273 | |
| 274 | <p class="extras<?php echo empty( $instance['id'] ) ? ' hidden' : ''; ?>"> |
| 275 | <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'target' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'target' ) ); ?>" |
| 276 | <?php if ( ! empty( $instance['target'] ) ) { |
| 277 | checked( 'on', $instance['target'] ); |
| 278 | } ?> /> |
| 279 | <label for="<?php echo esc_attr( $this->get_field_id( 'target' ) ); ?>"> |
| 280 | <?php esc_html_e( 'Open link in a new tab or window' ); ?> |
| 281 | </label> |
| 282 | </p> |
| 283 | |
| 284 | <fieldset class="media-widget-hidden-fields"> |
| 285 | <?php |
| 286 | // Use hidden form fields to capture the attachment details from the media manager. |
| 287 | unset( $instance['title'] ); |
| 288 | unset( $instance['target'] ); |
| 289 | |
| 290 | foreach( array_keys( $instance ) as $i ) { ?> |
| 291 | <input type="hidden" id="<?php echo esc_attr( $this->get_field_id( $i ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $i ) ); ?>" value="<?php echo esc_attr( $instance[ $i ] ); ?>" /> |
| 292 | <?php } ?> |
| 293 | </fieldset> |
| 294 | </div> |
| 295 | <?php |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Registers the stylesheet for handling the widget in the back-end. |
| 300 | * |
| 301 | * @since 4.4.0 |
| 302 | * @access public |
| 303 | */ |
| 304 | public function enqueue_admin_styles() { |
| 305 | wp_enqueue_style( 'wp-media-widget-styles', includes_url( 'css/wp-media-widget.css' ), array( 'media-views' ) ); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Registers the scripts for handling the widget in the back-end. |
| 310 | * |
| 311 | * @since 4.4.0 |
| 312 | * @access public |
| 313 | */ |
| 314 | public function enqueue_admin_scripts() { |
| 315 | global $pagenow; |
| 316 | |
| 317 | // Bail if we are not in the widgets or customize screens. |
| 318 | if ( ! ( 'widgets.php' == $pagenow || 'customize.php' == $pagenow ) ) { |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | // Load the required media files for the media manager. |
| 323 | wp_enqueue_media(); |
| 324 | |
| 325 | // Register, localize and enqueue custom JS. |
| 326 | wp_enqueue_script( |
| 327 | 'wp-media-widget', |
| 328 | includes_url( 'js/wp-media-widget.js' ), |
| 329 | array( 'jquery', 'media-models', 'media-views' ), |
| 330 | '', |
| 331 | true |
| 332 | ); |
| 333 | |
| 334 | wp_localize_script( 'wp-media-widget', '_mediaWidgetl10n', |
| 335 | array( |
| 336 | 'title' => __( 'Select an Image' ), |
| 337 | 'button' => __( 'Insert Image' ) |
| 338 | ) |
| 339 | ); |
| 340 | |
| 341 | add_action( 'admin_print_footer_scripts', array( $this, 'admin_print_footer_scripts' ) ); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Prints footer scripts. |
| 346 | * |
| 347 | * @since 4.4.0 |
| 348 | * @access public |
| 349 | */ |
| 350 | public function admin_print_footer_scripts() { |
| 351 | ?> |
| 352 | <script type="text/html" id="tmpl-wp-media-widget-audio"> |
| 353 | <?php wp_underscore_audio_template() ?> |
| 354 | </script> |
| 355 | |
| 356 | <script type="text/html" id="tmpl-wp-media-widget-video"> |
| 357 | <?php wp_underscore_video_template() ?> |
| 358 | </script> |
| 359 | |
| 360 | <?php |
| 361 | } |
| 362 | } |