Beispiel #1
0
/**
 * Print scripts to WP Tweets PRO pages. 
 */
function wpt_admin_script()
{
    global $current_screen;
    if ($current_screen->base == 'post' || $current_screen->id == 'wp-tweets-pro_page_wp-to-twitter-schedule') {
        wp_register_style('wpt-post-styles', plugins_url('css/post-styles.css', __FILE__));
        wp_enqueue_style('wpt-post-styles');
        if ($current_screen->base == 'post') {
            $allowed = 140 - mb_strlen(get_option('jd_twit_prepend') . get_option('jd_twit_append'));
        } else {
            $allowed = wpt_is_ssl(home_url()) ? 137 : 138;
        }
        if (function_exists('wpt_pro_exists') && get_option('jd_individual_twitter_users') == 1) {
            $first = '#authors';
        } else {
            if (function_exists('wpt_pro_exists')) {
                $first = '#custom';
            } else {
                $first = '#notes';
            }
        }
        wp_register_script('wpt-base-js', plugins_url('js/base.js', __FILE__), array('jquery'));
        wp_enqueue_script('wpt-base-js');
        wp_localize_script('wpt-base-js', 'wptSettings', array('allowed' => $allowed, 'first' => $first, 'text' => __('Characters left: ', 'wp-to-twitter')));
        echo "\r\n<style type='text/css'>\r\n#wp2t h3 span { padding-left: 30px; background: url(" . plugins_url('wp-to-twitter/images/twitter-bird-light-bgs.png') . ") left 50% no-repeat; }\r\n</style>";
    }
}
Beispiel #2
0
function wpt_admin_script($hook)
{
    global $current_screen;
    if ($current_screen->base == 'post' || $current_screen->id == 'wp-tweets-pro_page_wp-to-twitter-schedule') {
        wp_register_style('wpt-post-styles', plugins_url('css/post-styles.css', __FILE__));
        wp_enqueue_style('wpt-post-styles');
        if ($current_screen->base == 'post') {
            $allowed = 140 - mb_strlen(get_option('jd_twit_prepend') . get_option('jd_twit_append'));
        } else {
            $allowed = wpt_is_ssl(home_url()) ? 137 : 138;
        }
        if (function_exists('wpt_pro_exists') && get_option('jd_individual_twitter_users') == 1) {
            $first = '#authors';
        } else {
            if (function_exists('wpt_pro_exists')) {
                $first = '#custom';
            } else {
                $first = '#notes';
            }
        }
        // yuck, this is ugly. I should do something about it. Like localize, man.
        echo "\r\n<script type='text/javascript'>\r\n\tjQuery(document).ready(function(\$){\t\r\n\t\t\$('#jtw').charCount( { allowed: {$allowed}, counterText: '" . __('Characters left: ', 'wp-to-twitter') . "' } );\r\n\t});\r\n\tjQuery(document).ready(function(\$){\r\n\t\t\$('#side-sortables .tabs a[href=\"{$first}\"]').addClass('active');\r\n\t\t\$('#side-sortables .wptab').not('{$first}').hide();\r\n\t\t\$('#side-sortables .tabs a').on('click',function(e) {\r\n\t\t\te.preventDefault();\r\n\t\t\t\$('#side-sortables .tabs a').removeClass('active');\r\n\t\t\t\$(this).addClass('active');\r\n\t\t\tvar target = \$(this).attr('href');\r\n\t\t\t\$('#side-sortables .wptab').not(target).hide();\r\n\t\t\t\$(target).show();\r\n\t\t});\r\n\t});\r\n</script>\r\n<style type='text/css'>\r\n#wp2t h3 span { padding-left: 30px; background: url(" . plugins_url('wp-to-twitter/images/twitter-bird-light-bgs.png') . ") left 50% no-repeat; }\r\n</style>";
    }
}
Beispiel #3
0
function jd_truncate_tweet($tweet, $post, $post_ID, $retweet = false, $ref = false)
{
    // media file occupies 22 characters, need to account for in shortening.
    $maxlength = apply_filters('wpt_max_length', array('with_media' => 116, 'without_media' => 139));
    $length = wpt_post_with_media($post_ID, $post) ? $maxlength['with_media'] : $maxlength['without_media'];
    $tweet = apply_filters('wpt_tweet_sentence', $tweet, $post_ID);
    $tweet = trim(wpt_custom_shortcodes($tweet, $post_ID));
    $encoding = get_option('blog_charset') != 'UTF-8' && get_option('blog_charset') != '' ? get_option('blog_charset') : 'UTF-8';
    $diff = 0;
    // Add custom append/prepend fields to Tweet text
    if (get_option('jd_twit_prepend') != "" && $tweet != '') {
        $tweet = stripslashes(get_option('jd_twit_prepend')) . " " . $tweet;
    }
    if (get_option('jd_twit_append') != "" && $tweet != '') {
        $tweet = $tweet . " " . stripslashes(get_option('jd_twit_append'));
    }
    // there are no tags in this Tweet. Truncate and return.
    if (!wpt_has_tags($tweet)) {
        $post_tweet = mb_substr($tweet, 0, $length, $encoding);
        return apply_filters('wpt_custom_truncate', $post_tweet, $tweet, $post_ID, $retweet, 1);
    }
    // create full unconditional post tweet - prior to truncation
    // order matters; arrays have to be ordered the same way.
    $tags = array_map('wpt_make_tag', wpt_tags());
    $values = wpt_create_values($post, $post_ID, $ref);
    $post_tweet = str_ireplace($tags, $values, $tweet);
    // check total length
    $str_length = mb_strlen(urldecode(wpt_normalize($post_tweet)), $encoding);
    /**
     * Check whether completed replacement is still within allowed length.
     *
     * If so, post as is.
     */
    if ($str_length < $length + 1) {
        if (mb_strlen(wpt_normalize($post_tweet)) > $length + 1) {
            $post_tweet = mb_substr($post_tweet, 0, $length, $encoding);
        }
        return apply_filters('wpt_custom_truncate', $post_tweet, $tweet, $post_ID, $retweet, 2);
        // return early if all is well without replacements.
    } else {
        $has_excerpt_tag = wpt_has($tweet, '#post#');
        $has_title_tag = wpt_has($tweet, '#title#');
        $has_short_url = wpt_has($tweet, '#url#');
        $has_long_url = wpt_has($tweet, '#longurl#');
        $url_strlen = mb_strlen(urldecode(wpt_normalize($values['url'])), $encoding);
        $longurl_strlen = mb_strlen(urldecode(wpt_normalize($values['longurl'])), $encoding);
        /**
         * Tweet is too long, so we'll have to truncate that sucker.
         */
        $length_array = wpt_length_array($values, $encoding);
        // Twitter's t.co shortener is mandatory. All URLS are max-character value set by Twitter.
        $tco = wpt_is_ssl($values['url']) ? 23 : 22;
        $order = get_option('wpt_truncation_order');
        if (is_array($order)) {
            asort($order);
            $preferred = array();
            foreach ($order as $k => $v) {
                if ($k == 'excerpt') {
                    $k = 'post';
                    $value = $length_array['post'];
                } else {
                    if ($k == 'blogname') {
                        $k = 'blog';
                        $value = $length_array['blog'];
                    } else {
                        $value = $length_array[$k];
                    }
                }
                $preferred[$k] = $value;
            }
        } else {
            $preferred = $length_array;
        }
        if ($has_short_url) {
            $diff = $url_strlen - $tco > 0 ? $url_strlen - $tco : 0;
        } else {
            if ($has_long_url) {
                $diff = $longurl_strlen - $tco > 0 ? $longurl_strlen - $tco : 0;
            }
        }
        if ($str_length > $length + 1 + $diff) {
            foreach ($preferred as $key => $value) {
                // don't truncate content of post excerpt or title if those tags not in use
                if (!($key == 'excerpt' && !$has_excerpt_tag) && !($key == 'title' && !$has_title_tag)) {
                    $str_length = mb_strlen(urldecode(wpt_normalize(trim($post_tweet))), $encoding);
                    if ($str_length > $length + 1 + $diff) {
                        $trim = $str_length - ($length + 1 + $diff);
                        $old_value = $values[$key];
                        // prevent URL from being modified
                        $post_tweet = str_ireplace(array($values['url'], $values['longurl']), array('#url#', '#longurl#'), $post_tweet);
                        /**
                         * These tag fields should be removed completely, rather than truncated. 
                         */
                        if (wpt_remove_tag($key)) {
                            $new_value = '';
                            /**
                             * These tag fields should have stray characters removed on word boundaries
                             */
                        } else {
                            if ($key == 'tags') {
                                // remove any stray hash characters due to string truncation
                                if (mb_strlen($old_value) - $trim <= 2) {
                                    $new_value = '';
                                } else {
                                    $new_value = $old_value;
                                    while (mb_strlen($old_value) - $trim < mb_strlen($new_value)) {
                                        $new_value = trim(mb_substr($new_value, 0, mb_strrpos($new_value, '#', $encoding) - 1));
                                    }
                                }
                                /**
                                 * Just flat out truncate everything else cold. 
                                 */
                            } else {
                                // trim letters
                                $new_value = mb_substr($old_value, 0, -$trim, $encoding);
                                // trim rest of last word
                                $last_space = strrpos($new_value, ' ');
                                $new_value = mb_substr($new_value, 0, $last_space, $encoding);
                                /**
                                 * If you want to add something like an ellipsis after truncation, use this filter.
                                 */
                                $new_value = apply_filters('wpt_filter_truncated_value', $new_value, $key, $old_value);
                            }
                        }
                        $post_tweet = str_ireplace($old_value, $new_value, $post_tweet);
                        // put URL back before checking length
                        $post_tweet = str_ireplace(array('#url#', '#longurl#'), array($values['url'], $values['longurl']), $post_tweet);
                    } else {
                        if (mb_strlen(wpt_normalize($post_tweet), $encoding) > $length + 1 + $diff) {
                            $post_tweet = mb_substr($post_tweet, 0, $length + $diff, $encoding);
                        }
                    }
                }
            }
        }
        // this is needed in case a tweet needs to be truncated outright and the truncation values aren't in the above.
        // 1) removes URL 2) checks length of remainder 3) Replaces URL
        if (mb_strlen(wpt_normalize($post_tweet)) > $length + 1) {
            $tweet = false;
            if ($has_short_url) {
                $url = $values['url'];
                $tag = '#url#';
            } else {
                if ($has_long_url) {
                    $url = $values['longurl'];
                    $tag = '#longurl#';
                } else {
                    $post_tweet = mb_substr($post_tweet, 0, $length + $diff, $encoding);
                    $tweet = true;
                }
            }
            if (!$tweet) {
                $temp = str_ireplace($url, $tag, $post_tweet);
                if (mb_strlen(wpt_normalize($temp)) > $length + 1 - ($tco - strlen($tag)) && $temp != $post_tweet) {
                    if (stripos($temp, '#url#') === false && stripos($temp, '#longurl#') === false) {
                        $post_tweet = trim(mb_substr($temp, 0, $length, $encoding));
                    } else {
                        $post_tweet = trim(mb_substr($temp, 0, $length - $tco - 1, $encoding));
                    }
                    // it's possible to trim off the #url# part in this process. If that happens, put it back.
                    $sub_sentence = !wpt_has($post_tweet, $tag) && ($has_short_url || $has_long_url) ? $post_tweet . ' ' . $tag : $post_tweet;
                    $post_tweet = str_ireplace($tag, $url, $sub_sentence);
                }
            }
        }
    }
    return apply_filters('wpt_custom_truncate', $post_tweet, $tweet, $post_ID, $retweet, 3);
}