feed_type = 'opml'; parent::start(); add_action('admin_init', array($this, 'register_settings')); //add_action( 'about_to_insert_pf_feed_items', array($this, 'subscribe_to_approved_feeds') ); add_action( 'already_a_feed_item', array($this, 'add_folders_to_items') ); //add_action( 'pf_tools', array($this, 'opml_tools') ); if ( isset( $_GET['pf']) && ('opml' == $_GET['pf']) ){ add_action('feed_folders_registered', array($this, 'make_OPML') ); } add_filter( 'pf_tabs_pf-tools', array($this, 'set_permitted_tools_tabs'), 20, 1 ); add_action( 'pf_do_pf-tools_tab_opml', array($this, 'opml_tools') ); } public function set_permitted_tools_tabs( $permitted_tabs ){ $permitted_tabs['opml'] = array( 'title' => __('OPML Link', 'pf'), 'cap' => pf_get_defining_capability_by_role('contributor') ); return $permitted_tabs; } public function set_permitted_feeds_tabs( $permitted_tabs ){ $permitted_tabs['opml'] = array( 'title' => __('OPML as Feed', 'pf'), 'cap' => get_option('pf_menu_feeder_access', pf_get_defining_capability_by_role('editor')) ); return $permitted_tabs; } public function folder_to_slug($folder){ $category = $folder->title; $category = rawurlencode( urldecode( $category ) ); $category = str_replace( '%2F', ' ', $category ); $category = str_replace( '%20', ' ', $category ); $category = str_replace( '/', ' ', $category ); $slug = sanitize_title( basename( $category ) ); return $slug; } public function set_folder_as_term( $folder, $id ){ if ( empty( $folder ) ){ pf_log('Attempting to set a folder with an empty folder object.'); return false; } $slug = $this->folder_to_slug($folder); $tax = pressforward('schema.feeds')->tag_taxonomy; $check = term_exists($slug, strval($tax) ); if ( !empty( $check ) ){ $cat = term_exists($slug, strval($tax) ); $cat_id = $cat['term_id']; } else { $cat = wp_insert_term( $folder->title, pressforward('schema.feeds')->tag_taxonomy, array( 'description' => $folder->text, 'slug' => $slug ) ); if (is_wp_error($cat) ){ pf_log('Insert term with slug of '.$slug.' has failed with the following error:'); pf_log($cat); return; } $cat_id = $cat['term_id']; } $cat_obj = get_term($cat_id, pressforward('schema.feeds')->tag_taxonomy); pf_log('Set category with slug of '.$slug); pf_log('Setting new category for '. $id . ' of ' . $slug . ' with term ID of '.$cat_obj->term_id); $check = wp_set_object_terms( $id, array( $cat_obj->term_id ), pressforward('schema.feeds')->tag_taxonomy, true ); if ( is_wp_error($check) ){ pf_log('Could not add category error:'); pf_log($check); } } public function add_folders_to_items($args){ $item = $args['item']; if (empty($item['obj']) || empty($item['obj']->feedUrl) ){ return $item; } $post_id = $args['post_id']; pf_log('Do something with post ID '.$post_id); $feed_obj = $item['obj']; if ( !method_exists($feed_obj, 'folder') || empty($feed_obj->folder) ){ pf_log('No folder available on'); pf_log($args); return $item; } foreach ($feed_obj->folder as $folder){ $slug = $this->folder_to_slug($folder); if ( !has_category( $slug, $post_id ) ){ pf_log('Add category '.$slug. ' to existing feed '. $post_id); $this->set_folder_as_term($folder, $post_id); } } } /** * This function runs on the post data after it * has been approved for insertion as a 'new' item. * This means that the feed hasn't been passed into the database * before and can safely be attempted to add to the feed list. * * @param array $data [description] * @return [type] [description] */ public function subscribe_to_approved_feeds( $item ){ if ( empty( $item['obj'] ) || empty( $item['obj']->feedUrl ) ){ return $item; } $feed_obj = $item['obj']; if ( empty( $item['parent_feed_id'] ) ){ $parent = 0; } else { $parent = pressforward('controller.metas')->get_post_pf_meta( $item['parent_feed_id'], 'user_added', true ); } $feed_array = array( 'title' => $feed_obj->title, 'url' => $feed_obj->feedUrl, 'htmlUrl' => $feed_obj->htmlUrl, 'type' => 'rss-quick', 'feedUrl' => $feed_obj->feedUrl, 'description' => $feed_obj->text, 'feed_author' => 'OPML', 'feed_icon' => false, 'copyright' => false, 'thumbnail' => false, 'user_added' => $parent, 'post_parent' => $item['parent_feed_id'], 'module_added' => 'opml-subscribe', 'tags' => array(), ); $new_feed_id = pressforward('schema.feeds')->create($feed_obj->feedUrl, $feed_array); pf_log('New feed created with ID of '.$new_feed_id); if ( !method_exists($feed_obj, 'folder') || empty($feed_obj->folder) ){ return $new_feed_id; } //Set up category here. foreach ($feed_obj->folder as $folder){ pf_log('Setting new category for '.$feed_obj->title); $this->set_folder_as_term($folder, $new_feed_id); } return $new_feed_id; } /** * Gets the data from an OPML file and turns it into a data object * as expected by PF * * @global $pf Used to access the feed_object() method */ public function get_data_object($aOPML){ //$feed_obj = new PF_Feeds_Schema(); pf_log( 'Invoked: PF_OPML_Subscribe::get_data_object()' ); $aOPML_id = $aOPML->ID; $aOPML_url = pressforward('controller.metas')->get_post_pf_meta($aOPML_id, 'feedUrl', true); if(empty($aOPML_url) || is_wp_error($aOPML_url) || !$aOPML_url){ $aOPML_url = $aOPML->post_title; pressforward('controller.metas')->update_pf_meta($aOPML_id, 'feedUrl', $aOPML_url); } pf_log( 'Getting OPML Feed at '.$aOPML_url ); $OPML_reader = new OPML_reader($aOPML_url); $opml_object = $OPML_reader->get_OPML_obj(); pf_log('OPML object received to turn into feeds.'); $c = 0; $opmlObject = array(); foreach($opml_object->feeds as $feed_obj){ /** * The Unique ID for this feed. * * Ok, so why don't we use the ->title property of the feed here? * The reason is because a feed could potentially be added by more than * one OPML file. BUT the ->title property is set by the owner of the * OPML file, if it is even set at all. Which means it could be different * across more than one OPML file. But we don't want to add a feed more * than once, so we only use the feedUrl as a unique notifier. * * @var string */ pf_log('Prepping item '.$feed_obj->title); $id = $feed_obj->id; if ( false === ( $rssObject['opml_' . $c] = get_transient( 'pf_opml_' . $id ) ) ) { # Adding this as a 'quick' type so that we can process the list quickly. if ( empty($feed_obj->title) ){ if(!empty($feed_obj->type)){ $feed_obj->type = $feed_obj->type.'-quick'; } else { $feed_obj->type = 'rss-quick'; } } else { $feed_obj->type = 'rss'; } if(!empty($feed_obj->text)){ $contentObj = pressforward('library.htmlchecker'); $feed_obj->text = $contentObj->closetags($feed_obj->text); } if(!empty($feed_obj->title)){ $contentObj = pressforward('library.htmlchecker'); $feed_obj->title = $contentObj->closetags($feed_obj->title); } else { $feed_obj->title = $feed_obj->feedUrl; } $item = array( 'obj' => $feed_obj, 'parent_feed_id' => $aOPML->ID ); $this->subscribe_to_approved_feeds($item); $content = 'Subscribed: ' . $feed_obj->title . ' - ' . $feed_obj->type . ' - ' . $feed_obj->feedUrl . ' on ' . date('r'); $opmlObject['opml_'.$c] = pf_feed_object( $feed_obj->title, 'OPML Subscription from ' . $opml_object->get_title(), date( 'r' ), 'OPML Subscription ' . $opml_object->get_title(), $content, $feed_obj->feedUrl, '', $id, date('r'), 'opml-feed', #tags '', #added '', #repeat '', 'made_readable', $feed_obj ); pf_log('Setting new transient for ' . $feed_obj->feedUrl . ' of ' . $opml_object->get_title() . '.'); set_transient( 'pf_' . $id, $opmlObject['opml_' . $c], 60*10 ); $c++; } } return $opmlObject; } public function add_to_feeder(){ //settings_fields( PF_SLUG . '_opml_group' ); //$feedlist = get_option( PF_SLUG . '_opml_module' ); //
You can share your subscription list as an OPML file by linking people to .