Exemplo n.º 1
0
/**
 * Generates the shortlink for the post specified by $post_id.
 *
 * @since   0.1
 *
 * @param   int $post_id The post ID we need a shortlink for.
 *
 * @return  bool|string          Returns the shortlink on success.
 */
function wpbitly_generate_shortlink($post_id)
{
    $wpbitly = wpbitly();
    // Avoid creating shortlinks during an autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    // or for revisions
    if (wp_is_post_revision($post_id)) {
        return;
    }
    // Token hasn't been verified, bail
    if (!$wpbitly->get_option('authorized')) {
        return;
    }
    // Verify this is a post we want to generate short links for
    if (!in_array(get_post_type($post_id), $wpbitly->get_option('post_types')) || !in_array(get_post_status($post_id), array('publish', 'future', 'private'))) {
        return;
    }
    // We made it this far? Let's get a shortlink
    $permalink = get_permalink($post_id);
    $shortlink = get_post_meta($post_id, '_wpbitly', true);
    $token = $wpbitly->get_option('oauth_token');
    if (!empty($shortlink)) {
        $url = sprintf(wpbitly_api('expand'), $token, $shortlink);
        $response = wpbitly_get($url);
        wpbitly_debug_log($response, '/expand/');
        if ($permalink == $response['data']['expand'][0]['long_url']) {
            return $shortlink;
        }
    }
    $url = sprintf(wpbitly_api('shorten'), $token, urlencode($permalink));
    $response = wpbitly_get($url);
    wpbitly_debug_log($response, '/shorten/');
    if (is_array($response)) {
        $shortlink = $response['data']['url'];
        update_post_meta($post_id, '_wpbitly', $shortlink);
    }
    return $shortlink;
}
 /**
  * Handles the display of the metabox.
  *
  * @since   2.0
  *
  * @param   object $post WordPress passed $post object
  * @param   array  $args Passed by our call to add_meta_box(), just the $shortlink in this case.
  */
 public function display_metabox($post, $args)
 {
     $wpbitly = wpbitly();
     $shortlink = $args['args'][0];
     // Look for a clicks response
     $url = sprintf(wpbitly_api('link/clicks'), $wpbitly->get_option('oauth_token'), $shortlink);
     $response = wpbitly_get($url);
     if (is_array($response)) {
         $clicks = $response['data']['link_clicks'];
     }
     // Look for referring domains metadata
     $url = sprintf(wpbitly_api('link/refer'), $wpbitly->get_option('oauth_token'), $shortlink);
     $response = wpbitly_get($url);
     if (is_array($response)) {
         $refer = $response['data']['referring_domains'];
     }
     echo '<label class="screen-reader-text" for="new-tag-post_tag">' . __('Bitly Statistics', 'wp-bitly') . '</label>';
     if (isset($clicks) && isset($refer)) {
         echo '<p>' . __('Global click through:', 'wp-bitly') . ' <strong>' . $clicks . '</strong></p>';
         if (!empty($refer)) {
             echo '<h4 style="padding-bottom: 3px; border-bottom: 4px solid #eee;">' . __('Your link was shared on', 'wp-bitly') . '</h4>';
             foreach ($refer as $domain) {
                 if (isset($domain['url'])) {
                     printf('<a href="%1$s" target="_blank" title="%2$s">%2$s</a> (%3$d)<br>', $domain['url'], $domain['domain'], $domain['clicks']);
                 } else {
                     printf('<strong>%1$s</strong> (%2$d)<br>', $domain['domain'], $domain['clicks']);
                 }
             }
         }
     } else {
         echo '<p class="error">' . __('There was a problem retrieving information about your link. There may be no statistics yet.', 'wp-bitly') . '</p>';
     }
 }