Skip to content

Commit

Permalink
Merge pull request #31 from percolate/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
EEGL authored Oct 3, 2017
2 parents e516339 + d2e44e6 commit 7f36a8b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 13 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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~
5 changes: 4 additions & 1 deletion api/models/percolate-post-model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 --------------
Expand Down
66 changes: 57 additions & 9 deletions api/services/percolate-media-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -221,19 +221,19 @@ 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) ) {
Percolate_Log::log(print_r($id, true));
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;
}

Expand Down Expand Up @@ -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) ) {
Expand All @@ -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;
}

}
}

}
2 changes: 1 addition & 1 deletion frontend/views/templates/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@

<p>
<small>
Plugin version: 4.x-1.2.5 (<i>Supported WordPress version - Plugin version</i>)<br>
Plugin version: 4.x-1.2.6 (<i>Supported WordPress version - Plugin version</i>)<br>
PHP >= 5.6 required for the plugin to function properly
</small>
</p>
2 changes: 1 addition & 1 deletion percolate-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand Down

0 comments on commit 7f36a8b

Please sign in to comment.