Plugin Directory

Changeset 786864


Ignore:
Timestamp:
10/12/2013 10:26:10 AM (12 years ago)
Author:
sydcode
Message:

Added support for custom fields.

Location:
youtube-comments/trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • youtube-comments/trunk/class-admin.php

    r759170 r786864  
    5252    public function settings_help() {
    5353        $screen = get_current_screen();
    54         if ($screen->id != $this->settings_page)
     54        if ($screen->id != $this->settings_page) {
    5555            return;
     56        }
    5657        $screen->add_help_tab(array(
    5758            'id'      => 'ppf-overview',
     
    7576    */
    7677    public function settings_template() {
    77         if (!current_user_can('manage_options'))
     78        if (!current_user_can('manage_options')) {
    7879            wp_die( __('You do not have sufficient permissions to access this page.') );
    79         else
     80        } else {
    8081            include('template-settings.php');
     82        }
    8183    }
    8284   
     
    9799        add_settings_field('yc_settings_max_results', 'Request Comments', array($this, 'settings_max_results'), 'yc_settings_page', 'yc_settings_common');
    98100        add_settings_field('yc_settings_post_comments', 'Post Comments', array($this, 'settings_post_comments'), 'yc_settings_page', 'yc_settings_common');
     101        add_settings_field('yc_settings_custom_fields', 'Custom Fields', array($this, 'settings_custom_fields'), 'yc_settings_page', 'yc_settings_common');
    99102        add_settings_field('yc_settings_client_id', 'Client ID', array($this, 'settings_client_id'), 'yc_settings_page', 'yc_settings_api');
    100103        add_settings_field('yc_settings_client_secret', 'Client Secret', array($this, 'settings_client_secret'), 'yc_settings_page', 'yc_settings_api');
     
    133136        echo "<p class='description'>Allow users to post comments on YouTube.</p>" . PHP_EOL;
    134137    }
     138   
     139    /**
     140    * Setting field for custom fields
     141    */
     142    public function settings_custom_fields() {
     143        $settings = get_option('yc_settings');
     144        $value = empty($settings['custom_fields']) ? '' : $settings['custom_fields'];
     145        echo "<input type='text' name='yc_settings[custom_fields]' class='regular-text' value='" . $value . "' />" . PHP_EOL;
     146        echo "<p class='description'>Enter the names of custom fields for videos.<br />Separate multiple names with commas.</p>" . PHP_EOL;
     147    }       
    135148
    136149    /**
  • youtube-comments/trunk/class-comments.php

    r759170 r786864  
    66   
    77    protected static $instance = null;
    8     const PLUGIN_VERSION = '1.1.0';
     8    const PLUGIN_VERSION = '1.2.0';
    99    private $client = null;
    1010    private $youtube = null;
     11    private $has_video = false;
    1112
    1213    /**
     
    3637        add_action('wp_ajax_nopriv_post_logoff', array($this, 'ajax_post_logoff'));             
    3738        add_shortcode('youtube-comments', array($this, 'handle_shortcode'));   
     39        // Avoid adding container twice
    3840        if (!empty($settings['show_all'])) {
    3941            add_filter('the_content', array($this, 'filter_content'));
     
    5961    */
    6062    public function filter_content($content) {
    61         if (is_single() || is_page()) {
    62             if ($this->get_first_video($content))
    63                 $content .= $this->add_container();
     63        if ($this->has_video) {
     64            $content .= $this->add_container();
    6465        }
    6566        return $content;
     
    6869    /**
    6970    * Handle shortcode
     71    *
     72    * @return string   
    7073    */
    7174    public function handle_shortcode() {
    7275        if (is_single() || is_page()) {
    7376            $settings = get_option('yc_settings');
    74             if (empty($settings['show_all']))
     77            if ($this->has_video && empty($settings['show_all'])) {
    7578                return $this->add_container();
    76             else
    77                 return ''; // Hide shortcode       
     79            } else {
     80                return ''; // Hide shortcode
     81            }
    7882        }
    7983    }
     
    100104        global $post;
    101105        if (is_single() || is_page()) {
     106            $video_id = $this->get_first_video($post->ID);
     107            if (empty($video_id)) {
     108                return;
     109            }
     110            $this->has_video = true;
    102111            $settings = get_option('yc_settings');
    103112            $results = empty($settings['max_results']) ? '10' : $settings['max_results'];
    104113            wp_enqueue_style('youtube-comments', plugins_url('style.css', __FILE__), array(), self::PLUGIN_VERSION);   
    105114            wp_enqueue_script('youtube-comments', plugins_url('script.js', __FILE__), array('jquery'), self::PLUGIN_VERSION, true);
    106             $content = get_post_field('post_content', $post->ID);
    107             $video_id = $this->get_first_video($content);
    108115            $vars = array(
    109116                'ajaxURL' => admin_url('admin-ajax.php'),
     
    129136    */
    130137    public function ajax_post_comment() {
    131         if (!wp_verify_nonce($_POST['nonce'], 'post-comment'))
     138        if (!wp_verify_nonce($_POST['nonce'], 'post-comment')) {
    132139            die('Error: Security check failed');
    133         if (empty($_SESSION['access-token']))
     140        }
     141        if (empty($_SESSION['access-token'])) {
    134142            die('Error: No access token');
     143        }
    135144        // Validate access token
    136145        $token = json_decode($_SESSION['access-token']);
    137         if (!$this->validate_token($token->access_token))
     146        if (!$this->validate_token($token->access_token)) {
    138147            die('Error: Access token invalid');
     148        }
    139149        $this->client->setAccessToken($_SESSION['access-token']);
    140150        if ($this->client->isAccessTokenExpired()) {
     
    172182    */
    173183    public function ajax_post_logoff() {
    174         if (!wp_verify_nonce($_POST['nonce'], 'post-comment'))
     184        if (!wp_verify_nonce($_POST['nonce'], 'post-comment')) {
    175185            die('Error: Security check failed');
     186        }
    176187        unset($_SESSION['access-token']);
    177188        $this->client->revokeToken();
     
    192203            if (empty($values->error)) {
    193204                $settings = get_option('yc_settings');
    194                 if (!empty($values->audience) && $values->audience == $settings['client_id'])
     205                if (!empty($values->audience) && $values->audience == $settings['client_id']) {
    195206                    return true;
     207                }
    196208            }
    197209        }
     
    200212   
    201213    /**
    202     * Parse content to get first video
    203     *
    204     * @param string $content   
    205     * @return string
    206     */
    207     public function get_first_video($content) {
    208         global $post;
    209         // Check for "Automatic YouTube Video Posts" plugin
    210         $video_id = get_post_meta($post->ID, '_tern_wp_youtube_video', true);
    211         if (!empty($video_id))
     214    * Search for first video ID
     215    *
     216    * @param integer $post_id   
     217    * @return string
     218    */
     219    public function get_first_video($post_id) {
     220        // Custom field for "Automatic YouTube Video Posts" plugin
     221        if (function_exists('WP_ayvpp_init')) {
     222            $video_id = get_post_meta($post_id, '_tern_wp_youtube_video', true);
     223            if (!empty($video_id)) {
     224                return $video_id;
     225            }
     226        }
     227        // Custom field for "deTube" premium theme
     228        $video_url = get_post_meta($post_id, 'dp_video_url', true);
     229        if (!empty($video_url)) {
     230            $video_id = $this->parse_youtube_url($video_url);
     231            if (!empty($video_id)) {
     232                return $video_id;   
     233            }
     234        }
     235        // Other custom fields
     236        $settings = get_option('yc_settings');
     237        $fields = empty($settings['custom_fields']) ? '' : trim($settings['custom_fields']);
     238        if (!empty($fields)) {
     239            $fields = explode(',', $settings['custom_fields']);
     240            foreach ($fields as $key) {
     241                $key = trim($key);
     242                $value = get_post_meta($post_id, $key, true);
     243                $value = trim($value);
     244                if (!empty($value)) {
     245                    // Custom field may contain URL or ID, so assume ID if not valid URL
     246                    $video_id = $this->parse_youtube_url($value);
     247                    if (!empty($video_id)) {
     248                        return $video_id;   
     249                    } else {
     250                        return $value;
     251                    }
     252                }
     253            }
     254        }
     255        // Otherwise, search post content for URL
     256        $content = get_post_field('post_content', $post_id);
     257        $video_id = $this->parse_youtube_url($content);
     258        if (!empty($video_id)) {
    212259            return $video_id;
    213         // Otherwise, search content for link   
     260        }
     261        return false;
     262    }   
     263   
     264    /**
     265    * Parse YouTube URL to get video ID
     266    *
     267    * @param string $url
     268    * @return string
     269    */
     270    public function parse_youtube_url($url) {   
    214271        $pattern = '%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i';
    215         preg_match($pattern, $content, $match);
    216         if (empty($match[1]))
    217             return false;
    218         else
     272        preg_match($pattern, $url, $match);
     273        if (!empty($match[1])) {
    219274            return $match[1];
    220     }   
     275        }
     276        return false;   
     277    }
    221278
    222279    /**
     
    284341    function template_redirect() { 
    285342        if (isset($_GET['state']) && isset($_GET['code'])) {
    286             if (strval($_SESSION['state']) !== strval($_GET['state']))
     343            if (strval($_SESSION['state']) !== strval($_GET['state'])) {
    287344                die('The session state did not match.');
     345            }
    288346            $this->client->authenticate($_GET['code']);
    289347            $_SESSION['access-token'] = $this->client->getAccessToken();
    290             if (isset($_SESSION['redirect_url']))
     348            if (isset($_SESSION['redirect_url'])) {
    291349                wp_safe_redirect($_SESSION['redirect_url']);
     350            }
    292351        }
    293352    }
  • youtube-comments/trunk/readme.txt

    r759170 r786864  
    44Tags: YouTube, video, comments
    55Requires at least: 3.3
    6 Tested up to: 3.5.2
    7 Stable tag: 1.1.0
     6Tested up to: 3.6.1
     7Stable tag: 1.2.0
    88License: GPLv3 or later
    99License URI: http://www.gnu.org/licenses/gpl-3.0.html
     
    4848The redirect URI must exactly match your site's URL. An extra trailing slash is enough to cause that error.
    4949
     50= 5. Can I use a custom field for videos? =
     51
     52Yes, a setting is provided for custom fields. YouTube Comments supports the "Automatic YouTube Video Posts" plugin, and the "deTube" premium theme.
     53
    5054== Upgrade Notice ==
    5155
    52 No upgrades available.
     56No upgrade notices.
    5357
    5458== Changelog ==
     
    6165* Fixed bug in comment display.
    6266* Updated the readme.
     67
     68= 1.2.0 =
     69* Added support for "deTube" premium theme.
     70* Added setting for URL custom fields.
     71* Updated the readme.
  • youtube-comments/trunk/template-comments.php

    r759170 r786864  
    4949        if (strpos($atts->rel, 'in-reply-to')) {
    5050            $reply = @simplexml_load_file($atts->href); // suppress errors
    51             if (isset($reply->author))
     51            if (isset($reply->author)) {
    5252                $reply_link = sprintf("<a href='%s' title=''>%s</a>", $reply->author->uri, $reply->author->name);
     53            }
    5354            break;
    5455        }           
  • youtube-comments/trunk/template-container.php

    r743953 r786864  
    44*/
    55?>
     6
    67<div id='youtube-comments'>
    78    <div class='comments-loading'><img src='<?php echo plugins_url('loading.gif', __FILE__); ?>' />Loading...</div>
  • youtube-comments/trunk/uninstall.php

    r743953 r786864  
    44*/
    55
    6 if (!defined('WP_UNINSTALL_PLUGIN'))
    7     exit();
     6if (!defined('WP_UNINSTALL_PLUGIN')) {
     7    exit;
     8}
    89
    910delete_option('yc_settings');
  • youtube-comments/trunk/youtube-comments.php

    r759170 r786864  
    44Plugin Name: YouTube Comments
    55Description: This plugin finds YouTube links in post content and imports the video comments.
    6 Version: 1.1.0
     6Version: 1.2.0
    77Author: sydcode
    88Author URI: http://profiles.wordpress.org/sydcode
Note: See TracChangeset for help on using the changeset viewer.