syndication icon indicating copy to clipboard operation
syndication copied to clipboard

Add Custom Taxonomy Support && Add automatic Category / Tag mapping

Open sboisvert opened this issue 9 years ago • 0 comments

This is a copy paste of a patch that was worked on for a while with these features. See ZD 27099 for more info:

diff --git a/includes/class-syndication-wp-rss-client.php b/includes/class-syndication-wp-rss-client.php
index d2241cd..d5b1832 100644
--- a/includes/class-syndication-wp-rss-client.php
+++ b/includes/class-syndication-wp-rss-client.php
@@ -10,6 +10,7 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client
     private $default_comment_status;
     private $default_ping_status;
     private $default_cat_status;
+    private $default_tax_import;

     function __construct( $site_ID ) {

@@ -34,6 +35,7 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client
         $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 );
+        $this->default_tax_import       = get_post_meta( $site_ID, 'syn_default_tax_import', 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 );
@@ -78,6 +80,7 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client
         $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 );
+        $default_tax_import         = get_post_meta( $site->ID, 'syn_default_tax_import', true );

         ?>

@@ -146,10 +149,34 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client
         </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>
+            <option value="yes" <?php selected( 'yes', $default_cat_status )  ?> ><?php echo esc_html__( 'import categories', 'push-syndication' ); ?></option>
             </select>
         </p>
+        <p class="tax_import_type">
+            <label for="default_tax_import"><?php echo esc_html__( 'Import as taxonomy', 'push-syndication' ); ?></label>
+        </p>
+        <p class="tax_import_type">
+            <?php
+            $args = array( 'public' => true );
+            $taxonomies = get_taxonomies( $args, 'objects', 'and' );
+            if ( $taxonomies ) :
+                ?>
+                <select name="default_tax_import" id="default_tax_import" />
+                    <option value="existing" <?php selected( 'existing', $default_tax_import )  ?> ><?php echo esc_html__( 'Check for existing', 'push-syndication' ); ?></option>
+                    <?php
+                      foreach ( $taxonomies  as $taxonomy ) :
+                       if ( 'post_format' != $taxonomy->name ) :?>
+                            <option value="<?php echo esc_attr( $taxonomy->name ); ?>" <?php selected( $taxonomy->name, $default_tax_import )  ?> ><?php echo $taxonomy->labels->singular_name; ?></option>
+                  <?php endif;
+                      endforeach;
+                    ?>
+                </select>
+                <?php
+            endif;
+            ?>
+            <p class="tax_import_type"><?php echo esc_html__( '"Check for existing" will see if an imported category exists as a term in any public taxonomy if it does not exist it will be created as a category.', 'push-syndication' ); ?></p>
+        </p>

         <?php

@@ -164,6 +191,7 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client
         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'] );
+        update_post_meta( $site_ID, 'syn_default_tax_import', $_POST['default_tax_import'] );
         return true;

     }
@@ -179,7 +207,7 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client

         // hold all the posts
         $posts = array();
-        $taxonomy = array( 'cats' => array(), 'tags' => array() );
+        $taxonomy = array( 'category' => array(), 'post_tag' => array(), 'taxonomy' => array() );

         foreach( $this->get_items() as $item ) {
             if ( 'yes' == $this->default_cat_status ) {
@@ -196,9 +224,11 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client
                 'comment_status'    => $this->default_comment_status,
                 'ping_status'       => $this->default_ping_status,
                 'post_guid'         => $item->get_id(),
-                'post_category'     => $taxonomy['cats'],
-                'tags_input'        => $taxonomy['tags']
+                'post_category'     => $taxonomy['category'],
+                'tags_input'        => $taxonomy['post_tag'],
+                'tax_input'            => $taxonomy['tax']
             );
+
             // 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 );
             if ( false === $post )
@@ -209,30 +239,62 @@ 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()
+            'category'    => array(),
+            'post_tag'    => array(),
+            'tax'     => array()
         );

-        foreach ( $cats as $cat ) {
-            // checks if term exists
-            if ( $result = get_term_by( 'name', $cat->term, 'category' ) ) {
-                if ( isset( $result->term_id ) ) {
-                    $ids['cats'][] = $result->term_id;
+        // get taxonomies
+        $args = array( 'public' => true );
+        $taxonomies = get_taxonomies( $args, 'objects', 'and' );
+        $tax = 'category';
+
+        if ( 'existing' == $this->default_tax_import ) {
+            foreach ( $cats as $cat ) {
+                // checks if term exists
+                foreach( $taxonomies as $taxonomy ) {
+                    $term = term_exists($cat->term,  $taxonomy->name);
+                    if ( ! is_null( $term ) ) {
+                        $tax =  $taxonomy->name;
+                    }
+                }
+
+                // checks if term exists
+                if ( ! $result = get_term_by( 'name', $cat->term, $tax ) ) {
+                    // if neither exist creates category
+                    $result = wp_insert_term( $cat->term, 'category' );
                 }
-            } elseif ( $result = get_term_by( 'name', $cat->term, 'post_tag' ) ) {
+
+                // sets ids for terms and tags
                 if ( isset( $result->term_id ) ) {
-                    $ids['tags'][] = $result->term_id;
-                }                    
-            } else {
-                // creates if not
-                $result = wp_insert_term( $cat->term, 'category' );
+                    $ids['tax'][$tax][] = $result->term_id;
+                }
+
+            }
+        } else {
+            foreach ( $cats as $cat ) {
+                if ( 'category' == $this->default_tax_import || 'post_tag' == $this->default_tax_import ) {
+                    $tax = $this->default_tax_import;
+                } else {
+                    $tax = 'tax';
+                }
+
+                // checks to see if term exists, creates it if it does.
+                if ( ! $result = get_term_by( 'name', $cat->term, $this->default_tax_import ) ) {
+                    $result = wp_insert_term( $cat->term, $this->default_tax_import );
+                }
+
                 if ( isset( $result->term_id ) ) {
-                    $ids['cats'][] = $result->term_id;
+                    $ids[$tax][] = ( is_taxonomy_hierarchical( $this->default_tax_import ) ) ? $result->term_id : $result->name;
                 }
+
+            }
+            if ( $tax == 'tax' ) {
+                $ids[$tax] = array( $this->default_tax_import => implode( ',', $ids[$tax] ) );
             }
         }

sboisvert avatar Jan 12 '15 17:01 sboisvert