WordPress.org

Plugin Directory

Changeset 626026


Ignore:
Timestamp:
11/16/12 08:06:07 (17 months ago)
Author:
jeremyfelt
Message:

Push v1.1 to trunk, see https://github.com/jeremyfelt/Youtube-Favorite-Video-Posts/ for specific commits

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

Legend:

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

    r543562 r626026  
    22 
    33Contributors: jeremyfelt 
    4 Donate link: http://www.jeremyfelt.com/wordpress/plugins/youtube-favorite-video-posts/ 
     4Donate link: http://jeremyfelt.com/wordpress/plugins/youtube-favorite-video-posts/ 
    55Tags: youtube, custom post type, embed, video, rss, feed 
    66Requires at least: 3.2.1 
    7 Tested up to: 3.4 
    8 Stable tag: 1.0 
     7Tested up to: 3.5 
     8Stable tag: 1.1 
    99 
    1010YouTube Favorite Video Posts grabs videos you mark as favorites in YouTube and adds them to WordPress under a custom post type. 
     
    5858 
    5959== Changelog == 
     60= 1.1 = 
     61 
     62* Smarter hashing of uniqueness, should handle Youtube video title changes 
     63* Better original title handling. You can now modify the title through a filter without worrying about it causing duplicates 
     64* Code cleanup 
     65 
    6066= 1.0 = 
    6167 
     
    8288 
    8389== Upgrade Notice == 
     90= 1.1 = 
     91 
     92* Upgrades some of the hashing and video handling to allow for better future customization. 
     93 
    8494= 1.0 = 
    8595 
  • youtube-favorite-video-posts/trunk/youtube-favorite-video-posts.php

    r543563 r626026  
    22/* 
    33Plugin Name: YouTube Favorite Video Posts 
    4 Plugin URI: http://www.jeremyfelt.com/wordpress/plugins/youtube-favorite-video-posts 
     4Plugin URI: http://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: 1.0 
     6Version: 1.1 
    77Author: Jeremy Felt 
    8 Author URI: http://www.jeremyfelt.com 
     8Author URI: http://jeremyfelt.com 
    99License: GPL2 
    1010*/ 
    1111 
    12 /*  Copyright 2011 Jeremy Felt (email: jeremy.felt@gmail.com) 
     12/*  Copyright 2011-2012 Jeremy Felt (email: jeremy.felt@gmail.com) 
    1313 
    1414    This program is free software; you can redistribute it and/or modify 
     
    4141        add_action( 'admin_init', array( $this, 'register_settings' ) ); 
    4242        add_action( 'admin_init', array( $this, 'add_languages' ) ); 
     43 
    4344        /* Register the jf_yfvp_youtube custom post type */ 
    4445        add_action( 'init', array( $this, 'create_content_type' ) ); 
     
    6162        /* If the custom post type provided by this plugin is selected, flush the rewrite 
    6263         * rules so that the URLs can be pretty */ 
    63         if ( isset( $current_options[ 'post_type' ] ) && 'jf_yfvp_youtube' == $current_options[ 'post_type' ] ) 
     64        if ( isset( $current_options['post_type'] ) && 'jf_yfvp_youtube' === $current_options['post_type'] ) 
    6465            flush_rewrite_rules( false ); 
    6566 
    6667        /* 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' ); 
     68        if ( isset( $current_options['fetch_interval'] ) && in_array( $current_options['fetch_interval'], $valid_fetch_intervals ) ) 
     69            wp_schedule_event( ( time() + 120 ) , $current_options['fetch_interval'], 'jf_yfvp_process_feed' ); 
    6970 
    7071    } 
     
    113114    public function edit_admin_icon(){ 
    114115        global $post_type; 
    115         if ( 'jf_yfvp_youtube' == $post_type ) 
     116        if ( 'jf_yfvp_youtube' === $post_type ) 
    116117            echo '<style>#icon-edit { background: url("' . plugins_url( 'images/youtube-icon-32.png', __FILE__ ) . '") no-repeat; background-size: 32px 32px; }</style>'; 
    117118    } 
     
    199200            <h3>RSS Fetch Frequency</h3> 
    200201            <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> 
    201             <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> 
     202            <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> 
    202203            <?php 
    203204        } else { 
     
    215216        $jf_yfvp_options = get_option( 'jf_yfvp_options', array() ); 
    216217 
    217         if ( ! isset( $jf_yfvp_options[ 'embed_width' ] ) ) 
    218             $jf_yfvp_options[ 'embed_width' ] = 330; 
     218        if ( ! isset( $jf_yfvp_options['embed_width'] ) ) 
     219            $jf_yfvp_options['embed_width'] = 330; 
    219220        ?> 
    220         <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' ] ); ?>" /> 
     221        <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'] ); ?>" /> 
    221222        <?php 
    222223    } 
     
    228229        $jf_yfvp_options = get_option( 'jf_yfvp_options', array() ); 
    229230 
    230         if ( ! isset( $jf_yfvp_options[ 'embed_height' ] ) ) 
    231             $jf_yfvp_options[ 'embed_height' ] = 270; 
     231        if ( ! isset( $jf_yfvp_options['embed_height'] ) ) 
     232            $jf_yfvp_options['embed_height'] = 270; 
    232233 
    233234        ?> 
    234         <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' ] ); ?>" /> 
     235        <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'] ); ?>" /> 
    235236        <?php 
    236237    } 
     
    243244 
    244245        /* If options have been saved before, but no name specified, toss up a warning */ 
    245         if ( ! empty( $jf_yfvp_options ) && empty( $jf_yfvp_options[ 'youtube_rss_feed'] ) ) { 
     246        if ( ! empty( $jf_yfvp_options ) && empty( $jf_yfvp_options['youtube_rss_feed'] ) ) { 
    246247            ?> 
    247248            <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> 
     
    249250        } 
    250251 
    251         if ( ! isset( $jf_yfvp_options[ 'youtube_rss_feed' ] ) ) 
    252             $jf_yfvp_options[ 'youtube_rss_feed' ] = ''; 
     252        if ( ! isset( $jf_yfvp_options['youtube_rss_feed'] ) ) 
     253            $jf_yfvp_options['youtube_rss_feed'] = ''; 
    253254        ?> 
    254         <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' ] ); ?>" /> 
     255        <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'] ); ?>" /> 
    255256        <?php 
    256257    } 
     
    262263        $jf_yfvp_options = get_option( 'jf_yfvp_options', array() ); 
    263264 
    264         if ( ! isset( $jf_yfvp_options[ 'post_type' ] ) ) 
    265             $jf_yfvp_options[ 'post_type' ] = 'jf_yfvp_youtube'; 
     265        if ( ! isset( $jf_yfvp_options['post_type'] ) ) 
     266            $jf_yfvp_options['post_type'] = 'jf_yfvp_youtube'; 
    266267 
    267268        $post_types = array_merge( get_post_types( array( '_builtin' => false ) ), array( 'post', 'link' ) ); 
     
    270271 
    271272        foreach( $post_types as $pt ){ 
    272             echo '<option value="' . esc_attr( $pt ) . '" ' . selected( $jf_yfvp_options[ 'post_type' ], $pt, false ) . '>' . esc_html( $pt ) . '</option>'; 
     273            echo '<option value="' . esc_attr( $pt ) . '" ' . selected( $jf_yfvp_options['post_type'], $pt, false ) . '>' . esc_html( $pt ) . '</option>'; 
    273274        } 
    274275 
     
    282283        $jf_yfvp_options = get_option( 'jf_yfvp_options', array() ); 
    283284 
    284         if ( ! isset( $jf_yfvp_options[ 'post_status' ] ) ) 
    285             $jf_yfvp_options[ 'post_status' ] = 'publish'; 
     285        if ( ! isset( $jf_yfvp_options['post_status'] ) ) 
     286            $jf_yfvp_options['post_status'] = 'publish'; 
    286287 
    287288        $post_statii = array( 'draft', 'publish', 'private' ); 
     
    290291 
    291292        foreach( $post_statii as $ps ) { 
    292             echo '<option value="' . esc_attr( $ps ) . '" ' . selected( $jf_yfvp_options[ 'post_status' ], $ps, false ) . '>' . esc_html( $ps ) . '</option>'; 
     293            echo '<option value="' . esc_attr( $ps ) . '" ' . selected( $jf_yfvp_options['post_status'], $ps, false ) . '>' . esc_html( $ps ) . '</option>'; 
    293294        } 
    294295 
     
    304305        $jf_yfvp_options = get_option( 'jf_yfvp_options', array() ); 
    305306 
    306         if ( ! isset( $jf_yfvp_options[ 'fetch_interval' ] ) ) 
    307             $jf_yfvp_options[ 'fetch_interval' ] = 'hourly'; 
     307        if ( ! isset( $jf_yfvp_options['fetch_interval'] ) ) 
     308            $jf_yfvp_options['fetch_interval'] = 'hourly'; 
    308309 
    309310        echo '<select id="jf_yfvp_fetch_interval" name="jf_yfvp_options[fetch_interval]">'; 
    310311 
    311312        foreach( $intervals as $i => $v ){ 
    312             echo '<option value="' . esc_attr( $i ) . '" ' . selected( $jf_yfvp_options[ 'fetch_interval' ], $i, false ) . '>' . esc_html( $v['display'] ) . '</option>'; 
     313            echo '<option value="' . esc_attr( $i ) . '" ' . selected( $jf_yfvp_options['fetch_interval'], $i, false ) . '>' . esc_html( $v['display'] ) . '</option>'; 
    313314        } 
    314315 
     
    322323        $jf_yfvp_options = get_option( 'jf_yfvp_options', array() ); 
    323324 
    324         if ( ! isset( $jf_yfvp_options[ 'max_fetch_items' ] ) ) 
    325             $jf_yfvp_options[ 'max_fetch_items' ] = 5; 
     325        if ( ! isset( $jf_yfvp_options['max_fetch_items'] ) ) 
     326            $jf_yfvp_options['max_fetch_items'] = 5; 
    326327        ?> 
    327         <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' ] ); ?>" /> 
     328        <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'] ); ?>" /> 
    328329        <?php 
    329330    } 
     
    342343        $valid_post_type_options = array_merge( get_post_types( array( '_builtin' => false ) ), array( 'post', 'link' ) ); 
    343344 
    344         if( ! in_array( $input[ 'post_status' ], $valid_post_status_options ) ) 
    345             $input[ 'post_status' ] = 'publish'; 
    346  
    347         if( ! in_array( $input[ 'post_type' ], $valid_post_type_options ) ) 
    348             $input[ 'post_type' ] = 'jf_yfvp_youtube'; 
    349  
    350         if( ! array_key_exists( $input[ 'fetch_interval' ], $valid_fetch_interval_options ) ) 
    351             $input[ 'fetch_interval' ] = 'hourly'; 
     345        if( ! in_array( $input['post_status'], $valid_post_status_options ) ) 
     346            $input['post_status'] = 'publish'; 
     347 
     348        if( ! in_array( $input['post_type'], $valid_post_type_options ) ) 
     349            $input['post_type'] = 'jf_yfvp_youtube'; 
     350 
     351        if( ! array_key_exists( $input['fetch_interval'], $valid_fetch_interval_options ) ) 
     352            $input['fetch_interval'] = 'hourly'; 
    352353 
    353354        /* It is possible the user just switched back to using our custom post type, so we should flush the rewrite rules */ 
    354         if ( 'jf_yfvp_youtube' == $input[ 'post_type' ] ) 
     355        if ( 'jf_yfvp_youtube' === $input['post_type'] ) 
    355356            flush_rewrite_rules( false ); 
    356357 
    357358        /*  This seems to be the only place we can reset the scheduled Cron if the frequency is changed, so here goes. */ 
    358359        wp_clear_scheduled_hook( 'jf_yfvp_process_feed' ); 
    359         wp_schedule_event( ( time() + 30 ) , $input[ 'fetch_interval' ], 'jf_yfvp_process_feed' ); 
    360  
    361         $input[ 'max_fetch_items' ] = absint( $input[ 'max_fetch_items' ] ); 
    362         $input[ 'embed_width' ] = absint( $input[ 'embed_width' ] ); 
    363         $input[ 'embed_height' ] = absint( $input[ 'embed_height' ] ); 
     360        wp_schedule_event( ( time() + 30 ) , $input['fetch_interval'], 'jf_yfvp_process_feed' ); 
     361 
     362        $input['max_fetch_items'] = absint( $input['max_fetch_items'] ); 
     363        $input['embed_width'] = absint( $input['embed_width'] ); 
     364        $input['embed_height'] = absint( $input['embed_height'] ); 
    364365 
    365366        return $input; 
     
    426427 
    427428        /* No username, no feed. No feed, no work. */ 
    428         if ( empty( $youtube_options[ 'youtube_rss_feed' ] ) ) 
     429        if ( empty( $youtube_options['youtube_rss_feed'] ) ) 
    429430            return; 
    430431 
    431432        /*  The feed URL we'll be grabbing. */ 
    432         $youtube_feed_url = 'http://gdata.youtube.com/feeds/base/users/' . esc_attr( $youtube_options[ 'youtube_rss_feed' ] ) . '/favorites?alt=rss'; 
    433  
    434         if ( isset( $youtube_options[ 'post_type' ] ) ) 
    435             $post_type = $youtube_options[ 'post_type' ]; 
     433        $youtube_feed_url = 'http://gdata.youtube.com/feeds/base/users/' . esc_attr( $youtube_options['youtube_rss_feed'] ) . '/favorites?alt=rss'; 
     434 
     435        if ( isset( $youtube_options['post_type'] ) ) 
     436            $post_type = $youtube_options['post_type']; 
    436437        else 
    437438            $post_type = 'jf_yfvp_youtube'; 
    438439 
    439         if ( isset( $youtube_options[ 'post_status' ] ) ) 
    440             $post_status = $youtube_options[ 'post_status' ]; 
     440        if ( isset( $youtube_options['post_status'] ) ) 
     441            $post_status = $youtube_options['post_status']; 
    441442        else 
    442443            $post_status = 'publish'; 
    443444 
    444         if ( isset( $youtube_options[ 'max_fetch_items' ] ) ) 
    445             $max_fetch_items = absint( $youtube_options[ 'max_fetch_items' ] ); 
     445        if ( isset( $youtube_options['max_fetch_items'] ) ) 
     446            $max_fetch_items = absint( $youtube_options['max_fetch_items'] ); 
    446447        else 
    447448            $max_fetch_items = 5; 
     
    456457            $youtube_items = $youtube_feed->get_items( 0, $max_items ); 
    457458            foreach( $youtube_items as $item ) { 
     459 
     460                // Hash the guid element from the RSS feed to determine uniqueness, since yeah... guid. 
     461                $video_guid = md5( $item->get_id() ); 
     462 
    458463                $video_token = substr( $item->get_id(), 43 ); 
    459  
    460                 $video_embed_code = '<iframe width=\"' . absint( $youtube_options[ 'embed_width' ] ) . 
    461                     '\" height=\"' . absint( $youtube_options[ 'embed_height' ] ) . 
     464                $old_item_hash = md5( $video_token ); 
     465 
     466                $video_embed_code = '<iframe width=\"' . absint( $youtube_options['embed_width'] ) . 
     467                    '\" height=\"' . absint( $youtube_options['embed_height'] ) . 
    462468                    '\" src=\"http://www.youtube.com/embed/' . 
    463469                    esc_attr( $video_token ) . '\" frameborder=\"0\" allowfullscreen></iframe>'; 
     
    469475                 * Also, we're disabling the kses filters below, so we need to clean up the title as 
    470476                 * YouTube allows " and the like. */ 
    471                 $item_title = esc_html( apply_filters( 'yfvp_new_video_item_title', $item->get_title() ) ); 
    472  
    473                 /*  Create a hash of the video token to store as post meta in order to check for unique content 
    474                  * if a video of the same title is stored one day. */ 
    475                 $item_hash = md5( $video_token ); 
    476  
    477                 /* We do our best to avoid duplicate videos. This will compare the existence of title/token in previous 
    478                  * videos and block the save if found. Keep this in mind if using the video title filter to do anything 
    479                  * fun. */ 
    480                 if ( get_page_by_title( $item_title, 'OBJECT', $post_type ) ) { 
    481                     $existing_hash = get_post_meta( get_page_by_title( $item_title, 'OBJECT', $post_type )->ID, 'jf_yfvp_hash', true ); 
    482  
    483                     if ( $item_hash == $existing_hash ) 
    484                         $skip = 1; 
    485                     else 
    486                         $skip = NULL; 
     477                $original_item_title = $item->get_title(); 
     478                $item_title = esc_html( apply_filters( 'yfvp_new_video_item_title', $original_item_title ) ); 
     479 
     480                /** 
     481                 * Our previous hash management was ugly, so now we need to check for the 
     482                 * existence of the old hash before checking the existence of the new hash. 
     483                 * If we do happen to find an old hash, we'll update it immediately with 
     484                 * the newer hash so that we can get rid of this code in the next release. 
     485                 */ 
     486                $existing_old_item = get_posts( array( 
     487                                                'post_type' => $post_type, 
     488                                                'numberposts' => 1, 
     489                                                'post_status' => array( 'publish', 'draft', 'private' ), 
     490                                                'meta_query' => array( 
     491                                                    array( 
     492                                                        'key' => 'jf_yfvp_hash', 
     493                                                        'value' => $old_item_hash, 
     494                                                    ), 
     495                                                ), 
     496                                                )); 
     497 
     498                if ( ! empty( $existing_old_item ) ) { 
     499                    update_post_meta( $existing_old_item[0]->ID, 'jf_yfvp_hash', $video_guid ); 
     500                    $existing_items = $existing_old_item; 
    487501                } else { 
    488                     $skip = NULL; 
     502                    $existing_items = get_posts( array( 
     503                                                  'post_type' => $post_type, 
     504                                                  'numberposts' => 1, 
     505                                                  'post_status' => array( 'publish', 'draft', 'private' ), 
     506                                                  'meta_query' => array( 
     507                                                      array( 
     508                                                          'key' => 'jf_yfvp_hash', 
     509                                                          'value' => $video_guid, 
     510                                                      ), 
     511                                                  ), 
     512                                                 )); 
    489513                } 
    490514 
    491                 if ( ! $skip ) { 
     515                // If we come back empty on our meta query, we should be ok to insert the video as normal 
     516                if ( empty ( $existing_items ) ) { 
    492517 
    493518                    $youtube_post = array( 
     
    503528                    $item_post_id = wp_insert_post( $youtube_post ); 
    504529                    kses_init_filters(); 
    505                     add_post_meta( $item_post_id, 'jf_yfvp_hash', $item_hash, true ); 
     530                    add_post_meta( $item_post_id, 'jf_yfvp_hash', $video_guid, true ); 
    506531                    add_post_meta( $item_post_id, 'jf_yfvp_video_token', $video_token, true ); 
     532                    add_post_meta( $item_post_id, 'jf_yfvp_original_title', sanitize_title( $original_item_title ), true ); 
     533                } else { 
     534                    $original_meta_title = get_post_meta( $existing_items[0]->ID, 'jf_yfvp_original_title', true ); 
     535                    /* 
     536                     * If the current item's original title does not match the matched post's original title, 
     537                     * update it with the current filtered version of the new item title. *whew* 
     538                     */ 
     539                    if ( $original_item_title !== $original_meta_title ) { 
     540                        wp_update_post( array( 'ID' => $existing_items[0]->ID, 'post_title' => $item_title ) ); 
     541                        update_post_meta( $existing_items[0]->ID, 'jf_yfvp_original_title', $original_item_title ); 
     542                    } 
    507543                } 
    508544            } 
Note: See TracChangeset for help on using the changeset viewer.