diff --git a/helpers.php b/helpers.php new file mode 100644 index 0000000..b53d425 --- /dev/null +++ b/helpers.php @@ -0,0 +1,68 @@ + sanitize_text_field( $request['params']['q'] ), 'key' => PJ_RESOURCE_SPACE_KEY, 'previewsize' => 'pre', - 'prettyfieldnames' => true, 'original' => true, 'results_per_page' => PJ_RESOURCE_SPACE_RESULTS_PER_PAGE, 'page' => absint( $request['page'] ), 'restypes' => 1, // Restrict to images only. - ) ), - sprintf( '%s/plugins/api_search/', PJ_RESOURCE_SPACE_DOMAIN ) - ); + ) ) ); + + $api_url = add_query_arg( $args, sprintf( '%s/plugins/api_search/', PJ_RESOURCE_SPACE_DOMAIN ) ); $request_args = array( 'headers' => array() @@ -88,7 +86,7 @@ public function request( array $request ) { 'date' => strtotime( $resource->creation_date ), 'id' => $resource->ref, 'thumbnail' => $resource->preview, - 'url' => $resource->original_link, + 'url' => null, ); $dirty_data = apply_filters( 'resourcespace_parse_raw_image_data', $dirty_data, $resource ); diff --git a/inc/class-resource-space-admin.php b/inc/class-resource-space-admin.php index 47e7a76..5af1e1b 100644 --- a/inc/class-resource-space-admin.php +++ b/inc/class-resource-space-admin.php @@ -65,13 +65,18 @@ function admin_enqueue_scripts() { $deps = array( 'jquery', 'backbone', 'media' ); wp_enqueue_script( 'resource-space-admin', PJ_RESOURCE_SPACE_PLUGIN_URL . '/js/admin.js', $deps, PJ_RESOURCESPACE_PLUGIN_VERSION, true ); + wp_localize_script( 'media-grid', '_wpMediaGridSettings', array( + 'adminUrl' => parse_url( self_admin_url(), PHP_URL_PATH ), + 'queryVars' => (object) array( 'meta_query' => 'resource_space' ), + ) ); + } } function ajax_query_attachments_args( $query ) { - if ( isset( $_REQUEST['query']['meta_query'] ) && 'resource_space' === $_REQUEST['query']['meta_query'] ) { + if ( isset( $_POST['query']['meta_query'] ) && 'resource_space' === $_POST['query']['meta_query'] ) { $query['meta_query'] = array( array( 'key' => 'resource_space', diff --git a/inc/class-resource-space-loader.php b/inc/class-resource-space-loader.php index aa2b01a..9039f5f 100644 --- a/inc/class-resource-space-loader.php +++ b/inc/class-resource-space-loader.php @@ -35,17 +35,15 @@ function ajax_get_image() { wp_send_json_error( esc_html__( 'Empty resource id', 'resourcespace' ) ); } - $url = PJ_RESOURCE_SPACE_DOMAIN . '/plugins/api_search/'; - $key = PJ_RESOURCE_SPACE_KEY; - - $url = add_query_arg( array( - 'key' => $key, + $args = array_map( 'rawurlencode', array( + 'key' => PJ_RESOURCE_SPACE_KEY, 'search' => $resource_id, - 'prettyfieldnames' => 1, - 'previewsize' => 'pre', + 'prettyfieldnames' => false, 'original' => true, - ), $url ); + 'previewsize' => 'scr', + ) ); + $url = add_query_arg( $args, PJ_RESOURCE_SPACE_DOMAIN . '/plugins/api_search/' ); $request_args = array( 'headers' => array() ); // Pass basic auth header if available. @@ -66,10 +64,13 @@ function ajax_get_image() { } // Request original URL. - // $attachment_id = $this->sideload_image( $data[0]->original ); + $attachment_id = wpcom_vip_download_image( $data[0]->preview ); + + // Update Metadata. + update_post_meta( $attachment_id, 'resource_space', 1 ); - // Request preview size. - $attachment_id = $this->sideload_image( $data[0]->preview ); + // Allow plugins to hook in here. + do_action( 'resourcespace_import_complete', $attachment_id, $data[0] ); if ( is_wp_error( $attachment_id ) ) { wp_send_json_error( $attachment_id->get_error_message() ); @@ -81,70 +82,4 @@ function ajax_get_image() { } - 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( $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 ); - - $upload_file = wp_upload_bits( $filename, null, file_get_contents( $file ) ); - - if ( ! $upload_file['error'] ) { - - $wp_filetype = wp_check_filetype( $filename, null ); - - $attachment = array( - 'post_mime_type' => $wp_filetype['type'], - 'post_parent' => 0, - 'post_title' => $data[0]->{'Légende'}, - '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( 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 ); - - return $attachment_id; - - } else { - unlink( $file ); - return new WP_Error( 'broke', esc_html__( 'Could not create attachment', 'resourcespace' ) ); - } - } else { - unlink( $file ); - return new WP_Error( 'broke', esc_html__( 'Upload error', 'resourcespace' ) ); - } - - unlink( $file ); - - } else { - return new WP_Error( 'broke', esc_html__( 'Unable to retrieve image', 'resourcespace' ) ); - } - - } - } diff --git a/js/admin.js b/js/admin.js index 66bf0b9..e96d8ea 100644 --- a/js/admin.js +++ b/js/admin.js @@ -1,8 +1,16 @@ (function ( $ ) { - var $oldContainer = ('#resource-space-images'); + var $oldContainer = $('#resource-space-images'); var $newContainer = $('#resource-space-new-images'); + var wp_media_frame; + + var library = window.wp.media({ + frame: 'manage', + container: $oldContainer, + library: _wpMediaGridSettings.queryVars, + }).open(); + jQuery('#resource-space-add-new').on( 'click', function( event ) { event.preventDefault(); @@ -13,51 +21,33 @@ $newContainer.show(); } - // Loop through attachments and build image element HTML. _.each( this.attachments, function( attachment ) { + library.state().attributes.library.add( attachment ); + }); - var $img = $(''); - var size = 'medium'; - - if ( attachment.sizes[ size ] ) { - $img.attr( 'src', attachment.sizes[ size ].url ); - $img.attr( 'width', attachment.sizes[ size ].width ); - $img.attr( 'height', attachment.sizes[ size ].height ); - } else { - $img.attr( 'src', attachment.url ); - $img.attr( 'width', attachment.width ); - $img.attr( 'height', attachment.height ); - } - - $img.attr( 'alt', attachment.title ); - - $img.addClass( 'alignnone' ); - $img.addClass( 'size-' + size ); - $img.addClass( 'wp-image-' + attachment.id ); + this.complete(); - $newContainer.append( $img ); + } + if ( ! wp_media_frame ) { + wp_media_frame = wp.media.frames.wp_media_frame = wp.media({ + frame : "post", + state : 'mexp-service-resource-space', + resourceSpaceInsertCallback: insertImages, }); - this.complete(); + // Hack to get load more working. + wp_media_frame.on( 'open', function() { + jQuery( '#resource-space-loadmore' ).click( function( event ) { + wp.media.frame.views.get('.media-frame-content' )[0].paginate( event ); + } ); + } ); } - var wp_media_frame = wp.media.frames.wp_media_frame = wp.media({ - frame : "post", - state : 'mexp-service-resource-space', - resourceSpaceInsertCallback: insertImages, - }); - wp_media_frame.open(); wp_media_frame.$el.addClass( 'hide-menu' ); }); - window.wp.media({ - frame: 'manage', - container: $oldContainer, - library: { 'meta_query': 'resource_space' }, - }).open(); - }( jQuery )); diff --git a/js/featured-image.js b/js/featured-image.js new file mode 100644 index 0000000..2b240aa --- /dev/null +++ b/js/featured-image.js @@ -0,0 +1,106 @@ +(function ( $ ) { + + var resourceSpaceFeaturedImage = function() { + + var controller = wp.media.controller.FeaturedImage; + + wp.media.controller.FeaturedImage = controller.extend({ + + initialize: function() { + wp.media.controller.FeaturedImage.__super__.initialize.apply( this ); + this.initResourceSpaceFrame(); + }, + + activate: function() { + + wp.media.controller.FeaturedImage.__super__.activate.apply( this, arguments ); + + // Add the stock images tab. + this.frame.on( 'router:render:browse', this.resourceSpaceCreateTab ); + this.frame.on( 'content:render:resourceSpace', this.resourceSpaceRenderTab ); + + }, + + resourceSpaceCreateTab: function( routerView ) { + + routerView.set({ + resourceSpace: { + text: 'Stock Images', + priority: 60 + } + }); + + routerView.controller.content.mode('browse'); + + }, + + resourceSpaceRenderTab: function() { + + var resourceSpaceFrame = wp.media.frames.resourceSpaceFeaturedImageFrame; + + resourceSpaceFrame.open(); + + // Switch content view back to browse in the original frame. + var routerView = wp.media.frame.views.get( '.media-frame-router' )[0]; + routerView.controller.content.mode('browse'); + + window.setTimeout( function() { + + // Ensure that the resource space frame is on top. + resourceSpaceFrame.$el.closest('.media-modal').parent().appendTo( 'body' ); + + // Hide all other menu options in the frame. + resourceSpaceFrame.$el.addClass( 'hide-menu' ); + + }, 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); + } + } ); + + }, + + initResourceSpaceFrame: function() { + + if ( ! wp.media.frames.resourceSpaceFeaturedImageFrame ) { + + var self = this; + + // Bit of an odd hack. But we have to set this to a non-falsey value + // in order to prevent infinite loop when creating the new frame. + wp.media.frames.resourceSpaceFeaturedImageFrame = 1; + + wp.media.frames.resourceSpaceFeaturedImageFrame = wp.media({ + frame : "post", + state : 'mexp-service-resource-space', + resourceSpaceInsertCallback: function() { + var selection = self.get( 'selection' ); + var attachments = ( this.attachments.length ) ? [ this.attachments[0] ] : []; + selection.reset( attachments ); + this.complete(); + + if ( attachments.length ) { + wp.media.featuredImage.set( attachments[0].id ); + } + + wp.media.frame.close(); + }, + }); + } + + } + + } ); + + } + + $(document).ready( function() { + resourceSpaceFeaturedImage() + } ); + +}( jQuery )); diff --git a/js/mexp.js b/js/mexp.js index 4aff878..d134293 100644 --- a/js/mexp.js +++ b/js/mexp.js @@ -114,6 +114,8 @@ alert( 'Failed to import image.' ); } + toggleLoading( false ); + return; } @@ -150,7 +152,7 @@ var view = media.view.MEXP - media.view.MEXP = view.extend( { + view = view.extend( { noMorePosts: false, diff --git a/resourcespace.php b/resourcespace.php index d6c5cff..f656829 100644 --- a/resourcespace.php +++ b/resourcespace.php @@ -9,23 +9,36 @@ Domain Path: /languages */ -define( 'PJ_RESOURCESPACE_PLUGIN_VERSION', '0.1' ); -define( 'PJ_RESOURCE_SPACE_PLUGIN_DIR', untrailingslashit( plugin_dir_path( __FILE__ ) ) ); -define( 'PJ_RESOURCE_SPACE_PLUGIN_URL', untrailingslashit( plugin_dir_url( __FILE__ ) ) ); +define( 'PJ_RESOURCESPACE_PLUGIN_VERSION', '0.2' ); +defined( 'PJ_RESOURCE_SPACE_PLUGIN_DIR' ) OR define( 'PJ_RESOURCE_SPACE_PLUGIN_DIR', untrailingslashit( plugin_dir_path( __FILE__ ) ) ); +defined( 'PJ_RESOURCE_SPACE_PLUGIN_URL' ) OR 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', 10 ); -if ( ! class_exists( 'MEXP_Service' ) ) { - wp_die( esc_html__( 'Media Explorer plugin must be enabled.', 'resourcespace' ) ); -} +add_action( 'init', function() { + + if ( ! class_exists( 'MEXP_Service' ) ) { + wp_die( esc_html__( 'Media Explorer plugin must be enabled.', 'resourcespace' ) ); + } + + if ( ! defined( 'PJ_RESOURCE_SPACE_DOMAIN' ) ) { + wp_die( __( 'You must define PJ_RESOURCE_SPACE_DOMAIN', 'resourcespace' ) ); + } -require_once( __DIR__ . '/inc/class-resource-space-loader.php' ); -require_once( __DIR__ . '/inc/class-resource-space-admin.php' ); -require_once( __DIR__ . '/inc/class-mexp-resource-space-service.php' ); -require_once( __DIR__ . '/inc/class-mexp-resource-space-template.php' ); + if ( ! defined( 'PJ_RESOURCE_SPACE_KEY' ) ) { + wp_die( __( 'You must define PJ_RESOURCE_SPACE_KEY', 'resourcespace' ) ); + } + + require_once( __DIR__ . '/helpers.php' ); + require_once( __DIR__ . '/inc/class-resource-space-loader.php' ); + require_once( __DIR__ . '/inc/class-resource-space-admin.php' ); + require_once( __DIR__ . '/inc/class-mexp-resource-space-service.php' ); + require_once( __DIR__ . '/inc/class-mexp-resource-space-template.php' ); + + Resource_Space_Loader::get_instance(); + Resource_Space_Admin::get_instance(); -Resource_Space_Loader::get_instance(); -Resource_Space_Admin::get_instance(); +}, 9 ); add_filter( 'mexp_services', function( array $services ) { @@ -63,13 +76,30 @@ function resource_space_vc_script() { add_action( 'admin_print_scripts-post.php', 'resource_space_vc_script' ); add_action( 'admin_print_scripts-post-new.php', 'resource_space_vc_script' ); +/** + * Resourcespace featured image Script. + * @return null + */ +function resource_space_featured_image_script() { + + if ( ! current_user_can( 'insert_from_resourcespace' ) ) { + return; + } + + wp_enqueue_script( 'resource-space-featured-image', plugins_url( 'js/featured-image.js', __FILE__ ), array( 'backbone' ), null, true ); + +} + +add_action( 'admin_print_scripts-post.php', 'resource_space_featured_image_script' ); +add_action( 'admin_print_scripts-post-new.php', 'resource_space_featured_image_script' ); + /** * Add the resourcespace MEXP capability for required roles. * Note - version check to ensure this isn't fired always as it writes to the database.d */ add_action( 'admin_init', function() { - if ( version_compare( PJ_RESOURCESPACE_PLUGIN_VERSION, get_option( 'pj_resourcespace_version'), '<=' ) ) { + if ( version_compare( PJ_RESOURCESPACE_PLUGIN_VERSION, get_option( 'pj_resourcespace_version' ), '<=' ) ) { return; } diff --git a/templates/admin-page.php b/templates/admin-page.php index 724152c..97183dd 100644 --- a/templates/admin-page.php +++ b/templates/admin-page.php @@ -9,10 +9,6 @@ - -