| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * WordPress Administration Media API. 4 * 5 * @package WordPress 6 * @subpackage Administration 7 */ 8 9 /** 10 * Defines the default media upload tabs. 11 * 12 * @since 2.5.0 13 * 14 * @return string[] Default tabs. 15 */ 16 function media_upload_tabs() { 17 $_default_tabs = array( 18 'type' => __( 'From Computer' ), // Handler action suffix => tab text. 19 'type_url' => __( 'From URL' ), 20 'gallery' => __( 'Gallery' ), 21 'library' => __( 'Media Library' ), 22 ); 23 24 /** 25 * Filters the available tabs in the legacy (pre-3.5.0) media popup. 26 * 27 * @since 2.5.0 28 * 29 * @param string[] $_default_tabs An array of media tabs. 30 */ 31 return apply_filters( 'media_upload_tabs', $_default_tabs ); 32 } 33 34 /** 35 * Adds the gallery tab back to the tabs array if post has image attachments. 36 * 37 * @since 2.5.0 38 * 39 * @global wpdb $wpdb WordPress database abstraction object. 40 * 41 * @param array $tabs Associative array of default tab names. 42 * @return array $tabs Filtered tabs with gallery if post has image attachment. 43 */ 44 function update_gallery_tab( $tabs ) { 45 global $wpdb; 46 47 if ( ! isset( $_REQUEST['post_id'] ) ) { 48 unset( $tabs['gallery'] ); 49 return $tabs; 50 } 51 52 $post_id = (int) $_REQUEST['post_id']; 53 54 if ( $post_id ) { 55 $attachments = (int) $wpdb->get_var( $wpdb->prepare( "SELECT count(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent = %d", $post_id ) ); 56 } 57 58 if ( empty( $attachments ) ) { 59 unset( $tabs['gallery'] ); 60 return $tabs; 61 } 62 63 /* translators: %s: Number of attachments. */ 64 $tabs['gallery'] = sprintf( __( 'Gallery (%s)' ), "<span id='attachments-count'>$attachments</span>" ); 65 66 return $tabs; 67 } 68 69 /** 70 * Outputs the legacy media upload tabs UI. 71 * 72 * @since 2.5.0 73 * 74 * @global string $redir_tab The name of the tab to redirect to. 75 */ 76 function the_media_upload_tabs() { 77 global $redir_tab; 78 $tabs = media_upload_tabs(); 79 $default = 'type'; 80 81 if ( ! empty( $tabs ) ) { 82 echo "<ul id='sidemenu'>\n"; 83 84 if ( isset( $redir_tab ) && array_key_exists( $redir_tab, $tabs ) ) { 85 $current = $redir_tab; 86 } elseif ( isset( $_GET['tab'] ) && array_key_exists( $_GET['tab'], $tabs ) ) { 87 $current = $_GET['tab']; 88 } else { 89 /** This filter is documented in wp-admin/media-upload.php */ 90 $current = apply_filters( 'media_upload_default_tab', $default ); 91 } 92 93 foreach ( $tabs as $callback => $text ) { 94 $class = ''; 95 96 if ( $current === $callback ) { 97 $class = " class='current'"; 98 } 99 100 $href = add_query_arg( 101 array( 102 'tab' => $callback, 103 's' => false, 104 'paged' => false, 105 'post_mime_type' => false, 106 'm' => false, 107 ) 108 ); 109 $link = "<a href='" . esc_url( $href ) . "'$class>$text</a>"; 110 echo "\t<li id='" . esc_attr( "tab-$callback" ) . "'>$link</li>\n"; 111 } 112 113 echo "</ul>\n"; 114 } 115 } 116 117 /** 118 * Retrieves the image HTML to send to the editor. 119 * 120 * @since 2.5.0 121 * 122 * @param int $id Image attachment ID. 123 * @param string $caption Image caption. 124 * @param string $title Image title attribute. 125 * @param string $align Image CSS alignment property. 126 * @param string $url Optional. Image src URL. Default empty. 127 * @param bool|string $rel Optional. Value for rel attribute or whether to add a default value. Default false. 128 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of 129 * width and height values in pixels (in that order). Default 'medium'. 130 * @param string $alt Optional. Image alt attribute. Default empty. 131 * @return string The HTML output to insert into the editor. 132 */ 133 function get_image_send_to_editor( $id, $caption, $title, $align, $url = '', $rel = false, $size = 'medium', $alt = '' ) { 134 135 $html = get_image_tag( $id, $alt, '', $align, $size ); 136 137 if ( $rel ) { 138 if ( is_string( $rel ) ) { 139 $rel = ' rel="' . esc_attr( $rel ) . '"'; 140 } else { 141 $rel = ' rel="attachment wp-att-' . (int) $id . '"'; 142 } 143 } else { 144 $rel = ''; 145 } 146 147 if ( $url ) { 148 $html = '<a href="' . esc_url( $url ) . '"' . $rel . '>' . $html . '</a>'; 149 } 150 151 /** 152 * Filters the image HTML markup to send to the editor when inserting an image. 153 * 154 * @since 2.5.0 155 * @since 5.6.0 The `$rel` parameter was added. 156 * 157 * @param string $html The image HTML markup to send. 158 * @param int $id The attachment ID. 159 * @param string $caption The image caption. 160 * @param string $title The image title. 161 * @param string $align The image alignment. 162 * @param string $url The image source URL. 163 * @param string|int[] $size Requested image size. Can be any registered image size name, or 164 * an array of width and height values in pixels (in that order). 165 * @param string $alt The image alternative, or alt, text. 166 * @param string $rel The image rel attribute. 167 */ 168 $html = apply_filters( 'image_send_to_editor', $html, $id, $caption, $title, $align, $url, $size, $alt, $rel ); 169 170 return $html; 171 } 172 173 /** 174 * Adds image shortcode with caption to editor. 175 * 176 * @since 2.6.0 177 * 178 * @param string $html The image HTML markup to send. 179 * @param int $id Image attachment ID. 180 * @param string $caption Image caption. 181 * @param string $title Image title attribute (not used). 182 * @param string $align Image CSS alignment property. 183 * @param string $url Image source URL (not used). 184 * @param string $size Image size (not used). 185 * @param string $alt Image `alt` attribute (not used). 186 * @return string The image HTML markup with caption shortcode. 187 */ 188 function image_add_caption( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) { 189 190 /** 191 * Filters the caption text. 192 * 193 * Note: If the caption text is empty, the caption shortcode will not be appended 194 * to the image HTML when inserted into the editor. 195 * 196 * Passing an empty value also prevents the {@see 'image_add_caption_shortcode'} 197 * Filters from being evaluated at the end of image_add_caption(). 198 * 199 * @since 4.1.0 200 * 201 * @param string $caption The original caption text. 202 * @param int $id The attachment ID. 203 */ 204 $caption = apply_filters( 'image_add_caption_text', $caption, $id ); 205 206 /** 207 * Filters whether to disable captions. 208 * 209 * Prevents image captions from being appended to image HTML when inserted into the editor. 210 * 211 * @since 2.6.0 212 * 213 * @param bool $bool Whether to disable appending captions. Returning true from the filter 214 * will disable captions. Default empty string. 215 */ 216 if ( empty( $caption ) || apply_filters( 'disable_captions', '' ) ) { 217 return $html; 218 } 219 220 $id = ( 0 < (int) $id ) ? 'attachment_' . $id : ''; 221 222 if ( ! preg_match( '/width=["\']([0-9]+)/', $html, $matches ) ) { 223 return $html; 224 } 225 226 $width = $matches[1]; 227 228 $caption = str_replace( array( "\r\n", "\r" ), "\n", $caption ); 229 $caption = preg_replace_callback( '/<[a-zA-Z0-9]+(?: [^<>]+>)*/', '_cleanup_image_add_caption', $caption ); 230 231 // Convert any remaining line breaks to <br />. 232 $caption = preg_replace( '/[ \n\t]*\n[ \t]*/', '<br />', $caption ); 233 234 $html = preg_replace( '/(class=["\'][^\'"]*)align(none|left|right|center)\s?/', '$1', $html ); 235 if ( empty( $align ) ) { 236 $align = 'none'; 237 } 238 239 $shcode = '[caption id="' . $id . '" align="align' . $align . '" width="' . $width . '"]' . $html . ' ' . $caption . '[/caption]'; 240 241 /** 242 * Filters the image HTML markup including the caption shortcode. 243 * 244 * @since 2.6.0 245 * 246 * @param string $shcode The image HTML markup with caption shortcode. 247 * @param string $html The image HTML markup. 248 */ 249 return apply_filters( 'image_add_caption_shortcode', $shcode, $html ); 250 } 251 252 /** 253 * Private preg_replace callback used in image_add_caption(). 254 * 255 * @access private 256 * @since 3.4.0 257 * 258 * @param array $matches Single regex match. 259 * @return string Cleaned up HTML for caption. 260 */ 261 function _cleanup_image_add_caption( $matches ) { 262 // Remove any line breaks from inside the tags. 263 return preg_replace( '/[\r\n\t]+/', ' ', $matches[0] ); 264 } 265 266 /** 267 * Adds image HTML to editor. 268 * 269 * @since 2.5.0 270 * 271 * @param string $html 272 * @return never 273 */ 274 function media_send_to_editor( $html ) { 275 wp_print_inline_script_tag( 276 sprintf( 277 '( window.dialogArguments || opener || parent || top ).send_to_editor( %s );', 278 wp_json_encode( $html, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) 279 ) 280 ); 281 exit; 282 } 283 284 /** 285 * Saves a file submitted from a POST request and create an attachment post for it. 286 * 287 * @since 2.5.0 288 * 289 * @param string $file_id Index of the `$_FILES` array that the file was sent. 290 * @param int $post_id The post ID of a post to attach the media item to. Required, but can 291 * be set to 0, creating a media item that has no relationship to a post. 292 * @param array $post_data Optional. Overwrite some of the attachment. 293 * @param array $overrides Optional. Override the wp_handle_upload() behavior. 294 * @return int|WP_Error ID of the attachment or a WP_Error object on failure. 295 */ 296 function media_handle_upload( $file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false ) ) { 297 $time = current_time( 'mysql' ); 298 $post = get_post( $post_id ); 299 300 if ( $post ) { 301 // The post date doesn't usually matter for pages, so don't backdate this upload. 302 if ( 'page' !== $post->post_type && substr( $post->post_date, 0, 4 ) > 0 ) { 303 $time = $post->post_date; 304 } 305 } 306 307 $file = wp_handle_upload( $_FILES[ $file_id ], $overrides, $time ); 308 309 if ( isset( $file['error'] ) ) { 310 return new WP_Error( 'upload_error', $file['error'] ); 311 } 312 313 $name = $_FILES[ $file_id ]['name']; 314 $ext = pathinfo( $name, PATHINFO_EXTENSION ); 315 $name = wp_basename( $name, ".$ext" ); 316 317 $url = $file['url']; 318 $type = $file['type']; 319 $file = $file['file']; 320 $title = sanitize_text_field( $name ); 321 $content = ''; 322 $excerpt = ''; 323 $alt = ''; 324 325 if ( preg_match( '#^audio#', $type ) ) { 326 $meta = wp_read_audio_metadata( $file ); 327 328 if ( ! empty( $meta['title'] ) ) { 329 $title = $meta['title']; 330 } 331 332 if ( ! empty( $title ) ) { 333 334 if ( ! empty( $meta['album'] ) && ! empty( $meta['artist'] ) ) { 335 /* translators: 1: Audio track title, 2: Album title, 3: Artist name. */ 336 $content .= sprintf( __( '"%1$s" from %2$s by %3$s.' ), $title, $meta['album'], $meta['artist'] ); 337 } elseif ( ! empty( $meta['album'] ) ) { 338 /* translators: 1: Audio track title, 2: Album title. */ 339 $content .= sprintf( __( '"%1$s" from %2$s.' ), $title, $meta['album'] ); 340 } elseif ( ! empty( $meta['artist'] ) ) { 341 /* translators: 1: Audio track title, 2: Artist name. */ 342 $content .= sprintf( __( '"%1$s" by %2$s.' ), $title, $meta['artist'] ); 343 } else { 344 /* translators: %s: Audio track title. */ 345 $content .= sprintf( __( '"%s".' ), $title ); 346 } 347 } elseif ( ! empty( $meta['album'] ) ) { 348 349 if ( ! empty( $meta['artist'] ) ) { 350 /* translators: 1: Audio album title, 2: Artist name. */ 351 $content .= sprintf( __( '%1$s by %2$s.' ), $meta['album'], $meta['artist'] ); 352 } else { 353 $content .= $meta['album'] . '.'; 354 } 355 } elseif ( ! empty( $meta['artist'] ) ) { 356 357 $content .= $meta['artist'] . '.'; 358 359 } 360 361 if ( ! empty( $meta['year'] ) ) { 362 /* translators: Audio file track information. %d: Year of audio track release. */ 363 $content .= ' ' . sprintf( __( 'Released: %d.' ), $meta['year'] ); 364 } 365 366 if ( ! empty( $meta['track_number'] ) ) { 367 $track_number = explode( '/', $meta['track_number'] ); 368 369 if ( is_numeric( $track_number[0] ) ) { 370 if ( isset( $track_number[1] ) && is_numeric( $track_number[1] ) ) { 371 $content .= ' ' . sprintf( 372 /* translators: Audio file track information. 1: Audio track number, 2: Total audio tracks. */ 373 __( 'Track %1$s of %2$s.' ), 374 number_format_i18n( $track_number[0] ), 375 number_format_i18n( $track_number[1] ) 376 ); 377 } else { 378 $content .= ' ' . sprintf( 379 /* translators: Audio file track information. %s: Audio track number. */ 380 __( 'Track %s.' ), 381 number_format_i18n( $track_number[0] ) 382 ); 383 } 384 } 385 } 386 387 if ( ! empty( $meta['genre'] ) ) { 388 /* translators: Audio file genre information. %s: Audio genre name. */ 389 $content .= ' ' . sprintf( __( 'Genre: %s.' ), $meta['genre'] ); 390 } 391 392 // Use image exif/iptc data for title and caption defaults if possible. 393 } elseif ( str_starts_with( $type, 'image/' ) ) { 394 $image_meta = wp_read_image_metadata( $file ); 395 396 if ( $image_meta ) { 397 if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) { 398 $title = $image_meta['title']; 399 } 400 401 if ( trim( $image_meta['caption'] ) ) { 402 $excerpt = $image_meta['caption']; 403 } 404 405 if ( trim( $image_meta['alt'] ) ) { 406 $alt = $image_meta['alt']; 407 } 408 } 409 } 410 411 // Construct the attachment array. 412 $attachment = array_merge( 413 array( 414 'post_mime_type' => $type, 415 'guid' => $url, 416 'post_parent' => $post_id, 417 'post_title' => $title, 418 'post_content' => $content, 419 'post_excerpt' => $excerpt, 420 ), 421 $post_data 422 ); 423 424 // This should never be set as it would then overwrite an existing attachment. 425 unset( $attachment['ID'] ); 426 427 // Save the data. 428 $attachment_id = wp_insert_attachment( $attachment, $file, $post_id, true ); 429 430 if ( trim( $alt ) ) { 431 update_post_meta( $attachment_id, '_wp_attachment_image_alt', sanitize_text_field( $alt ) ); 432 } 433 434 if ( ! is_wp_error( $attachment_id ) ) { 435 /* 436 * Set a custom header with the attachment_id. 437 * Used by the browser/client to resume creating image sub-sizes after a PHP fatal error. 438 */ 439 if ( ! headers_sent() ) { 440 header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id ); 441 } 442 443 /* 444 * The image sub-sizes are created during wp_generate_attachment_metadata(). 445 * This is generally slow and may cause timeouts or out of memory errors. 446 */ 447 wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) ); 448 } 449 450 return $attachment_id; 451 } 452 453 /** 454 * Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload(). 455 * 456 * @since 2.6.0 457 * @since 5.3.0 The `$post_id` parameter was made optional. 458 * 459 * @param string[] $file_array Array that represents a `$_FILES` upload array. 460 * @param int $post_id Optional. The post ID the media is associated with. 461 * @param string $desc Optional. Description of the side-loaded file. Default null. 462 * @param array $post_data Optional. Post data to override. Default empty array. 463 * @return int|WP_Error The ID of the attachment or a WP_Error on failure. 464 */ 465 function media_handle_sideload( $file_array, $post_id = 0, $desc = null, $post_data = array() ) { 466 $overrides = array( 'test_form' => false ); 467 468 if ( isset( $post_data['post_date'] ) && substr( $post_data['post_date'], 0, 4 ) > 0 ) { 469 $time = $post_data['post_date']; 470 } else { 471 $post = get_post( $post_id ); 472 if ( $post && substr( $post->post_date, 0, 4 ) > 0 ) { 473 $time = $post->post_date; 474 } else { 475 $time = current_time( 'mysql' ); 476 } 477 } 478 479 $file = wp_handle_sideload( $file_array, $overrides, $time ); 480 481 if ( isset( $file['error'] ) ) { 482 return new WP_Error( 'upload_error', $file['error'] ); 483 } 484 485 $url = $file['url']; 486 $type = $file['type']; 487 $file = $file['file']; 488 $title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) ); 489 $content = ''; 490 $alt = ''; 491 492 // Use image exif/iptc data for title and caption defaults if possible. 493 $image_meta = wp_read_image_metadata( $file ); 494 495 if ( $image_meta ) { 496 if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) { 497 $title = $image_meta['title']; 498 } 499 500 if ( trim( $image_meta['caption'] ) ) { 501 $content = $image_meta['caption']; 502 } 503 if ( trim( $image_meta['alt'] ) ) { 504 $alt = $image_meta['alt']; 505 } 506 } 507 508 if ( isset( $desc ) ) { 509 $title = $desc; 510 } 511 512 // Construct the attachment array. 513 $attachment = array_merge( 514 array( 515 'post_mime_type' => $type, 516 'guid' => $url, 517 'post_parent' => $post_id, 518 'post_title' => $title, 519 'post_content' => $content, 520 ), 521 $post_data 522 ); 523 524 // This should never be set as it would then overwrite an existing attachment. 525 unset( $attachment['ID'] ); 526 527 // Save the attachment metadata. 528 $attachment_id = wp_insert_attachment( $attachment, $file, $post_id, true ); 529 530 if ( trim( $alt ) ) { 531 update_post_meta( $attachment_id, '_wp_attachment_image_alt', sanitize_text_field( $alt ) ); 532 } 533 534 if ( ! is_wp_error( $attachment_id ) ) { 535 wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) ); 536 } 537 538 return $attachment_id; 539 } 540 541 /** 542 * Outputs the iframe to display the media upload page. 543 * 544 * @since 2.5.0 545 * @since 5.3.0 Formalized the existing and already documented `...$args` parameter 546 * by adding it to the function signature. 547 * 548 * @global string $body_id The ID attribute value for the body element. 549 * 550 * @param callable $content_func Function that outputs the content. 551 * @param mixed ...$args Optional additional parameters to pass to the callback function when it's called. 552 */ 553 function wp_iframe( $content_func, ...$args ) { 554 global $body_id; 555 556 _wp_admin_html_begin(); 557 ?> 558 <title><?php bloginfo( 'name' ); ?> › <?php _e( 'Uploads' ); ?> — <?php _e( 'WordPress' ); ?></title> 559 <?php 560 561 wp_enqueue_style( 'colors' ); 562 // Check callback name for 'media'. 563 if ( 564 ( is_array( $content_func ) && ! empty( $content_func[1] ) && str_starts_with( (string) $content_func[1], 'media' ) ) || 565 ( ! is_array( $content_func ) && str_starts_with( $content_func, 'media' ) ) 566 ) { 567 wp_enqueue_style( 'deprecated-media' ); 568 } 569 570 wp_print_inline_script_tag( 571 <<<'JS' 572 function addLoadEvent( func ) { 573 if ( typeof jQuery !== 'undefined' ) { 574 jQuery( function () { 575 func(); 576 } ); 577 } else if ( typeof wpOnload !== 'function' ) { 578 window.wpOnload = func; 579 } else { 580 const oldOnload = window.wpOnload; 581 window.wpOnload = function () { 582 oldOnload(); 583 func(); 584 }; 585 } 586 } 587 JS 588 ); 589 wp_print_inline_script_tag( 590 sprintf( 591 'Object.assign( window, %s );', 592 wp_json_encode( 593 array( 594 'ajaxurl' => admin_url( 'admin-ajax.php', 'relative' ), 595 'pagenow' => 'media-upload-popup', 596 'adminpage' => 'media-upload-popup', 597 'isRtl' => (int) is_rtl(), 598 ), 599 JSON_HEX_TAG | JSON_UNESCAPED_SLASHES 600 ) 601 ) 602 ); 603 /** This action is documented in wp-admin/admin-header.php */ 604 do_action( 'admin_enqueue_scripts', 'media-upload-popup' ); 605 606 /** 607 * Fires when admin styles enqueued for the legacy (pre-3.5.0) media upload popup are printed. 608 * 609 * @since 2.9.0 610 */ 611 do_action( 'admin_print_styles-media-upload-popup' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 612 613 /** This action is documented in wp-admin/admin-header.php */ 614 do_action( 'admin_print_styles' ); 615 616 /** 617 * Fires when admin scripts enqueued for the legacy (pre-3.5.0) media upload popup are printed. 618 * 619 * @since 2.9.0 620 */ 621 do_action( 'admin_print_scripts-media-upload-popup' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 622 623 /** This action is documented in wp-admin/admin-header.php */ 624 do_action( 'admin_print_scripts' ); 625 626 /** 627 * Fires when scripts enqueued for the admin header for the legacy (pre-3.5.0) 628 * media upload popup are printed. 629 * 630 * @since 2.9.0 631 */ 632 do_action( 'admin_head-media-upload-popup' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 633 634 /** This action is documented in wp-admin/admin-header.php */ 635 do_action( 'admin_head' ); 636 637 if ( is_string( $content_func ) ) { 638 /** 639 * Fires in the admin header for each specific form tab in the legacy 640 * (pre-3.5.0) media upload popup. 641 * 642 * The dynamic portion of the hook name, `$content_func`, refers to the form 643 * callback for the media upload type. 644 * 645 * @since 2.5.0 646 */ 647 do_action( "admin_head_{$content_func}" ); 648 } 649 650 $body_id_attr = ''; 651 652 if ( isset( $body_id ) ) { 653 $body_id_attr = ' id="' . $body_id . '"'; 654 } 655 656 ?> 657 </head> 658 <body<?php echo $body_id_attr; ?> class="wp-core-ui no-js <?php echo 'admin-color-' . sanitize_html_class( get_user_option( 'admin_color' ), 'modern' ); ?>"> 659 <?php 660 wp_print_inline_script_tag( 661 <<<'JS' 662 document.body.className = document.body.className.replace( 'no-js', 'js' ); 663 JS 664 ); 665 666 call_user_func_array( $content_func, $args ); 667 668 /** This action is documented in wp-admin/admin-footer.php */ 669 do_action( 'admin_print_footer_scripts' ); 670 671 wp_print_inline_script_tag( 672 <<<'JS' 673 if ( typeof wpOnload === 'function' ) { 674 wpOnload(); 675 } 676 JS 677 ); 678 ?> 679 </body> 680 </html> 681 <?php 682 } 683 684 /** 685 * Adds the media button to the editor. 686 * 687 * @since 2.5.0 688 * 689 * @global int $post_ID 690 * 691 * @param string $editor_id 692 */ 693 function media_buttons( $editor_id = 'content' ) { 694 static $instance = 0; 695 ++$instance; 696 697 $post = get_post(); 698 699 if ( ! $post && ! empty( $GLOBALS['post_ID'] ) ) { 700 $post = $GLOBALS['post_ID']; 701 } 702 703 wp_enqueue_media( array( 'post' => $post ) ); 704 705 $img = '<span class="wp-media-buttons-icon" aria-hidden="true"></span> '; 706 707 $id_attribute = 1 === $instance ? ' id="insert-media-button"' : ''; 708 709 printf( 710 '<button type="button"%s class="button insert-media add_media" data-editor="%s" aria-haspopup="dialog" aria-controls="wp-media-modal">%s</button>', 711 $id_attribute, 712 esc_attr( $editor_id ), 713 $img . __( 'Add Media' ) 714 ); 715 716 /** 717 * Filters the legacy (pre-3.5.0) media buttons. 718 * 719 * Use {@see 'media_buttons'} action instead. 720 * 721 * @since 2.5.0 722 * @deprecated 3.5.0 Use {@see 'media_buttons'} action instead. 723 * 724 * @param string $string Media buttons context. Default empty. 725 */ 726 $legacy_filter = apply_filters_deprecated( 'media_buttons_context', array( '' ), '3.5.0', 'media_buttons' ); 727 728 if ( $legacy_filter ) { 729 // #WP22559. Close <a> if a plugin started by closing <a> to open their own <a> tag. 730 if ( 0 === stripos( trim( $legacy_filter ), '</a>' ) ) { 731 $legacy_filter .= '</a>'; 732 } 733 echo $legacy_filter; 734 } 735 } 736 737 /** 738 * Retrieves the upload iframe source URL. 739 * 740 * @since 3.0.0 741 * 742 * @global int $post_ID 743 * 744 * @param string $type Media type. 745 * @param int $post_id Post ID. 746 * @param string $tab Media upload tab. 747 * @return string Upload iframe source URL. 748 */ 749 function get_upload_iframe_src( $type = null, $post_id = null, $tab = null ) { 750 global $post_ID; 751 752 if ( empty( $post_id ) ) { 753 $post_id = $post_ID; 754 } 755 756 $upload_iframe_src = add_query_arg( 'post_id', (int) $post_id, admin_url( 'media-upload.php' ) ); 757 758 if ( $type && 'media' !== $type ) { 759 $upload_iframe_src = add_query_arg( 'type', $type, $upload_iframe_src ); 760 } 761 762 if ( ! empty( $tab ) ) { 763 $upload_iframe_src = add_query_arg( 'tab', $tab, $upload_iframe_src ); 764 } 765 766 /** 767 * Filters the upload iframe source URL for a specific media type. 768 * 769 * The dynamic portion of the hook name, `$type`, refers to the type 770 * of media uploaded. 771 * 772 * Possible hook names include: 773 * 774 * - `image_upload_iframe_src` 775 * - `media_upload_iframe_src` 776 * 777 * @since 3.0.0 778 * 779 * @param string $upload_iframe_src The upload iframe source URL. 780 */ 781 $upload_iframe_src = apply_filters( "{$type}_upload_iframe_src", $upload_iframe_src ); 782 783 return add_query_arg( 'TB_iframe', true, $upload_iframe_src ); 784 } 785 786 /** 787 * Handles form submissions for the legacy media uploader. 788 * 789 * @since 2.5.0 790 * 791 * @return null|array Array of error messages keyed by attachment ID, null on success, or exit. 792 */ 793 function media_upload_form_handler() { 794 check_admin_referer( 'media-form' ); 795 796 $errors = null; 797 798 if ( isset( $_POST['send'] ) ) { 799 $send_id = (int) array_key_first( $_POST['send'] ); 800 } 801 802 if ( ! empty( $_POST['attachments'] ) ) { 803 foreach ( $_POST['attachments'] as $attachment_id => $attachment ) { 804 $post = get_post( $attachment_id, ARRAY_A ); 805 $_post = $post; 806 807 if ( ! current_user_can( 'edit_post', $attachment_id ) ) { 808 continue; 809 } 810 811 if ( isset( $attachment['post_content'] ) ) { 812 $post['post_content'] = $attachment['post_content']; 813 } 814 815 if ( isset( $attachment['post_title'] ) ) { 816 $post['post_title'] = $attachment['post_title']; 817 } 818 819 if ( isset( $attachment['post_excerpt'] ) ) { 820 $post['post_excerpt'] = $attachment['post_excerpt']; 821 } 822 823 if ( isset( $attachment['menu_order'] ) ) { 824 $post['menu_order'] = $attachment['menu_order']; 825 } 826 827 if ( isset( $send_id ) && $attachment_id === $send_id ) { 828 if ( isset( $attachment['post_parent'] ) ) { 829 $post['post_parent'] = $attachment['post_parent']; 830 } 831 } 832 833 /** 834 * Filters the attachment fields to be saved. 835 * 836 * @since 2.5.0 837 * 838 * @see wp_get_attachment_metadata() 839 * 840 * @param array $post An array of post data. 841 * @param array $attachment An array of attachment metadata. 842 */ 843 $post = apply_filters( 'attachment_fields_to_save', $post, $attachment ); 844 845 if ( isset( $attachment['image_alt'] ) ) { 846 $image_alt = wp_unslash( $attachment['image_alt'] ); 847 848 if ( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) !== $image_alt ) { 849 $image_alt = wp_strip_all_tags( $image_alt, true ); 850 851 // update_post_meta() expects slashed. 852 update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) ); 853 } 854 } 855 856 if ( isset( $post['errors'] ) ) { 857 $errors[ $attachment_id ] = $post['errors']; 858 unset( $post['errors'] ); 859 } 860 861 if ( $post != $_post ) { 862 wp_update_post( $post ); 863 } 864 865 foreach ( get_attachment_taxonomies( $post ) as $t ) { 866 if ( isset( $attachment[ $t ] ) ) { 867 wp_set_object_terms( $attachment_id, array_map( 'trim', preg_split( '/,+/', $attachment[ $t ] ) ), $t, false ); 868 } 869 } 870 } 871 } 872 873 if ( isset( $_POST['insert-gallery'] ) || isset( $_POST['update-gallery'] ) ) { 874 wp_print_inline_script_tag( 875 <<<'JS' 876 ( window.dialogArguments || opener || parent || top ).tb_remove(); 877 JS 878 ); 879 880 exit; 881 } 882 883 if ( isset( $send_id ) ) { 884 $attachment = wp_unslash( $_POST['attachments'][ $send_id ] ); 885 $html = $attachment['post_title'] ?? ''; 886 887 if ( ! empty( $attachment['url'] ) ) { 888 $rel = ''; 889 890 if ( str_contains( $attachment['url'], 'attachment_id' ) || get_attachment_link( $send_id ) === $attachment['url'] ) { 891 $rel = " rel='attachment wp-att-" . esc_attr( $send_id ) . "'"; 892 } 893 894 $html = "<a href='{$attachment['url']}'$rel>$html</a>"; 895 } 896 897 /** 898 * Filters the HTML markup for a media item sent to the editor. 899 * 900 * @since 2.5.0 901 * 902 * @see wp_get_attachment_metadata() 903 * 904 * @param string $html HTML markup for a media item sent to the editor. 905 * @param int $send_id The first key from the $_POST['send'] data. 906 * @param array $attachment Array of attachment metadata. 907 */ 908 $html = apply_filters( 'media_send_to_editor', $html, $send_id, $attachment ); 909 910 media_send_to_editor( $html ); 911 } 912 913 return $errors; 914 } 915 916 /** 917 * Handles the process of uploading media. 918 * 919 * @since 2.5.0 920 * 921 * @return null|string The form handler result or null. 922 */ 923 function wp_media_upload_handler() { 924 $errors = array(); 925 $id = 0; 926 927 if ( isset( $_POST['html-upload'] ) && ! empty( $_FILES ) ) { 928 check_admin_referer( 'media-form' ); 929 // Upload File button was clicked. 930 $id = media_handle_upload( 'async-upload', $_REQUEST['post_id'] ); 931 unset( $_FILES ); 932 933 if ( is_wp_error( $id ) ) { 934 $errors['upload_error'] = $id; 935 $id = false; 936 } 937 } 938 939 if ( ! empty( $_POST['insertonlybutton'] ) ) { 940 $src = $_POST['src']; 941 942 if ( ! empty( $src ) && ! strpos( $src, '://' ) ) { 943 $src = "http://$src"; 944 } 945 946 if ( isset( $_POST['media_type'] ) && 'image' !== $_POST['media_type'] ) { 947 $title = esc_html( wp_unslash( $_POST['title'] ) ); 948 if ( empty( $title ) ) { 949 $title = esc_html( wp_basename( $src ) ); 950 } 951 952 if ( $title && $src ) { 953 $html = "<a href='" . esc_url( $src ) . "'>$title</a>"; 954 } 955 956 $type = 'file'; 957 $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src ); 958 959 if ( $ext ) { 960 $ext_type = wp_ext2type( $ext ); 961 if ( 'audio' === $ext_type || 'video' === $ext_type ) { 962 $type = $ext_type; 963 } 964 } 965 966 /** 967 * Filters the URL sent to the editor for a specific media type. 968 * 969 * The dynamic portion of the hook name, `$type`, refers to the type 970 * of media being sent. 971 * 972 * Possible hook names include: 973 * 974 * - `audio_send_to_editor_url` 975 * - `file_send_to_editor_url` 976 * - `video_send_to_editor_url` 977 * 978 * @since 3.3.0 979 * 980 * @param string $html HTML markup sent to the editor. 981 * @param string $src Media source URL. 982 * @param string $title Media title. 983 */ 984 $html = apply_filters( "{$type}_send_to_editor_url", $html, sanitize_url( $src ), $title ); 985 } else { 986 $align = ''; 987 $alt = esc_attr( wp_unslash( $_POST['alt'] ) ); 988 989 if ( isset( $_POST['align'] ) ) { 990 $align = esc_attr( wp_unslash( $_POST['align'] ) ); 991 $class = " class='align$align'"; 992 } 993 994 if ( ! empty( $src ) ) { 995 $html = "<img src='" . esc_url( $src ) . "' alt='$alt'$class />"; 996 } 997 998 /** 999 * Filters the image URL sent to the editor. 1000 * 1001 * @since 2.8.0 1002 * 1003 * @param string $html HTML markup sent to the editor for an image. 1004 * @param string $src Image source URL. 1005 * @param string $alt Image alternate, or alt, text. 1006 * @param string $align The image alignment. Default 'alignnone'. Possible values include 1007 * 'alignleft', 'aligncenter', 'alignright', 'alignnone'. 1008 */ 1009 $html = apply_filters( 'image_send_to_editor_url', $html, sanitize_url( $src ), $alt, $align ); 1010 } 1011 1012 media_send_to_editor( $html ); 1013 } 1014 1015 if ( isset( $_POST['save'] ) ) { 1016 $errors['upload_notice'] = __( 'Saved.' ); 1017 wp_enqueue_script( 'admin-gallery' ); 1018 1019 return wp_iframe( 'media_upload_gallery_form', $errors ); 1020 1021 } elseif ( ! empty( $_POST ) ) { 1022 $return = media_upload_form_handler(); 1023 1024 if ( is_string( $return ) ) { 1025 return $return; 1026 } 1027 1028 if ( is_array( $return ) ) { 1029 $errors = $return; 1030 } 1031 } 1032 1033 if ( isset( $_GET['tab'] ) && 'type_url' === $_GET['tab'] ) { 1034 $type = 'image'; 1035 1036 if ( isset( $_GET['type'] ) && in_array( $_GET['type'], array( 'video', 'audio', 'file' ), true ) ) { 1037 $type = $_GET['type']; 1038 } 1039 1040 return wp_iframe( 'media_upload_type_url_form', $type, $errors, $id ); 1041 } 1042 1043 return wp_iframe( 'media_upload_type_form', 'image', $errors, $id ); 1044 } 1045 1046 /** 1047 * Downloads an image from the specified URL, saves it as an attachment, and optionally attaches it to a post. 1048 * 1049 * @since 2.6.0 1050 * @since 4.2.0 Introduced the `$return_type` parameter. 1051 * @since 4.8.0 Introduced the 'id' option for the `$return_type` parameter. 1052 * @since 5.3.0 The `$post_id` parameter was made optional. 1053 * @since 5.4.0 The original URL of the attachment is stored in the `_source_url` 1054 * post meta value. 1055 * @since 5.8.0 Added 'webp' to the default list of allowed file extensions. 1056 * 1057 * @param string $file The URL of the image to download. 1058 * @param int $post_id Optional. The post ID the media is to be associated with. 1059 * @param string $desc Optional. Description of the image. 1060 * @param string $return_type Optional. Accepts 'html' (image tag html) or 'src' (URL), 1061 * or 'id' (attachment ID). Default 'html'. 1062 * @return string|int|WP_Error Populated HTML img tag, attachment ID, or attachment source 1063 * on success, WP_Error object otherwise. 1064 */ 1065 function media_sideload_image( $file, $post_id = 0, $desc = null, $return_type = 'html' ) { 1066 if ( ! empty( $file ) ) { 1067 1068 $allowed_extensions = array( 'jpg', 'jpeg', 'jpe', 'png', 'gif', 'webp' ); 1069 1070 /** 1071 * Filters the list of allowed file extensions when sideloading an image from a URL. 1072 * 1073 * The default allowed extensions are: 1074 * 1075 * - `jpg` 1076 * - `jpeg` 1077 * - `jpe` 1078 * - `png` 1079 * - `gif` 1080 * - `webp` 1081 * 1082 * @since 5.6.0 1083 * @since 5.8.0 Added 'webp' to the default list of allowed file extensions. 1084 * 1085 * @param string[] $allowed_extensions Array of allowed file extensions. 1086 * @param string $file The URL of the image to download. 1087 */ 1088 $allowed_extensions = apply_filters( 'image_sideload_extensions', $allowed_extensions, $file ); 1089 $allowed_extensions = array_map( 'preg_quote', $allowed_extensions ); 1090 1091 // Set variables for storage, fix file filename for query strings. 1092 preg_match( '/[^\?]+\.(' . implode( '|', $allowed_extensions ) . ')\b/i', $file, $matches ); 1093 1094 if ( ! $matches ) { 1095 return new WP_Error( 'image_sideload_failed', __( 'Invalid image URL.' ) ); 1096 } 1097 1098 $file_array = array(); 1099 $file_array['name'] = wp_basename( $matches[0] ); 1100 1101 // Download file to temp location. 1102 $file_array['tmp_name'] = download_url( $file ); 1103 1104 // If error storing temporarily, return the error. 1105 if ( is_wp_error( $file_array['tmp_name'] ) ) { 1106 return $file_array['tmp_name']; 1107 } 1108 1109 // Do the validation and storage stuff. 1110 $id = media_handle_sideload( $file_array, $post_id, $desc ); 1111 1112 // If error storing permanently, unlink. 1113 if ( is_wp_error( $id ) ) { 1114 @unlink( $file_array['tmp_name'] ); 1115 return $id; 1116 } 1117 1118 // Store the original attachment source in meta. 1119 add_post_meta( $id, '_source_url', $file ); 1120 1121 // If attachment ID was requested, return it. 1122 if ( 'id' === $return_type ) { 1123 return $id; 1124 } 1125 1126 $src = wp_get_attachment_url( $id ); 1127 } 1128 1129 // Finally, check to make sure the file has been saved, then return the HTML. 1130 if ( ! empty( $src ) ) { 1131 if ( 'src' === $return_type ) { 1132 return $src; 1133 } 1134 1135 $alt = isset( $desc ) ? esc_attr( $desc ) : ''; 1136 $html = "<img src='$src' alt='$alt' />"; 1137 1138 return $html; 1139 } else { 1140 return new WP_Error( 'image_sideload_failed' ); 1141 } 1142 } 1143 1144 /** 1145 * Retrieves the legacy media uploader form in an iframe. 1146 * 1147 * @since 2.5.0 1148 * 1149 * @return string|null The form handler result or null. 1150 */ 1151 function media_upload_gallery() { 1152 $errors = array(); 1153 1154 if ( ! empty( $_POST ) ) { 1155 $return = media_upload_form_handler(); 1156 1157 if ( is_string( $return ) ) { 1158 return $return; 1159 } 1160 1161 if ( is_array( $return ) ) { 1162 $errors = $return; 1163 } 1164 } 1165 1166 wp_enqueue_script( 'admin-gallery' ); 1167 return wp_iframe( 'media_upload_gallery_form', $errors ); 1168 } 1169 1170 /** 1171 * Retrieves the legacy media library form in an iframe. 1172 * 1173 * @since 2.5.0 1174 * 1175 * @return string|null The form handler result or null. 1176 */ 1177 function media_upload_library() { 1178 $errors = array(); 1179 1180 if ( ! empty( $_POST ) ) { 1181 $return = media_upload_form_handler(); 1182 1183 if ( is_string( $return ) ) { 1184 return $return; 1185 } 1186 if ( is_array( $return ) ) { 1187 $errors = $return; 1188 } 1189 } 1190 1191 return wp_iframe( 'media_upload_library_form', $errors ); 1192 } 1193 1194 /** 1195 * Retrieves HTML for the image alignment radio buttons with the specified one checked. 1196 * 1197 * @since 2.7.0 1198 * 1199 * @param WP_Post $post 1200 * @param string $checked 1201 * @return string HTML for the image alignment radio buttons. 1202 */ 1203 function image_align_input_fields( $post, $checked = '' ) { 1204 1205 if ( empty( $checked ) ) { 1206 $checked = get_user_setting( 'align', 'none' ); 1207 } 1208 1209 $alignments = array( 1210 'none' => __( 'None' ), 1211 'left' => __( 'Left' ), 1212 'center' => __( 'Center' ), 1213 'right' => __( 'Right' ), 1214 ); 1215 1216 if ( ! array_key_exists( (string) $checked, $alignments ) ) { 1217 $checked = 'none'; 1218 } 1219 1220 $output = array(); 1221 1222 foreach ( $alignments as $name => $label ) { 1223 $name = esc_attr( $name ); 1224 $output[] = "<input type='radio' name='attachments[{$post->ID}][align]' id='image-align-{$name}-{$post->ID}' value='$name'" . 1225 ( $checked === $name ? " checked='checked'" : '' ) . 1226 " /><label for='image-align-{$name}-{$post->ID}' class='align image-align-{$name}-label'>$label</label>"; 1227 } 1228 1229 return implode( "\n", $output ); 1230 } 1231 1232 /** 1233 * Retrieves HTML for the size radio buttons with the specified one checked. 1234 * 1235 * @since 2.7.0 1236 * 1237 * @param WP_Post $post 1238 * @param bool|string $check 1239 * @return array<string, string> An array of data for the image size input fields. 1240 */ 1241 function image_size_input_fields( $post, $check = '' ) { 1242 /** 1243 * Filters the names and labels of the default image sizes. 1244 * 1245 * @since 3.3.0 1246 * 1247 * @param string[] $size_names Array of image size labels keyed by their name. Default values 1248 * include 'Thumbnail', 'Medium', 'Large', and 'Full Size'. 1249 */ 1250 $size_names = apply_filters( 1251 'image_size_names_choose', 1252 array( 1253 'thumbnail' => __( 'Thumbnail' ), 1254 'medium' => __( 'Medium' ), 1255 'large' => __( 'Large' ), 1256 'full' => __( 'Full Size' ), 1257 ) 1258 ); 1259 1260 if ( empty( $check ) ) { 1261 $check = get_user_setting( 'imgsize', 'medium' ); 1262 } 1263 1264 $output = array(); 1265 1266 foreach ( $size_names as $size => $label ) { 1267 $downsize = image_downsize( $post->ID, $size ); 1268 $checked = ''; 1269 1270 // Is this size selectable? 1271 $enabled = ( $downsize[3] || 'full' === $size ); 1272 $css_id = "image-size-{$size}-{$post->ID}"; 1273 1274 // If this size is the default but that's not available, don't select it. 1275 if ( $size === $check ) { 1276 if ( $enabled ) { 1277 $checked = " checked='checked'"; 1278 } else { 1279 $check = ''; 1280 } 1281 } elseif ( ! $check && $enabled && 'thumbnail' !== $size ) { 1282 /* 1283 * If $check is not enabled, default to the first available size 1284 * that's bigger than a thumbnail. 1285 */ 1286 $check = $size; 1287 $checked = " checked='checked'"; 1288 } 1289 1290 $html = "<div class='image-size-item'><input type='radio' " . disabled( $enabled, false, false ) . "name='attachments[$post->ID][image-size]' id='{$css_id}' value='{$size}'$checked />"; 1291 1292 $html .= "<label for='{$css_id}'>$label</label>"; 1293 1294 // Only show the dimensions if that choice is available. 1295 if ( $enabled ) { 1296 $html .= " <label for='{$css_id}' class='help'>" . sprintf( '(%d × %d)', $downsize[1], $downsize[2] ) . '</label>'; 1297 } 1298 $html .= '</div>'; 1299 1300 $output[] = $html; 1301 } 1302 1303 return array( 1304 'label' => __( 'Size' ), 1305 'input' => 'html', 1306 'html' => implode( "\n", $output ), 1307 ); 1308 } 1309 1310 /** 1311 * Retrieves HTML for the Link URL buttons with the default link type as specified. 1312 * 1313 * @since 2.7.0 1314 * 1315 * @param WP_Post $post 1316 * @param string $url_type 1317 * @return string HTML markup for the link URL buttons. 1318 * 1319 * @phpstan-return non-falsy-string 1320 */ 1321 function image_link_input_fields( $post, $url_type = '' ) { 1322 1323 $file = wp_get_attachment_url( $post->ID ); 1324 $link = get_attachment_link( $post->ID ); 1325 1326 if ( empty( $url_type ) ) { 1327 $url_type = get_user_setting( 'urlbutton', 'post' ); 1328 } 1329 1330 $url = ''; 1331 1332 if ( 'file' === $url_type ) { 1333 $url = $file; 1334 } elseif ( 'post' === $url_type ) { 1335 $url = $link; 1336 } 1337 1338 return " 1339 <input type='text' class='text urlfield' name='attachments[$post->ID][url]' value='" . esc_attr( $url ) . "' /><br /> 1340 <button type='button' class='button urlnone' data-link-url=''>" . __( 'None' ) . "</button> 1341 <button type='button' class='button urlfile' data-link-url='" . esc_url( $file ) . "'>" . __( 'File URL' ) . "</button> 1342 <button type='button' class='button urlpost' data-link-url='" . esc_url( $link ) . "'>" . __( 'Attachment Post URL' ) . '</button> 1343 '; 1344 } 1345 1346 /** 1347 * Outputs a textarea element for inputting an attachment caption. 1348 * 1349 * @since 3.4.0 1350 * 1351 * @param WP_Post $edit_post Attachment WP_Post object. 1352 * @return string HTML markup for the textarea element. 1353 * 1354 * @phpstan-return non-falsy-string 1355 */ 1356 function wp_caption_input_textarea( $edit_post ) { 1357 // Post data is already escaped. 1358 $name = "attachments[{$edit_post->ID}][post_excerpt]"; 1359 1360 return '<textarea name="' . $name . '" id="' . $name . '">' . $edit_post->post_excerpt . '</textarea>'; 1361 } 1362 1363 /** 1364 * Retrieves the image attachment fields to edit form fields. 1365 * 1366 * @since 2.5.0 1367 * 1368 * @param array $form_fields 1369 * @param object $post 1370 * @return array<string, array<string, mixed>> The attachment form fields. 1371 */ 1372 function image_attachment_fields_to_edit( $form_fields, $post ) { 1373 return $form_fields; 1374 } 1375 1376 /** 1377 * Retrieves the single non-image attachment fields to edit form fields. 1378 * 1379 * @since 2.5.0 1380 * 1381 * @param array $form_fields An array of attachment form fields. 1382 * @param WP_Post $post The WP_Post attachment object. 1383 * @return array Filtered attachment form fields. 1384 */ 1385 function media_single_attachment_fields_to_edit( $form_fields, $post ) { 1386 unset( $form_fields['url'], $form_fields['align'], $form_fields['image-size'] ); 1387 return $form_fields; 1388 } 1389 1390 /** 1391 * Retrieves the post non-image attachment fields to edit form fields. 1392 * 1393 * @since 2.8.0 1394 * 1395 * @param array $form_fields An array of attachment form fields. 1396 * @param WP_Post $post The WP_Post attachment object. 1397 * @return array Filtered attachment form fields. 1398 */ 1399 function media_post_single_attachment_fields_to_edit( $form_fields, $post ) { 1400 unset( $form_fields['image_url'] ); 1401 return $form_fields; 1402 } 1403 1404 /** 1405 * Retrieves the media element HTML to send to the editor. 1406 * 1407 * @since 2.5.0 1408 * 1409 * @param string $html 1410 * @param int $attachment_id 1411 * @param array $attachment 1412 * @return string HTML markup for the media element. 1413 */ 1414 function image_media_send_to_editor( $html, $attachment_id, $attachment ) { 1415 $post = get_post( $attachment_id ); 1416 1417 if ( str_starts_with( $post->post_mime_type, 'image' ) ) { 1418 $url = $attachment['url']; 1419 $align = ! empty( $attachment['align'] ) ? $attachment['align'] : 'none'; 1420 $size = ! empty( $attachment['image-size'] ) ? $attachment['image-size'] : 'medium'; 1421 $alt = ! empty( $attachment['image_alt'] ) ? $attachment['image_alt'] : ''; 1422 $rel = ( str_contains( $url, 'attachment_id' ) || get_attachment_link( $attachment_id ) === $url ); 1423 1424 return get_image_send_to_editor( $attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt ); 1425 } 1426 1427 return $html; 1428 } 1429 1430 /** 1431 * Retrieves the attachment fields to edit form fields. 1432 * 1433 * @since 2.5.0 1434 * 1435 * @param WP_Post $post 1436 * @param array $errors 1437 * @return array<string, array<string, mixed>|string> The attachment fields, keyed by field name. Each value is a 1438 * field definition array, except for the `_final` key, which the 1439 * `attachment_fields_to_edit` filter may set to raw HTML that is 1440 * rendered after all other fields. 1441 * 1442 * @phpstan-return array{ 1443 * _final?: string, 1444 * menu_order?: array<string, mixed>, // Listed explicitly so that unsetting it in get_media_item() does not collapse this shape. 1445 * ...<string, array<string, mixed>> 1446 * } 1447 */ 1448 function get_attachment_fields_to_edit( $post, $errors = null ) { 1449 if ( is_int( $post ) ) { 1450 $post = get_post( $post ); 1451 } 1452 1453 if ( is_array( $post ) ) { 1454 $post = new WP_Post( (object) $post ); 1455 } 1456 1457 $image_url = wp_get_attachment_url( $post->ID ); 1458 1459 $edit_post = sanitize_post( $post, 'edit' ); 1460 1461 $form_fields = array( 1462 'post_title' => array( 1463 'label' => __( 'Title' ), 1464 'value' => $edit_post->post_title, 1465 ), 1466 'image_alt' => array(), 1467 'post_excerpt' => array( 1468 'label' => __( 'Caption' ), 1469 'input' => 'html', 1470 'html' => wp_caption_input_textarea( $edit_post ), 1471 ), 1472 'post_content' => array( 1473 'label' => __( 'Description' ), 1474 'value' => $edit_post->post_content, 1475 'input' => 'textarea', 1476 ), 1477 'url' => array( 1478 'label' => __( 'Link URL' ), 1479 'input' => 'html', 1480 'html' => image_link_input_fields( $post, get_option( 'image_default_link_type' ) ), 1481 'helps' => __( 'Enter a link URL or click above for presets.' ), 1482 ), 1483 'menu_order' => array( 1484 'label' => __( 'Order' ), 1485 'value' => $edit_post->menu_order, 1486 ), 1487 'image_url' => array( 1488 'label' => __( 'File URL' ), 1489 'input' => 'html', 1490 'html' => "<input type='text' class='text urlfield' readonly='readonly' name='attachments[$post->ID][url]' value='" . esc_attr( $image_url ) . "' /><br />", 1491 'value' => wp_get_attachment_url( $post->ID ), 1492 'helps' => __( 'Location of the uploaded file.' ), 1493 ), 1494 ); 1495 1496 foreach ( get_attachment_taxonomies( $post ) as $taxonomy ) { 1497 $t = (array) get_taxonomy( $taxonomy ); 1498 1499 if ( ! $t['public'] || ! $t['show_ui'] ) { 1500 continue; 1501 } 1502 1503 if ( empty( $t['label'] ) ) { 1504 $t['label'] = $taxonomy; 1505 } 1506 1507 if ( empty( $t['args'] ) ) { 1508 $t['args'] = array(); 1509 } 1510 1511 $terms = get_object_term_cache( $post->ID, $taxonomy ); 1512 1513 if ( false === $terms ) { 1514 $terms = wp_get_object_terms( $post->ID, $taxonomy, $t['args'] ); 1515 } 1516 1517 $values = array(); 1518 1519 foreach ( $terms as $term ) { 1520 $values[] = $term->slug; 1521 } 1522 1523 $t['value'] = implode( ', ', $values ); 1524 1525 $form_fields[ $taxonomy ] = $t; 1526 } 1527 1528 /* 1529 * Merge default fields with their errors, so any key passed with the error 1530 * (e.g. 'error', 'helps', 'value') will replace the default. 1531 * The recursive merge is easily traversed with array casting: 1532 * foreach ( (array) $things as $thing ) 1533 */ 1534 $form_fields = array_merge_recursive( $form_fields, (array) $errors ); 1535 1536 // This was formerly in image_attachment_fields_to_edit(). 1537 if ( str_starts_with( $post->post_mime_type, 'image' ) ) { 1538 $alt = get_post_meta( $post->ID, '_wp_attachment_image_alt', true ); 1539 1540 if ( empty( $alt ) ) { 1541 $alt = ''; 1542 } 1543 1544 $form_fields['post_title']['required'] = true; 1545 1546 $form_fields['image_alt'] = array( 1547 'value' => $alt, 1548 'label' => __( 'Alternative Text' ), 1549 'helps' => __( 'Alt text for the image, e.g. “The Mona Lisa”' ), 1550 ); 1551 1552 $form_fields['align'] = array( 1553 'label' => __( 'Alignment' ), 1554 'input' => 'html', 1555 'html' => image_align_input_fields( $post, get_option( 'image_default_align' ) ), 1556 ); 1557 1558 $form_fields['image-size'] = image_size_input_fields( $post, get_option( 'image_default_size', 'medium' ) ); 1559 1560 } else { 1561 unset( $form_fields['image_alt'] ); 1562 } 1563 1564 /** 1565 * Filters the attachment fields to edit. 1566 * 1567 * @since 2.5.0 1568 * 1569 * @param array $form_fields An array of attachment form fields. 1570 * @param WP_Post $post The WP_Post attachment object. 1571 */ 1572 $form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post ); 1573 1574 return $form_fields; 1575 } 1576 1577 /** 1578 * Retrieves HTML for media items of post gallery. 1579 * 1580 * The HTML markup retrieved will be created for the progress of SWF Upload 1581 * component. Will also create link for showing and hiding the form to modify 1582 * the image attachment. 1583 * 1584 * @since 2.5.0 1585 * 1586 * @global WP_Query $wp_the_query WordPress Query object. 1587 * 1588 * @param int $post_id Post ID. 1589 * @param array $errors Errors for attachment, if any. 1590 * @return string HTML content for media items of post gallery. 1591 */ 1592 function get_media_items( $post_id, $errors ) { 1593 $attachments = array(); 1594 1595 if ( $post_id ) { 1596 $post = get_post( $post_id ); 1597 1598 if ( $post && 'attachment' === $post->post_type ) { 1599 $attachments = array( $post->ID => $post ); 1600 } else { 1601 $attachments = get_children( 1602 array( 1603 'post_parent' => $post_id, 1604 'post_type' => 'attachment', 1605 'orderby' => 'menu_order ASC, ID', 1606 'order' => 'DESC', 1607 ) 1608 ); 1609 } 1610 } else { 1611 if ( is_array( $GLOBALS['wp_the_query']->posts ) ) { 1612 foreach ( $GLOBALS['wp_the_query']->posts as $attachment ) { 1613 $attachments[ $attachment->ID ] = $attachment; 1614 } 1615 } 1616 } 1617 1618 $output = ''; 1619 foreach ( (array) $attachments as $id => $attachment ) { 1620 if ( 'trash' === $attachment->post_status ) { 1621 continue; 1622 } 1623 1624 $item = get_media_item( $id, array( 'errors' => $errors[ $id ] ?? null ) ); 1625 1626 if ( $item ) { 1627 $output .= "\n<div id='media-item-$id' class='media-item child-of-$attachment->post_parent preloaded'><div class='progress hidden'><div class='bar'></div></div><div id='media-upload-error-$id' class='hidden'></div><div class='filename hidden'></div>$item\n</div>"; 1628 } 1629 } 1630 1631 return $output; 1632 } 1633 1634 /** 1635 * Retrieves HTML form for modifying the image attachment. 1636 * 1637 * @since 2.5.0 1638 * 1639 * @global string $redir_tab 1640 * 1641 * @param int $attachment_id Attachment ID for modification. 1642 * @param string|array $args Optional. Override defaults. 1643 * @return string HTML form for attachment. 1644 * 1645 * @phpstan-return non-falsy-string 1646 */ 1647 function get_media_item( $attachment_id, $args = null ) { 1648 global $redir_tab; 1649 1650 $thumb_url = false; 1651 $attachment_id = (int) $attachment_id; 1652 1653 if ( $attachment_id ) { 1654 $thumb_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true ); 1655 1656 if ( $thumb_url ) { 1657 $thumb_url = $thumb_url[0]; 1658 } 1659 } 1660 1661 $post = get_post( $attachment_id ); 1662 $current_post_id = ! empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0; 1663 1664 $default_args = array( 1665 'errors' => null, 1666 'send' => $current_post_id ? post_type_supports( get_post_type( $current_post_id ), 'editor' ) : true, 1667 'delete' => true, 1668 'toggle' => true, 1669 'show_title' => true, 1670 ); 1671 1672 $parsed_args = wp_parse_args( $args, $default_args ); 1673 1674 /** 1675 * Filters the arguments used to retrieve an image for the edit image form. 1676 * 1677 * @since 3.1.0 1678 * 1679 * @see get_media_item 1680 * 1681 * @param array $parsed_args An array of arguments. 1682 */ 1683 $parsed_args = apply_filters( 'get_media_item_args', $parsed_args ); 1684 1685 $toggle_on = __( 'Show' ); 1686 $toggle_off = __( 'Hide' ); 1687 1688 $file = get_attached_file( $post->ID ); 1689 $filename = esc_html( wp_basename( $file ) ); 1690 $title = esc_attr( $post->post_title ); 1691 1692 $post_mime_types = get_post_mime_types(); 1693 $matched_types = wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ); 1694 $type = array_key_first( $matched_types ) ?? ''; 1695 $type_html = "<input type='hidden' id='type-of-$attachment_id' value='" . esc_attr( $type ) . "' />"; 1696 1697 $form_fields = get_attachment_fields_to_edit( $post, $parsed_args['errors'] ); 1698 1699 if ( $parsed_args['toggle'] ) { 1700 $class = empty( $parsed_args['errors'] ) ? 'startclosed' : 'startopen'; 1701 $toggle_links = " 1702 <a class='toggle describe-toggle-on' href='#'>$toggle_on</a> 1703 <a class='toggle describe-toggle-off' href='#'>$toggle_off</a>"; 1704 } else { 1705 $class = ''; 1706 $toggle_links = ''; 1707 } 1708 1709 $display_title = ( ! empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case. 1710 $display_title = $parsed_args['show_title'] ? "<div class='filename new'><span class='title'>" . wp_html_excerpt( $display_title, 60, '…' ) . '</span></div>' : ''; 1711 1712 $gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' === $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' === $redir_tab ) ); 1713 $order = ''; 1714 1715 foreach ( $form_fields as $key => $val ) { 1716 if ( 'menu_order' === $key ) { 1717 if ( $gallery ) { 1718 $order = "<div class='menu_order'> <input class='menu_order_input' type='text' id='attachments[$attachment_id][menu_order]' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' /></div>"; 1719 } else { 1720 $order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />"; 1721 } 1722 1723 unset( $form_fields['menu_order'] ); 1724 break; 1725 } 1726 } 1727 1728 $media_dims = ''; 1729 $meta = wp_get_attachment_metadata( $post->ID ); 1730 1731 if ( isset( $meta['width'], $meta['height'] ) ) { 1732 /* translators: 1: A number of pixels wide, 2: A number of pixels tall. */ 1733 $media_dims .= "<span id='media-dims-$post->ID'>" . sprintf( __( '%1$s by %2$s pixels' ), $meta['width'], $meta['height'] ) . '</span>'; 1734 } 1735 1736 /** 1737 * Filters the media metadata. 1738 * 1739 * @since 2.5.0 1740 * 1741 * @param string $media_dims The HTML markup containing the media dimensions. 1742 * @param WP_Post $post The WP_Post attachment object. 1743 */ 1744 $media_dims = apply_filters( 'media_meta', $media_dims, $post ); 1745 1746 $image_edit_button = ''; 1747 1748 if ( wp_attachment_is_image( $post->ID ) && wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) { 1749 $nonce = wp_create_nonce( "image_editor-$post->ID" ); 1750 $image_edit_button = "<input type='button' id='imgedit-open-btn-$post->ID' onclick='imageEdit.open( $post->ID, \"$nonce\" )' class='button' value='" . esc_attr__( 'Edit Image' ) . "' /> <span class='spinner'></span>"; 1751 } 1752 1753 $attachment_url = get_permalink( $attachment_id ); 1754 1755 $item = " 1756 $type_html 1757 $toggle_links 1758 $order 1759 $display_title 1760 <table class='slidetoggle describe $class'> 1761 <thead class='media-item-info' id='media-head-$post->ID'> 1762 <tr> 1763 <td class='A1B1' id='thumbnail-head-$post->ID'> 1764 <p><a href='$attachment_url' target='_blank'><img class='thumbnail' src='$thumb_url' alt='' /></a></p> 1765 <p>$image_edit_button</p> 1766 </td> 1767 <td> 1768 <p><strong>" . __( 'File name:' ) . "</strong> $filename</p> 1769 <p><strong>" . __( 'File type:' ) . "</strong> $post->post_mime_type</p> 1770 <p><strong>" . __( 'Upload date:' ) . '</strong> ' . mysql2date( __( 'F j, Y' ), $post->post_date ) . '</p>'; 1771 1772 if ( ! empty( $media_dims ) ) { 1773 $item .= '<p><strong>' . __( 'Dimensions:' ) . "</strong> $media_dims</p>\n"; 1774 } 1775 1776 $item .= "</td></tr>\n"; 1777 1778 $item .= " 1779 </thead> 1780 <tbody> 1781 <tr><td colspan='2' class='imgedit-response' id='imgedit-response-$post->ID'></td></tr>\n 1782 <tr><td style='display:none' colspan='2' class='image-editor' id='image-editor-$post->ID'></td></tr>\n 1783 <tr><td colspan='2'><p class='media-types media-types-required-info'>" . 1784 wp_required_field_message() . 1785 "</p></td></tr>\n"; 1786 1787 $defaults = array( 1788 'input' => 'text', 1789 'required' => false, 1790 'value' => '', 1791 'extra_rows' => array(), 1792 ); 1793 1794 if ( $parsed_args['send'] ) { 1795 $parsed_args['send'] = get_submit_button( __( 'Insert into Post' ), '', "send[$attachment_id]", false ); 1796 } 1797 1798 $delete = empty( $parsed_args['delete'] ) ? '' : $parsed_args['delete']; 1799 if ( $delete && current_user_can( 'delete_post', $attachment_id ) ) { 1800 if ( ! EMPTY_TRASH_DAYS ) { 1801 $delete = "<a href='" . wp_nonce_url( "post.php?action=delete&post=$attachment_id", 'delete-post_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete-permanently'>" . __( 'Delete Permanently' ) . '</a>'; 1802 } elseif ( ! MEDIA_TRASH ) { 1803 $delete = "<a href='#' class='del-link' onclick=\"document.getElementById('del_attachment_$attachment_id').style.display='block';return false;\">" . __( 'Delete' ) . "</a> 1804 <div id='del_attachment_$attachment_id' class='del-attachment' style='display:none;'>" . 1805 /* translators: %s: File name. */ 1806 '<p>' . sprintf( __( 'You are about to delete %s.' ), '<strong>' . $filename . '</strong>' ) . "</p> 1807 <a href='" . wp_nonce_url( "post.php?action=delete&post=$attachment_id", 'delete-post_' . $attachment_id ) . "' id='del[$attachment_id]' class='button'>" . __( 'Continue' ) . "</a> 1808 <a href='#' class='button' onclick=\"this.parentNode.style.display='none';return false;\">" . __( 'Cancel' ) . '</a> 1809 </div>'; 1810 } else { 1811 $delete = "<a href='" . wp_nonce_url( "post.php?action=trash&post=$attachment_id", 'trash-post_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete'>" . __( 'Move to Trash' ) . "</a> 1812 <a href='" . wp_nonce_url( "post.php?action=untrash&post=$attachment_id", 'untrash-post_' . $attachment_id ) . "' id='undo[$attachment_id]' class='undo hidden'>" . __( 'Undo' ) . '</a>'; 1813 } 1814 } else { 1815 $delete = ''; 1816 } 1817 1818 $thumbnail = ''; 1819 $calling_post_id = 0; 1820 1821 if ( isset( $_GET['post_id'] ) ) { 1822 $calling_post_id = absint( $_GET['post_id'] ); 1823 } elseif ( ! empty( $_POST ) ) { // Like for async-upload where $_GET['post_id'] isn't set. 1824 $calling_post_id = $post->post_parent; 1825 } 1826 1827 if ( 'image' === $type && $calling_post_id 1828 && current_theme_supports( 'post-thumbnails', get_post_type( $calling_post_id ) ) 1829 && post_type_supports( get_post_type( $calling_post_id ), 'thumbnail' ) 1830 && get_post_thumbnail_id( $calling_post_id ) !== $attachment_id 1831 ) { 1832 1833 $calling_post = get_post( $calling_post_id ); 1834 $calling_post_type_object = get_post_type_object( $calling_post->post_type ); 1835 1836 $ajax_nonce = wp_create_nonce( "set_post_thumbnail-$calling_post_id" ); 1837 $thumbnail = "<a class='wp-post-thumbnail' id='wp-post-thumbnail-" . $attachment_id . "' href='#' onclick='WPSetAsThumbnail(\"$attachment_id\", \"$ajax_nonce\");return false;'>" . esc_html( $calling_post_type_object->labels->use_featured_image ) . '</a>'; 1838 } 1839 1840 if ( ( $parsed_args['send'] || $thumbnail || $delete ) && ! isset( $form_fields['buttons'] ) ) { 1841 $form_fields['buttons'] = array( 'tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>" . $parsed_args['send'] . " $thumbnail $delete</td></tr>\n" ); 1842 } 1843 1844 $hidden_fields = array(); 1845 1846 foreach ( $form_fields as $id => $field ) { 1847 if ( '_' === $id[0] ) { 1848 continue; 1849 } 1850 1851 if ( ! empty( $field['tr'] ) ) { 1852 $item .= $field['tr']; 1853 continue; 1854 } 1855 1856 $field = array_merge( $defaults, $field ); 1857 $name = "attachments[$attachment_id][$id]"; 1858 1859 if ( 'hidden' === $field['input'] ) { 1860 $hidden_fields[ $name ] = $field['value']; 1861 continue; 1862 } 1863 1864 $required = $field['required'] ? ' ' . wp_required_field_indicator() : ''; 1865 $required_attr = $field['required'] ? ' required' : ''; 1866 $class = $id; 1867 $class .= $field['required'] ? ' form-required' : ''; 1868 1869 $item .= "\t\t<tr class='$class'>\n\t\t\t<th scope='row' class='label'><label for='$name'><span class='alignleft'>{$field['label']}{$required}</span><br class='clear' /></label></th>\n\t\t\t<td class='field'>"; 1870 1871 if ( ! empty( $field[ $field['input'] ] ) ) { 1872 $item .= $field[ $field['input'] ]; 1873 } elseif ( 'textarea' === $field['input'] ) { 1874 if ( 'post_content' === $id && user_can_richedit() ) { 1875 // Sanitize_post() skips the post_content when user_can_richedit. 1876 $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES ); 1877 } 1878 // Post_excerpt is already escaped by sanitize_post() in get_attachment_fields_to_edit(). 1879 $item .= "<textarea id='$name' name='$name'{$required_attr}>" . $field['value'] . '</textarea>'; 1880 } else { 1881 $item .= "<input type='text' class='text' id='$name' name='$name' value='" . esc_attr( $field['value'] ) . "'{$required_attr} />"; 1882 } 1883 1884 if ( ! empty( $field['helps'] ) ) { 1885 $item .= "<p class='help'>" . implode( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>'; 1886 } 1887 $item .= "</td>\n\t\t</tr>\n"; 1888 1889 $extra_rows = array(); 1890 1891 if ( ! empty( $field['errors'] ) ) { 1892 foreach ( array_unique( (array) $field['errors'] ) as $error ) { 1893 $extra_rows['error'][] = $error; 1894 } 1895 } 1896 1897 if ( ! empty( $field['extra_rows'] ) ) { 1898 foreach ( $field['extra_rows'] as $class => $rows ) { 1899 foreach ( (array) $rows as $html ) { 1900 $extra_rows[ $class ][] = $html; 1901 } 1902 } 1903 } 1904 1905 foreach ( $extra_rows as $class => $rows ) { 1906 foreach ( $rows as $html ) { 1907 $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n"; 1908 } 1909 } 1910 } 1911 1912 if ( ! empty( $form_fields['_final'] ) ) { 1913 $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n"; 1914 } 1915 1916 $item .= "\t</tbody>\n"; 1917 $item .= "\t</table>\n"; 1918 1919 foreach ( $hidden_fields as $name => $value ) { 1920 $item .= "\t<input type='hidden' name='$name' id='$name' value='" . esc_attr( $value ) . "' />\n"; 1921 } 1922 1923 if ( $post->post_parent < 1 && isset( $_REQUEST['post_id'] ) ) { 1924 $parent = (int) $_REQUEST['post_id']; 1925 $parent_name = "attachments[$attachment_id][post_parent]"; 1926 $item .= "\t<input type='hidden' name='$parent_name' id='$parent_name' value='$parent' />\n"; 1927 } 1928 1929 return $item; 1930 } 1931 1932 /** 1933 * Retrieves the media markup for an attachment. 1934 * 1935 * @since 3.5.0 1936 * 1937 * @param int $attachment_id 1938 * @param array $args 1939 * @return array<string, string> An array containing the media item and its metadata. 1940 */ 1941 function get_compat_media_markup( $attachment_id, $args = null ) { 1942 $post = get_post( $attachment_id ); 1943 1944 $default_args = array( 1945 'errors' => null, 1946 'in_modal' => false, 1947 ); 1948 1949 $user_can_edit = current_user_can( 'edit_post', $attachment_id ); 1950 1951 $args = wp_parse_args( $args, $default_args ); 1952 1953 /** This filter is documented in wp-admin/includes/media.php */ 1954 $args = apply_filters( 'get_media_item_args', $args ); 1955 1956 $form_fields = array(); 1957 1958 if ( $args['in_modal'] ) { 1959 foreach ( get_attachment_taxonomies( $post ) as $taxonomy ) { 1960 $t = (array) get_taxonomy( $taxonomy ); 1961 1962 if ( ! $t['public'] || ! $t['show_ui'] ) { 1963 continue; 1964 } 1965 1966 if ( empty( $t['label'] ) ) { 1967 $t['label'] = $taxonomy; 1968 } 1969 1970 if ( empty( $t['args'] ) ) { 1971 $t['args'] = array(); 1972 } 1973 1974 $terms = get_object_term_cache( $post->ID, $taxonomy ); 1975 1976 if ( false === $terms ) { 1977 $terms = wp_get_object_terms( $post->ID, $taxonomy, $t['args'] ); 1978 } 1979 1980 $values = array(); 1981 1982 foreach ( $terms as $term ) { 1983 $values[] = $term->slug; 1984 } 1985 1986 $t['value'] = implode( ', ', $values ); 1987 $t['taxonomy'] = true; 1988 1989 $form_fields[ $taxonomy ] = $t; 1990 } 1991 } 1992 1993 /* 1994 * Merge default fields with their errors, so any key passed with the error 1995 * (e.g. 'error', 'helps', 'value') will replace the default. 1996 * The recursive merge is easily traversed with array casting: 1997 * foreach ( (array) $things as $thing ) 1998 */ 1999 $form_fields = array_merge_recursive( $form_fields, (array) $args['errors'] ); 2000 2001 /** This filter is documented in wp-admin/includes/media.php */ 2002 $form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post ); 2003 2004 unset( 2005 $form_fields['image-size'], 2006 $form_fields['align'], 2007 $form_fields['image_alt'], 2008 $form_fields['post_title'], 2009 $form_fields['post_excerpt'], 2010 $form_fields['post_content'], 2011 $form_fields['url'], 2012 $form_fields['menu_order'], 2013 $form_fields['image_url'] 2014 ); 2015 2016 /** This filter is documented in wp-admin/includes/media.php */ 2017 $media_meta = apply_filters( 'media_meta', '', $post ); 2018 2019 $defaults = array( 2020 'input' => 'text', 2021 'required' => false, 2022 'value' => '', 2023 'extra_rows' => array(), 2024 'show_in_edit' => true, 2025 'show_in_modal' => true, 2026 ); 2027 2028 $hidden_fields = array(); 2029 2030 $item = ''; 2031 2032 foreach ( $form_fields as $id => $field ) { 2033 if ( '_' === $id[0] ) { 2034 continue; 2035 } 2036 2037 $name = "attachments[$attachment_id][$id]"; 2038 $id_attr = "attachments-$attachment_id-$id"; 2039 2040 if ( ! empty( $field['tr'] ) ) { 2041 $item .= $field['tr']; 2042 continue; 2043 } 2044 2045 $field = array_merge( $defaults, $field ); 2046 2047 if ( ( ! $field['show_in_edit'] && ! $args['in_modal'] ) || ( ! $field['show_in_modal'] && $args['in_modal'] ) ) { 2048 continue; 2049 } 2050 2051 if ( 'hidden' === $field['input'] ) { 2052 $hidden_fields[ $name ] = $field['value']; 2053 continue; 2054 } 2055 2056 $readonly = ! $user_can_edit && ! empty( $field['taxonomy'] ) ? " readonly='readonly' " : ''; 2057 $required = $field['required'] ? ' ' . wp_required_field_indicator() : ''; 2058 $required_attr = $field['required'] ? ' required' : ''; 2059 $class = 'compat-field-' . $id; 2060 $class .= $field['required'] ? ' form-required' : ''; 2061 2062 $item .= "\t\t<tr class='$class'>"; 2063 $item .= "\t\t\t<th scope='row' class='label'><label for='$id_attr'><span class='alignleft'>{$field['label']}</span>$required<br class='clear' /></label>"; 2064 $item .= "</th>\n\t\t\t<td class='field'>"; 2065 2066 if ( ! empty( $field[ $field['input'] ] ) ) { 2067 $item .= $field[ $field['input'] ]; 2068 } elseif ( 'textarea' === $field['input'] ) { 2069 if ( 'post_content' === $id && user_can_richedit() ) { 2070 // sanitize_post() skips the post_content when user_can_richedit. 2071 $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES ); 2072 } 2073 $item .= "<textarea id='$id_attr' name='$name'{$required_attr}>" . $field['value'] . '</textarea>'; 2074 } else { 2075 $item .= "<input type='text' class='text' id='$id_attr' name='$name' value='" . esc_attr( $field['value'] ) . "' $readonly{$required_attr} />"; 2076 } 2077 2078 if ( ! empty( $field['helps'] ) ) { 2079 $item .= "<p class='help'>" . implode( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>'; 2080 } 2081 2082 $item .= "</td>\n\t\t</tr>\n"; 2083 2084 $extra_rows = array(); 2085 2086 if ( ! empty( $field['errors'] ) ) { 2087 foreach ( array_unique( (array) $field['errors'] ) as $error ) { 2088 $extra_rows['error'][] = $error; 2089 } 2090 } 2091 2092 if ( ! empty( $field['extra_rows'] ) ) { 2093 foreach ( $field['extra_rows'] as $class => $rows ) { 2094 foreach ( (array) $rows as $html ) { 2095 $extra_rows[ $class ][] = $html; 2096 } 2097 } 2098 } 2099 2100 foreach ( $extra_rows as $class => $rows ) { 2101 foreach ( $rows as $html ) { 2102 $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n"; 2103 } 2104 } 2105 } 2106 2107 if ( ! empty( $form_fields['_final'] ) ) { 2108 $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n"; 2109 } 2110 2111 if ( $item ) { 2112 $item = '<p class="media-types media-types-required-info">' . 2113 wp_required_field_message() . 2114 '</p>' . 2115 '<table class="compat-attachment-fields">' . $item . '</table>'; 2116 } 2117 2118 foreach ( $hidden_fields as $hidden_field => $value ) { 2119 $item .= '<input type="hidden" name="' . esc_attr( $hidden_field ) . '" value="' . esc_attr( $value ) . '" />' . "\n"; 2120 } 2121 2122 if ( $item ) { 2123 $item = '<input type="hidden" name="attachments[' . $attachment_id . '][menu_order]" value="' . esc_attr( $post->menu_order ) . '" />' . $item; 2124 } 2125 2126 return array( 2127 'item' => $item, 2128 'meta' => $media_meta, 2129 ); 2130 } 2131 2132 /** 2133 * Outputs the legacy media upload header. 2134 * 2135 * @since 2.5.0 2136 */ 2137 function media_upload_header() { 2138 $post_id = isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : 0; 2139 2140 wp_print_inline_script_tag( 2141 sprintf( 'var post_id = %s;', wp_json_encode( $post_id, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) ) 2142 ); 2143 2144 if ( empty( $_GET['chromeless'] ) ) { 2145 echo '<div id="media-upload-header">'; 2146 the_media_upload_tabs(); 2147 echo '</div>'; 2148 } 2149 } 2150 2151 /** 2152 * Outputs the legacy media upload form. 2153 * 2154 * @since 2.5.0 2155 * 2156 * @global string $type 2157 * @global string $tab 2158 * 2159 * @param array $errors 2160 */ 2161 function media_upload_form( $errors = null ) { 2162 global $type, $tab; 2163 2164 if ( ! _device_can_upload() ) { 2165 echo '<p>' . sprintf( 2166 /* translators: %s: https://apps.wordpress.org/ */ 2167 __( 'The web browser on your device cannot be used to upload files. You may be able to use the <a href="%s">native app for your device</a> instead.' ), 2168 'https://apps.wordpress.org/' 2169 ) . '</p>'; 2170 return; 2171 } 2172 2173 $upload_action_url = admin_url( 'async-upload.php' ); 2174 $post_id = isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : 0; 2175 $_type = $type ?? ''; 2176 $_tab = $tab ?? ''; 2177 2178 $max_upload_size = wp_max_upload_size(); 2179 if ( ! $max_upload_size ) { 2180 $max_upload_size = 0; 2181 } 2182 2183 ?> 2184 <div id="media-upload-notice"> 2185 <?php 2186 2187 if ( isset( $errors['upload_notice'] ) ) { 2188 echo $errors['upload_notice']; 2189 } 2190 2191 ?> 2192 </div> 2193 <div id="media-upload-error"> 2194 <?php 2195 2196 if ( isset( $errors['upload_error'] ) && is_wp_error( $errors['upload_error'] ) ) { 2197 echo $errors['upload_error']->get_error_message(); 2198 } 2199 2200 ?> 2201 </div> 2202 <?php 2203 2204 if ( is_multisite() && ! is_upload_space_available() ) { 2205 /** 2206 * Fires when an upload will exceed the defined upload space quota for a network site. 2207 * 2208 * @since 3.5.0 2209 */ 2210 do_action( 'upload_ui_over_quota' ); 2211 return; 2212 } 2213 2214 /** 2215 * Fires just before the legacy (pre-3.5.0) upload interface is loaded. 2216 * 2217 * @since 2.6.0 2218 */ 2219 do_action( 'pre-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 2220 2221 $post_params = array( 2222 'post_id' => $post_id, 2223 '_wpnonce' => wp_create_nonce( 'media-form' ), 2224 'type' => $_type, 2225 'tab' => $_tab, 2226 'short' => '1', 2227 ); 2228 2229 /** 2230 * Filters the media upload post parameters. 2231 * 2232 * @since 3.1.0 As 'swfupload_post_params' 2233 * @since 3.3.0 2234 * 2235 * @param array $post_params An array of media upload parameters used by Plupload. 2236 */ 2237 $post_params = apply_filters( 'upload_post_params', $post_params ); 2238 2239 /* 2240 * Since 4.9 the `runtimes` setting is hardcoded in our version of Plupload to `html5,html4`, 2241 * and the `flash_swf_url` and `silverlight_xap_url` are not used. 2242 */ 2243 $plupload_init = array( 2244 'browse_button' => 'plupload-browse-button', 2245 'container' => 'plupload-upload-ui', 2246 'drop_element' => 'drag-drop-area', 2247 'file_data_name' => 'async-upload', 2248 'url' => $upload_action_url, 2249 'filters' => array( 'max_file_size' => $max_upload_size . 'b' ), 2250 'multipart_params' => $post_params, 2251 ); 2252 2253 /* 2254 * Currently only iOS Safari supports multiple files uploading, 2255 * but iOS 7.x has a bug that prevents uploading of videos when enabled. 2256 * See #29602. 2257 */ 2258 if ( 2259 wp_is_mobile() && 2260 str_contains( $_SERVER['HTTP_USER_AGENT'], 'OS 7_' ) && 2261 str_contains( $_SERVER['HTTP_USER_AGENT'], 'like Mac OS X' ) 2262 ) { 2263 $plupload_init['multi_selection'] = false; 2264 } 2265 2266 /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php */ 2267 $prevent_unsupported_uploads = apply_filters( 'wp_prevent_unsupported_mime_type_uploads', true, null ); 2268 2269 if ( $prevent_unsupported_uploads ) { 2270 // Check if WebP images can be edited. 2271 if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) { 2272 $plupload_init['webp_upload_error'] = true; 2273 } 2274 2275 // Check if AVIF images can be edited. 2276 if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/avif' ) ) ) { 2277 $plupload_init['avif_upload_error'] = true; 2278 } 2279 } 2280 2281 /** 2282 * Filters the default Plupload settings. 2283 * 2284 * @since 3.3.0 2285 * 2286 * @param array<string, mixed> $plupload_init An array of default settings used by Plupload. 2287 */ 2288 $plupload_init = apply_filters( 'plupload_init', $plupload_init ); 2289 2290 // Verify size is an int. If not return default value. 2291 $large_size_h = absint( get_option( 'large_size_h' ) ); 2292 2293 if ( ! $large_size_h ) { 2294 $large_size_h = 1024; 2295 } 2296 2297 $large_size_w = absint( get_option( 'large_size_w' ) ); 2298 2299 if ( ! $large_size_w ) { 2300 $large_size_w = 1024; 2301 } 2302 2303 wp_print_inline_script_tag( 2304 sprintf( 2305 'Object.assign( window, %s );', 2306 wp_json_encode( 2307 array( 2308 'resize_height' => $large_size_h, 2309 'resize_width' => $large_size_w, 2310 'wpUploaderInit' => $plupload_init, 2311 ), 2312 JSON_HEX_TAG | JSON_UNESCAPED_SLASHES 2313 ) 2314 ) 2315 ); 2316 ?> 2317 2318 <div id="plupload-upload-ui" class="hide-if-no-js"> 2319 <?php 2320 /** 2321 * Fires before the upload interface loads. 2322 * 2323 * @since 2.6.0 As 'pre-flash-upload-ui' 2324 * @since 3.3.0 2325 */ 2326 do_action( 'pre-plupload-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 2327 2328 ?> 2329 <div id="drag-drop-area"> 2330 <div class="drag-drop-inside"> 2331 <p class="drag-drop-info"><?php _e( 'Drop files to upload' ); ?></p> 2332 <p><?php _ex( 'or', 'Uploader: Drop files here - or - Select Files' ); ?></p> 2333 <p class="drag-drop-buttons"><input id="plupload-browse-button" type="button" value="<?php esc_attr_e( 'Select Files' ); ?>" class="button" /></p> 2334 </div> 2335 </div> 2336 <?php 2337 /** 2338 * Fires after the upload interface loads. 2339 * 2340 * @since 2.6.0 As 'post-flash-upload-ui' 2341 * @since 3.3.0 2342 */ 2343 do_action( 'post-plupload-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 2344 ?> 2345 </div> 2346 2347 <div id="html-upload-ui" class="hide-if-js"> 2348 <?php 2349 /** 2350 * Fires before the upload button in the media upload interface. 2351 * 2352 * @since 2.6.0 2353 */ 2354 do_action( 'pre-html-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 2355 2356 ?> 2357 <p id="async-upload-wrap"> 2358 <label class="screen-reader-text" for="async-upload"> 2359 <?php 2360 /* translators: Hidden accessibility text. */ 2361 _ex( 'Upload', 'verb' ); 2362 ?> 2363 </label> 2364 <input type="file" name="async-upload" id="async-upload" /> 2365 <?php submit_button( _x( 'Upload', 'verb' ), 'primary', 'html-upload', false ); ?> 2366 <a href="#" onclick="try{top.tb_remove();}catch(e){}; return false;"><?php _e( 'Cancel' ); ?></a> 2367 </p> 2368 <div class="clear"></div> 2369 <?php 2370 /** 2371 * Fires after the upload button in the media upload interface. 2372 * 2373 * @since 2.6.0 2374 */ 2375 do_action( 'post-html-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 2376 2377 ?> 2378 </div> 2379 2380 <p class="max-upload-size"> 2381 <?php 2382 /* translators: %s: Maximum allowed file size. */ 2383 printf( __( 'Maximum upload file size: %s.' ), esc_html( size_format( $max_upload_size ) ) ); 2384 ?> 2385 </p> 2386 <?php 2387 2388 /** 2389 * Fires on the post upload UI screen. 2390 * 2391 * Legacy (pre-3.5.0) media workflow hook. 2392 * 2393 * @since 2.6.0 2394 */ 2395 do_action( 'post-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 2396 } 2397 2398 /** 2399 * Outputs the legacy media upload form for a given media type. 2400 * 2401 * @since 2.5.0 2402 * 2403 * @param string $type 2404 * @param array $errors 2405 * @param int|WP_Error $id 2406 */ 2407 function media_upload_type_form( $type = 'file', $errors = null, $id = null ) { 2408 2409 media_upload_header(); 2410 2411 $post_id = isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : 0; 2412 2413 $form_action_url = admin_url( "media-upload.php?type=$type&tab=type&post_id=$post_id" ); 2414 2415 /** 2416 * Filters the media upload form action URL. 2417 * 2418 * @since 2.6.0 2419 * 2420 * @param string $form_action_url The media upload form action URL. 2421 * @param string $type The type of media. Default 'file'. 2422 */ 2423 $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type ); 2424 $form_class = 'media-upload-form type-form validate'; 2425 2426 if ( get_user_setting( 'uploader' ) ) { 2427 $form_class .= ' html-uploader'; 2428 } 2429 2430 ?> 2431 <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="<?php echo $type; ?>-form"> 2432 <?php submit_button( '', 'hidden', 'save', false ); ?> 2433 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" /> 2434 <?php wp_nonce_field( 'media-form' ); ?> 2435 2436 <h3 class="media-title"><?php _e( 'Add media files from your computer' ); ?></h3> 2437 2438 <?php media_upload_form( $errors ); ?> 2439 2440 <?php 2441 wp_print_inline_script_tag( 2442 <<<'JS' 2443 jQuery( function ( $ ) { 2444 var preloaded = $( '.media-item.preloaded' ); 2445 if ( preloaded.length > 0 ) { 2446 preloaded.each( function () { 2447 prepareMediaItem( { id: this.id.replace( /[^0-9]/g, '' ) }, '' ); 2448 } ); 2449 } 2450 updateMediaForm(); 2451 } ); 2452 JS 2453 ); 2454 ?> 2455 <div id="media-items"> 2456 <?php 2457 2458 if ( $id ) { 2459 if ( ! is_wp_error( $id ) ) { 2460 add_filter( 'attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2 ); 2461 echo get_media_items( $id, $errors ); 2462 } else { 2463 echo '<div id="media-upload-error">' . esc_html( $id->get_error_message() ) . '</div></div>'; 2464 exit; 2465 } 2466 } 2467 2468 ?> 2469 </div> 2470 2471 <p class="savebutton ml-submit"> 2472 <?php submit_button( __( 'Save all changes' ), '', 'save', false ); ?> 2473 </p> 2474 </form> 2475 <?php 2476 } 2477 2478 /** 2479 * Outputs the legacy media upload form for external media. 2480 * 2481 * @since 2.7.0 2482 * 2483 * @param string $type 2484 * @param object $errors 2485 * @param int $id 2486 */ 2487 function media_upload_type_url_form( $type = null, $errors = null, $id = null ) { 2488 if ( null === $type ) { 2489 $type = 'image'; 2490 } 2491 2492 media_upload_header(); 2493 2494 $post_id = isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : 0; 2495 2496 $form_action_url = admin_url( "media-upload.php?type=$type&tab=type&post_id=$post_id" ); 2497 /** This filter is documented in wp-admin/includes/media.php */ 2498 $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type ); 2499 $form_class = 'media-upload-form type-form validate'; 2500 2501 if ( get_user_setting( 'uploader' ) ) { 2502 $form_class .= ' html-uploader'; 2503 } 2504 2505 ?> 2506 <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="<?php echo $type; ?>-form"> 2507 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" /> 2508 <?php wp_nonce_field( 'media-form' ); ?> 2509 2510 <h3 class="media-title"><?php _e( 'Insert media from another website' ); ?></h3> 2511 2512 <?php ob_start(); ?> 2513 <script> 2514 var addExtImage = { 2515 2516 width : '', 2517 height : '', 2518 align : 'alignnone', 2519 2520 insert : function() { 2521 var t = this, html, f = document.forms[0], cls, title = '', alt = '', caption = ''; 2522 2523 if ( '' === f.src.value || '' === t.width ) 2524 return false; 2525 2526 if ( f.alt.value ) 2527 alt = f.alt.value.replace(/'/g, ''').replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>'); 2528 2529 <?php 2530 /** This filter is documented in wp-admin/includes/media.php */ 2531 if ( ! apply_filters( 'disable_captions', '' ) ) { 2532 ?> 2533 if ( f.caption.value ) { 2534 caption = f.caption.value.replace(/\r\n|\r/g, '\n'); 2535 caption = caption.replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(a){ 2536 return a.replace(/[\r\n\t]+/, ' '); 2537 }); 2538 2539 caption = caption.replace(/\s*\n\s*/g, '<br />'); 2540 } 2541 <?php 2542 } 2543 2544 ?> 2545 cls = caption ? '' : ' class="'+t.align+'"'; 2546 2547 html = '<img alt="'+alt+'" src="'+f.src.value+'"'+cls+' width="'+t.width+'" height="'+t.height+'" />'; 2548 2549 if ( f.url.value ) { 2550 url = f.url.value.replace(/'/g, ''').replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>'); 2551 html = '<a href="'+url+'">'+html+'</a>'; 2552 } 2553 2554 if ( caption ) 2555 html = '[caption id="" align="'+t.align+'" width="'+t.width+'"]'+html+caption+'[/caption]'; 2556 2557 var win = window.dialogArguments || opener || parent || top; 2558 win.send_to_editor(html); 2559 return false; 2560 }, 2561 2562 resetImageData : function() { 2563 var t = addExtImage; 2564 2565 t.width = t.height = ''; 2566 document.getElementById('go_button').style.color = '#bbb'; 2567 if ( ! document.forms[0].src.value ) 2568 document.getElementById('status_img').innerHTML = ''; 2569 else document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/no.png' ) ); ?>" alt="" />'; 2570 }, 2571 2572 updateImageData : function() { 2573 var t = addExtImage; 2574 2575 t.width = t.preloadImg.width; 2576 t.height = t.preloadImg.height; 2577 document.getElementById('go_button').style.color = '#333'; 2578 document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/yes.png' ) ); ?>" alt="" />'; 2579 }, 2580 2581 getImageData : function() { 2582 if ( jQuery('table.describe').hasClass('not-image') ) 2583 return; 2584 2585 var t = addExtImage, src = document.forms[0].src.value; 2586 2587 if ( ! src ) { 2588 t.resetImageData(); 2589 return false; 2590 } 2591 2592 document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/spinner-2x.gif' ) ); ?>" alt="" width="16" height="16" />'; 2593 t.preloadImg = new Image(); 2594 t.preloadImg.onload = t.updateImageData; 2595 t.preloadImg.onerror = t.resetImageData; 2596 t.preloadImg.src = src; 2597 } 2598 }; 2599 2600 jQuery( function($) { 2601 $('.media-types input').click( function() { 2602 $('table.describe').toggleClass('not-image', $('#not-image').prop('checked') ); 2603 }); 2604 } ); 2605 </script> 2606 <?php wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( (string) ob_get_clean() ) ); ?> 2607 2608 <div id="media-items"> 2609 <div class="media-item media-blank"> 2610 <?php 2611 /** 2612 * Filters the insert media from URL form HTML. 2613 * 2614 * @since 3.3.0 2615 * 2616 * @param string $form_html The insert from URL form HTML. 2617 */ 2618 echo apply_filters( 'type_url_form_media', wp_media_insert_url_form( $type ) ); 2619 2620 ?> 2621 </div> 2622 </div> 2623 </form> 2624 <?php 2625 } 2626 2627 /** 2628 * Adds gallery form to upload iframe. 2629 * 2630 * @since 2.5.0 2631 * 2632 * @global string $redir_tab 2633 * @global string $type 2634 * @global string $tab 2635 * 2636 * @param array $errors 2637 */ 2638 function media_upload_gallery_form( $errors ) { 2639 global $redir_tab, $type; 2640 2641 $redir_tab = 'gallery'; 2642 media_upload_header(); 2643 2644 $post_id = (int) $_REQUEST['post_id']; 2645 $form_action_url = admin_url( "media-upload.php?type=$type&tab=gallery&post_id=$post_id" ); 2646 /** This filter is documented in wp-admin/includes/media.php */ 2647 $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type ); 2648 $form_class = 'media-upload-form validate'; 2649 2650 if ( get_user_setting( 'uploader' ) ) { 2651 $form_class .= ' html-uploader'; 2652 } 2653 2654 wp_print_inline_script_tag( 2655 <<<'JS' 2656 jQuery( function ( $ ) { 2657 var preloaded = $( '.media-item.preloaded' ); 2658 if ( preloaded.length > 0 ) { 2659 preloaded.each( function () { 2660 prepareMediaItem( { id: this.id.replace( /[^0-9]/g, '' ) }, '' ); 2661 } ); 2662 updateMediaForm(); 2663 } 2664 } ); 2665 JS 2666 ); 2667 ?> 2668 <div id="sort-buttons" class="hide-if-no-js"> 2669 <span> 2670 <?php _e( 'All Tabs:' ); ?> 2671 <a href="#" id="showall"><?php _e( 'Show' ); ?></a> 2672 <a href="#" id="hideall" style="display:none;"><?php _e( 'Hide' ); ?></a> 2673 </span> 2674 <?php _e( 'Sort Order:' ); ?> 2675 <a href="#" id="asc"><?php _e( 'Ascending' ); ?></a> | 2676 <a href="#" id="desc"><?php _e( 'Descending' ); ?></a> | 2677 <a href="#" id="clear"><?php _ex( 'Clear', 'verb' ); ?></a> 2678 </div> 2679 <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="gallery-form"> 2680 <?php wp_nonce_field( 'media-form' ); ?> 2681 <table class="widefat"> 2682 <thead><tr> 2683 <th><?php _e( 'Media' ); ?></th> 2684 <th class="order-head"><?php _e( 'Order' ); ?></th> 2685 <th class="actions-head"><?php _e( 'Actions' ); ?></th> 2686 </tr></thead> 2687 </table> 2688 <div id="media-items"> 2689 <?php add_filter( 'attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2 ); ?> 2690 <?php echo get_media_items( $post_id, $errors ); ?> 2691 </div> 2692 2693 <p class="ml-submit"> 2694 <?php 2695 submit_button( 2696 __( 'Save all changes' ), 2697 'savebutton', 2698 'save', 2699 false, 2700 array( 2701 'id' => 'save-all', 2702 'style' => 'display: none;', 2703 ) 2704 ); 2705 ?> 2706 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" /> 2707 <input type="hidden" name="type" value="<?php echo esc_attr( $GLOBALS['type'] ); ?>" /> 2708 <input type="hidden" name="tab" value="<?php echo esc_attr( $GLOBALS['tab'] ); ?>" /> 2709 </p> 2710 2711 <div id="gallery-settings" style="display:none;"> 2712 <div class="title"><?php _e( 'Gallery Settings' ); ?></div> 2713 <table id="basic" class="describe"><tbody> 2714 <tr> 2715 <th scope="row" class="label"> 2716 <label> 2717 <span class="alignleft"><?php _e( 'Link thumbnails to:' ); ?></span> 2718 </label> 2719 </th> 2720 <td class="field"> 2721 <input type="radio" name="linkto" id="linkto-file" value="file" /> 2722 <label for="linkto-file" class="radio"><?php _e( 'Image File' ); ?></label> 2723 2724 <input type="radio" checked="checked" name="linkto" id="linkto-post" value="post" /> 2725 <label for="linkto-post" class="radio"><?php _e( 'Attachment Page' ); ?></label> 2726 </td> 2727 </tr> 2728 2729 <tr> 2730 <th scope="row" class="label"> 2731 <label> 2732 <span class="alignleft"><?php _e( 'Order images by:' ); ?></span> 2733 </label> 2734 </th> 2735 <td class="field"> 2736 <select id="orderby" name="orderby"> 2737 <option value="menu_order" selected="selected"><?php _e( 'Menu order' ); ?></option> 2738 <option value="title"><?php _e( 'Title' ); ?></option> 2739 <option value="post_date"><?php _e( 'Date/Time' ); ?></option> 2740 <option value="rand"><?php _e( 'Random' ); ?></option> 2741 </select> 2742 </td> 2743 </tr> 2744 2745 <tr> 2746 <th scope="row" class="label"> 2747 <label> 2748 <span class="alignleft"><?php _e( 'Order:' ); ?></span> 2749 </label> 2750 </th> 2751 <td class="field"> 2752 <input type="radio" checked="checked" name="order" id="order-asc" value="asc" /> 2753 <label for="order-asc" class="radio"><?php _e( 'Ascending' ); ?></label> 2754 2755 <input type="radio" name="order" id="order-desc" value="desc" /> 2756 <label for="order-desc" class="radio"><?php _e( 'Descending' ); ?></label> 2757 </td> 2758 </tr> 2759 2760 <tr> 2761 <th scope="row" class="label"> 2762 <label> 2763 <span class="alignleft"><?php _e( 'Gallery columns:' ); ?></span> 2764 </label> 2765 </th> 2766 <td class="field"> 2767 <select id="columns" name="columns"> 2768 <option value="1">1</option> 2769 <option value="2">2</option> 2770 <option value="3" selected="selected">3</option> 2771 <option value="4">4</option> 2772 <option value="5">5</option> 2773 <option value="6">6</option> 2774 <option value="7">7</option> 2775 <option value="8">8</option> 2776 <option value="9">9</option> 2777 </select> 2778 </td> 2779 </tr> 2780 </tbody></table> 2781 2782 <p class="ml-submit"> 2783 <input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="insert-gallery" id="insert-gallery" value="<?php esc_attr_e( 'Insert gallery' ); ?>" /> 2784 <input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="update-gallery" id="update-gallery" value="<?php esc_attr_e( 'Update gallery settings' ); ?>" /> 2785 </p> 2786 </div> 2787 </form> 2788 <?php 2789 } 2790 2791 /** 2792 * Outputs the legacy media upload form for the media library. 2793 * 2794 * @since 2.5.0 2795 * 2796 * @global wpdb $wpdb WordPress database abstraction object. 2797 * @global WP_Query $wp_query WordPress Query object. 2798 * @global WP_Locale $wp_locale WordPress date and time locale object. 2799 * @global string $type 2800 * @global string $tab 2801 * @global array $post_mime_types 2802 * 2803 * @param array $errors 2804 */ 2805 function media_upload_library_form( $errors ) { 2806 global $wpdb, $wp_query, $wp_locale, $type, $tab, $post_mime_types; 2807 2808 media_upload_header(); 2809 2810 $post_id = isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : 0; 2811 2812 $form_action_url = admin_url( "media-upload.php?type=$type&tab=library&post_id=$post_id" ); 2813 /** This filter is documented in wp-admin/includes/media.php */ 2814 $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type ); 2815 $form_class = 'media-upload-form validate'; 2816 2817 if ( get_user_setting( 'uploader' ) ) { 2818 $form_class .= ' html-uploader'; 2819 } 2820 2821 $q = $_GET; 2822 $q['posts_per_page'] = 10; 2823 $q['paged'] = isset( $q['paged'] ) ? (int) $q['paged'] : 0; 2824 if ( $q['paged'] < 1 ) { 2825 $q['paged'] = 1; 2826 } 2827 $q['offset'] = ( $q['paged'] - 1 ) * 10; 2828 if ( $q['offset'] < 1 ) { 2829 $q['offset'] = 0; 2830 } 2831 2832 list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query( $q ); 2833 2834 ?> 2835 <form id="filter" method="get"> 2836 <input type="hidden" name="type" value="<?php echo esc_attr( $type ); ?>" /> 2837 <input type="hidden" name="tab" value="<?php echo esc_attr( $tab ); ?>" /> 2838 <input type="hidden" name="post_id" value="<?php echo (int) $post_id; ?>" /> 2839 <input type="hidden" name="post_mime_type" value="<?php echo isset( $_GET['post_mime_type'] ) ? esc_attr( $_GET['post_mime_type'] ) : ''; ?>" /> 2840 <input type="hidden" name="context" value="<?php echo isset( $_GET['context'] ) ? esc_attr( $_GET['context'] ) : ''; ?>" /> 2841 2842 <p id="media-search" class="search-box"> 2843 <label class="screen-reader-text" for="media-search-input"> 2844 <?php 2845 /* translators: Hidden accessibility text. */ 2846 _e( 'Search Media:' ); 2847 ?> 2848 </label> 2849 <input type="search" id="media-search-input" name="s" value="<?php the_search_query(); ?>" /> 2850 <?php submit_button( __( 'Search Media' ), '', '', false ); ?> 2851 </p> 2852 2853 <ul class="subsubsub"> 2854 <?php 2855 $type_links = array(); 2856 $_num_posts = (array) wp_count_attachments(); 2857 $matches = wp_match_mime_types( array_keys( $post_mime_types ), array_keys( $_num_posts ) ); 2858 foreach ( $matches as $_type => $reals ) { 2859 foreach ( $reals as $real ) { 2860 if ( isset( $num_posts[ $_type ] ) ) { 2861 $num_posts[ $_type ] += $_num_posts[ $real ]; 2862 } else { 2863 $num_posts[ $_type ] = $_num_posts[ $real ]; 2864 } 2865 } 2866 } 2867 // If available type specified by media button clicked, filter by that type. 2868 if ( empty( $_GET['post_mime_type'] ) && ! empty( $num_posts[ $type ] ) ) { 2869 $_GET['post_mime_type'] = $type; 2870 list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query(); 2871 } 2872 if ( empty( $_GET['post_mime_type'] ) || 'all' === $_GET['post_mime_type'] ) { 2873 $class = ' class="current"'; 2874 } else { 2875 $class = ''; 2876 } 2877 $type_links[] = '<li><a href="' . esc_url( 2878 add_query_arg( 2879 array( 2880 'post_mime_type' => 'all', 2881 'paged' => false, 2882 'm' => false, 2883 ) 2884 ) 2885 ) . '"' . $class . '>' . __( 'All Types' ) . '</a>'; 2886 foreach ( $post_mime_types as $mime_type => $label ) { 2887 $class = ''; 2888 2889 if ( ! wp_match_mime_types( $mime_type, $avail_post_mime_types ) ) { 2890 continue; 2891 } 2892 2893 if ( isset( $_GET['post_mime_type'] ) && wp_match_mime_types( $mime_type, $_GET['post_mime_type'] ) ) { 2894 $class = ' class="current"'; 2895 } 2896 2897 $type_links[] = '<li><a href="' . esc_url( 2898 add_query_arg( 2899 array( 2900 'post_mime_type' => $mime_type, 2901 'paged' => false, 2902 ) 2903 ) 2904 ) . '"' . $class . '>' . sprintf( translate_nooped_plural( $label[2], $num_posts[ $mime_type ] ), '<span id="' . $mime_type . '-counter">' . number_format_i18n( $num_posts[ $mime_type ] ) . '</span>' ) . '</a>'; 2905 } 2906 /** 2907 * Filters the media upload mime type list items. 2908 * 2909 * Returned values should begin with an `<li>` tag. 2910 * 2911 * @since 3.1.0 2912 * 2913 * @param string[] $type_links An array of list items containing mime type link HTML. 2914 */ 2915 echo implode( ' | </li>', apply_filters( 'media_upload_mime_type_links', $type_links ) ) . '</li>'; 2916 unset( $type_links ); 2917 ?> 2918 </ul> 2919 2920 <div class="tablenav"> 2921 2922 <?php 2923 $page_links = paginate_links( 2924 array( 2925 'base' => add_query_arg( 'paged', '%#%' ), 2926 'format' => '', 2927 'prev_text' => __( '«' ), 2928 'next_text' => __( '»' ), 2929 'total' => (int) ceil( $wp_query->found_posts / 10 ), 2930 'current' => $q['paged'], 2931 ) 2932 ); 2933 2934 if ( $page_links ) { 2935 echo "<div class='tablenav-pages'>$page_links</div>"; 2936 } 2937 ?> 2938 2939 <div class="alignleft actions"> 2940 <?php 2941 $months = $wpdb->get_results( 2942 "SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month 2943 FROM $wpdb->posts 2944 WHERE post_type = 'attachment' 2945 ORDER BY post_date DESC" 2946 ); 2947 2948 $month_count = count( $months ); 2949 $selected_month = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0; 2950 2951 if ( $month_count && ( 1 !== $month_count || 0 !== (int) $months[0]->month ) ) { 2952 ?> 2953 <select name='m'> 2954 <option<?php selected( $selected_month, 0 ); ?> value='0'><?php _e( 'All dates' ); ?></option> 2955 <?php 2956 foreach ( $months as $arc_row ) { 2957 if ( 0 === (int) $arc_row->year ) { 2958 continue; 2959 } 2960 2961 $month = zeroise( $arc_row->month, 2 ); 2962 $year = $arc_row->year; 2963 2964 printf( 2965 "<option %s value='%s'>%s</option>\n", 2966 selected( $selected_month, $year . $month, false ), 2967 esc_attr( $year . $month ), 2968 /* translators: 1: Month name, 2: 4-digit year. */ 2969 esc_html( sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month ), $year ) ) 2970 ); 2971 } 2972 ?> 2973 </select> 2974 <?php } ?> 2975 2976 <?php submit_button( __( 'Filter »' ), 'compact', 'post-query-submit', false ); ?> 2977 2978 </div> 2979 2980 <br class="clear" /> 2981 </div> 2982 </form> 2983 2984 <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="library-form"> 2985 <?php wp_nonce_field( 'media-form' ); ?> 2986 2987 <?php 2988 wp_print_inline_script_tag( 2989 <<<'JS' 2990 jQuery( function ( $ ) { 2991 var preloaded = $( '.media-item.preloaded' ); 2992 if ( preloaded.length > 0 ) { 2993 preloaded.each( function () { 2994 prepareMediaItem( { id: this.id.replace( /[^0-9]/g, '' ) }, '' ); 2995 } ); 2996 updateMediaForm(); 2997 } 2998 } ); 2999 JS 3000 ); 3001 ?> 3002 3003 <div id="media-items"> 3004 <?php add_filter( 'attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2 ); ?> 3005 <?php echo get_media_items( null, $errors ); ?> 3006 </div> 3007 <p class="ml-submit"> 3008 <?php submit_button( __( 'Save all changes' ), 'savebutton', 'save', false ); ?> 3009 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" /> 3010 </p> 3011 </form> 3012 <?php 3013 } 3014 3015 /** 3016 * Creates the form for external url. 3017 * 3018 * @since 2.7.0 3019 * 3020 * @param string $default_view 3021 * @return string HTML content of the form. 3022 * 3023 * @phpstan-return non-falsy-string 3024 */ 3025 function wp_media_insert_url_form( $default_view = 'image' ) { 3026 /** This filter is documented in wp-admin/includes/media.php */ 3027 if ( ! apply_filters( 'disable_captions', '' ) ) { 3028 $caption = ' 3029 <tr class="image-only"> 3030 <th scope="row" class="label"> 3031 <label for="caption"><span class="alignleft">' . __( 'Image Caption' ) . '</span></label> 3032 </th> 3033 <td class="field"><textarea id="caption" name="caption"></textarea></td> 3034 </tr>'; 3035 } else { 3036 $caption = ''; 3037 } 3038 3039 $default_align = get_option( 'image_default_align' ); 3040 3041 if ( empty( $default_align ) ) { 3042 $default_align = 'none'; 3043 } 3044 3045 if ( 'image' === $default_view ) { 3046 $view = 'image-only'; 3047 $table_class = ''; 3048 } else { 3049 $view = 'not-image'; 3050 $table_class = $view; 3051 } 3052 3053 return ' 3054 <p class="media-types"><label><input type="radio" name="media_type" value="image" id="image-only"' . checked( 'image-only', $view, false ) . ' /> ' . __( 'Image' ) . '</label> <label><input type="radio" name="media_type" value="generic" id="not-image"' . checked( 'not-image', $view, false ) . ' /> ' . __( 'Audio, Video, or Other File' ) . '</label></p> 3055 <p class="media-types media-types-required-info">' . 3056 wp_required_field_message() . 3057 '</p> 3058 <table class="describe ' . $table_class . '"><tbody> 3059 <tr> 3060 <th scope="row" class="label" style="width:130px;"> 3061 <label for="src"><span class="alignleft">' . __( 'URL' ) . '</span> ' . wp_required_field_indicator() . '</label> 3062 <span class="alignright" id="status_img"></span> 3063 </th> 3064 <td class="field"><input id="src" name="src" value="" type="text" required onblur="addExtImage.getImageData()" /></td> 3065 </tr> 3066 3067 <tr> 3068 <th scope="row" class="label"> 3069 <label for="title"><span class="alignleft">' . __( 'Title' ) . '</span> ' . wp_required_field_indicator() . '</label> 3070 </th> 3071 <td class="field"><input id="title" name="title" value="" type="text" required /></td> 3072 </tr> 3073 3074 <tr class="not-image"><td></td><td><p class="help">' . __( 'Link text, e.g. “Ransom Demands (PDF)”' ) . '</p></td></tr> 3075 3076 <tr class="image-only"> 3077 <th scope="row" class="label"> 3078 <label for="alt"><span class="alignleft">' . __( 'Alternative Text' ) . '</span> ' . wp_required_field_indicator() . '</label> 3079 </th> 3080 <td class="field"><input id="alt" name="alt" value="" type="text" required /> 3081 <p class="help">' . __( 'Alt text for the image, e.g. “The Mona Lisa”' ) . '</p></td> 3082 </tr> 3083 ' . $caption . ' 3084 <tr class="align image-only"> 3085 <th scope="row" class="label"><p><label for="align">' . __( 'Alignment' ) . '</label></p></th> 3086 <td class="field"> 3087 <input name="align" id="align-none" value="none" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ( 'none' === $default_align ? ' checked="checked"' : '' ) . ' /> 3088 <label for="align-none" class="align image-align-none-label">' . __( 'None' ) . '</label> 3089 <input name="align" id="align-left" value="left" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ( 'left' === $default_align ? ' checked="checked"' : '' ) . ' /> 3090 <label for="align-left" class="align image-align-left-label">' . __( 'Left' ) . '</label> 3091 <input name="align" id="align-center" value="center" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ( 'center' === $default_align ? ' checked="checked"' : '' ) . ' /> 3092 <label for="align-center" class="align image-align-center-label">' . __( 'Center' ) . '</label> 3093 <input name="align" id="align-right" value="right" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ( 'right' === $default_align ? ' checked="checked"' : '' ) . ' /> 3094 <label for="align-right" class="align image-align-right-label">' . __( 'Right' ) . '</label> 3095 </td> 3096 </tr> 3097 3098 <tr class="image-only"> 3099 <th scope="row" class="label"> 3100 <label for="url"><span class="alignleft">' . __( 'Link Image To:' ) . '</span></label> 3101 </th> 3102 <td class="field"><input id="url" name="url" value="" type="text" /><br /> 3103 3104 <button type="button" class="button" value="" onclick="document.forms[0].url.value=null">' . __( 'None' ) . '</button> 3105 <button type="button" class="button" value="" onclick="document.forms[0].url.value=document.forms[0].src.value">' . __( 'Link to image' ) . '</button> 3106 <p class="help">' . __( 'Enter a link URL or click above for presets.' ) . '</p></td> 3107 </tr> 3108 <tr class="image-only"> 3109 <td></td> 3110 <td> 3111 <input type="button" class="button" id="go_button" style="color:#bbb;" onclick="addExtImage.insert()" value="' . esc_attr__( 'Insert into Post' ) . '" /> 3112 </td> 3113 </tr> 3114 <tr class="not-image"> 3115 <td></td> 3116 <td> 3117 ' . get_submit_button( __( 'Insert into Post' ), '', 'insertonlybutton', false ) . ' 3118 </td> 3119 </tr> 3120 </tbody></table>'; 3121 } 3122 3123 /** 3124 * Displays the multi-file uploader message. 3125 * 3126 * @since 2.6.0 3127 */ 3128 function media_upload_flash_bypass() { 3129 ?> 3130 <p class="upload-flash-bypass"> 3131 <?php 3132 printf( 3133 /* translators: %s: HTML attributes for button. */ 3134 __( 'You are using the multi-file uploader. Problems? Try the <button %s>browser uploader</button> instead.' ), 3135 'type="button" class="button-link"' 3136 ); 3137 ?> 3138 </p> 3139 <?php 3140 } 3141 3142 /** 3143 * Displays the browser's built-in uploader message. 3144 * 3145 * @since 2.6.0 3146 */ 3147 function media_upload_html_bypass() { 3148 ?> 3149 <p class="upload-html-bypass hide-if-no-js"> 3150 <?php 3151 printf( 3152 /* translators: %s: HTML attributes for button. */ 3153 __( 'You are using the browser’s built-in file uploader. The WordPress uploader includes multiple file selection and drag and drop capability. <button %s>Switch to the multi-file uploader</button>.' ), 3154 'type="button" class="button-link"' 3155 ); 3156 ?> 3157 </p> 3158 <?php 3159 } 3160 3161 /** 3162 * Used to display a "After a file has been uploaded..." help message. 3163 * 3164 * @since 3.3.0 3165 */ 3166 function media_upload_text_after() {} 3167 3168 /** 3169 * Displays the checkbox to scale images. 3170 * 3171 * @since 3.3.0 3172 */ 3173 function media_upload_max_image_resize() { 3174 $checked = get_user_setting( 'upload_resize' ) ? ' checked="true"' : ''; 3175 $a = ''; 3176 $end = ''; 3177 3178 if ( current_user_can( 'manage_options' ) ) { 3179 $a = '<a href="' . esc_url( admin_url( 'options-media.php' ) ) . '" target="_blank">'; 3180 $end = '</a>'; 3181 } 3182 3183 ?> 3184 <p class="hide-if-no-js"><label> 3185 <input name="image_resize" type="checkbox" id="image_resize" value="true"<?php echo $checked; ?> /> 3186 <?php 3187 /* translators: 1: Link start tag, 2: Link end tag, 3: Width, 4: Height. */ 3188 printf( __( 'Scale images to match the large size selected in %1$simage options%2$s (%3$d × %4$d).' ), $a, $end, (int) get_option( 'large_size_w', '1024' ), (int) get_option( 'large_size_h', '1024' ) ); 3189 3190 ?> 3191 </label></p> 3192 <?php 3193 } 3194 3195 /** 3196 * Displays the out of storage quota message in Multisite. 3197 * 3198 * @since 3.5.0 3199 */ 3200 function multisite_over_quota_message() { 3201 echo '<p>' . sprintf( 3202 /* translators: %s: Allowed space allocation. */ 3203 __( 'Sorry, you have used your space allocation of %s. Please delete some files to upload more files.' ), 3204 size_format( get_space_allowed() * MB_IN_BYTES ) 3205 ) . '</p>'; 3206 } 3207 3208 /** 3209 * Displays the image and editor in the post editor 3210 * 3211 * @since 3.5.0 3212 * 3213 * @param WP_Post $post A post object. 3214 */ 3215 function edit_form_image_editor( $post ) { 3216 $open = isset( $_GET['image-editor'] ); 3217 3218 if ( $open ) { 3219 require_once ABSPATH . 'wp-admin/includes/image-edit.php'; 3220 } 3221 3222 $thumb_url = false; 3223 $attachment_id = (int) $post->ID; 3224 3225 if ( $attachment_id ) { 3226 $thumb_url = wp_get_attachment_image_src( $attachment_id, array( 900, 450 ), true ); 3227 } 3228 3229 $alt_text = get_post_meta( $post->ID, '_wp_attachment_image_alt', true ); 3230 3231 $att_url = wp_get_attachment_url( $post->ID ); 3232 ?> 3233 <div class="wp_attachment_holder wp-clearfix"> 3234 <?php 3235 3236 if ( wp_attachment_is_image( $post->ID ) ) : 3237 $image_edit_button = ''; 3238 if ( wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) { 3239 $nonce = wp_create_nonce( "image_editor-$post->ID" ); 3240 $image_edit_button = "<input type='button' id='imgedit-open-btn-$post->ID' onclick='imageEdit.open( $post->ID, \"$nonce\" )' class='button' value='" . esc_attr__( 'Edit Image' ) . "' /> <span class='spinner'></span>"; 3241 } 3242 3243 $open_style = ''; 3244 $not_open_style = ''; 3245 3246 if ( $open ) { 3247 $open_style = ' style="display:none"'; 3248 } else { 3249 $not_open_style = ' style="display:none"'; 3250 } 3251 3252 ?> 3253 <div class="imgedit-response" id="imgedit-response-<?php echo $attachment_id; ?>"></div> 3254 3255 <div<?php echo $open_style; ?> class="wp_attachment_image wp-clearfix" id="media-head-<?php echo $attachment_id; ?>"> 3256 <p id="thumbnail-head-<?php echo $attachment_id; ?>"><img class="thumbnail" src="<?php echo set_url_scheme( $thumb_url[0] ); ?>" style="max-width:100%" alt="" /></p> 3257 <p><?php echo $image_edit_button; ?></p> 3258 </div> 3259 <div<?php echo $not_open_style; ?> class="image-editor" id="image-editor-<?php echo $attachment_id; ?>"> 3260 <?php 3261 3262 if ( $open ) { 3263 wp_image_editor( $attachment_id ); 3264 } 3265 3266 ?> 3267 </div> 3268 <?php 3269 elseif ( $attachment_id && wp_attachment_is( 'audio', $post ) ) : 3270 3271 wp_maybe_generate_attachment_metadata( $post ); 3272 3273 echo wp_audio_shortcode( array( 'src' => $att_url ) ); 3274 3275 elseif ( $attachment_id && wp_attachment_is( 'video', $post ) ) : 3276 3277 wp_maybe_generate_attachment_metadata( $post ); 3278 3279 $meta = wp_get_attachment_metadata( $attachment_id ); 3280 $w = ! empty( $meta['width'] ) ? min( $meta['width'], 640 ) : 0; 3281 $h = ! empty( $meta['height'] ) ? $meta['height'] : 0; 3282 3283 if ( $h && $w < $meta['width'] ) { 3284 $h = round( ( $meta['height'] * $w ) / $meta['width'] ); 3285 } 3286 3287 $attr = array( 'src' => $att_url ); 3288 3289 if ( ! empty( $w ) && ! empty( $h ) ) { 3290 $attr['width'] = $w; 3291 $attr['height'] = $h; 3292 } 3293 3294 $thumb_id = get_post_thumbnail_id( $attachment_id ); 3295 3296 if ( ! empty( $thumb_id ) ) { 3297 $attr['poster'] = wp_get_attachment_url( $thumb_id ); 3298 } 3299 3300 echo wp_video_shortcode( $attr ); 3301 3302 elseif ( isset( $thumb_url[0] ) ) : 3303 ?> 3304 <div class="wp_attachment_image wp-clearfix" id="media-head-<?php echo $attachment_id; ?>"> 3305 <p id="thumbnail-head-<?php echo $attachment_id; ?>"> 3306 <img class="thumbnail" src="<?php echo set_url_scheme( $thumb_url[0] ); ?>" style="max-width:100%" alt="" /> 3307 </p> 3308 </div> 3309 <?php 3310 3311 else : 3312 3313 /** 3314 * Fires when an attachment type can't be rendered in the edit form. 3315 * 3316 * @since 4.6.0 3317 * 3318 * @param WP_Post $post A post object. 3319 */ 3320 do_action( 'wp_edit_form_attachment_display', $post ); 3321 3322 endif; 3323 3324 ?> 3325 </div> 3326 <div class="wp_attachment_details edit-form-section"> 3327 <?php if ( str_starts_with( $post->post_mime_type, 'image' ) ) : ?> 3328 <p class="attachment-alt-text"> 3329 <label for="attachment_alt"><strong><?php _e( 'Alternative Text' ); ?></strong></label><br /> 3330 <textarea class="widefat" name="_wp_attachment_image_alt" id="attachment_alt" aria-describedby="alt-text-description"><?php echo esc_attr( $alt_text ); ?></textarea> 3331 </p> 3332 <p class="attachment-alt-text-description" id="alt-text-description"> 3333 <?php 3334 3335 printf( 3336 /* translators: 1: Link to tutorial, 2: Additional link attributes, 3: Accessibility text. */ 3337 __( '<a href="%1$s" %2$s>Learn how to describe the purpose of the image%3$s</a>. Leave empty if the image is purely decorative.' ), 3338 /* translators: Localized tutorial, if one exists. W3C Web Accessibility Initiative link has list of existing translations. */ 3339 esc_url( __( 'https://www.w3.org/WAI/tutorials/images/decision-tree/' ) ), 3340 'target="_blank"', 3341 sprintf( 3342 '<span class="screen-reader-text"> %s</span><span aria-hidden="true" class="dashicons dashicons-external"></span>', 3343 /* translators: Hidden accessibility text. */ 3344 __( '(opens in a new tab)' ) 3345 ) 3346 ); 3347 3348 ?> 3349 </p> 3350 <?php endif; ?> 3351 3352 <p> 3353 <label for="attachment_caption"><strong> 3354 <?php 3355 if ( wp_attachment_is( 'image', $post ) ) { 3356 esc_html_e( 'Image Caption' ); 3357 } else { 3358 esc_html_e( 'Short Description' ); 3359 } 3360 ?> 3361 </strong></label><br /> 3362 <textarea class="widefat" name="excerpt" id="attachment_caption"><?php echo $post->post_excerpt; ?></textarea> 3363 </p> 3364 3365 <?php 3366 3367 $quicktags_settings = array( 'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,close' ); 3368 $editor_args = array( 3369 'textarea_name' => 'content', 3370 'textarea_rows' => 5, 3371 'media_buttons' => false, 3372 /** 3373 * Filters the TinyMCE argument for the media description field on the attachment details screen. 3374 * 3375 * @since 6.6.0 3376 * 3377 * @param bool $tinymce Whether to activate TinyMCE in media description field. Default false. 3378 */ 3379 'tinymce' => apply_filters( 'activate_tinymce_for_media_description', false ), 3380 'quicktags' => $quicktags_settings, 3381 ); 3382 3383 ?> 3384 3385 <label for="attachment_content" class="attachment-content-description"><strong><?php _e( 'Description' ); ?></strong> 3386 <?php 3387 3388 if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) { 3389 echo ': ' . __( 'Displayed on attachment pages.' ); 3390 } 3391 3392 ?> 3393 </label> 3394 <?php wp_editor( format_to_edit( $post->post_content ), 'attachment_content', $editor_args ); ?> 3395 3396 </div> 3397 <?php 3398 3399 $extras = get_compat_media_markup( $post->ID ); 3400 echo $extras['item']; 3401 echo '<input type="hidden" id="image-edit-context" value="edit-attachment" />' . "\n"; 3402 } 3403 3404 /** 3405 * Displays non-editable attachment metadata in the publish meta box. 3406 * 3407 * @since 3.5.0 3408 */ 3409 function attachment_submitbox_metadata() { 3410 $post = get_post(); 3411 $attachment_id = $post->ID; 3412 3413 $file = get_attached_file( $attachment_id ); 3414 $filename = esc_html( wp_basename( $file ) ); 3415 3416 $media_dims = ''; 3417 $meta = wp_get_attachment_metadata( $attachment_id ); 3418 3419 if ( isset( $meta['width'], $meta['height'] ) ) { 3420 /* translators: 1: A number of pixels wide, 2: A number of pixels tall. */ 3421 $media_dims .= "<span id='media-dims-$attachment_id'>" . sprintf( __( '%1$s by %2$s pixels' ), $meta['width'], $meta['height'] ) . '</span>'; 3422 } 3423 /** This filter is documented in wp-admin/includes/media.php */ 3424 $media_dims = apply_filters( 'media_meta', $media_dims, $post ); 3425 3426 $att_url = wp_get_attachment_url( $attachment_id ); 3427 3428 $author = new WP_User( $post->post_author ); 3429 3430 $uploaded_by_name = __( '(no author)' ); 3431 $uploaded_by_link = ''; 3432 3433 if ( $author->exists() ) { 3434 $uploaded_by_name = $author->display_name ? $author->display_name : $author->nickname; 3435 $uploaded_by_link = get_edit_user_link( $author->ID ); 3436 } 3437 ?> 3438 <div class="misc-pub-section misc-pub-uploadedby word-wrap-break-word"> 3439 <?php if ( $uploaded_by_link ) { ?> 3440 <?php _e( 'Uploaded by:' ); ?> <a href="<?php echo $uploaded_by_link; ?>"><strong><?php echo $uploaded_by_name; ?></strong></a> 3441 <?php } else { ?> 3442 <?php _e( 'Uploaded by:' ); ?> <strong><?php echo $uploaded_by_name; ?></strong> 3443 <?php } ?> 3444 </div> 3445 3446 <?php 3447 if ( $post->post_parent ) { 3448 $post_parent = get_post( $post->post_parent ); 3449 if ( $post_parent && current_user_can( 'read_post', $post_parent->ID ) ) { 3450 $uploaded_to_title = $post_parent->post_title ? $post_parent->post_title : __( '(no title)' ); 3451 $uploaded_to_link = get_edit_post_link( $post->post_parent, 'raw' ); 3452 ?> 3453 <div class="misc-pub-section misc-pub-uploadedto"> 3454 <?php if ( $uploaded_to_link ) { ?> 3455 <?php _e( 'Uploaded to:' ); ?> <a href="<?php echo $uploaded_to_link; ?>"><strong><?php echo $uploaded_to_title; ?></strong></a> 3456 <?php } else { ?> 3457 <?php _e( 'Uploaded to:' ); ?> <strong><?php echo $uploaded_to_title; ?></strong> 3458 <?php } ?> 3459 </div> 3460 <?php 3461 } 3462 } 3463 ?> 3464 3465 <div class="misc-pub-section misc-pub-attachment"> 3466 <label for="attachment_url"><?php _e( 'File URL:' ); ?></label> 3467 <input type="text" class="widefat urlfield ltr" readonly="readonly" name="attachment_url" id="attachment_url" value="<?php echo esc_attr( $att_url ); ?>" /> 3468 <span class="copy-to-clipboard-container"> 3469 <button type="button" class="button copy-attachment-url edit-media" data-clipboard-target="#attachment_url"><?php _e( 'Copy URL to clipboard' ); ?></button> 3470 <span class="success hidden" aria-hidden="true"><?php _e( 'Copied!' ); ?></span> 3471 </span> 3472 </div> 3473 <div class="misc-pub-section misc-pub-download"> 3474 <a href="<?php echo esc_url( $att_url ); ?>" download><?php _e( 'Download file' ); ?></a> 3475 </div> 3476 <div class="misc-pub-section misc-pub-filename"> 3477 <?php _e( 'File name:' ); ?> <strong><?php echo $filename; ?></strong> 3478 </div> 3479 <div class="misc-pub-section misc-pub-filetype"> 3480 <?php _e( 'File type:' ); ?> 3481 <strong> 3482 <?php 3483 3484 if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) ) { 3485 echo esc_html( strtoupper( $matches[1] ) ); 3486 list( $mime_type ) = explode( '/', $post->post_mime_type ); 3487 if ( 'image' !== $mime_type && ! empty( $meta['mime_type'] ) ) { 3488 if ( "$mime_type/" . strtolower( $matches[1] ) !== $meta['mime_type'] ) { 3489 echo ' (' . $meta['mime_type'] . ')'; 3490 } 3491 } 3492 } else { 3493 echo strtoupper( str_replace( 'image/', '', $post->post_mime_type ) ); 3494 } 3495 3496 ?> 3497 </strong> 3498 </div> 3499 3500 <?php 3501 3502 $file_size = false; 3503 3504 if ( isset( $meta['filesize'] ) && is_numeric( $meta['filesize'] ) && (int) $meta['filesize'] > 0 ) { 3505 $file_size = (int) $meta['filesize']; 3506 } elseif ( is_string( $file ) && '' !== $file && is_readable( $file ) ) { 3507 $file_size = wp_filesize( $file ); 3508 } 3509 3510 if ( ! empty( $file_size ) ) { 3511 ?> 3512 <div class="misc-pub-section misc-pub-filesize"> 3513 <?php _e( 'File size:' ); ?> <strong><?php echo size_format( $file_size ); ?></strong> 3514 </div> 3515 <?php 3516 } 3517 3518 if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) { 3519 $fields = array( 3520 'length_formatted' => __( 'Length:' ), 3521 'bitrate' => __( 'Bitrate:' ), 3522 ); 3523 3524 /** 3525 * Filters the audio and video metadata fields to be shown in the publish meta box. 3526 * 3527 * The key for each item in the array should correspond to an attachment 3528 * metadata key, and the value should be the desired label. 3529 * 3530 * @since 3.7.0 3531 * @since 4.9.0 Added the `$post` parameter. 3532 * 3533 * @param array $fields An array of the attachment metadata keys and labels. 3534 * @param WP_Post $post WP_Post object for the current attachment. 3535 */ 3536 $fields = apply_filters( 'media_submitbox_misc_sections', $fields, $post ); 3537 3538 foreach ( $fields as $key => $label ) { 3539 if ( empty( $meta[ $key ] ) ) { 3540 continue; 3541 } 3542 3543 ?> 3544 <div class="misc-pub-section misc-pub-mime-meta misc-pub-<?php echo sanitize_html_class( $key ); ?>"> 3545 <?php echo $label; ?> 3546 <strong> 3547 <?php 3548 3549 switch ( $key ) { 3550 case 'bitrate': 3551 echo round( $meta['bitrate'] / 1000 ) . 'kb/s'; 3552 if ( ! empty( $meta['bitrate_mode'] ) ) { 3553 echo ' ' . strtoupper( esc_html( $meta['bitrate_mode'] ) ); 3554 } 3555 break; 3556 case 'length_formatted': 3557 echo human_readable_duration( $meta['length_formatted'] ); 3558 break; 3559 default: 3560 echo esc_html( $meta[ $key ] ); 3561 break; 3562 } 3563 3564 ?> 3565 </strong> 3566 </div> 3567 <?php 3568 } 3569 3570 $fields = array( 3571 'dataformat' => __( 'Audio Format:' ), 3572 'codec' => __( 'Audio Codec:' ), 3573 ); 3574 3575 /** 3576 * Filters the audio attachment metadata fields to be shown in the publish meta box. 3577 * 3578 * The key for each item in the array should correspond to an attachment 3579 * metadata key, and the value should be the desired label. 3580 * 3581 * @since 3.7.0 3582 * @since 4.9.0 Added the `$post` parameter. 3583 * 3584 * @param array $fields An array of the attachment metadata keys and labels. 3585 * @param WP_Post $post WP_Post object for the current attachment. 3586 */ 3587 $audio_fields = apply_filters( 'audio_submitbox_misc_sections', $fields, $post ); 3588 3589 foreach ( $audio_fields as $key => $label ) { 3590 if ( empty( $meta['audio'][ $key ] ) ) { 3591 continue; 3592 } 3593 3594 ?> 3595 <div class="misc-pub-section misc-pub-audio misc-pub-<?php echo sanitize_html_class( $key ); ?>"> 3596 <?php echo $label; ?> <strong><?php echo esc_html( $meta['audio'][ $key ] ); ?></strong> 3597 </div> 3598 <?php 3599 } 3600 } 3601 3602 if ( $media_dims ) { 3603 ?> 3604 <div class="misc-pub-section misc-pub-dimensions"> 3605 <?php _e( 'Dimensions:' ); ?> <strong><?php echo $media_dims; ?></strong> 3606 </div> 3607 <?php 3608 } 3609 3610 if ( ! empty( $meta['original_image'] ) ) { 3611 ?> 3612 <div class="misc-pub-section misc-pub-original-image word-wrap-break-word"> 3613 <?php _e( 'Original image:' ); ?> 3614 <a href="<?php echo esc_url( wp_get_original_image_url( $attachment_id ) ); ?>"> 3615 <strong><?php echo esc_html( wp_basename( wp_get_original_image_path( $attachment_id ) ) ); ?></strong> 3616 </a> 3617 </div> 3618 <?php 3619 } 3620 } 3621 3622 /** 3623 * Parses ID3v2, ID3v1, and getID3 comments to extract usable data. 3624 * 3625 * @since 3.6.0 3626 * 3627 * @param array $metadata An existing array with data. 3628 * @param array $data Data supplied by ID3 tags. 3629 */ 3630 function wp_add_id3_tag_data( &$metadata, $data ) { 3631 foreach ( array( 'id3v2', 'id3v1' ) as $version ) { 3632 if ( ! empty( $data[ $version ]['comments'] ) ) { 3633 foreach ( $data[ $version ]['comments'] as $key => $list ) { 3634 if ( 'length' !== $key && ! empty( $list ) ) { 3635 $metadata[ $key ] = is_array( $list ) ? wp_kses_post_deep( reset( $list ) ) : wp_kses_post( $list ); 3636 // Fix bug in byte stream analysis. 3637 if ( 'terms_of_use' === $key && str_starts_with( $metadata[ $key ], 'yright notice.' ) ) { 3638 $metadata[ $key ] = 'Cop' . $metadata[ $key ]; 3639 } 3640 } 3641 } 3642 break; 3643 } 3644 } 3645 3646 if ( ! empty( $data['id3v2']['APIC'] ) ) { 3647 $image = reset( $data['id3v2']['APIC'] ); 3648 if ( ! empty( $image['data'] ) ) { 3649 $metadata['image'] = array( 3650 'data' => $image['data'], 3651 'mime' => $image['image_mime'], 3652 'width' => $image['image_width'], 3653 'height' => $image['image_height'], 3654 ); 3655 } 3656 } elseif ( ! empty( $data['comments']['picture'] ) ) { 3657 $image = reset( $data['comments']['picture'] ); 3658 if ( ! empty( $image['data'] ) ) { 3659 $metadata['image'] = array( 3660 'data' => $image['data'], 3661 'mime' => $image['image_mime'], 3662 ); 3663 } 3664 } 3665 } 3666 3667 /** 3668 * Retrieves metadata from a video file's ID3 tags. 3669 * 3670 * @since 3.6.0 3671 * 3672 * @param string $file Path to file. 3673 * @return array|false Returns array of metadata, if found. 3674 */ 3675 function wp_read_video_metadata( $file ) { 3676 if ( ! file_exists( $file ) ) { 3677 return false; 3678 } 3679 3680 $metadata = array(); 3681 3682 if ( ! defined( 'GETID3_TEMP_DIR' ) ) { 3683 define( 'GETID3_TEMP_DIR', get_temp_dir() ); 3684 } 3685 3686 if ( ! class_exists( 'getID3', false ) ) { 3687 require ABSPATH . WPINC . '/ID3/getid3.php'; 3688 } 3689 3690 $id3 = new getID3(); 3691 // Required to get the `created_timestamp` value. 3692 $id3->options_audiovideo_quicktime_ReturnAtomData = true; // phpcs:ignore WordPress.NamingConventions.ValidVariableName 3693 3694 $data = $id3->analyze( $file ); 3695 3696 if ( isset( $data['video']['lossless'] ) ) { 3697 $metadata['lossless'] = $data['video']['lossless']; 3698 } 3699 3700 if ( ! empty( $data['video']['bitrate'] ) ) { 3701 $metadata['bitrate'] = (int) $data['video']['bitrate']; 3702 } 3703 3704 if ( ! empty( $data['video']['bitrate_mode'] ) ) { 3705 $metadata['bitrate_mode'] = $data['video']['bitrate_mode']; 3706 } 3707 3708 if ( ! empty( $data['filesize'] ) ) { 3709 $metadata['filesize'] = (int) $data['filesize']; 3710 } 3711 3712 if ( ! empty( $data['mime_type'] ) ) { 3713 $metadata['mime_type'] = $data['mime_type']; 3714 } 3715 3716 if ( ! empty( $data['playtime_seconds'] ) ) { 3717 $metadata['length'] = (int) round( $data['playtime_seconds'] ); 3718 } 3719 3720 if ( ! empty( $data['playtime_string'] ) ) { 3721 $metadata['length_formatted'] = $data['playtime_string']; 3722 } 3723 3724 if ( ! empty( $data['video']['resolution_x'] ) ) { 3725 $metadata['width'] = (int) $data['video']['resolution_x']; 3726 } 3727 3728 if ( ! empty( $data['video']['resolution_y'] ) ) { 3729 $metadata['height'] = (int) $data['video']['resolution_y']; 3730 } 3731 3732 if ( ! empty( $data['fileformat'] ) ) { 3733 $metadata['fileformat'] = $data['fileformat']; 3734 } 3735 3736 if ( ! empty( $data['video']['dataformat'] ) ) { 3737 $metadata['dataformat'] = $data['video']['dataformat']; 3738 } 3739 3740 if ( ! empty( $data['video']['encoder'] ) ) { 3741 $metadata['encoder'] = $data['video']['encoder']; 3742 } 3743 3744 if ( ! empty( $data['video']['codec'] ) ) { 3745 $metadata['codec'] = $data['video']['codec']; 3746 } 3747 3748 if ( ! empty( $data['audio'] ) ) { 3749 unset( $data['audio']['streams'] ); 3750 $metadata['audio'] = $data['audio']; 3751 } 3752 3753 if ( empty( $metadata['created_timestamp'] ) ) { 3754 $created_timestamp = wp_get_media_creation_timestamp( $data ); 3755 3756 if ( false !== $created_timestamp ) { 3757 $metadata['created_timestamp'] = $created_timestamp; 3758 } 3759 } 3760 3761 wp_add_id3_tag_data( $metadata, $data ); 3762 3763 $file_format = $metadata['fileformat'] ?? null; 3764 3765 /** 3766 * Filters the array of metadata retrieved from a video. 3767 * 3768 * In core, usually this selection is what is stored. 3769 * More complete data can be parsed from the `$data` parameter. 3770 * 3771 * @since 4.9.0 3772 * 3773 * @param array $metadata Filtered video metadata. 3774 * @param string $file Path to video file. 3775 * @param string|null $file_format File format of video, as analyzed by getID3. 3776 * Null if unknown. 3777 * @param array $data Raw metadata from getID3. 3778 */ 3779 return apply_filters( 'wp_read_video_metadata', $metadata, $file, $file_format, $data ); 3780 } 3781 3782 /** 3783 * Retrieves metadata from an audio file's ID3 tags. 3784 * 3785 * @since 3.6.0 3786 * 3787 * @param string $file Path to file. 3788 * @return array|false Returns array of metadata, if found. 3789 */ 3790 function wp_read_audio_metadata( $file ) { 3791 if ( ! file_exists( $file ) ) { 3792 return false; 3793 } 3794 3795 $metadata = array(); 3796 3797 if ( ! defined( 'GETID3_TEMP_DIR' ) ) { 3798 define( 'GETID3_TEMP_DIR', get_temp_dir() ); 3799 } 3800 3801 if ( ! class_exists( 'getID3', false ) ) { 3802 require ABSPATH . WPINC . '/ID3/getid3.php'; 3803 } 3804 3805 $id3 = new getID3(); 3806 // Required to get the `created_timestamp` value. 3807 $id3->options_audiovideo_quicktime_ReturnAtomData = true; // phpcs:ignore WordPress.NamingConventions.ValidVariableName 3808 3809 $data = $id3->analyze( $file ); 3810 3811 if ( ! empty( $data['audio'] ) ) { 3812 unset( $data['audio']['streams'] ); 3813 $metadata = $data['audio']; 3814 } 3815 3816 if ( ! empty( $data['fileformat'] ) ) { 3817 $metadata['fileformat'] = $data['fileformat']; 3818 } 3819 3820 if ( ! empty( $data['filesize'] ) ) { 3821 $metadata['filesize'] = (int) $data['filesize']; 3822 } 3823 3824 if ( ! empty( $data['mime_type'] ) ) { 3825 $metadata['mime_type'] = $data['mime_type']; 3826 } 3827 3828 if ( ! empty( $data['playtime_seconds'] ) ) { 3829 $metadata['length'] = (int) round( $data['playtime_seconds'] ); 3830 } 3831 3832 if ( ! empty( $data['playtime_string'] ) ) { 3833 $metadata['length_formatted'] = $data['playtime_string']; 3834 } 3835 3836 if ( empty( $metadata['created_timestamp'] ) ) { 3837 $created_timestamp = wp_get_media_creation_timestamp( $data ); 3838 3839 if ( false !== $created_timestamp ) { 3840 $metadata['created_timestamp'] = $created_timestamp; 3841 } 3842 } 3843 3844 wp_add_id3_tag_data( $metadata, $data ); 3845 3846 $file_format = $metadata['fileformat'] ?? null; 3847 3848 /** 3849 * Filters the array of metadata retrieved from an audio file. 3850 * 3851 * In core, usually this selection is what is stored. 3852 * More complete data can be parsed from the `$data` parameter. 3853 * 3854 * @since 6.1.0 3855 * 3856 * @param array $metadata Filtered audio metadata. 3857 * @param string $file Path to audio file. 3858 * @param string|null $file_format File format of audio, as analyzed by getID3. 3859 * Null if unknown. 3860 * @param array $data Raw metadata from getID3. 3861 */ 3862 return apply_filters( 'wp_read_audio_metadata', $metadata, $file, $file_format, $data ); 3863 } 3864 3865 /** 3866 * Parses creation date from media metadata. 3867 * 3868 * The getID3 library doesn't have a standard method for getting creation dates, 3869 * so the location of this data can vary based on the MIME type. 3870 * 3871 * @since 4.9.0 3872 * 3873 * @link https://github.com/JamesHeinrich/getID3/blob/master/structure.txt 3874 * 3875 * @param array $metadata The metadata returned by getID3::analyze(). 3876 * @return int|false A Unix timestamp for the media's creation date if available 3877 * or a boolean false if the timestamp could not be determined. 3878 */ 3879 function wp_get_media_creation_timestamp( $metadata ) { 3880 $creation_date = false; 3881 3882 if ( empty( $metadata['fileformat'] ) ) { 3883 return $creation_date; 3884 } 3885 3886 switch ( $metadata['fileformat'] ) { 3887 case 'asf': 3888 if ( isset( $metadata['asf']['file_properties_object']['creation_date_unix'] ) ) { 3889 $creation_date = (int) $metadata['asf']['file_properties_object']['creation_date_unix']; 3890 } 3891 break; 3892 3893 case 'matroska': 3894 case 'webm': 3895 if ( isset( $metadata['matroska']['comments']['creation_time'][0] ) ) { 3896 $creation_date = strtotime( $metadata['matroska']['comments']['creation_time'][0] ); 3897 } elseif ( isset( $metadata['matroska']['info'][0]['DateUTC_unix'] ) ) { 3898 $creation_date = (int) $metadata['matroska']['info'][0]['DateUTC_unix']; 3899 } 3900 break; 3901 3902 case 'quicktime': 3903 case 'mp4': 3904 if ( isset( $metadata['quicktime']['moov']['subatoms'][0]['creation_time_unix'] ) ) { 3905 $creation_date = (int) $metadata['quicktime']['moov']['subatoms'][0]['creation_time_unix']; 3906 } 3907 break; 3908 } 3909 3910 return $creation_date; 3911 } 3912 3913 /** 3914 * Encapsulates the logic for Attach/Detach actions. 3915 * 3916 * @since 4.2.0 3917 * 3918 * @global wpdb $wpdb WordPress database abstraction object. 3919 * 3920 * @param int $parent_id Attachment parent ID. 3921 * @param string $action Optional. Attach/detach action. Accepts 'attach' or 'detach'. 3922 * Default 'attach'. 3923 */ 3924 function wp_media_attach_action( $parent_id, $action = 'attach' ) { 3925 global $wpdb; 3926 3927 if ( ! $parent_id ) { 3928 return; 3929 } 3930 3931 if ( ! current_user_can( 'edit_post', $parent_id ) ) { 3932 wp_die( __( 'Sorry, you are not allowed to edit this post.' ) ); 3933 } 3934 3935 $ids = array(); 3936 3937 foreach ( (array) $_REQUEST['media'] as $attachment_id ) { 3938 $attachment_id = (int) $attachment_id; 3939 3940 if ( ! current_user_can( 'edit_post', $attachment_id ) ) { 3941 continue; 3942 } 3943 3944 $ids[] = $attachment_id; 3945 } 3946 3947 if ( ! empty( $ids ) ) { 3948 $ids_string = implode( ',', $ids ); 3949 3950 if ( 'attach' === $action ) { 3951 $result = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ( $ids_string )", $parent_id ) ); 3952 } else { 3953 $result = $wpdb->query( "UPDATE $wpdb->posts SET post_parent = 0 WHERE post_type = 'attachment' AND ID IN ( $ids_string )" ); 3954 } 3955 } 3956 3957 if ( isset( $result ) ) { 3958 foreach ( $ids as $attachment_id ) { 3959 /** 3960 * Fires when media is attached or detached from a post. 3961 * 3962 * @since 5.5.0 3963 * 3964 * @param string $action Attach/detach action. Accepts 'attach' or 'detach'. 3965 * @param int $attachment_id The attachment ID. 3966 * @param int $parent_id Attachment parent ID. 3967 */ 3968 do_action( 'wp_media_attach_action', $action, $attachment_id, $parent_id ); 3969 3970 clean_attachment_cache( $attachment_id ); 3971 } 3972 3973 $location = 'upload.php'; 3974 $referer = wp_get_referer(); 3975 3976 if ( $referer ) { 3977 if ( str_contains( $referer, 'upload.php' ) ) { 3978 $location = remove_query_arg( array( 'attached', 'detach' ), $referer ); 3979 } 3980 } 3981 3982 $key = 'attach' === $action ? 'attached' : 'detach'; 3983 $location = add_query_arg( array( $key => $result ), $location ); 3984 3985 wp_redirect( $location ); 3986 exit; 3987 } 3988 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sun Oct 11 08:20:32 2026 | Cross-referenced by PHPXref |