Created
March 21, 2013 20:20
-
-
Save letsgetrandy/5216355 to your computer and use it in GitHub Desktop.
Wordpress plugin to crosspost to Posterous blogs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=== Crossposterous === | |
Contributors: Randy Hunt | |
Tags: posterous, posts, crosspost | |
Requires at least: 2.7 | |
Tested up to: 3.0 | |
Stable tag: 1.2.1 | |
WordPress to Posterous cross-posting plugin. | |
== Description == | |
Crossposterous will cross-post your Wordpress blog post to your Posterous site. You can choose whether to post a teaser with a link back your Wordpress blog post or cross-post the full content. | |
== Installation == | |
1. If you are upgrading, it is recommended that you deactivate the plugin from the Plugins page, and delete the posterize folder from your server. | |
2. Upload the unzipped contents to your /wp-content/plugins/ directory. | |
3. Active the plugin from the Plugins page. | |
4. Go to settings page to configure Crossposterous | |
== Configuration == | |
1. Enter your Posterous email and password (used to log into http://posterous.com) | |
2. Select the site from the drop-down | |
3. Chose "Link back to post" or "Post full content" | |
== Changelog == | |
= 1.2 = | |
* Corrected a "function_exists" bug | |
= 1.1 = | |
* Added php4 compatible xml parsing | |
= 1.0 = | |
* Initial version | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Crossposterous | |
Plugin URI: http://www.bbqiguana.com/wordpress-plugins/crossposterous/ | |
Description: This plugin will automatically cross-post your Wordpress blog entry to your Posterous site. | |
Version: 1.2.1 | |
Author: Randy Hunt | |
Author URI: http://www.bbqiguana.com/ | |
*/ | |
/* Copyright 2010 Randy Hunt (email : [email protected]) | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation; either version 2 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
*/ | |
/* When plugin is activated */ | |
register_activation_hook (__FILE__, 'crossposterous_activate' ); | |
register_deactivation_hook(__FILE__, 'crossposterous_deactivate'); | |
function crossposterous_activate () { | |
$options = array('email'=>'', 'password'=>'', 'siteid'=>'', 'posttype'=>''); | |
add_option('crossposterous', $options); | |
} | |
function crossposterous_deactivate () { | |
delete_option('crossposterous'); | |
} | |
if (is_admin()) { | |
add_action('admin_menu', 'crossposterous_admin_menu'); | |
add_action('admin_init', 'crossposterous_init'); | |
add_filter('plugin_row_meta', 'posterizePluginLinks',10,2); | |
function crossposterous_admin_menu(){ | |
add_options_page('Crossposterous Settings', 'Crossposterous', 'administrator', __FILE__, 'crossposterous_admin_page'); | |
} | |
function posterizePluginLinks($links, $file){ | |
if( $file == 'posterize/posterize.php') { | |
$links[] = '<a href="' . admin_url( 'options-general.php?page=posterize-settings' ) . '">' . __('Settings') . '</a>'; | |
$links[] = '<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=QC745TKR6AHBS" target="_blank">Donate</a>'; | |
} | |
return $links; | |
} | |
function crossposterous_init() { | |
register_setting('crossposterous','crossposterous'); | |
add_settings_section('posterous', 'Posterous settings', 'crossposterous_foo', __FILE__); | |
add_settings_field('email', 'Email address', 'crossposterous_render_email', __FILE__, 'posterous'); | |
add_settings_field('password', 'Password', 'crossposterous_render_password', __FILE__, 'posterous'); | |
add_settings_field('siteid', 'Site ID', 'crossposterous_render_sites', __FILE__, 'posterous'); | |
add_settings_field('posttype', 'What to post', 'crossposterous_render_posttype', __FILE__, 'posterous'); | |
} | |
function crossposterous_foo() { | |
//echo '1.foo'; | |
} | |
function crossposterous_render_email () { | |
$options = get_option('crossposterous'); | |
echo '<input id="posterous_email" name="crossposterous[posterous_email]" size="40" type="text" value="' . $options['posterous_email'] . '" class="regular-text" />'; | |
} | |
function crossposterous_render_password () { | |
$options = get_option('crossposterous'); | |
echo '<input id="posterous_password" name="crossposterous[posterous_password]" size="40" type="text" value="' . $options['posterous_password'] . '" class="regular-text" />'; | |
} | |
function crossposterous_render_sites () { | |
$options = get_option('crossposterous'); | |
$email = $options['posterous_email']; | |
$password = $options['posterous_password']; | |
$xml = invoke_rest_api('http://posterous.com/api/getsites', 'GET', null, $email, $password); | |
if (function_exists('simplexml_load_string')) { | |
$root = simplexml_load_string($xml); | |
$data = get_object_vars($root); | |
} else { | |
require('xml2arr.php'); | |
$data = xml2array($xml); | |
if ($data['rsp']) $data = $data['rsp']; | |
} | |
$sites = array(); | |
if(!$data['site']){ | |
print '<div class="error"><strong>Error:</strong> Check your username and password</div>'; | |
} else { | |
$html = '<select id="posterous_site" name="crossposterous[siteid]"><option value=""></option>'; | |
// /if(is_array($data['site'])){ | |
// | |
// foreach ($data['site'] as $key => $value) { | |
// $site[$key] = $value; | |
// //foreach ($values as $key => $value) { | |
// // $sites['site'][$keys][$key] = $value; | |
// //} | |
// } | |
// $html .= "<option value=\"{$site['id']}\">{$site['name']}</option>"; | |
// //foreach($sites['site'] as $site){ | |
// // $html .= '<option value="'.$site['id'].'">'.$site['name'].'</option>'; | |
// //} | |
// }else{ | |
foreach ($data['site'] as $key => $value) { | |
$site[$key] = $value; | |
} | |
$html .= "<option value=\"{$site['id']}\"".($site['id']==$options['siteid']?" selected" :"").">{$site['name']}</option>"; | |
// } | |
echo $html . '</select>'; | |
} | |
} | |
function crossposterous_render_posttype () { | |
$options = get_option('crossposterous'); | |
echo '<input name="crossposterous[posttype]" type="radio" value="link"'.($options['posttype']=='link'?' checked':'').'> <label>Teaser and link.</label><br>'; | |
echo '<input name="crossposterous[posttype]" type="radio" value="full"'.($options['posttype']=='full'?' checked':'').'> <label>Post full content.</label><br>'; | |
} | |
function crossposterous_admin_page() { | |
echo '<div class="wrap">'; | |
echo '<div class="icon32" id="icon-options-general"><br></div>'; | |
echo '<h2>crossposterous</h2>'; | |
echo '<form action="options.php" method="post">'; | |
settings_fields('crossposterous'); | |
do_settings_sections(__FILE__); | |
echo '<p class="submit">'; | |
echo '<input name="Submit" type="submit" class="button-primary" value="' . __('Save Changes') . '" />'; | |
echo '</p>'; | |
echo '</form>'; | |
echo '</div>'; | |
} | |
} | |
function crossposterous_excerpt ($text, $more='... ', $limit=55) { | |
$text = strip_shortcodes( $text ); | |
$text = str_replace(']]>', ']]>', $text); | |
if (preg_match('/<!--more(.*?)?-->/', $text, $matches) ) { | |
$text = explode($matches[0], $text, 2); | |
$text = strip_tags($text[0]) . $more; | |
} else { | |
$text = strip_tags($text); | |
$words = explode(' ', $text, $limit + 1); | |
if (count($words) > $limit) { | |
array_pop($words); | |
$text = implode(' ', $words) . $more; | |
} | |
} | |
return $text; | |
} | |
function send_to_posterous ($post_ID) { | |
$options = get_option('crossposterous'); | |
$email = $options['posterous_email']; | |
$password = $options['posterous_password']; | |
$siteid = $options['posterous_siteid']; | |
if ($email!='' && $password!='') { | |
global $userdata; | |
get_currentuserinfo(); | |
$post = get_post($post_ID); | |
$data = array(); | |
$data['site_id'] = $options['siteid']; | |
$data['title'] = $post->post_title; | |
if ($options['posttype'] == 'full') { | |
$data['body'] = $post->post_content; | |
} else { | |
$blog_title = get_bloginfo(); | |
$data['body'] = crossposterous_excerpt($post->post_content) . ' <a href="'.get_permalink($post_ID).'">continue reading at ' . $blog_title . '</a>'; | |
} | |
$tags = array(); | |
$posttags = get_the_tags($post_ID); | |
if ($posttags) { | |
foreach($posttags as $tag) { | |
$tag[] = $tag->name; | |
} | |
} | |
$data['tags'] = implode(',', $tags); | |
$data['source'] = 'Crossposterous'; | |
$data['sourceLink'] = 'http://www.bbqiguana.com/wordpress-plugins/crossposterous/'; | |
$url = 'http://posterous.com/api/newpost'; | |
invoke_rest_api($url, 'POST', $data, $email, $password); | |
} | |
} | |
function invoke_rest_api ($endpoint, $method = 'GET', $data = null, $username = '', $password = '') { | |
$url = $endpoint; | |
$ch = curl_init(); | |
if($data) { | |
$postdata = array(); | |
foreach ($data as $key=>$value) | |
array_push($postdata, $key . '=' . urlencode($value)); | |
if ($method = 'POST') { | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $postdata)); | |
} else { | |
$url .= '?' . implode('&', $postdata); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
} | |
} else { | |
curl_setopt($ch, CURLOPT_URL, $url); | |
} | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
if ($username && $password) { | |
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); | |
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password); | |
} | |
$response = curl_exec($ch); | |
curl_close($ch); | |
return $response; | |
} | |
add_action('new_to_publish', 'send_to_posterous'); | |
add_action('draft_to_publish', 'send_to_posterous'); | |
add_action('future_to_publish', 'send_to_posterous'); | |
add_action('pending_to_publish', 'send_to_posterous'); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* xml2array() will convert the given XML text to an array in the XML structure. | |
* Link: http://www.bin-co.com/php/scripts/xml2array/ | |
* Arguments : $contents - The XML text | |
* $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value. | |
* $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance. | |
* Return: The parsed XML in an array form. Use print_r() to see the resulting array structure. | |
* Examples: $array = xml2array(file_get_contents('feed.xml')); | |
* $array = xml2array(file_get_contents('feed.xml', 1, 'attribute')); | |
*/ | |
function xml2array($contents, $get_attributes=1, $priority = 'tag') { | |
if(!$contents) return array(); | |
if(!function_exists('xml_parser_create')) { | |
//print "'xml_parser_create()' function not found!"; | |
return array(); | |
} | |
//Get the XML parser of PHP - PHP must have this module for the parser to work | |
$parser = xml_parser_create(''); | |
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss | |
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); | |
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); | |
xml_parse_into_struct($parser, trim($contents), $xml_values); | |
xml_parser_free($parser); | |
if(!$xml_values) return;//Hmm... | |
//Initializations | |
$xml_array = array(); | |
$parents = array(); | |
$opened_tags = array(); | |
$arr = array(); | |
$current = &$xml_array; //Refference | |
//Go through the tags. | |
$repeated_tag_index = array();//Multiple tags with same name will be turned into an array | |
foreach($xml_values as $data) { | |
unset($attributes,$value);//Remove existing values, or there will be trouble | |
//This command will extract these variables into the foreach scope | |
// tag(string), type(string), level(int), attributes(array). | |
extract($data);//We could use the array by itself, but this cooler. | |
$result = array(); | |
$attributes_data = array(); | |
if(isset($value)) { | |
if($priority == 'tag') $result = $value; | |
else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode | |
} | |
//Set the attributes too. | |
if(isset($attributes) and $get_attributes) { | |
foreach($attributes as $attr => $val) { | |
if($priority == 'tag') $attributes_data[$attr] = $val; | |
else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr' | |
} | |
} | |
//See tag status and do the needed. | |
if($type == "open") {//The starting of the tag '<tag>' | |
$parent[$level-1] = &$current; | |
if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag | |
$current[$tag] = $result; | |
if($attributes_data) $current[$tag. '_attr'] = $attributes_data; | |
$repeated_tag_index[$tag.'_'.$level] = 1; | |
$current = &$current[$tag]; | |
} else { //There was another element with the same tag name | |
if(isset($current[$tag][0])) {//If there is a 0th element it is already an array | |
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result; | |
$repeated_tag_index[$tag.'_'.$level]++; | |
} else {//This section will make the value an array if multiple tags with the same name appear together | |
$current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array | |
$repeated_tag_index[$tag.'_'.$level] = 2; | |
if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well | |
$current[$tag]['0_attr'] = $current[$tag.'_attr']; | |
unset($current[$tag.'_attr']); | |
} | |
} | |
$last_item_index = $repeated_tag_index[$tag.'_'.$level]-1; | |
$current = &$current[$tag][$last_item_index]; | |
} | |
} elseif($type == "complete") { //Tags that ends in 1 line '<tag />' | |
//See if the key is already taken. | |
if(!isset($current[$tag])) { //New Key | |
$current[$tag] = $result; | |
$repeated_tag_index[$tag.'_'.$level] = 1; | |
if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data; | |
} else { //If taken, put all things inside a list(array) | |
if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array... | |
// ...push the new element into that array. | |
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result; | |
if($priority == 'tag' and $get_attributes and $attributes_data) { | |
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data; | |
} | |
$repeated_tag_index[$tag.'_'.$level]++; | |
} else { //If it is not an array... | |
$current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value | |
$repeated_tag_index[$tag.'_'.$level] = 1; | |
if($priority == 'tag' and $get_attributes) { | |
if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well | |
$current[$tag]['0_attr'] = $current[$tag.'_attr']; | |
unset($current[$tag.'_attr']); | |
} | |
if($attributes_data) { | |
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data; | |
} | |
} | |
$repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken | |
} | |
} | |
} elseif($type == 'close') { //End of tag '</tag>' | |
$current = &$parent[$level-1]; | |
} | |
} | |
return($xml_array); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment