Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move tweet_webcomic function and add custom field token support.
Since WebcomicPost is only loaded in the admin it was possible for a
transition_post_status event to fire without actually calling the
tweet_webcomic function.

Also adds support for custom field tokens.
  • Loading branch information
Michael Sisk committed Jan 25, 2013
1 parent 69553f2 commit 48aa157
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 65 deletions.
65 changes: 0 additions & 65 deletions -/php/posts.php
Expand Up @@ -36,7 +36,6 @@ public function __construct() {
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'restrict_manage_posts', array( $this, 'restrict_manage_posts' ) );
add_action( 'wp_insert_post', array( $this, 'save_webcomic_commerce' ), 10, 2 );
add_action( 'transition_post_status', array( $this, 'tweet_webcomic' ), 10, 3 );
add_action( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10, 2 );
add_action( 'wp_insert_post', array( $this, 'save_webcomic_transcripts' ), 10, 2 );
add_action( 'bulk_edit_custom_box', array( $this, 'bulk_edit_custom_box' ), 10, 2 );
Expand Down Expand Up @@ -229,70 +228,6 @@ public function save_webcomic_commerce( $id, $post ) {
}
}

/** Auto tweet on webcomic publish.
*
* @param string $new New post status.
* @param string $old Old post status.
* @param string $post Post object to update.
* @uses Webcomic::$config
* @hook transition_post_status
* @filter string webcomic_tweet Filters the tweet text pushed to Twitter whenever a webcomic is published. Defaults to the collection-specific tweet format.
*/
public function tweet_webcomic( $new, $old, $post ) {
if ( 'publish' === $new and 'publish' !== $old and !empty( self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'format' ] ) and !empty( self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'oauth_token' ] ) and !empty( self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'oauth_secret' ] ) ) {
$status = self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'format' ];

if ( false !== strpos( self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'format' ], '%' ) ) {
$s = $c = array();
$link = wp_get_shortlink( $post->ID );

if ( false !== strpos( self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'format' ], '%storylines' ) and $storylines = wp_get_object_terms( $post->ID, "{$post->post_type}_storyline" ) and !is_wp_error( $storylines ) ) {
foreach ( $storylines as $storyline ) {
$s[] = str_replace( array( '_', '-' ), '', "#{$storyline->slug}" );
}
}

if ( false !== strpos( self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'format' ], '%characters' ) and $characters = wp_get_object_terms( $post->ID, "{$post->post_type}_character" ) and !is_wp_error( $characters ) ) {
foreach ( $characters as $character ) {
$c[] = str_replace( array( '_', '-' ), '', "#{$character->slug}" );
}
}

$tokens = array(
'%url' => $link ? $link : get_permalink( $post->ID ),
'%date' => get_the_time( get_option( 'date_format' ), $post ),
'%time' => get_the_time( get_option( 'time_format' ), $post ),
'%title' => get_the_title( $post->ID ),
'%author' => get_the_author_meta( 'display_name', $post->post_author ),
'%site-url' => home_url(),
'%permalink' => get_permalink( $post->ID ),
'%site-name' => get_bloginfo( 'name' ),
'%storylines' => join( ' ', $s ),
'%characters' => join( ' ', $c ),
'%collection' => '#' . str_replace( array( '_', '-' ), '', self::$config[ 'collections' ][ $post->post_type ][ 'slugs' ][ 'name' ] ),
'%collection-name' => strip_tags( self::$config[ 'collections' ][ $post->post_type ][ 'name' ] )
);

$status = apply_filters( 'webcomic_tweet', str_replace( array_keys( $tokens ), $tokens, $status ), $post );
}

if ( $status ) {
if ( !class_exists( 'TwitterOAuth' ) ) {
require_once self::$dir . '-/library/twitter.php';
}

$oauth = new TwitterOAuth( self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'consumer_key' ], self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'consumer_secret' ], self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'oauth_token' ], self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'oauth_secret' ] );
$response = $oauth->post( 'statuses/update', array( 'status' => substr( strip_tags( $status ), 0, 140 ) ) );

if ( isset( $response->error ) ) {
$errors = get_transient( 'webcomic_error' );

set_transient( 'webcomic_error', array_merge( array( sprintf( __( '<b>Twitter Error: %s</b>', 'webcomic' ), $response->error ) ), $errors ? $errors : array() ), 1 );
}
}
}
}

/** Update webcomic title, slug, and content prior to saving.
*
* @param array $data An array of post data.
Expand Down
73 changes: 73 additions & 0 deletions webcomic.php
Expand Up @@ -129,6 +129,7 @@ public function __construct() {
add_action( 'webcomic_buffer_alert', array( $this, 'buffer_alert' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'template_redirect', array( $this, 'template_redirect' ) );
add_action( 'transition_post_status', array( $this, 'tweet_webcomic' ), 10, 3 );

add_filter( 'request', array( $this, 'request' ), 10, 1 );
add_filter( 'get_term', array( $this, 'get_term' ), 10, 2 );
Expand Down Expand Up @@ -565,6 +566,78 @@ public function template_redirect() {
}
}

/** Auto tweet on webcomic publish.
*
* @param string $new New post status.
* @param string $old Old post status.
* @param string $post Post object to update.
* @uses Webcomic::$config
* @hook transition_post_status
* @filter string webcomic_tweet Filters the tweet text pushed to Twitter whenever a webcomic is published. Defaults to the collection-specific tweet format.
*/
public function tweet_webcomic( $new, $old, $post ) {
if ( 'publish' === $new and 'publish' !== $old and !empty( self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'format' ] ) and !empty( self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'oauth_token' ] ) and !empty( self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'oauth_secret' ] ) ) {
$status = self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'format' ];

if ( false !== strpos( self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'format' ], '%' ) ) {
$s = $c = $m = array();
$link = wp_get_shortlink( $post->ID );

if ( false !== strpos( self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'format' ], '%storylines' ) and $storylines = wp_get_object_terms( $post->ID, "{$post->post_type}_storyline" ) and !is_wp_error( $storylines ) ) {
foreach ( $storylines as $storyline ) {
$s[] = str_replace( array( '_', '-' ), '', "#{$storyline->slug}" );
}
}

if ( false !== strpos( self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'format' ], '%characters' ) and $characters = wp_get_object_terms( $post->ID, "{$post->post_type}_character" ) and !is_wp_error( $characters ) ) {
foreach ( $characters as $character ) {
$c[] = str_replace( array( '_', '-' ), '', "#{$character->slug}" );
}
}

if ( preg_match_all( '/%field:(?(?=\{).+?\}|\S+)/', self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'format' ], $matches ) ) {
foreach ( $matches[ 0 ] as $match ) {
if ( empty( $m[ $match ] ) ) {
$m[ $match ] = get_post_meta( $post->ID, str_replace( array( '%field:', '{', '}' ), '', $match ), true );
}
}
}

$tokens = array_merge( array(
'%url' => $link ? $link : get_permalink( $post->ID ),
'%date' => get_the_time( get_option( 'date_format' ), $post ),
'%time' => get_the_time( get_option( 'time_format' ), $post ),
'%title' => get_the_title( $post->ID ),
'%author' => get_the_author_meta( 'display_name', $post->post_author ),
'%site-url' => home_url(),
'%permalink' => get_permalink( $post->ID ),
'%site-name' => get_bloginfo( 'name' ),
'%storylines' => join( ' ', $s ),
'%characters' => join( ' ', $c ),
'%collection' => '#' . str_replace( array( '_', '-' ), '', self::$config[ 'collections' ][ $post->post_type ][ 'slugs' ][ 'name' ] ),
'%collection-name' => strip_tags( self::$config[ 'collections' ][ $post->post_type ][ 'name' ] )
), $m );

$status = apply_filters( 'webcomic_tweet', str_replace( array_keys( $tokens ), $tokens, $status ), $post );
}

if ( $status ) {
if ( !class_exists( 'TwitterOAuth' ) ) {
require_once self::$dir . '-/library/twitter.php';
}

$oauth = new TwitterOAuth( self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'consumer_key' ], self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'consumer_secret' ], self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'oauth_token' ], self::$config[ 'collections' ][ $post->post_type ][ 'twitter' ][ 'oauth_secret' ] );
$response = $oauth->post( 'statuses/update', array( 'status' => substr( strip_tags( $status ), 0, 140 ) ) );

if ( isset( $response->error ) ) {
$errors = get_transient( 'webcomic_error' );

set_transient( 'webcomic_error', array_merge( array( sprintf( __( '<b>Twitter Error: %s</b>', 'webcomic' ), $response->error ) ), $errors ? $errors : array() ), 1 );
}
}
}
}

/** Return the appropriate theme ID for custom collection themes.
*
* @param string $theme Name of the current theme.
Expand Down

0 comments on commit 48aa157

Please sign in to comment.