From 45a7a51a05c52b7245fa8a855ad7602c61910625 Mon Sep 17 00:00:00 2001 From: Matth_eu Date: Thu, 13 Aug 2015 16:35:33 +0100 Subject: [PATCH 1/5] update upstream --- inc/class-mexp-resource-space-service.php | 11 +++++---- inc/class-resource-space-admin.php | 4 ++-- inc/class-resource-space-loader.php | 29 ++++++++++++----------- js/mexp.js | 9 +++++-- js/resource-space-vc.js | 9 +++++++ resourcespace.php | 6 ++++- templates/admin-page.php | 6 ++--- 7 files changed, 48 insertions(+), 26 deletions(-) diff --git a/inc/class-mexp-resource-space-service.php b/inc/class-mexp-resource-space-service.php index 7d86612..ba319b5 100644 --- a/inc/class-mexp-resource-space-service.php +++ b/inc/class-mexp-resource-space-service.php @@ -112,10 +112,13 @@ public function request( array $request ) { $item->set_id( $clean_data['id'] ); $item->set_url( $clean_data['url'] ); - $clean_data['thumbnail'] = add_query_arg( array( - 'action' => 'pj_rs_proxy_resource', - 'src' => urlencode( $dirty_data['thumbnail'] ), - ), admin_url( 'admin-ajax.php' ) ); + // Proxy image requests. + if ( defined( 'PJ_RESOURCE_SPACE_PROXY_THUMBNAILS' ) && PJ_RESOURCE_SPACE_PROXY_THUMBNAILS ) { + $clean_data['thumbnail'] = add_query_arg( array( + 'action' => 'pj_rs_proxy_resource', + 'src' => urlencode( $dirty_data['thumbnail'] ), + ), admin_url( 'admin-ajax.php' ) ); + } $item->set_thumbnail( $clean_data['thumbnail'] ); diff --git a/inc/class-resource-space-admin.php b/inc/class-resource-space-admin.php index 11b8322..54f6336 100644 --- a/inc/class-resource-space-admin.php +++ b/inc/class-resource-space-admin.php @@ -28,7 +28,7 @@ public function setup_actions() { add_action( 'admin_notices', function() { ?>
-

+

'resource_space', 'compare' => 'EXISTS', - ) + ), ); } diff --git a/inc/class-resource-space-loader.php b/inc/class-resource-space-loader.php index 25cebe0..09b4509 100644 --- a/inc/class-resource-space-loader.php +++ b/inc/class-resource-space-loader.php @@ -43,7 +43,7 @@ function ajax_get_image() { 'key' => $key, 'search' => $resource_id, 'prettyfieldnames' => 1, - 'previewsize' => 'sit', + 'original' => true, ), $url ); $request_args = array( 'headers' => array() ); @@ -61,12 +61,17 @@ function ajax_get_image() { wp_send_json_error( __( 'Unable to query API', 'resourcespace' ) ); } + if ( count( $data ) < 1 ) { + wp_send_json_error( __( 'Resource not found', 'resourcespace' ) ); + } + // All good, continue + $file = get_temp_dir() . sanitize_file_name( $data[0]->Original_filename ); - $downloadurl = $data[0]->preview; - $file = get_temp_dir() . sanitize_file_name( $data[0]->Original_filename ); + // TODO test. Advice from Kirill was to use the users cookie. + // Hopefully it isn't required as this isn't as robust as using basic auth. + $response = wp_remote_get( $data[0]->original_link, $request_args ); - $response = wp_remote_get( $downloadurl, $request_args ); if ( 200 == wp_remote_retrieve_response_code( $response ) ) { file_put_contents( $file, wp_remote_retrieve_body( $response ) ); @@ -79,26 +84,22 @@ function ajax_get_image() { $wp_filetype = wp_check_filetype( $filename, null ); - date_default_timezone_set( 'Europe/London' ); - $d = new DateTime(); - $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_parent' => 0, 'post_title' => $data[0]->{'Légende'}, - 'post_content' => 'Downloaded ' . $d->format( 'd/m/Y \a\t H:i:s' ), - 'post_status' => 'inherit' + 'post_content' => 'Downloaded ' . current_time( 'd/m/Y \a\t H:i:s' ), + 'post_status' => 'inherit', ); $attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], $parent_post_id ); if ( ! is_wp_error( $attachment_id ) ) { - require_once( ABSPATH . "wp-admin" . '/includes/image.php' ); - /* Add some attachment data */ - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] ); - $attachment_data['image_meta']['created_timestamp'] = $d->format( 'Y-m-d H:i:s' ); - //$attachment_data['image_meta']['copyright'] = 'Yelster ' . $d->format('Y'); + require_once( trailingslashit( ABSPATH ) . 'wp-admin/includes/image.php' ); + + $attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] ); + $attachment_data['image_meta']['created_timestamp'] = current_time( 'Y-m-d H:i:s', true ); wp_update_attachment_metadata( $attachment_id, $attachment_data ); diff --git a/js/mexp.js b/js/mexp.js index e5e2808..4aff878 100644 --- a/js/mexp.js +++ b/js/mexp.js @@ -107,7 +107,13 @@ }).done( function( response ) { if ( ! response.success ) { - alert( response.data ); + + if ( 'data' in response ) { + alert( response.data ); + } else { + alert( 'Failed to import image.' ); + } + return; } @@ -121,7 +127,6 @@ // Allow overriding insert callback. if ( self.frame.options.resourceSpaceInsertCallback ) { - callback = self.frame.options.resourceSpaceInsertCallback } diff --git a/js/resource-space-vc.js b/js/resource-space-vc.js index 98305c1..478ecfc 100644 --- a/js/resource-space-vc.js +++ b/js/resource-space-vc.js @@ -91,6 +91,15 @@ }, 1 ); + // Slightly hcky workaround because for some reason the load more + // button doesn't exist when the event callback is attached. + $('#resource-space-loadmore').on('click', function(e) { + var view = wp.media.frames.resourceSpaceFrame.views.get('.media-frame-content' ); + if ( view.length ) { + view[0].paginate(e); + } + } ); + }, this ); // Create the toolbar. diff --git a/resourcespace.php b/resourcespace.php index 6852875..058d88f 100644 --- a/resourcespace.php +++ b/resourcespace.php @@ -13,7 +13,11 @@ define( 'PJ_RESOURCE_SPACE_PLUGIN_DIR', untrailingslashit( plugin_dir_path( __FILE__ ) ) ); define( 'PJ_RESOURCE_SPACE_PLUGIN_URL', untrailingslashit( plugin_dir_url( __FILE__ ) ) ); -defined( 'PJ_RESOURCE_SPACE_RESULTS_PER_PAGE' ) or define( 'PJ_RESOURCE_SPACE_RESULTS_PER_PAGE', 20 ); +defined( 'PJ_RESOURCE_SPACE_RESULTS_PER_PAGE' ) or define( 'PJ_RESOURCE_SPACE_RESULTS_PER_PAGE', 10 ); + +if ( ! defined( 'PJ_RESOURCE_SPACE_PROXY_THUMBNAILS' ) ) { + define( 'PJ_RESOURCE_SPACE_PROXY_THUMBNAILS', defined( 'PJ_RESOURCE_SPACE_AUTHP' ) ); +} if ( ! class_exists( 'MEXP_Service' ) ) { wp_die( __( 'Media Explorer plugin must be enabled.', 'resourcespace' ) ); diff --git a/templates/admin-page.php b/templates/admin-page.php index 7f8e09c..724152c 100644 --- a/templates/admin-page.php +++ b/templates/admin-page.php @@ -5,12 +5,12 @@

- Stock Images - + +

From 72e75a42e24226b9add9425e33600be454d008c0 Mon Sep 17 00:00:00 2001 From: Matth_eu Date: Thu, 13 Aug 2015 16:45:14 +0100 Subject: [PATCH 2/5] remove proxy code --- inc/class-mexp-resource-space-service.php | 9 ----- inc/class-resource-space-loader.php | 44 ----------------------- resourcespace.php | 4 --- 3 files changed, 57 deletions(-) diff --git a/inc/class-mexp-resource-space-service.php b/inc/class-mexp-resource-space-service.php index ba319b5..b8a985d 100644 --- a/inc/class-mexp-resource-space-service.php +++ b/inc/class-mexp-resource-space-service.php @@ -111,15 +111,6 @@ public function request( array $request ) { $item->set_date_format( $clean_data['date_format'] ); $item->set_id( $clean_data['id'] ); $item->set_url( $clean_data['url'] ); - - // Proxy image requests. - if ( defined( 'PJ_RESOURCE_SPACE_PROXY_THUMBNAILS' ) && PJ_RESOURCE_SPACE_PROXY_THUMBNAILS ) { - $clean_data['thumbnail'] = add_query_arg( array( - 'action' => 'pj_rs_proxy_resource', - 'src' => urlencode( $dirty_data['thumbnail'] ), - ), admin_url( 'admin-ajax.php' ) ); - } - $item->set_thumbnail( $clean_data['thumbnail'] ); $response->add_item( $item ); diff --git a/inc/class-resource-space-loader.php b/inc/class-resource-space-loader.php index 09b4509..78f6cf5 100644 --- a/inc/class-resource-space-loader.php +++ b/inc/class-resource-space-loader.php @@ -21,7 +21,6 @@ public static function get_instance() { public function __construct() { add_action( 'wp_ajax_pj_rs_get_resource', array( $this, 'ajax_get_image' ) ); - add_action( 'wp_ajax_pj_rs_proxy_resource' , array( $this, 'ajax_proxy_image' ) ); } /** @@ -125,47 +124,4 @@ function ajax_get_image() { exit(); } - function ajax_proxy_image() { - - $src = isset( $_GET['src'] ) ? sanitize_text_field( urldecode( $_GET['src'] ) ) : null; - - if ( ! $src ) { - die('Please provide an image src.' ); - } - - $bits_request = parse_url( $src ); - $bits_allowed = parse_url( PJ_RESOURCE_SPACE_DOMAIN ); - - $allowed = isset( $bits_request['host'] ) && isset( $bits_allowed['host'] ) && ( $bits_request['host'] === $bits_allowed['host'] ); - - if ( ! $allowed ) { - die('Domain not allowed.'); - } - - $request_args = array( - 'headers' => array() - ); - - // Pass basic auth header if available. - if ( defined( 'PJ_RESOURCE_SPACE_AUTHL' ) && defined( 'PJ_RESOURCE_SPACE_AUTHP' ) ) { - $request_args['headers']['Authorization'] = 'Basic ' . base64_encode( PJ_RESOURCE_SPACE_AUTHL . ':' . PJ_RESOURCE_SPACE_AUTHP ); - } - - $response = wp_remote_get( $src, $request_args ); - - if ( - 'OK' !== wp_remote_retrieve_response_message( $response ) - || 200 !== wp_remote_retrieve_response_code( $response ) - ) { - die( 'request failed' ); - } - - foreach ( wp_remote_retrieve_headers( $response ) as $header => $value ) { - header( "$header: $value" ); - } - - echo wp_remote_retrieve_body( $response ); - - } - } diff --git a/resourcespace.php b/resourcespace.php index 058d88f..02e563d 100644 --- a/resourcespace.php +++ b/resourcespace.php @@ -15,10 +15,6 @@ defined( 'PJ_RESOURCE_SPACE_RESULTS_PER_PAGE' ) or define( 'PJ_RESOURCE_SPACE_RESULTS_PER_PAGE', 10 ); -if ( ! defined( 'PJ_RESOURCE_SPACE_PROXY_THUMBNAILS' ) ) { - define( 'PJ_RESOURCE_SPACE_PROXY_THUMBNAILS', defined( 'PJ_RESOURCE_SPACE_AUTHP' ) ); -} - if ( ! class_exists( 'MEXP_Service' ) ) { wp_die( __( 'Media Explorer plugin must be enabled.', 'resourcespace' ) ); } From 36334666aa7f96692ccc7a1ea5f6633d97b09f55 Mon Sep 17 00:00:00 2001 From: Matth_eu Date: Thu, 13 Aug 2015 17:07:14 +0100 Subject: [PATCH 3/5] simpler sideload --- helpers.php | 68 +++++++++++++++++++++++++++++ inc/class-resource-space-loader.php | 36 +++++++++++---- 2 files changed, 95 insertions(+), 9 deletions(-) create mode 100644 helpers.php diff --git a/helpers.php b/helpers.php new file mode 100644 index 0000000..b53d425 --- /dev/null +++ b/helpers.php @@ -0,0 +1,68 @@ + $key, 'search' => $resource_id, 'prettyfieldnames' => 1, - 'original' => true, + 'previewsize' => 'pre', ), $url ); $request_args = array( 'headers' => array() ); @@ -64,15 +64,34 @@ function ajax_get_image() { wp_send_json_error( __( 'Resource not found', 'resourcespace' ) ); } - // All good, continue - $file = get_temp_dir() . sanitize_file_name( $data[0]->Original_filename ); + $attachment_id = $this->sideload_image( $data[0]->preview ); + + if ( is_wp_error( $attachment_id ) ) { + wp_send_json_error( $attachment_id->get_error_message() ); + } else { + wp_send_json_success( wp_prepare_attachment_for_js( $attachment_id ) ); + } + + exit(); + + } + + private function sideload_image( $url ) { + + $request_args = array( 'headers' => array() ); + + // Pass basic auth header if available. + if ( defined( 'PJ_RESOURCE_SPACE_AUTHL' ) && defined( 'PJ_RESOURCE_SPACE_AUTHP' ) ) { + $request_args['headers']['Authorization'] = 'Basic ' . base64_encode( PJ_RESOURCE_SPACE_AUTHL . ':' . PJ_RESOURCE_SPACE_AUTHP ); + } // TODO test. Advice from Kirill was to use the users cookie. // Hopefully it isn't required as this isn't as robust as using basic auth. - $response = wp_remote_get( $data[0]->original_link, $request_args ); + $response = wp_remote_get( $url, $request_args ); if ( 200 == wp_remote_retrieve_response_code( $response ) ) { + $file = get_temp_dir() . sanitize_file_name( $data[0]->Original_filename ); file_put_contents( $file, wp_remote_retrieve_body( $response ) ); $filename = basename( $file ); @@ -104,24 +123,23 @@ function ajax_get_image() { add_post_meta( $attachment_id, 'resource_space', true, true ); - wp_send_json_success( wp_prepare_attachment_for_js( $attachment_id ) ); + return $attachment_id; } else { unlink( $file ); - wp_send_json_error( __( 'Could not create attachment', 'resourcespace' ) ); + return new WP_Error( 'broke', __( 'Could not create attachment', 'resourcespace' ) ); } } else { unlink( $file ); - wp_send_json_error( __( 'Upload error', 'resourcespace' ) ); + return new WP_Error( 'broke', __( 'Upload error', 'resourcespace' ) ); } unlink( $file ); } else { - wp_send_json_error( __( 'Unable to retrieve image', 'resourcespace' ) ); + return new WP_Error( 'broke', __( 'Unable to retrieve image', 'resourcespace' ) ); } - exit(); } } From 47f6bdfdbe163b49c5bc97d1b74050fd70cd4c00 Mon Sep 17 00:00:00 2001 From: Matth_eu Date: Thu, 13 Aug 2015 17:07:56 +0100 Subject: [PATCH 4/5] cleanup --- helpers.php | 68 ----------------------------------------------------- 1 file changed, 68 deletions(-) delete mode 100644 helpers.php diff --git a/helpers.php b/helpers.php deleted file mode 100644 index b53d425..0000000 --- a/helpers.php +++ /dev/null @@ -1,68 +0,0 @@ - Date: Thu, 13 Aug 2015 17:15:39 +0100 Subject: [PATCH 5/5] fixes --- inc/class-mexp-resource-space-service.php | 1 + inc/class-resource-space-loader.php | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/inc/class-mexp-resource-space-service.php b/inc/class-mexp-resource-space-service.php index b8a985d..353a338 100644 --- a/inc/class-mexp-resource-space-service.php +++ b/inc/class-mexp-resource-space-service.php @@ -103,6 +103,7 @@ public function request( array $request ) { } elseif ( ! isset( $dirty_data[ $field ] ) && isset( $args['default'] ) ) { $clean_data[ $field ] = $args['default']; } + } $item = new MEXP_Response_Item(); diff --git a/inc/class-resource-space-loader.php b/inc/class-resource-space-loader.php index a84414d..f53203b 100644 --- a/inc/class-resource-space-loader.php +++ b/inc/class-resource-space-loader.php @@ -43,6 +43,7 @@ function ajax_get_image() { 'search' => $resource_id, 'prettyfieldnames' => 1, 'previewsize' => 'pre', + 'original' => true, ), $url ); $request_args = array( 'headers' => array() ); @@ -64,6 +65,10 @@ function ajax_get_image() { wp_send_json_error( __( 'Resource not found', 'resourcespace' ) ); } + // Request original URL. + // $attachment_id = $this->sideload_image( $data[0]->original ); + + // Request preview size. $attachment_id = $this->sideload_image( $data[0]->preview ); if ( is_wp_error( $attachment_id ) ) {