Advertisement
Guest User

hashtag to tag

a guest
Jan 26th, 2011
3,008
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.21 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @package YD_feedwordpress_content_filter
  4.  * @author Yann Dubois
  5.  * @version 0.2.0
  6.  */
  7.  
  8. /*
  9.  Plugin Name: YD Feedwordpress content filter
  10.  Plugin URI: http://www.yann.com/en/wp-plugins/yd-feedwordpress-content-filter
  11.  Description: Automatically filters the content of syndicated posts. | Funded by <a href="http://www.abc.fr">ABC.fr</a>
  12.  Version: 0.2.0
  13.  Author: Yann Dubois
  14.  Author URI: http://www.yann.com/
  15.  License: GPL2
  16.  */
  17.  
  18. /**
  19.  * @copyright 2010  Yann Dubois  ( email : yann _at_ abc.fr )
  20.  *
  21.  *  Original development of this plugin was kindly funded by http://www.abc.fr
  22.  *  
  23.  *  Additional developments kindly provided by Alessandro Nuzzo ( http://www.e-one.it )
  24.  *
  25.  *  This program is free software; you can redistribute it and/or modify
  26.  *  it under the terms of the GNU General Public License as published by
  27.  *  the Free Software Foundation; either version 2 of the License, or
  28.  *  (at your option) any later version.
  29.  *
  30.  *  This program is distributed in the hope that it will be useful,
  31.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  32.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  33.  *  GNU General Public License for more details.
  34.  *
  35.  *  You should have received a copy of the GNU General Public License
  36.  *  along with this program; if not, write to the Free Software
  37.  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  38.  */
  39.  
  40. /**
  41.  Revision 0.1.0:
  42.  - Original beta release
  43.  Revision 0.2.0:
  44.  - Refactored using the YD Plugins Framework
  45.  - Added some options
  46.  - Added image attachment hack contributed by Alessandro Nuzzo
  47.  - Added redirected URL resolution
  48.  - Got rid of unused code
  49.  - Small bug fixes
  50.  */
  51.  
  52. include_once( 'inc/yd-widget-framework.inc.php' );
  53.  
  54. $junk = new YD_Plugin(
  55.     array(
  56.         'name'              => 'YD Feedwordpress Content Filter',
  57.         'version'           => '0.2.0',
  58.         'has_option_page'   => true,
  59.         'has_shortcode'     => false,
  60.         'has_widget'        => false,
  61.         'widget_class'      => '',
  62.         'has_cron'          => false,
  63.         'crontab'           => array(
  64.             'daily'         => array( 'yd_fwp_filter', 'daily_update' ),
  65.             'hourly'        => array( 'yd_fwp_filter', 'hourly_update' )
  66.         ),
  67.         'has_stylesheet'    => false,
  68.         'stylesheet_file'   => 'css/yd.css',
  69.         'has_translation'   => false,
  70.         'translation_domain'=> '', // must be copied in the widget class!!!
  71.         'translations'      => array(
  72.             array( 'English', 'Yann Dubois', 'http://www.yann.com/' )
  73.         ),
  74.         'initial_funding'   => array( 'Yann.com', 'http://www.yann.com' ),
  75.         'additional_funding'=> array(),
  76.         'form_blocks'       => array(
  77.             'Main options' => array(
  78.                 'google_filter' => 'bool',
  79.                 'yahoo_filter'  => 'bool',
  80.                 'wikio_filter'  => 'bool',
  81.                 'local_images'  => 'bool',
  82.                 'fix_attach'    => 'bool',
  83.                 'fix_first'     => 'bool',
  84.                 'fix_redirect'  => 'bool',
  85.                 'hash_to_tags'  => 'bool'   // Hashtag
  86.             )
  87.         ),
  88.         'option_field_labels'=>array(
  89.                 'google_filter' => 'Apply Google News item filter',
  90.                 'yahoo_filter'  => 'Apply Yahoo News item filter',
  91.                 'wikio_filter'  => 'Apply Wikio News item filter',
  92.                 'local_images'  => 'Import images as local attachments',
  93.                 'fix_attach'    => 'Fix attachment IDs',
  94.                 'fix_first'     => 'Fix repeated title on first line',
  95.                 'fix_redirect'  => 'Resolve redirected source URL',
  96.                 'hash_to_tags'  => 'Twitter Hashtags to tags' // Hashtag
  97.         ),
  98.         'option_defaults'   => array(
  99.                 'google_filter' => 1,
  100.                 'yahoo_filter'  => 1,
  101.                 'wikio_filter'  => 1,
  102.                 'local_images'  => 1,
  103.                 'fix_attach'    => 1,
  104.                 'fix_first' => 1,
  105.                 'fix_redirect'  => 1,
  106.                 'hash_to_tags'  => 1,   // Hashtag
  107.         ),
  108.         'form_add_actions'  => array(
  109.             // no custom actions
  110.         ),
  111.         'has_cache'         => false,
  112.         'option_page_text'  => '',
  113.         'backlinkware_text' => 'Featuring YD Feedwordpress Content Filter Plugin',
  114.         'plugin_file'       => __FILE__
  115.     )
  116. );
  117.  
  118. class yd_fwp_filter {
  119.     const option_key = 'yd-feedwordpress-content-filter';
  120.    
  121.     function __construct() {
  122.         $this->yd_fwp_filter();
  123.     }
  124.     function yd_fwp_filter() {
  125.         $options = get_option( self::option_key );
  126.         add_action( 'syndicated_post', array( &$this, 'filter' ), 10, 1 );
  127.         if( $options['fix_attach'] ) {
  128.             add_action( 'post_syndicated_item', array( &$this, 'fixAttach' ), 20, 1 );
  129.             add_action( 'update_syndicated_item', array( &$this, 'fixAttach' ), 20, 1 );
  130.         }
  131.     }
  132.     function filter( $post ) {
  133.         if( !$post ) return $post;
  134.         $options = get_option( self::option_key );
  135.        
  136.         $content = $post['post_content'];
  137.         $title = $post['post_title'];
  138.        
  139.         $stripped_content = strip_tags( $content, '<i><sup><sub><strong><u><br><p><img>' ); //<b><em>
  140.         $stripped_content = preg_replace( '|^(<[^>]*>)*(<br\s*\/?>)+|i', "$1", $stripped_content ); // no <br> at beginning (Google)
  141.         $stripped_content = preg_replace( '|<img[^src]+>|i', '', $stripped_content ); // no sourceless <img> (Google)
  142.         $stripped_content = preg_replace( '|(<p\s*>)*\s*et plus encore&nbsp;&raquo;\s*(</p\s*>)*|ims', '', $stripped_content );
  143.  
  144.         // Hashtag
  145.         if( $options['hash_to_tags'] ) {
  146.             // find hashtags
  147.             preg_match_all( '/\B#(\w*[a-zA-Z-]+\w*)/', $content, $matches );
  148.             $hashtags = $matches[0]; // #bleh
  149.             $tags = $matches[1];     //  bleh
  150.             unset( $matches );     
  151.             global $id;
  152.             wp_set_post_tags( $id, implode( ', ', $tags ) );
  153.         }
  154.         // i nice try ^^
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement