WordPress.org

Plugin Directory

Changeset 626489


Ignore:
Timestamp:
11/17/12 20:07:44 (17 months ago)
Author:
niallkennedy
Message:

version 1.0.3: change description generator. do not require a description property on photo type. mention filters in readme

Location:
twitter-cards/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • twitter-cards/trunk/includes/twitter-cards-php/README.md

    r559436 r626489  
    55The Twitter_Cards PHP class helps you build Twitter Card markup for your website. Build a summary or photo card, set the appropriate attributes, and build `<meta>` elements suitable for output inside your (x)HTML document `<head>`. 
    66 
    7 Note: As of June 2012 Twitter Card display on Twitter.com or its applications requires [whitelisting domains](https://dev.twitter.com/form/participate-twitter-cards "request Twitter Card domain whitelist inclusion"). 
     7Note: As of November 2012 Twitter Card display on Twitter.com or its applications requires [whitelisting domains](https://dev.twitter.com/form/participate-twitter-cards "request Twitter Card domain whitelist inclusion"). 
     8 
     9Test your markup and/or URLs using in the [Twitter Card preview tool](https://dev.twitter.com/docs/cards/preview). 
    810 
    911## Summary example 
     
    8284An image is a required property of a photo card. A specified image must be at least 280 pixels wide and 150 pixels tall. Your image may be resized up to a width of 560 pixels; plan your content accordingly. 
    8385 
     86The description property is optional. 
     87 
    8488## Player example 
    8589 
  • twitter-cards/trunk/includes/twitter-cards-php/twitter-card.php

    r566472 r626489  
    44 * 
    55 * @since 1.0 
    6  * @version 1.01 
     6 * @version 1.02 
    77 * @author Niall Kennedy <niall@niallkennedy.com> 
    88 * @link https://dev.twitter.com/docs/cards Twitter Card documentation 
     
    1616     * @var string 
    1717     */ 
    18     const VERSION = '1.01'; 
     18    const VERSION = '1.02'; 
    1919 
    2020    /** 
     
    303303     */ 
    304304    private function required_properties_exist() { 
    305         if ( ! ( isset( $this->url ) && isset( $this->title ) && isset( $this->description ) ) ) 
     305        if ( ! ( isset( $this->url ) && isset( $this->title ) ) ) 
    306306            return false; 
     307 
     308        // description required for summary & video but not photo 
     309        if ( ! isset( $this->description ) && $this->card !== 'photo' ) 
     310            return false; 
     311 
     312        // image optional for summary 
    307313        if ( in_array( $this->card, array( 'photo', 'player' ), true ) && ! ( isset( $this->image ) && isset( $this->image->url ) ) ) 
    308314            return false; 
     315 
     316        // video player needs a video 
    309317        if ( $this->card === 'player' && ! ( isset( $this->video ) && isset( $this->video->url ) && isset( $this->video->width ) && isset( $this->video->height ) ) ) 
    310318            return false; 
     
    325333            'card' => $this->card, 
    326334            'url' => $this->url, 
    327             'title' => $this->title, 
    328             'description' => $this->description 
     335            'title' => $this->title 
    329336        ); 
     337 
     338        if ( isset( $this->description ) ) 
     339            $t['description'] = $this->description; 
    330340 
    331341        // add an image 
  • twitter-cards/trunk/readme.txt

    r566472 r626489  
    44Requires at least: 3.3 
    55Tested up to: 3.4 
    6 Stable tag: 1.0.2 
     6Stable tag: 1.0.3 
    77License: GPLv2 
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html 
     
    1616All cards are a Twitter Card summary by default. Tap into the `twitter_card_properties` filter to reference the Twitter accounts of your site or author. 
    1717 
     18== Filters == 
     19 
     20* `twitter_cards_properties` - act on an array of properties before they are output to the page 
     21* `twitter_cards_htmlxml` - override the default treatment of `html` with `xml`. XML will self-close the meta void element ( "<element />" vs. "<element>" ) 
     22 
    1823== Frequently Asked Questions == 
    1924 
     
    2227The plugin outputs HTML-style void elements without a trailing slash by default. Add XML-style trailing slashes by returning a value of `xml` on the `twitter_cards_htmlxml` filter. 
    2328 
     29== Upgrade Notice == 
     30 
     31= 1.0.3 = 
     32Simplify description generator. Allow no description for photo cards. 
     33 
     34= 1.0.2 = 
     35Change attribute from value to content to match current Twitter documentation. 
     36 
     37= 1.0.1 = 
     38Improved auto-generated description. 
     39 
    2440== Changelog == 
     41 
     42= 1.0.3 = 
     43* Simplify the description generator. Based on the Open Graph protocol description generator in the Facebook plugin. 
     44* Update bundled version of Twitter Cards PHP, fixing description property treated as a required property for photo card type. 
    2545 
    2646= 1.0.2 = 
    2747 
    28 Change attribute from value to content to match current Twitter documentation. 
     48* Change attribute from value to content to match current Twitter documentation. 
    2949 
    3050= 1.0.1 = 
     
    3454= 1.0 = 
    3555* Initial release 
    36  
    37 == Upgrade Notice == 
    38  
    39 = 1.0.2 = 
    40 Change attribute from value to content to match current Twitter documentation. 
    41  
    42 = 1.0.1 = 
    43 Improved auto-generated description. 
  • twitter-cards/trunk/twitter-cards.php

    r566472 r626489  
    66 * Author: Niall Kennedy 
    77 * Author URI: http://www.niallkennedy.com/ 
    8  * Version: 1.0.2 
     8 * Version: 1.0.3 
    99 */ 
    1010 
     
    1414 * 
    1515 * @since 1.0 
    16  * @version 1.0.2 
     16 * @version 1.0.3 
    1717 */ 
    1818class Twitter_Cards { 
     
    3434    public static function markup() { 
    3535        global $post; 
     36        if ( ! isset( $post ) ) 
     37            return; 
     38        setup_postdata( $post ); 
    3639 
    3740        if ( ! class_exists( 'Twitter_Card_WP' ) ) 
     
    3942 
    4043        $card = new Twitter_Card_WP(); 
    41         $card->setURL( apply_filters( 'rel_canonical', get_permalink() ) ); 
     44        $card->setURL( get_permalink() ); 
    4245        $post_type = get_post_type(); 
    4346        if ( post_type_supports( $post_type, 'title' ) ) 
     
    4548        if ( post_type_supports( $post_type, 'excerpt' ) ) { 
    4649            // one line, no HTML 
    47             $card->setDescription( self::make_description( $post ) ); 
     50            $description = self::make_description( $post ); 
     51            if ( $description ) 
     52                $card->setDescription( $description ); 
     53            unset( $description ); 
    4854        } 
    4955        // does current post type and the current theme support post thumbnails? 
     
    6874     */ 
    6975    public static function make_description( $post ) { 
    70         if ( ! isset( $post ) ) 
     76        if ( ! ( isset( $post ) && isset( $post->post_excerpt ) && isset( $post->post_content ) ) ) 
    7177            return ''; 
    7278 
    73         $text = ''; 
     79        // did the publisher specify a custom excerpt? use it 
     80        if ( ! empty( $post->post_excerpt ) ) 
     81            $description = apply_filters( 'get_the_excerpt', $post->post_excerpt ); 
     82        else 
     83            $description = $post->post_content; 
    7484 
    75         // allow plugins to modify, prepend, and append content in excerpt or main content 
    76         if ( ! empty( $post->post_excerpt ) ) { 
    77             // the_content may be triggered when building an excerpt from nothing 
    78             $filters = array( 'the_excerpt', 'the_content' ); 
    79             foreach ( $filters as $filter ) { 
    80                 remove_filter( $filter, 'wptexturize' ); 
    81             } 
    82             $text = trim( apply_filters( 'the_excerpt', $post->post_excerpt ) ); 
    83             foreach ( $filters as $filter ) { 
    84                 add_filter( $filter, 'wptexturize' ); 
    85             } 
    86             unset( $filters ); 
    87         } else if ( isset( $post->post_content ) ) { 
    88             remove_filter( 'the_content', 'wptexturize' ); 
    89             $text = trim( apply_filters( 'the_content', $post->post_content ) ); 
    90             add_filter( 'the_content', 'wptexturize' ); 
    91         } 
    92  
    93         if ( empty( $text ) ) 
     85        $description = trim( $description ); 
     86        if ( ! $description ) 
    9487            return ''; 
    9588 
    96         // shortcodes should have been handled in the_content filter 11. if they are still present then strip 
    97         $text = strip_shortcodes( $text ); 
     89        // basic filters from wp_trim_excerpt() that should apply to both excerpt and content 
     90        // note: the_content filter is so polluted with extra third-party stuff we purposely avoid to better represent page content 
     91        $description = strip_shortcodes( $description ); // remove any shortcodes that may exist, such as an image macro 
     92        $description = str_replace( ']]>', ']]&gt;', $description ); 
     93        $description = trim( $description ); 
    9894 
    99         $text = str_replace( ']]>', ']]&gt;', $text ); 
    100         $text = wp_strip_all_tags( $text ); 
    101         $text = str_replace( array( "\r\n", "\r", "\n" ), ' ', $text ); 
     95        if ( ! $description ) 
     96            return ''; 
    10297 
    103         $excerpt_more = apply_filters( 'excerpt_more', '[...]' ); 
     98        // use the built-in WordPress function for localized support of words vs. characters 
     99        // Twitter asks for 200 characters. look for 300 to provide a buffer, allow for change, and support possible uses by other consuming agents 
     100        // assume ~4 characters per word, 75 words max if 300 characters 
     101        $description = wp_trim_words( $description, 75, '' ); 
    104102 
    105         // prep for a pure string compare 
    106         $excerpt_more = html_entity_decode( $excerpt_more, ENT_QUOTES, 'UTF-8' ); 
    107         $excerpt_more = trim( $excerpt_more ); 
    108         $text = html_entity_decode( $text, ENT_QUOTES, 'UTF-8' ); 
    109         $text = trim( $text ); 
     103        if ( $description ) 
     104            return $description; 
    110105 
    111         if ( $excerpt_more ) { 
    112             $excerpt_more_length = strlen( $excerpt_more ); 
    113             // test if text ends with excerpt more. if so, remove it 
    114             if ( strlen( $text ) > $excerpt_more_length && substr_compare( $text, $excerpt_more, $excerpt_more_length * -1, $excerpt_more_length ) === 0 ) { 
    115                 $text = trim( substr( $text, 0, $excerpt_more_length * -1 ) ); 
    116             } 
    117         } 
    118  
    119         // Twitter asks for 200 characters. look for 300 to provide a buffer, allow for change, and support possible uses by other consuming agents 
    120         if ( strlen( $text ) > 300 ) { 
    121             // assume ~4 characters per word, 75 words max if 300 characters 
    122             $text = trim( wp_trim_words( $text, 75, '' ) ); 
    123         } 
    124  
    125         return $text; 
     106        return ''; 
    126107    } 
    127108} 
Note: See TracChangeset for help on using the changeset viewer.