Skip to content

Commit

Permalink
Merge pull request #23 from percolate/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
EEGL authored Jan 31, 2017
2 parents 927f580 + 15fa975 commit 44cdcd6
Show file tree
Hide file tree
Showing 12 changed files with 547 additions and 179 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ available to Percolate clients. The plugin is designed specifically for
WordPress, and includes support for common WordPress concepts like
Custom Post Types, post categories, Media Library, and Featured Images.

It is tested up to WordPress 4.6.1, and is hosted on Github.
It is tested up to WordPress 4.7.2, and is hosted on Github.

#### How it works

Expand Down Expand Up @@ -198,6 +198,12 @@ You should see that:

## Changelog

### 4.x-1.2.0

* WPML support
* PHP 7 support
* Code refactoring

### 4.x-1.1.4

* Fixing post transitioning & logs
Expand Down Expand Up @@ -259,4 +265,4 @@ please refer to the original repository:
***

_Please do not remove this version declaration_
~Current Version:4.x-1.1.4~
~Current Version:4.x-1.2.0~
100 changes: 98 additions & 2 deletions models/percolate-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public function __construct() {
include_once(__DIR__ . '/percolate-acf.php');
$this->ACF = Percolate_ACF_Model::instance();

// WPML methods
include_once(__DIR__ . '/percolate-wpml.php');
$this->Wpml = Percolate_WPML::instance();

// Percolate API methods
include_once(__DIR__ . '/percolate-api.php');
$this->Percolate = Percolate_API_Model::instance();
Expand All @@ -53,6 +57,41 @@ public function __construct() {
// Queue
include_once(__DIR__ . '/percolate-queue.php');
$this->Queue = Percolate_Queue::instance();

// Serve templates to Angular
add_action( 'wp_ajax_template', array( $this, 'getTemplate' ) );
// Get the Percolate data model
add_action( 'wp_ajax_get_data', array( $this, 'getData' ) );
// Save the Percolate data model
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
add_action( 'wp_ajax_get_categories', array( $this, 'getCategories' ) );
// Get WP users
add_action( 'wp_ajax_get_users', array( $this, 'getUsers' ) );
// Is ACF active
add_action( 'wp_ajax_get_acf_status', array( $this, 'getAcfStatus' ) );
// Get ACF data
add_action( 'wp_ajax_get_acf_data', array( $this, 'getAcfData' ) );
// Is WPML active
add_action( 'wp_ajax_get_wpml_status', array( $this, 'getWpmlStatus' ) );
// Get WPML default language
add_action( 'wp_ajax_get_wpml_language', array( $this, 'getWpmlDefaultLanguage' ) );
// Call the Percolate API
add_action( 'wp_ajax_call_percolate', array( $this, 'callPercolateApi' ) );
// Get the warning messages
add_action( 'wp_ajax_get_messages', array( $this, 'getMessages' ) );
// Set the warning messages
add_action( 'wp_ajax_set_messages', array( $this, 'setMessages' ) );
// Get the log
add_action( 'wp_ajax_get_log', array( $this, 'getLog' ) );
// Delete the log
add_action( 'wp_ajax_delete_log', array( $this, 'deleteLog' ) );
// Get the queue
add_action( 'wp_ajax_get_queue', array( $this, 'getQueue' ) );
// Delete the queue
add_action( 'wp_ajax_delete_queue', array( $this, 'deleteQueue' ) );
}

/**
Expand Down Expand Up @@ -106,8 +145,35 @@ public function getCategories()
'hierarchical' => 1,
'taxonomy' => 'category'
);
$res = get_categories( $args );
echo json_encode($res);

if ($this->Wpml->isActive()) {

$categories = array();
$languages = $this->Wpml->getLanguages();

foreach ($languages as $key => $language) {
do_action( 'wpml_switch_language', $key );

$categoriesPerLang = get_categories( $args );

// add language property to categories
foreach ($categoriesPerLang as $category) {
$language_code = apply_filters( 'wpml_element_language_code', null, array(
'element_id'=> (int)$category->term_id,
'element_type'=> 'category'
));
$category->language = $language_code;

$categories[] = $category;
}
}

} else {
$categories = get_categories( $args );
}

// Percolate_Log::log('Categroies: ' . print_r($categories, true));
echo json_encode($categories);
wp_die();
}
/**
Expand Down Expand Up @@ -156,6 +222,36 @@ public function getAcfData()
wp_die();
}

/**
* WPML: check if plugin is active
*/
public function getWpmlStatus()
{
$res = $this->Wpml->isActive();
echo json_encode($res);
wp_die();
}

/**
* WPML: check if plugin is active
*/
public function getWpmlDefaultLanguage()
{
$res = $this->Wpml->getDefaultLanguage();
echo json_encode($res);
wp_die();
}

/**
* WPML: get available languages
*/
public function getWpmlLanguages()
{
$res = $this->Wpml->getLanguages();
echo json_encode($res);
wp_die();
}

/**
* Get the warning messages
*/
Expand Down
3 changes: 3 additions & 0 deletions models/percolate-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public function __construct() {
include_once(__DIR__ . '/percolate-api.php');
$this->Percolate = Percolate_API_Model::instance();

// AJAX endpoint
add_action( 'wp_ajax_image_import', array( $this, 'importImageEndpoint' ) );

// Add the media button
add_action( 'media_buttons', array( $this, 'mediaButtons' ), 20 );

Expand Down
80 changes: 65 additions & 15 deletions models/percolate-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,19 @@ public function __construct() {
// Queue
include_once(__DIR__ . '/percolate-queue.php');
$this->Queue = Percolate_Queue::instance();
// WPML
include_once(__DIR__ . '/percolate-wpml.php');
$this->Wpml = Percolate_WPML::instance();

// Dom Parser plugin
if (!class_exists('simple_html_dom_node')) {
// Percolate_Log::log("simple_html_dom_node isn't present");
require_once( dirname(__DIR__) . '/vendor/simple_html_dom.php' );
}

// AJAX endpoint
add_action( 'wp_ajax_do_import', array( $this, 'importChannelPosts' ) );

}


Expand All @@ -64,7 +71,7 @@ public function importChannelPosts()
{
if( isset($_POST['data']) ) {
$option = json_decode( $this->getChannels() );
$channel = $option->channels->$_POST['data'];
$channel = $option->channels->{$_POST['data']};

$res = $this->processChannel( $channel );
}
Expand Down Expand Up @@ -152,7 +159,7 @@ public function processChannel($channel)
foreach ($schemas as $schema) {

// Get the plugin's template (called channel on the frontend)
$template = $channel->$schema['id'];
$template = $channel->{$schema['id']};

// Flag if there are multiple versions of the schame has been found
$schemaVersionMismatch = false;
Expand Down Expand Up @@ -306,14 +313,16 @@ public function importPost($post, $template, $schema, $channel)

// ------ Check if imported already --------
$args = array(
'post_type' => $template->postType,
'post_status' => 'any',
'meta_key' => 'percolate_id',
'meta_value' => $post['id']
'post_type' => $template->postType,
'post_status' => 'any',
'meta_key' => 'percolate_id',
'meta_value' => $post['id'],
'suppress_filters' => true // need to bypass the WPML language filter
);
// ----------- Post basics --------------
$posts = new WP_Query( $args );
if ( $posts->post_count > 0) {
Percolate_Log::log('Post already imported: ' . $post['id']);
// Delete post if any
// wp_delete_post($posts->posts[0]->ID, true);
$res['success'] = false;
Expand Down Expand Up @@ -378,8 +387,18 @@ public function importPost($post, $template, $schema, $channel)
if( isset($post['topic_ids']) && !empty($post['topic_ids']) ) {
foreach ($post['topic_ids'] as $topic_id) {
$topic_id = str_replace( 'topic:', '', $topic_id );
$category_wp = $channel->topics->$topic_id;
$post_category[] = $category_wp;

if ($this->checkWpml($template) && $channel->topicsWpml == 'on')
{
// Percolate_Log::log('Post with WPML categories' . print_r($channel->{'topicsWPML'.$postLang}, true));
$postLang = $post['ext'][$template->wpmlField];
$category_wp = $channel->{'topicsWPML'.$postLang}->{$topic_id};
$post_category[] = $category_wp;
} else {
$category_wp = $channel->topics->{$topic_id};
$post_category[] = $category_wp;
}

}
}

Expand Down Expand Up @@ -495,8 +514,8 @@ public function importPost($post, $template, $schema, $channel)
// ----- ACF -----
if( isset($template->acf) && $template->acf == 'on' ) {
// Check for mapping
if( isset($template->mapping->$key) && !empty($template->mapping->$key) ) {
$_fieldname = $template->mapping->$key;
if( isset($template->mapping->{$key}) && !empty($template->mapping->{$key}) ) {
$_fieldname = $template->mapping->{$key};
} else {
$_fieldname = false;
}
Expand All @@ -506,8 +525,8 @@ public function importPost($post, $template, $schema, $channel)
else {

// Check for mapping
if( isset($template->mapping->$key) && !empty($template->mapping->$key) ) {
$_fieldname = $template->mapping->$key;
if( isset($template->mapping->{$key}) && !empty($template->mapping->{$key}) ) {
$_fieldname = $template->mapping->{$key};
} else {
$_fieldname = $key;
}
Expand All @@ -524,7 +543,7 @@ public function importPost($post, $template, $schema, $channel)
if( isset($post['term_ids']) && !empty($post['term_ids']) ) {
$res['terms'] = array();
foreach ($post['term_ids'] as $term) {

// Percolate_Log::log('term_id: ' . $term);
// Get term from Percolate
// https://percolate.com/api/v5/term/?ids=term%3A2030798
$key = $channel->key;
Expand All @@ -535,9 +554,16 @@ public function importPost($post, $template, $schema, $channel)
$res_tag = $this->Percolate->callAPI($key, $method, $fields);

if( isset($res_tag['data']) && isset($res_tag['data'][0]['name']) ) {
wp_set_post_tags( $wp_post_id, $res_tag['data'][0]['name'], true );
$termName = $res_tag['data'][0]['name'];
// Percolate_Log::log('term_name: ' . $termName);

// if ($this->checkWpml($template)) {
// $postLang = $post['ext'][$template->wpmlField];
// Percolate_Log::log('Swithcing to: ' . $postLang);
// do_action( 'wpml_switch_language', $postLang);
// }
wp_set_post_tags( $wp_post_id, $termName, true );

$meta_success = update_post_meta($wp_post_id, $_fieldname, $value);
$res['term'][] = 'Adding term: ' . $term;
} else {
$res['term'][] = 'Cannot add term: ' . $term;
Expand All @@ -552,6 +578,26 @@ public function importPost($post, $template, $schema, $channel)
set_post_thumbnail( $wp_post_id, $imageID );
}

// ----------- WPML --------------
if ($this->checkWpml($template)) {
Percolate_Log::log('Post WPML - handling translations for ' . print_r($wp_post_id, true) . '. Language field: ' . $template->wpmlField);

// Get the language from Percolate
$postLang = $post['ext'][$template->wpmlField];
Percolate_Log::log('Post WPML - language: ' . $postLang);

// Set the language code in WPML's table
$set_language_args = array(
'element_id' => $wp_post_id,
'language_code' => $postLang,
'trid' => FALSE // If set to FALSE it will create a new trid for the element
);
do_action( 'wpml_set_element_language_details', $set_language_args );

// Add the original language code, so we can check if it's changed when syncing content
update_post_meta($wp_post_id, 'percolate_language', $postLang);
}

// ----------- All done here --------------
update_post_meta($post['id'], 'import_done', 'yes');
$res['success'] = true;
Expand All @@ -578,6 +624,10 @@ public function deactivateCron(){
wp_clear_scheduled_hook('percolate_transition_posts_event');
}

private function checkWpml($template)
{
return $this->Wpml->isActive() && isset($template->wpmlStatus) && $template->wpmlStatus == 'on' && isset($template->wpmlField);
}

private function searchInArray($array, $key, $value) {
$results = array();
Expand Down
Loading

0 comments on commit 44cdcd6

Please sign in to comment.