Make WordPress Core

Ticket #3499: xmlrpc-attachment.diff

File xmlrpc-attachment.diff, 7.1 KB (added by ruckus, 19 years ago)
  • wp-includes/functions-post.php

     
    386386        return $post;
    387387}
    388388
     389function wp_get_attachment_metadata( $post_id, $unfiltered = false ) {
     390        $post_id = (int) $post_id;
     391
     392        $data = get_post_meta( $post_id, '_wp_attachment_metadata', true );
     393        if ( $unfiltered )
     394                return $data;
     395        return apply_filters( 'wp_get_attachment_metadata', $data, $post_id );
     396}
     397
     398function wp_update_attachment_metadata( $post_id, $data ) {
     399        if ( !get_post( $post_id ) )
     400                return false;
     401
     402        $old_data = wp_get_attachment_metadata( $post_id, true );
     403
     404        $data = apply_filters( 'wp_update_attachment_metadata', $data, $post_id );
     405
     406        if ( $old_data )
     407                return update_post_meta( $post_id, '_wp_attachment_metadata', $data, $old_data );
     408        else
     409                return add_post_meta( $post_id, '_wp_attachment_metadata', $data );
     410}
     411
    389412function wp_get_single_post($postid = 0, $mode = OBJECT) {
    390413        global $wpdb;
    391414
  • xmlrpc.php

     
    3232exit;
    3333}
    3434
     35include_once(ABSPATH . 'wp-admin/admin-functions.php');
    3536include_once(ABSPATH . WPINC . '/class-IXR.php');
    3637
    3738// Turn off all warnings and errors.
     
    396397            return new IXR_Error(500, 'Sorry, your entry could not be posted. Something wrong happened.');
    397398          }
    398399
     400          $this->attach_uploads( $post_ID, $post_content );
     401
    399402          logIO('O', "Posted ! ID: $post_ID");
    400403
    401404          return $post_ID;
     
    445448                return new IXR_Error(500, 'For some strange yet very annoying reason, this post could not be edited.');
    446449          }
    447450
     451          $this->attach_uploads( $ID, $post_content );
     452
    448453          return true;
    449454        }
    450455
     
    564569            return new IXR_Error(500, 'Sorry, your entry could not be posted. Something wrong happened.');
    565570          }
    566571
     572          $this->attach_uploads( $post_ID, $post_content );
     573
    567574          logIO('O', "Posted ! ID: $post_ID");
    568575
    569576          return strval($post_ID);
    570577        }
    571578
    572 
    573579        /* metaweblog.editPost ...edits a post */
    574580        function mw_editPost($args) {
    575581
     
    644650            return new IXR_Error(500, 'Sorry, your entry could not be edited. Something wrong happened.');
    645651          }
    646652
     653          $this->attach_uploads( $ID, $post_content );
     654
    647655          logIO('O',"(MW) Edited ! ID: $post_ID");
    648656
    649657          return true;
     
    833841                        return $this->error;
    834842                }
    835843
     844                if ( $upload_err = apply_filters( "pre_upload_error", false ) )
     845                        return new IXR_Error(500, $upload_err);
     846
    836847                $upload = wp_upload_bits($name, $type, $bits);
    837848                if ( ! empty($upload['error']) ) {
    838849                        logIO('O', '(MW) Could not write file '.$name);
    839850                        return new IXR_Error(500, 'Could not write file '.$name);
    840851                }
    841                
    842                 return array('url' => $upload['url']);
     852                // Construct the attachment array
     853                // attach to post_id -1
     854                $post_id = -1;
     855                $attachment = array(
     856                        'post_title' => $name,
     857                        'post_content' => '',
     858                        'post_status' => 'attachment',
     859                        'post_parent' => $post_id,
     860                        'post_mime_type' => $type,
     861                        'guid' => $upload[ 'url' ]
     862                );
     863                // Save the data
     864                $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id );
     865                wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
     866
     867                return apply_filters( 'wp_handle_upload', array( 'file' => $upload[ 'file' ], 'url' => $upload[ 'url' ], 'type' => $type ) );
    843868        }
    844869
     870        function attach_uploads( $post_ID, $post_content ) {
     871          global $wpdb;
     872          // find any unattached files
     873          $attachments = $wpdb->get_results( "SELECT ID, guid FROM {$wpdb->posts} WHERE post_parent = '-1' AND post_status = 'attachment'" );
     874          if( is_array( $attachments ) ) {
     875            foreach( $attachments as $file ) {
     876              if( strpos( $post_content, $file->guid ) !== false ) {
     877                $wpdb->query( "UPDATE {$wpdb->posts} SET post_parent = '$post_ID' WHERE ID = '{$file->ID}'" );
     878              }
     879            }
     880          }
     881        }
    845882
    846883        /* MovableType API functions
    847884         * specs on http://www.movabletype.org/docs/mtmanual_programmatic.html
     
    12151252
    12161253                $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_content', 'comment_type');
    12171254
    1218                 wp_new_comment($commentdata);
    1219                 do_action('pingback_post', $wpdb->insert_id);
     1255                $comment_ID = wp_new_comment($commentdata);
     1256                do_action('pingback_post', $comment_ID);
    12201257               
    12211258                return "Pingback from $pagelinkedfrom to $pagelinkedto registered. Keep the web talking! :-)";
    12221259        }
  • wp-admin/inline-uploading.php

     
    7474
    7575// Save the data
    7676$id = wp_insert_attachment($attachment, $file, $post);
     77wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    7778
    78 if ( preg_match('!^image/!', $attachment['post_mime_type']) ) {
    79         // Generate the attachment's postmeta.
    80         $imagesize = getimagesize($file);
    81         $imagedata['width'] = $imagesize['0'];
    82         $imagedata['height'] = $imagesize['1'];
    83         list($uwidth, $uheight) = get_udims($imagedata['width'], $imagedata['height']);
    84         $imagedata['hwstring_small'] = "height='$uheight' width='$uwidth'";
    85         $imagedata['file'] = $file;
    86 
    87         add_post_meta($id, '_wp_attachment_metadata', $imagedata);
    88 
    89         if ( $imagedata['width'] * $imagedata['height'] < 3 * 1024 * 1024 ) {
    90                 if ( $imagedata['width'] > 128 && $imagedata['width'] >= $imagedata['height'] * 4 / 3 )
    91                         $thumb = wp_create_thumbnail($file, 128);
    92                 elseif ( $imagedata['height'] > 96 )
    93                         $thumb = wp_create_thumbnail($file, 96);
    94 
    95                 if ( @file_exists($thumb) ) {
    96                         $newdata = $imagedata;
    97                         $newdata['thumb'] = basename($thumb);
    98                         update_post_meta($id, '_wp_attachment_metadata', $newdata, $imagedata);
    99                 } else {
    100                         $error = $thumb;
    101                 }
    102         }
    103 } else {
    104         add_post_meta($id, '_wp_attachment_metadata', array());
    105 }
    106 
    10779wp_redirect(basename(__FILE__) . "?post=$post&all=$all&action=view&start=0");
    10880die();
    10981
  • wp-admin/admin-functions.php

     
    18881888                return array((int) ($width / $height * 96), 96);
    18891889}
    18901890
     1891function wp_generate_attachment_metadata( $attachment_id, $file ) {
     1892        $attachment = get_post( $attachment_id );
     1893
     1894        $metadata = array();
     1895        if ( preg_match('!^image/!', get_post_mime_type( $attachment )) ) {
     1896                $imagesize = getimagesize($file);
     1897                $metadata['width'] = $imagesize['0'];
     1898                $metadata['height'] = $imagesize['1'];
     1899                list($uwidth, $uheight) = get_udims($metadata['width'], $metadata['height']);
     1900                $metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
     1901                $metadata['file'] = $file;
     1902
     1903                if ( $metadata['width'] * $metadata['height'] < 3 * 1024 * 1024 ) {
     1904                        if ( $metadata['width'] > 128 && $metadata['width'] >= $metadata['height'] * 4 / 3 )
     1905                                $thumb = wp_create_thumbnail($file, 128);
     1906                        elseif ( $metadata['height'] > 96 )
     1907                                $thumb = wp_create_thumbnail($file, 96);
     1908
     1909                        if ( @file_exists($thumb) )
     1910                                $metadata['thumb'] = basename($thumb);
     1911                }
     1912        }
     1913        return apply_filters( 'wp_generate_attachment_metadata', $metadata );
     1914}
     1915
    18911916?>