Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 80f329bca2
Fetching contributors…

Cannot retrieve contributors at this time

file 239 lines (217 sloc) 6.3 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
<?php # -*- coding: utf-8 -*-
declare ( encoding = 'UTF-8' );
/**
* Plugin Name: T5 Opera Speed Dial Preview
* Plugin URI: https://github.com/toscho/T5-Opera-Speed-Dial-Preview
* Description: Your latest posts and comments in Opera’s Speed Dial preview.
* Version: 2012.03.05
* Required: 3.3
* Author: Thomas Scholz <info@toscho.de>
* Author URI: http://toscho.de
* License: MIT
* License URI: http://www.opensource.org/licenses/mit-license.php
*
* Copyright (c) 2012 Thomas Scholz
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

// Not a WordPress context? Stop.
! defined( 'ABSPATH' ) and exit;

// Wait until all needed functions are loaded.
add_action( 'plugins_loaded', array ( 'T5_Opera_Speed_Dial', 'init' ) );
add_action( 'init', array ( 'T5_Opera_Speed_Dial', 'set_rewrite_rule' ) );

add_filter(
'plugin_row_meta',
array( 'T5_Opera_Speed_Dial', 'add_feedback_link' ),
10,
2
);

register_activation_hook(
__FILE__,
array ( 'T5_Opera_Speed_Dial', 'set_rewrite_rule' )
);
/**
* Creates a dedicated speed dial page.
*
* @author Thomas Scholz, <info@toscho.de>
* @link http://dev.opera.com/articles/view/opera-speed-dial-enhancements/
*
*/
class T5_Opera_Speed_Dial
{
/**
* Internal handler for WordPress
*
* @type string
*/
protected static $query_var = 'speeddial';

/**
* Creates a new instance.
*
* @wp-hook plugins_loaded
* @see __construct()
* @return void
*/
public static function init()
{
new self;
}

/**
* Set actions, filters and basic variables, load language.
*/
public function __construct()
{

add_filter( 'query_vars', array ( $this, 'add_query_var' ) );
// Hook in late to allow other plugins to operate earlier.
add_action( 'template_redirect', array ( $this, 'render' ), 100 );
}

/**
* Redirect to speed dial page if it is a preview request.
*
* @wp-hook template_redirect
* @return void
*/
public function redirect()
{
isset ( $_SERVER['HTTP_X_PURPOSE'] )
and 'preview' == $_SERVER['HTTP_X_PURPOSE']
and ! get_query_var( self::$query_var )
and wp_redirect( home_url( self::$query_var ) )
and exit;
}

/**
* Register an URI.
*
* @wp-hook activation
* @return void
*/
public static function set_rewrite_rule()
{
add_rewrite_rule(
'^' . self::$query_var . '$',
'index.php?' . self::$query_var . '=1',
'top'
);
'init' != current_filter() and flush_rewrite_rules();
}

/**
* Register our query var.
*
* @wp-hook query_vars
* @param array $vars Existing query vars
* @return array
*/
public function add_query_var( $vars )
{
$vars[] = self::$query_var;
return $vars;
}

/**
* Redirect to speed dial page or print it out if we are already there.
*
* @wp-hook template_redirect
* @return void
*/
public function render()
{
self::set_rewrite_rule();
if ( ! get_query_var( self::$query_var ) )
{
$this->redirect();
return;
}

$template = 'speed-dial.php';
$path = locate_template( array ( $template ) );

if ( '' != $path and file_exists( $path ) )
{
require $path;
}
else
{
require dirname( __FILE__ ) . "/$template";
}
exit;
}

/**
* Shortens an UTF-8 encoded string without breaking words.
* Template helper.
*
* @link http://wordpress.stackexchange.com/q/11085/11089#11089
* @param string $string String to shorten.
* @param int $max_chars Maximal length in characters.
* @param string $append Replacement for truncated words.
* @return string
*/
public static function utf8_truncate(
$string,
$max_chars = 25,
$append = "\xC2\xA0…"
)
{
$string = strip_tags( $string );
$string = html_entity_decode( $string, ENT_QUOTES, 'utf-8' );
// \xC2\xA0 is the no-break space
$string = trim( $string, "\n\r\t .-;–,—\xC2\xA0" );
$length = strlen( utf8_decode( $string ) );

// Nothing to do.
if ( $length < $max_chars )
{
return $string;
}

// mb_substr() is in /wp-includes/compat.php as a fallback if
// your the current PHP installation is missing it.
$string = mb_substr( $string, 0, $max_chars, 'utf-8' );

// No white space. One long word or chinese/korean/japanese text.
if ( FALSE === strpos( $string, ' ' ) )
{
return $string . $append;
}

// Avoid breaks within words. Find the last white space.
if ( extension_loaded( 'mbstring' ) )
{
$pos = mb_strrpos( $string, ' ', 'utf-8' );
$short = mb_substr( $string, 0, $pos, 'utf-8' );
}
else
{
// Workaround. May be slow on long strings.
$words = explode( ' ', $string );
// Drop the last word.
array_pop( $words );
$short = implode( ' ', $words );
}

return $short . $append;
}

/**
* Adds a link to the GitHub bug tracker.
*
* @param array $links Already existing links.
* @return string
*/
public static function add_feedback_link( $links, $file )
{
static $base_name = '';
'' == $base_name and $base_name = plugin_basename( __FILE__ );

if ( $base_name == $file )
{
$links[] = "<a href='https://github.com/toscho/T5-Opera-Speed-Dial-Preview/issues'>Send feedback</a>";
}

return $links;
}
}
Something went wrong with that request. Please try again.