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

/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

Large files files are truncated, but you can click here to view the full file

  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='".__(

Large files files are truncated, but you can click here to view the full file