Пример #1
0
/**
 * Linkify @usernames, #hashtags and t.co links, then return text 
 *
 * @param  object $tweet  a Tweet object (json_decoded result of https://dev.twitter.com/docs/platform-objects/tweets)
 * @return string         formatted tweet
 */
function ozh_ta_linkify_tweet($tweet)
{
    global $ozh_ta;
    $text = $tweet->text;
    // Linkify twitter names if applicable
    if (isset($tweet->entities->user_mentions) && ($mentions = $tweet->entities->user_mentions)) {
        foreach ($mentions as $mention) {
            $screen_name = $mention->screen_name;
            $name = $mention->name;
            $text = ozh_ta_convert_mentions($text, $screen_name, $name);
        }
    }
    // un-t.co links if applicable
    if (isset($tweet->entities->urls) && ($urls = $tweet->entities->urls)) {
        foreach ($urls as $url) {
            $expanded_url = $url->expanded_url;
            $display_url = $url->display_url;
            $tco_url = $url->url;
            $text = ozh_ta_convert_links($text, $expanded_url, $display_url, $tco_url);
        }
    }
    // hashtag links if applicable
    if (isset($tweet->entities->hashtags) && ($hashes = $tweet->entities->hashtags)) {
        foreach ($hashes as $hash) {
            $hash_text = $hash->text;
            $text = ozh_ta_convert_hashtags($text, $hash_text);
        }
    }
    // embed images if applicable. This operation shall be the last one
    if (isset($tweet->extended_entities->media) && ($medias = $tweet->extended_entities->media)) {
        foreach ($medias as $media) {
            $media_url = $media->media_url_https;
            $display_url = $media->display_url;
            $expanded_url = $media->expanded_url;
            $tco_url = $media->url;
            // Convert links
            if ($ozh_ta['un_tco'] == 'yes') {
                $replace = sprintf('<a href="%s" title="%s" class="link link_untco link_untco_image">%s</a>', $expanded_url, $expanded_url, $display_url);
            } else {
                $replace = sprintf('<a href="%s" class="link link_tco link_tco_image">%s</a>', $tco_url, $tco_url);
            }
            // Add image
            if ($ozh_ta['embed_images'] == 'yes') {
                $insert = sprintf('<span class="embed_image embed_image_yes"><a href="%s"><img src="%s" /></a></span>', $expanded_url, $media_url);
            } else {
                $insert = '';
            }
            $text = preg_replace('!' . $tco_url . '!', $replace, $text, 1);
            $text .= $insert;
        }
    }
    return $text;
}
Пример #2
0
function ozh_ta_convert_old_posts($text)
{
    // Has this post already been converted? Assuming a span means already formatted
    if (strpos($text, '<span class="') !== false) {
        return $text;
    }
    global $ozh_ta;
    // Get unformatted title: this will be the unmodified original tweet -- pure text, no HTML
    global $post;
    $title = $post->post_title;
    $ID = $post->ID;
    // Keep track of whether the post has been formatted
    $updated = false;
    // Tweet has links that have not been converted
    if ((strpos($title, 'http://') !== false or strpos($title, 'https://') !== false) && strpos($text, 'class="link') === false) {
        preg_match_all('!https?://\\S*!', $title, $matches);
        foreach ($matches[0] as $url) {
            // t.co URL ?
            if ($ozh_ta['un_tco'] == 'yes' && strpos($url, 'http://t.co/') === 0) {
                $expanded_url = ozh_ta_expand_tco_url($url);
                $tco_url = $url;
            } else {
                $expanded_url = $tco_url = $url;
            }
            $display_url = ozh_ta_trim_long_string(preg_replace('/https?:\\/\\//', '', $expanded_url));
            $text = ozh_ta_convert_links($text, $expanded_url, $display_url, $tco_url);
        }
        $updated = true;
    }
    // Tweet has @mentions that have not been converted
    if (strpos($title, '@') !== false && strpos($text, 'class="username') === false) {
        preg_match_all('/\\B@(\\w+)/', $title, $matches);
        // good news, this won't match joe@site.com
        if (isset($matches[1])) {
            foreach ($matches[1] as $mention) {
                $text = ozh_ta_convert_mentions($text, $mention, $mention);
            }
        }
        $updated = true;
    }
    // Tweet has #hashtags that have not been converted
    if (strpos($title, '#') !== false && strpos($text, 'class="hashtag') === false) {
        preg_match_all('/\\B#(\\w*[a-zA-Z-]+\\w*)/', $text, $matches);
        if (isset($matches[1])) {
            foreach ($matches[1] as $tag) {
                $text = ozh_ta_convert_hashtags($text, $tag);
            }
            if ($ozh_ta['add_hash_as_tags'] == 'yes') {
                wp_set_post_tags($ID, implode(', ', $matches[1]));
            }
        }
        $updated = true;
    }
    // Did we alter the post? Update it, then
    if ($updated) {
        $post->post_content = $text;
        wp_update_post($post);
    }
    return $text;
}