loco-translate
Last commit date
languages
8 years ago
lib
8 years ago
pub
8 years ago
src
8 years ago
tpl
8 years ago
loco.php
8 years ago
loco.xml
8 years ago
readme.txt
8 years ago
loco.php
174 lines
| 1 | <?php |
| 2 | /* |
| 3 | Plugin Name: Loco Translate |
| 4 | Plugin URI: https://wordpress.org/plugins/loco-translate/ |
| 5 | Description: Translate themes and plugins directly in WordPress |
| 6 | Author: Tim Whitlock |
| 7 | Version: 2.1.0 |
| 8 | Author URI: https://localise.biz/wordpress/plugin |
| 9 | Text Domain: loco-translate |
| 10 | Domain Path: /languages/ |
| 11 | */ |
| 12 | |
| 13 | // disallow execution out of context |
| 14 | if( ! function_exists('is_admin') ){ |
| 15 | return; |
| 16 | } |
| 17 | |
| 18 | |
| 19 | // legacy plugin should not be installed at the same time |
| 20 | if( function_exists('loco_require') ){ |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | |
| 25 | /** |
| 26 | * Get absolute path to Loco primary plugin file |
| 27 | * @return string |
| 28 | */ |
| 29 | function loco_plugin_file(){ |
| 30 | return __FILE__; |
| 31 | } |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * Get version of this plugin |
| 36 | * @return string |
| 37 | */ |
| 38 | function loco_plugin_version(){ |
| 39 | return '2.1.0'; |
| 40 | } |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * Get Loco plugin handle, used by WordPress to identify plugin as a relative path |
| 45 | * @return string |
| 46 | */ |
| 47 | function loco_plugin_self(){ |
| 48 | static $handle; |
| 49 | isset($handle) or $handle = plugin_basename(__FILE__); |
| 50 | return $handle; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | /** |
| 55 | * Get absolute path to plugin root directory |
| 56 | * @return string |
| 57 | */ |
| 58 | function loco_plugin_root(){ |
| 59 | static $root; |
| 60 | isset($root) or $root = dirname(__FILE__); |
| 61 | return $root; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * Check whether currently running in debug mode |
| 67 | * @return bool |
| 68 | */ |
| 69 | function loco_debugging(){ |
| 70 | return apply_filters('loco_debug', WP_DEBUG ); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /** |
| 75 | * Whether currently processing an Ajax request |
| 76 | * @return bool |
| 77 | */ |
| 78 | function loco_doing_ajax(){ |
| 79 | return defined('DOING_AJAX') && DOING_AJAX; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /** |
| 84 | * Evaluate a constant by name |
| 85 | * @return mixed |
| 86 | */ |
| 87 | function loco_constant( $name ){ |
| 88 | $value = defined($name) ? constant($name) : null; |
| 89 | // constant values will only be modified in tests |
| 90 | if( defined('LOCO_TEST') && LOCO_TEST ){ |
| 91 | $value = apply_filters('loco_constant', $value, $name ); |
| 92 | $value = apply_filters('loco_constant_'.$name, $value ); |
| 93 | } |
| 94 | return $value; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | /** |
| 99 | * Runtime inclusion of any file under plugin root |
| 100 | * @return mixed |
| 101 | */ |
| 102 | function loco_include( $relpath ){ |
| 103 | $path = loco_plugin_root().'/'.$relpath; |
| 104 | if( ! file_exists($path) ){ |
| 105 | throw new Loco_error_Exception('File not found: '.$path); |
| 106 | } |
| 107 | return include $path; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | /** |
| 112 | * Require dependant library once only |
| 113 | * @return void |
| 114 | */ |
| 115 | function loco_require_lib( $path ){ |
| 116 | require_once loco_plugin_root().'/lib/'.$path; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /** |
| 121 | * Check PHP extension required by Loco and load polyfill if needed |
| 122 | * @return bool |
| 123 | */ |
| 124 | function loco_check_extension( $name ){ |
| 125 | static $cache = array(); |
| 126 | if( ! isset($cache[$name]) ){ |
| 127 | if( extension_loaded($name) ){ |
| 128 | $cache[$name] = true; |
| 129 | } |
| 130 | else { |
| 131 | Loco_error_AdminNotices::warn( sprintf( __('Loco requires the "%s" PHP extension. Ask your hosting provider to install it','loco-translate'), $name ) ); |
| 132 | $class = 'Loco_compat_'.ucfirst($name).'Extension.php'; |
| 133 | $cache[$name] = class_exists( $class ); |
| 134 | } |
| 135 | } |
| 136 | return $cache[$name]; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | /** |
| 141 | * Class autoloader for Loco classes under src directory. |
| 142 | * e.g. class "Loco_foo_FooBar" wil be found in "src/foo/FooBar.php" |
| 143 | * Also does autoload for polyfills under "src/compat" if classname < 20 chars |
| 144 | * @return void |
| 145 | */ |
| 146 | function loco_autoload( $name ){ |
| 147 | if( 'Loco_' === substr($name,0,5) ){ |
| 148 | loco_include( 'src/'.strtr( substr($name,5), '_', '/' ).'.php' ); |
| 149 | } |
| 150 | else if( ! isset($name{20}) && file_exists( $path = loco_plugin_root().'/src/compat/'.$name.'.php') ){ |
| 151 | require $path; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | spl_autoload_register( 'loco_autoload', false ); |
| 156 | |
| 157 | |
| 158 | // provide safe directory for custom translations that won't be deleted during auto-updates |
| 159 | if( ! defined('LOCO_LANG_DIR') ){ |
| 160 | define( 'LOCO_LANG_DIR', rtrim(loco_constant('WP_LANG_DIR'),'/').'/loco' ); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | // text domain loading helper for custom file locations. disable by setting constant empty |
| 165 | if( LOCO_LANG_DIR ){ |
| 166 | new Loco_hooks_LoadHelper; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | // initialize hooks for admin screens |
| 171 | if( is_admin() ){ |
| 172 | new Loco_hooks_AdminHooks; |
| 173 | } |
| 174 |