From 0961ef4fe87fad156ce5b022116fbb001243094d Mon Sep 17 00:00:00 2001 From: Matth_eu Date: Thu, 13 Aug 2015 17:17:23 +0100 Subject: [PATCH 01/25] vip --- helpers.php | 68 ++++++++++++++++++++++++++++ inc/class-resource-space-loader.php | 70 +---------------------------- resourcespace.php | 1 + 3 files changed, 71 insertions(+), 68 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 @@ +sideload_image( $data[0]->original ); + // $attachment_id = wpcom_vip_download_image( $data[0]->original ); // Request preview size. - $attachment_id = $this->sideload_image( $data[0]->preview ); + $attachment_id = wpcom_vip_download_image( $data[0]->preview ); if ( is_wp_error( $attachment_id ) ) { wp_send_json_error( $attachment_id->get_error_message() ); @@ -81,70 +81,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', __( 'Could not create attachment', 'resourcespace' ) ); - } - } else { - unlink( $file ); - return new WP_Error( 'broke', __( 'Upload error', 'resourcespace' ) ); - } - - unlink( $file ); - - } else { - return new WP_Error( 'broke', __( 'Unable to retrieve image', 'resourcespace' ) ); - } - - } - } diff --git a/resourcespace.php b/resourcespace.php index 02e563d..2ccf0cb 100644 --- a/resourcespace.php +++ b/resourcespace.php @@ -19,6 +19,7 @@ wp_die( __( 'Media Explorer plugin must be enabled.', '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' ); From 946e1ad3e2dbf65381cb123227e1f4a47a274aea Mon Sep 17 00:00:00 2001 From: Matth_eu Date: Thu, 13 Aug 2015 17:20:57 +0100 Subject: [PATCH 02/25] pass parent id --- inc/class-resource-space-loader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/class-resource-space-loader.php b/inc/class-resource-space-loader.php index cff4f35..bbfd0a1 100644 --- a/inc/class-resource-space-loader.php +++ b/inc/class-resource-space-loader.php @@ -69,7 +69,7 @@ function ajax_get_image() { // $attachment_id = wpcom_vip_download_image( $data[0]->original ); // Request preview size. - $attachment_id = wpcom_vip_download_image( $data[0]->preview ); + $attachment_id = wpcom_vip_download_image( $data[0]->preview, $parent_post_id ); if ( is_wp_error( $attachment_id ) ) { wp_send_json_error( $attachment_id->get_error_message() ); From 9a4328b73ed68568f0a856db0b8226c88c28617a Mon Sep 17 00:00:00 2001 From: Matth_eu Date: Thu, 13 Aug 2015 17:55:41 +0100 Subject: [PATCH 03/25] bugfixes --- js/mexp.js | 2 +- resourcespace.php | 24 ++++++++++++++---------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/js/mexp.js b/js/mexp.js index 4aff878..58c7a60 100644 --- a/js/mexp.js +++ b/js/mexp.js @@ -150,7 +150,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 2ccf0cb..fefd7bf 100644 --- a/resourcespace.php +++ b/resourcespace.php @@ -15,18 +15,22 @@ 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' ) ); -} +add_action( 'plugins_loaded', function() { + + if ( ! class_exists( 'MEXP_Service' ) ) { + wp_die( __( 'Media Explorer plugin must be enabled.', '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' ); -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(); +}); add_filter( 'mexp_services', function( array $services ) { From 45ea0a537e7120ee1b4cfd74d884dfeaee2f6541 Mon Sep 17 00:00:00 2001 From: Matth_eu Date: Thu, 13 Aug 2015 18:04:19 +0100 Subject: [PATCH 04/25] More bugfixes --- inc/class-resource-space-loader.php | 8 ++------ js/mexp.js | 2 ++ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/inc/class-resource-space-loader.php b/inc/class-resource-space-loader.php index bbfd0a1..f682911 100644 --- a/inc/class-resource-space-loader.php +++ b/inc/class-resource-space-loader.php @@ -42,8 +42,7 @@ function ajax_get_image() { 'key' => $key, 'search' => $resource_id, 'prettyfieldnames' => 1, - 'previewsize' => 'pre', - 'original' => true, + 'previewsize' => 'scr', ), $url ); $request_args = array( 'headers' => array() ); @@ -66,10 +65,7 @@ function ajax_get_image() { } // Request original URL. - // $attachment_id = wpcom_vip_download_image( $data[0]->original ); - - // Request preview size. - $attachment_id = wpcom_vip_download_image( $data[0]->preview, $parent_post_id ); + $attachment_id = wpcom_vip_download_image( $data[0]->preview ); if ( is_wp_error( $attachment_id ) ) { wp_send_json_error( $attachment_id->get_error_message() ); diff --git a/js/mexp.js b/js/mexp.js index 58c7a60..d134293 100644 --- a/js/mexp.js +++ b/js/mexp.js @@ -114,6 +114,8 @@ alert( 'Failed to import image.' ); } + toggleLoading( false ); + return; } From e11750d09611a406869c2da333410a5a2acfb85d Mon Sep 17 00:00:00 2001 From: Matth_eu Date: Thu, 13 Aug 2015 18:11:30 +0100 Subject: [PATCH 05/25] instructions --- resourcespace.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/resourcespace.php b/resourcespace.php index fefd7bf..3f3b1b3 100644 --- a/resourcespace.php +++ b/resourcespace.php @@ -21,6 +21,14 @@ wp_die( __( 'Media Explorer plugin must be enabled.', 'resourcespace' ) ); } + if ( ! defined( 'PJ_RESOURCE_SPACE_DOMAIN' ) ) { + wp_die( __( 'You must define PJ_RESOURCE_SPACE_DOMAIN', 'resourcespace' ) ); + } + + 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' ); From d1c6e14ce76ad0b2d5670fb76cb0cdee3b0eb3a3 Mon Sep 17 00:00:00 2001 From: Matth_eu Date: Fri, 14 Aug 2015 12:03:55 +0100 Subject: [PATCH 06/25] disable admin --- resourcespace.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resourcespace.php b/resourcespace.php index 3f3b1b3..fc6566b 100644 --- a/resourcespace.php +++ b/resourcespace.php @@ -36,7 +36,7 @@ require_once( __DIR__ . '/inc/class-mexp-resource-space-template.php' ); Resource_Space_Loader::get_instance(); - Resource_Space_Admin::get_instance(); + // Resource_Space_Admin::get_instance(); }); From b05d994447727948a54f99e4e60126d0410f7df7 Mon Sep 17 00:00:00 2001 From: Matth_eu Date: Fri, 14 Aug 2015 12:04:16 +0100 Subject: [PATCH 07/25] use POST not REQUEST --- inc/class-resource-space-admin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/class-resource-space-admin.php b/inc/class-resource-space-admin.php index 54f6336..26b0f10 100644 --- a/inc/class-resource-space-admin.php +++ b/inc/class-resource-space-admin.php @@ -71,7 +71,7 @@ function admin_enqueue_scripts() { 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', From a4cb55e9b1c17c4fb0c2af4efe1b5c147e91e679 Mon Sep 17 00:00:00 2001 From: Matth_eu Date: Fri, 14 Aug 2015 12:04:25 +0100 Subject: [PATCH 08/25] store meta data --- inc/class-resource-space-loader.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/inc/class-resource-space-loader.php b/inc/class-resource-space-loader.php index f682911..407e928 100644 --- a/inc/class-resource-space-loader.php +++ b/inc/class-resource-space-loader.php @@ -67,6 +67,8 @@ function ajax_get_image() { // Request original URL. $attachment_id = wpcom_vip_download_image( $data[0]->preview ); + update_post_meta( $attachment_id, 'resource_space', 1 ); + if ( is_wp_error( $attachment_id ) ) { wp_send_json_error( $attachment_id->get_error_message() ); } else { From 1d1d6c7d05894da43822a7b9703226313cbaf295 Mon Sep 17 00:00:00 2001 From: Matth_eu Date: Fri, 14 Aug 2015 12:04:39 +0100 Subject: [PATCH 09/25] fix JS error --- js/admin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/admin.js b/js/admin.js index 66bf0b9..322db90 100644 --- a/js/admin.js +++ b/js/admin.js @@ -1,6 +1,6 @@ (function ( $ ) { - var $oldContainer = ('#resource-space-images'); + var $oldContainer = $('#resource-space-images'); var $newContainer = $('#resource-space-new-images'); jQuery('#resource-space-add-new').on( 'click', function( event ) { From 136d66f102796aef0c5dedaec65bd9360d6d0dd9 Mon Sep 17 00:00:00 2001 From: Matth_eu Date: Wed, 26 Aug 2015 10:49:06 +0100 Subject: [PATCH 10/25] Get the admin frame working --- inc/class-resource-space-admin.php | 5 +++++ js/admin.js | 36 ++++++------------------------ resourcespace.php | 2 +- templates/admin-page.php | 4 ---- 4 files changed, 13 insertions(+), 34 deletions(-) diff --git a/inc/class-resource-space-admin.php b/inc/class-resource-space-admin.php index 26b0f10..e3dbade 100644 --- a/inc/class-resource-space-admin.php +++ b/inc/class-resource-space-admin.php @@ -65,6 +65,11 @@ 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' ), + ) ); + } } diff --git a/js/admin.js b/js/admin.js index 322db90..5d35590 100644 --- a/js/admin.js +++ b/js/admin.js @@ -3,6 +3,12 @@ var $oldContainer = $('#resource-space-images'); var $newContainer = $('#resource-space-new-images'); + 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,30 +19,8 @@ $newContainer.show(); } - // Loop through attachments and build image element HTML. _.each( this.attachments, function( 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 ); - - $newContainer.append( $img ); - + library.state().attributes.library.add( attachment ); }); this.complete(); @@ -54,10 +38,4 @@ }); - window.wp.media({ - frame: 'manage', - container: $oldContainer, - library: { 'meta_query': 'resource_space' }, - }).open(); - }( jQuery )); diff --git a/resourcespace.php b/resourcespace.php index fc6566b..3f3b1b3 100644 --- a/resourcespace.php +++ b/resourcespace.php @@ -36,7 +36,7 @@ require_once( __DIR__ . '/inc/class-mexp-resource-space-template.php' ); Resource_Space_Loader::get_instance(); - // Resource_Space_Admin::get_instance(); + Resource_Space_Admin::get_instance(); }); 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 @@ - -