| 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 | if ( ! empty( $selectedLink ) ) { |
| 88 | $media_output .= '<a href="' . $selectedLink . '"'; |
| 89 | if ( 'on' == $instance['target'] ) { |
| 90 | $media_output .= ' target="_blank"'; |
| 91 | } |
| 92 | $media_output .= '>'; |
| 93 | } |
| 94 | |
| 95 | if ( ! empty( $instance['id'] ) ) { |
| 96 | if ( $attachment = get_post( $instance['id'] ) ) { |
| 97 | // Image. |
| 98 | if ( wp_attachment_is_image( $attachment ) ) { |
| 99 | |
| 100 | $media_output .= wp_get_attachment_image( $instance['id'], $instance['size'], false, array( |
| 101 | 'data-id' => esc_attr( $args['widget_id'] ), |
| 102 | 'class' => 'image align' . $instance['align'], |
| 103 | 'title' => $instance['title'], |
| 104 | ) ); |
| 105 | |
| 106 | // Audio. |
| 107 | } elseif ( wp_attachment_is( 'audio', $attachment ) ) { |
| 108 | if ( ! empty( $selectedLink ) ) { |
| 109 | $media_output .= $attachment->post_title; |
| 110 | } else { |
| 111 | $media_output .= wp_audio_shortcode( array( |
| 112 | 'src' => wp_get_attachment_url( $attachment->ID ) |
| 113 | ) ); |
| 114 | } |
| 115 | |
| 116 | // Video. |
| 117 | } elseif( wp_attachment_is( 'video', $attachment ) ) { |
| 118 | if ( ! empty( $selectedLink ) ) { |
| 119 | $media_output .= $attachment->post_title; |
| 120 | } else { |
| 121 | $media_output .= wp_video_shortcode( array( |
| 122 | 'src' => wp_get_attachment_url( $attachment->ID ) |
| 123 | ) ); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if ( ! empty( $selectedLink ) ) { |
| 130 | $media_output .= '</a>'; |
| 131 | } |
| 132 | |
| 133 | echo $media_output; |
| 134 | echo $args['after_widget']; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Sanitizes the widget form values as they are saved. |
| 139 | * |
| 140 | * @since 4.4.0 |
| 141 | * @access public |
| 142 | * |
| 143 | * @see WP_Widget::update() |
| 144 | * |
| 145 | * @param array $new_instance Values just sent to be saved. |
| 146 | * @param array $old_instance Previously saved values from database. |
| 147 | * @return array Updated safe values to be saved. |
| 148 | */ |
| 149 | function update( $new_instance, $old_instance ) { |
| 150 | $instance = $old_instance; |
| 151 | |
| 152 | // ID, title, target. |
| 153 | $instance['id'] = (int) $new_instance['id']; |
| 154 | $instance['title'] = sanitize_text_field( $new_instance['title'] ); |
| 155 | $instance['target'] = isset( $new_instance['target'] ) ? sanitize_text_field( $new_instance['target'] ) : ''; |
| 156 | |
| 157 | // Everything else. |
| 158 | $instance['align'] = sanitize_text_field( $new_instance['align'] ); |
| 159 | $instance['size'] = sanitize_text_field( $new_instance['size'] ); |
| 160 | $instance['link'] = sanitize_text_field( $new_instance['link'] ); |
| 161 | return $instance; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Renders an image attachment preview. |
| 166 | * |
| 167 | * @since 4.4.0 |
| 168 | * @access public |
| 169 | * |
| 170 | * @param WP_Post $attachment Attachment object. |
| 171 | * @param string $widget_id Widget ID. |
| 172 | * @param array $instance Current widget instance arguments. |
| 173 | */ |
| 174 | public function render_image( $attachment, $widget_id, $instance ) { |
| 175 | // If an image id is saved for this widget, display the image using `wp_get_attachment_image()`. |
| 176 | echo '<p class="media-widget-admin-preview">', |
| 177 | wp_get_attachment_image( $attachment->ID, $instance['size'], false, array( |
| 178 | 'data-id' => esc_attr( $widget_id ), |
| 179 | 'class' => 'image align' . esc_attr( $instance['align'] ), |
| 180 | 'title' => esc_attr( $instance['title'] ), |
| 181 | ) ), |
| 182 | '</p>'; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Renders an audio attachment preview. |
| 187 | * |
| 188 | * @since 4.4.0 |
| 189 | * @access public |
| 190 | * |
| 191 | * @param WP_Post $attachment Attachment object. |
| 192 | * @param string $widget_id Widget ID. |
| 193 | * @param array $instance Current widget instance arguments. |
| 194 | */ |
| 195 | public function render_audio( $attachment, $widget_id, $instance ) { |
| 196 | echo '<p class="media-widget-admin-preview">'; |
| 197 | if( $instance['link'] == 'embed' ){ |
| 198 | echo wp_audio_shortcode( array( |
| 199 | 'src' => wp_get_attachment_url( $attachment->ID ) |
| 200 | ) ); |
| 201 | } else { |
| 202 | echo '<a href="#">' . $attachment->post_title .'</a>'; |
| 203 | } |
| 204 | echo '</p>'; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Renders a video attachment preview. |
| 209 | * |
| 210 | * @since 4.4.0 |
| 211 | * @access public |
| 212 | * |
| 213 | * @param WP_Post $attachment Attachment object. |
| 214 | * @param string $widget_id Widget ID. |
| 215 | * @param array $instance Current widget instance arguments. |
| 216 | */ |
| 217 | public function render_video( $attachment, $widget_id, $instance ) { |
| 218 | echo '<p class="media-widget-admin-preview">'; |
| 219 | if( $instance['link'] == 'embed' ){ |
| 220 | echo wp_video_shortcode( array( |
| 221 | 'src' => wp_get_attachment_url( $attachment->ID ) |
| 222 | ) ); |
| 223 | } else { |
| 224 | echo '<a href="#">' . $attachment->post_title .'</a>'; |
| 225 | } |
| 226 | echo '</p>'; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Outputs the settings update form. |
| 231 | * |
| 232 | * @since 4.4.0 |
| 233 | * @access public |
| 234 | * |
| 235 | * @param array $saved_instance Current settings. |
| 236 | * @return string Default return is 'noform'. |
| 237 | */ |
| 238 | public function form( $saved_instance ) { |
| 239 | $defaults = array( |
| 240 | 'title' => '', |
| 241 | 'target' => '', |
| 242 | // Attachment props. |
| 243 | 'id' => '', |
| 244 | 'align' => '', |
| 245 | 'size' => '', |
| 246 | 'link' => '', |
| 247 | ); |
| 248 | $instance = wp_parse_args( (array) $saved_instance, $defaults ); |
| 249 | $widget_id = $this->id; |
| 250 | ?> |
| 251 | <div class="<?php echo esc_attr( $widget_id ); ?> media-widget-preview"> |
| 252 | <p> |
| 253 | <label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e('Title:'); ?></label> |
| 254 | <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" |
| 255 | name="<?php echo $this->get_field_name('title'); ?>" |
| 256 | type="text" value="<?php echo esc_html( $instance['title'] ); ?>" /> |
| 257 | </p> |
| 258 | |
| 259 | <p> |
| 260 | <?php _e( 'Add an image, gallery, video, or audio to your sidebar.' ); ?> |
| 261 | </p> |
| 262 | |
| 263 | <p> |
| 264 | <a data-id="<?php echo esc_attr( $widget_id ); ?>" class="button select-media widefat"><?php |
| 265 | esc_html_e( 'Select Media' ); |
| 266 | ?></a> |
| 267 | </p> |
| 268 | |
| 269 | <?php |
| 270 | if ( ! empty( $instance['id'] ) ) { |
| 271 | |
| 272 | if ( $attachment = get_post( $instance['id'] ) ) { |
| 273 | // Image. |
| 274 | if ( wp_attachment_is_image( $attachment ) ) { |
| 275 | $this->render_image( $attachment, $widget_id, $instance ); |
| 276 | |
| 277 | // Audio. |
| 278 | } elseif ( wp_attachment_is( 'audio', $attachment ) ) { |
| 279 | $this->render_audio( $attachment, $widget_id, $instance ); |
| 280 | |
| 281 | // Video. |
| 282 | } elseif( wp_attachment_is( 'video', $attachment ) ) { |
| 283 | $this->render_video( $attachment, $widget_id, $instance ); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | ?> |
| 288 | |
| 289 | <p class="extras<?php echo empty( $instance['id'] ) ? ' hidden' : ''; ?>"> |
| 290 | <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'target' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'target' ) ); ?>" |
| 291 | <?php if ( ! empty( $instance['target'] ) ) { |
| 292 | checked( 'on', $instance['target'] ); |
| 293 | } ?> /> |
| 294 | <label for="<?php echo esc_attr( $this->get_field_id( 'target' ) ); ?>"> |
| 295 | <?php esc_html_e( 'Open link in a new tab or window' ); ?> |
| 296 | </label> |
| 297 | </p> |
| 298 | |
| 299 | <fieldset class="media-widget-hidden-fields"> |
| 300 | <?php |
| 301 | // Use hidden form fields to capture the attachment details from the media manager. |
| 302 | unset( $instance['title'] ); |
| 303 | unset( $instance['target'] ); |
| 304 | |
| 305 | foreach( array_keys( $instance ) as $i ) { ?> |
| 306 | <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 ] ); ?>" /> |
| 307 | <?php } ?> |
| 308 | </fieldset> |
| 309 | </div> |
| 310 | <?php |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Registers the stylesheet for handling the widget in the back-end. |
| 315 | * |
| 316 | * @since 4.4.0 |
| 317 | * @access public |
| 318 | */ |
| 319 | public function enqueue_admin_styles() { |
| 320 | wp_enqueue_style( 'wp-media-widget-styles', includes_url( 'css/wp-media-widget.css' ), array( 'media-views' ) ); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Registers the scripts for handling the widget in the back-end. |
| 325 | * |
| 326 | * @since 4.4.0 |
| 327 | * @access public |
| 328 | */ |
| 329 | public function enqueue_admin_scripts() { |
| 330 | global $pagenow; |
| 331 | |
| 332 | // Bail if we are not in the widgets or customize screens. |
| 333 | if ( ! ( 'widgets.php' == $pagenow || 'customize.php' == $pagenow ) ) { |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | // Load the required media files for the media manager. |
| 338 | wp_enqueue_media(); |
| 339 | |
| 340 | // Register, localize and enqueue custom JS. |
| 341 | wp_enqueue_script( |
| 342 | 'wp-media-widget', |
| 343 | includes_url( 'js/wp-media-widget.js' ), |
| 344 | array( 'jquery', 'media-models', 'media-views' ), |
| 345 | '', |
| 346 | true |
| 347 | ); |
| 348 | |
| 349 | wp_localize_script( 'wp-media-widget', '_mediaWidgetl10n', |
| 350 | array( |
| 351 | 'title' => __( 'Select an Image' ), |
| 352 | 'button' => __( 'Insert Image' ) |
| 353 | ) |
| 354 | ); |
| 355 | |
| 356 | add_action( 'admin_print_footer_scripts', array( $this, 'admin_print_footer_scripts' ) ); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Prints footer scripts. |
| 361 | * |
| 362 | * @since 4.4.0 |
| 363 | * @access public |
| 364 | */ |
| 365 | public function admin_print_footer_scripts() { |
| 366 | ?> |
| 367 | <script type="text/html" id="tmpl-wp-media-widget-audio"> |
| 368 | <?php wp_underscore_audio_template() ?> |
| 369 | </script> |
| 370 | |
| 371 | <script type="text/html" id="tmpl-wp-media-widget-video"> |
| 372 | <?php wp_underscore_video_template() ?> |
| 373 | </script> |
| 374 | |
| 375 | <?php |
| 376 | } |
| 377 | } |