diff --git a/README.md b/README.md index bccee13..406f4e1 100644 --- a/README.md +++ b/README.md @@ -198,6 +198,10 @@ You should see that: ## Changelog +### 4.x-1.2.6 + +* Image import backward compatibility for WP < v4.8 + ### 4.x-1.2.5 * Complete rewrite of image importing @@ -291,4 +295,4 @@ please refer to the original repository: *** _Please do not remove this version declaration_ -~Current Version:4.x-1.2.5~ +~Current Version:4.x-1.2.6~ diff --git a/api/models/percolate-post-model.php b/api/models/percolate-post-model.php index 66db497..0232764 100644 --- a/api/models/percolate-post-model.php +++ b/api/models/percolate-post-model.php @@ -510,7 +510,7 @@ public function importPost($post, $template, $schema, $channel, $wpPostID = null $definition = PercolateHelpers::searchInArray($fieldDefinitions, 'key', $key); if( $definition[0]['type'] == 'asset' ) { Percolate_Log::log('Asset field found, importing from Percolate'); - $imageID = $this->Media->importImageWP($value, $channel->key); + $imageID = $this->Media->importImage($value, $channel->key); $value = $importedFields[$key] = $imageID; } @@ -622,7 +622,10 @@ public function importPost($post, $template, $schema, $channel, $wpPostID = null if ( isset($template->image) && $template->image == 'on' && isset($template->postImage) && isset($importedFields[$template->postImage]) ) { // Gegt image ID from the imported fields array $imageID = $importedFields[$template->postImage]; + Percolate_Log::log('Setting imageID:' . $imageID . ' to wpPostID' . $wpPostID ); set_post_thumbnail( $wpPostID, $imageID ); + Percolate_Log::log('Set imageID:' . $imageID . ' to wpPostID' . $wpPostID ); + } // ----------- WPML -------------- diff --git a/api/services/percolate-media-service.php b/api/services/percolate-media-service.php index 31b8f35..726dc14 100644 --- a/api/services/percolate-media-service.php +++ b/api/services/percolate-media-service.php @@ -104,7 +104,7 @@ public function importImageEndpoint($value='') * $_POST['data'] keys: $imageKey, $key, $postId, $featured */ if( isset($_POST['data']) ) { - $imageID = $this->importImageWP(array($_POST['data']['imageKey']), $_POST['data']['key']); + $imageID = $this->importImage(array($_POST['data']['imageKey']), $_POST['data']['key']); if( $_POST['data']['featured'] === "true" && $imageID) { Percolate_Log::log('featured: ' . $imageID); set_post_thumbnail( $_POST['data']['postId'], $imageID ); @@ -138,7 +138,7 @@ public function importImageEndpoint($value='') * @param $key: importer's custom channel set's key * @return string|false: imported image's ID */ - public function importImageWP ($imageKey, $key) { + public function importImage ($imageKey, $key) { if( is_array($imageKey) ) { $imageKey = $imageKey[0]; @@ -221,12 +221,7 @@ public function importImageWP ($imageKey, $key) { $title = $imageData['id']; } - $id = media_sideload_image($src, null, $title, 'id'); - - // ----------- Percolate meta fields -------------- - update_post_meta($id, 'percolate_id', $imageData['id']); - update_post_meta($id, 'percolate_uid', $imageData['uid']); - update_post_meta($id, 'percolate_created_at', strtotime($imageData['metadata']['created_at'])); + $id = $this->uploadToWp($src, $title); // If error storing permanently, unlink if ( is_wp_error($id) ) { @@ -234,6 +229,11 @@ public function importImageWP ($imageKey, $key) { return false; } + // ----------- Percolate meta fields -------------- + update_post_meta($id, 'percolate_id', $imageData['id']); + update_post_meta($id, 'percolate_uid', $imageData['uid']); + update_post_meta($id, 'percolate_created_at', strtotime($imageData['metadata']['created_at'])); + return $id; } @@ -271,7 +271,7 @@ public function importImageFromUrl ($url) { } - $id = media_sideload_image($url, null, $title, 'id'); + $id = $this->uploadToWp($url, $title); // If error importing if ( is_wp_error($id) ) { @@ -282,4 +282,52 @@ public function importImageFromUrl ($url) { return $src = wp_get_attachment_url( $id ); } + /** + * Uploads the image from given URL to WP + * @param string $src: The image's src + * @param string $title: Title of the asset + * @return int|WP_Error id: The uploaded asset's ID or error + */ + private function uploadToWp($src, $title = '') + { + // media_sideload_image can only return the ID on WP v4.8 and above + if (version_compare ( get_bloginfo('version') , '4.8.0' , '>=')) { + + return media_sideload_image($src, null, $title, 'id'); + + } else { + + if ( ! empty( $src ) ) { + + // Set variables for storage, fix file filename for query strings. + preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $src, $matches ); + if ( ! $matches ) { + return new WP_Error( 'image_sideload_failed', __( 'Invalid image URL' ) ); + } + + $file_array = array(); + $file_array['name'] = basename( $matches[0] ); + + // Download file to temp location. + $file_array['tmp_name'] = download_url( $src ); + + // If error storing temporarily, return the error. + if ( is_wp_error( $file_array['tmp_name'] ) ) { + return $file_array['tmp_name']; + } + + // Do the validation and storage stuff. + $id = media_handle_sideload( $file_array, 0, $title ); + + // If error storing permanently, unlink. + if ( is_wp_error( $id ) ) { + @unlink( $file_array['tmp_name'] ); + } + + return $id; + } + + } + } + } diff --git a/frontend/views/templates/settings.php b/frontend/views/templates/settings.php index 48a3827..7e7e783 100644 --- a/frontend/views/templates/settings.php +++ b/frontend/views/templates/settings.php @@ -83,7 +83,7 @@

- Plugin version: 4.x-1.2.5 (Supported WordPress version - Plugin version)
+ Plugin version: 4.x-1.2.6 (Supported WordPress version - Plugin version)
PHP >= 5.6 required for the plugin to function properly

diff --git a/percolate-sync.php b/percolate-sync.php index f6ee6b2..1558958 100644 --- a/percolate-sync.php +++ b/percolate-sync.php @@ -7,7 +7,7 @@ Plugin URI: https://github.com/percolate/wordpress Description: Percolate integration for Wordpress, which includes the ability to sync posts, media library elements and custom creative templates. Author: Percolate Industries, Inc. -Version: 4.x-1.2.5 +Version: 4.x-1.2.6 Author URI: http://percolate.com */