WordPress.org

Make WordPress Core

Ticket #21329: 21329.3.diff

File 21329.3.diff, 8.2 KB (added by martythornley, 6 years ago)

Added some function descriptions to help clarify

  • wp-admin/includes/media.php

     
    77 */
    88
    99/**
    10  * {@internal Missing Short Description}}
     10 * Defines media tabs to be used in thickbox
    1111 *
    1212 * @since 2.5.0
    1313 *
    14  * @return unknown
     14 * @uses apply_filters() calls media_upload_tabs
     15 *
     16 * @return an associative array of tab names in the form of type=>name
    1517 */
    1618function media_upload_tabs() {
    1719        $_default_tabs = array(
     
    2527}
    2628
    2729/**
    28  * {@internal Missing Short Description}}
     30 * Added to media_upload_tabs filter to define the gallery tab
     31 * checks for attachments and removes gallery tab if the current post has none.
    2932 *
    3033 * @since 2.5.0
    3134 *
    32  * @param unknown_type $tabs
    33  * @return unknown
     35 * @param array $tabs
     36 * @return an associative array of tab names in the form of type=>name
    3437 */
    3538function update_gallery_tab($tabs) {
    3639        global $wpdb;
     
    5760add_filter('media_upload_tabs', 'update_gallery_tab');
    5861
    5962/**
    60  * {@internal Missing Short Description}}
     63 * Echoes the thickbox media upload tabs to the screen
    6164 *
    6265 * @since 2.5.0
    6366 */
     
    6568        global $redir_tab;
    6669        $tabs = media_upload_tabs();
    6770        $default = 'type';
    68 
     71        $html = '';
     72       
    6973        if ( !empty($tabs) ) {
    70                 echo "<ul id='sidemenu'>\n";
     74                $html = "<ul id='sidemenu'>\n";
    7175                if ( isset($redir_tab) && array_key_exists($redir_tab, $tabs) )
    7276                        $current = $redir_tab;
    7377                elseif ( isset($_GET['tab']) && array_key_exists($_GET['tab'], $tabs) )
     
    8387
    8488                        $href = add_query_arg(array('tab' => $callback, 's' => false, 'paged' => false, 'post_mime_type' => false, 'm' => false));
    8589                        $link = "<a href='" . esc_url($href) . "'$class>$text</a>";
    86                         echo "\t<li id='" . esc_attr("tab-$callback") . "'>$link</li>\n";
     90                        $html .= "\t<li id='" . esc_attr("tab-$callback") . "'>$link</li>\n";
    8791                }
    88                 echo "</ul>\n";
     92                $html .= "</ul>\n";
    8993        }
     94        echo $html;
    9095}
    9196
    9297/**
    93  * {@internal Missing Short Description}}
     98 * Creates the html for an image as it is added to the editor
    9499 *
    95100 * @since 2.5.0
    96101 *
     102 * @uses apply_filters() calls image_send_to_editor
    97103 * @param unknown_type $id
    98104 * @param unknown_type $alt
    99105 * @param unknown_type $title
     
    101107 * @param unknown_type $url
    102108 * @param unknown_type $rel
    103109 * @param unknown_type $size
    104  * @return unknown
     110 * @return string - the html for an image tag
    105111 */
    106112function get_image_send_to_editor($id, $caption, $title, $align, $url='', $rel = false, $size='medium', $alt = '') {
    107113
     
    118124}
    119125
    120126/**
    121  * {@internal Missing Short Description}}
     127 * Wraps an image in the caption code
    122128 *
    123129 * @since 2.6.0
    124130 *
     
    170176}
    171177
    172178/**
    173  * {@internal Missing Short Description}}
     179 * Echoes the javascript used to send html back to the editor
    174180 *
    175181 * @since 2.5.0
    176182 *
    177  * @param unknown_type $html
     183 * @param string $html
    178184 */
    179185function media_send_to_editor($html) {
    180186?>
     
    189195}
    190196
    191197/**
    192  * {@internal Missing Short Description}}
    193  *
    194198 * This handles the file upload POST itself, creating the attachment post.
    195199 *
    196200 * @since 2.5.0
     
    260264 *
    261265 * @since 2.6.0
    262266 *
     267 * @uses wp_handle_sideload
     268 * @uses wp_read_image_metadata
     269 * @uses wp_insert_attachment
     270 * @uses wp_update_attachment_metadata
     271 * @uses wp_generate_attachment_metadata
    263272 * @param array $file_array Array similar to a {@link $_FILES} upload array
    264273 * @param int $post_id The post ID the media is associated with
    265274 * @param string $desc Description of the sideloaded file
     
    312321}
    313322
    314323/**
    315  * {@internal Missing Short Description}}
     324 * Generates html for the iframe used in the thickbox window for uploads
    316325 *
    317326 * Wrap iframe content (produced by $content_func) in a doctype, html head/body
    318327 * etc any additional function args will be passed to content_func.
     
    373382}
    374383
    375384/**
    376  * {@internal Missing Short Description}}
     385 * Echoes the html for media upload buttons
     386 * added to the action call for 'media_buttons'
    377387 *
    378388 * @since 2.5.0
    379389 */
     
    386396}
    387397add_action( 'media_buttons', 'media_buttons' );
    388398
     399/**
     400 * Determine url of the upload thickbox to be used in the media_buttons() link
     401 *
     402 * @uses apply_filters() calls $type . '_upload_iframe_src'
     403 * @param string $type
     404 * @param int $post_id
     405 * @param string $tab
     406 * @since unknown
     407 */
    389408function get_upload_iframe_src( $type = null, $post_id = null, $tab = null ) {
    390409        global $post_ID;
    391410
     
    625644}
    626645
    627646/**
    628  * {@internal Missing Short Description}}
     647 * Download an image from the specified URL and attach it to a post.
    629648 *
    630  * @since 2.5.0
     649 * @since 2.6.0
    631650 *
    632  * @return unknown
     651 * @param string $file The URL of the image to download
     652 * @param int $post_id The post ID the media is to be associated with
     653 * @param string $desc Optional. Description of the image
     654 * @return string|WP_Error Populated HTML img tag on success
    633655 */
    634 function media_upload_gallery() {
     656function media_sideload_image($file, $post_id, $desc = null) {
     657        if ( ! empty($file) ) {
     658                // Download file to temp location
     659                $tmp = download_url( $file );
     660
     661                // Set variables for storage
     662                // fix file filename for query strings
     663                preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
     664                $file_array['name'] = basename($matches[0]);
     665                $file_array['tmp_name'] = $tmp;
     666
     667                // If error storing temporarily, unlink
     668                if ( is_wp_error( $tmp ) ) {
     669                        @unlink($file_array['tmp_name']);
     670                        $file_array['tmp_name'] = '';
     671                }
     672
     673                // do the validation and storage stuff
     674                $id = media_handle_sideload( $file_array, $post_id, $desc );
     675                // If error storing permanently, unlink
     676                if ( is_wp_error($id) ) {
     677                        @unlink($file_array['tmp_name']);
     678                        return $id;
     679                }
     680
     681                $src = wp_get_attachment_url( $id );
     682        }
     683
     684        // Finally check to make sure the file has been saved, then return the html
     685        if ( ! empty($src) ) {
     686                $alt = isset($desc) ? esc_attr($desc) : '';
     687                $html = "<img src='$src' alt='$alt' />";
     688                return $html;
     689        }
     690}
     691
     692/**
     693 * Returns wp_iframe with the proper upload form for media uploads
     694 * added to the media_upload filter in media-upload.php
     695 *
     696 * @since 3.5.0
     697 *
     698 * @return HTML from the wp_iframe function
     699 */
     700function media_upload() {
    635701        $errors = array();
    636 
    637         if ( !empty($_POST) ) {
     702        $current_filter = current_filter();
     703        $type = str_replace( 'media_upload_' , '' , $current_filter );
     704       
     705        if ( !empty($_POST) || empty($type) ) {
    638706                $return = media_upload_form_handler();
    639 
    640707                if ( is_string($return) )
    641708                        return $return;
    642709                if ( is_array($return) )
     
    644711        }
    645712
    646713        wp_enqueue_script('admin-gallery');
    647         return wp_iframe( 'media_upload_gallery_form', $errors );
     714       
     715        switch($type) {
     716                case 'library' :
     717                                $return = 'media_upload_library_form';
     718                        break;
     719                case 'gallery' :
     720                                $return = 'media_upload_gallery_form';
     721                        break;
     722                default :
     723                                $return = 'wp_media_upload_handler';
     724                        break;                         
     725        }
     726        return wp_iframe( $return , $errors );
    648727}
    649728
    650729/**
     
    654733 *
    655734 * @return unknown
    656735 */
     736function media_upload_gallery() {
     737        media_upload( 'gallery' );
     738}
     739
     740/**
     741 * {@internal Missing Short Description}}
     742 *
     743 * @since 2.5.0
     744 *
     745 * @return unknown
     746 */
    657747function media_upload_library() {
    658         $errors = array();
    659         if ( !empty($_POST) ) {
    660                 $return = media_upload_form_handler();
    661 
    662                 if ( is_string($return) )
    663                         return $return;
    664                 if ( is_array($return) )
    665                         $errors = $return;
    666         }
    667 
    668         return wp_iframe( 'media_upload_library_form', $errors );
     748        media_upload( 'library' );
    669749}
    670750
    671751/**
     
    21002180add_filter( 'async_upload_video', 'get_media_item', 10, 2 );
    21012181add_filter( 'async_upload_file',  'get_media_item', 10, 2 );
    21022182
    2103 add_action( 'media_upload_image', 'wp_media_upload_handler' );
    2104 add_action( 'media_upload_audio', 'wp_media_upload_handler' );
    2105 add_action( 'media_upload_video', 'wp_media_upload_handler' );
    2106 add_action( 'media_upload_file',  'wp_media_upload_handler' );
     2183add_action( 'media_upload_image', 'media_upload' );
     2184add_action( 'media_upload_audio', 'media_upload' );
     2185add_action( 'media_upload_video', 'media_upload' );
     2186add_action( 'media_upload_file',  'media_upload' );
    21072187
    2108 add_filter( 'media_upload_gallery', 'media_upload_gallery' );
    2109 add_filter( 'media_upload_library', 'media_upload_library' );
     2188add_filter( 'media_upload_gallery', 'media_upload' );
     2189add_filter( 'media_upload_library', 'media_upload' );