WordPress.org

Plugin Directory

Changeset 543562


Ignore:
Timestamp:
05/13/12 01:13:58 (2 years ago)
Author:
jeremyfelt
Message:

Update Youtube Favorite Video Posts to version 1.0

See change log in read me for all the awesomeness...

Location:
youtube-favorite-video-posts/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • youtube-favorite-video-posts/trunk/readme.txt

    r490004 r543562  
    55Tags: youtube, custom post type, embed, video, rss, feed 
    66Requires at least: 3.2.1 
    7 Tested up to: 3.3 
    8 Stable tag: 0.3 
     7Tested up to: 3.4 
     8Stable tag: 1.0 
    99 
    1010YouTube Favorite Video Posts grabs videos you mark as favorites in YouTube and adds them to WordPress under a custom post type. 
     
    2626    * If you aren't a regular YouTube favoriter, you may want to reduce this so that your server doesn't process the same items over and over again. 
    2727* Post Type 
    28     * By default a new custom post type for YouTube favorites has been added. You can change this to any of your other custom post types. 
     28    * By default a new custom post type for YouTube favorites has been added. You can change this to any of your other custom post types or the WordPress default 'post'. 
    2929* Default Post Status 
    3030    * Choose to publish the new posts immediately, or save them as drafts for later processing. 
    3131* Feed Fetch Interval 
    32     * Defaults to hourly, but can be changed to either daily or twice daily. 
     32    * Defaults to hourly, but can be changed to any interval registered with your WordPress setup. 
     33 
     34Filters are available for: 
     35 
     36* 'yfvp_new_video_embed_code' - Alter or replace the video embed code before the new post is created 
     37* 'yfvp_new_video_item_title' - Alter or replace the video title before the new post is created 
     38 
     39Checkout the [example code for the new filters] (http://jeremyfelt.com/wordpress/2012/05/12/filters-in-youtube-favorite-video-posts). 
    3340 
    3441== Installation == 
     
    5158 
    5259== Changelog == 
     60= 1.0 = 
     61 
     62* Add filters to allow other themes and plugins to change the post content and title before saving 
     63* Allow the selection of any registered interval for Cron tasks 
     64* Better internationalization, everything is now attached to the youtube-favorite-video-posts text domain 
     65* Better handling of events when Youtube name has not yet been provided 
     66* Code cleanup, move everything to a class 
     67* Front and back end documentation cleanup 
     68* Confirmed support for upcoming WordPress 3.4 release 
     69 
    5370= 0.3 = 
    5471 
     
    6582 
    6683== Upgrade Notice == 
     84= 1.0 = 
     85 
     86* A bunch of great updates. Definitely upgrade. 
     87 
    6788= 0.3 = 
    6889 
  • youtube-favorite-video-posts/trunk/uninstall.php

    r482629 r543562  
    1414/*  Delete the afip_options */ 
    1515delete_option( 'jf_yfvp_options' ); 
    16  
    17 ?> 
  • youtube-favorite-video-posts/trunk/youtube-favorite-video-posts.php

    r490004 r543562  
    44Plugin URI: http://www.jeremyfelt.com/wordpress/plugins/youtube-favorite-video-posts 
    55Description: Checks your YouTube favorite videos RSS feed and creates new posts in a custom post type. 
    6 Version: 0.3 
     6Version: 1.0 
    77Author: Jeremy Felt 
    88Author URI: http://www.jeremyfelt.com 
     
    2626*/ 
    2727 
    28 /*  Things happen when we activate and deactivate the plugin of course. */ 
    29 register_activation_hook( __FILE__, 'jf_yfvp_plugin_activation' ); 
    30 register_deactivation_hook( __FILE__, 'jf_yfvp_plugin_deactivation' ); 
    31  
    32 /*  The stuff we only want to do as an admin. */ 
    33 if ( is_admin() ){ 
    34     /*  Add a fancy icon to the custom post type page in wp-admin. */ 
    35     add_action( 'admin_head', 'jf_yfvp_edit_icon' ); 
    36     /*  Add our custom settings to the admin menu. */ 
    37     add_action( 'admin_menu', 'jf_yfvp_add_settings' ); 
    38     /*  Register all of the custom settings. */ 
    39     add_action( 'admin_init', 'jf_yfvp_register_settings' ); 
    40     /*  Make a pretty link for settings under the plugin information. */ 
    41     add_filter( 'plugin_action_links', 'jf_yfvp_plugin_action_links', 10, 2); 
     28class Youtube_Favorite_Video_Posts_Foghlaim { 
     29 
     30    public function __construct() { 
     31        /*  Things happen when we activate and deactivate the plugin of course. */ 
     32        register_activation_hook( __FILE__, array( $this, 'activate_plugin' ) ); 
     33        register_deactivation_hook( __FILE__, array( $this, 'deactivate_plugin' ) ); 
     34 
     35        /*  Make a pretty link for settings under the plugin information. */ 
     36        add_filter( 'plugin_action_links', array( $this, 'add_plugin_action_links' ), 10, 2 ); 
     37 
     38        /*  Add our custom settings to the admin menu. */ 
     39        add_action( 'admin_head', array( $this, 'edit_admin_icon' ) ); 
     40        add_action( 'admin_menu', array( $this, 'add_settings' ) ); 
     41        add_action( 'admin_init', array( $this, 'register_settings' ) ); 
     42 
     43        /* Register the jf_yfvp_youtube custom post type */ 
     44        add_action( 'init', array( $this, 'create_content_type' ) ); 
     45 
     46        /* Our hook added when we schedule a WP Cron event */ 
     47        add_action( 'jf_yfvp_process_feed', array( $this, 'process_feed' ) ); 
     48    } 
     49 
     50    /** 
     51     * When activating the plugin, register our custom post type and flush the rewrite 
     52     * rules if the option to use our custom post type has been selected. 
     53     */ 
     54    public function activate_plugin(){ 
     55        /*  Create the custom post type upon activation. */ 
     56        $this->create_content_type(); 
     57 
     58        $current_options = get_option( 'jf_yfvp_options', array() ); 
     59        $valid_fetch_intervals = wp_get_schedules(); 
     60 
     61        /* If the custom post type provided by this plugin is selected, flush the rewrite 
     62         * rules so that the URLs can be pretty */ 
     63        if ( isset( $current_options[ 'post_type' ] ) && 'jf_yfvp_youtube' == $current_options[ 'post_type' ] ) 
     64            flush_rewrite_rules( false ); 
     65 
     66        /* If a fetch interval has previously been selected, use that. Otherwise, we'll not schedule the event until settings save. */ 
     67        if ( isset( $current_options[ 'fetch_interval' ] ) && in_array( $current_options[ 'fetch_interval' ], $valid_fetch_intervals ) ) 
     68            wp_schedule_event( ( time() + 120 ) , $current_options[ 'fetch_interval' ], 'jf_yfvp_process_feed' ); 
     69 
     70    } 
     71 
     72    /** 
     73     * When the plugin is deactivated, we want to make sure that the WP Cron event 
     74     * we have scheduled is cleared. 
     75     */ 
     76    public function deactivate_plugin(){ 
     77        wp_clear_scheduled_hook( 'jf_yfvp_process_feed' ); 
     78    } 
     79 
     80    /** 
     81     * Add a link for the plugin settings page when viewing the general plugins display. 
     82     * 
     83     * Function gratefully borrowed from Pippin Williamson's WPMods article: 
     84     * http://www.wpmods.com/adding-plugin-action-links/ 
     85     * 
     86     * @param $links array Current array of links to be displayed under the plugin 
     87     * @param $file string The current plugin file being processed 
     88     * @return array New array of links to be displayed under the plugin 
     89     */ 
     90    public function add_plugin_action_links( $links, $file ){ 
     91        static $this_plugin; 
     92 
     93        if ( ! $this_plugin ) 
     94            $this_plugin = plugin_basename( __FILE__ ); 
     95 
     96        if ( $file == $this_plugin ) { 
     97            $settings_link = '<a href="' . site_url( '/wp-admin/options-general.php?page=youtube-favorite-video-posts-settings' ) . '">' . __( 'Settings', 'youtube-favorite-video-posts' ) . '</a>'; 
     98            array_unshift( $links, $settings_link ); 
     99        } 
     100        return $links; 
     101    } 
     102 
     103    /** 
     104     * Add some style to the plugin with a YouTube icon at the top of the page. 
     105     */ 
     106    public function edit_admin_icon(){ 
     107        global $post_type; 
     108        if ( 'jf_yfvp_youtube' == $post_type ) 
     109            echo '<style>#icon-edit { background: url("' . plugins_url( 'images/youtube-icon-32.png', __FILE__ ) . '") no-repeat; background-size: 32px 32px; }</style>'; 
     110    } 
     111 
     112    /** 
     113     * Add the sub-menu item under the Settings top-level menu. 
     114     */ 
     115    public function add_settings(){ 
     116        add_options_page( __('YouTube Favorites', 'youtube-favorite-video-posts' ), __('YouTube Favorites', 'youtube-favorite-video-posts'), 'manage_options', 'youtube-favorite-video-posts-settings', array( $this, 'view_settings' ) ); 
     117    } 
     118 
     119    /** 
     120     * Display the main settings view for Youtube Favorite Video Posts 
     121     */ 
     122    public function view_settings(){ 
     123        ?> 
     124        <div class="wrap"> 
     125            <div class="icon32" id="icon-options-general"></div> 
     126            <h2><?php _e( 'YouTube Favorite Video Posts', 'youtube-favorite-video-posts' ); ?></h2> 
     127            <h3><?php _e( 'Overview', 'youtube-favorite-video-posts' ); ?>:</h3> 
     128            <p style="margin-left:12px; max-width:640px;"><?php _e( 'The settings below will help determine where to check for your favorite YouTube videos, how often to look for them, and how they should be stored once new items are found.', 'youtube-favorite-video-posts' ); ?></p> 
     129            <p style="margin-left:12px; max-width:640px;"><?php _e( 'The most important part of this process will be to determine the RSS feed for your favorite YouTube videos. To do this, your username <strong>must</strong> be filled out below. This can usually be found in the upper right hand corner of <a href="http://www.youtube.com">YouTube.com</a>.', 'youtube-favorite-video-posts' ); ?></p> 
     130            <ol style="margin-left:36px;"> 
     131                <li><?php _e( 'Username must be filled in below. Email address will not work.', 'youtube-favorite-video-posts' ); ?></li> 
     132                <li><?php _e( 'The embed width and height settings will be applied to the iframe in your post content.', 'youtube-favorite-video-posts' ); ?></li> 
     133                <li><?php _e( 'If you would like to change the content or title before the new content is saved, you may be interested in the <a href="http://jeremyfelt.com/wordpress/2012/05/12/filters-in-youtube-favorite-video-posts/">available filters</a>.', 'youtube-favorite-video-posts' ); ?></li> 
     134            </ol> 
     135            <form method="POST" action="options.php"> 
     136                <?php 
     137                settings_fields( 'jf_yfvp_options' ); 
     138                do_settings_sections( 'jf_yfvp' ); 
     139                ?> 
     140                <p class="submit"><input type="submit" class="button-primary" value="<?php _e( 'Save Changes', 'youtube-favorite-video-posts' ); ?>" /></p> 
     141            </form> 
     142        </div> 
     143        <?php 
     144    } 
     145 
     146    /** 
     147     * Register the settings, sections, and fields that we want made available for 
     148     * the plugin. 
     149     */ 
     150    public function register_settings(){ 
     151        register_setting( 'jf_yfvp_options', 'jf_yfvp_options', array( $this, 'validate_options' ) ); 
     152        add_settings_section( 'jf_yfvp_section_main', '', array( $this, 'main_section_text' ), 'jf_yfvp' ); 
     153        add_settings_section( 'jf_yfvp_section_post_type', '', array( $this, 'post_type_section_text' ), 'jf_yfvp' ); 
     154        add_settings_section( 'jf_yfvp_section_interval', '', array( $this, 'interval_section_text' ), 'jf_yfvp' ); 
     155        add_settings_field( 'jf_yfvp_youtube_rss_feed', 'YouTube Username:', array( $this, 'youtube_user_text' ), 'jf_yfvp', 'jf_yfvp_section_main' ); 
     156        add_settings_field( 'jf_yfvp_embed_width', 'Default Embed Width:', array( $this, 'embed_width_text' ), 'jf_yfvp', 'jf_yfvp_section_main' ); 
     157        add_settings_field( 'jf_yfvp_embed_height', 'Default Embed Height:', array( $this, 'embed_height_text' ), 'jf_yfvp', 'jf_yfvp_section_main' ); 
     158        add_settings_field( 'jf_yfvp_max_fetch_items', 'Max Items To Fetch:', array( $this, 'max_fetch_items_text' ), 'jf_yfvp', 'jf_yfvp_section_main' ); 
     159        add_settings_field( 'jf_yfvp_post_type', 'Post Type:', array( $this, 'post_type_selection_text' ), 'jf_yfvp', 'jf_yfvp_section_post_type' ); 
     160        add_settings_field( 'jf_yfvp_post_status', __( 'Default Post Status:', 'youtube-favorite-video-posts' ) , array( $this, 'post_status_selection_text' ), 'jf_yfvp', 'jf_yfvp_section_post_type' ); 
     161        add_settings_field( 'jf_yfvp_fetch_interval', 'Feed Fetch Interval: ', array( $this, 'fetch_interval_selection_text' ), 'jf_yfvp', 'jf_yfvp_section_interval' ); 
     162    } 
     163 
     164    /** 
     165     * Always seems weird to have to include this. 
     166     */ 
     167    public function main_section_text() { } 
     168 
     169    /** 
     170     * Describe the selection of a post type for the plugin to use. 
     171     */ 
     172    public function post_type_section_text() { 
     173        ?> 
     174        <h3>Custom Or Default Post Type</h3> 
     175        <p style="margin-left:12px; max-width: 640px;"><?php _e( 'A new custom post type that adds an \'youtube\' slug to new items has been added and selected by default. You can change this to any other available post type if you would like.', 'youtube-favorite-video-posts' ); ?></p> 
     176        <?php 
     177    } 
     178 
     179    /** 
     180     * Describe the selection of the WP Cron interval we'll use 
     181     */ 
     182    public function interval_section_text() { 
     183 
     184        $next_scheduled_time = wp_next_scheduled( 'jf_yfvp_process_feed' ); 
     185 
     186        if ( $next_scheduled_time ) { 
     187            $next_scheduled_time = $next_scheduled_time  + ( get_option( 'gmt_offset' ) * 3600 ); 
     188            $user_current_time = time() + ( get_option( 'gmt_offset' ) * 3600 ); 
     189            $time_till_cron = human_time_diff( $user_current_time, $next_scheduled_time ); 
     190            $next_cron_date = date( 'g:i:sa', $next_scheduled_time ); 
     191            ?> 
     192            <h3>RSS Fetch Frequency</h3> 
     193            <p style="margin-left:12px; max-width: 630px;"><?php _e( 'This plugin currently depends on WP Cron operating fully as expected. In most cases, you should be able to select one of the intervals below and things will work. If not, please let <a href="http://www.jeremyfelt.com">me</a> know. By default, we check for new items on an hourly basis.', 'youtube-favorite-video-posts' ); ?></p> 
     194            <p style="margin-left:12px; max-width: 630px;"><?php printf( __( 'Your Youtube favorites feed is scheduled to be loaded next in %1$s, at %2$s.', 'youtube-favorite-video-posts' ),$time_till_cron, $next_cron_date ); ?></p> 
     195            <?php 
     196        } else { 
     197            ?> 
     198            <h3>RSS Fetch Frequency</h3> 
     199            <p style="margin-left:12px; max-width: 630px;"><?php _e( 'An interval has not yet been saved. Please select the frequency with which you would like this plugin to check your favorite video feed. The default of Once Hourly will be used if you do not change the interval before saving.', 'youtube-favorite-video-posts' ); ?></p> 
     200            <?php 
     201        } 
     202    } 
     203 
     204    /** 
     205     * Provide an input for the embed width 
     206     */ 
     207    public function embed_width_text() { 
     208        $jf_yfvp_options = get_option( 'jf_yfvp_options', array() ); 
     209 
     210        if ( ! isset( $jf_yfvp_options[ 'embed_width' ] ) ) 
     211            $jf_yfvp_options[ 'embed_width' ] = 330; 
     212        ?> 
     213        <input style="width: 100px;" type="text" id="jf_yfvp_embed_width" name="jf_yfvp_options[embed_width]" value="<?php echo esc_attr( $jf_yfvp_options[ 'embed_width' ] ); ?>" /> 
     214        <?php 
     215    } 
     216 
     217    /** 
     218     * Provide in input for the embed height 
     219     */ 
     220    public function embed_height_text() { 
     221        $jf_yfvp_options = get_option( 'jf_yfvp_options', array() ); 
     222 
     223        if ( ! isset( $jf_yfvp_options[ 'embed_height' ] ) ) 
     224            $jf_yfvp_options[ 'embed_height' ] = 270; 
     225 
     226        ?> 
     227        <input style="width: 100px;" type="text" id="jf_yfvp_embed_height" name="jf_yfvp_options[embed_height]" value="<?php echo esc_attr( $jf_yfvp_options[ 'embed_height' ] ); ?>" /> 
     228        <?php 
     229    } 
     230 
     231    /** 
     232     * Provide an input for the Youtube username 
     233     */ 
     234    public function youtube_user_text() { 
     235        $jf_yfvp_options = get_option( 'jf_yfvp_options', array() ); 
     236 
     237        /* If options have been saved before, but no name specified, toss up a warning */ 
     238        if ( ! empty( $jf_yfvp_options ) && empty( $jf_yfvp_options[ 'youtube_rss_feed'] ) ) { 
     239            ?> 
     240            <div class="error" style="width: 615px;padding: 10px;"><?php _e( 'It looks like a Youtube username has not yet been entered, even though other options have been saved. Please note that we are unable to fetch your favorite videos until a username is provided.', 'youtube-favorite-video-posts' ); ?></div> 
     241            <?php 
     242        } 
     243 
     244        if ( ! isset( $jf_yfvp_options[ 'youtube_rss_feed' ] ) ) 
     245            $jf_yfvp_options[ 'youtube_rss_feed' ] = ''; 
     246        ?> 
     247        <input style="width: 200px;" type="text" id="jf_yfvp_youtube_rss_feed" name="jf_yfvp_options[youtube_rss_feed]" value="<?php echo esc_attr( $jf_yfvp_options[ 'youtube_rss_feed' ] ); ?>" /> 
     248        <?php 
     249    } 
     250 
     251    /** 
     252     * Provide an input for the selection of post types 
     253     */ 
     254    public function post_type_selection_text() { 
     255        $jf_yfvp_options = get_option( 'jf_yfvp_options', array() ); 
     256 
     257        if ( ! isset( $jf_yfvp_options[ 'post_type' ] ) ) 
     258            $jf_yfvp_options[ 'post_type' ] = 'jf_yfvp_youtube'; 
     259 
     260        $post_types = array_merge( get_post_types( array( '_builtin' => false ) ), array( 'post', 'link' ) ); 
     261 
     262        echo '<select id="jf_yfvp_post_type" name="jf_yfvp_options[post_type]">'; 
     263 
     264        foreach( $post_types as $pt ){ 
     265            echo '<option value="' . esc_attr( $pt ) . '" ' . selected( $jf_yfvp_options[ 'post_type' ], $pt, false ) . '>' . esc_html( $pt ) . '</option>'; 
     266        } 
     267 
     268        echo '</select>'; 
     269    } 
     270 
     271    /** 
     272     * Provide an input for the selection of post status 
     273     */ 
     274    public function post_status_selection_text() { 
     275        $jf_yfvp_options = get_option( 'jf_yfvp_options', array() ); 
     276 
     277        if ( ! isset( $jf_yfvp_options[ 'post_status' ] ) ) 
     278            $jf_yfvp_options[ 'post_status' ] = 'publish'; 
     279 
     280        $post_statii = array( 'draft', 'publish', 'private' ); 
     281 
     282        echo '<select id="jf_yfvp_post_status" name="jf_yfvp_options[post_status]">'; 
     283 
     284        foreach( $post_statii as $ps ) { 
     285            echo '<option value="' . esc_attr( $ps ) . '" ' . selected( $jf_yfvp_options[ 'post_status' ], $ps, false ) . '>' . esc_html( $ps ) . '</option>'; 
     286        } 
     287 
     288        echo '</select>'; 
     289    } 
     290 
     291    /** 
     292     * Provide an input to select the WP Cron interval to schedule the hook with 
     293     */ 
     294    public function fetch_interval_selection_text() { 
     295        $intervals = wp_get_schedules(); 
     296 
     297        $jf_yfvp_options = get_option( 'jf_yfvp_options', array() ); 
     298 
     299        if ( ! isset( $jf_yfvp_options[ 'fetch_interval' ] ) ) 
     300            $jf_yfvp_options[ 'fetch_interval' ] = 'hourly'; 
     301 
     302        echo '<select id="jf_yfvp_fetch_interval" name="jf_yfvp_options[fetch_interval]">'; 
     303 
     304        foreach( $intervals as $i => $v ){ 
     305            echo '<option value="' . esc_attr( $i ) . '" ' . selected( $jf_yfvp_options[ 'fetch_interval' ], $i, false ) . '>' . esc_html( $v['display'] ) . '</option>'; 
     306        } 
     307 
     308        echo '</select>'; 
     309    } 
     310 
     311    /** 
     312     * Provide an input for the max number of items to fetch 
     313     */ 
     314    public function max_fetch_items_text() { 
     315        $jf_yfvp_options = get_option( 'jf_yfvp_options', array() ); 
     316 
     317        if ( ! isset( $jf_yfvp_options[ 'max_fetch_items' ] ) ) 
     318            $jf_yfvp_options[ 'max_fetch_items' ] = 5; 
     319        ?> 
     320        <input type="text" id="jf_yfvp_max_fetch_items" name="jf_yfvp_options[max_fetch_items]" value="<?php echo esc_attr( $jf_yfvp_options[ 'max_fetch_items' ] ); ?>" /> 
     321        <?php 
     322    } 
     323 
     324    /** 
     325     * Validate the options being saved for the plugin 
     326     * 
     327     * @param $input array New values that the user is attempting to save 
     328     * @return array Validated values that we pass on 
     329     */ 
     330    public function validate_options( $input ) { 
     331 
     332        $valid_post_status_options = array( 'draft', 'publish', 'private' ); 
     333        $valid_fetch_interval_options = wp_get_schedules(); 
     334 
     335        $valid_post_type_options = array_merge( get_post_types( array( '_builtin' => false ) ), array( 'post', 'link' ) ); 
     336 
     337        if( ! in_array( $input[ 'post_status' ], $valid_post_status_options ) ) 
     338            $input[ 'post_status' ] = 'publish'; 
     339 
     340        if( ! in_array( $input[ 'post_type' ], $valid_post_type_options ) ) 
     341            $input[ 'post_type' ] = 'jf_yfvp_youtube'; 
     342 
     343        if( ! array_key_exists( $input[ 'fetch_interval' ], $valid_fetch_interval_options ) ) 
     344            $input[ 'fetch_interval' ] = 'hourly'; 
     345 
     346        /* It is possible the user just switched back to using our custom post type, so we should flush the rewrite rules */ 
     347        if ( 'jf_yfvp_youtube' == $input[ 'post_type' ] ) 
     348            flush_rewrite_rules( false ); 
     349 
     350        /*  This seems to be the only place we can reset the scheduled Cron if the frequency is changed, so here goes. */ 
     351        wp_clear_scheduled_hook( 'jf_yfvp_process_feed' ); 
     352        wp_schedule_event( ( time() + 30 ) , $input[ 'fetch_interval' ], 'jf_yfvp_process_feed' ); 
     353 
     354        $input[ 'max_fetch_items' ] = absint( $input[ 'max_fetch_items' ] ); 
     355        $input[ 'embed_width' ] = absint( $input[ 'embed_width' ] ); 
     356        $input[ 'embed_height' ] = absint( $input[ 'embed_height' ] ); 
     357 
     358        return $input; 
     359    } 
     360 
     361    /** 
     362     * Register our custom post type - jf_yfvp_youtube - for possible use with the plugin 
     363     */ 
     364    public function create_content_type() { 
     365        register_post_type( 'jf_yfvp_youtube', 
     366            array( 
     367                 'labels' => array( 
     368                     'name' => __( 'YouTube', 'youtube-favorite-video-posts' ), 
     369                     'singular_name' => __( 'YouTube Favorite', 'youtube-favorite-video-posts' ), 
     370                     'all_items' => __( 'All YouTube Favorites', 'youtube-favorite-video-posts' ), 
     371                     'add_new_item' => __( 'Add YouTube Favorite', 'youtube-favorite-video-posts' ), 
     372                     'edit_item' => __( 'Edit YouTube Favorite', 'youtube-favorite-video-posts' ), 
     373                     'new_item' => __( 'New YouTube Favorite', 'youtube-favorite-video-posts' ), 
     374                     'view_item' => __( 'View YouTube Favorite', 'youtube-favorite-video-posts' ), 
     375                     'search_items' => __( 'Search YouTube Favorites', 'youtube-favorite-video-posts' ), 
     376                     'not_found' => __( 'No YouTube Favorites found', 'youtube-favorite-video-posts' ), 
     377                     'not_found_in_trash' => __( 'No YouTube Favorites found in trash', 'youtube-favorite-video-posts' ), 
     378                 ), 
     379                 'description' => __( 'YouTube posts created by the YouTube Favorite Video Posts plugin.', 'youtube-favorite-video-posts' ), 
     380                 'public' => true, 
     381                 'menu_icon' => plugins_url( '/images/youtube-icon-16.png', __FILE__ ), 
     382                 'menu_position' => 5, 
     383                 'hierarchical' => false, 
     384                 'supports' => array ( 
     385                     'title', 
     386                     'editor', 
     387                     'author', 
     388                     'custom-fields', 
     389                     'comments', 
     390                     'revisions', 
     391                 ), 
     392                 'has_archive' => true, 
     393                 'rewrite' => array( 
     394                     'slug' => 'youtube', 
     395                     'with_front' => false 
     396                 ), 
     397            ) 
     398        ); 
     399    } 
     400 
     401    /** 
     402     * The default SimplePie cache lifetime is 12 hours. We really do want to update more 
     403     * frequently, so we'll make it 30 seconds during our update. 
     404     * 
     405     * @return int Number of seconds for the SimplePie cache to last 
     406     */ 
     407    public function modify_simplepie_cache_lifetime() { 
     408        return 30; 
     409    } 
     410 
     411    /** 
     412     * Grab the configured YouTube favorites RSS feed and create new posts based on that. 
     413     * 
     414     * @return mixed Only returns if leaving the function 
     415     */ 
     416    public function process_feed() { 
     417        /*  Go get some options! */ 
     418        $youtube_options = get_option( 'jf_yfvp_options', array() ); 
     419 
     420        /* No username, no feed. No feed, no work. */ 
     421        if ( empty( $youtube_options[ 'youtube_rss_feed' ] ) ) 
     422            return; 
     423 
     424        /*  The feed URL we'll be grabbing. */ 
     425        $youtube_feed_url = 'http://gdata.youtube.com/feeds/base/users/' . esc_attr( $youtube_options[ 'youtube_rss_feed' ] ) . '/favorites?alt=rss'; 
     426 
     427        if ( isset( $youtube_options[ 'post_type' ] ) ) 
     428            $post_type = $youtube_options[ 'post_type' ]; 
     429        else 
     430            $post_type = 'jf_yfvp_youtube'; 
     431 
     432        if ( isset( $youtube_options[ 'post_status' ] ) ) 
     433            $post_status = $youtube_options[ 'post_status' ]; 
     434        else 
     435            $post_status = 'publish'; 
     436 
     437        if ( isset( $youtube_options[ 'max_fetch_items' ] ) ) 
     438            $max_fetch_items = absint( $youtube_options[ 'max_fetch_items' ] ); 
     439        else 
     440            $max_fetch_items = 5; 
     441 
     442        /*  Now fetch with the WordPress SimplePie function. */ 
     443        add_filter( 'wp_feed_cache_transient_lifetime', array( $this, 'modify_simplepie_cache_lifetime' ) ); 
     444        $youtube_feed = fetch_feed( $youtube_feed_url ); 
     445        remove_filter( 'wp_feed_cache_transient_lifetime', array( $this, 'modify_simplepie_cache_lifetime' ) ); 
     446 
     447        if ( ! is_wp_error( $youtube_feed ) ) { 
     448            $max_items = $youtube_feed->get_item_quantity( $max_fetch_items ); 
     449            $youtube_items = $youtube_feed->get_items( 0, $max_items ); 
     450            foreach( $youtube_items as $item ) { 
     451                $video_token = substr( $item->get_id(), 43 ); 
     452 
     453                $video_embed_code = '<iframe width=\"' . absint( $youtube_options[ 'embed_width' ] ) . 
     454                    '\" height=\"' . absint( $youtube_options[ 'embed_height' ] ) . 
     455                    '\" src=\"http://www.youtube.com/embed/' . 
     456                    esc_attr( $video_token ) . '\" frameborder=\"0\" allowfullscreen></iframe>'; 
     457 
     458                /* Allow other plugins or themes to alter or replace the post content before storing */ 
     459                $video_embed_code = apply_filters( 'yfvp_new_video_embed_code', $video_embed_code, $video_token ); 
     460 
     461                /* Allow other plugins or themes to alter or replace the post title before storing. 
     462                 * Also, we're disabling the kses filters below, so we need to clean up the title as 
     463                 * YouTube allows " and the like. */ 
     464                $item_title = esc_html( apply_filters( 'yfvp_new_video_item_title', $item->get_title() ) ); 
     465 
     466                /*  Create a hash of the video token to store as post meta in order to check for unique content 
     467                 * if a video of the same title is stored one day. */ 
     468                $item_hash = md5( $video_token ); 
     469 
     470                /* We do our best to avoid duplicate videos. This will compare the existence of title/token in previous 
     471                 * videos and block the save if found. Keep this in mind if using the video title filter to do anything 
     472                 * fun. */ 
     473                if ( get_page_by_title( $item_title, 'OBJECT', $post_type ) ) { 
     474                    $existing_hash = get_post_meta( get_page_by_title( $item_title, 'OBJECT', $post_type )->ID, 'jf_yfvp_hash', true ); 
     475 
     476                    if ( $item_hash == $existing_hash ) 
     477                        $skip = 1; 
     478                    else 
     479                        $skip = NULL; 
     480                } else { 
     481                    $skip = NULL; 
     482                } 
     483 
     484                if ( ! $skip ) { 
     485 
     486                    $youtube_post = array( 
     487                        'post_title' => $item_title, 
     488                        'post_content' => $video_embed_code, 
     489                        'post_author' => 1, 
     490                        'post_status' => $post_status, 
     491                        'post_type' => $post_type, 
     492                        'filter' => true, 
     493                    ); 
     494 
     495                    kses_remove_filters(); 
     496                    $item_post_id = wp_insert_post( $youtube_post ); 
     497                    kses_init_filters(); 
     498                    add_post_meta( $item_post_id, 'jf_yfvp_hash', $item_hash, true ); 
     499                    add_post_meta( $item_post_id, 'jf_yfvp_video_token', $video_token, true ); 
     500                } 
     501            } 
     502        } 
     503    } 
    42504} 
    43  
    44 /*  Of course, the custom post type needs to be there for everyone, so we call this outside of the admin check. */ 
    45 add_action( 'init', 'jf_yfvp_create_youtube_type' ); 
    46  
    47 /*  And we want to make sure the cron tasks are there for everyone as well. */ 
    48 add_action( 'jf_yfvp_hourly_action', 'jf_yfvp_on_the_hour' ); 
    49  
    50 function jf_yfvp_plugin_activation(){ 
    51     $current_jf_yfvp_options = get_option( 'jf_yfvp_options' ); 
    52     $jf_yfvp_options[ 'youtube_rss_feed' ] = $current_jf_yfvp_options[ 'youtube_rss_feed' ] ? $current_jf_yfvp_options[ 'youtube_rss_feed' ] : ''; 
    53     $jf_yfvp_options[ 'embed_width' ] = $current_jf_yfvp_options[ 'embed_width' ] ? $current_jf_yfvp_options[ 'embed_width' ] : 330; 
    54     $jf_yfvp_options[ 'embed_height' ] = $current_jf_yfvp_options[ 'embed_height' ] ? $current_jf_yfvp_options[ 'embed_height' ] : 270; 
    55     $jf_yfvp_options[ 'post_type' ] = $current_jf_yfvp_options[ 'post_type' ] ? $current_jf_yfvp_options[ 'post_type' ] : 'jf_yfvp_youtube'; 
    56     $jf_yfvp_options[ 'post_status' ] = $current_jf_yfvp_options[ 'post_status' ] ? $current_jf_yfvp_options[ 'post_status' ] : 'publish'; 
    57     $jf_yfvp_options[ 'fetch_interval' ] = $current_jf_yfvp_options[ 'fetch_interval' ] ? $current_jf_yfvp_options[ 'fetch_interval' ] : 'hourly'; 
    58     $jf_yfvp_options[ 'max_fetch_items' ] = $current_jf_yfvp_options[ 'max_fetch_items' ] ? $current_jf_yfvp_options[ 'max_fetch_items' ] : 5; 
    59     add_option( 'jf_yfvp_options', $jf_yfvp_options ); 
    60  
    61     /*  Create the custom post type upon activation. */ 
    62     jf_yfvp_create_youtube_type(); 
    63  
    64     /*  Flush the rewrite rules so that the new custom post type works. */ 
    65     flush_rewrite_rules( false ); 
    66  
    67     /*  Schedule the first CRON event to happen 60 seconds from now, then hourly after that. */ 
    68     wp_schedule_event( ( time() + 60 ) , 'hourly', 'jf_yfvp_hourly_action' ); 
    69     /*  TODO: Make frequency a configurable option. */ 
    70 } 
    71  
    72 function jf_yfvp_plugin_deactivation(){ 
    73     /*  If the plugin is deactivated, we want to clear our hourly CRON to keep things clean. */ 
    74     wp_clear_scheduled_hook( 'jf_yfvp_hourly_action' ); 
    75 } 
    76  
    77 function jf_yfvp_plugin_action_links( $links, $file ){ 
    78     /*  Function gratefully taken (and barely modified) from Pippin Williamson's 
    79         WPMods article: http://www.wpmods.com/adding-plugin-action-links/ */ 
    80     static $this_plugin; 
    81  
    82     if ( ! $this_plugin ) { 
    83         $this_plugin = plugin_basename( __FILE__ ); 
    84     } 
    85  
    86     // check to make sure we are on the correct plugin 
    87     if ( $file == $this_plugin ){ 
    88         $settings_path = '/wp-admin/options-general.php?page=youtube-favorite-video-posts-settings'; 
    89         $settings_link = '<a href="' . get_bloginfo( 'wpurl' ) . $settings_path . '">' . __( 'Settings', 'youtube-favorite-video-posts' ) . '</a>'; 
    90         array_unshift( $links, $settings_link );  // add the link to the list 
    91     } 
    92  
    93     return $links; 
    94 } 
    95  
    96 function jf_yfvp_edit_icon(){ 
    97     global $post_type; 
    98     if ( 'jf_yfvp_youtube' == $post_type ) { 
    99         echo '<style>#icon-edit { background: url("' . plugins_url( 'images/youtube-icon-32.png', __FILE__ ) . '") no-repeat; 
    100                                   background-size: 32px 32px; }</style>'; 
    101     } 
    102 } 
    103  
    104 function jf_yfvp_add_settings(){ 
    105     /*  Add the sub-menu item under the Settings top-level menu. */ 
    106     add_options_page( __('YouTube Favorites', 'youtube-favorite-video-posts' ), __('YouTube Favorites', 'youtube-favorite-video-posts'), 'manage_options', 'youtube-favorite-video-posts-settings', 'jf_yfvp_view_settings' ); 
    107 } 
    108  
    109 function jf_yfvp_view_settings(){ 
    110  
    111     /*  Display the main settings view for Youtube Favorite Video Posts. */ 
    112     echo '<div class="wrap"> 
    113         <div class="icon32" id="icon-options-general"></div> 
    114             <h2>' . __( 'YouTube Favorite Video Posts', 'youtube-favorite-video-posts' ) . '</h2> 
    115             <h3>' . __( 'Overview', 'youtube-favorite-video-posts' ) . ':</h3> 
    116             <p style="margin-left:12px;max-width:640px;"> 
    117             ' . __( 'The settings below will help determine where to check for your favorite YouTube videos, how often to 
    118             look for them, and how they should be stored once new items are found.', 'youtube-favorite-video-posts' ) . '</p> 
    119             <p style="margin-left:12px;max-width:640px;"> 
    120                 The most important part of this process will be to determine the RSS feed for your favorite YouTube videos. In order 
    121                 to do this, your username <strong>must</strong> be filled out below. This can usually be found in the upper right hand 
    122                 corner of <a href="http://www.youtube.com">YouTube.com</a>. 
    123             </p> 
    124             <ol style="margin-left:36px;"> 
    125                 <li>Username must be filled in below. Email address will not work.</li> 
    126                 <li>The embed width and height settings will be applied to the iframe in your post content.</li> 
    127             </ol>'; 
    128  
    129        echo '<form method="post" action="options.php">'; 
    130  
    131     settings_fields( 'jf_yfvp_options' ); 
    132     do_settings_sections( 'jf_yfvp' ); // Display the main section of settings. 
    133  
    134     echo '<p class="submit"><input type="submit" class="button-primary" value="'; 
    135     _e( 'Save Changes', 'youtube-favorite-video-posts' ); 
    136     echo '" /> 
    137             </p> 
    138             </form> 
    139         </div>'; 
    140 } 
    141  
    142 function jf_yfvp_register_settings(){ 
    143     /*  Register the settings we want available for this. */ 
    144     register_setting( 'jf_yfvp_options', 'jf_yfvp_options', 'jf_yfvp_options_validate' ); 
    145     add_settings_section( 'jf_yfvp_section_main', '', 'jf_yfvp_section_text', 'jf_yfvp' ); 
    146     add_settings_section( 'jf_yfvp_section_post_type', '', 'jf_yfvp_section_post_type_text', 'jf_yfvp' ); 
    147     add_settings_section( 'jf_yfvp_section_interval', '', 'jf_yfvp_section_interval_text', 'jf_yfvp' ); 
    148     add_settings_field( 'jf_yfvp_youtube_rss_feed', 'YouTube Username:', 'jf_yfvp_youtube_rss_feed_text', 'jf_yfvp', 'jf_yfvp_section_main' ); 
    149     add_settings_field( 'jf_yfvp_embed_width', 'Default Embed Width:', 'jf_yfvp_embed_width_text', 'jf_yfvp', 'jf_yfvp_section_main' ); 
    150     add_settings_field( 'jf_yfvp_embed_height', 'Default Embed Height:', 'jf_yfvp_embed_height_text', 'jf_yfvp', 'jf_yfvp_section_main' ); 
    151     add_settings_field( 'jf_yfvp_max_fetch_items', 'Max Items To Fetch:', 'jf_yfvp_max_fetch_items_text', 'jf_yfvp', 'jf_yfvp_section_main' ); 
    152     add_settings_field( 'jf_yfvp_post_type', 'Post Type:', 'jf_yfvp_post_type_text', 'jf_yfvp', 'jf_yfvp_section_post_type' ); 
    153     add_settings_field( 'jf_yfvp_post_status', __( 'Default Post Status:', 'youtube-favorite-video-posts' ) , 'jf_yfvp_post_status_text', 'jf_yfvp', 'jf_yfvp_section_post_type' ); 
    154     add_settings_field( 'jf_yfvp_fetch_interval', 'Feed Fetch Interval: ', 'jf_yfvp_fetch_interval_text', 'jf_yfvp', 'jf_yfvp_section_interval' ); 
    155 } 
    156  
    157 function jf_yfvp_section_text(){ 
    158     /*  Placeholder for later. Nothing really needed at the moment. */ 
    159 } 
    160  
    161 function jf_yfvp_section_post_type_text(){ 
    162     echo '<h3>Custom Or Default Post Type</h3> 
    163     <p style="margin-left:12px;max-width: 640px;">A new custom post type that adds an \'youtube\' slug to new items has been added and selected by default. 
    164     You can change this to any other available post type if you would like.</p>'; 
    165 } 
    166  
    167 function jf_yfvp_section_interval_text(){ 
    168     echo '<h3>RSS Fetch Frequency</h3> 
    169         <p style="margin-left:12px;max-width: 630px;">This plugin currently depends on WP Cron operating fully as expected. In most cases, you should 
    170         be able to select one of the intervals below and things will work as expected. If not, please let <a href="http://www.jeremyfelt.com">me</a> know. By 
    171         default, we check for new items on an hourly basis.</p>'; 
    172     $seconds_till_cron = wp_next_scheduled( 'jf_yfvp_hourly_action' ) - time(); 
    173     $user_next_cron = date( 'H:i:sA', wp_next_scheduled( 'jf_yfvp_hourly_action' ) + ( get_option( 'gmt_offset' ) * 3600 ) ); 
    174     echo '<p style="margin-left:12px;">The next check is scheduled to run at ' . $user_next_cron . ', which occurs in ' . $seconds_till_cron . ' seconds.</p>'; 
    175 } 
    176  
    177 function jf_yfvp_embed_width_text(){ 
    178     $jf_yfvp_options = get_option( 'jf_yfvp_options' ); 
    179     echo '<input style="width: 100px;" type="text" 
    180                  id="jf_yfvp_embed_width" 
    181                  name="jf_yfvp_options[embed_width]" 
    182                  value="' . $jf_yfvp_options[ 'embed_width' ] . '">'; 
    183 } 
    184  
    185 function jf_yfvp_embed_height_text(){ 
    186     $jf_yfvp_options = get_option( 'jf_yfvp_options' ); 
    187     echo '<input style="width: 100px;" type="text" 
    188                  id="jf_yfvp_embed_height" 
    189                  name="jf_yfvp_options[embed_height]" 
    190                  value="' . $jf_yfvp_options[ 'embed_height' ] . '">'; 
    191 } 
    192  
    193 function jf_yfvp_youtube_rss_feed_text(){ 
    194     $jf_yfvp_options = get_option( 'jf_yfvp_options' ); 
    195     echo '<input style="width: 200px;" type="text" 
    196                  id="jf_yfvp_youtube_rss_feed" 
    197                  name="jf_yfvp_options[youtube_rss_feed]" 
    198                  value="' . $jf_yfvp_options[ 'youtube_rss_feed' ] . '">'; 
    199 } 
    200  
    201 function jf_yfvp_post_type_text(){ 
    202     $jf_yfvp_options = get_option( 'jf_yfvp_options' ); 
    203     $post_types = array( 'post', 'link' ); 
    204     $all_post_types = get_post_types( array( '_builtin' => false ) ); 
    205  
    206     foreach( $all_post_types as $p=>$k ){ 
    207         $post_types[] = $p; 
    208     } 
    209  
    210     echo '<select id="jf_yfvp_post_type" name="jf_yfvp_options[post_type]">'; 
    211  
    212     foreach( $post_types as $pt ){ 
    213         echo '<option value="' . $pt . '" ' . selected( $jf_yfvp_options[ 'post_type' ], $pt, false ) . '>' . $pt . '</option>'; 
    214     } 
    215  
    216     echo '</select>'; 
    217 } 
    218  
    219 function jf_yfvp_post_status_text(){ 
    220     $jf_yfvp_options = get_option( 'jf_yfvp_options' ); 
    221  
    222     echo '<select id="jf_yfvp_post_status" name="jf_yfvp_options[post_status]"> 
    223             <option value="draft" '   . selected( $jf_yfvp_options[ 'post_status' ], 'draft', false )   . '>draft</option> 
    224             <option value="publish" ' . selected( $jf_yfvp_options[ 'post_status' ], 'publish', false ) . '>publish</option> 
    225             <option value="private" ' . selected( $jf_yfvp_options[ 'post_status' ], 'private', false ) . '>private</option> 
    226           </select>'; 
    227 } 
    228  
    229 function jf_yfvp_fetch_interval_text(){ 
    230     /* TODO: Custom intervals can be added to a WordPress install, so we should query those and offer as an option. */ 
    231     $intervals = array( 'hourly', 'twicedaily', 'daily' ); 
    232     $jf_yfvp_options = get_option( 'jf_yfvp_options' ); 
    233  
    234     echo '<select id="jf_yfvp_fetch_interval" name="jf_yfvp_options[fetch_interval]">'; 
    235  
    236     foreach( $intervals as $i ){ 
    237         echo '<option value="' . $i . '" ' . selected( $jf_yfvp_options[ 'fetch_interval' ], $i, false ) . '>' . $i . '</option>'; 
    238     } 
    239  
    240     echo '</select>'; 
    241 } 
    242  
    243 function jf_yfvp_max_fetch_items_text(){ 
    244     $jf_yfvp_options = get_option( 'jf_yfvp_options' ); 
    245     echo '<input type="text" 
    246                  id="jf_yfvp_max_fetch_items" 
    247                  name="jf_yfvp_options[max_fetch_items]" 
    248                  value="' . $jf_yfvp_options[ 'max_fetch_items' ] . '">'; 
    249 } 
    250  
    251 function jf_yfvp_options_validate( $input ) { 
    252     /*  Validation of a drop down. Hmm. Well, if it isn't on our list, we'll force it onto our list. */ 
    253     $valid_post_status_options = array( 'draft', 'publish', 'private' ); 
    254     $valid_post_type_options = array( 'post', 'link' ); 
    255     $valid_fetch_interval_options = array( 'hourly', 'twicedaily', 'daily' ); 
    256  
    257     $all_post_types = get_post_types( array( '_builtin' => false ) ); 
    258     foreach( $all_post_types as $p=>$k ){ 
    259         $valid_post_type_options[] = $p; 
    260     } 
    261  
    262     if( ! in_array( $input[ 'post_status' ], $valid_post_status_options ) ) 
    263         $input[ 'post_status' ] = 'draft'; 
    264  
    265     if( ! in_array( $input[ 'post_type' ], $valid_post_type_options ) ) 
    266         $input[ 'post_type' ] = 'jf_yfvp_youtube'; 
    267  
    268     if( ! in_array( $input[ 'fetch_interval' ], $valid_fetch_interval_options ) ) 
    269         $input[ 'fetch_interval' ] = 'hourly'; 
    270  
    271     /*  This seems to be the only place we can reset the scheduled Cron if the frequency is changed, so here goes. */ 
    272     wp_clear_scheduled_hook( 'jf_yfvp_hourly_action' ); 
    273     wp_schedule_event( ( time() + 30 ) , $input[ 'fetch_interval' ], 'jf_yfvp_hourly_action' ); 
    274  
    275     $input[ 'max_fetch_items' ] = absint( $input[ 'max_fetch_items' ] ); 
    276     $input[ 'embed_width' ] = absint( $input[ 'embed_width' ] ); 
    277     $input[ 'embed_height' ] = absint( $input[ 'embed_height' ] ); 
    278  
    279     return $input; 
    280 } 
    281  
    282 function jf_yfvp_create_youtube_type(){ 
    283     /*  Add the custom post type 'jf_yfvp_youtube' to WordPress. */ 
    284     register_post_type( 'jf_yfvp_youtube', 
    285         array( 
    286             'labels' => array( 
    287                 'name' => __( 'YouTube' ), 
    288                 'singular_name' => __( 'YouTube Favorite' ), 
    289                 'all_items' => __( 'All YouTube Favorites' ), 
    290                 'add_new_item' => __( 'Add YouTube Favorite' ), 
    291                 'edit_item' => __( 'Edit YouTube Favorite' ), 
    292                 'new_item' => __( 'New YouTube Favorite' ), 
    293                 'view_item' => __( 'View YouTube Favorite' ), 
    294                 'search_items' => __( 'Search YouTube Favorites' ), 
    295                 'not_found' => __( 'No YouTube Favorites found' ), 
    296                 'not_found_in_trash' => __( 'No YouTube Favorites found in trash' ), 
    297             ), 
    298         'description' => 'YouTube posts created by the YouTube Favorite Video Posts plugin.', 
    299         'public' => true, 
    300         'menu_icon' => plugins_url( '/images/youtube-icon-16.png', __FILE__ ), 
    301         'menu_position' => 5, 
    302         'hierarchical' => false, 
    303         'supports' => array ( 
    304             'title', 
    305             'editor', 
    306             'author', 
    307             'custom-fields', 
    308             'comments', 
    309             'revisions', 
    310             ), 
    311         'has_archive' => true, 
    312         'rewrite' => array( 
    313             'slug' => 'youtube', 
    314             'with_front' => false 
    315             ), 
    316         ) 
    317     ); 
    318 } 
    319  
    320 function jf_yfvp_modify_cache_lifetime( $seconds ) { 
    321     /* The default SimplePie cache lifetime is 12 hours. We really do want to update 
    322         more frequently, so we'll make it 30 seconds during our update. */ 
    323     return 30; 
    324 } 
    325  
    326 function jf_yfvp_on_the_hour(){ 
    327     /*  Grab the configured YouTube favorites RSS feed and create new posts based on that. */ 
    328  
    329     /*  Go get some options! */ 
    330     $youtube_options = get_option( 'jf_yfvp_options' ); 
    331     /*  The feed URL we'll be grabbing. */ 
    332     $youtube_feed_url = 'http://gdata.youtube.com/feeds/base/users/' . $youtube_options[ 'youtube_rss_feed' ] . '/favorites?alt=rss'; 
    333     /*  The post type we'll be saving as. We designed it to be custom, but why not allow anything. */ 
    334     $post_type = $youtube_options[ 'post_type' ]; 
    335     /*  The post status we'll use. */ 
    336     $post_status = $youtube_options[ 'post_status' ]; 
    337  
    338     add_filter( 'wp_feed_cache_transient_lifetime', 'jf_yfvp_modify_cache_lifetime' ); 
    339     /*  Now fetch with the WordPress SimplePie function. */ 
    340     $youtube_feed = fetch_feed( $youtube_feed_url ); 
    341     remove_filter( 'wp_feed_cache_transient_lifetime', 'jf_yfvp_modify_cach_lifetime' ); 
    342  
    343     if ( ! is_wp_error( $youtube_feed ) ){ 
    344         /*  Feed looks like a good object, continue. */ 
    345         $max_items = $youtube_feed->get_item_quantity( absint( $youtube_options[ 'max_fetch_items' ] ) ); 
    346         $youtube_items = $youtube_feed->get_items(0, $max_items); 
    347         foreach( $youtube_items as $item ){ 
    348             $video_token = substr( $item->get_id() ,43 ); 
    349  
    350             $video_embed_code = '<iframe width=\"' . $youtube_options[ 'embed_width' ] . 
    351                                 '\" height=\"' . $youtube_options[ 'embed_height' ] . 
    352                                 '\" src=\"http://www.youtube.com/embed/' . 
    353                                 $video_token . '\" frameborder=\"0\" allowfullscreen></iframe>'; 
    354  
    355             /*  We're disabling the kses filters below, so we need to clean up the title as YouTube allows " and the like. */ 
    356             $item_title = esc_html( $item->get_title() ); 
    357  
    358             /*  Create a hash of the video token to store as post meta in order to check for unique content if a video 
    359                 of the same title is stored one day. */ 
    360             $item_hash = md5( $video_token ); 
    361  
    362             if ( get_page_by_title( $item_title, 'OBJECT', $post_type ) ){ 
    363                 /*  Title already exists. */ 
    364                 $existing_hash = get_post_meta( get_page_by_title( $item_title, 'OBJECT', $post_type )->ID, 'jf_yfvp_hash', true ); 
    365  
    366                 if ( $item_hash == $existing_hash ){ 
    367                     $skip = 1; 
    368                 }else{ 
    369                     $skip = NULL; 
    370                 } 
    371             }else{ 
    372                 $skip = NULL; 
    373             } 
    374  
    375             if ( ! $skip ){ 
    376  
    377                 $youtube_post = array( 
    378                     'post_title' => $item_title, 
    379                     'post_content' => $video_embed_code, 
    380                     'post_author' => 1, 
    381                     'post_status' => $post_status, 
    382                     'post_type' => $post_type, 
    383                     'filter' => true 
    384                 ); 
    385  
    386                 kses_remove_filters(); 
    387                 $item_post_id = wp_insert_post( $youtube_post ); 
    388                 kses_init_filters(); 
    389                 add_post_meta( $item_post_id, 'jf_yfvp_hash', $item_hash, true ); 
    390             } 
    391         } 
    392     }else{ 
    393         /*  Uhhh, feels a little shady to die silently, but for now that's all we got. */ 
    394     } 
    395 } 
     505new Youtube_Favorite_Video_Posts_Foghlaim(); 
Note: See TracChangeset for help on using the changeset viewer.