diff --git a/inc/class-mexp-resource-space-service.php b/inc/class-mexp-resource-space-service.php
index 7d86612..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();
@@ -111,12 +112,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'] );
-
- $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-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..f53203b 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' ) );
}
/**
@@ -43,7 +42,8 @@ function ajax_get_image() {
'key' => $key,
'search' => $resource_id,
'prettyfieldnames' => 1,
- 'previewsize' => 'sit',
+ 'previewsize' => 'pre',
+ 'original' => true,
), $url );
$request_args = array( 'headers' => array() );
@@ -61,14 +61,42 @@ function ajax_get_image() {
wp_send_json_error( __( 'Unable to query API', 'resourcespace' ) );
}
- // All good, continue
+ if ( count( $data ) < 1 ) {
+ 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 ) ) {
+ 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 ) {
- $downloadurl = $data[0]->preview;
- $file = get_temp_dir() . sanitize_file_name( $data[0]->Original_filename );
+ $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( $url, $request_args );
- $response = wp_remote_get( $downloadurl, $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 );
@@ -79,92 +107,44 @@ 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 );
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' ) );
- }
-
- 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 );
+ return new WP_Error( 'broke', __( 'Unable to retrieve image', 'resourcespace' ) );
}
- $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/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..02e563d 100644
--- a/resourcespace.php
+++ b/resourcespace.php
@@ -13,7 +13,7 @@
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 ( ! 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 @@