Ejemplo 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;
}
 /**
  * Validate user settings. This will also authorize their OAuth token if it has
  * changed.
  *
  * @since   2.0
  * @uses    wpbitly()
  *
  * @param   array $input WordPress sanitized data array
  *
  * @return  array           WP Bit.ly sanitized data
  */
 public function validate_settings($input)
 {
     $wpbitly = wpbitly();
     $input['debug'] = '1' == $input['debug'] ? true : false;
     $input['oauth_token'] = sanitize_text_field($input['oauth_token']);
     $url = sprintf(wpbitly_api('user/info'), $input['oauth_token']);
     $response = wpbitly_get($url);
     wpbitly_debug_log($response, 'Validate OAuth', $input['debug']);
     $input['authorized'] = isset($response['data']['member_since']) ? true : false;
     if (!isset($input['post_types'])) {
         $input['post_types'] = array();
     } else {
         $post_types = apply_filters('wpbitly_allowed_post_types', get_post_types(array('public' => true)));
         foreach ($input['post_types'] as $key => $pt) {
             if (!in_array($pt, $post_types)) {
                 unset($input['post_types'][$key]);
             }
         }
     }
     return $input;
 }