Skip to content

Commit

Permalink
Merge pull request humanmade#7 from humanmade/update-upstream
Browse files Browse the repository at this point in the history
update upstream
  • Loading branch information
mattheu committed Aug 13, 2015
2 parents 8623799 + 1bea05a commit d836697
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 78 deletions.
7 changes: 1 addition & 6 deletions inc/class-mexp-resource-space-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 );
Expand Down
4 changes: 2 additions & 2 deletions inc/class-resource-space-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function setup_actions() {

add_action( 'admin_notices', function() { ?>
<div class="error">
<p><?php _e( 'You must define the resource space domain and API key in your wp-config.php. See readme for more details.', 'resourcespace' ); ?></p>
<p><?php esc_html_e( 'You must define the resource space domain and API key in your wp-config.php. See readme for more details.', 'resourcespace' ); ?></p>
</div>
<?php } );

Expand Down Expand Up @@ -76,7 +76,7 @@ function ajax_query_attachments_args( $query ) {
array(
'key' => 'resource_space',
'compare' => 'EXISTS',
)
),
);
}

Expand Down
108 changes: 44 additions & 64 deletions inc/class-resource-space-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
}

/**
Expand All @@ -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() );
Expand All @@ -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 );
Expand All @@ -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 );

}

}
9 changes: 7 additions & 2 deletions js/mexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -121,7 +127,6 @@

// Allow overriding insert callback.
if ( self.frame.options.resourceSpaceInsertCallback ) {

callback = self.frame.options.resourceSpaceInsertCallback
}

Expand Down
9 changes: 9 additions & 0 deletions js/resource-space-vc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion resourcespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
Expand Down
6 changes: 3 additions & 3 deletions templates/admin-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<div class="wrap">

<h2 class="page-title">
Stock Images
<a href="#" id="resource-space-add-new" class="add-new-h2"><?php _e( 'Add new', 'resourcespace' ); ?></a>
<?php esc_html_e( 'Stock Images', 'resourcespace' ); ?>
<a href="#" id="resource-space-add-new" class="add-new-h2"><?php esc_html_e( 'Add new', 'resourcespace' ); ?></a>
</h2>

<div id="resource-space-new-images" style="display: none;">
<h3><?php _e( 'Recently Imported Stock Images', 'resourcespace' ); ?></h3>
<h3><?php esc_html_e( 'Recently Imported Stock Images', 'resourcespace' ); ?></h3>
</div>

<div class="wrap" id="resource-space-images" data-search="<?php _admin_search_query() ?>">
Expand Down

0 comments on commit d836697

Please sign in to comment.