Skip to content

Commit

Permalink
Merge pull request #32 from percolate/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
EEGL authored Oct 6, 2017
2 parents 7f36a8b + 970623a commit a36aa6c
Show file tree
Hide file tree
Showing 15 changed files with 309 additions and 437 deletions.
4 changes: 4 additions & 0 deletions 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.7

* Adding support for Percolate's overhauled v5 Taxonomy API

### 4.x-1.2.6

* Image import backward compatibility for WP < v4.8
Expand Down
30 changes: 29 additions & 1 deletion api/models/percolate-post-model.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ public function importPost($post, $template, $schema, $channel, $wpPostID = null
Percolate_Log::log('Post already imported: ' . $post['id']);
// DEBUG: Delete post if any
// wp_delete_post($posts->posts[0]->ID, true);

$res['success'] = false;
$res['percolate_id'] = $post['id'];
$res['message'] = "Post already imported";
Expand Down Expand Up @@ -373,7 +374,7 @@ public function importPost($post, $template, $schema, $channel, $wpPostID = null
}
}

// ----------- Categories --------------
// ----------- OBSOLATE: Topic -> Category --------------
$post_category = array();

if( isset($post['topic_ids']) && !empty($post['topic_ids']) ) {
Expand Down Expand Up @@ -608,7 +609,34 @@ public function importPost($post, $template, $schema, $channel, $wpPostID = null
}
}

// ----------- Taxonomies v5 --------------
if( isset($channel->taxonomyMapping) && !empty($channel->taxonomyMapping) ) {
foreach ($channel->taxonomyMapping as $index => $mapping) {
Percolate_Log::log('Adding terms for : ' . print_r($mapping, true));
$wpTerms = [];
foreach ($schema['fields'] as $field) {
if ($field['type'] == 'term') {
foreach ($post['ext'][$field['key']] as $termID) {
$wpTerms[] = $mapping->terms->${termID};
}
}
}
Percolate_Log::log('$wpTerms' . print_r($wpTerms, true));
$taxonomyWp = get_taxonomy( $mapping->taxonomyWP );
if (!$taxonomyWp->hierarchical) {
Percolate_Log::log('Taxonomy is not hierarchical');
foreach ($wpTerms as $key => $termId) {
$term = get_term( $termId, $mapping->taxonomyWP );
$wpTerms[$key] = $term->slug;
}
}
wp_set_post_terms($wpPostID, $wpTerms, $mapping->taxonomyWP, $index==0 ? false : true);
}
}

// ----------- Custom Taxonomies --------------
// Not supported anymore
// Superseded by v5 custom taxonomies
if ( isset($template->taxonomy) && $template->taxonomy == 'on' && isset($template->taxonomyField) && isset($template->taxonomyWP) ) {
$terms = $post['ext'][$template->taxonomyField];
if (!is_array($terms)) {
Expand Down
18 changes: 16 additions & 2 deletions api/models/percolate-wp-model.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,35 @@ public function getCategories()
}

/**
* Get custom taxonomies
* Get all taxonomies
*
* @return array|false Taxonomies
*/
public function getTaxonomies()
{
$args = array(
'public' => true,
'_builtin' => false
// '_builtin' => false

);
$taxonomies = get_taxonomies( $args, 'objects', 'and' );
return $taxonomies;
}

/**
* Get all terms
*
* @return array|false Terms
*/
public function getTerms()
{
$args = array(
'hide_empty' => false,
);
$terms = get_terms( $args );
return $terms;
}


/**
* Get WP users
Expand Down
18 changes: 15 additions & 3 deletions api/services/percolate-ajax-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public function __construct(
add_action( 'wp_ajax_get_categories', array( $this, 'getCategories' ) );
// Get WP taxonomies
add_action( 'wp_ajax_get_taxonomies', array( $this, 'getTaxonomies' ) );
// Get WP terms
add_action( 'wp_ajax_get_terms', array( $this, 'getTerms' ) );
// Get WP users
add_action( 'wp_ajax_get_users', array( $this, 'getUsers' ) );
// Is ACF active
Expand Down Expand Up @@ -118,12 +120,22 @@ public function getCategories()
}

/**
* Get WP custom taxonomies
* Get WP taxonomies
*/
public function getTaxonomies()
{
$categories = $this->Wp->getTaxonomies();
echo json_encode($categories);
$taxonomies = $this->Wp->getTaxonomies();
echo json_encode($taxonomies);
wp_die();
}

/**
* Get WP terms
*/
public function getTerms()
{
$terms = $this->Wp->getTerms();
echo json_encode($terms);
wp_die();
}

Expand Down
2 changes: 1 addition & 1 deletion api/services/percolate-api-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function callAPI ($api_key, $method, $fields=array(), $jsonFields=array()
$data = json_decode( wp_remote_retrieve_body($res), true );

if ($status != 200 && $status != 201) {
$message = "An unknown error occurred communicating with Percolate ($status): " . print_r($res, true);
$message = "An unknown error occurred communicating with Percolate ($status): "; // . print_r($res, true);
if ($data) {
if ($data['error']) {
$message = $data['error'];
Expand Down
161 changes: 34 additions & 127 deletions frontend/scripts/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,185 +4,92 @@ angular.module('wpApi', [])
.factory('Api', function ($http) {
var _url = ajax_object.ajax_url

function callApi(data) {
return $http({
method : 'POST',
url : _url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : jQuery.param(data)
})
}

return {
getTemplate: function ($tpl) {
return $http({
method : 'POST',
url : _url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : jQuery.param({ action : 'template', template: $tpl})
})
return callApi({ action : 'template', template: $tpl})
},
getData: function () {
return $http({
method : 'POST',
url : _url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : jQuery.param({ action : 'get_data'})
})
return callApi({ action : 'get_data'})
},
setData: function ($data) {
$data.timestamp = new Date().toUTCString()

return $http({
method : 'POST',
url : _url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : jQuery.param({ action : 'set_data', data: $data})
})
return callApi({ action : 'set_data', data: $data})
},
getCategories: function () {
return $http({
method : 'POST',
url : _url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : jQuery.param({ action : 'get_categories'})
})
return callApi({ action : 'get_categories'})
},
getTaxonomies: function () {
return $http({
method : 'POST',
url : _url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : jQuery.param({ action : 'get_taxonomies'})
})
return callApi({ action : 'get_taxonomies'})
},
getTerms: function () {
return callApi({ action : 'get_terms'})
},
getUsers: function () {
return $http({
method : 'POST',
url : _url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : jQuery.param({ action : 'get_users'})
})
return callApi({ action : 'get_users'})
},
getCpts: function () {
return $http({
method : 'POST',
url : _url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : jQuery.param({ action : 'get_cpts'})
})
return callApi({ action : 'get_cpts'})
},
getPostData: function () {
return $http({
method : 'POST',
url : _url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : jQuery.param({ action : 'get_post_data'})
})
return callApi({ action : 'get_post_data'})
},
getAcfStatus: function () {
return $http({
method : 'POST',
url : _url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : jQuery.param({ action : 'get_acf_status'})
})
return callApi({ action : 'get_acf_status'})
},
getAcfData: function () {
return $http({
method : 'POST',
url : _url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : jQuery.param({ action : 'get_acf_data'})
})
return callApi({ action : 'get_acf_data'})
},
getMetaBoxStatus: function () {
return $http({
method : 'POST',
url : _url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : jQuery.param({ action : 'get_metabox_status'})
})
return callApi({ action : 'get_metabox_status'})
},
getMetaBoxData: function () {
return $http({
method : 'POST',
url : _url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : jQuery.param({ action : 'get_metabox_data'})
})
return callApi({ action : 'get_metabox_data'})
},
getWpmlStatus: function () {
return $http({
method : 'POST',
url : _url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : jQuery.param({ action : 'get_wpml_status'})
})
return callApi({ action : 'get_wpml_status'})
},
getWpmlData: function () {
return $http({
method : 'POST',
url : _url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : jQuery.param({ action : 'get_wpml_language'})
})
return callApi({ action : 'get_wpml_language'})
},
getMessages: function () {
return $http({
method : 'POST',
url : _url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : jQuery.param({ action : 'get_messages'})
})
return callApi({ action : 'get_messages'})
},
setMessages: function ($data) {
return $http({
method : 'POST',
url : _url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : jQuery.param({ action : 'set_messages', data: $data})
})
return callApi({ action : 'set_messages', data: $data})
},
getLog: function () {
return $http({
method : 'POST',
url : _url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : jQuery.param({ action : 'get_log'})
})
return callApi({ action : 'get_log'})
},
deleteLog: function () {
return $http({
method : 'POST',
url : _url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : jQuery.param({ action : 'delete_log'})
})
return callApi({ action : 'delete_log'})
},
getQueue: function () {
return $http({
method : 'POST',
url : _url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : jQuery.param({ action : 'get_queue'})
})
return callApi({ action : 'get_queue'})
},
deleteQueue: function () {
return $http({
method : 'POST',
url : _url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : jQuery.param({ action : 'delete_queue'})
})
return callApi({ action : 'delete_queue'})
},


/**
* Imports image into WP */
importImage: function (data) {
return $http({
method : 'POST',
url : _url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : jQuery.param({ action: 'image_import', data: {
return callApi({ action: 'image_import', data: {
key : data.key,
imageKey: data.uid,
postId : data.postId,
featured: data.featured,
size : data.size
}})
}
})
},
}
Expand Down
Loading

0 comments on commit a36aa6c

Please sign in to comment.