From 6628695d134d070b85dee243fce14cce3ed28dec Mon Sep 17 00:00:00 2001
From: Jack Reichert <contact@jackreichert.com>
Date: Sat, 17 May 2014 12:23:41 -0400
Subject: [PATCH] Adds Category (and tag) Mapping

Notes on the patch:
Simplepie doesn't pull attributes, so there is no real way to
differentiate currently between categories and tags. My solution was to
check first for existing categories, if not exist, tags, if not exist
create as a category.
---
 includes/class-syndication-wp-rss-client.php | 56 +++++++++++++++++++-
 1 file changed, 54 insertions(+), 2 deletions(-)

diff --git a/includes/class-syndication-wp-rss-client.php b/includes/class-syndication-wp-rss-client.php
index c986afb..3f1e7d4 100644
--- a/includes/class-syndication-wp-rss-client.php
+++ b/includes/class-syndication-wp-rss-client.php
@@ -9,7 +9,8 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client
     private $default_post_status;
     private $default_comment_status;
     private $default_ping_status;
-
+	private $default_cat_status;
+	
     function __construct( $site_ID ) {
 
         switch( SIMPLEPIE_VERSION ) {
@@ -32,6 +33,7 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client
         $this->default_post_status      = get_post_meta( $site_ID, 'syn_default_post_status', true );
         $this->default_comment_status   = get_post_meta( $site_ID, 'syn_default_comment_status', true );
         $this->default_ping_status      = get_post_meta( $site_ID, 'syn_default_ping_status', true );
+		$this->default_cat_status		= get_post_meta( $site_ID, 'syn_default_cat_status', true );		
 
         add_action( 'syn_post_pull_new_post', array( __CLASS__, 'save_meta' ), 10, 5 );
         add_action( 'syn_post_pull_new_post', array( __CLASS__, 'save_tax' ), 10, 5 );
@@ -75,6 +77,7 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client
         $default_post_status        = get_post_meta( $site->ID, 'syn_default_post_status', true );
         $default_comment_status     = get_post_meta( $site->ID, 'syn_default_comment_status', true );
         $default_ping_status        = get_post_meta( $site->ID, 'syn_default_ping_status', true );
+		$default_cat_status			= get_post_meta( $site->ID, 'syn_default_cat_status', true );
 
         ?>
 
@@ -138,6 +141,15 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client
             <option value="closed" <?php selected( 'closed', $default_ping_status )  ?> >closed</option>
             </select>
         </p>
+		<p>
+			<label for="default_cat_status"><?php echo esc_html__( 'Select category status', 'push-syndication' ); ?></label>
+		</p>
+		<p>
+			<select name="default_cat_status" id="default_cat_status" />
+			<option value="yes" <?php selected( 'yes', $default_cat_status )  ?> ><?php echo esc_html__( 'import categories', 'push-syndication' ); ?></option>
+			<option value="no" <?php selected( 'no', $default_cat_status )  ?> ><?php echo esc_html__( 'ignore categories', 'push-syndication' ); ?></option>
+			</select>
+		</p>
 
         <?php
 
@@ -151,6 +163,7 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client
         update_post_meta( $site_ID, 'syn_default_post_status', $_POST['default_post_status'] );
         update_post_meta( $site_ID, 'syn_default_comment_status', $_POST['default_comment_status'] );
         update_post_meta( $site_ID, 'syn_default_ping_status', $_POST['default_ping_status'] );
+        update_post_meta( $site_ID, 'syn_default_cat_status', $_POST['default_cat_status'] );		
         return true;
 
     }
@@ -166,8 +179,13 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client
 
         // hold all the posts
         $posts = array();
+		$taxonomy = array( 'cats' => array(), 'tags' => array() );
 
         foreach( $this->get_items() as $item ) {
+			if ( 'yes' == $this->default_cat_status ) {
+            	$taxonomy = $this->set_taxonomy( $item );
+			}
+			
             $post = array(
                 'post_title'        => $item->get_title(),
                 'post_content'      => $item->get_content(),
@@ -177,7 +195,9 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client
                 'post_date'         => date( 'Y-m-d H:i:s', strtotime( $item->get_date() ) ),
                 'comment_status'    => $this->default_comment_status,
                 'ping_status'       => $this->default_ping_status,
-                'post_guid'         => $item->get_id()
+                'post_guid'         => $item->get_id(),
+				'post_category'     => $taxonomy['cats'],
+                'tags_input'        => $taxonomy['tags']
             );
 			// This filter can be used to exclude or alter posts during a pull import
 			$post = apply_filters( 'syn_rss_pull_filter_post', $post, $args, $item );
@@ -189,6 +209,38 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client
         return $posts;
 
     }
+	
+	public function set_taxonomy( $item ) {
+		$cats = $item->get_categories();
+		$ids = array(
+			'cats'	=> array(),
+			'tags'			=> array()
+		);
+
+		foreach ( $cats as $cat ) {
+			// checks if term exists
+			if ( ! $result = get_term_by( 'name', $cat->term, 'category' ) ) {
+				if ( ! $result = get_term_by( 'name', $cat->term, 'post_tag' ) ) {
+					// creates if not
+					$result = wp_insert_term( $cat->term, 'category' );
+					if ( isset( $result->term_id ) ) {
+						$ids['cats'][] = $result->term_id;
+					}
+				} else {
+					if ( isset( $result->term_id ) ) {
+						$ids['tags'][] = $result->term_id;
+					}
+				}
+			} else {
+				if ( isset( $result->term_id ) ) {
+					$ids['cats'][] = $result->term_id;
+				}
+			}               
+		}
+
+		// returns array ready for post creation
+		return $ids;
+	}
     
     public static function save_meta( $result, $post, $site, $transport_type, $client ) {
         if ( ! $result || is_wp_error( $result ) || ! isset( $post['postmeta'] ) ) {
-- 
GitLab