Automattic / Automattic/syndication
Add Custom Taxonomy Support && Add automatic Category / Tag mapping
- Dominant language
- PHP
- Stars
- 107
- Forks
- 44
- Avg merge
- 18m
- Merged PRs (30d)
- 19
Description
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
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
- >
>
+ >
+
+
+
+
+ true );
+ $taxonomies = get_taxonomies( $args, 'objects', 'and' );
+ if ( $taxonomies ) :
+ ?>
+
+ >
+ name ) :?>
+ name, $default_tax_import ) ?> >labels->singular_name; ?>
+
+
+
+
+
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] ) );
}
}
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with includes/class-syndication-wp-rss-client.php and compare the pasted diff with the current settings and taxonomy flow. Trace how imported categories are mapped to existing or selected public taxonomies, then verify that the configuration is saved and applied during pulls; no test file is named in the issue.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend, content
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100