function jd_update_oauth_settings($auth = false, $post = false) { if (isset($post['oauth_settings'])) { switch ($post['oauth_settings']) { case 'wtt_oauth_test': if (!wp_verify_nonce($post['_wpnonce'], 'wp-to-twitter-nonce') && !$auth) { wp_die('Oops, please try again.'); } if (!empty($post['wtt_app_consumer_key']) && !empty($post['wtt_app_consumer_secret']) && !empty($post['wtt_oauth_token']) && !empty($post['wtt_oauth_token_secret'])) { $ack = trim($post['wtt_app_consumer_key']); $acs = trim($post['wtt_app_consumer_secret']); $ot = trim($post['wtt_oauth_token']); $ots = trim($post['wtt_oauth_token_secret']); if (!$auth) { update_option('app_consumer_key', $ack); update_option('app_consumer_secret', $acs); update_option('oauth_token', $ot); update_option('oauth_token_secret', $ots); } else { update_user_meta($auth, 'app_consumer_key', $ack); update_user_meta($auth, 'app_consumer_secret', $acs); update_user_meta($auth, 'oauth_token', $ot); update_user_meta($auth, 'oauth_token_secret', $ots); } $message = 'failed'; if ($connection = wtt_oauth_connection($auth)) { $data = $connection->get('https://api.twitter.com/1.1/account/verify_credentials.json'); if ($connection->http_code != '200') { $data = json_decode($data); $code = "<a href='https://dev.twitter.com/docs/error-codes-responses'>" . $data->errors[0]->code . "</a>"; $error = $data->errors[0]->message; update_option('wpt_error', "{$code}: {$error}"); } else { delete_option('wpt_error'); } if ($connection->http_code == '200') { $error_information = ''; $decode = json_decode($data); if (!$auth) { update_option('wtt_twitter_username', stripslashes($decode->screen_name)); } else { update_user_meta($auth, 'wtt_twitter_username', stripslashes($decode->screen_name)); } $oauth_hash = wtt_oauth_credentials_to_hash($auth); if (!$auth) { update_option('wtt_oauth_hash', $oauth_hash); } else { update_user_meta($auth, 'wtt_oauth_hash', $oauth_hash); } $message = 'success'; delete_option('wpt_curl_error'); } else { if ($connection->http_code == 0) { $error_information = __("WP to Twitter was unable to establish a connection to Twitter.", 'wp-to-twitter'); update_option('wpt_curl_error', "{$error_information}"); } else { $status = isset($connection->http_header['status']) ? $connection->http_header['status'] : '404'; $error_information = array("http_code" => $connection->http_code, "status" => $status); $error_code = __("Twitter response: http_code {$error_information['http_code']} - {$error_information['status']}", 'wp-to-twitter'); update_option('wpt_curl_error', $error_code); } } if (get_option('wp_debug_oauth') == '1') { echo "<pre><strong>Summary Connection Response:</strong><br />"; print_r($error_information); echo "<br /><strong>Account Verification Data:</strong><br />"; print_r($data); echo "<br /><strong>Full Connection Response:</strong><br />"; print_r($connection); echo "</pre>"; } } } else { $message = "nodata"; } if ($message == 'failed' && (time() < strtotime($connection->http_header['date']) - 300 || time() > strtotime($connection->http_header['date']) + 300)) { $message = 'nosync'; } return $message; break; case 'wtt_twitter_disconnect': if (!wp_verify_nonce($post['_wpnonce'], 'wp-to-twitter-nonce') && !$auth) { wp_die('Oops, please try again.'); } if (!$auth) { update_option('app_consumer_key', ''); update_option('app_consumer_secret', ''); update_option('oauth_token', ''); update_option('oauth_token_secret', ''); update_option('wtt_twitter_username', ''); } else { delete_user_meta($auth, 'app_consumer_key'); delete_user_meta($auth, 'app_consumer_secret'); delete_user_meta($auth, 'oauth_token'); delete_user_meta($auth, 'oauth_token_secret'); delete_user_meta($auth, 'wtt_twitter_username'); } $message = "cleared"; return $message; break; } } return ''; }
function jd_doTwitterAPIPost($twit, $auth = false, $id = false, $media = false) { if (!wpt_check_oauth($auth)) { $error = __('This account is not authorized to post to Twitter.', 'wp-to-twitter'); wpt_saves_error($id, $auth, $twit, $error, '401', time()); wpt_set_log('wpt_status_message', $id, $error); return true; } // exit silently if not authorized $check = !$auth ? get_option('jd_last_tweet') : get_user_meta($auth, 'wpt_last_tweet', true); // get user's last tweet // prevent duplicate Tweets if ($check == $twit) { if (WPT_DEBUG && function_exists('wpt_pro_exists')) { wpt_mail("Matched: tweet identical: #{$id}", "{$twit}, {$auth}, {$id}"); // DEBUG } $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'); wpt_saves_error($id, $auth, $twit, $error, '403', time()); wpt_set_log('wpt_status_message', $id, $error); return true; } else { if ($twit == '' || !$twit) { if (WPT_DEBUG && function_exists('wpt_pro_exists')) { wpt_mail("Tweet check: empty sentence: #{$id}", "{$twit}, {$auth}, {$id}"); // DEBUG } $error = __('This tweet was blank and could not be sent to Twitter.', 'wp-tweets-pro'); wpt_saves_error($id, $auth, $twit, $error, '403', time()); wpt_set_log('wpt_status_message', $id, $error); return true; } else { // must be designated as media and have a valid attachment $attachment = $media ? wpt_post_attachment($id) : false; if ($attachment) { $meta = wp_get_attachment_metadata($attachment); if (!isset($meta['width'], $meta['height'])) { if (WPT_DEBUG && function_exists('wpt_pro_exists')) { wpt_mail("Image Data Does not Exist for Attachment #{$attachment}", print_r($args, 1)); } $attachment = false; } } // support for HTTP deprecated as of 1/14/2014 -- https://dev.twitter.com/discussions/24239 $api = $media && $attachment ? "https://api.twitter.com/1.1/statuses/update_with_media.json" : "https://api.twitter.com/1.1/statuses/update.json"; if (wtt_oauth_test($auth) && ($connection = wtt_oauth_connection($auth))) { if ($media && $attachment) { $connection->media($api, array('status' => $twit, 'source' => 'wp-to-twitter', 'include_entities' => 'true', 'id' => $id, 'auth' => $auth)); } else { $connection->post($api, array('status' => $twit, 'source' => 'wp-to-twitter', 'include_entities' => 'true')); } $http_code = $connection ? $connection->http_code : 'failed'; } else { if (wtt_oauth_test(false) && ($connection = wtt_oauth_connection(false))) { if ($media) { $connection->media($api, array('status' => $twit, 'source' => 'wp-to-twitter', 'include_entities' => 'true', 'id' => $id, 'auth' => $auth)); } else { $connection->post($api, array('status' => $twit, 'source' => 'wp-to-twitter', 'include_entities' => 'true')); } $http_code = $connection ? $connection->http_code : 'failed'; } } if (WPT_DEBUG && function_exists('wpt_pro_exists')) { wpt_mail('Twitter Connection', print_r($connection, 1)); } if ($connection) { 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 = ''; } $return = false; switch ($http_code) { case '200': $return = true; $error = __("200 OK: Success!", 'wp-to-twitter'); update_option('wpt_authentication_missing', false); break; case '304': $error = __("304 Not Modified: There was no new data to return", 'wp-to-twitter'); break; case '400': $error = __("400 Bad Request: The request was invalid. This is the status code returned during rate limiting.", 'wp-to-twitter'); break; case '401': $error = __("401 Unauthorized: Authentication credentials were missing or incorrect.", 'wp-to-twitter'); update_option('wpt_authentication_missing', "{$auth}"); break; case '403': $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'); break; case '404': $error = __("404 Not Found: The URI requested is invalid or the resource requested does not exist.", 'wp-to-twitter'); break; case '406': $error = __("406 Not Acceptable: Invalid Format Specified.", 'wp-to-twitter'); break; case '422': $error = __("422 Unprocessable Entity: The image uploaded could not be processed..", 'wp-to-twitter'); break; case '429': $error = __("429 Too Many Requests: You have exceeded your rate limits.", 'wp-to-twitter'); break; case '500': $error = __("500 Internal Server Error: Something is broken at Twitter.", 'wp-to-twitter'); break; case '502': $error = __("502 Bad Gateway: Twitter is down or being upgraded.", 'wp-to-twitter'); break; case '503': $error = __("503 Service Unavailable: The Twitter servers are up, but overloaded with requests - Please try again later.", 'wp-to-twitter'); break; case '504': $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'); break; default: $error = __("<strong>Code {$http_code}</strong>: Twitter did not return a recognized response code.", 'wp-to-twitter'); break; } $error .= $supplement != '' ? " {$supplement}" : ''; // debugging if (WPT_DEBUG && function_exists('wpt_pro_exists')) { wpt_mail("Twitter Response: {$http_code}, #{$id}", "{$http_code}, {$error}"); // DEBUG } // end debugging $update = !$auth ? update_option('jd_last_tweet', $twit) : update_user_meta($auth, 'wpt_last_tweet', $twit); wpt_saves_error($id, $auth, $twit, $error, $http_code, time()); if ($http_code == '200') { $jwt = get_post_meta($id, '_jd_wp_twitter', true); if (!is_array($jwt)) { $jwt = array(); } $jwt[] = urldecode($twit); if (empty($_POST)) { $_POST = array(); } $_POST['_jd_wp_twitter'] = $jwt; update_post_meta($id, '_jd_wp_twitter', $jwt); if (!function_exists('wpt_pro_exists')) { // schedule a one-time promotional box for 4 weeks after first successful Tweet. Experiment... if (get_option('wpt_promotion_scheduled') == false) { wp_schedule_single_event(time() + 60 * 60 * 24 * 7 * 4, 'wpt_schedule_promotion_action'); update_option('wpt_promotion_scheduled', 1); } } } if (!$return) { wpt_set_log('wpt_status_message', $id, $error); } else { wpt_set_log('wpt_status_message', $id, __('Tweet sent successfully.', 'wp-to-twitter')); } return $return; } else { wpt_set_log('wpt_status_message', $id, __('No Twitter OAuth connection found.', 'wp-to-twitter')); return false; } } } }
function jd_doTwitterAPIPost($twit, $auth = false) { // prevent duplicate Tweets if (!wpt_check_oauth($auth)) { return true; } // exit silently if not authorized $check = !$auth ? get_option('jd_last_tweet') : get_user_meta($auth, 'wpt_last_tweet', true); // get user's last tweet if ($check == $twit || $twit == '' || !$twit) { return true; } else { global $jdwp_api_post_status; if (wtt_oauth_test($auth) && ($connection = wtt_oauth_connection($auth))) { $connection->post($jdwp_api_post_status, array('status' => $twit, 'source' => 'wp-to-twitter')); $http_code = $connection ? $connection->http_code : 'failed'; } else { if (wtt_oauth_test(false) && ($connection = wtt_oauth_connection(false))) { $connection->post($jdwp_api_post_status, array('status' => $twit, 'source' => 'wp-to-twitter')); $http_code = $connection ? $connection->http_code : 'failed'; } } if ($connection) { switch ($http_code) { case '200': $return = true; $error = __("200 OK: Success!", 'wp-to-twitter'); delete_option('wpt_authentication_missing'); break; case '400': $return = false; $error = __("400 Bad Request: The request was invalid. This is the status code returned during rate limiting.", 'wp-to-twitter'); break; case '401': $return = false; $error = __("401 Unauthorized: Authentication credentials were missing or incorrect.", 'wp-to-twitter'); update_option('wpt_authentication_missing', 'true'); break; case '403': $return = false; $error = __("403 Forbidden: The request is understood, but it has been refused. This code is used when requests are understood, but are denied by Twitter. Reasons can include: Too many Tweets created in a short time or the same Tweet was submitted twice in a row, among others. This is not an error by WP to Twitter.", 'wp-to-twitter'); break; case '500': $return = false; $error = __("500 Internal Server Error: Something is broken at Twitter.", 'wp-to-twitter'); break; case '503': $return = false; $error = __("503 Service Unavailable: The Twitter servers are up, but overloaded with requests - Please try again later.", 'wp-to-twitter'); break; case '502': $return = false; $error = __("502 Bad Gateway: Twitter is down or being upgraded.", 'wp-to-twitter'); break; default: $return = false; $error = __("<strong>Code {$http_code}</strong>: Twitter did not return a recognized response code.", 'wp-to-twitter'); break; } // debugging //wp_mail('*****@*****.**','Response code',"$http_code $error" ); // end debugging $update = !$auth ? update_option('jd_last_tweet', $twit) : update_user_meta($auth, 'wpt_last_tweet', $twit); if (!$return) { update_option('jd_status_message', $error); } else { delete_option('jd_status_message'); } return $return; } else { update_option('jd_status_message', __('No Twitter OAuth connection found.', 'wp-to-twitter')); } } }