build
12 years ago
loco-admin.php
12 years ago
loco-boot.php
12 years ago
loco-locales.php
12 years ago
loco-boot.php
266 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Loco | bootstraps plugin when it's needed. |
| 4 | * Top-level Loco class holds some basic utilities |
| 5 | */ |
| 6 | abstract class Loco { |
| 7 | |
| 8 | /** plugin namespace */ |
| 9 | const NS = 'loco-translate'; |
| 10 | |
| 11 | const VERSION = '1.1.3'; |
| 12 | const CAPABILITY = 'manage_options'; |
| 13 | |
| 14 | /* whether to enable APC cache */ |
| 15 | public static $apc_enabled; |
| 16 | |
| 17 | /* call Wordpress __ with our text domain */ |
| 18 | public static function __( $msgid = '' ){ |
| 19 | return __( $msgid, self::NS ); |
| 20 | } |
| 21 | |
| 22 | /* call Wordpress _n with our text domain */ |
| 23 | public static function _n( $msgid = '', $msgid_plural = '', $n = 0 ){ |
| 24 | return _n( $msgid, $msgid_plural, $n, self::NS ); |
| 25 | } |
| 26 | |
| 27 | /* call Wordpress _x with our text domain */ |
| 28 | public static function _x( $msgid = '', $msgctxt = '', $n = 0 ){ |
| 29 | return _x( $msgid, $msgctxt, self::NS ); |
| 30 | } |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * Bootstrap localisation of self |
| 35 | */ |
| 36 | public static function load_textdomain(){ |
| 37 | $locale = get_locale(); |
| 38 | if( 0 === strpos($locale,'en') ){ |
| 39 | return; |
| 40 | } |
| 41 | // see if MO file exists with exact name |
| 42 | $mopath = loco_basedir().'/languages/'.Loco::NS.'-'.$locale.'.mo'; |
| 43 | if( ! file_exists($mopath) ){ |
| 44 | // Try some locale sanitization to find suitable MO file. |
| 45 | function_exists('loco_locale_resolve') or loco_require('loco-locales'); |
| 46 | $locale = loco_locale_resolve($locale)->get_code(); |
| 47 | $mopath = loco_basedir().'/languages/'.Loco::NS.'-'.$locale.'.mo'; |
| 48 | if( ! file_exists($mopath) ){ |
| 49 | // translations really not found - check PO is compiled to MO |
| 50 | return; |
| 51 | } |
| 52 | } |
| 53 | load_textdomain( Loco::NS, $mopath ); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /** |
| 58 | * Get plugin local base directory in case __DIR__ isn't available (php<5.3) |
| 59 | */ |
| 60 | public static function basedir(){ |
| 61 | static $dir; |
| 62 | isset($dir) or $dir = dirname(__FILE__); |
| 63 | return $dir; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | /** |
| 68 | * Get plugin base URL path. |
| 69 | */ |
| 70 | public static function baseurl(){ |
| 71 | static $url; |
| 72 | if( ! isset($url) ){ |
| 73 | $here = __FILE__; |
| 74 | if( 0 !== strpos( WP_PLUGIN_DIR, $here ) ){ |
| 75 | // something along this path has been symlinked into the document tree |
| 76 | // temporary measure assumes name of plugin folder is unchanged. |
| 77 | $here = WP_PLUGIN_DIR.'/'.Loco::NS.'/loco.php'; |
| 78 | } |
| 79 | $url = plugins_url( '', $here ); |
| 80 | } |
| 81 | return $url; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /** |
| 86 | * Simple template renderer |
| 87 | */ |
| 88 | public static function render( $tpl, array $arguments = array() ){ |
| 89 | extract( $arguments ); |
| 90 | include loco_basedir().'/tpl/'.$tpl.'.tpl.php'; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | /** |
| 95 | * replacement for bloated esc_html function |
| 96 | */ |
| 97 | public static function html( $text ){ |
| 98 | return htmlspecialchars( $text, ENT_COMPAT, 'UTF-8' ); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | /** |
| 103 | * html output printer with printf built-in |
| 104 | */ |
| 105 | public static function h( $text, $_ = null ){ |
| 106 | if( isset($_) ){ |
| 107 | $args = func_get_args(); |
| 108 | $text = call_user_func_array('sprintf', $args ); |
| 109 | } |
| 110 | echo self::html( $text ); |
| 111 | return ''; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /** |
| 116 | * Abstract enquement of JavaScript |
| 117 | */ |
| 118 | public static function enqueue_scripts(){ |
| 119 | static $v, $i = 0; |
| 120 | $stubs = func_get_args(); |
| 121 | if( ! isset($v) ){ |
| 122 | $v = WP_DEBUG ? time() : Loco::VERSION; |
| 123 | // @todo enqueue JavaScript translations here |
| 124 | } |
| 125 | foreach( $stubs as $stub ){ |
| 126 | $js = Loco::baseurl().'/pub/js/'.$stub.'.js'; |
| 127 | $id = self::NS.'-js-'.( ++$i ); |
| 128 | wp_enqueue_script( $id, $js, array('jquery'), $v, true ); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | |
| 133 | |
| 134 | /** |
| 135 | * Abstract enquement of Stylesheets |
| 136 | */ |
| 137 | public static function enqueue_styles(){ |
| 138 | static $v, $i = 0; |
| 139 | isset($v) or $v = WP_DEBUG ? time() : Loco::VERSION; |
| 140 | foreach( func_get_args() as $stub ){ |
| 141 | $css = Loco::baseurl().'/pub/css/'.$stub.'.css'; |
| 142 | wp_enqueue_style( self::NS.'-css-'.(++$i), $css, array(), $v ); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | |
| 147 | |
| 148 | /** |
| 149 | * |
| 150 | */ |
| 151 | public static function utm_query( $utm_medium = 'wp', $utm_campaign = 'wp' ){ |
| 152 | static $utm_source, $utm_content; |
| 153 | if( ! isset($utm_source) ){ |
| 154 | $utm_source = parse_url( get_bloginfo('url'), PHP_URL_HOST ) or $utm_source = $_SERVER['HTTP_HOST']; |
| 155 | $utm_content = Loco::NS.'-'.Loco::VERSION; |
| 156 | } |
| 157 | return http_build_query( compact('utm_campaign','utm_medium','utm_content','utm_source') ); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | |
| 162 | /** |
| 163 | * Get actual postdata, not hacked postdata Wordpress ruined with wp_magic_quotes |
| 164 | * @return array |
| 165 | */ |
| 166 | public static function postdata(){ |
| 167 | static $post; |
| 168 | if( ! is_array($post) ){ |
| 169 | // Not using Wordpress's hacked POST collection. |
| 170 | $str = file_get_contents('php://input') or |
| 171 | // preferred way is to parse original data |
| 172 | $str = isset($_SERVER['HTTP_RAW_POST_DATA']) ? $_SERVER['HTTP_RAW_POST_DATA'] : ''; |
| 173 | if( $str ){ |
| 174 | parse_str( $str, $post ); |
| 175 | } |
| 176 | // fall back to undoing Wordpress 'magic' |
| 177 | else { |
| 178 | $post = stripslashes_deep( $_POST ); |
| 179 | } |
| 180 | } |
| 181 | return $post; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | |
| 186 | /** |
| 187 | * Abstraction of cache retrieval, using apc where possible |
| 188 | * @return mixed |
| 189 | */ |
| 190 | public static function cached( $key ){ |
| 191 | $key = self::cache_key($key); |
| 192 | if( self::$apc_enabled ){ |
| 193 | return apc_fetch( $key ); |
| 194 | } |
| 195 | return get_transient( $key ); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | |
| 200 | /** |
| 201 | * Abstraction of cache storage, using apc where possible |
| 202 | * @return void |
| 203 | */ |
| 204 | public static function cache( $key, $value, $ttl = 0 ){ |
| 205 | $key = self::cache_key($key); |
| 206 | if( self::$apc_enabled ){ |
| 207 | apc_store( $key, $value, $ttl ); |
| 208 | return; |
| 209 | } |
| 210 | if( ! $ttl ){ |
| 211 | // WP would expire immediately as opposed to never |
| 212 | $ttl = 31536000; |
| 213 | } |
| 214 | set_transient( $key, $value, $ttl ); |
| 215 | } |
| 216 | |
| 217 | |
| 218 | |
| 219 | /** |
| 220 | * Sanitize a cache key |
| 221 | */ |
| 222 | private static function cache_key( $key ){ |
| 223 | $key = 'loco_'.preg_replace('/[^a-z]+/','_', strtolower($key) ); |
| 224 | if( isset($key{45}) ){ |
| 225 | $key = 'loco_'.md5($key); |
| 226 | } |
| 227 | return $key; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | /** |
| 232 | * Plugin option getter/setter |
| 233 | */ |
| 234 | public static function config( array $update = array() ){ |
| 235 | static $conf; |
| 236 | if( ! isset($conf) ){ |
| 237 | $conf = array ( |
| 238 | 'which_msgfmt' => '/usr/bin/msgfmt', |
| 239 | ); |
| 240 | foreach( $conf as $key => $val ){ |
| 241 | $conf[$key] = get_option( Loco::NS.'-'.$key); |
| 242 | if( empty($conf[$key]) && ! is_string($conf[$key]) ){ |
| 243 | $conf[$key] = $val; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | foreach( $update as $key => $val ){ |
| 248 | if( isset($conf[$key]) ){ |
| 249 | update_option( Loco::NS.'-'.$key, $val ); |
| 250 | $conf[$key] = $val; |
| 251 | } |
| 252 | } |
| 253 | return $conf; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | // minimum config |
| 261 | Loco::$apc_enabled = function_exists('apc_fetch') && ini_get('apc.enabled'); |
| 262 | Loco::load_textdomain(); |
| 263 | |
| 264 | |
| 265 | |
| 266 |