Skip to content

Commit

Permalink
Merge pull request #26 from percolate/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
EEGL authored Mar 14, 2017
2 parents ba2da1a + 55cd718 commit f68b351
Show file tree
Hide file tree
Showing 13 changed files with 261 additions and 40 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ You should see that:

## Changelog

### 4.x-1.2.2

* Custom taxonomy support
* User mapping pagination
* Better detection for the installed version of ACF

### 4.x-1.2.1

* Added Post Syncing - changes from Percolate automatically get picked up
Expand Down Expand Up @@ -271,4 +277,4 @@ please refer to the original repository:
***

_Please do not remove this version declaration_
~Current Version:4.x-1.2.1~
~Current Version:4.x-1.2.2~
45 changes: 27 additions & 18 deletions api/models/percolate-acf-model.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,23 @@ public function __construct() {

public function get_ACF_data()
{
if ( is_plugin_active( 'advanced-custom-fields/acf.php' ) ) {
// Percolate_Log::log('ACF v4 active');
$this->acf = 'v4';
} else if ( is_plugin_active( 'advanced-custom-fields-pro/acf.php' ) ) {
// Percolate_Log::log('ACF v5 active');
$this->acf = 'v5';
global $acf;

if (isset($acf))
{
// Percolate_Log::log($acf->settings['version']);
$_ver = $acf->settings['version'];

if (version_compare($_ver, '5.0.0') >= 0) {
$this->acf = 'v5';
}
elseif (version_compare($_ver, '4.0.0') >= 0) {
$this->acf = 'v4';
} else {
$this->acf = null;
}
// Percolate_Log::log('ACF version: ' . $this->acf);

} else {
$this->acf = null;
}
Expand All @@ -35,11 +46,9 @@ public function get_ACF_data()
*/
public function getAcfStatus()
{
// // We don't want ACF for now
// echo false;
// wp_die();
global $acf;

if ( is_plugin_active( 'advanced-custom-fields/acf.php' ) || is_plugin_active( 'advanced-custom-fields-pro/acf.php' ) ) {
if ( isset($acf) ) {
echo true;
wp_die();
} else {
Expand All @@ -63,13 +72,12 @@ public function getAcfData()

public function getAcfGroups()
{
if ($this->acf && $this->acf == 'v5'){
$this->acfGroups = get_posts(array('posts_per_page' => -1, 'post_type' => 'acf-field-group'));
}
else
{
$this->acfGroups = get_posts(array('posts_per_page' => -1, 'post_type' => 'acf'));
}
if ($this->acf == 'v5') {
$this->acfGroups = get_posts(array('posts_per_page' => -1, 'post_type' => 'acf-field-group'));
} else {
$this->acfGroups = get_posts(array('posts_per_page' => -1, 'post_type' => 'acf'));
}

return $this->acfGroups;
}

Expand All @@ -83,10 +91,11 @@ public function getAcfFields()

$all_existing_acf[$group->ID] = array();

if ($this->acf == 'v5' ){
if ($this->acf == 'v5' || $this->acf == 'v55' ){
$fields = get_posts(array('posts_per_page' => -1, 'post_type' => 'acf-field', 'post_parent' => $group->ID));

foreach ($fields as $field) {
// Percolate_Log::log('Fields: ' . print_r($field, true));
// Percolate_Log::log('Fields: ' . print_r(unserialize( $field->post_content ), true));
$all_existing_acf[$group->ID][] = array(
'key' => $field->post_name,
Expand Down
10 changes: 10 additions & 0 deletions api/models/percolate-post-model.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,16 @@ public function importPost($post, $template, $schema, $channel, $wpPostID = null
}
}

// ----------- Custom Taxonomies --------------
if ( isset($template->taxonomy) && $template->taxonomy == 'on' && isset($template->taxonomyField) && isset($template->taxonomyWP) ) {
$terms = $post['ext'][$template->taxonomyField];
if (!is_array($terms)) {
$terms = explode(',', $terms);
}
Percolate_Log::log("Mapping {$template->taxonomyField} to {$template->taxonomyWP}, terms: " . print_r($terms, true));
wp_set_object_terms($wpPostID, $terms, $template->taxonomyWP, false);
}

// ----------- Featured image --------------
if ( isset($template->image) && $template->image == 'on' && isset($template->postImage) && isset($importedFields[$template->postImage]) ) {
// Gegt image ID from the imported fields array
Expand Down
16 changes: 16 additions & 0 deletions api/models/percolate-wp-model.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,22 @@ public function getCategories()
return $categories;
}

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

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


/**
* Get WP users
Expand Down
14 changes: 13 additions & 1 deletion api/services/percolate-ajax-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ public function __construct(
add_action( 'wp_ajax_set_data', array( $this, 'setData' ) );
// Get WP categories
add_action( 'wp_ajax_get_cpts', array( $this, 'getCpts' ) );
// Get WP post types
// Get WP categories
add_action( 'wp_ajax_get_categories', array( $this, 'getCategories' ) );
// Get WP taxonomies
add_action( 'wp_ajax_get_taxonomies', array( $this, 'getTaxonomies' ) );
// Get WP users
add_action( 'wp_ajax_get_users', array( $this, 'getUsers' ) );
// Is ACF active
Expand Down Expand Up @@ -115,6 +117,16 @@ public function getCategories()
wp_die();
}

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

/**
* Get WP users
*/
Expand Down
8 changes: 8 additions & 0 deletions frontend/scripts/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ angular.module('wpApi', [])
data : jQuery.param({ 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'})
})
},
getUsers: function () {
return $http({
method : 'POST',
Expand Down
14 changes: 13 additions & 1 deletion frontend/scripts/settings/controllers/add.templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ angular.module('myApp')

function getTemplateSchemas(res) {
console.log('Schemas', res.data)
$scope.stopLoader()

if( !res.data || !res.data.data ) {
$scope.showError('There was an error.')
Expand Down Expand Up @@ -109,6 +108,14 @@ angular.module('myApp')
})
}
}
return Api.getTaxonomies()
}

function getTaxonomies(res) {
console.log('Taxonomies', res)
if (res.data) {
$scope.taxonomies = res.data
}
return Api.getWpmlStatus()
}

Expand All @@ -124,6 +131,7 @@ angular.module('myApp')
}

function getMetaBoxData (res) {
$scope.stopLoader()
if ($scope.isMetaBoxActive) {
$scope.metaboxGroups = res.data.groups
}
Expand Down Expand Up @@ -214,6 +222,7 @@ angular.module('myApp')
.then(getCpts, apiError)
.then(getAcfStatus, apiError)
.then(getAcfData, apiError)
.then(getTaxonomies, apiError)
.then(getWpmlStatus, apiError)
.then(getMetaBoxStatus, apiError)
.then(getMetaBoxData, apiError)
Expand All @@ -234,6 +243,9 @@ angular.module('myApp')
})
.filter('filterType', function () {
return function (list, type) {
if(Array.isArray(type)) {
return _.filter(list, function(obj){ return type.indexOf(obj.type) > -1 })
}
return _.filter(list, function(obj){ return obj.type === type })
}
})
39 changes: 26 additions & 13 deletions frontend/scripts/settings/controllers/add.topics.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';

var PAGINATION_LIMIT = 10

angular.module('myApp')
.controller('AddTopicsCtr', function ($scope, $state, Api, Percolate) {
.controller('AddTopicsCtr', function ($scope, $state, Api, Percolate, Pagination) {
console.log('Add New Channel - Topics state')

/* --------------------------------------
Expand Down Expand Up @@ -55,14 +57,27 @@ angular.module('myApp')

function processPercolateTopics (res) {
console.log('Topics', res.data)
if ($scope.percolateUsers) $scope.stopLoader()
if ($scope.percolateUsers) { $scope.stopLoader() }

if( !res.data || !res.data.data ) {
$scope.showError('There was an error.')
return
}

return $scope.topics = res.data.data
$scope.topics = res.data.data
}

function fetchPercolatUsers(paginationData) {
$scope.showLoader('Loading users from Percolate...')
$scope.percolateUsers = null
return Percolate.getUsersByLicense({
key : $scope.activeChannel.key,
license: $scope.activeChannel.license,
fields : {
limit: (paginationData && paginationData.limit) ? paginationData.limit : PAGINATION_LIMIT,
offset: (paginationData && paginationData.offset) ? paginationData.offset : 0
}
}).then(processPercolateUsers, apiError)
}

function processPercolateUsers(res){
Expand All @@ -73,7 +88,7 @@ angular.module('myApp')
$scope.showError('There was an error.')
return
}

$scope.userPagination = Pagination.build(res.data.pagination)
$scope.percolateUsers = res.data.data
}

Expand Down Expand Up @@ -156,29 +171,27 @@ angular.module('myApp')
* Bootstrap
* -------------------------------------- */


angular.extend($scope, {
fetchPercolatUsers: fetchPercolatUsers
})

// Check if we have the active User
if( !$scope.activeChannel.user ) {
$scope.showError('No active user found.')
$state.go('manage')
}

// Get Percolate topics
$scope.showLoader('Getting data from Percolate...')
$scope.showLoader('Loading data from Percolate...')
Percolate.getTopics({
key : $scope.activeChannel.key,
fields : {
owner_uid: 'license:' + $scope.activeChannel.license
}
}).then(processPercolateTopics, apiError)

Percolate.getUsersByLicense({
key : $scope.activeChannel.key,
license: $scope.activeChannel.license,
fields : {
limit: 100,
offset: 0
}
}).then(processPercolateUsers, apiError)
fetchPercolatUsers()

// Get WP users
Api.getUsers().then(processWpUsers, apiError)
Expand Down
68 changes: 68 additions & 0 deletions frontend/scripts/settings/services/pagination.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';

angular.module('myApp')
.service('Pagination', function() {

function build(paginationData) {
if (!paginationData.total) { return false }
var _pagination = {
pages: Math.floor(paginationData.total/paginationData.limit) + 1,
offsets: [],
activePage: paginationData.offset / paginationData.limit,
}

if (_pagination.pages > 10) {
var _start = 0
if (_pagination.activePage - 4 > 0) {
_start = _pagination.activePage - 4
}

var _end = _start + 8
if (_end > _pagination.pages - 1) {
_end = _pagination.pages - 1
}

for (var i = _start; i < _end; i++) {
_pagination.offsets.push({
label: i+1,
offset: paginationData.limit * i,
limit: paginationData.limit,
active: paginationData.offset === paginationData.limit * i ? true : false
})
}

if ( _pagination.activePage > 4) {
_pagination.prev = {
label: _pagination.activePage - 1,
offset: _start,
limit: paginationData.limit
}
}
if (_pagination.activePage < _pagination.pages - 5) {
_pagination.next = {
label: _pagination.activePage + 1,
offset: _end,
limit: paginationData.limit
}
}

} else {

for (var i = 0; i <_pagination.pages; i++) {
_pagination.offsets.push({
label: i+1,
offset: paginationData.limit * i,
limit: paginationData.limit,
active: paginationData.offset === paginationData.limit * i ? true : false
})
}

}
console.log(_pagination);
return _pagination
}

return {
build : build
}
})
Loading

0 comments on commit f68b351

Please sign in to comment.