PageRenderTime 56ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wp-to-twitter/wp-to-twitter.php

https://gitlab.com/f3z0/tc-site
PHP | 1576 lines | 1433 code | 54 blank | 89 comment | 559 complexity | de8ca2af4fd03a7ce2a8c727e182809d MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, AGPL-1.0, Apache-2.0, LGPL-2.1
  1. <?php
  2. /*
  3. Plugin Name: WP to Twitter
  4. Plugin URI: http://www.joedolson.com/articles/wp-to-twitter/
  5. Description: Posts a Tweet when you update your WordPress blog or post to your blogroll, using your URL shortening service. Rich in features for customizing and promoting your Tweets.
  6. Version: 2.8.7
  7. Author: Joseph Dolson
  8. Author URI: http://www.joedolson.com/
  9. */
  10. /* Copyright 2008-2014 Joseph C Dolson (email : plugins@joedolson.com)
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  24. apply_filters( 'debug', 'WP to Twitter Init' );
  25. global $wp_version;
  26. $wp_content_url = content_url();
  27. $wp_content_dir = str_replace( '/plugins/wp-to-twitter','',plugin_dir_path( __FILE__ ) );
  28. if ( defined('WP_CONTENT_URL') ) { $wp_content_url = constant('WP_CONTENT_URL');}
  29. if ( defined('WP_CONTENT_DIR') ) { $wp_content_dir = constant('WP_CONTENT_DIR');}
  30. define( 'WPT_DEBUG', false );
  31. define( 'WPT_DEBUG_ADDRESS', 'debug@joedolson.com' );
  32. define( 'WPT_FROM', "From: \"".get_option('blogname')."\" <".get_option('admin_email').">" );
  33. // define( 'WPT_DEBUG_ADDRESS', 'debug@joedolson.com, yourname@youraddress.com' ); // for multiple recipients.
  34. $wp_plugin_url = plugins_url();
  35. include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); // required in order to access is_plugin_active()
  36. require_once( plugin_dir_path(__FILE__).'/wp-to-twitter-oauth.php' );
  37. require_once( plugin_dir_path(__FILE__).'/wp-to-twitter-shorteners.php' );
  38. require_once( plugin_dir_path(__FILE__).'/wp-to-twitter-manager.php' );
  39. require_once( plugin_dir_path(__FILE__).'/wpt-functions.php' );
  40. require_once( plugin_dir_path(__FILE__).'/wpt-feed.php' );
  41. require_once( plugin_dir_path(__FILE__).'/wpt-widget.php' );
  42. global $wpt_version;
  43. $wpt_version = "2.8.7";
  44. $plugin_dir = basename(dirname(__FILE__));
  45. load_plugin_textdomain( 'wp-to-twitter', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' );
  46. function wpt_pro_compatibility() {
  47. global $wptp_version;
  48. $current_wptp_version = '1.6.3';
  49. if ( version_compare( $wptp_version, $current_wptp_version, '<' ) ) {
  50. echo "<div class='error notice'><p class='upgrade'>".sprintf( __('The current version of WP Tweets PRO is <strong>%s</strong>. <a href="http://www.joedolson.com/articles/account/">Upgrade for best compatibility!</a>','wp-to-twitter'),$current_wptp_version )."</p></div>";
  51. }
  52. }
  53. $wpt_donate_url = "http://www.joedolson.com/articles/wp-tweets-pro/";
  54. function wpt_commments_removed() {
  55. if ( isset($_GET['dismiss']) ) {
  56. update_option( 'wpt_dismissed', 'true' );
  57. }
  58. if ( get_option( 'comment-published-update' ) == 1 && !function_exists( 'wpt_pro_exists' ) && get_option( 'wpt_dismissed' ) != 'true' ) {
  59. $update_notice = sprintf( __('Tweeting of comments has been moved to <a href="%1$s">WP Tweets PRO</a>. You will need to upgrade in order to Tweet comments. <a href="%2$s">Dismiss</a>'), 'http://www.joedolson.com/articles/wp-tweets-pro/', admin_url( "options-general.php?page=wp-to-twitter/wp-to-twitter.php&dismiss=true" ) );
  60. if ( is_admin() ) {
  61. echo "<div class='updated'><p>".($update_notice)."</p></div>";
  62. }
  63. }
  64. }
  65. // check for OAuth configuration
  66. function wpt_check_oauth( $auth=false ) {
  67. if ( !function_exists('wtt_oauth_test') ) {
  68. $oauth = false;
  69. } else {
  70. $oauth = wtt_oauth_test( $auth );
  71. }
  72. return $oauth;
  73. }
  74. function wpt_check_version() {
  75. global $wpt_version;
  76. $prev_version = get_option( 'wp_to_twitter_version' );
  77. if ( version_compare( $prev_version,$wpt_version,"<" ) ) {
  78. wptotwitter_activate();
  79. }
  80. }
  81. function wptotwitter_activate() {
  82. global $wpt_version;
  83. $prev_version = get_option( 'wp_to_twitter_version' );
  84. // this is a switch to plan for future versions
  85. $upgrade = version_compare( $prev_version, "2.3.1","<" );
  86. if ($upgrade) {
  87. $array =
  88. array(
  89. 'post'=> array(
  90. 'post-published-update'=>get_option('newpost-published-update'),
  91. 'post-published-text'=>get_option('newpost-published-text'),
  92. 'post-edited-update'=>get_option('oldpost-edited-update'),
  93. 'post-edited-text'=>get_option('oldpost-edited-text')
  94. ),
  95. 'page'=> array(
  96. 'post-published-update'=>get_option('jd_twit_pages'),
  97. 'post-published-text'=>get_option('newpage-published-text'),
  98. 'post-edited-update'=>get_option('jd_twit_edited_pages'),
  99. 'post-edited-text'=>get_option('oldpage-edited-text')
  100. )
  101. );
  102. add_option( 'wpt_post_types', $array );
  103. add_option( 'comment-published-update', 0 );
  104. add_option( 'comment-published-text', 'New comment on #title# #url#' );
  105. delete_option('newpost-published-update');
  106. delete_option('newpost-published-text');
  107. delete_option('oldpost-edited-update');
  108. delete_option('oldpost-edited-text');
  109. delete_option('newpage-published-text');
  110. delete_option('oldpage-edited-text');
  111. delete_option( 'newpost-published-showlink' );
  112. delete_option( 'oldpost-edited-showlink' );
  113. delete_option( 'jd_twit_pages' );
  114. delete_option( 'jd_twit_edited_pages' );
  115. delete_option( 'jd_twit_postie' );
  116. }
  117. $upgrade = version_compare( $prev_version, "2.3.3","<" );
  118. if ( $upgrade ) {
  119. delete_option( 'jd_twit_quickpress' );
  120. }
  121. $upgrade = version_compare( $prev_version, "2.3.4","<" );
  122. if ( $upgrade ) {
  123. add_option( 'wpt_inline_edits', '0' );
  124. }
  125. $upgrade = version_compare( $prev_version, "2.3.15","<" );
  126. if ( $upgrade ) {
  127. $use = get_option( 'use_tags_as_hashtags' );
  128. if ( $use == 1 ) {
  129. $wpt_settings = get_option( 'wpt_post_types' );
  130. $post_types = get_post_types( '', 'names' );
  131. foreach ( $post_types as $type ) {
  132. if ( isset($wpt_settings[$type]) ) {
  133. $t1 = $wpt_settings[$type]['post-published-text'].' #tags#';
  134. $t2 = $wpt_settings[$type]['post-edited-text'].' #tags#';
  135. $wpt_settings[$type]['post-published-text'] = $t1;
  136. $wpt_settings[$type]['post-edited-text'] = $t2;
  137. }
  138. }
  139. update_option('wpt_post_types',$wpt_settings );
  140. }
  141. delete_option( 'use_tags_as_hashtags' );
  142. }
  143. $upgrade = version_compare( $prev_version, "2.4.0","<" );
  144. if ( $upgrade ) {
  145. $perms = get_option('wtt_user_permissions');
  146. switch( $perms ) {
  147. case 'read':$update = 'subscriber'; break;
  148. case 'edit_posts':$update = 'contributor'; break;
  149. case 'publish_posts':$update = 'author'; break;
  150. case 'moderate_comments':$update = 'editor'; break;
  151. case 'manage_options':$update = 'administrator'; break;
  152. default:$update = 'administrator';
  153. }
  154. update_option( 'wtt_user_permissions',$update );
  155. }
  156. $upgrade = version_compare( $prev_version, "2.4.1","<" );
  157. if ( $upgrade ) {
  158. $subscriber = get_role('subscriber');
  159. $contributor = get_role('contributor');
  160. $author = get_role('author');
  161. $editor = get_role('editor');
  162. $administrator = get_role('administrator');
  163. $administrator->add_cap('wpt_twitter_oauth');
  164. $administrator->add_cap('wpt_twitter_custom');
  165. $administrator->add_cap('wpt_twitter_switch'); // can toggle tweet/don't tweet
  166. switch ( get_option('wtt_user_permissions') ) { // users that can add twitter information
  167. case 'subscriber': $subscriber->add_cap('wpt_twitter_oauth'); $contributor->add_cap('wpt_twitter_oauth'); $author->add_cap('wpt_twitter_oauth'); $editor->add_cap('wpt_twitter_oauth'); break;
  168. case 'contributor': $contributor->add_cap('wpt_twitter_oauth'); $author->add_cap('wpt_twitter_oauth'); $editor->add_cap('wpt_twitter_oauth'); break;
  169. case 'author': $author->add_cap('wpt_twitter_oauth'); $editor->add_cap('wpt_twitter_oauth'); break;
  170. case 'editor':$editor->add_cap('wpt_twitter_oauth'); break;
  171. case 'administrator': break;
  172. default:
  173. $role = get_role( get_option('wtt_user_permissions') );
  174. if ( is_object($role) ) {
  175. $role->add_cap('wpt_twitter_oauth');
  176. }
  177. break;
  178. }
  179. switch ( get_option('wtt_show_custom_tweet') ) { // users that can compose a custom tweet
  180. case 'subscriber': $subscriber->add_cap('wpt_twitter_custom'); $contributor->add_cap('wpt_twitter_custom'); $author->add_cap('wpt_twitter_custom'); $editor->add_cap('wpt_twitter_custom'); break;
  181. case 'contributor': $contributor->add_cap('wpt_twitter_custom'); $author->add_cap('wpt_twitter_custom'); $editor->add_cap('wpt_twitter_custom'); break;
  182. case 'author': $author->add_cap('wpt_twitter_custom'); $editor->add_cap('wpt_twitter_custom'); break;
  183. case 'editor':$editor->add_cap('wpt_twitter_custom'); break;
  184. case 'administrator': break;
  185. default:
  186. $role = get_role( get_option('wtt_show_custom_tweet') );
  187. if ( is_object($role) ) {
  188. $role->add_cap('wpt_twitter_custom');
  189. }
  190. break;
  191. }
  192. }
  193. $upgrade = version_compare( $prev_version, "2.4.13","<" );
  194. if ( $upgrade ) {
  195. $administrator = get_role('administrator');
  196. $administrator->add_cap('wpt_can_tweet');
  197. $editor = get_role('editor');
  198. if ( is_object( $editor ) ) { $editor->add_cap('wpt_can_tweet'); }
  199. $author = get_role('author');
  200. if ( is_object( $author ) ) { $author->add_cap('wpt_can_tweet'); }
  201. $contributor = get_role('contributor');
  202. if ( is_object( $contributor ) ) { $contributor->add_cap('wpt_can_tweet'); }
  203. update_option('wpt_can_tweet','contributor');
  204. }
  205. update_option( 'wp_to_twitter_version',$wpt_version );
  206. }
  207. // Function checks for an alternate URL to be Tweeted. Contribution by Bill Berry.
  208. function wpt_link( $post_ID ) {
  209. $ex_link = false;
  210. $external_link = get_option('jd_twit_custom_url');
  211. $permalink = get_permalink( $post_ID );
  212. if ( $external_link != '' ) {
  213. $ex_link = get_post_meta( $post_ID, $external_link, true );
  214. }
  215. return ( $ex_link ) ? $ex_link : $permalink;
  216. }
  217. function wpt_saves_error( $id, $auth, $twit, $error, $http_code, $ts ) {
  218. $http_code = (int) $http_code;
  219. if ( $http_code != 200 ) {
  220. add_post_meta( $id, '_wpt_failed', array( 'author'=>$auth, 'sentence'=>$twit, 'error'=>$error,'code'=>$http_code, 'timestamp'=>$ts ) );
  221. }
  222. }
  223. // This function performs the API post to Twitter
  224. function jd_doTwitterAPIPost( $twit, $auth=false, $id=false, $media=false ) {
  225. if ( !wpt_check_oauth( $auth ) ) {
  226. $error = __('This account is not authorized to post to Twitter.','wp-to-twitter' );
  227. wpt_saves_error( $id, $auth, $twit, $error, '401', time() );
  228. wpt_set_log( 'wpt_status_message', $id, $error );
  229. return true;
  230. } // exit silently if not authorized
  231. $check = ( !$auth ) ? get_option('jd_last_tweet') : get_user_meta( $auth, 'wpt_last_tweet', true ); // get user's last tweet
  232. // prevent duplicate Tweets
  233. if ( $check == $twit ) {
  234. if ( WPT_DEBUG && function_exists( 'wpt_pro_exists' ) ) {
  235. wpt_mail( "Matched: tweet identical: #$id","$twit, $auth, $id" ); // DEBUG
  236. }
  237. $error = __( 'This tweet is identical to another Tweet recently sent to this account.','wp-to-twitter' ).' '.__( 'Twitter requires all Tweets to be unique.', 'wp-to-twitter' );
  238. wpt_saves_error( $id, $auth, $twit, $error, '403', time() );
  239. wpt_set_log( 'wpt_status_message', $id, $error );
  240. return true;
  241. } else if ( $twit == '' || !$twit ) {
  242. if ( WPT_DEBUG && function_exists( 'wpt_pro_exists' ) ) {
  243. wpt_mail( "Tweet check: empty sentence: #$id","$twit, $auth, $id"); // DEBUG
  244. }
  245. $error = __('This tweet was blank and could not be sent to Twitter.','wp-tweets-pro');
  246. wpt_saves_error( $id, $auth, $twit, $error, '403', time() );
  247. wpt_set_log( 'wpt_status_message', $id, $error );
  248. return true;
  249. } else {
  250. // must be designated as media and have a valid attachment
  251. $attachment = ( $media ) ? wpt_post_attachment( $id ) : false;
  252. if ( $attachment ) {
  253. $meta = wp_get_attachment_metadata($attachment);
  254. if ( !isset( $meta['width'], $meta['height'] ) ) {
  255. if ( WPT_DEBUG && function_exists( 'wpt_pro_exists' ) ) {
  256. wpt_mail( "Image Data Does not Exist for Attachment #$attachment", print_r( $args, 1 ) );
  257. }
  258. $attachment = false;
  259. }
  260. }
  261. // support for HTTP deprecated as of 1/14/2014 -- https://dev.twitter.com/discussions/24239
  262. $api = ( $media && $attachment )?"https://api.twitter.com/1.1/statuses/update_with_media.json":"https://api.twitter.com/1.1/statuses/update.json";
  263. if ( wtt_oauth_test( $auth ) && ( $connection = wtt_oauth_connection( $auth ) ) ) {
  264. if ( $media && $attachment ) {
  265. $connection->media( $api, array( 'status' => $twit, 'source' => 'wp-to-twitter', 'include_entities' => 'true', 'id'=>$id, 'auth'=>$auth ) );
  266. } else {
  267. $connection->post( $api, array( 'status' => $twit, 'source' => 'wp-to-twitter', 'include_entities' => 'true' ) );
  268. }
  269. $http_code = ( $connection ) ? $connection->http_code : 'failed';
  270. } else if ( wtt_oauth_test( false ) && ( $connection = wtt_oauth_connection( false ) ) ) {
  271. if ( $media ) {
  272. $connection->media( $api, array( 'status' => $twit, 'source' => 'wp-to-twitter', 'include_entities' => 'true', 'id'=>$id, 'auth'=>$auth ) );
  273. } else {
  274. $connection->post( $api, array( 'status' => $twit, 'source' => 'wp-to-twitter', 'include_entities' => 'true' ) );
  275. }
  276. $http_code = ( $connection ) ? $connection->http_code : 'failed';
  277. }
  278. if ( WPT_DEBUG && function_exists( 'wpt_pro_exists' ) ) {
  279. wpt_mail( 'Twitter Connection', print_r( $connection, 1 ) );
  280. }
  281. if ( $connection ) {
  282. if ( isset( $connection->http_header['x-access-level'] ) && $connection->http_header['x-access-level'] == 'read' ) { $supplement = sprintf( __('Your Twitter application does not have read and write permissions. Go to <a href="%s">your Twitter apps</a> to modify these settings.','wp-to-twitter'), 'https://dev.twitter.com/apps/' ); } else { $supplement = ''; }
  283. $return = false;
  284. switch ($http_code) {
  285. case '200':
  286. $return = true;
  287. $error = __("200 OK: Success!",'wp-to-twitter');
  288. update_option('wpt_authentication_missing', false );
  289. break;
  290. case '304':
  291. $error = __("304 Not Modified: There was no new data to return",'wp-to-twitter');
  292. break;
  293. case '400':
  294. $error = __("400 Bad Request: The request was invalid. This is the status code returned during rate limiting.",'wp-to-twitter');
  295. break;
  296. case '401':
  297. $error = __("401 Unauthorized: Authentication credentials were missing or incorrect.",'wp-to-twitter');
  298. update_option( 'wpt_authentication_missing',"$auth");
  299. break;
  300. case '403':
  301. $error = __("403 Forbidden: The request is understood, but has been refused by Twitter. Possible reasons: too many Tweets, same Tweet submitted twice, Tweet longer than 140 characters.",'wp-to-twitter');
  302. break;
  303. case '404':
  304. $error = __("404 Not Found: The URI requested is invalid or the resource requested does not exist.",'wp-to-twitter');
  305. break;
  306. case '406':
  307. $error = __("406 Not Acceptable: Invalid Format Specified.",'wp-to-twitter');
  308. break;
  309. case '422':
  310. $error = __("422 Unprocessable Entity: The image uploaded could not be processed..",'wp-to-twitter');
  311. break;
  312. case '429':
  313. $error = __("429 Too Many Requests: You have exceeded your rate limits.",'wp-to-twitter');
  314. break;
  315. case '500':
  316. $error = __("500 Internal Server Error: Something is broken at Twitter.",'wp-to-twitter');
  317. break;
  318. case '502':
  319. $error = __("502 Bad Gateway: Twitter is down or being upgraded.",'wp-to-twitter');
  320. break;
  321. case '503':
  322. $error = __("503 Service Unavailable: The Twitter servers are up, but overloaded with requests - Please try again later.",'wp-to-twitter');
  323. break;
  324. case '504':
  325. $error = __("504 Gateway Timeout: The Twitter servers are up, but the request couldn't be serviced due to some failure within our stack. Try again later.",'wp-to-twitter');
  326. break;
  327. default:
  328. $error = __("<strong>Code $http_code</strong>: Twitter did not return a recognized response code.",'wp-to-twitter');
  329. break;
  330. }
  331. $error .= ($supplement != '')?" $supplement":'';
  332. // debugging
  333. if ( WPT_DEBUG && function_exists( 'wpt_pro_exists' ) ) {
  334. wpt_mail( "Twitter Response: $http_code, #$id","$http_code, $error"); // DEBUG
  335. }
  336. // end debugging
  337. $update = ( !$auth )?update_option( 'jd_last_tweet',$twit ):update_user_meta( $auth, 'wpt_last_tweet',$twit );
  338. wpt_saves_error( $id, $auth, $twit, $error, $http_code, time() );
  339. if ( $http_code == '200' ) {
  340. $jwt = get_post_meta( $id, '_jd_wp_twitter', true );
  341. if ( !is_array( $jwt ) ){ $jwt=array(); }
  342. $jwt[] = urldecode( $twit );
  343. if ( empty($_POST) ) { $_POST = array(); }
  344. $_POST['_jd_wp_twitter'] = $jwt;
  345. update_post_meta( $id, '_jd_wp_twitter', $jwt );
  346. if ( !function_exists( 'wpt_pro_exists' ) ) {
  347. // schedule a one-time promotional box for 4 weeks after first successful Tweet.
  348. if ( get_option( 'wpt_promotion_scheduled' ) == false ) {
  349. wp_schedule_single_event( time()+( 60*60*24*7*4 ), 'wpt_schedule_promotion_action' );
  350. update_option( 'wpt_promotion_scheduled', 1 );
  351. }
  352. }
  353. }
  354. if ( !$return ) {
  355. wpt_set_log( 'wpt_status_message', $id, $error );
  356. } else {
  357. wpt_set_log( 'wpt_status_message', $id, __( 'Tweet sent successfully.','wp-to-twitter' ) );
  358. }
  359. return $return;
  360. } else {
  361. wpt_set_log( 'wpt_status_message', $id, __( 'No Twitter OAuth connection found.','wp-to-twitter' ) );
  362. return false;
  363. }
  364. }
  365. }
  366. function fake_normalize( $string ) {
  367. if ( version_compare( PHP_VERSION, '5.0.0', '>=' ) && function_exists('normalizer_normalize') && 1==2 ) {
  368. if ( normalizer_is_normalized( $string ) ) { return $string; }
  369. return normalizer_normalize( $string );
  370. } else {
  371. return preg_replace( '~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml|mp);~i', '$1', htmlentities( $string, ENT_NOQUOTES, 'UTF-8' ) );
  372. }
  373. }
  374. function wpt_is_ssl( $url ) {
  375. if ( stripos( $url, 'https' ) ) { return true; } else { return false; }
  376. }
  377. function jd_truncate_tweet( $tweet, $post, $post_ID, $retweet=false, $ref=false ) {
  378. // media file occupies 22 characters, need to account for in shortening.
  379. $tweet_length = ( wpt_post_with_media( $post_ID ) ) ? 117 : 139;
  380. $tweet = trim(custom_shortcodes( $tweet, $post_ID ));
  381. $shrink = ( $post['shortUrl'] != '' )?$post['shortUrl']:apply_filters( 'wptt_shorten_link', $post['postLink'], $post['postTitle'], $post_ID, false );
  382. // generate all template variable values
  383. $auth = $post['authId'];
  384. $title = trim( apply_filters( 'wpt_status', $post['postTitle'], $post_ID, 'title' ) );
  385. $blogname = trim($post['blogTitle']);
  386. $excerpt = trim( apply_filters( 'wpt_status', $post['postExcerpt'], $post_ID, 'post' ) );
  387. $thisposturl = trim($shrink);
  388. $category = trim($post['category']);
  389. $cat_desc = trim($post['cat_desc']);
  390. $user_account = get_user_meta( $auth,'wtt_twitter_username', true ) ;
  391. $tags = wpt_generate_hash_tags( $post_ID );
  392. $account = get_option('wtt_twitter_username');
  393. $date = trim($post['postDate']);
  394. $modified = trim($post['postModified']);
  395. if ( get_option( 'jd_individual_twitter_users' ) == 1 ) {
  396. if ( $user_account == '' ) {
  397. if ( get_user_meta( $auth, 'wp-to-twitter-enable-user',true ) == 'mainAtTwitter' ) {
  398. $account = stripcslashes(get_user_meta( $auth, 'wp-to-twitter-user-username',true ));
  399. } else if ( get_user_meta( $auth, 'wp-to-twitter-enable-user',true ) == 'mainAtTwitterPlus' ) {
  400. $account = stripcslashes(get_user_meta( $auth, 'wp-to-twitter-user-username',true ) . ' @' . get_option( 'wtt_twitter_username' ));
  401. }
  402. } else {
  403. $account = "$user_account";
  404. }
  405. }
  406. $display_name = get_the_author_meta( 'display_name', $auth );
  407. // value of #author#
  408. $author = ( $account != '' )?"@$account":$display_name;
  409. // value of #account#
  410. $account = ( $account != '' )?"@$account":'';
  411. // value of #@#
  412. $uaccount = ( $user_account != '' )?"@$user_account":"$account";
  413. // clean up data if extra @ included //
  414. $account = str_ireplace( '@@','@',$account );
  415. $uaccount = str_ireplace( '@@', '@', $uaccount );
  416. $author = str_ireplace( '@@', '@', $author );
  417. if ( get_user_meta( $auth, 'wpt-remove', true ) == 'on' ) { $account = ''; }
  418. if ( get_option( 'jd_twit_prepend' ) != "" && $tweet != '' ) {
  419. $tweet = stripslashes(get_option( 'jd_twit_prepend' )) . " " . $tweet;
  420. }
  421. if ( get_option( 'jd_twit_append' ) != "" && $tweet != '' ) {
  422. $tweet = $tweet . " " . stripslashes(get_option( 'jd_twit_append' ));
  423. }
  424. $encoding = get_option('blog_charset');
  425. if ( $encoding == '' ) { $encoding = 'UTF-8'; }
  426. $has_excerpt_tag = ( strpos( $tweet, '#post#' ) === false ) ? false : true;
  427. if ( strpos( $tweet, '#url#' ) === false
  428. && strpos( $tweet, '#title#' ) === false
  429. && strpos( $tweet, '#blog#' ) === false
  430. && strpos( $tweet, '#post#' ) === false
  431. && strpos( $tweet, '#category#' ) === false
  432. && strpos( $tweet, '#date#' ) === false
  433. && strpos( $tweet, '#author#' ) === false
  434. && strpos( $tweet, '#displayname#' ) === false
  435. && strpos( $tweet, '#tags#' ) === false
  436. && strpos( $tweet, '#modified#' ) === false
  437. && strpos( $tweet, '#reference#' ) === false
  438. && strpos( $tweet, '#account#' ) === false
  439. && strpos( $tweet, '#@#' ) === false
  440. && strpos( $tweet, '#cat_desc' ) === false
  441. ) {
  442. // there are no tags in this Tweet. Truncate and return.
  443. $post_tweet = mb_substr( $tweet, 0, $tweet_length, $encoding );
  444. return $post_tweet;
  445. }
  446. if ( function_exists('wpt_pro_exists') && wpt_pro_exists() == true ) {
  447. $reference = ( $ref ) ? $account : '@' . get_option( 'wtt_twitter_username' );
  448. }
  449. // create full unconditional post tweet - prior to truncation
  450. $replace = ( function_exists('wpt_pro_exists') && wpt_pro_exists() == true ) ? $reference : '' ;
  451. $search = array( '#account#', '#@#', '#reference#', '#url#', '#title#', '#blog#', '#post#', '#category#', '#cat_desc#', '#date#', '#author#', '#displayname#', '#tags#', '#modified#' );
  452. $replace = array( $account, $uaccount, $replace, $thisposturl, $title, $blogname, $excerpt, $category, $cat_desc, $date, $author, $display_name, $tags, $modified );
  453. $post_tweet = str_ireplace( $search, $replace, $tweet );
  454. $url_strlen = mb_strlen( urldecode( fake_normalize( $thisposturl ) ), $encoding );
  455. // check total length
  456. $str_length = mb_strlen( urldecode( fake_normalize( $post_tweet ) ), $encoding );
  457. if ( $str_length < $tweet_length+1 ) {
  458. if ( mb_strlen( fake_normalize ( $post_tweet ) ) > $tweet_length+1 ) {
  459. $post_tweet = mb_substr( $post_tweet,0,$tweet_length,$encoding );
  460. }
  461. return $post_tweet; // return early if all is well without replacements.
  462. } else {
  463. // what is the excerpt supposed to be?
  464. $length = get_option( 'jd_post_excerpt' );
  465. // build an array of variable names and the number of characters in that variable.
  466. $length_array = array();
  467. $length_array['excerpt'] = mb_strlen( fake_normalize( $excerpt ),$encoding );
  468. $length_array['title'] = mb_strlen( fake_normalize( $title ),$encoding );
  469. $length_array['date'] = mb_strlen( fake_normalize( $date ),$encoding );
  470. $length_array['category'] = mb_strlen( fake_normalize( $category ),$encoding );
  471. $length_array['cat_desc'] = mb_strlen( fake_normalize( $cat_desc ),$encoding );
  472. $length_array['@'] = mb_strlen( fake_normalize( $uaccount ),$encoding );
  473. $length_array['blogname'] = mb_strlen( fake_normalize( $blogname ),$encoding );
  474. $length_array['author'] = mb_strlen( fake_normalize( $author ),$encoding );
  475. $length_array['account'] = mb_strlen( fake_normalize( $account ),$encoding );
  476. if ( function_exists('wpt_pro_exists') && wpt_pro_exists() == true ) {
  477. $length_array['reference'] = mb_strlen( fake_normalize( $reference ),$encoding );
  478. }
  479. $length_array['tags'] = mb_strlen( fake_normalize( $tags ),$encoding );
  480. $length_array['modified'] = mb_strlen( fake_normalize( $modified ),$encoding );
  481. // if the total length is too long, truncate items until the length is appropriate.
  482. // Twitter's t.co shortener is mandatory. All URLS are max-character value set by Twitter.
  483. $tco = ( wpt_is_ssl( $thisposturl ) )?23:22;
  484. $order = get_option( 'wpt_truncation_order' );
  485. if ( is_array( $order ) ) {
  486. asort($order);
  487. $preferred = array();
  488. foreach ( $order as $k => $v ) {
  489. $preferred[$k] = $length_array[$k];
  490. }
  491. } else {
  492. $preferred = $length_array;
  493. }
  494. $diff = ( ($url_strlen - $tco) > 0 )?$url_strlen-$tco:0;
  495. if ( $str_length > ( $tweet_length+ 1 + $diff ) ) {
  496. foreach ( $preferred AS $key=>$value ) {
  497. // don't truncate content of post excerpt if excerpt tag not in use
  498. if ( !( $key == 'excerpt' && !$has_excerpt_tag ) ) {
  499. $str_length = mb_strlen( urldecode( fake_normalize( trim( $post_tweet ) ) ),$encoding );
  500. if ( $str_length > ( $tweet_length + 1 + $diff ) ) {
  501. $trim = $str_length - ( $tweet_length + 1 + $diff );
  502. $old_value = ${$key};
  503. // prevent URL from being modified
  504. $post_tweet = str_ireplace( $thisposturl, '#url#', $post_tweet );
  505. // modify the value and replace old with new
  506. if ( $key == 'account' || $key == 'author' || $key == 'category' || $key == 'date' || $key == 'modified' || $key == 'reference' || $key == '@' ) {
  507. // these elements make no sense if truncated, so remove them entirely.
  508. $new_value = '';
  509. } else if ( $key == 'tags' ) {
  510. // remove any stray hash characters due to string truncation
  511. if (mb_strlen($old_value)-$trim <= 2) {
  512. $new_value = '';
  513. } else {
  514. $new_value = $old_value;
  515. while ((mb_strlen($old_value)-$trim) < mb_strlen($new_value)) {
  516. $new_value = trim(mb_substr($new_value,0,mb_strrpos($new_value,'#',$encoding)-1));
  517. }
  518. }
  519. } else {
  520. $new_value = mb_substr( $old_value,0,-( $trim ),$encoding );
  521. }
  522. $post_tweet = str_ireplace( $old_value,$new_value,$post_tweet );
  523. // put URL back before checking length
  524. $post_tweet = str_ireplace( '#url#', $thisposturl, $post_tweet );
  525. } else {
  526. if ( mb_strlen( fake_normalize ( $post_tweet ),$encoding ) > ( $tweet_length + 1 + $diff ) ) {
  527. $post_tweet = mb_substr( $post_tweet,0,( $tweet_length + $diff ),$encoding );
  528. }
  529. }
  530. }
  531. }
  532. }
  533. // this is needed in case a tweet needs to be truncated outright and the truncation values aren't in the above.
  534. // 1) removes URL 2) checks length of remainder 3) Replaces URL
  535. if ( mb_strlen( fake_normalize( $post_tweet ) ) > $tweet_length + 1 ) {
  536. $temp = str_ireplace( $thisposturl, '#url#', $post_tweet );
  537. if ( mb_strlen( fake_normalize( $temp ) ) > ( ( $tweet_length + 1 ) - $tco) && $temp != $post_tweet ) {
  538. $post_tweet = trim(mb_substr( $temp,0,( ( $tweet_length + 1 ) -$tco),$encoding ));
  539. // it's possible to trim off the #url# part in this process. If that happens, put it back.
  540. $sub_sentence = (strpos( $tweet, '#url#' )===false )?$post_tweet:$post_tweet .' '. $thisposturl;
  541. $post_tweet = ( strpos( $post_tweet,'#url#' ) === false )?$sub_sentence:str_ireplace( '#url#',$thisposturl,$post_tweet );
  542. }
  543. }
  544. }
  545. return $post_tweet; // catch all, should never happen. But no reason not to include it.
  546. }
  547. function wpt_in_allowed_category( $array ) {
  548. $allowed_categories = get_option( 'tweet_categories' );
  549. if ( is_array( $array ) && is_array( $allowed_categories ) ) {
  550. $common = @array_intersect( $array,$allowed_categories );
  551. if ( WPT_DEBUG && function_exists( 'wpt_pro_exists' ) ) {
  552. wpt_mail( 'Category Limits Results: ---', print_r($common,1) );
  553. }
  554. if ( count( $common ) >= 1 ) {
  555. return true;
  556. } else {
  557. return false;
  558. }
  559. } else {
  560. return true;
  561. }
  562. }
  563. function jd_post_info( $post_ID ) {
  564. $encoding = get_option('blog_charset');
  565. if ( $encoding == '' ) { $encoding = 'UTF-8'; }
  566. $post = get_post( $post_ID );
  567. $category_ids = false;
  568. $values = array();
  569. $values['id'] = $post_ID;
  570. // get post author
  571. $values['postinfo'] = $post;
  572. $values['authId'] = $post->post_author;
  573. $postdate = $post->post_date;
  574. $altformat = "Y-m-d H:i:s";
  575. $dateformat = (get_option('jd_date_format')=='')?get_option('date_format'):get_option('jd_date_format');
  576. $thisdate = mysql2date( $dateformat,$postdate );
  577. $altdate = mysql2date( $altformat, $postdate );
  578. $values['_postDate'] = $altdate;
  579. $values['postDate'] = $thisdate;
  580. $moddate = $post->post_modified;
  581. $values['_postModified'] = mysql2date( $altformat,$moddate );
  582. $values['postModified'] = mysql2date( $dateformat,$moddate );
  583. // get first category
  584. $category = $cat_desc = null;
  585. $categories = get_the_category( $post_ID );
  586. if ( is_array( $categories ) ) {
  587. if ( count($categories) > 0 ) {
  588. $category = $categories[0]->cat_name;
  589. $cat_desc = $categories[0]->description;
  590. }
  591. foreach ($categories AS $cat) {
  592. $category_ids[] = $cat->term_id;
  593. }
  594. } else {
  595. $category = '';
  596. $cat_desc = '';
  597. $category_ids = array();
  598. }
  599. $values['categoryIds'] = $category_ids;
  600. $values['category'] = html_entity_decode( $category, ENT_COMPAT, $encoding );
  601. $values['cat_desc'] = html_entity_decode( $cat_desc, ENT_COMPAT, $encoding );
  602. $excerpt_length = get_option( 'jd_post_excerpt' );
  603. $post_excerpt = ( trim( $post->post_excerpt ) == "" )?@mb_substr( strip_tags( strip_shortcodes( $post->post_content ) ), 0, $excerpt_length ):@mb_substr( strip_tags( strip_shortcodes( $post->post_excerpt ) ), 0, $excerpt_length );
  604. $values['postExcerpt'] = html_entity_decode( $post_excerpt, ENT_COMPAT, $encoding );
  605. $thisposttitle = stripcslashes( strip_tags( $post->post_title ) );
  606. if ( $thisposttitle == "" && isset( $_POST['title'] ) ) {
  607. $thisposttitle = stripcslashes( strip_tags( $_POST['title'] ) );
  608. }
  609. $values['postTitle'] = html_entity_decode( $thisposttitle, ENT_COMPAT, $encoding );
  610. $values['postLink'] = wpt_link( $post_ID );
  611. $values['blogTitle'] = get_bloginfo( 'name' );
  612. $values['shortUrl'] = wpt_short_url( $post_ID );
  613. $values['postStatus'] = $post->post_status;
  614. $values['postType'] = $post->post_type;
  615. $values = apply_filters( 'wpt_post_info',$values, $post_ID );
  616. return $values;
  617. }
  618. function wpt_short_url( $post_id ) {
  619. global $post_ID;
  620. if ( !$post_id ) { $post_id = $post_ID; }
  621. $jd_short = get_post_meta( $post_id, '_wp_jd_bitly', true );
  622. if ( $jd_short == "" ) {$jd_short = get_post_meta( $post_id, '_wp_jd_goo', true );}
  623. if ( $jd_short == "" ) {$jd_short = get_post_meta( $post_id, '_wp_jd_supr', true ); }
  624. if ( $jd_short == "" ) {$jd_short = get_post_meta( $post_id, '_wp_jd_wp', true ); }
  625. if ( $jd_short == "" ) {$jd_short = get_post_meta( $post_id, '_wp_jd_ind', true ); }
  626. if ( $jd_short == "" ) {$jd_short = get_post_meta( $post_id, '_wp_jd_yourls', true );}
  627. if ( $jd_short == "" ) {$jd_short = get_post_meta( $post_id, '_wp_jd_url', true );}
  628. if ( $jd_short == "" ) {$jd_short = get_post_meta( $post_id, '_wp_jd_joturl', true );}
  629. if ( $jd_short == "" ) {$jd_short = get_post_meta( $post_id, '_wp_jd_target', true );}
  630. if ( $jd_short == "" ) {$jd_short = get_post_meta( $post_id, '_wp_jd_clig', true );}
  631. return $jd_short;
  632. }
  633. function wpt_post_with_media( $post_ID, $post_info=array() ) {
  634. $return = false;
  635. if ( isset( $post_info['wpt_image'] ) && $post_info['wpt_image'] == 1 ) return $return;
  636. if ( !function_exists( 'wpt_pro_exists' ) || get_option( 'wpt_media') != '1' ) {
  637. $return = false;
  638. } else {
  639. if ( has_post_thumbnail( $post_ID ) || wpt_post_attachment( $post_ID ) ) {
  640. $return = true;
  641. }
  642. }
  643. return apply_filters( 'wpt_upload_media', $return, $post_ID );
  644. }
  645. function wpt_category_limit( $post_type, $post_info, $post_ID ) {
  646. $post_type_cats = get_object_taxonomies( $post_type );
  647. $continue = true;
  648. if ( in_array( 'category', $post_type_cats ) ) {
  649. // 'category' is assigned to this post type, so apply filters.
  650. if ( get_option('jd_twit_cats') == '1' ) {
  651. $continue = ( !wpt_in_allowed_category( $post_info['categoryIds'] ) )?true:false;
  652. } else {
  653. $continue = ( wpt_in_allowed_category( $post_info['categoryIds'] ) )?true:false;
  654. }
  655. }
  656. if ( WPT_DEBUG && function_exists( 'wpt_pro_exists' ) && !$continue ) {
  657. wpt_mail( "3b: Category limits applied #$post_ID", print_r($post_info['categoryIds'],1));
  658. }
  659. $continue = ( get_option('limit_categories') == '0' )?true:$continue;
  660. $args = array( 'type'=>$post_type, 'info'=>$post_info, 'id'=>$post_ID );
  661. return apply_filters( 'wpt_filter_terms', $continue, $args );
  662. }
  663. function jd_twit( $post_ID, $type='instant' ) {
  664. if ( WPT_DEBUG && function_exists( 'wpt_pro_exists' ) ) {
  665. wpt_mail( "0: jd_twit running #$post_ID","Post ID: $post_ID / $type"); // DEBUG
  666. }
  667. if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || wp_is_post_revision($post_ID) ) { return $post_ID; }
  668. wpt_check_version();
  669. $jd_tweet_this = get_post_meta( $post_ID, '_jd_tweet_this', true );
  670. $newpost = $oldpost = $is_inline_edit = false;
  671. $sentence = $template = '';
  672. if ( get_option('wpt_inline_edits') != 1 ) {
  673. if ( isset( $_POST['_inline_edit'] ) || isset( $_REQUEST['bulk_edit'] ) ) { return; }
  674. } else {
  675. if ( isset( $_POST['_inline_edit'] ) || isset( $_REQUEST['bulk_edit'] ) ) { $is_inline_edit = true; }
  676. }
  677. if ( WPT_DEBUG && function_exists( 'wpt_pro_exists' ) ) {
  678. wpt_mail( "1: JD Tweet This Value: #$post_ID","Tweet this: $jd_tweet_this /". get_option('jd_tweet_default')." / $type"); // DEBUG
  679. }
  680. if ( get_option('jd_tweet_default') == 0 ) {
  681. $test = ( $jd_tweet_this != 'no' ) ? true : false;
  682. } else {
  683. $test = ( $jd_tweet_this == 'yes' ) ? true : false;
  684. }
  685. if ( $test ) { // test switch: depend on default settings.
  686. $post_info = jd_post_info( $post_ID );
  687. $media = wpt_post_with_media( $post_ID, $post_info );
  688. if ( function_exists( 'wpt_pro_exists' ) && wpt_pro_exists() == true ) {
  689. $auth = ( get_option( 'wpt_cotweet_lock' ) == 'false' || !get_option('wpt_cotweet_lock') )?$post_info['authId']:get_option('wpt_cotweet_lock');
  690. } else {
  691. $auth = $post_info['authId'];
  692. }
  693. /* debug data */
  694. if ( WPT_DEBUG && function_exists( 'wpt_pro_exists' ) ) {
  695. wpt_mail( "2: POST Debug Data #$post_ID","Post_Info: ".print_r($post_info,1)."\n\nPOST: ".print_r($_POST, 1). " / $type" );
  696. }
  697. if ( function_exists( 'wpt_pro_exists' ) && wpt_pro_exists() == true && function_exists('wpt_filter_post_info') ) {
  698. $filter = wpt_filter_post_info( $post_info );
  699. if ( $filter == true ) {
  700. if ( WPT_DEBUG && function_exists( 'wpt_pro_exists' ) ) {
  701. wpt_mail( "3a: Post filtered: #$post_ID",print_r($post_info,1)." / $type");
  702. }
  703. return false;
  704. }
  705. }
  706. /* Filter Tweet based on POST data -- allows custom filtering of unknown plug-ins, etc. */
  707. $filter = apply_filters( 'wpt_filter_post_data', false, $_POST );
  708. if ( $filter ) {
  709. return false;
  710. }
  711. $post_type = $post_info['postType'];
  712. if ( $type == 'future' || get_post_meta( $post_ID, 'wpt_publishing' ) == 'future' ) {
  713. $new = 1; // if this is a future action, then it should be published regardless of relationship
  714. if ( WPT_DEBUG && function_exists( 'wpt_pro_exists' ) ) {
  715. wpt_mail( "4: Future post: #$post_ID",print_r($post_info,1)." / $type");
  716. }
  717. delete_post_meta( $post_ID, 'wpt_publishing' );
  718. } else {
  719. // if the post modified date and the post date are the same, this is new.
  720. // true if first date before or equal to last date
  721. $new = wpt_date_compare( $post_info['_postModified'], $post_info['_postDate'] );
  722. }
  723. // post is not previously published but has been backdated:
  724. // (post date is edited, but save option is 'publish')
  725. if ( $new == 0 && ( isset( $_POST['edit_date'] ) && $_POST['edit_date'] == 1 && !isset( $_POST['save'] ) ) ) { $new = 1; }
  726. // can't catch posts that were set to a past date as a draft, then published.
  727. $post_type_settings = get_option('wpt_post_types');
  728. $post_types = array_keys($post_type_settings);
  729. if ( in_array( $post_type, $post_types ) ) {
  730. // identify whether limited by category/taxonomy
  731. $continue = wpt_category_limit( $post_type, $post_info, $post_ID );
  732. if ( $continue == false ) { return; }
  733. // create Tweet and ID whether current action is edit or new
  734. $cT = get_post_meta( $post_ID, '_jd_twitter', true );
  735. if ( isset( $_POST['_jd_twitter'] ) && $_POST['_jd_twitter'] != '' ) { $cT = $_POST['_jd_twitter']; }
  736. $customTweet = ( $cT != '' )?stripcslashes( trim( $cT ) ):'';
  737. // if ops is set and equals 'publish', this is being edited. Otherwise, it's a new post.
  738. if ( $new == 0 || $is_inline_edit == true ) {
  739. // if this is an old post and editing updates are enabled
  740. if ( get_option( 'jd_tweet_default_edit' ) == 1 ) {
  741. $jd_tweet_this = apply_filters( 'wpt_tweet_this_edit', $jd_tweet_this, $_POST );
  742. if ( $jd_tweet_this != 'yes' ) {
  743. if ( WPT_DEBUG && function_exists( 'wpt_pro_exists' ) ) {
  744. wpt_mail( "3c: Tweet this: not equal to yes","Exit // Post ID: $post_ID"); // DEBUG
  745. }
  746. return;
  747. }
  748. }
  749. if ( WPT_DEBUG && function_exists( 'wpt_pro_exists' ) ) {
  750. wpt_mail( "4a: Edited post #$post_ID","Tweet this: ".print_r($post_info,1)." / $type"); // DEBUG
  751. }
  752. if ( $post_type_settings[$post_type]['post-edited-update'] == '1' ) {
  753. $nptext = stripcslashes( $post_type_settings[$post_type]['post-edited-text'] );
  754. $oldpost = true;
  755. }
  756. } else {
  757. if ( WPT_DEBUG && function_exists( 'wpt_pro_exists' ) ) {
  758. wpt_mail( "4b: New Post #$post_ID","Tweet this: ".print_r($post_info,1)." / $type"); // DEBUG
  759. }
  760. if ( $post_type_settings[$post_type]['post-published-update'] == '1' ) {
  761. $nptext = stripcslashes( $post_type_settings[$post_type]['post-published-text'] );
  762. $newpost = true;
  763. }
  764. }
  765. if ( $newpost || $oldpost ) {
  766. $template = ( $customTweet != "" ) ? $customTweet : $nptext;
  767. $sentence = jd_truncate_tweet( $template, $post_info, $post_ID );
  768. if ( WPT_DEBUG && function_exists( 'wpt_pro_exists' ) ) {
  769. wpt_mail( "5: Tweet Truncated #$post_ID","Truncated Tweet: $sentence / $template / $type"); // DEBUG
  770. }
  771. if ( function_exists('wpt_pro_exists') && wpt_pro_exists() == true ) {
  772. $sentence2 = jd_truncate_tweet( $template, $post_info, $post_ID, false, $auth );
  773. }
  774. }
  775. if ( $sentence != '' ) {
  776. // WPT PRO //
  777. if ( function_exists( 'wpt_pro_exists' ) && wpt_pro_exists() == true ) {
  778. $wpt_selected_users = $post_info['wpt_authorized_users'];
  779. /* set up basic author/main account values */
  780. $auth_verified = wtt_oauth_test( $auth,'verify' );
  781. if ( empty( $wpt_selected_users ) && get_option( 'jd_individual_twitter_users' ) == 1 ) {
  782. $wpt_selected_users = ($auth_verified)? array( $auth ) : array( false );
  783. }
  784. if ( $post_info['wpt_cotweet'] == 1 || get_option( 'jd_individual_twitter_users' ) != 1 ) {
  785. $wpt_selected_users['main'] = false;
  786. }
  787. // filter selected users before using
  788. $wpt_selected_users = apply_filters( 'wpt_filter_users', $wpt_selected_users, $post_info );
  789. if ( $post_info['wpt_delay_tweet'] == 0 || $post_info['wpt_delay_tweet'] == '' || $post_info['wpt_no_delay'] == 'on' ) {
  790. foreach ( $wpt_selected_users as $acct ) {
  791. if ( wtt_oauth_test( $acct, 'verify' ) ) {
  792. $tweet = jd_doTwitterAPIPost( $sentence2, $acct, $post_ID, $media );
  793. }
  794. }
  795. } else {
  796. foreach ( $wpt_selected_users as $acct ) {
  797. if ( $auth != $acct ) {
  798. $offset = rand( 60,480 ); // offset by 1-8 minutes for additional users
  799. } else {
  800. $offset = 0;
  801. }
  802. if ( wtt_oauth_test( $acct,'verify' ) ) {
  803. $time = apply_filters( 'wpt_schedule_delay',( (int) $post_info['wpt_delay_tweet'] )*60, $acct );
  804. $scheduled = wp_schedule_single_event( time()+$time+$offset, 'wpt_schedule_tweet_action', array( 'id'=>$acct, 'sentence'=>$sentence, 'rt'=>0, 'post_id'=>$post_ID ) );
  805. $tweet = true; // if scheduled, return true.
  806. if ( WPT_DEBUG && function_exists( 'wpt_pro_exists' ) ) {
  807. if ( $acct ) { $author_id = "#$acct"; } else { $author_id = 'Main'; }
  808. wpt_mail( "7a: Tweet Scheduled for Auth ID $author_id #$post_ID", print_r( array( 'id'=>$acct, 'sentence'=>$sentence, 'rt'=>0, 'post_id'=>$post_ID, 'timestamp'=>time()+$time+$offset, 'current_time'=>time(), 'timezone'=>get_option('gmt_offset'), 'scheduled'=>$scheduled, 'timestamp_string'=>date( 'Y-m-d H:i:s',time()+$time+$offset ),'current_time_string'=>date( 'Y-m-d H:i:s',time() ), ),1 ) ); // DEBUG
  809. }
  810. }
  811. }
  812. }
  813. /* This cycle handles scheduling the automatic retweets */
  814. if ( $post_info['wpt_retweet_after'] != 0 && $post_info['wpt_no_repost'] != 'on' ) {
  815. $repeat = $post_info['wpt_retweet_repeat'];
  816. $first = true;
  817. foreach ( $wpt_selected_users as $acct ) {
  818. if ( wtt_oauth_test( $acct,'verify' ) ) {
  819. for ( $i=1;$i<=$repeat;$i++ ) {
  820. switch( $i ) {
  821. case 1:
  822. $prepend = ( get_option('wpt_prepend') == 1 )?'':get_option('wpt_prepend_rt');
  823. $append = ( get_option('wpt_prepend') != 1 )?'':get_option('wpt_prepend_rt');
  824. break;
  825. case 2:
  826. $prepend = ( get_option('wpt_prepend') == 1 )?'':get_option('wpt_prepend_rt2');
  827. $append = ( get_option('wpt_prepend') != 1 )?'':get_option('wpt_prepend_rt2');
  828. break;
  829. case 3:
  830. $prepend = ( get_option('wpt_prepend') == 1 )?'':get_option('wpt_prepend_rt3');
  831. $append = ( get_option('wpt_prepend') != 1 )?'':get_option('wpt_prepend_rt3');
  832. break;
  833. }
  834. if ( get_option( 'wpt_custom_type' ) == 'template' ) {
  835. $retweet = jd_truncate_tweet( trim( $prepend.$append ), $post_info, $post_ID, true, $acct );
  836. } else {
  837. $retweet = jd_truncate_tweet( trim( $prepend.$template.$append ), $post_info, $post_ID, true, $acct );
  838. }
  839. // add original delay to schedule
  840. $delay = ( isset($post_info['wpt_delay_tweet'] ) )?( (int) $post_info['wpt_delay_tweet'] )*60:0;
  841. /* Don't delay the first Tweet of the group */
  842. $offset = ( $first == true )?0:rand(60,240); // delay each co-tweet by 1-4 minutes
  843. $time = apply_filters( 'wpt_schedule_retweet',($post_info['wpt_retweet_after'])*(60*60)*$i, $acct, $i, $post_info );
  844. wp_schedule_single_event( time()+$time+$offset+$delay, 'wpt_schedule_tweet_action', array( 'id'=>$acct, 'sentence'=>$retweet, 'rt'=>$i, 'post_id'=>$post_ID ) );
  845. $tweet = true;
  846. if ( $i == 4 ) { break; }
  847. }
  848. }
  849. $first = false;
  850. }
  851. }
  852. } else {
  853. $tweet = jd_doTwitterAPIPost( $sentence, false, $post_ID, $media );
  854. }
  855. // END WPT PRO //
  856. }
  857. } else {
  858. if ( WPT_DEBUG && function_exists( 'wpt_pro_exists' ) ) {
  859. wpt_mail( "3c: Not a Tweeted post type #$post_ID","Post_Info: ".print_r($post_info,1). " / $type" );
  860. }
  861. return $post_ID;
  862. }
  863. }
  864. return $post_ID;
  865. }
  866. // Add Tweets on links in Blogroll
  867. function jd_twit_link( $link_ID ) {
  868. wpt_check_version();
  869. global $wpt_version;
  870. $thislinkprivate = $_POST['link_visible'];
  871. if ($thislinkprivate != 'N') {
  872. $thislinkname = stripcslashes( $_POST['link_name'] );
  873. $thispostlink = $_POST['link_url'] ;
  874. $thislinkdescription = stripcslashes( $_POST['link_description'] );
  875. $sentence = stripcslashes( get_option( 'newlink-published-text' ) );
  876. $sentence = str_ireplace("#title#",$thislinkname,$sentence);
  877. $sentence = str_ireplace("#description#",$thislinkdescription,$sentence);
  878. if (mb_strlen( $sentence ) > 118) {
  879. $sentence = mb_substr($sentence,0,114) . '...';
  880. }
  881. $shrink = apply_filters( 'wptt_shorten_link', $thispostlink, $thislinkname, false, 'link' );
  882. if ( stripos($sentence,"#url#") === FALSE ) {
  883. $sentence = $sentence . " " . $shrink;
  884. } else {
  885. $sentence = str_ireplace("#url#",$shrink,$sentence);
  886. }
  887. if ( $sentence != '' ) {
  888. $tweet = jd_doTwitterAPIPost( $sentence, false, $link_ID );
  889. }
  890. return $link_ID;
  891. } else {
  892. return;
  893. }
  894. }
  895. function wpt_generate_hash_tags( $post_ID ) {
  896. $hashtags = '';
  897. $term_meta = $t_id = false;
  898. $max_tags = get_option( 'jd_max_tags' );
  899. $max_characters = get_option( 'jd_max_characters' );
  900. $max_characters = ( $max_characters == 0 || $max_characters == "" )?100:$max_characters + 1;
  901. if ($max_tags == 0 || $max_tags == "") { $max_tags = 100; }
  902. $tags = get_the_tags( $post_ID );
  903. if ( $tags > 0 ) {
  904. $i = 1;
  905. foreach ( $tags as $value ) {
  906. if ( function_exists( 'wpt_pro_exists' ) ) {
  907. $t_id = $value->term_id;
  908. $term_meta = get_option( "wpt_taxonomy_$t_id" );
  909. }
  910. if ( get_option('wpt_tag_source') == 'slug' ) {
  911. $tag = $value->slug;
  912. } else {
  913. $tag = $value->name;
  914. }
  915. $strip = get_option( 'jd_strip_nonan' );
  916. $search = "/[^\p{L}\p{N}\s]/u";
  917. $replace = get_option( 'jd_replace_character' );
  918. $replace = ( $replace == "[ ]" || $replace == "" )?"":$replace;
  919. $tag = str_ireplace( " ",$replace,trim( $tag ) );
  920. $tag = preg_replace( '/[\/]/',$replace,$tag ); // remove forward slashes.
  921. if ($strip == '1') { $tag = preg_replace( $search, $replace, $tag ); }
  922. switch ( $term_meta ) {
  923. case 1 : $newtag = "#$tag"; break;
  924. case 2 : $newtag = "$$tag"; break;
  925. case 3 : $newtag = ''; break;
  926. default: $newtag = apply_filters( 'wpt_tag_default', "#", $t_id ).$tag;
  927. }
  928. if ( mb_strlen( $newtag ) > 2 && (mb_strlen( $newtag ) <= $max_characters) && ($i <= $max_tags) ) {
  929. $hashtags .= "$newtag ";
  930. $i++;
  931. }
  932. }
  933. }
  934. $hashtags = trim( $hashtags );
  935. if ( mb_strlen( $hashtags ) <= 1 ) { $hashtags = ""; }
  936. return $hashtags;
  937. }
  938. add_action('admin_menu','jd_add_twitter_outer_box');
  939. function jd_add_twitter_outer_box() {
  940. wpt_check_version();
  941. // add Twitter panel to post types where it's enabled.
  942. $wpt_post_types = get_option('wpt_post_types');
  943. if ( function_exists( 'add_meta_box' )) {
  944. if ( is_array( $wpt_post_types ) ) {
  945. foreach ($wpt_post_types as $key=>$value) {
  946. if ( $value['post-published-update'] == 1 || $value['post-edited-update'] == 1 ) {
  947. add_meta_box( 'wp2t','WP to Twitter', 'jd_add_twitter_inner_box', $key, 'side' );
  948. }
  949. }
  950. }
  951. }
  952. }
  953. function jd_add_twitter_inner_box( $post ) {
  954. if ( current_user_can( 'wpt_can_tweet' ) ) {
  955. $is_pro = ( function_exists( 'wpt_pro_exists' ) ) ? 'pro' : 'free';
  956. echo "<div class='wp-to-twitter $is_pro'>";
  957. global $wpt_donate_url;
  958. $tweet_status = '';
  959. $options = get_option('wpt_post_types');
  960. if ( is_object( $post ) ) {
  961. $type = $post->post_type;
  962. $status = $post->post_status;
  963. $post_id = $post->ID;
  964. }
  965. $jd_tweet_this = get_post_meta( $post_id, '_jd_tweet_this', true );
  966. if ( !$jd_tweet_this ) {
  967. $jd_tweet_this = ( get_option( 'jd_tweet_default' ) == '1' ) ? 'no':'yes';
  968. }
  969. if ( isset( $_GET['action'] ) && $_GET['action'] == 'edit' && get_option( 'jd_tweet_default_edit' ) == '1' ) { $jd_tweet_this = 'no'; }
  970. if ( isset( $_REQUEST['message'] ) && $_REQUEST['message'] != 10 ) { // don't display when draft is updated or if no message
  971. if ( !( ( $_REQUEST['message'] == 1 ) && ( $status == 'publish' && $options[$type]['post-edited-update'] != 1 ) ) ) {
  972. $log = wpt_log( 'wpt_status_message', $post_id );
  973. $class = ( $log != 'Tweet sent successfully.' ) ? 'error' : 'updated' ;
  974. if ( $log != '' ) {
  975. echo "<div class='$class'><p>".wpt_log( 'wpt_status_message', $post_id )."</p></div>";
  976. }
  977. }
  978. }
  979. $previous_tweets = get_post_meta ( $post_id, '_jd_wp_twitter', true );
  980. $failed_tweets = get_post_meta( $post_id, '_wpt_failed' );
  981. $tweet = esc_attr( stripcslashes( get_post_meta( $post_id, '_jd_twitter', true ) ) );
  982. $tweet = apply_filters( 'wpt_user_text', $tweet, $status );
  983. $jd_template = ( $status == 'publish' )?$options[$type]['post-edited-text']:$options[$type]['post-published-text'];
  984. if ( $status == 'publish' && $options[$type]['post-edited-update'] != 1 ) {
  985. $tweet_status = sprintf(__('Tweeting %s edits is disabled.','wp-to-twitter'), $type );
  986. }
  987. if ( current_user_can('update_core') && function_exists( 'wpt_pro_exists' ) ) {
  988. wpt_pro_compatibility();
  989. }
  990. if ( $tweet_status != '' ) { echo "<p class='disabled'>$tweet_status</p>"; }
  991. if ( current_user_can( 'wpt_twitter_custom' ) || current_user_can('update_core') ) { ?>
  992. <p class='jtw'>
  993. <label for="jtw"><?php _e("Custom Twitter Post", 'wp-to-twitter', 'wp-to-twitter') ?></label><br /><textarea class="attachmentlinks" name="_jd_twitter" id="jtw" rows="2" cols="60"><?php echo esc_attr( $tweet ); ?></textarea>
  994. </p>
  995. <?php
  996. $jd_expanded = $jd_template;
  997. if ( get_option( 'jd_twit_prepend' ) != "" ) {
  998. $jd_expanded = "<span title='".__('Your prepended Tweet text; not part of your template.','wp-to-twitter')."'>".stripslashes( get_option( 'jd_twit_prepend' )) . "</span> " . $jd_expanded;
  999. }
  1000. if ( get_option( 'jd_twit_append' ) != "" ) {
  1001. $jd_expanded = $jd_expanded . " <span title='".__('Your appended Tweet text; not part of your template.','wp-to-twitter')."'>" . stripslashes(get_option( 'jd_twit_append' ))."</span>";
  1002. }
  1003. ?>
  1004. <p class='template'><?php _e('Your template:','wp-to-twitter'); ?> <code><?php echo stripcslashes( $jd_expanded ); ?></code></p>
  1005. <?php
  1006. if ( get_option('jd_keyword_format') == 2 ) {
  1007. $custom_keyword = get_post_meta( $post_id, '_yourls_keyword', true );
  1008. echo "<label for='yourls_keyword'>".__('YOURLS Custom Keyword','wp-to-twitter')."</label> <input type='text' name='_yourls_keyword' id='yourls_keyword' value='$custom_keyword' />";
  1009. }
  1010. } else { ?>
  1011. <input type="hidden" name='_jd_twitter' value='<?php echo esc_attr($tweet); ?>' />
  1012. <?php
  1013. }
  1014. if ( current_user_can( 'wpt_twitter_switch' ) || current_user_can('update_core') ) {
  1015. // "no" means 'Don't Tweet' (is checked)
  1016. $nochecked = ( $jd_tweet_this == 'no' )?' checked="checked"':'';
  1017. $yeschecked = ( $jd_tweet_this == 'yes' )?' checked="checked"':'';
  1018. ?>
  1019. <p><input type="radio" name="_jd_tweet_this" value="no" id="jtn"<?php echo $nochecked; ?> /> <label for="jtn"><?php _e("Don't Tweet this post.", 'wp-to-twitter'); ?></label> <input type="radio" name="_jd_tweet_this" value="yes" id="jty"<?php echo $yeschecked; ?> /> <label for="jty"><?php _e("Tweet this post.", 'wp-to-twitter'); ?></label></p>
  1020. <?php
  1021. } else {
  1022. ?>
  1023. <input type='hidden' name='_jd_tweet_this' value='<?php echo $jd_tweet_this; ?>' />
  1024. <?php
  1025. }
  1026. ?>
  1027. <div class='wpt-options'>
  1028. <?php
  1029. if ( $is_pro == 'pro' ) { $pro_active = " class='active'"; $free_active = ''; } else { $free_active = " class='active'"; $pro_active = ''; }
  1030. ?>
  1031. <ul class='tabs'>
  1032. <li><a href='#authors'<?php echo $pro_active; ?>>Tweet to</a></li>
  1033. <li><a href='#custom'>Options</a></li>
  1034. <li><a href='#notes'<?php echo $free_active; ?>>Notes</a></li>
  1035. </ul>
  1036. <?php
  1037. /* WPT PRO OPTIONS */
  1038. if ( current_user_can( 'edit_others_posts' ) ) {
  1039. echo "<div class='wptab' id='authors'>";
  1040. if ( get_option( 'jd_individual_twitter_users' ) == 1 ) {
  1041. $selected = ( get_post_meta( $post_id, '_wpt_authorized_users', true ) )?get_post_meta( $post_id, '_wpt_authorized_users', true ):array();
  1042. if ( function_exists( 'wpt_authorized_users' ) ) {
  1043. echo wpt_authorized_users( $selected );
  1044. do_action( 'wpt_authors_tab', $post_id, $selected );
  1045. } else {
  1046. echo "<p>";
  1047. if ( function_exists( 'wpt_pro_exists' ) ) {
  1048. printf( __( 'WP Tweets PRO 1.5.2+ allows you to select Twitter accounts. <a href="%s">Log in and download now!</a>', 'wp-to-twitter' ), 'http://www.joedolson.com/articles/account/' );
  1049. } else {
  1050. printf( __( 'Upgrade to WP Tweets PRO to select Twitter accounts! <a href="%s">Upgrade now!</a>', 'wp-to-twitter' ), 'http://www.joedolson.com/articles/wp-tweets-pro/' );
  1051. }
  1052. echo "</p>";
  1053. }
  1054. }
  1055. echo "</div>";
  1056. }
  1057. ?>
  1058. <div class='wptab' id='custom'><?php
  1059. if ( function_exists('wpt_pro_exists') && wpt_pro_exists() == true && ( current_user_can( 'wpt_twitter_custom' ) || current_user_can( 'update_core' ) ) ) {
  1060. wpt_schedule_values( $post_id );
  1061. do_action( 'wpt_custom_tab', $post_id, 'visible' );
  1062. } else {
  1063. printf( "<p>".__( 'Upgrade to WP Tweets PRO to configure options! <a href="%s">Upgrade now!</a>'."</p>", 'wp-to-twitter' ), 'http://www.joedolson.com/articles/wp-tweets-pro/' );
  1064. }
  1065. ?></div>
  1066. <?php
  1067. /* WPT PRO */
  1068. if ( !current_user_can( 'wpt_twitter_custom' ) && !current_user_can( 'update_core' ) ) { ?>
  1069. <div class='wptab' id='custom'>
  1070. <p><?php _e('Access to customizing WP to Twitter values is not allowed for your user role.','wp-to-twitter'); ?></p>
  1071. <?php
  1072. if ( function_exists('wpt_pro_exists') && wpt_pro_exists() == true ) {
  1073. wpt_schedule_values( $post_id, 'hidden' );
  1074. do_action( 'wpt_custom_tab', $post_id, 'hidden' );
  1075. } ?>
  1076. </div>
  1077. <?php
  1078. }
  1079. if ( current_user_can( 'wpt_twitter_custom' ) || current_user_can( 'update_core' ) ) { ?>
  1080. <div class='wptab' id='notes'>
  1081. <p>
  1082. <?php _e("Tweets must be less than 140 characters; Twitter counts URLs as 22 or 23 characters. Template Tags: <code>#url#</code>, <code>#title#</code>, <code>#post#</code>, <code>#category#</code>, <code>#date#</code>, <code>#modified#</code>, <code>#author#</code>, <code>#account#</code>, <code>#tags#</code>, or <code>#blog#</code>.", 'wp-to-twitter');
  1083. do_action( 'wpt_notes_tab', $post_id );
  1084. ?>
  1085. </p>
  1086. </div>
  1087. <?php
  1088. } ?>
  1089. </div>
  1090. <p class="wpt-support">
  1091. <?php if ( !function_exists( 'wpt_pro_exists' ) ) { ?>
  1092. <a target="_blank" href="<?php echo admin_url('options-general.php?page=wp-to-twitter/wp-to-twitter.php'); ?>#get-support"><?php _e('Get Support', 'wp-to-twitter', 'wp-to-twitter') ?></a> &bull; <strong><a target="__blank" href="<?php echo $wpt_donate_url; ?>"><?php _e('Upgrade to WP Tweets Pro', 'wp-to-twitter', 'wp-to-twitter') ?></a></strong> &raquo;
  1093. <?php } else { ?>
  1094. <a target="_blank" href="<?php echo admin_url('admin.php?page=wp-tweets-pro'); ?>#get-support"><?php _e('Get Support', 'wp-to-twitter', 'wp-to-twitter') ?></a> &raquo;
  1095. <?php } ?>
  1096. </p>
  1097. <?php wpt_show_tweets( $previous_tweets, $failed_tweets ); ?>
  1098. </div>
  1099. <?php
  1100. } else { // permissions: this user isn't allowed to Tweet;
  1101. _e('Your role does not have the ability to Post Tweets from this site.','wp-to-twitter'); ?> <input type='hidden' name='_jd_tweet_this' value='no' /> <?php
  1102. }
  1103. }
  1104. function wpt_show_tweets( $previous_tweets, $failed_tweets ) {
  1105. if ( !is_array( $previous_tweets ) && $previous_tweets != '' ) { $previous_tweets = array( 0=>$previous_tweets ); }
  1106. if ( ! empty( $previous_tweets ) || ! empty( $failed_tweets ) ) { ?>
  1107. <hr>
  1108. <p class='error'><em><?php _e('Previous Tweets','wp-to-twitter'); ?>:</em></p>
  1109. <ul>
  1110. <?php
  1111. $has_history = false;
  1112. $hidden_fields = '';
  1113. if ( is_array( $previous_tweets ) ) {
  1114. foreach ( $previous_tweets as $previous_tweet ) {
  1115. if ( $previous_tweet != '' ) {
  1116. $has_history = true;
  1117. $hidden_fields .= "<input type='hidden' name='_jd_wp_twitter[]' value='".esc_attr($previous_tweet)."' />";
  1118. echo "<li>$previous_tweet <a href='http://twitter.com/intent/tweet?text=".urlencode($previous_tweet)."'>Retweet this</a></li>";
  1119. }
  1120. }
  1121. }
  1122. ?>
  1123. </ul>
  1124. <?php
  1125. $list = false; $error_list = '';
  1126. if ( is_array( $failed_tweets ) ) {
  1127. foreach ( $failed_tweets as $failed_tweet ) {
  1128. if ( !empty($failed_tweet) ) {
  1129. $ft = $failed_tweet['sentence'];
  1130. $reason = $failed_tweet['code'];
  1131. $error = $failed_tweet['error'];
  1132. $list = true;
  1133. $error_list .= "<li> <code>Error: $reason</code> $ft <a href='http://twitter.com/intent/tweet?text=".urlencode($ft)."'>Tweet this</a><br /><em>$error</em></li>";
  1134. }
  1135. }
  1136. if ( $list == true ) {
  1137. "<p class='error'><em>".__('Failed Tweets','wp-to-twitter').":</em></p>
  1138. <ul>$error_list</ul>";
  1139. }
  1140. }
  1141. echo "<div>".$hidden_fields."</div>";
  1142. if ( $has_history || $list ) {
  1143. echo "<p><input type='checkbox' name='wpt_clear_history' id='wptch' value='clear' /> <label for='wptch'>".__('Delete Tweet History', 'wp-to-twitter' )."</label></p>";
  1144. }
  1145. }
  1146. }
  1147. function wpt_admin_scripts( $hook ) {
  1148. global $current_screen;
  1149. if ( $current_screen->base == 'post' || $current_screen->id == 'wp-tweets-pro_page_wp-to-twitter-schedule' ) {
  1150. wp_enqueue_script( 'charCount', plugins_url( 'wp-to-twitter/js/jquery.charcount.js'), array('jquery') );
  1151. }
  1152. //echo $current_screen->id;
  1153. if ( $current_screen->id == 'settings_page_wp-to-twitter/wp-to-twitter' || $current_screen->id == 'toplevel_page_wp-tweets-pro' ) {
  1154. wp_register_script( 'wpt.tabs', plugins_url( 'js/tabs.js', __FILE__ ), array( 'jquery' ) );
  1155. wp_enqueue_script( 'wpt.tabs' );
  1156. wp_localize_script( 'wpt.tabs', 'firstItem', 'wpt_post' );
  1157. wp_enqueue_script( 'dashboard' );
  1158. }
  1159. }
  1160. add_action( 'admin_enqueue_scripts', 'wpt_admin_scripts', 10, 1 );
  1161. function wpt_admin_script( $hook ) {
  1162. global $current_screen;
  1163. if ( $current_screen->base == 'post' || $current_screen->id == 'wp-tweets-pro_page_wp-to-twitter-schedule' ) {
  1164. wp_register_style( 'wpt-post-styles', plugins_url('css/post-styles.css',__FILE__) );
  1165. wp_enqueue_style('wpt-post-styles');
  1166. if ( $current_screen->base == 'post' ) {
  1167. $allowed = 140 - mb_strlen( get_option('jd_twit_prepend').get_option('jd_twit_append') );
  1168. } else {
  1169. $allowed = ( wpt_is_ssl( home_url() ) )?137:138;
  1170. }
  1171. if ( function_exists( 'wpt_pro_exists' ) && get_option( 'jd_individual_twitter_users' ) == 1 ) {
  1172. $first = '#authors';
  1173. } else if ( function_exists( 'wpt_pro_exists' ) ) {
  1174. $first = '#custom';
  1175. } else {
  1176. $first = '#notes';
  1177. }
  1178. // yuck, this is ugly. I should do something about it. Like localize, man.
  1179. echo "
  1180. <script type='text/javascript'>
  1181. jQuery(document).ready(function(\$){
  1182. \$('#jtw').charCount( { allowed: $allowed, counterText: '".__('Characters left: ','wp-to-twitter')."' } );
  1183. });
  1184. jQuery(document).ready(function(\$){
  1185. \$('#side-sortables .tabs a[href=\"$first\"]').addClass('active');
  1186. \$('#side-sortables .wptab').not('$first').hide();
  1187. \$('#side-sortables .tabs a').on('click',function(e) {
  1188. e.preventDefault();
  1189. \$('#side-sortables .tabs a').removeClass('active');
  1190. \$(this).addClass('active');
  1191. var target = $(this).attr('href');
  1192. \$('#side-sortables .wptab').not(target).hide();
  1193. \$(target).show();
  1194. });
  1195. });
  1196. </script>
  1197. <style type='text/css'>
  1198. #wp2t h3 span { padding-left: 30px; background: url(".plugins_url('wp-to-twitter/images/twitter-bird-light-bgs.png').") left 50% no-repeat; }
  1199. </style>";
  1200. }
  1201. }
  1202. add_action( 'admin_head', 'wpt_admin_script' );
  1203. // Post the Custom Tweet into the post meta table
  1204. function post_jd_twitter( $id ) {
  1205. if ( empty($_POST) || ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) || wp_is_post_revision($id) || isset($_POST['_inline_edit'] ) || ( defined('DOING_AJAX') && DOING_AJAX ) || !wpt_in_post_type( $id ) ) { return; }
  1206. if ( isset( $_POST['_yourls_keyword'] ) ) {
  1207. $yourls = $_POST[ '_yourls_keyword' ];
  1208. $update = update_post_meta( $id, '_yourls_keyword', $yourls );
  1209. }
  1210. if ( isset( $_POST[ '_jd_twitter' ] ) ) {
  1211. $jd_twitter = $_POST[ '_jd_twitter' ];
  1212. $update = update_post_meta( $id, '_jd_twitter', $jd_twitter );
  1213. }
  1214. if ( isset( $_POST[ '_jd_wp_twitter' ] ) && $_POST['_jd_wp_twitter'] != '' ) {
  1215. $jd_wp_twitter = $_POST[ '_jd_wp_twitter' ];
  1216. $update = update_post_meta( $id, '_jd_wp_twitter', $jd_wp_twitter );
  1217. }
  1218. if ( isset( $_POST[ '_jd_tweet_this' ] ) ) {
  1219. $jd_tweet_this = ( $_POST[ '_jd_tweet_this' ] == 'no')?'no':'yes';
  1220. $update = update_post_meta( $id, '_jd_tweet_this', $jd_tweet_this );
  1221. } else {
  1222. if ( isset($_POST['_wpnonce'] ) ) {
  1223. $jd_tweet_default = ( get_option( 'jd_tweet_default' ) == 1 )?'no':'yes';
  1224. $update = update_post_meta( $id, '_jd_tweet_this', $jd_tweet_default );
  1225. }
  1226. }
  1227. if ( isset( $_POST['wpt_clear_history'] ) && $_POST['wpt_clear_history'] == 'clear' ) {
  1228. // delete stored Tweets and errors
  1229. delete_post_meta( $id, '_wpt_failed' );
  1230. delete_post_meta( $id, '_jd_wp_twitter' );
  1231. // delete stored short URLs
  1232. delete_post_meta( $id, '_wp_jd_supr' );
  1233. delete_post_meta( $id, '_wp_jd_ind' );
  1234. delete_post_meta( $id, '_wp_jd_bitly' );
  1235. delete_post_meta( $id, '_wp_jd_wp' );
  1236. delete_post_meta( $id, '_wp_jd_yourls' );
  1237. delete_post_meta( $id, '_wp_jd_url' );
  1238. delete_post_meta( $id, '_wp_jd_goo' );
  1239. delete_post_meta( $id, '_wp_jd_target' );
  1240. delete_post_meta( $id, '_wp_jd_clig' );
  1241. delete_post_meta( $id, '_wp_jd_joturl' );
  1242. }
  1243. // WPT PRO //
  1244. $update = apply_filters( 'wpt_insert_post', $_POST, $id );
  1245. // WPT PRO //
  1246. // only send debug data if post meta is updated.
  1247. if ( WPT_DEBUG && function_exists( 'wpt_pro_exists' ) && ( $update == true || is_int( $update ) ) ) {
  1248. wpt_mail( "Post Meta Inserted: #$id",print_r($_POST,1) ); // DEBUG
  1249. }
  1250. }
  1251. function jd_twitter_profile() {
  1252. global $user_ID;
  1253. get_currentuserinfo();
  1254. if ( current_user_can( 'wpt_twitter_oauth' ) || current_user_can('update_core') ) {
  1255. $user_edit = ( isset($_GET['user_id']) )?(int) $_GET['user_id']:$user_ID;
  1256. $is_enabled = get_user_meta( $user_edit, 'wp-to-twitter-enable-user',true );
  1257. $twitter_username = get_user_meta( $user_edit, 'wp-to-twitter-user-username',true );
  1258. $wpt_remove = get_user_meta( $user_edit, 'wpt-remove', true );
  1259. ?>
  1260. <h3><?php _e('WP Tweets User Settings', 'wp-to-twitter'); ?></h3>
  1261. <?php if ( function_exists('wpt_connect_oauth_message') ) { wpt_connect_oauth_message( $user_edit ); } ?>
  1262. <table class="form-table">
  1263. <tr>
  1264. <th scope="row"><?php _e("Use My Twitter Username", 'wp-to-twitter'); ?></th>
  1265. <td><input type="radio" name="wp-to-twitter-enable-user" id="wp-to-twitter-enable-user-3" value="mainAtTwitter"<?php if ($is_enabled == "mainAtTwitter") { echo " checked='checked'"; } ?> /> <label for="wp-to-twitter-enable-user-3"><?php _e("Tweet my posts with an @ reference to my username.", 'wp-to-twitter'); ?></label><br />
  1266. <input type="radio" name="wp-to-twitter-enable-user" id="wp-to-twitter-enable-user-4" value="mainAtTwitterPlus"<?php if ($is_enabled == "mainAtTwitterPlus") { echo " checked='checked'"; } ?> /> <label for="wp-to-twitter-enable-user-3"><?php _e("Tweet my posts with an @ reference to both my username and to the main site username.", 'wp-to-twitter'); ?></label>
  1267. </td>
  1268. </tr>
  1269. <tr>
  1270. <th scope="row"><label for="wp-to-twitter-user-username"><?php _e("Your Twitter Username", 'wp-to-twitter'); ?></label></th>
  1271. <td><input type="text" name="wp-to-twitter-user-username" id="wp-to-twitter-user-username" value="<?php echo esc_attr( $twitter_username ); ?>" /> <?php _e('Enter your own Twitter username.', 'wp-to-twitter'); ?></td>
  1272. </tr>
  1273. <tr>
  1274. <th scope="row"><label for="wpt-remove"><?php _e("Hide account name in Tweets", 'wp-to-twitter'); ?></label></th>
  1275. <td><input type="checkbox" name="wpt-remove" id="wpt-remove" value="on"<?php if ( $wpt_remove == 'on' ) { echo ' checked="checked"'; } ?> /> <?php _e('Do not display my account in the #account# template tag.', 'wp-to-twitter'); ?></td>
  1276. </tr>
  1277. <?php if ( !function_exists('wpt_pro_exists') ) { add_filter( 'wpt_twitter_user_fields',create_function('','return;') ); } ?>
  1278. <?php echo apply_filters('wpt_twitter_user_fields',$user_edit ); ?>
  1279. </table>
  1280. <?php
  1281. if ( function_exists('wpt_schedule_tweet') ) {
  1282. if ( function_exists('wtt_connect_oauth') ) { wtt_connect_oauth( $user_edit ); }
  1283. }
  1284. }
  1285. }
  1286. function custom_shortcodes( $sentence, $post_ID ) {
  1287. $pattern = '/([([\[\]?)([A-Za-z0-9-_])*(\]\]]?)+/';
  1288. $params = array(0=>"[[",1=>"]]");
  1289. preg_match_all($pattern,$sentence, $matches);
  1290. if ($matches && is_array($matches[0])) {
  1291. foreach ($matches[0] as $value) {
  1292. $shortcode = "$value";
  1293. $field = str_replace($params, "", $shortcode);
  1294. $custom = apply_filters( 'wpt_custom_shortcode',strip_tags(get_post_meta( $post_ID, $field, TRUE )), $post_ID, $field );
  1295. $sentence = str_replace( $shortcode, $custom, $sentence );
  1296. }
  1297. return $sentence;
  1298. } else {
  1299. return $sentence;
  1300. }
  1301. }
  1302. function jd_twitter_save_profile(){
  1303. global $user_ID;
  1304. get_currentuserinfo();
  1305. if ( isset($_POST['user_id']) ) {
  1306. $edit_id = (int) $_POST['user_id'];
  1307. } else {
  1308. $edit_id = $user_ID;
  1309. }
  1310. $enable = ( isset($_POST['wp-to-twitter-enable-user']) )?$_POST['wp-to-twitter-enable-user']:'';
  1311. $username = ( isset($_POST['wp-to-twitter-user-username']) )?$_POST['wp-to-twitter-user-username']:'';
  1312. $wpt_remove = ( isset($_POST['wpt-remove']) )?'on':'';
  1313. update_user_meta($edit_id ,'wp-to-twitter-enable-user' , $enable );
  1314. update_user_meta($edit_id ,'wp-to-twitter-user-username' , $username );
  1315. update_user_meta($edit_id ,'wpt-remove' , $wpt_remove );
  1316. //WPT PRO
  1317. apply_filters( 'wpt_save_user', $edit_id, $_POST );
  1318. }
  1319. // Add the administrative settings to the "Settings" menu.
  1320. function jd_addTwitterAdminPages() {
  1321. if ( function_exists( 'add_options_page' ) && !function_exists( 'wpt_pro_functions') ) {
  1322. $plugin_page = add_options_page( 'WP to Twitter', 'WP to Twitter', 'manage_options', __FILE__, 'wpt_update_settings' );
  1323. }
  1324. }
  1325. add_action( 'admin_head', 'jd_addTwitterAdminStyles' );
  1326. function jd_addTwitterAdminStyles() {
  1327. if ( isset($_GET['page']) && ( $_GET['page'] == "wp-to-twitter" || $_GET['page'] == "wp-to-twitter/wp-to-twitter.php" || $_GET['page'] == "wp-tweets-pro" || $_GET['page'] == "wp-to-twitter-schedule" || $_GET['page'] == "wp-to-twitter-tweets" || $_GET['page'] == "wp-to-twitter-errors" ) ) {
  1328. wp_enqueue_style( 'wpt-styles', plugins_url( '/wp-to-twitter/styles.css' ) );
  1329. }
  1330. }
  1331. function jd_plugin_action($links, $file) {
  1332. if ( $file == plugin_basename(dirname(__FILE__).'/wp-to-twitter.php') ) {
  1333. $admin_url = ( is_plugin_active('wp-tweets-pro/wpt-pro-functions.php') )?admin_url('admin.php?page=wp-tweets-pro'):admin_url('options-general.php?page=wp-to-twitter/wp-to-twitter.php');
  1334. global $wpt_donate_url;
  1335. $links[] = "<a href='$admin_url'>" . __('Settings', 'wp-to-twitter', 'wp-to-twitter') . "</a>";
  1336. if ( ! function_exists( 'wpt_pro_exists' ) ) { $links[] = "<a href='$wpt_donate_url'>" . __('Upgrade', 'wp-to-twitter', 'wp-to-twitter') . "</a>"; }
  1337. }
  1338. return $links;
  1339. }
  1340. //Add Plugin Actions to WordPress
  1341. add_filter('plugin_action_links', 'jd_plugin_action', -10, 2);
  1342. if ( get_option( 'jd_individual_twitter_users')=='1') {
  1343. add_action( 'show_user_profile', 'jd_twitter_profile' );
  1344. add_action( 'edit_user_profile', 'jd_twitter_profile' );
  1345. add_action( 'profile_update', 'jd_twitter_save_profile');
  1346. }
  1347. $admin_url = ( is_plugin_active('wp-tweets-pro/wpt-pro-functions.php') )?admin_url('admin.php?page=wp-tweets-pro'):admin_url('options-general.php?page=wp-to-twitter/wp-to-twitter.php');
  1348. add_action( 'in_plugin_update_message-wp-to-twitter/wp-to-twitter.php', 'wpt_plugin_update_message' );
  1349. function wpt_plugin_update_message() {
  1350. global $wpt_version;
  1351. $note = '';
  1352. define('WPT_PLUGIN_README_URL', 'http://svn.wp-plugins.org/wp-to-twitter/trunk/readme.txt');
  1353. $response = wp_remote_get( WPT_PLUGIN_README_URL, array ('user-agent' => 'WordPress/WP to Twitter' . $wpt_version . '; ' . get_bloginfo( 'url' ) ) );
  1354. if ( ! is_wp_error( $response ) || is_array( $response ) ) {
  1355. $data = $response['body'];
  1356. $bits=explode('== Upgrade Notice ==',$data);
  1357. $note = '<div id="wpt-upgrade"><p><strong style="color:#c22;">Upgrade Notes:</strong> '.nl2br(trim($bits[1])).'</p></div>';
  1358. } else {
  1359. printf(__('<br /><strong>Note:</strong> Please review the <a class="thickbox" href="%1$s">changelog</a> before upgrading.','wp-to-twitter'),'plugin-install.php?tab=plugin-information&amp;plugin=wp-to-twitter&amp;TB_iframe=true&amp;width=640&amp;height=594');
  1360. }
  1361. echo $note;
  1362. }
  1363. if ( get_option( 'jd_twit_blogroll' ) == '1' ) {
  1364. add_action( 'add_link', 'jd_twit_link' );
  1365. }
  1366. add_action( 'save_post', 'wpt_twit', 16 );
  1367. add_action( 'save_post', 'post_jd_twitter', 10 );
  1368. function wpt_in_post_type( $id ) {
  1369. $post_type_settings = get_option('wpt_post_types');
  1370. $post_types = array_keys($post_type_settings);
  1371. $type = get_post_type( $id );
  1372. if ( in_array( $type, $post_types ) ) {
  1373. if ( $post_type_settings[$type]['post-edited-update'] == '1' || $post_type_settings[$type]['post-published-update'] == '1' ) {
  1374. return true;
  1375. }
  1376. }
  1377. return false;
  1378. }
  1379. add_action( 'future_to_publish', 'wpt_future_to_publish', 16 );
  1380. function wpt_future_to_publish( $post ) {
  1381. $id = $post->ID;
  1382. if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || wp_is_post_revision( $id ) || !wpt_in_post_type( $id ) ) { return; }
  1383. wpt_twit_future( $id );
  1384. }
  1385. function wpt_twit( $id ) {
  1386. if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || wp_is_post_revision( $id ) || !wpt_in_post_type( $id ) ) return;
  1387. $post = get_post( $id );
  1388. if ( $post->post_status != 'publish' ) return; // is there any reason to accept any other status?
  1389. wpt_twit_instant( $id );
  1390. }
  1391. add_action( 'xmlrpc_publish_post', 'wpt_twit_xmlrpc' );
  1392. add_action( 'publish_phone', 'wpt_twit_xmlrpc' );
  1393. function wpt_twit_future( $id ) {
  1394. set_transient( '_wpt_twit_future', $id, 10 );
  1395. // instant action has already run for this post. // prevent running actions twice (need both for older WP)
  1396. if ( get_transient ( '_wpt_twit_instant' ) && get_transient( '_wpt_twit_instant' ) == $id ) {
  1397. delete_transient( '_wpt_twit_instant' );
  1398. return;
  1399. }
  1400. jd_twit( $id, 'future' );
  1401. }
  1402. function wpt_twit_instant( $id ) {
  1403. set_transient( '_wpt_twit_instant', $id, 10 );
  1404. // future action has already run for this post.
  1405. if ( get_transient ( '_wpt_twit_future' ) && get_transient( '_wpt_twit_future' ) == $id ) {
  1406. delete_transient( '_wpt_twit_future' );
  1407. return;
  1408. }
  1409. // xmlrpc action has already run for this post.
  1410. if ( get_transient ( '_wpt_twit_xmlrpc' ) && get_transient( '_wpt_twit_xmlrpc' ) == $id ) {
  1411. delete_transient( '_wpt_twit_xmlrpc' );
  1412. return;
  1413. }
  1414. jd_twit( $id, 'instant' );
  1415. }
  1416. function wpt_twit_xmlrpc( $id ) {
  1417. set_transient( '_wpt_twit_xmlrpc', $id, 10 );
  1418. if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || wp_is_post_revision( $id ) || !wpt_in_post_type( $id ) ) { return $id; }
  1419. jd_twit( $id, 'xmlrpc' );
  1420. }
  1421. add_action( 'wpt_schedule_promotion_action', 'wpt_schedule_promotion' );
  1422. function wpt_schedule_promotion() {
  1423. if ( !function_exists( 'wpt_pro_exists' ) && get_option( 'wpt_promotion_scheduled' ) == 1 ) {
  1424. update_option( 'wpt_promotion_scheduled', 2 );
  1425. }
  1426. }
  1427. function wpt_dismiss_promotion() {
  1428. if ( isset($_GET['dismiss']) && $_GET['dismiss'] == 'promotion' ) {
  1429. update_option( 'wpt_promotion_scheduled', 3 );
  1430. }
  1431. }
  1432. wpt_dismiss_promotion();
  1433. add_action( 'admin_notices', 'wpt_promotion_notice' );
  1434. function wpt_promotion_notice() {
  1435. if ( current_user_can( 'activate_plugins' ) && get_option( 'wpt_promotion_scheduled' ) == 2 && get_option( 'jd_donations' ) != 1 ) {
  1436. $upgrade = "http://www.joedolson.com/articles/wp-tweets-pro/";
  1437. $dismiss = admin_url('options-general.php?page=wp-to-twitter/wp-to-twitter.php&dismiss=promotion');
  1438. echo "<div class='updated fade'><p>".sprintf( __("I hope you've enjoyed <strong>WP to Twitter</strong>! Take a look at <a href='%s'>upgrading to WP Tweets PRO</a> for advanced Tweeting with WordPress! <a href='%s'>Dismiss</a>",'wp-to-twitter'), $upgrade, $dismiss )."</p></div>";
  1439. }
  1440. }
  1441. add_action( 'admin_menu', 'jd_addTwitterAdminPages' );
  1442. /* Enqueue styles for Twitter feed */
  1443. add_action('wp_enqueue_scripts', 'wpt_stylesheet');
  1444. function wpt_stylesheet() {
  1445. $apply = apply_filters( 'wpt_enqueue_feed_styles', true );
  1446. if ( $apply ) {
  1447. $file = apply_filters( 'wpt_feed_stylesheet', plugins_url( 'css/twitter-feed.css', __FILE__ ) );
  1448. wp_register_style( 'wpt-twitter-feed', $file );
  1449. wp_enqueue_style( 'wpt-twitter-feed' );
  1450. }
  1451. }
  1452. add_filter( 'wpt_enqueue_feed_styles', 'wpt_permit_feed_styles' );
  1453. function wpt_permit_feed_styles( $value ) {
  1454. if ( get_option( 'wpt_permit_feed_styles' ) == 1 ) {
  1455. $value = false;
  1456. }
  1457. return $value;
  1458. }
  1459. /*
  1460. // Add notes about Tweet status to posts admin
  1461. function wpt_column($cols) {
  1462. $cols['wpt'] = __('Tweet Status','wp-to-twitter');
  1463. return $cols;
  1464. }
  1465. // Echo the ID for the new column
  1466. function wpt_value($column_name, $id) {
  1467. if ($column_name == 'wpt') {
  1468. $marked = ucfirst( ( get_post_meta($id,'_jd_tweet_this',true) ) );
  1469. echo $marked;
  1470. }
  1471. }
  1472. function wpt_return_value($value, $column_name, $id) {
  1473. if ( $column_name == 'wpt' ) {
  1474. $value = $id;
  1475. }
  1476. return $value;
  1477. }
  1478. */
  1479. // Output CSS for width of new column
  1480. add_action('admin_head', 'wpt_css');
  1481. function wpt_css() {
  1482. ?>
  1483. <style type="text/css">
  1484. th#wpt { width: 60px; }
  1485. .wpt_twitter .authorized { padding: 1px 3px; border-radius: 3px; background: #070; color: #fff; }
  1486. </style>
  1487. <?php
  1488. }
  1489. /*
  1490. // Actions/Filters for various tables and the css output
  1491. add_action('admin_init', 'wpt_add');
  1492. function wpt_add() {
  1493. $post_type_settings = get_option('wpt_post_types');
  1494. $post_types = array_keys($post_type_settings);
  1495. add_action('admin_head', 'wpt_css');
  1496. if ( !$post_types || in_array( 'post', $post_types ) ) {
  1497. add_filter('manage_posts_columns', 'wpt_column');
  1498. add_action('manage_posts_custom_column', 'wpt_value', 10, 2);
  1499. }
  1500. if ( !$post_types || in_array( 'page', $post_types ) ) {
  1501. add_filter('manage_pages_columns', 'wpt_column');
  1502. add_action('manage_pages_custom_column', 'wpt_value', 10, 2);
  1503. }
  1504. foreach ( $post_types as $types ) {
  1505. add_action("manage_${types}_columns", 'wpt_column');
  1506. add_filter("manage_${types}_custom_column", 'wpt_value', 10, 2);
  1507. }
  1508. } */