loco-translate
Last commit date
languages
1 year ago
lib
1 year ago
pub
1 year ago
src
1 year ago
tpl
1 year ago
loco.php
1 year ago
loco.xml
1 year ago
readme.txt
1 year ago
loco.php
194 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.8.0 |
| 8 | Requires at least: 6.6 |
| 9 | Requires PHP: 7.4 |
| 10 | Tested up to: 6.8.1 |
| 11 | Author URI: https://localise.biz/wordpress/plugin |
| 12 | Text Domain: loco-translate |
| 13 | Domain Path: /languages/ |
| 14 | */ |
| 15 | |
| 16 | // disallow execution out of context |
| 17 | if( ! function_exists('is_admin') ){ |
| 18 | return; |
| 19 | } |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * Get absolute path to Loco primary plugin file |
| 24 | */ |
| 25 | function loco_plugin_file(): string { |
| 26 | return __FILE__; |
| 27 | } |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Get version of this plugin |
| 32 | */ |
| 33 | function loco_plugin_version(): string { |
| 34 | return '2.8.0'; |
| 35 | } |
| 36 | |
| 37 | |
| 38 | /** |
| 39 | * Get Loco plugin handle, used by WordPress to identify plugin as a relative path |
| 40 | * @return string probably "loco-translate/loco.php" |
| 41 | */ |
| 42 | function loco_plugin_self(): string { |
| 43 | static $handle; |
| 44 | isset($handle) or $handle = plugin_basename(__FILE__); |
| 45 | return $handle; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * Get absolute path to plugin root directory |
| 51 | */ |
| 52 | function loco_plugin_root(): string { |
| 53 | return __DIR__; |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /** |
| 58 | * Check whether currently running in debug mode |
| 59 | */ |
| 60 | function loco_debugging(): bool { |
| 61 | return apply_filters('loco_debug', WP_DEBUG ); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * Whether currently processing an Ajax request |
| 67 | */ |
| 68 | function loco_doing_ajax(): bool { |
| 69 | return defined('DOING_AJAX') && DOING_AJAX; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | if( ! function_exists('loco_constant') ) { |
| 74 | /** |
| 75 | * Evaluate a constant by name |
| 76 | * @return mixed |
| 77 | */ |
| 78 | function loco_constant( string $name ) { |
| 79 | return defined($name) ? constant($name) : null; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /** |
| 85 | * Runtime inclusion of any file under plugin root |
| 86 | * |
| 87 | * @param string $relpath PHP file path relative to __DIR__ |
| 88 | * @return mixed return value from included file |
| 89 | */ |
| 90 | function loco_include( string $relpath ){ |
| 91 | $path = loco_plugin_root().'/'.$relpath; |
| 92 | if( ! file_exists($path) ){ |
| 93 | $message = 'File not found: '.$path; |
| 94 | // debug specifics to error log in case full call stack not visible |
| 95 | if( 'cli' !== PHP_SAPI ) { |
| 96 | error_log( sprintf( '[Loco.debug] Failed on loco_include(%s). !file_exists(%s)', var_export($relpath,true), var_export($path,true) ) ); |
| 97 | } |
| 98 | // handle circular file inclusion error if error class not found |
| 99 | if( loco_class_exists('Loco_error_Exception') ){ |
| 100 | throw new Loco_error_Exception($message); |
| 101 | } |
| 102 | else { |
| 103 | throw new Exception($message.'; additionally src/error/Exception.php not loadable'); |
| 104 | } |
| 105 | } |
| 106 | return include $path; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | /** |
| 111 | * Require dependant library once only |
| 112 | |
| 113 | * @param string $path PHP file path relative to ./lib |
| 114 | */ |
| 115 | function loco_require_lib( string $path ):void { |
| 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 | */ |
| 123 | function loco_check_extension( string $name ): bool { |
| 124 | static $cache = []; |
| 125 | if( ! array_key_exists($name,$cache) ) { |
| 126 | if( extension_loaded($name) ){ |
| 127 | $cache[$name] = true; |
| 128 | } |
| 129 | else { |
| 130 | // translators: %s refers to the name of a missing PHP extension, for example "mbstring". |
| 131 | Loco_error_AdminNotices::warn( sprintf( __('Loco Translate requires the "%s" PHP extension. Ask your hosting provider to install it','loco-translate'), $name ) ); |
| 132 | class_exists( ucfirst($name).'Extension' ); // <- pings Loco_hooks_AdminHooks::autoload_compat |
| 133 | $cache[$name] = false; |
| 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_Bar" will be found in "src/foo/Bar.php" |
| 143 | * |
| 144 | * @internal |
| 145 | */ |
| 146 | function loco_autoload( string $name ):void { |
| 147 | if( 'Loco_' === substr($name,0,5) ){ |
| 148 | loco_include( 'src/'.strtr( substr($name,5), '_', '/' ).'.php' ); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | |
| 153 | /** |
| 154 | * class_exists wrapper that fails silently. |
| 155 | */ |
| 156 | function loco_class_exists( string $class ): bool { |
| 157 | try { |
| 158 | return class_exists($class); |
| 159 | } |
| 160 | catch( Throwable $e ){ |
| 161 | return false; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | |
| 166 | // Startup errors will raise notices. Check your error logs if error reporting is quiet |
| 167 | try { |
| 168 | spl_autoload_register('loco_autoload'); |
| 169 | |
| 170 | // provide safe directory for custom translations that won't be deleted during auto-updates |
| 171 | if ( ! defined( 'LOCO_LANG_DIR' ) ) { |
| 172 | define( 'LOCO_LANG_DIR', trailingslashit( loco_constant('WP_LANG_DIR') ) . 'loco' ); |
| 173 | } |
| 174 | |
| 175 | // text domain loading helper for custom file locations. Set constant empty to disable |
| 176 | if( LOCO_LANG_DIR ){ |
| 177 | new Loco_hooks_LoadHelper; |
| 178 | } |
| 179 | |
| 180 | // initialize hooks for admin screens |
| 181 | if ( is_admin() ) { |
| 182 | new Loco_hooks_AdminHooks; |
| 183 | } |
| 184 | |
| 185 | // enable wp cli commands |
| 186 | if( class_exists('WP_CLI',false) ) { |
| 187 | WP_CLI::add_command('loco','Loco_cli_Commands'); |
| 188 | } |
| 189 | |
| 190 | } |
| 191 | catch( Throwable $e ){ |
| 192 | trigger_error(sprintf('[Loco.fatal] %s in %s:%u',$e->getMessage(), $e->getFile(), $e->getLine() ) ); |
| 193 | } |
| 194 |