function jd_remote_json($url, $array = true) { $input = jd_fetch_url($url); $obj = json_decode($input, $array); return $obj; // TODO: some error handling ? }
function jd_remote_json($url, $array = true) { $input = jd_fetch_url($url); $obj = json_decode($input, $array); if (function_exists('json_last_error')) { // > PHP 5.3 try { if (is_null($obj)) { switch (json_last_error()) { case JSON_ERROR_DEPTH: $msg = ' - Maximum stack depth exceeded'; break; case JSON_ERROR_STATE_MISMATCH: $msg = ' - Underflow or the modes mismatch'; break; case JSON_ERROR_CTRL_CHAR: $msg = ' - Unexpected control character found'; break; case JSON_ERROR_SYNTAX: $msg = ' - Syntax error, malformed JSON'; break; case JSON_ERROR_UTF8: $msg = ' - Malformed UTF-8 characters, possibly incorrectly encoded'; break; default: $msg = ' - Unknown error'; break; } throw new Exception($msg); } } catch (Exception $e) { return $e->getMessage(); } } return $obj; }
function jd_shorten_link($url, $thisposttitle, $post_ID, $testmode = false) { if (WPT_DEBUG && function_exists('wpt_pro_exists')) { wpt_mail("Initial Link Data: #{$post_ID}", "{$url}, {$thisposttitle}, {$post_ID}, {$testmode}"); // DEBUG } // filter link before sending to shortener or adding analytics $url = apply_filters('wpt_shorten_link', $url, $post_ID); if ($testmode == false) { if (get_option('use-twitter-analytics') == 1 || get_option('use_dynamic_analytics') == 1) { if (get_option('use_dynamic_analytics') == '1') { $campaign_type = get_option('jd_dynamic_analytics'); if ($campaign_type == "post_category" && $testmode != 'link') { $category = get_the_category($post_ID); $campaign = sanitize_title($category[0]->cat_name); } else { if ($campaign_type == "post_ID") { $campaign = $post_ID; } else { if ($campaign_type == "post_title" && $testmode != 'link') { $post = get_post($post_ID); $campaign = sanitize_title($post->post_title); } else { if ($testmode != 'link') { $post = get_post($post_ID); $post_author = $post->post_author; $campaign = get_the_author_meta('user_login', $post_author); } else { $post_author = ''; $campaign = ''; } } } } } else { $campaign = get_option('twitter-analytics-campaign'); } if (strpos($url, "%3F") === FALSE && strpos($url, "?") === FALSE) { $ct = "?"; } else { $ct = "&"; } $medium = apply_filters('wpt_utm_medium', 'twitter'); $source = apply_filters('wpt_utm_source', 'twitter'); $ga = "utm_campaign={$campaign}&utm_medium={$medium}&utm_source={$source}"; $url .= $ct .= $ga; } $url = urldecode(trim($url)); // prevent double-encoding $encoded = urlencode($url); } else { $url = urldecode(trim($url)); // prevent double-encoding $encoded = urlencode($url); } // custom word setting $keyword_format = get_option('jd_keyword_format') == '1' ? $post_ID : false; $keyword_format = get_option('jd_keyword_format') == '2' ? get_post_meta($post_ID, '_yourls_keyword', true) : $keyword_format; $error = ''; // Generate and grab the short url switch (get_option('jd_shortener')) { case 0: case 1: case 3: $shrink = $url; break; case 4: if (function_exists('wp_get_shortlink')) { // wp_get_shortlink doesn't natively support custom post types; but don't return an error in that case. $shrink = $post_ID != false ? wp_get_shortlink($post_ID, 'post') : $url; } if (!$shrink) { $shrink = $url; } break; case 2: // updated to v3 3/31/2010 $bitlyapi = trim(get_option('bitlyapi')); $bitlylogin = trim(strtolower(get_option('bitlylogin'))); $decoded = jd_remote_json("https://api-ssl.bitly.com/v3/shorten?longUrl=" . $encoded . "&login="******"&apiKey=" . $bitlyapi . "&format=json"); if ($decoded) { if ($decoded['status_code'] != 200) { $shrink = $decoded; $error = $decoded['status_txt']; } else { $shrink = $decoded['data']['url']; } } else { $shrink = false; } if (!is_valid_url($shrink)) { $shrink = false; } break; case 5: // local YOURLS installation global $yourls_reserved_URL; define('YOURLS_INSTALLING', true); // Pretend we're installing YOURLS to bypass test for install or upgrade define('YOURLS_FLOOD_DELAY_SECONDS', 0); // Disable flood check $opath = get_option('yourlspath'); $ypath = str_replace('user', 'includes', $opath); if (file_exists(dirname($ypath) . '/load-yourls.php')) { // YOURLS 1.4+ require_once dirname($ypath) . '/load-yourls.php'; global $ydb; if (function_exists('yourls_add_new_link')) { $yourls_result = yourls_add_new_link($url, $keyword_format, $thisposttitle); } else { $yourls_result = $url; } } else { // YOURLS 1.3 if (file_exists(get_option('yourslpath'))) { require_once get_option('yourlspath'); $yourls_db = new wpdb(YOURLS_DB_USER, YOURLS_DB_PASS, YOURLS_DB_NAME, YOURLS_DB_HOST); $yourls_result = yourls_add_new_link($url, $keyword_format, $yourls_db); } } if ($yourls_result) { $shrink = $yourls_result['shorturl']; } else { $shrink = false; } break; case 6: // remote YOURLS installation $yourlslogin = trim(get_option('yourlslogin')); $yourlsapi = stripcslashes(get_option('yourlsapi')); $api_url = sprintf(get_option('yourlsurl') . '?username=%s&password=%s&url=%s&format=json&action=shorturl&keyword=%s', $yourlslogin, $yourlsapi, $encoded, $keyword_format); $json = jd_remote_json($api_url, false); if ($json) { $shrink = $json->shorturl; } else { $shrink = false; } break; case 7: $suprapi = trim(get_option('suprapi')); $suprlogin = trim(get_option('suprlogin')); if ($suprapi != '') { $decoded = jd_remote_json("http://su.pr/api/shorten?longUrl=" . $encoded . "&login="******"&apiKey=" . $suprapi); } else { $decoded = jd_remote_json("http://su.pr/api/shorten?longUrl=" . $encoded); } if ($decoded['statusCode'] == 'OK') { $page = str_replace("&", "&", urldecode($url)); $shrink = $decoded['results'][$page]['shortUrl']; $error = $decoded['errorMessage']; } else { $shrink = false; $error = $decoded['errorMessage']; } if (!is_valid_url($shrink)) { $shrink = false; } break; case 8: // Goo.gl $target = "https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyBSnqQOg3vX1gwR7y2l-40yEG9SZiaYPUQ"; $body = "{'longUrl':'{$url}'}"; //$body = json_encode($data); $json = jd_fetch_url($target, 'POST', $body, 'Content-Type: application/json'); $decoded = json_decode($json); //$url = $decoded['id']; $shrink = $decoded->id; if (!is_valid_url($shrink)) { $shrink = false; } break; case 9: // Twitter Friendly Links $shrink = $url; if (function_exists('twitter_link')) { // use twitter_link if available $shrink = twitter_link($post_ID); } break; case 10: // jotURL //jotURL, added: 2013-04-10 $joturlapi = trim(get_option('joturlapi')); $joturllogin = trim(get_option('joturllogin')); $joturl_longurl_params = trim(get_option('joturl_longurl_params')); if ($joturl_longurl_params != '') { if (strpos($url, "%3F") === FALSE && strpos($url, "?") === FALSE) { $ct = "?"; } else { $ct = "&"; } $url .= $ct . $joturl_longurl_params; $encoded = urlencode(urldecode(trim($url))); // prevent double-encoding } //\jotURL $decoded = jd_fetch_url("https://api.joturl.com/a/v1/shorten?url=" . $encoded . "&login="******"&key=" . $joturlapi . "&format=plain"); if ($decoded !== false) { $shrink = $decoded; //jotURL, added: 2013-04-10 $joturl_shorturl_params = trim(get_option('joturl_shorturl_params')); if ($joturl_shorturl_params != '') { if (strpos($shrink, "%3F") === FALSE && strpos($shrink, "?") === FALSE) { $ct = "?"; } else { $ct = "&"; } $shrink .= $ct . $joturl_shorturl_params; } //\jotURL } else { $error = $decoded; $shrink = false; } if (!is_valid_url($shrink)) { $shrink = false; } break; update_option('wpt_shortener_status', "{$shrink} : {$error}"); } if (!$testmode) { if ($shrink === false || filter_var($shrink, FILTER_VALIDATE_URL) === false) { update_option('wp_url_failure', '1'); $shrink = urldecode($url); } else { update_option('wp_url_failure', '0'); } } wpt_store_url($post_ID, $shrink); return $shrink; }
function jd_shorten_link($thispostlink, $thisposttitle, $post_ID, $testmode = 'false') { // filter link before sending to shortener or adding analytics $thispostlink = apply_filters('wpt_shorten_link', $thispostlink, $post_ID); $suprapi = trim(get_option('suprapi')); $suprlogin = trim(get_option('suprlogin')); $bitlyapi = trim(get_option('bitlyapi')); $bitlylogin = trim(strtolower(get_option('bitlylogin'))); $yourlslogin = trim(get_option('yourlslogin')); $yourlsapi = stripcslashes(get_option('yourlsapi')); if ($testmode == 'false') { if (get_option('use-twitter-analytics') == 1 || get_option('use_dynamic_analytics') == 1) { if (get_option('use_dynamic_analytics') == '1') { $campaign_type = get_option('jd_dynamic_analytics'); if ($campaign_type == "post_category" && $testmode != 'link') { $category = get_the_category($post_ID); $campaign = $category[0]->cat_name; } else { if ($campaign_type == "post_ID") { $campaign = $post_ID; } else { if ($campaign_type == "post_title" && $testmode != 'link') { $post = get_post($post_ID); $campaign = $post->post_title; } else { if ($testmode != 'link') { $post = get_post($post_ID); $post_author = $post->post_author; $campaign = get_the_author_meta('user_login', $post_author); } else { $post_author = ''; $campaign = ''; } } } } } else { $campaign = get_option('twitter-analytics-campaign'); } $campaign = urlencode($campaign); if (strpos($thispostlink, "%3F") === FALSE && strpos($thispostlink, "?") === FALSE) { $ct = "?"; } else { $ct = "&"; } $ga = "utm_campaign={$campaign}&utm_medium=twitter&utm_source=twitter"; $thispostlink .= $ct .= $ga; } } $thispostlink = urldecode(trim($thispostlink)); $thispostlink = urlencode($thispostlink); // custom word setting $keyword_format = get_option('jd_keyword_format') == '1' ? $post_ID : ''; $keyword_format = get_option('jd_keyword_format') == '2' ? get_post_meta($post_ID, '_yourls_keyword', true) : $keyword_format; // Generate and grab the short url switch (get_option('jd_shortener')) { case 0: case 1: $shrink = urldecode($thispostlink); case 4: if (function_exists('wp_get_shortlink')) { $shrink = wp_get_shortlink($post_ID); } else { $shrink = urldecode($thispostlink); } break; case 2: // updated to v3 3/31/2010 $decoded = jd_remote_json("http://api.bitly.com/v3/shorten?longUrl=" . $thispostlink . "&login="******"&apiKey=" . $bitlyapi . "&format=json"); $error = ''; if ($decoded) { if ($decoded['status_code'] != 200) { $shrink = $decoded; $error = $decoded['status_txt']; } else { $shrink = $decoded['data']['url']; } } else { $shrink = false; update_option('wp_bitly_error', "JSON result could not be decoded"); } if (!is_valid_url($shrink)) { $shrink = false; update_option('wp_bitly_error', $error); } break; case 3: $shrink = urldecode($thispostlink); break; case 5: // local YOURLS installation $thispostlink = urldecode($thispostlink); global $yourls_reserved_URL; define('YOURLS_INSTALLING', true); // Pretend we're installing YOURLS to bypass test for install or upgrade define('YOURLS_FLOOD_DELAY_SECONDS', 0); // Disable flood check $opath = get_option('yourlspath'); $ypath = str_replace('user', 'includes', $opath); if (file_exists(dirname($ypath) . '/load-yourls.php')) { // YOURLS 1.4+ global $ydb; require_once dirname($ypath) . '/load-yourls.php'; if (function_exists('yourls_add_new_link')) { $yourls_result = yourls_add_new_link($thispostlink, $keyword_format); } else { $yourls_result = $thispostlink; } } else { // YOURLS 1.3 require_once get_option('yourlspath'); $yourls_db = new wpdb(YOURLS_DB_USER, YOURLS_DB_PASS, YOURLS_DB_NAME, YOURLS_DB_HOST); $yourls_result = yourls_add_new_link($thispostlink, $keyword_format, $yourls_db); } if ($yourls_result) { $shrink = $yourls_result['shorturl']; } else { $shrink = false; } break; case 6: // remote YOURLS installation $api_url = sprintf(get_option('yourlsurl') . '?username=%s&password=%s&url=%s&format=json&action=shorturl&keyword=%s', $yourlslogin, $yourlsapi, $thispostlink, $keyword_format); $json = jd_remote_json($api_url, false); if ($json) { $shrink = $json->shorturl; } else { $shrink = false; } break; case 7: if ($suprapi != '') { $decoded = jd_remote_json("http://su.pr/api/shorten?longUrl=" . $thispostlink . "&login="******"&apiKey=" . $suprapi); } else { $decoded = jd_remote_json("http://su.pr/api/shorten?longUrl=" . $thispostlink); } update_option('wp_supr_error', "Su.pr API result: {$decoded}"); if ($decoded['statusCode'] == 'OK') { $page = str_replace("&", "&", urldecode($thispostlink)); $shrink = $decoded['results'][$page]['shortUrl']; $error = $decoded['errorMessage']; } else { $shrink = false; $error = $decoded['errorMessage']; update_option('wp_supr_error', "JSON result could not be decoded"); } if (!is_valid_url($shrink)) { $shrink = false; update_option('wp_supr_error', $error); } break; case 8: // Goo.gl $url = "https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyBSnqQOg3vX1gwR7y2l-40yEG9SZiaYPUQ"; $link = urldecode($thispostlink); $body = "{'longUrl':'{$link}'}"; //$body = json_encode($data); $json = jd_fetch_url($url, 'POST', $body, 'Content-Type: application/json'); $decoded = json_decode($json); //$url = $decoded['id']; $shrink = $decoded->id; if (!is_valid_url($shrink)) { $shrink = false; } break; } if ($testmode != 'true') { if ($shrink === false || stristr($shrink, "http://") === FALSE) { update_option('wp_url_failure', '1'); $shrink = urldecode($thispostlink); } else { update_option('wp_url_failure', '0'); } } return $shrink; }