Skip to content
Snippets Groups Projects
Commit 6628695d authored by Jack Reichert's avatar Jack Reichert
Browse files

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.
parent 67bafc7e
No related branches found
No related tags found
No related merge requests found
...@@ -9,7 +9,8 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client ...@@ -9,7 +9,8 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client
private $default_post_status; private $default_post_status;
private $default_comment_status; private $default_comment_status;
private $default_ping_status; private $default_ping_status;
private $default_cat_status;
function __construct( $site_ID ) { function __construct( $site_ID ) {
switch( SIMPLEPIE_VERSION ) { switch( SIMPLEPIE_VERSION ) {
...@@ -32,6 +33,7 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client ...@@ -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_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_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_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_meta' ), 10, 5 );
add_action( 'syn_post_pull_new_post', array( __CLASS__, 'save_tax' ), 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 ...@@ -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_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_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_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 ...@@ -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> <option value="closed" <?php selected( 'closed', $default_ping_status ) ?> >closed</option>
</select> </select>
</p> </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 <?php
...@@ -151,6 +163,7 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client ...@@ -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_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_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_ping_status', $_POST['default_ping_status'] );
update_post_meta( $site_ID, 'syn_default_cat_status', $_POST['default_cat_status'] );
return true; return true;
} }
...@@ -166,8 +179,13 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client ...@@ -166,8 +179,13 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client
// hold all the posts // hold all the posts
$posts = array(); $posts = array();
$taxonomy = array( 'cats' => array(), 'tags' => array() );
foreach( $this->get_items() as $item ) { foreach( $this->get_items() as $item ) {
if ( 'yes' == $this->default_cat_status ) {
$taxonomy = $this->set_taxonomy( $item );
}
$post = array( $post = array(
'post_title' => $item->get_title(), 'post_title' => $item->get_title(),
'post_content' => $item->get_content(), 'post_content' => $item->get_content(),
...@@ -177,7 +195,9 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client ...@@ -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() ) ), 'post_date' => date( 'Y-m-d H:i:s', strtotime( $item->get_date() ) ),
'comment_status' => $this->default_comment_status, 'comment_status' => $this->default_comment_status,
'ping_status' => $this->default_ping_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 // 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 ); $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 ...@@ -189,6 +209,38 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client
return $posts; 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 ) { public static function save_meta( $result, $post, $site, $transport_type, $client ) {
if ( ! $result || is_wp_error( $result ) || ! isset( $post['postmeta'] ) ) { if ( ! $result || is_wp_error( $result ) || ! isset( $post['postmeta'] ) ) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment