WordPress.org

Plugin Directory

Changeset 602201


Ignore:
Timestamp:
09/22/12 00:37:45 (19 months ago)
Author:
betzster
Message:

Add Twitter Cards

Location:
tweet-posts/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • tweet-posts/trunk/readme.txt

    r602200 r602201  
    44Requires at least: 3.4 
    55Tested up to: 3.4.1 
    6 Stable tag: 0.1.1 
     6Stable tag: 0.2 
    77 
    88Tweet your posts. 
     
    1010== Description == 
    1111 
    12 Probably the simplest plugin that exists for automatically tweeting new posts. 
     12Tweets when you post, obviously. It looks at the post format to figure out what the message should look like. Also adds the appropriate meta tags to activate [Twitter Cards](https://dev.twitter.com/docs/cards). 
    1313 
    1414== Installation == 
     
    19194. Update the Twitter API information in the Options -> Tweet Posts menu 
    2020 
     21If you'd like to use Twitter Cards, apply for that on the [Twitter Dev](https://dev.twitter.com/form/participate-twitter-cards) site as well. 
     22 
    2123== Changelog == 
     24 
     25= 0.2 = 
     26* Add support for Twitter Cards 
     27* Add Twitter to user profiles 
    2228 
    2329= 0.1.1 = 
  • tweet-posts/trunk/tweet-posts.php

    r602200 r602201  
    44Plugin URI: http://joshbetz.com/2012/08/tweet-posts/ 
    55Description: Tweet your posts. 
    6 Version: 0.1.1 
     6Version: 0.2 
    77Author: Josh Betz 
    88Author URI: http://joshbetz.com 
     
    1313class Tweet_Posts { 
    1414 
    15     const VERSION = '0.1.1'; 
     15    const VERSION = '0.2'; 
    1616    const SLUG    = 'tweet-posts'; 
    1717    const OPTION  = 'tweet_posts'; 
     
    2121    function __construct() { 
    2222        add_action( 'init', array( $this, 'init' ) ); 
     23 
     24        add_action( 'wp_head', array( $this, 'wp_head' ) ); 
     25 
     26        /* Admin actions */ 
    2327        add_action( 'admin_init', array( $this, 'admin_init' ) ); 
    2428        add_action( 'admin_menu', array( $this, 'admin_menu' ) ); 
     
    3034    function init() { 
    3135        register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) ); 
     36 
     37        add_filter( 'user_contactmethods', array( $this, 'user_contactmethods') ); 
    3238    } 
    3339 
    3440    function deactivate() { 
    3541        unregister_setting( self::OPTION, self::OPTION, array( $this, 'validate_settings' ) ); 
     42    } 
     43 
     44    function user_contactmethods( $m ) { 
     45        $m['twitter'] = __( 'Twitter', 'tweet-posts' ); 
     46        return $m; 
     47    } 
     48 
     49    function wp_head() { 
     50        if ( is_single() ) { 
     51            // We need to see the post so we know what to fill in 
     52            the_post(); 
     53 
     54            if ( !empty( $this->options['twitter_site'] ) ) 
     55                echo '<meta name="twitter:site" content="@' . $this->options['twitter_site'] . '">'; 
     56 
     57            $twitter = get_the_author_meta( 'twitter' ); 
     58            if ( !empty( $twitter ) ) 
     59                echo '<meta name="twitter:creator" content="@' . get_the_author_meta( 'twitter'  ) . '">'; 
     60 
     61            switch ( get_post_format() ) { 
     62                case 'image': 
     63                    echo '<meta name="twitter:card" content="photo">'; 
     64                    break; 
     65                case 'video': 
     66                case 'audio': 
     67                    //TODO: implement player cards 
     68                    //echo '<meta name="twitter:card" content="player">'; 
     69                    //echo '<meta name="twitter:player" content="">'; 
     70                    //echo '<meta name="twitter:player:stream" content="">'; 
     71                    //echo '<meta name="twitter:player:stream:content_type" content="">'; 
     72                    //echo '<meta name="twitter:image" content="">'; 
     73                    break; 
     74                default: 
     75                    echo '<meta name="twitter:card" content="summary">'; 
     76                    break; 
     77            } 
     78            echo '<meta name="twitter:url" content="' . get_permalink() . '">'; 
     79            echo '<meta name="twitter:title" content="' . get_the_title() . '">'; 
     80            echo '<meta name="twitter:description" content="' . wp_trim_excerpt( get_the_excerpt() ) . '">'; 
     81 
     82            if ( has_post_thumbnail() ) { 
     83                $src = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' ); 
     84                echo '<meta name="twitter:image" content="' . $src[0] . '">'; 
     85            } 
     86 
     87            // Rewind the loop even though it's a single page 
     88            rewind_posts(); 
     89        } 
    3690    } 
    3791 
     
    50104 
    51105    function do_tweet( $tweet ) { 
    52         $connection = new TwitterOAuth( $this->keys['consumer_key'], $this->keys['consumer_secret'], $this->keys['oauth_token'], $this->keys['oauth_token_secret'] ); 
     106        $connection = new TwitterOAuth( $this->options['consumer_key'], $this->options['consumer_secret'], $this->options['oauth_token'], $this->options['oauth_token_secret'] ); 
    53107 
    54108        $args = array( 
     
    110164            <h2><?php _e( 'Tweet Posts', 'tweet-posts' ); ?></h2> 
    111165            <form action="options.php" method="post"> 
    112                 <?php settings_fields( self::OPTION ); ?> 
    113                 <?php do_settings_sections( self::SLUG ); ?> 
    114                 <?php submit_button(); ?> 
     166                <?php 
     167                    settings_fields( self::OPTION ); 
     168                    do_settings_sections( self::SLUG ); 
     169                    submit_button(); 
     170                ?> 
    115171            </form> 
    116172        </div> 
     
    119175    function admin_init() { 
    120176        register_setting( self::OPTION, self::OPTION, array( $this, 'validate_settings' ) ); 
    121         add_settings_section( self::SLUG, __( 'Twitter API', 'tweet-posts' ), array( $this, 'api_section' ), self::SLUG ); 
    122         add_settings_field( 'consumer_key', __( 'Consumer Key', 'tweet-posts' ), array( $this, 'input' ), self::SLUG, self::SLUG, array( 'name' => 'consumer_key' ) ); 
    123         add_settings_field( 'consumer_secret', __( 'Consumer Secret', 'tweet-posts' ), array( $this, 'input' ), self::SLUG, self::SLUG, array( 'name' => 'consumer_secret' ) ); 
    124         add_settings_field( 'oauth_token', __( 'OAuth Token', 'tweet-posts' ), array( $this, 'input' ), self::SLUG, self::SLUG, array( 'name' => 'oauth_token' ) ); 
    125         add_settings_field( 'oauth_token_secret', __( 'OAuth Token Secret', 'tweet-posts' ), array( $this, 'input' ), self::SLUG, self::SLUG, array( 'name' => 'oauth_token_secret' ) ); 
    126     } 
     177        add_settings_section( self::SLUG . 'info', __( 'Twitter Account Info', 'tweet-posts' ), array( $this, 'twitter_section' ), self::SLUG ); 
     178        add_settings_field( 'twitter_site', __( 'Site Twitter', 'tweet-posts' ), array( $this, 'input' ), self::SLUG, self::SLUG . 'info', array( 'name' => 'twitter_site' ) ); 
     179 
     180        add_settings_section( self::SLUG . 'api', __( 'Twitter API', 'tweet-posts' ), array( $this, 'api_section' ), self::SLUG ); 
     181        add_settings_field( 'consumer_key', __( 'Consumer Key', 'tweet-posts' ), array( $this, 'input' ), self::SLUG, self::SLUG . 'api', array( 'name' => 'consumer_key' ) ); 
     182        add_settings_field( 'consumer_secret', __( 'Consumer Secret', 'tweet-posts' ), array( $this, 'input' ), self::SLUG, self::SLUG . 'api', array( 'name' => 'consumer_secret' ) ); 
     183        add_settings_field( 'oauth_token', __( 'OAuth Token', 'tweet-posts' ), array( $this, 'input' ), self::SLUG, self::SLUG . 'api', array( 'name' => 'oauth_token' ) ); 
     184        add_settings_field( 'oauth_token_secret', __( 'OAuth Token Secret', 'tweet-posts' ), array( $this, 'input' ), self::SLUG, self::SLUG . 'api', array( 'name' => 'oauth_token_secret' ) ); 
     185    } 
     186 
     187    function twitter_section() { ?> 
     188        <p class="description">Fill in your site's Twitter account information. You can change your personal Twitter username in <a href="<?php echo admin_url( 'profile.php' ); ?>">your profile</a>.</p> 
     189<?php } 
    127190 
    128191    function api_section() { ?> 
    129         <p>Fill in your Twitter API details.</p> 
     192        <p class="description">Fill in your Twitter API details.</p> 
    130193<?php } 
    131194 
    132195    function input( $post ) { 
    133         $options = $this->keys; 
     196        $options = $this->options; 
    134197        $setting = $post['name']; 
    135         $value = !empty( $options ) ? $options[$setting] : ''; ?> 
     198        $value = !empty( $options[$setting] ) ? $options[$setting] : ''; ?> 
    136199        <input id='<?php echo $setting; ?>' name='<?php echo self::OPTION . '[' . $setting . ']'; ?>' size='40' type='text' value='<?php echo $value; ?>' /> 
    137200<?php } 
    138201 
    139202    function validate_settings( $input ) { 
     203        // API Data 
    140204        $new['consumer_key'] = esc_attr( $input['consumer_key'] ); 
    141205        $new['consumer_secret'] = esc_attr( $input['consumer_secret'] ); 
    142206        $new['oauth_token'] = esc_attr( $input['oauth_token'] ); 
    143207        $new['oauth_token_secret'] = esc_attr( $input['oauth_token_secret'] ); 
     208 
     209        // Twitter Account Data 
     210        $new['twitter_site'] = esc_attr( $input['twitter_site'] ); 
    144211        return $new; 
    145212    } 
Note: See TracChangeset for help on using the changeset viewer.