Created
June 6, 2013 11:27
-
-
Save koke/5720862 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function xtu_xmlrpc_methods($methods) { | |
$methods['wp.testUpload'] = 'xtu_test_upload'; | |
return $methods; | |
} | |
function xtu_test_upload($args) { | |
global $wpdb, $wp_xmlrpc_server; | |
$blog_ID = (int) $args[0]; | |
$username = $wpdb->escape($args[1]); | |
$password = $wpdb->escape($args[2]); | |
$data = $args[3]; | |
$name = sanitize_file_name( $data['name'] ); | |
$type = $data['type']; | |
$bits = $data['bits']; | |
if ( !$user = $wp_xmlrpc_server->login($username, $password) ) | |
return $wp_xmlrpc_server->error; | |
do_action('xmlrpc_call', 'metaWeblog.newMediaObject'); | |
if ( !current_user_can('upload_files') ) { | |
$wp_xmlrpc_server->error = new IXR_Error( 401, __( 'You do not have permission to upload files.' ) ); | |
return $wp_xmlrpc_server->error; | |
} | |
if ( $upload_err = apply_filters( 'pre_upload_error', false ) ) | |
return new IXR_Error(500, $upload_err); | |
if ( !empty($data['overwrite']) && ($data['overwrite'] == true) ) { | |
// Get postmeta info on the object. | |
$old_file = $wpdb->get_row(" | |
SELECT ID | |
FROM {$wpdb->posts} | |
WHERE post_title = '{$name}' | |
AND post_type = 'attachment' | |
"); | |
// Delete previous file. | |
wp_delete_attachment($old_file->ID); | |
// Make sure the new name is different by pre-pending the | |
// previous post id. | |
$filename = preg_replace('/^wpid\d+-/', '', $name); | |
$name = "wpid{$old_file->ID}-{$filename}"; | |
} | |
$upload = wp_upload_bits($name, null, $bits); | |
if ( ! empty($upload['error']) ) { | |
$errorString = sprintf(__('Could not write file %1$s (%2$s)'), $name, $upload['error']); | |
return new IXR_Error(500, $errorString); | |
} | |
// Construct the attachment array | |
$post_id = 0; | |
if ( ! empty( $data['post_id'] ) ) { | |
$post_id = (int) $data['post_id']; | |
if ( ! current_user_can( 'edit_post', $post_id ) ) | |
return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) ); | |
} | |
$attachment = array( | |
'post_title' => $name, | |
'post_content' => '', | |
'post_type' => 'attachment', | |
'post_parent' => $post_id, | |
'post_mime_type' => $type, | |
'guid' => $upload[ 'url' ] | |
); | |
// Save the data | |
$id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id ); | |
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) ); | |
do_action( 'xmlrpc_call_success_mw_newMediaObject', $id, $args ); | |
$mem = memory_get_peak_usage(true); | |
$struct = array( | |
'id' => strval( $id ), | |
'file' => $name, | |
'url' => $upload[ 'url' ], | |
'type' => $type, | |
'mem' => $mem | |
); | |
error_log(''); | |
error_log('file: '. $name); | |
error_log('peak: '. var_export($mem, true)); | |
error_log(''); | |
return apply_filters( 'wp_handle_upload', $struct, 'upload' ); | |
} | |
add_filter( 'xmlrpc_methods', 'xtu_xmlrpc_methods' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment