first commit

This commit is contained in:
2024-07-15 11:28:08 +02:00
commit f52d538ea5
21891 changed files with 6161164 additions and 0 deletions

View File

@@ -0,0 +1,469 @@
<?php
/**
* Represents a WordPress locale
*
* @property string $lang
* @property string $region
* @property string $variant
*/
class Loco_Locale implements JsonSerializable {
/**
* Language subtags
* @var array
*/
private $tag;
/**
* Cached composite tag
* @var string
*/
private $_tag;
/**
* Cached icon css class
* @var string
*/
private $icon;
/**
* Name in English
* @var string
*/
private $name;
/**
* Name in language of self
* @var string
*/
private $_name;
/**
* Cache of raw plural data
* @var array
*/
private $plurals;
/**
* Validity cache
* @var bool
*/
private $valid;
/**
* @return Loco_Locale
*/
public static function parse( $tag ){
$locale = new Loco_Locale('');
try {
$locale->setSubtags( loco_parse_wp_locale($tag) );
}
catch( Exception $e ){
// isValid should return false
}
do_action( 'loco_parse_locale', $locale, $tag );
return $locale;
}
/**
* Construct from subtags NOT from composite tag. See self::parse
* Note that this skips normalization and validation steps
*/
public function __construct( $lang = '', $region = '', $variant = '' ){
$this->tag = compact('lang','region','variant');
}
/**
* @internal
* Allow read access to subtags
*/
public function __get( $t ){
return isset($this->tag[$t]) ? $this->tag[$t] : '';
}
/**
* @internal
* Allow write access to subtags
*/
public function __set( $t, $s ){
if( isset($this->tag[$t]) ){
$this->tag[$t] = $s;
$this->setSubtags( $this->tag );
}
}
/**
* Set subtags as produced from loco_parse_wp_locale
* @return Loco_Locale
*/
public function setSubtags( array $tag ){
$this->valid = false;
$default = array( 'lang' => '', 'region' => '', 'variant' => '' );
// disallow setting of unsupported tags
if( $bad = array_diff_key($tag, $default) ){
throw new Loco_error_LocaleException('Unsupported subtags: '.implode(',',$bad) );
}
$tag += $default;
// language tag is minimum requirement
if( ! $tag['lang'] ){
throw new Loco_error_LocaleException('Locale must have a language');
}
// no UN codes in Wordpress
if( is_numeric($tag['region']) ){
throw new Loco_error_LocaleException('Numeric regions not supported');
}
// single, scalar variant. Only using for Formal german currently.
if( is_array($tag['variant']) ){
$tag['variant'] = implode('_',$tag['variant']);
}
// normalize case
$tag['lang'] = strtolower($tag['lang']);
$tag['region'] = strtoupper($tag['region']);
$tag['variant'] = strtolower($tag['variant']);
// set subtags and invalidate cache of language tag
$this->tag = $tag;
$this->_tag = null;
$this->icon = null;
$this->valid = true;
return $this;
}
/**
* @return Loco_Locale
*/
public function normalize(){
try {
$this->setSubtags( $this->tag );
}
catch( Loco_error_LocaleException $e ){
$this->_tag = '';
$this->icon = null;
$this->name = 'Invalid locale';
$this->_name = null;
}
return $this;
}
/**
* @return string
*/
public function __toString(){
$str = $this->_tag;
if( is_null($str) ){
$str = implode('_',array_filter($this->tag));
$this->_tag = $str;
}
return $str;
}
/**
* Get stored name in current display language.
* Note that no dynamic translation of English name is performed, but can be altered with loco_parse_locale filter
* @return string | null
*/
public function getName(){
if( $name = $this->name ){
// use canonical native name only when current language matches
// deliberately not matching whole tag such that fr_CA would show native name of fr_FR
if( $_name = $this->getNativeName() ){
$locale = self::parse( function_exists('get_user_locale') ? get_user_locale() : get_locale() );
if( $this->lang === $locale->lang ){
$name = $_name;
}
}
return $name;
}
}
/**
* Get canonical native name as defined by WordPress
* @return string | null
*/
public function getNativeName(){
if( $name = $this->_name ){
return $name;
}
}
/**
* @return string
*/
public function getIcon(){
$icon = $this->icon;
if( is_null($icon) ){
$tag = array();
if( ! $this->tag['lang'] ){
$tag[] = 'lang lang-zxx';
}
foreach( $this->tag as $class => $code ){
if( $code ){
$tag[] = $class.' '.$class.'-'.$code;
}
}
$icon = strtolower( implode(' ',$tag) );
$this->icon = $icon;
}
return $icon;
}
/**
* @return Loco_Locale
*/
public function setIcon( $css ){
if( $css ){
$this->icon = (string) $css;
}
else {
$this->icon = null;
}
return $this;
}
/**
* @return Loco_Locale
*/
public function setName( $english_name, $native_name = '' ){
$this->name = apply_filters('loco_locale_name', $english_name, $native_name );
$this->_name = (string) $native_name;
return $this;
}
/**
* Test whether locale is valid
*/
public function isValid(){
if( is_null($this->valid) ){
$this->normalize();
}
return $this->valid;
}
/**
* Resolve this locale's "official" name from WordPress's translation api
* @return string English name currently set
*/
public function fetchName( Loco_api_WordPressTranslations $api ){
$tag = (string) $this;
// pull from WordPress translations API if network allowed
if( $locale = $api->getLocale($tag) ){
$this->setName( $locale->getName(), $locale->getNativeName() );
}
return $this->getName();
}
/**
* Resolve this locale's name from compiled Loco data
* @return string English name currently set
*/
public function buildName(){
$names = array();
// should at least have a language or not valid
if( $this->isValid() ){
$code = $this->tag['lang'];
$db = Loco_data_CompiledData::get('languages');
if( $name = $db[$code] ){
// if variant is present add only that in brackets (no lookup required)
if( $code = $this->tag['variant'] ){
$name .= ' ('.ucfirst($code).')';
}
// else add region in brackets if present
else if( $code = $this->tag['region'] ){
$db = Loco_data_CompiledData::get('regions');
if( $extra = $db[$code] ){
$name .= ' ('.$extra.')';
}
else {
$name .= ' ('.$code.')';
}
}
$this->setName( $name );
}
}
else {
$this->setName( __('Invalid locale','loco-translate') );
}
return $this->getName();
}
/**
* Ensure locale has a label, even if it has to fall back to language code or error
* @return string
*/
public function ensureName( Loco_api_WordPressTranslations $api ){
$name = $this->getName();
if( ! $name ){
$name = $this->fetchName($api);
// failing that, build own own name from components
if( ! $name ){
$name = $this->buildName();
// last resort, use tag as name
if( ! $name ){
$name = (string) $this;
$this->setName( $name );
}
}
}
return $name;
}
/**
* @return array
*/
public function jsonSerialize(){
$a = $this->tag;
$a['label'] = $this->getName();
// plural data expected by editor
$p = $this->getPluralData();
$a['pluraleq'] = $p[0];
$a['plurals'] = $p[1];
$a['nplurals'] = count($p[1]);
return $a;
}
/**
* Get plural data with translated forms
* @internal
* @return array [ (string) equation, (array) forms ]
*/
public function getPluralData(){
$cache = $this->plurals;
if( ! $cache ){
$lc = $this->lang;
$db = Loco_data_CompiledData::get('plurals');
$id = $lc && isset($db[$lc]) ? $db[$lc] : 0;
$cache = $this->setPlurals( $db[''][$id] );
}
return $cache;
}
/**
* @return int
*/
public function getPluralCount(){
$raw = $this->getPluralData();
return count( $raw[1] );
}
/**
* @return array
*/
private function setPlurals( array $raw ){
$raw = apply_filters( 'loco_locale_plurals', $raw, $this );
// handle languages with no plural forms, where n is always 0
if( ! isset($raw[1][1]) ){
// Translators: Plural category for languages that have no plurals
$raw[1] = array( _x('All forms','Plural category','loco-translate') );
$raw[0] = '0';
}
// else translate all implemented plural forms
// for meaning of categories, see http://cldr.unicode.org/index/cldr-spec/plural-rules
else {
$forms = array(
// Translators: Plural category for zero quantity
'zero' => _x('Zero','Plural category','loco-translate'),
// Translators: Plural category for singular quantity
'one' => _x('One','Plural category','loco-translate'),
// Translators: Plural category used in some multi-plural languages
'two' => _x('Two','Plural category','loco-translate'),
// Translators: Plural category used in some multi-plural languages
'few' => _x('Few','Plural category','loco-translate'),
// Translators: Plural category used in some multi-plural languages
'many' => _x('Many','Plural category','loco-translate'),
// Translators: General plural category not covered by other forms
'other' => _x('Other','Plural category','loco-translate'),
);
foreach( $raw[1] as $k => $v ){
if( isset($forms[$v]) ){
$raw[1][$k] = $forms[$v];
}
}
}
$this->plurals = $raw;
return $raw;
}
/**
* Get PO style Plural-Forms header value comprising number of forms and integer equation for n
* @return string
*/
public function getPluralFormsHeader(){
list( $equation, $forms ) = $this->getPluralData();
return sprintf('nplurals=%u; plural=%s;', count($forms), $equation );
}
/**
* Apply PO style Plural-Forms header.
* @param string e.g. "nplurals=2; plural=n != 1;"
* @return Loco_Locale
*/
public function setPluralFormsHeader( $str ){
if( ! preg_match('/^nplurals=(\\d);\s*plural=([ +\\-\\/*%!=<>|&?:()n0-9]+);?$/', $str, $match ) ){
throw new InvalidArgumentException('Invalid Plural-Forms header, '.json_encode($str) );
}
$cache = $this->getPluralData();
$exprn = $match[2];
// always alter if equation differs
if( $cache[0] !== $exprn ){
$this->plurals[0] = $exprn;
// alter number of forms if changed
$nplurals = max( 1, (int) $match[1] );
if( $nplurals !== count($cache[1]) ){
// named forms must also change, but Plural-Forms cannot contain this information
// as a cheat, we'll assume first form always "one" and last always "other"
for( $i = 1; $i < $nplurals; $i++ ){
$name = 1 === $i ? 'one' : sprintf('Plural %u',$i);
$forms[] = $name;
}
$forms[] = 'other';
$this->setPlurals( array($exprn,$forms) );
}
}
return $this;
}
/**
* @return string
*/
public function exportJson(){
return json_encode( $this->jsonSerialize() );
}
}
// Depends on compiled library
if( ! function_exists('loco_parse_wp_locale') ){
loco_include('lib/compiled/locales.php');
}

View File

@@ -0,0 +1,78 @@
<?php
/**
* @codeCoverageIgnore
*/
class Loco_admin_DebugController extends Loco_mvc_AdminController {
/**
* {@inheritdoc}
*/
public function init(){
parent::init();
$this->set('title','DEBUG');
}
/**
* {@inheritdoc}
*/
public function render(){
// debug package listener
$themes = array();
/* @var $bundle Loco_package_Bundle */
foreach( Loco_package_Listener::singleton()->getThemes() as $bundle ){
$themes[] = array (
'id' => $bundle->getId(),
'name' => $bundle->getName(),
'default' => $bundle->getDefaultProject()->getSlug(),
'count' => count($bundle),
);
}
$this->set('themes', $themes );
$plugins = array();
/* @var $bundle Loco_package_Bundle */
foreach( Loco_package_Listener::singleton()->getPlugins() as $bundle ){
$plugins[] = array (
'id' => $bundle->getId(),
'name' => $bundle->getName(),
'default' => $bundle->getDefaultProject()->getSlug(),
'count' => count($bundle),
);
}
// $this->set( 'plugins', Loco_package_Plugin::get_plugins() );
// $this->set('installed', wp_get_installed_translations('plugins') );
// $this->set('active', get_option( 'active_plugins', array() ) );
// $this->set('langs',get_available_languages());
/*$plugins = get_plugins();
$plugin_info = get_site_transient( 'update_plugins' );
foreach( $plugins as $plugin_file => $plugin_data ){
if ( isset( $plugin_info->response[$plugin_file] ) ) {
$plugins[$plugin_file]['____'] = $plugin_info->response[$plugin_file];
}
}*/
/*/ inspect session and test flash messages
$session = Loco_data_Session::get();
$session->flash( 'success', microtime() );
$this->set('session', $session->getArrayCopy() );
Loco_data_Session::close();*/
// try some notices
Loco_error_AdminNotices::add( new Loco_error_Success('This is a sample success message') );
Loco_error_AdminNotices::add( new Loco_error_Warning('This is a sample warning') );
Loco_error_AdminNotices::add( new Loco_error_Exception('This is a sample error') );
Loco_error_AdminNotices::add( new Loco_error_Debug('This is a sample debug message') );
//*/
return $this->view('admin/debug');
}
}

View File

@@ -0,0 +1,42 @@
<?php
/**
*
*/
class Loco_admin_ErrorController extends Loco_mvc_AdminController {
public function renderError( Exception $e ){
$this->set('error', Loco_error_Exception::convert($e) );
return $this->render();
}
public function render(){
$e = $this->get('error');
if( $e ){
/* @var Loco_error_Exception $e */
$file = Loco_mvc_FileParams::create( new Loco_fs_File( $e->getRealFile() ) );
$file['line'] = $e->getRealLine();
$this->set('file', $file );
if( loco_debugging() ){
$trace = array();
foreach( $e->getRealTrace() as $raw ) {
$frame = new Loco_mvc_ViewParams($raw);
if( $frame->has('file') ){
$frame['file'] = Loco_mvc_FileParams::create( new Loco_fs_File($frame['file']) )->relpath;
}
$trace[] = $frame;
}
$this->set('trace',$trace);
}
}
else {
$e = new Loco_error_Exception('Unknown error');
$this->set('error', $e );
}
return $this->view( $e->getTemplate() );
}
}

View File

@@ -0,0 +1,72 @@
<?php
/**
* Generic navigation helper.
*/
class Loco_admin_Navigation extends ArrayIterator {
/**
* @return Loco_admin_Navigation
*/
public function add( $name, $href = null, $active = false ){
$this[] = new Loco_mvc_ViewParams( compact('name','href','active') );
return $this;
}
/* not currently used
* @return Loco_admin_Navigation
*
public function addRoute( $name, $action ){
$href = Loco_mvc_AdminRouter::generate( $action );
return $this->add( $name, $href );
}*/
/**
* Create a breadcrumb trail for a given view below a bundle
* @return Loco_admin_Navigation
*/
public static function createBreadcrumb( Loco_package_Bundle $bundle ){
$nav = new Loco_admin_Navigation;
// root link depends on bundle type
$type = strtolower( $bundle->getType() );
if( 'core' !== $type ){
$link = new Loco_mvc_ViewParams( array(
'href' => Loco_mvc_AdminRouter::generate($type),
) );
if( 'theme' === $type ){
$link['name'] = __('Themes','loco-translate');
}
else {
$link['name'] = __('Plugins','loco-translate');
}
$nav[] = $link;
}
// Add actual bundle page, href may be unset to show as current page if needed
$nav->add (
$bundle->getName(),
Loco_mvc_AdminRouter::generate( $type.'-view', array( 'bundle' => $bundle->getHandle() ) )
);
// client code will add current page
return $nav;
}
/**
* @return Loco_mvc_ViewParams
*
public function getSecondLast(){
$i = count($this);
if( $i > 1 ){
return $this[ $i-2 ];
}
}*/
}

View File

@@ -0,0 +1,35 @@
<?php
/**
*
*/
abstract class Loco_admin_RedirectController extends Loco_mvc_AdminController {
/**
* Get full URL for redirecting to.
* @var string
*/
abstract public function getLocation();
/**
* {@inheritdoc}
*/
public function init(){
$location = $this->getLocation();
if( $location && wp_redirect($location) ){
// @codeCoverageIgnoreStart
exit;
}
}
/**
* @internal
*/
public function render(){
return 'Failed to redirect';
}
}

View File

@@ -0,0 +1,90 @@
<?php
/**
* Highest level Loco admin screen.
*/
class Loco_admin_RootController extends Loco_admin_list_BaseController {
/**
* {@inheritdoc}
*/
public function getHelpTabs(){
return array (
__('Overview','default') => $this->viewSnippet('tab-home'),
);
}
/**
* Render main entry home screen
*/
public function render(){
// translators: home screen title where %s is the version number
$this->set('title', sprintf( __('Loco Translate %s','loco-translate'), loco_plugin_version() ) );
// Show currently active theme on home page
$theme = Loco_package_Theme::create(null);
$this->set('theme', $this->bundleParam($theme) );
// Show plugins that have currently loaded translations
$bundles = array();
foreach( Loco_package_Listener::singleton()->getPlugins() as $bundle ){
try {
$bundles[] = $this->bundleParam($bundle);
}
catch( Exception $e ){
// bundle should exist if we heard it. reduce to debug notice
Loco_error_AdminNotices::debug( $e->getMessage() );
}
}
$this->set('plugins', $bundles );
// Show recently "used' bundles
$bundles = array();
$recent = Loco_data_RecentItems::get();
// filter in lieu of plugin setting
$maxlen = apply_filters('loco_num_recent_bundles', 10 );
foreach( $recent->getBundles(0,$maxlen) as $id ){
try {
$bundle = Loco_package_Bundle::fromId($id);
$bundles[] = $this->bundleParam($bundle);
}
catch( Exception $e ){
// possible that bundle ID changed since being saved in recent items list
}
}
$this->set('recent', $bundles );
// current locale and related links
$locale = Loco_Locale::parse( get_locale() );
$api = new Loco_api_WordPressTranslations;
$tag = (string) $locale;
$this->set( 'siteLocale', new Loco_mvc_ViewParams( array(
'code' => $tag,
'name' => ( $name = $locale->ensureName($api) ),
'attr' => 'class="'.$locale->getIcon().'" lang="'.$locale->lang.'"',
'link' => '<a href="'.esc_url(Loco_mvc_AdminRouter::generate('lang-view', array('locale'=>$tag) )).'">'.esc_html($name).'</a>',
//'opts' => admin_url('options-general.php').'#WPLANG',
) ) );
// user's "admin" language may differ and is worth showing
if( function_exists('get_user_locale') ){
$locale = Loco_Locale::parse( get_user_locale() );
$alt = (string) $locale;
if( $tag !== $alt ){
$this->set( 'adminLocale', new Loco_mvc_ViewParams( array(
'name' => ( $name = $locale->ensureName($api) ),
'link' => '<a href="'.esc_url(Loco_mvc_AdminRouter::generate('lang-view', array('locale'=>$tag) )).'">'.esc_html($name).'</a>',
) ) );
}
}
$this->set('title', __('Welcome to Loco Translate','loco-translate') );
return $this->view('admin/root');
}
}

View File

@@ -0,0 +1,161 @@
<?php
/**
* Base controller for any admin screen related to a bundle
*/
abstract class Loco_admin_bundle_BaseController extends Loco_mvc_AdminController {
/**
* @var Loco_package_Bundle
*/
private $bundle;
/**
* @var Loco_package_Project
*/
private $project;
/**
* @return Loco_package_Bundle
*/
public function getBundle(){
if( ! $this->bundle ){
$type = $this->get('type');
$handle = $this->get('bundle');
$this->bundle = Loco_package_Bundle::createType( $type, $handle );
}
return $this->bundle;
}
/**
* Commit bundle config to database
* @return Loco_admin_bundle_BaseController
*/
protected function saveBundle(){
$custom = new Loco_config_CustomSaved;
if( $custom->setBundle($this->bundle)->persist() ){
Loco_error_AdminNotices::success( __('Configuration saved','loco-translate') );
}
// invalidate bundle in memory so next fetch is re-configured from DB
$this->bundle = null;
return $this;
}
/**
* Remove bundle config from database
* @return Loco_admin_bundle_BaseController
*/
protected function resetBundle(){
$option = $this->bundle->getCustomConfig();
if( $option && $option->remove() ){
Loco_error_AdminNotices::success( __('Configuration reset','loco-translate') );
// invalidate bundle in memory so next fetch falls back to auto-config
$this->bundle = null;
}
return $this;
}
/**
* @return Loco_package_Project
*/
public function getProject(){
if( ! $this->project ){
$bundle = $this->getBundle();
$domain = $this->get('domain');
if( ! $domain ){
throw new Loco_error_Exception( sprintf('Translation set not known in %s', $bundle ) );
}
$this->project = $bundle->getProjectById($domain);
if( ! $this->project ){
throw new Loco_error_Exception( sprintf('Unknown translation set: %s not in %s', json_encode($domain), $bundle ) );
}
}
return $this->project;
}
/**
* @return Loco_admin_Navigation
*/
protected function prepareNavigation(){
$bundle = $this->getBundle();
// navigate up to bundle listing page
$breadcrumb = Loco_admin_Navigation::createBreadcrumb( $bundle );
$this->set( 'breadcrumb', $breadcrumb );
// navigate between bundle view siblings
$tabs = new Loco_admin_Navigation;
$this->set( 'tabs', $tabs );
$actions = array (
'view' => __('Overview','loco-translate'),
'setup' => __('Setup','loco-translate'),
'conf' => __('Advanced','loco-translate'),
);
if( loco_debugging() ){
$actions['debug'] = __('Debug','loco-translate');
}
$suffix = $this->get('action');
$prefix = strtolower( $this->get('type') );
$getarg = array_intersect_key( $_GET, array('bundle'=>'') );
foreach( $actions as $action => $name ){
$href = Loco_mvc_AdminRouter::generate( $prefix.'-'.$action, $getarg );
$tabs->add( $name, $href, $action === $suffix );
}
return $breadcrumb;
}
/**
* Prepare file system connect
* @param string "create", "update", "delete"
* @param string path relative to wp-content
* @return Loco_mvc_HiddenFields
*/
protected function prepareFsConnect( $type, $relpath ){
$fields = new Loco_mvc_HiddenFields( array(
'auth' => $type,
'path' => $relpath,
'loco-nonce' => wp_create_nonce('fsConnect'),
'_fs_nonce' => wp_create_nonce('filesystem-credentials'), // <- WP 4.7.5 added security fix
) ) ;
$this->set('fsFields', $fields );
// may have fs credentials saved in session
try {
if( Loco_data_Settings::get()->fs_persist ){
$session = Loco_data_Session::get();
if( isset($session['loco-fs']) ){
$fields['connection_type'] = $session['loco-fs']['connection_type'];
}
}
}
catch( Exception $e ){
Loco_error_AdminNotices::debug( $e->getMessage() );
}
// Run pre-checks that may determine file should not be written
if( $relpath ){
$file = new Loco_fs_File( $relpath );
$file->normalize( loco_constant('WP_CONTENT_DIR') );
// total file system block makes connection type irrelevant
try {
$api = new Loco_api_WordPressFileSystem;
$api->preAuthorize($file);
}
catch( Loco_error_WriteException $e ){
$this->set('fsLocked', $e->getMessage() );
}
}
return $fields;
}
}

View File

@@ -0,0 +1,145 @@
<?php
/**
* Bundle configuration page
*/
class Loco_admin_bundle_ConfController extends Loco_admin_bundle_BaseController {
/**
* {@inheritdoc}
*/
public function init(){
parent::init();
$this->enqueueStyle('config');
$this->enqueueScript('config');
$bundle = $this->getBundle();
// translators: where %s is a plugin or theme
$this->set( 'title', sprintf( __('Configure %s','loco-translate'),$bundle->getName() ) );
$post = Loco_mvc_PostParams::get();
// always set a nonce for current bundle
$nonce = $this->setNonce( $this->get('_route').'-'.$this->get('bundle') );
$this->set('nonce', $nonce );
try {
// Save configuration if posted
if( $post->has('conf') ){
if( ! $post->name ){
$post->name = $bundle->getName();
}
$this->checkNonce( $nonce->action );
$model = new Loco_config_FormModel;
$model->loadForm( $post );
// configure bundle from model in full
$bundle->clear();
$reader = new Loco_config_BundleReader( $bundle );
$reader->loadModel( $model );
$this->saveBundle();
}
// Delete configuration if posted
else if( $post->has('unconf') ){
$this->resetBundle();
}
}
catch( Exception $e ){
Loco_error_AdminNotices::warn( $e->getMessage() );
}
}
/**
* {@inheritdoc}
*/
public function getHelpTabs(){
return array (
__('Advanced tab','loco-translate') => $this->viewSnippet('tab-bundle-conf'),
);
}
/**
* {@inheritdoc}
*/
public function render() {
$parent = null;
$bundle = $this->getBundle();
$default = $bundle->getDefaultProject();
$base = $bundle->getDirectoryPath();
// parent themes are inherited into bundle, we don't want them in the child theme config
if( $bundle->isTheme() && ( $parent = $bundle->getParent() ) ){
$this->set( 'parent', new Loco_mvc_ViewParams( array(
'name' => $parent->getName(),
'href' => Loco_mvc_AdminRouter::generate('theme-conf', array( 'bundle' => $parent->getSlug() ) + $_GET ),
) ) );
}
// render postdata straight back to form if sent
$data = Loco_mvc_PostParams::get();
// else build initial data from current bundle state
if( ! $data->has('conf') ){
// create single default set for totally unconfigured bundles
if( 0 === count($bundle) ){
$bundle->createDefault('');
}
$writer = new Loco_config_BundleWriter($bundle);
$data = $writer->toForm();
// removed parent bundle from config form, as they are inherited
/* @var Loco_package_Project $project */
foreach( $bundle as $i => $project ){
if( $parent && $parent->hasProject($project) ){
// warn if child theme uses parent theme's text domain (but allowing to render so we don't get an empty form.
if( $project === $default ){
Loco_error_AdminNotices::warn( __("Child theme declares the same Text Domain as the parent theme",'loco-translate') );
}
// else safe to remove parent theme configuration as it should be held in its own bundle
else {
$data['conf'][$i]['removed'] = true;
}
}
}
}
// build config blocks for form
$i = 0;
$conf = array();
foreach( $data['conf'] as $raw ){
if( empty($raw['removed']) ){
$slug = $raw['slug'];
$domain = $raw['domain'] or $domain = 'untitled';
$raw['prefix'] = sprintf('conf[%u]', $i++ );
$raw['short'] = ! $slug || ( $slug === $domain ) ? $domain : $domain.'→'.$slug;
$conf[] = new Loco_mvc_ViewParams( $raw );
}
}
// bundle level configs
$name = $bundle->getName();
$excl = $data['exclude'];
// access to type of configuration that's currently saved
$this->set('saved', $bundle->isConfigured() );
// link to author if there are config problems
$info = $bundle->getHeaderInfo();
$this->set('author', $info->getAuthorLink() );
// link for downloading current configuration XML file
$args = array (
'path' => 'loco.xml',
'action' => 'loco_download',
'bundle' => $bundle->getHandle(),
'type' => $bundle->getType()
);
$this->set( 'xmlUrl', Loco_mvc_AjaxRouter::generate( 'DownloadConf', $args ) );
$this->set( 'manUrl', apply_filters('loco_external','https://localise.biz/wordpress/plugin/manual/bundle-config') );
$this->prepareNavigation()->add( __('Advanced configuration','loco-translate') );
return $this->view('admin/bundle/conf', compact('conf','base','name','excl') );
}
}

View File

@@ -0,0 +1,52 @@
<?php
/**
* Bundle debugger.
* Shows bundle diagnostics and highlights problems
*/
class Loco_admin_bundle_DebugController extends Loco_admin_bundle_BaseController {
/**
* {@inheritdoc}
*/
public function init(){
parent::init();
$bundle = $this->getBundle();
$this->set('title', 'Debug: '.$bundle );
}
/**
* {@inheritdoc}
*/
public function render(){
$this->prepareNavigation()->add( __('Bundle diagnostics','loco-translate') );
$bundle = $this->getBundle();
$debugger = new Loco_package_Debugger($bundle);
$this->set('notices', $notices = new Loco_mvc_ViewParams );
/* @var $notice Loco_error_Exception */
foreach( $debugger as $notice ){
$notices[] = new Loco_mvc_ViewParams( array(
'style' => 'notice inline notice-'.$notice->getType(),
'title' => $notice->getTitle(),
'body' => $notice->getMessage(),
) );
}
$meta = $bundle->getHeaderInfo();
$this->set('meta', new Loco_mvc_ViewParams( array(
'vendor' => $meta->getVendorHost(),
'author' => $meta->getAuthorCredit(),
) ) );
if( count($bundle) ){
$writer = new Loco_config_BundleWriter( $bundle );
$this->set( 'xml', $writer->toXml() );
}
return $this->view('admin/bundle/debug');
}
}

View File

@@ -0,0 +1,162 @@
<?php
/**
* Pseudo-bundle view, lists all files available in a single locale
*/
class Loco_admin_bundle_LocaleController extends Loco_mvc_AdminController {
/**
* @var Loco_Locale
*/
private $locale;
/**
* {@inheritdoc}
*/
public function init(){
parent::init();
$tag = $this->get('locale');
$locale = Loco_Locale::parse($tag);
if( $locale->isValid() ){
$api = new Loco_api_WordPressTranslations;
$this->set('title', $locale->ensureName($api) );
$this->locale = $locale;
$this->enqueueStyle('locale')->enqueueStyle('fileinfo');
}
}
/**
* {@inheritdoc}
*/
public function getHelpTabs(){
return array (
__('Overview','default') => $this->viewSnippet('tab-locale-view'),
);
}
/**
* {@inheritdoc}
*/
public function render(){
// locale already parsed during init (for page title)
$locale = $this->locale;
if( ! $locale || ! $locale->isValid() ){
throw new Loco_error_Exception('Invalid locale argument');
}
// language may not be "installed" but we still want to inspect available files
$api = new Loco_api_WordPressTranslations;
$installed = $api->isInstalled($locale);
$tag = (string) $locale;
$package = new Loco_package_Locale( $locale );
// Get PO files for this locale
$files = $package->findLocaleFiles();
$translations = array();
$modified = 0;
$npofiles = 0;
$nfiles = 0;
// source locale means we want to see POT instead of translations
if( 'en_US' === $tag ){
$files = $package->findTemplateFiles()->augment($files);
}
/* @var Loco_fs_File */
foreach( $files as $file ){
$nfiles++;
if( 'pot' !== $file->extension() ){
$npofiles++;
}
$modified = max( $modified, $file->modified() );
$project = $package->getProject($file);
// do similarly to Loco_admin_bundle_ViewController::createFileParams
$meta = Loco_gettext_Metadata::load($file);
$dir = new Loco_fs_LocaleDirectory( $file->dirname() );
// arguments for deep link into project
$slug = $project->getSlug();
$domain = $project->getDomain()->getName();
$bundle = $project->getBundle();
$type = strtolower( $bundle->getType() );
$args = array(
// 'locale' => $tag,
'bundle' => $bundle->getHandle(),
'domain' => $project->getId(),
'path' => $meta->getPath(false),
);
// append data required for PO table row, except use bundle data instead of locale data
$translations[$type][] = new Loco_mvc_ViewParams( array (
// bundle info
'title' => $project->getName(),
'domain' => $domain,
'short' => ! $slug || $project->isDomainDefault() ? $domain : $domain.'→'.$slug,
// file info
'meta' => $meta,
'name' => $file->basename(),
'time' => $file->modified(),
'type' => strtoupper( $file->extension() ),
'todo' => $meta->countIncomplete(),
'total' => $meta->getTotal(),
// author / system / custom / other
'store' => $dir->getTypeLabel( $dir->getTypeId() ),
// links
'view' => Loco_mvc_AdminRouter::generate( $type.'-file-view', $args ),
'info' => Loco_mvc_AdminRouter::generate( $type.'-file-info', $args ),
'edit' => Loco_mvc_AdminRouter::generate( $type.'-file-edit', $args ),
'move' => Loco_mvc_AdminRouter::generate( $type.'-file-move', $args ),
'delete' => Loco_mvc_AdminRouter::generate( $type.'-file-delete', $args ),
'copy' => Loco_mvc_AdminRouter::generate( $type.'-msginit', $args ),
) );
}
$title = __( 'Installed languages', 'loco-translate' );
$breadcrumb = new Loco_admin_Navigation;
$breadcrumb->add( $title, Loco_mvc_AdminRouter::generate('lang') );
//$breadcrumb->add( $locale->getName() );
$breadcrumb->add( $tag );
// It's unlikely that an "installed" language would have no files, but could happen if only MO on disk
if( 0 === $nfiles ){
return $this->view('admin/errors/no-locale', compact('breadcrumb','locale') );
}
// files may be available for language even if not installed (i.e. no core files on disk)
if( ! $installed || ! isset($translations['core']) && 'en_US' !== $tag ){
Loco_error_AdminNotices::warn( __('No core translation files are installed for this language','loco-translate') )
->addLink('https://codex.wordpress.org/Installing_WordPress_in_Your_Language', __('Documentation','loco-translate') );
}
// Translated type labels and "See all <type>" links
$types = array(
'core' => new Loco_mvc_ViewParams( array(
'name' => __('WordPress Core','loco-translate'),
'text' => __('See all core translations','loco-translate'),
'href' => Loco_mvc_AdminRouter::generate('core')
) ),
'theme' => new Loco_mvc_ViewParams( array(
'name' => __('Themes','loco-translate'),
'text' => __('See all themes','loco-translate'),
'href' => Loco_mvc_AdminRouter::generate('theme')
) ),
'plugin' => new Loco_mvc_ViewParams( array(
'name' => __('Plugins','loco-translate'),
'text' => __('See all plugins','loco-translate'),
'href' => Loco_mvc_AdminRouter::generate('plugin')
) ),
);
$this->set( 'locale', new Loco_mvc_ViewParams( array(
'code' => $tag,
'name' => $locale->getName(),
'attr' => 'class="'.$locale->getIcon().'" lang="'.$locale->lang.'"',
) ) );
return $this->view( 'admin/bundle/locale', compact('breadcrumb','translations','types','npofiles','modified') );
}
}

View File

@@ -0,0 +1,193 @@
<?php
/**
*
*/
class Loco_admin_bundle_SetupController extends Loco_admin_bundle_BaseController {
/**
* {@inheritdoc}
*/
public function init(){
parent::init();
$bundle = $this->getBundle();
// translators: where %s is a plugin or theme
$this->set( 'title', sprintf( __('Set up %s','loco-translate'),$bundle->getName() ) );
}
/**
* {@inheritdoc}
*/
public function getHelpTabs(){
return array (
__('Setup tab','loco-translate') => $this->viewSnippet('tab-bundle-setup'),
);
}
/**
* {@inheritdoc}
*/
public function render(){
$this->prepareNavigation()->add( __('Bundle setup','loco-translate') );
$bundle = $this->getBundle();
$action = 'setup:'.$bundle->getId();
// execute auto-configure if posted
$post = Loco_mvc_PostParams::get();
if( $post->has('auto-setup') && $this->checkNonce( 'auto-'.$action) ){
if( 0 === count($bundle) ){
$bundle->createDefault();
}
foreach( $bundle as $project ){
if( ! $project->getPot() && ( $file = $project->guessPot() ) ){
$project->setPot( $file );
}
}
// forcefully add every additional project into bundle
foreach( $bundle->invert() as $project ){
if( ! $project->getPot() && ( $file = $project->guessPot() ) ){
$project->setPot( $file );
}
$bundle[] = $project;
}
$this->saveBundle();
$bundle = $this->getBundle();
$this->set('auto', null );
}
// execute XML-based config if posted
else if( $post->has('xml-setup') && $this->checkNonce( 'xml-'.$action) ){
$bundle->clear();
$model = new Loco_config_XMLModel;
$model->loadXml( trim( $post['xml-content'] ) );
$reader = new Loco_config_BundleReader($bundle);
$reader->loadModel( $model );
$this->saveBundle();
$bundle = $this->getBundle();
$this->set('xml', null );
}
// execute JSON-based config if posted
else if( $post->has('json-setup') && $this->checkNonce( 'json-'.$action) ){
$bundle->clear();
$model = new Loco_config_ArrayModel;
$model->loadJson( trim( $post['json-content'] ) );
$reader = new Loco_config_BundleReader($bundle);
$reader->loadModel( $model );
$this->saveBundle();
$bundle = $this->getBundle();
$this->set('json', null );
}
// execute reset if posted
else if( $post->has('reset-setup') && $this->checkNonce( 'reset-'.$action) ){
$this->resetBundle();
$bundle = $this->getBundle();
}
// bundle author links
$info = $bundle->getHeaderInfo();
$this->set( 'credit', $info->getAuthorCredit() );
// render according to current configuration method (save type)
$configured = $this->get('force') or $configured = $bundle->isConfigured();
$notices = new ArrayIterator;
$this->set('notices', $notices );
// collect configuration warnings
foreach( $bundle as $project ){
$potfile = $project->getPot();
if( ! $potfile ){
$notices[] = sprintf('No translation template for the "%s" text domain', $project->getSlug() );
}
}
// if extra files found consider incomplete
if( $bundle->isTheme() || ( $bundle->isPlugin() && ! $bundle->isSingleFile() ) ){
$unknown = Loco_package_Inverter::export($bundle);
$n = 0;
foreach( $unknown as $ext => $files ){
$n += count($files);
}
if( $n ){
$notices[] = sprintf( _n("One file can't be matched to a known set of strings","%s files can't be matched to a known set of strings",$n,'loco-translate'), number_format($n) );
}
}
// display setup options if at least one option specified
$doconf = false;
// enable form to invoke auto-configuration
if( $this->get('auto') ){
$fields = new Loco_mvc_HiddenFields();
$fields->setNonce( 'auto-'.$action );
$this->set('autoFields', $fields );
$doconf = true;
}
// enable form to paste XML config
if( $this->get('xml') ){
$fields = new Loco_mvc_HiddenFields();
$fields->setNonce( 'xml-'.$action );
$this->set('xmlFields', $fields );
$doconf = true;
}
// enable form to paste JSON config (via remote lookup)
if( $this->get('json') ){
$fields = new Loco_mvc_HiddenFields( array(
'json-content' => '',
'version' => $info->Version,
) );
$fields->setNonce( 'json-'.$action );
$this->set('jsonFields', $fields );
// other information for looking up bundle via api
$this->set('vendorSlug', $bundle->getSlug() );
// remote config is done via JavaScript
$this->enqueueScript('setup');
$apiBase = apply_filters( 'loco_api_url', 'https://localise.biz/api' );
$this->set('js', new Loco_mvc_ViewParams( array(
'apiUrl' => $apiBase.'/wp/'.strtolower( $bundle->getType() ),
) ) );
$doconf = true;
}
// display configurator if configurating
if( $doconf ){
return $this->view( 'admin/bundle/setup/conf' );
}
// else set configurator links back to self with required option
// ...
if( ! $configured || ! count($bundle) ){
return $this->view( 'admin/bundle/setup/none' );
}
if( 'db' === $configured ){
// form for resetting config
$fields = new Loco_mvc_HiddenFields();
$fields->setNonce( 'reset-'.$action );
$this->set( 'reset', $fields );
return $this->view('admin/bundle/setup/saved');
}
if( 'internal' === $configured ){
return $this->view('admin/bundle/setup/core');
}
if( 'file' === $configured ){
return $this->view('admin/bundle/setup/author');
}
if( count($notices) ){
return $this->view('admin/bundle/setup/partial');
}
return $this->view('admin/bundle/setup/meta');
}
}

View File

@@ -0,0 +1,322 @@
<?php
/**
* Bundle overview.
* First tier bundle view showing resources across all projects
*/
class Loco_admin_bundle_ViewController extends Loco_admin_bundle_BaseController {
/**
* {@inheritdoc}
*/
public function init(){
parent::init();
$bundle = $this->getBundle();
$this->set('title', $bundle->getName() );
$this->enqueueStyle('bundle');
}
/**
* {@inheritdoc}
*/
public function getHelpTabs(){
return array (
__('Overview','default') => $this->viewSnippet('tab-bundle-view'),
);
}
/**
* Generate a link for a specific file resource within a project
* @return string
*/
private function getResourceLink( $page, Loco_package_Project $project, Loco_gettext_Metadata $meta, array $args = array() ){
$args['path'] = $meta->getPath(false);
return $this->getProjectLink( $page, $project, $args );
}
/**
* Generate a link for a project, but without being for a specific file
* @return string
*/
private function getProjectLink( $page, Loco_package_Project $project, array $args = array() ){
$args['bundle'] = $this->get('bundle');
$args['domain'] = $project->getId();
$route = strtolower( $this->get('type') ).'-'.$page;
return Loco_mvc_AdminRouter::generate( $route, $args );
}
/**
* Initialize view parameters for a project
* @param Loco_package_Project
* @return Loco_mvc_ViewParams
*/
private function createProjectParams( Loco_package_Project $project ){
$name = $project->getName();
$domain = $project->getDomain()->getName();
$slug = $project->getSlug();
$p = new Loco_mvc_ViewParams( array (
'id' => $project->getId(),
'name' => $name,
'slug' => $slug,
'domain' => $domain,
'short' => ! $slug || $project->isDomainDefault() ? $domain : $domain.'→'.$slug,
) );
// POT template file
$file = $project->getPot();
if( $file && $file->exists() ){
$meta = Loco_gettext_Metadata::load($file);
$p['pot'] = new Loco_mvc_ViewParams( array(
// POT info
'name' => $file->basename(),
'time' => $file->modified(),
// POT links
'info' => $this->getResourceLink('file-info', $project, $meta ),
'edit' => $this->getResourceLink('file-edit', $project, $meta ),
) );
}
// PO/MO files
$po = $project->findLocaleFiles('po');
$mo = $project->findLocaleFiles('mo');
$p['po'] = $this->createProjectPairs( $project, $po, $mo );
// also pull invalid files so everything is available to the UI
$mo = $project->findNotLocaleFiles('mo');
$po = $project->findNotLocaleFiles('po')->augment( $project->findNotLocaleFiles('pot') );
$p['_po'] = $this->createProjectPairs( $project, $po, $mo );
// always offer msginit even if we find out later we can't extract any strings
$p['nav'][] = new Loco_mvc_ViewParams( array(
'href' => $this->getProjectLink('msginit', $project ),
'name' => __('New language','loco-translate'),
'icon' => 'add',
) );
$pot = $project->getPot();
// prevent editing of POT when config prohibits
if( $project->isPotLocked() ) {
if( $pot && $pot->exists() ){
$meta = Loco_gettext_Metadata::load($pot);
$p['nav'][] = new Loco_mvc_ViewParams( array(
'href' => $this->getResourceLink('file-view', $project, $meta ),
'name' => __('View template','loco-translate'),
'icon' => 'file',
) );
}
}
// offer template editing if permitted
else if( $pot && $pot->exists() ){
$p['pot'] = $pot;
$meta = Loco_gettext_Metadata::load($pot);
$p['nav'][] = new Loco_mvc_ViewParams( array(
'href' => $this->getResourceLink('file-edit', $project, $meta ),
'name' => __('Edit template','loco-translate'),
'icon' => 'pencil',
) );
}
// else offer creation of new Template
else {
$p['nav'][] = new Loco_mvc_ViewParams( array(
'href' => $this->getProjectLink('xgettext', $project ),
'name' => __('Create template','loco-translate'),
'icon' => 'add',
) );
}
return $p;
}
/**
* Collect PO/MO pairings, ignoring any PO that is in use as a template
*/
private function createPairs( Loco_fs_FileList $po, Loco_fs_FileList $mo, Loco_fs_File $pot = null ){
$pairs = array();
/* @var $pofile Loco_fs_LocaleFile */
foreach( $po as $pofile ){
if( $pot && $pofile->equal($pot) ){
continue;
}
$pair = array( $pofile, null );
$mofile = $pofile->cloneExtension('mo');
if( $mofile->exists() ){
$pair[1] = $mofile;
}
$pairs[] = $pair;
}
/* @var $mofile Loco_fs_LocaleFile */
foreach( $mo as $mofile ){
$pofile = $mofile->cloneExtension('po');
if( $pot && $pofile->equal($pot) ){
continue;
}
if( ! $pofile->exists() ){
$pairs[] = array( null, $mofile );
}
}
return $pairs;
}
/**
* Initialize view parameters for each row representing a localized resource pair
* @return array collection of entries corresponding to available PO/MO pair.
*/
private function createProjectPairs( Loco_package_Project $project, Loco_fs_LocaleFileList $po, Loco_fs_LocaleFileList $mo ){
// populate official locale names for all found, or default to our own
if( $locales = $po->getLocales() + $mo->getLocales() ){
$api = new Loco_api_WordPressTranslations;
/* @var $locale Loco_Locale */
foreach( $locales as $tag => $locale ){
$locale->ensureName($api);
}
}
// collate as unique [PO,MO] pairs ensuring canonical template excluded
$pairs = $this->createPairs( $po, $mo, $project->getPot() );
$rows = array();
foreach( $pairs as $pair ){
// favour PO file if it exists
list( $pofile, $mofile ) = $pair;
$file = $pofile or $file = $mofile;
// establish locale, or assume invalid
$locale = null;
/* @var Loco_fs_LocaleFile $file */
if( 'pot' !== $file->extension() ){
$tag = $file->getSuffix();
if( isset($locales[$tag]) ){
$locale = $locales[$tag];
}
}
$rows[] = $this->createFileParams( $project, $file, $locale );
}
return $rows;
}
/**
* @param Loco_package_Project
* @param Loco_fs_File
* @param Loco_Locale
* @return Loco_mvc_ViewParams
*/
private function createFileParams( Loco_package_Project $project, Loco_fs_File $file, Loco_Locale $locale = null ){
// Pull Gettext meta data from cache if possible
$meta = Loco_gettext_Metadata::load($file);
$dir = new Loco_fs_LocaleDirectory( $file->dirname() );
// routing arguments
$args = array (
'path' => $meta->getPath(false),
);
// Return data required for PO table row
return new Loco_mvc_ViewParams( array (
// locale info
'lcode' => $locale ? (string) $locale : '',
'lname' => $locale ? $locale->getName() : '',
'lattr' => $locale ? 'class="'.$locale->getIcon().'" lang="'.$locale->lang.'"' : '',
// file info
'meta' => $meta,
'name' => $file->basename(),
'time' => $file->modified(),
'type' => strtoupper( $file->extension() ),
'todo' => $meta->countIncomplete(),
'total' => $meta->getTotal(),
// author / system / custom / other
'store' => $dir->getTypeLabel( $dir->getTypeId() ),
// links
'view' => $this->getProjectLink('file-view', $project, $args ),
'info' => $this->getProjectLink('file-info', $project, $args ),
'edit' => $this->getProjectLink('file-edit', $project, $args ),
'move' => $this->getProjectLink('file-move', $project, $args ),
'delete' => $this->getProjectLink('file-delete', $project, $args ),
'copy' => $this->getProjectLink('msginit', $project, $args ),
) );
}
/**
* Prepare view parameters for all projects in a bundle
* @param Loco_package_Bundle
* @return array<Loco_mvc_ViewParams>
*/
private function createBundleListing( Loco_package_Bundle $bundle ){
$projects = array();
/* @var $project Loco_package_Project */
foreach( $bundle as $project ){
$projects[] = $this->createProjectParams($project);
}
return $projects;
}
/**
* {@inheritdoc}
*/
public function render(){
$this->prepareNavigation();
$bundle = $this->getBundle();
$this->set('name', $bundle->getName() );
// bundle may not be fully configured
$configured = $bundle->isConfigured();
// Hello Dolly is an exception. don't show unless configured deliberately
if( ! $configured && 'hello.php' === $bundle->getHandle() && 'Hello Dolly' === $bundle->getName() ){
$this->set( 'redirect', Loco_mvc_AdminRouter::generate('core-view') );
return $this->view('admin/bundle/alias');
}
// Collect all configured projects
$projects = $this->createBundleListing( $bundle );
$unknown = array();
// sniff additional unknown files if bundle is a theme or directory-based plugin that's been auto-detected
if( 'file' === $configured || 'internal' === $configured ){
// presumed complete
}
else if( $bundle->isTheme() || ( $bundle->isPlugin() && ! $bundle->isSingleFile() ) ){
// TODO This needs abstracting into the Loco_package_Inverter class
$prefixes = array();
$po = new Loco_fs_LocaleFileList;
$mo = new Loco_fs_LocaleFileList;
foreach( Loco_package_Inverter::export($bundle) as $ext => $files ){
$list = 'mo' === $ext ? $mo : $po;
foreach( $files as $file ){
$file = new Loco_fs_LocaleFile($file);
$list->addLocalized( $file );
// Only look in system locations if locale is valid and domain/prefix available
$locale = $file->getLocale();
if( $locale->isValid() && ( $domain = $file->getPrefix() ) ){
$prefixes[$domain] = true;
}
}
}
// pick up given files in system locations only
foreach( $prefixes as $domain => $_bool ){
$dummy = new Loco_package_Project( $bundle, new Loco_package_TextDomain($domain), '' );
$bundle->addProject( $dummy ); // <- required to configure locations
$dummy->excludeTargetPath( $bundle->getDirectoryPath() );
$po->augment( $dummy->findLocaleFiles('po') );
$mo->augment( $dummy->findLocaleFiles('mo') );
}
// a fake project is required to disable functions that require a configured project
$dummy = new Loco_package_Project( $bundle, new Loco_package_TextDomain(''), '' );
$unknown = $this->createProjectPairs( $dummy, $po, $mo );
}
$this->set('projects', $projects );
$this->set('unknown', $unknown );
return $this->view( 'admin/bundle/view' );
}
}

View File

@@ -0,0 +1,45 @@
<?php
/**
* Base controller for global plugin configuration screens
*/
abstract class Loco_admin_config_BaseController extends Loco_mvc_AdminController {
/**
* {@inheritdoc}
*/
public function init(){
parent::init();
// navigate between config view siblings, but only if privileged user
if( current_user_can('manage_options') ){
$tabs = new Loco_admin_Navigation;
$this->set( 'tabs', $tabs );
$actions = array (
'' => __('Site options','loco-translate'),
'user' => __('User options','loco-translate'),
'version' => __('Version','loco-translate'),
);
if( loco_debugging() ){
$actions['debug'] = __('Debug','loco-translate');
}
$suffix = (string) $this->get('action');
foreach( $actions as $action => $name ){
$href = Loco_mvc_AdminRouter::generate( 'config-'.$action, $_GET );
$tabs->add( $name, $href, $action === $suffix );
}
}
}
/**
* {@inheritdoc}
*/
public function getHelpTabs(){
return array (
__('Overview','default') => $this->viewSnippet('tab-settings'),
);
}
}

View File

@@ -0,0 +1,150 @@
<?php
/**
* Plugin config check (system diagnostics)
*/
class Loco_admin_config_DebugController extends Loco_admin_config_BaseController {
/**
* {@inheritdoc}
*/
public function init(){
parent::init();
$this->set( 'title', __('Debug','loco-translate') );
}
/**
* @param string
* @return int
*/
private function memory_size( $raw ){
$bytes = wp_convert_hr_to_bytes($raw);
return Loco_mvc_FileParams::renderBytes($bytes);
}
/**
* @param string
* @return string
*/
private function rel_path( $path ){
if( is_string($path) && $path && '/' === $path[0] ){
$file = new Loco_fs_File( $path );
$path = $file->getRelativePath(ABSPATH);
}
else if( ! $path ){
$path = '(none)';
}
return $path;
}
/**
* {@inheritdoc}
*/
public function render(){
$title = __('System diagnostics','loco-translate');
$breadcrumb = new Loco_admin_Navigation;
$breadcrumb->add( $title );
// extensions that are normally enabled in PHP by default
loco_check_extension('json');
loco_check_extension('ctype');
// product versions:
$versions = new Loco_mvc_ViewParams( array (
'Loco Translate' => loco_plugin_version(),
'WordPress' => $GLOBALS['wp_version'],
'PHP' => phpversion().' ('.PHP_SAPI.')',
'Server' => isset($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] : ( function_exists('apache_get_version') ? apache_get_version() : '' ),
) );
// we want to know about modules in case there are security mods installed known to break functionality
if( function_exists('apache_get_modules') && ( $mods = preg_grep('/^mod_/',apache_get_modules() ) ) ){
$versions['Server'] .= ' + '.implode(', ',$mods);
}
// byte code cache (currently only checking for Zend OPcache)
if( function_exists('opcache_get_configuration') && ini_get('opcache.enable') ){
$info = opcache_get_configuration();
$vers = $info['version'];
$versions[ $vers['opcache_product_name'] ] = ' '.$vers['version'];
}
// utf8 / encoding:
$encoding = new Loco_mvc_ViewParams( array (
'OK' => "\xCE\x9F\xCE\x9A",
'tick' => "\xE2\x9C\x93",
'json' => json_decode('"\\u039f\\u039a \\u2713"'),
'mbstring' => loco_check_extension('mbstring') ? "\xCE\x9F\xCE\x9A \xE2\x9C\x93" : 'No',
) );
// Sanity check mbstring.func_overload
if( 2 !== strlen("\xC2\xA3") ){
$encoding->mbstring = 'Error, disable mbstring.func_overload';
}
// PHP / env memory settings:
$memory = new Loco_mvc_PostParams( array(
'WP_MEMORY_LIMIT' => $this->memory_size( loco_constant('WP_MEMORY_LIMIT') ),
'WP_MAX_MEMORY_LIMIT' => $this->memory_size( loco_constant('WP_MAX_MEMORY_LIMIT') ),
'PHP memory_limit' => $this->memory_size( ini_get('memory_limit') ),
'PHP post_max_size' => $this->memory_size( ini_get('post_max_size') ),
//'PHP upload_max_filesize' => $this->memory_size( ini_get('upload_max_filesize') ),
'PHP max_execution_time' => (string) ini_get('max_execution_time'),
) );
// Check if raising memory limit works (wp>=4.6)
if( function_exists('wp_is_ini_value_changeable') && wp_is_ini_value_changeable('memory_limit') ){
$memory['PHP memory_limit'] .= ' (changeable)';
}
// Ajaxing:
$this->enqueueScript('debug');
$this->set( 'js', new Loco_mvc_ViewParams( array (
'nonces' => array( 'ping' => wp_create_nonce('ping') ),
) ) );
// File system access
$dir = new Loco_fs_Directory( loco_constant('LOCO_LANG_DIR') ) ;
$ctx = new Loco_fs_FileWriter( $dir );
$fsp = Loco_data_Settings::get()->fs_protect;
$fs = new Loco_mvc_PostParams( array(
'langdir' => $this->rel_path( $dir->getPath() ),
'writable' => $ctx->writable(),
'disabled' => $ctx->disabled(),
'fs_protect' => 1 === $fsp ? 'Warn' : ( $fsp ? 'Block' : 'Off' ),
) );
// Debug and error log settings
$debug = new Loco_mvc_ViewParams( array(
'WP_DEBUG' => loco_constant('WP_DEBUG') ? 'On' : 'Off',
'WP_DEBUG_LOG' => loco_constant('WP_DEBUG_LOG') ? 'On' : 'Off',
'WP_DEBUG_DISPLAY' => loco_constant('WP_DEBUG_DISPLAY') ? 'On' : 'Off',
'PHP display_errors' => ini_get('display_errors') ? 'On' : 'Off',
'PHP log_errors' => ini_get('log_errors') ? 'On' : 'Off',
'PHP error_log' => $this->rel_path( ini_get('error_log') ),
) );
/* Output buffering settings
$this->set('ob', new Loco_mvc_ViewParams( array(
'output_handler' => ini_get('output_handler'),
'zlib.output_compression' => ini_get('zlib.output_compression'),
'zlib.output_compression_level' => ini_get('zlib.output_compression_level'),
'zlib.output_handler' => ini_get('zlib.output_handler'),
) ) );*/
// alert to known system setting problems:
if( version_compare(PHP_VERSION,'7.4','<') ){
if( get_magic_quotes_gpc() ){
Loco_error_AdminNotices::add( new Loco_error_Debug('You have "magic_quotes_gpc" enabled. We recommend you disable this in PHP') );
}
if( get_magic_quotes_runtime() ){
Loco_error_AdminNotices::add( new Loco_error_Debug('You have "magic_quotes_runtime" enabled. We recommend you disable this in PHP') );
}
}
return $this->view('admin/config/debug', compact('breadcrumb','versions','encoding','memory','fs','debug') );
}
}

View File

@@ -0,0 +1,55 @@
<?php
/**
* User-level plugin preferences
*/
class Loco_admin_config_PrefsController extends Loco_admin_config_BaseController {
/**
* {@inheritdoc}
*/
public function init(){
parent::init();
$this->set( 'title', __('User options','loco-translate') );
// user preference options
$opts = Loco_data_Preferences::get();
$this->set( 'opts', $opts );
// default value for Last-Translator credit
$user = wp_get_current_user();
$name = $user->get('display_name') or $name = 'nobody';
$email = $user->get('user_email') or $email = 'nobody@localhost';
$this->set('credit', sprintf('%s <%s>', $name, $email ) );
// handle save action
$nonce = $this->setNonce('save-prefs');
try {
if( $this->checkNonce($nonce->action) ){
$post = Loco_mvc_PostParams::get();
if( $post->has('opts') ){
$opts->populate( $post->opts )->persist();
Loco_error_AdminNotices::success( __('Settings saved','loco-translate') );
}
}
}
catch( Loco_error_Exception $e ){
Loco_error_AdminNotices::add($e);
}
}
/**
* {@inheritdoc}
*/
public function render(){
$title = __('Plugin settings','loco-translate');
$breadcrumb = new Loco_admin_Navigation;
$breadcrumb->add( $title );
return $this->view('admin/config/prefs', compact('breadcrumb') );
}
}

View File

@@ -0,0 +1,84 @@
<?php
/**
* Site-wide Loco options (plugin settings)
*/
class Loco_admin_config_SettingsController extends Loco_admin_config_BaseController {
/**
* {@inheritdoc}
*/
public function init(){
parent::init();
// set current plugin options and defaults for placeholders
$opts = Loco_data_Settings::get();
$this->set( 'opts', $opts );
$this->set( 'dflt', Loco_data_Settings::create() );
// roles and capabilities
$perms = new Loco_data_Permissions;
// handle save action
$nonce = $this->setNonce('save-config');
try {
if( $this->checkNonce($nonce->action) ){
$post = Loco_mvc_PostParams::get();
if( $post->has('opts') ){
$opts->populate( $post->opts )->persist();
$perms->populate( $post->has('caps') ? $post->caps : array() );
// done update
Loco_error_AdminNotices::success( __('Settings saved','loco-translate') );
// remove saved params from session if persistent options unset
if( ! $opts['fs_persist'] ){
$session = Loco_data_Session::get();
if( isset($session['loco-fs']) ){
unset( $session['loco-fs'] );
$session->persist();
}
}
}
}
}
catch( Loco_error_Exception $e ){
Loco_error_AdminNotices::add($e);
}
$this->set('caps', $caps = new Loco_mvc_ViewParams );
// there is no distinct role for network admin, so we'll fake it for UI
if( is_multisite() ){
$caps[''] = new Loco_mvc_ViewParams( array(
'label' => __('Super Admin','default'),
'name' => 'dummy-admin-cap',
'attrs' => 'checked disabled'
) );
}
/* @var $role WP_Role */
foreach( $perms->getRoles() as $id => $role ){
$caps[$id] = new Loco_mvc_ViewParams( array(
'value' => '1',
'label' => $perms->getRoleName($id),
'name' => 'caps['.$id.'][loco_admin]',
'attrs' => $perms->isProtectedRole($role) ? 'checked disabled ' : ( $role->has_cap('loco_admin') ? 'checked ' : '' ),
) );
}
}
/**
* {@inheritdoc}
*/
public function render(){
$title = __('Plugin settings','loco-translate');
$breadcrumb = new Loco_admin_Navigation;
$breadcrumb->add( $title );
return $this->view('admin/config/settings', compact('breadcrumb') );
}
}

View File

@@ -0,0 +1,65 @@
<?php
/**
* Plugin version / upgrade screen
*/
class Loco_admin_config_VersionController extends Loco_admin_config_BaseController {
/**
* {@inheritdoc}
*/
public function init(){
parent::init();
$this->set( 'title', __('Version','loco-translate') );
}
/**
* {@inheritdoc}
*/
public function render(){
$title = __('Plugin settings','loco-translate');
$breadcrumb = new Loco_admin_Navigation;
$breadcrumb->add( $title );
// current plugin version
$version = loco_plugin_version();
// check for auto-update availability
if( $updates = get_site_transient('update_plugins') ){
$key = loco_plugin_self();
if( isset($updates->response[$key]) ){
$latest = $updates->response[$key]->new_version;
// if current version is lower than latest, prompt update
if( version_compare($version,$latest,'<') ){
$this->setUpdate($latest);
}
}
}
// notify if running a development snapshot, but only if ahead of latest stable
if( '-dev' === substr($version,-4) ){
$this->set( 'devel', true );
}
// $this->setUpdate('2.0.1-debug');
return $this->view('admin/config/version', compact('breadcrumb','version') );
}
/**
* @param string version
* @return void
*/
private function setUpdate( $version ){
$action = 'upgrade-plugin_'.loco_plugin_self();
$link = admin_url( 'update.php?action=upgrade-plugin&plugin='.rawurlencode(loco_plugin_self()) );
$this->set('update', $version );
$this->set('update_href', wp_nonce_url( $link, $action ) );
}
}

View File

@@ -0,0 +1,167 @@
<?php
/**
* Base class for a file resource belonging to a bundle
* Root > List > Bundle > Resource
*/
abstract class Loco_admin_file_BaseController extends Loco_admin_bundle_BaseController {
/**
* @var Loco_Locale
*/
private $locale;
/**
* @return Loco_Locale
*/
protected function getLocale(){
return $this->locale;
}
/**
* Check file is valid or return error
* @param Loco_fs_File
* @return string rendered error
*/
protected function getFileError( Loco_fs_File $file = null ){
// file must exist for editing
if( is_null($file) || ! $file->exists() ){
return $this->view( 'admin/errors/file-missing', array() );
}
if( $file->isDirectory() ){
$this->set('info', Loco_mvc_FileParams::create($file) );
return $this->view( 'admin/errors/file-isdir', array() );
}
// security validations
try {
Loco_gettext_Data::ext( $file );
// TODO also need to block access to files outside content directory
// this is more difficult as can symlink into and out of the tree.
}
catch( Exception $e ){
return $this->view( 'admin/errors/file-sec', array( 'reason' => $e->getMessage() ) );
}
return '';
}
/**
* {@inheritdoc}
*/
public function init(){
parent::init();
// views at this level are always related to a file
// file is permitted to be missing during this execution.
$path = $this->get('path');
if( ! $path ){
throw new Loco_error_Exception('path argument required');
}
$file = new Loco_fs_LocaleFile( $path );
$file->normalize( loco_constant('WP_CONTENT_DIR') );
$ext = strtolower( $file->extension() );
// POT file has no locale
if( 'pot' === $ext ){
$locale = null;
$localised = false;
}
// else file may have a locale suffix (unless invalid, such as "default.po")
else {
$locale = $file->getLocale();
$localised = $locale->isValid();
}
if( $localised ){
$this->locale = $locale;
$code = (string) $locale;
$this->set( 'locale', new Loco_mvc_ViewParams( array(
'code' => $code,
'lang' => $locale->lang,
'icon' => $locale->getIcon(),
'name' => $locale->ensureName( new Loco_api_WordPressTranslations ),
'href' => Loco_mvc_AdminRouter::generate('lang-view', array('locale'=>$code) ),
) ) );
}
else {
$this->set( 'locale', null );
}
$this->set('file', $file );
$this->set('filetype', strtoupper($ext) );
$this->set('title', $file->basename() );
// navigate up to root from this bundle sub view
$bundle = $this->getBundle();
$breadcrumb = Loco_admin_Navigation::createBreadcrumb( $bundle );
$this->set( 'breadcrumb', $breadcrumb );
// navigate between sub view siblings for this resource
$tabs = new Loco_admin_Navigation;
$this->set( 'tabs', $tabs );
$actions = array (
'file-edit' => __('Editor','loco-translate'),
'file-view' => __('Source','loco-translate'),
'file-info' => __('File info','loco-translate'),
'file-diff' => __('Restore','loco-translate'),
'file-move' => $localised ? __('Relocate','loco-translate') : null,
'file-delete' => __('Delete','loco-translate'),
);
$suffix = $this->get('action');
$prefix = $this->get('type');
$args = array_intersect_key($_GET,array('path'=>1,'bundle'=>1,'domain'=>1));
foreach( $actions as $action => $name ){
if( is_string($name) ){
$href = Loco_mvc_AdminRouter::generate( $prefix.'-'.$action, $args );
$tabs->add( $name, $href, $action === $suffix );
}
}
// Provide common language creation link if project scope is is valid
try {
$project = $this->getProject();
$args = array( 'bundle' => $bundle->getHandle(), 'domain' => $project->getId() );
$this->set( 'msginit', new Loco_mvc_ViewParams( array (
'href' => Loco_mvc_AdminRouter::generate( $prefix.'-msginit', $args ),
'text' => __('New language','loco-translate'),
) ) );
}
catch( Exception $e ){
}
}
/**
* {@inheritdoc}
*/
public function view( $tpl, array $args = array() ){
if( $breadcrumb = $this->get('breadcrumb') ){
// Add project name into breadcrumb if not the same as bundle name
try {
$project = $this->getProject();
if( $project->getName() !== $this->getBundle()->getName() ){
$breadcrumb->add( $project->getName() );
}
}
catch( Loco_error_Exception $e ){
// ignore missing project in breadcrumb
}
// Always add page title as final breadcrumb element
$title = $this->get('title') or $title = 'Untitled';
$breadcrumb->add( $title );
}
return parent::view( $tpl, $args );
}
}

View File

@@ -0,0 +1,113 @@
<?php
/**
* File delete function
*/
class Loco_admin_file_DeleteController extends Loco_admin_file_BaseController {
/**
* Expand single path to all files that will be deleted
* @param Loco_fs_File primary file being deleted, probably the PO
* @return array
*/
private function expandFiles( Loco_fs_File $file ){
try {
$siblings = new Loco_fs_Siblings( $file );
}
catch( InvalidArgumentException $e ){
$ext = $file->extension();
throw new Loco_error_Exception( sprintf('Refusing to delete a %s file', strtoupper($ext) ) );
}
return $siblings->expand();
}
/**
* {@inheritdoc}
*/
public function init(){
parent::init();
$file = $this->get('file');
// set up form for delete confirmation
if( $file->exists() && ! $file->isDirectory() ){
// nonce action will be specific to file for extra security
// TODO could also add file MD5 to avoid deletion after changes made.
$path = $file->getPath();
$action = 'delete:'.$path;
// set up view now in case of late failure
$fields = new Loco_mvc_HiddenFields( array() );
$fields->setNonce( $action );
$this->set( 'hidden', $fields );
// attempt delete if valid nonce posted back
if( $this->checkNonce($action) ){
$api = new Loco_api_WordPressFileSystem;
// delete dependant files first, so master still exists if others fail
$files = array_reverse( $this->expandFiles($file) );
try {
/* @var $trash Loco_fs_File */
foreach( $files as $trash ){
$api->authorizeDelete($trash);
$trash->unlink();
}
// flash message for display after redirect
try {
$n = count( $files );
Loco_data_Session::get()->flash('success', sprintf( _n('File deleted','%u files deleted',$n,'loco-translate'),$n) );
Loco_data_Session::close();
}
catch( Exception $e ){
// tolerate session failure
}
// redirect to bundle overview
$href = Loco_mvc_AdminRouter::generate( $this->get('type').'-view', array( 'bundle' => $this->get('bundle') ) );
if( wp_redirect($href) ){
exit;
}
}
catch( Loco_error_Exception $e ){
Loco_error_AdminNotices::add( $e );
}
}
}
// set page title before render sets inline title
$bundle = $this->getBundle();
$this->set('title', sprintf( __('Delete %s','loco-translate'), $file->basename() ).' &lsaquo; '.$bundle->getName() );
}
/**
* {@inheritdoc}
*/
public function render(){
$file = $this->get('file');
if( $fail = $this->getFileError($file) ){
return $fail;
}
$files = $this->expandFiles( $file );
$info = Loco_mvc_FileParams::create($file);
$this->set( 'info', $info );
$this->set( 'title', sprintf( __('Delete %s','loco-translate'), $info->name ) );
// warn about additional files that will be deleted along with this
if( $deps = array_slice($files,1) ){
$count = count($deps);
$this->set('warn', sprintf( _n( 'One dependent file will also be deleted', '%u dependent files will also be deleted', $count, 'loco-translate' ), $count ) );
$infos = array();
foreach( $deps as $depfile ){
$infos[] = Loco_mvc_FileParams::create( $depfile );
}
$this->set('deps', $infos );
}
$this->prepareFsConnect( 'delete', $this->get('path') );
$this->enqueueScript('delete');
return $this->view('admin/file/delete');
}
}

View File

@@ -0,0 +1,156 @@
<?php
/**
* File revisions and rollback
*/
class Loco_admin_file_DiffController extends Loco_admin_file_BaseController {
/**
* {@inheritdoc}
*/
public function init(){
parent::init();
$this->enqueueStyle('podiff');
$pofile = $this->get('file');
if( $pofile->exists() && ! $pofile->isDirectory() ){
$path = $pofile->getPath();
$action = 'restore:'.$path;
// set up view now in case of late failure
$fields = new Loco_mvc_HiddenFields( array() );
$fields->setNonce( $action );
$this->set( 'hidden', $fields );
// attempt rollback if valid nonce posted back with backup path
if( $this->checkNonce($action) ){
try {
$post = Loco_mvc_PostParams::get();
$api = new Loco_api_WordPressFileSystem;
// Restore
if( $path = $post->backup ){
$target = new Loco_fs_File( $path );
$target->normalize( loco_constant('WP_CONTENT_DIR') );
// parse PO. we'll need it for MO compile anyway
$source = $target->getContents();
$data = Loco_gettext_Data::fromSource( $source );
// backup current master before restoring
$backups = new Loco_fs_Revisions($pofile);
if( $num_backups = Loco_data_Settings::get()->num_backups ){
$api->authorizeCopy($pofile);
$backups->create();
}
// authorize master for file modification
$api->authorizeUpdate($pofile);
// recompile binary if it exists
$mofile = $pofile->cloneExtension('mo');
if( $mofile->exists() ){
$mofile->putContents( $data->msgfmt() );
}
// replacing source file last in case of failures
$pofile->putContents( $source );
Loco_error_AdminNotices::success( __('File restored','loco-translate') );
// prune to configured level after success
$backups->prune( $num_backups );
$backups = null;
}
// Delete
else if( $path = $post->delete ){
$target = new Loco_fs_File( $path );
$target->normalize( loco_constant('WP_CONTENT_DIR') );
$api->authorizeDelete( $target );
$target->unlink();
Loco_error_AdminNotices::success( __('File deleted','loco-translate') );
}
else {
throw new Loco_error_Exception('Nothing selected');
}
}
catch( Loco_error_Exception $e ){
Loco_error_AdminNotices::add( $e );
}
}
}
$bundle = $this->getBundle();
$this->set('title', sprintf( __('Restore %s','loco-translate'), $pofile->basename() ).' &lsaquo; '.$bundle->getName() );
}
/**
* {@inheritdoc}
*/
public function render(){
$file = $this->get('file');
if( $fail = $this->getFileError($file) ){
return $fail;
}
$info = Loco_mvc_FileParams::create($file);
$info['mtime'] = $file->modified();
$this->set( 'master', $info );
$this->set( 'title', sprintf( __('Restore %s','loco-translate'), $info->name ) );
$enabled = Loco_data_Settings::get()->num_backups;
$this->set( 'enabled', $enabled );
$files = array();
$wp_content = loco_constant('WP_CONTENT_DIR');
$paths = array( $file->getRelativePath($wp_content) );
$podate = 'pot' === $file->extension() ? 'POT-Creation-Date' : 'PO-Revision-Date';
$backups = new Loco_fs_Revisions($file);
foreach( $backups->getPaths() as $path ){
$tmp = new Loco_fs_File( $path );
$info = Loco_mvc_FileParams::create($tmp);
// time file was snapshotted is actually the time the next version was updated
// $info['mtime'] = $backups->getTimestamp($path);
// pull "real" update time, meaning when the revision was last updated as current version
try {
$head = Loco_gettext_Data::head($tmp)->getHeaders();
if( $value = $head->trimmed($podate) ){
$info['potime'] = Loco_gettext_Data::parseDate($value);
}
else {
throw new Loco_error_Exception('Backup has no '.$podate.' field');
}
}
catch( Exception $e ){
Loco_error_AdminNotices::debug( $e->getMessage() );
continue;
}
$paths[] = $tmp->getRelativePath($wp_content);
$files[] = $info;
}
// no backups = no restore
if( ! $files ){
return $this->view('admin/errors/no-backups');
}
/*/ warn if current backup settings aren't enough to restore without losing older revisions
$min = count($files) + 1;
if( $enabled < $min ){
$notice = Loco_error_AdminNotices::info('We recommend enabling more backups before restoring');
$notice->addLink( apply_filters('loco_external','https://localise.biz/wordpress/plugin/manual/settings#po'), __('Documentation','loco-translate') )
->addLink( Loco_mvc_AdminRouter::generate('config').'#loco--num-backups', __('Settings') );
}*/
// restore permissions required are create and delete on current location
$this->prepareFsConnect( 'update', $this->get('path') );
// prepare revision arguments for JavaScript
$this->set( 'js', new Loco_mvc_ViewParams( array(
'paths' => $paths,
'nonces' => array (
'diff' => wp_create_nonce('diff'),
)
) ) );
$this->enqueueScript('podiff');
return $this->view('admin/file/diff', compact('files','backups') );
}
}

View File

@@ -0,0 +1,205 @@
<?php
/**
* PO editor view
*/
class Loco_admin_file_EditController extends Loco_admin_file_BaseController {
/**
* {@inheritdoc}
*/
public function init(){
parent::init();
$this->enqueueStyle('editor');
//
$file = $this->get('file');
$bundle = $this->getBundle();
// translators: %1$s is the file name, %2$s is the bundle name
$this->set('title', sprintf( __('Editing %1$s in %2$s','loco-translate'), $file->basename(), $bundle ) );
}
/**
* {@inheritdoc}
*/
public function getHelpTabs(){
return array (
__('Overview','default') => $this->viewSnippet('tab-file-edit'),
);
}
/**
* {@inheritdoc}
*/
public function render(){
// file must exist for editing
$file = $this->get('file');
if( $fail = $this->getFileError($file) ){
return $fail;
}
// editor will be rendered
$this->enqueueScript('editor');
// Parse file data into JavaScript for editor
try {
$this->set('modified', $file->modified() );
$data = Loco_gettext_Data::load( $file );
}
catch( Exception $e ){
Loco_error_AdminNotices::add( Loco_error_Exception::convert($e) );
$data = Loco_gettext_Data::dummy();
}
$head = $data->getHeaders();
// default is to permit editing of any file
$readonly = false;
// Establish if file belongs to a configured project
try {
$bundle = $this->getBundle();
$project = $this->getProject();
}
// Fine if not, this just means sync isn't possible.
catch( Loco_error_Exception $e ){
Loco_error_AdminNotices::add( $e );
Loco_error_AdminNotices::debug( sprintf("Sync is disabled because this file doesn't relate to a known set of translations", $bundle ) );
$project = null;
}
// Establish PO/POT edit mode
if( $locale = $this->getLocale() ){
// alternative POT file may be forced by PO headers
if( $value = $head['X-Loco-Template'] ){
$potfile = new Loco_fs_File($value);
$potfile->normalize( $bundle->getDirectoryPath() );
}
// no way to get configured POT if invalid project
else if( is_null($project) ){
$potfile = null;
}
// else use project-configured template, assuming there is one
else if( $potfile = $project->getPot() ){
// Handle situation where project defines a localised file as the official template
if( $potfile->equal($file) ){
$locale = null;
$potfile = null;
}
}
if( $potfile ){
// Validate template file as long as it exists
if( $potfile->exists() ){
try {
$potdata = Loco_gettext_Data::load( $potfile );
}
catch( Exception $e ){
// translators: Where %s is the name of the invalid POT file
Loco_error_AdminNotices::warn( sprintf( __('Translation template is invalid (%s)','loco-translate'), $potfile->basename() ) );
$potfile = null;
}
if( $potfile && ! $potdata->equalSource($data) ){
Loco_error_AdminNotices::debug( sprintf( __("Translations don't match template. Run sync to update from %s",'loco-translate'), $potfile->basename() ) );
}
}
// else template doesn't exist, so sync will be done to source code
else {
// Loco_error_AdminNotices::debug( sprintf( __('Template file not found (%s)','loco-translate'), $potfile->basename() ) );
$potfile = null;
}
}
if( $locale ){
// allow PO file to dictate its own Plural-Forms
try {
$locale->setPluralFormsHeader( $head['Plural-Forms'] );
}
catch( InvalidArgumentException $e ){
// ignore invalid Plural-Forms
}
// fill in missing PO headers now locale is fully resolved
$data->localize($locale);
// If MO file will be compiled, check for library/config problems
if ( 2 !== strlen( "\xC2\xA3" ) ) {
Loco_error_AdminNotices::warn('Your mbstring configuration will result in corrupt MO files. Please ensure mbstring.func_overload is disabled');
}
}
}
// notify if template is locked (save and sync will be disabled)
if( is_null($locale) && $project && $project->isPotLocked() ){
$this->set('fsDenied', true );
$readonly = true;
}
// back end expects paths relative to wp-content
$wp_content = loco_constant('WP_CONTENT_DIR');
$this->set( 'js', new Loco_mvc_ViewParams( array(
'podata' => $data->jsonSerialize(),
'powrap' => (int) Loco_data_Settings::get()->po_width,
'multipart' => (bool) Loco_data_Settings::get()->ajax_files,
'locale' => $locale ? $locale->jsonSerialize() : null,
'potpath' => $locale && $potfile ? $potfile->getRelativePath($wp_content) : null,
'popath' => $this->get('path'),
'readonly' => $readonly,
'project' => $project ? array (
'bundle' => $bundle->getId(),
'domain' => (string) $project->getId(),
) : null,
'nonces' => $readonly ? null : array (
'save' => wp_create_nonce('save'),
'sync' => wp_create_nonce('sync'),
),
) ) );
$this->set( 'ui', new Loco_mvc_ViewParams( array(
// Translators: button for adding a new string when manually editing a POT file
'add' => _x('Add','Editor','loco-translate'),
// Translators: button for removing a string when manually editing a POT file
'del' => _x('Remove','Editor','loco-translate'),
'help' => __('Help','loco-translate'),
// Translators: Button that saves translations to disk
'save' => _x('Save','Editor','loco-translate'),
// Translators: Button that runs in-editor sync/operation
'sync' => _x('Sync','Editor','loco-translate'),
// Translators: Button that reloads current screen
'revert' => _x('Revert','Editor','loco-translate'),
// Translators: Button that toggles a translation's Fuzzy flag
'fuzzy' => _x('Fuzzy','Editor','loco-translate'),
// Translators: Button for downloading a PO, MO or POT file
'download' => _x('Download','Editor','loco-translate'),
// Translators: Placeholder text for text filter above editor
'filter' => __('Filter translations','loco-translate'),
// Translators: Button that toggles invisible characters
'invs' => _x('Toggle invisibles','Editor','loco-translate'),
// Translators: Button that toggles between "code" and regular text editing modes
'code' => _x('Toggle code view','Editor','loco-translate'),
) ) );
// Download form params
$hidden = new Loco_mvc_HiddenFields( array(
'path' => '',
'source' => '',
'route' => 'download',
'action' => 'loco_download',
) );
$this->set( 'dlFields', $hidden->setNonce('download') );
$this->set( 'dlAction', admin_url('admin-ajax.php','relative') );
// Remote file system required if file is not directly writable
$this->prepareFsConnect( 'update', $this->get('path') );
// set simpler title for breadcrumb
$this->set('title', $file->basename() );
// ok to render editor as either po or pot
$tpl = $locale ? 'po' : 'pot';
return $this->view( 'admin/file/edit-'.$tpl, array() );
}
}

View File

@@ -0,0 +1,201 @@
<?php
/**
* File info / management view.
*/
class Loco_admin_file_InfoController extends Loco_admin_file_BaseController {
/**
* {@inheritdoc}
*/
public function init(){
parent::init();
$this->enqueueStyle('fileinfo');
//
$file = $this->get('file');
$bundle = $this->getBundle();
$this->set('title', $file->basename().' &lsaquo; '.$bundle->getName() );
}
/**
* {@inheritdoc}
*/
public function getHelpTabs(){
return array (
__('Overview','default') => $this->viewSnippet('tab-file-info'),
);
}
/**
* {@inheritdoc}
*/
public function render(){
$file = $this->get('file');
$name = $file->basename();
$this->set('title', $name );
if( $fail = $this->getFileError($file) ){
return $fail;
}
// file info
$ext = strtolower( $file->extension() );
$finfo = Loco_mvc_FileParams::create( $file );
$this->set('file', $finfo );
$finfo['type'] = strtoupper($ext);
if( $file->exists() ){
$finfo['existent'] = true;
$finfo['writable'] = $file->writable();
$finfo['deletable'] = $file->deletable();
$finfo['mtime'] = $file->modified();
// Notify if file is managed by WordPress
$api = new Loco_api_WordPressFileSystem;
if( $api->isAutoUpdatable($file) ){
$finfo['autoupdate'] = true;
}
}
// location info
$dir = new Loco_fs_LocaleDirectory( $file->dirname() );
$dinfo = Loco_mvc_FileParams::create( $dir );
$this->set('dir', $dinfo );
$dinfo['type'] = $dir->getTypeId();
if( $dir->exists() && $dir->isDirectory() ){
$dinfo['existent'] = true;
$dinfo['writable'] = $dir->writable();
}
// collect note worthy problems with file headers
$debugging = loco_debugging();
$debug = array();
// get the name of the webserver for information purposes
$this->set('httpd', Loco_compat_PosixExtension::getHttpdUser() );
// unknown file template if required
$locale = null;
$project = null;
$tpl = 'admin/file/info-other';
// we should know the project the file belongs to, but permitting orphans for debugging
try {
$project = $this->getProject();
$template = $project->getPot();
$isTemplate = $template && $file->equal($template);
$this->set('isTemplate', $isTemplate );
$this->set('project', $project );
}
catch( Loco_error_Exception $e ){
$debug[] = $e->getMessage();
$isTemplate = false;
$template = null;
}
// file will be Gettext most likely
if( 'pot' === $ext || 'po' === $ext || 'mo' === $ext ){
// treat as template until locale verified
$tpl = 'admin/file/info-pot';
// don't attempt to pull locale of template file
if( 'pot' !== $ext && ! $isTemplate ){
$locale = $file->getLocale();
$code = (string) $locale;
if( $locale->isValid() ){
// find PO/MO counter parts
if( 'po' === $ext ){
$tpl = 'admin/file/info-po';
$sibling = $file->cloneExtension('mo');
}
else {
$tpl = 'admin/file/info-mo';
$sibling = $file->cloneExtension('po');
}
$info = Loco_mvc_FileParams::create($sibling);
$this->set( 'sibling', $info );
if( $sibling->exists() ){
$info['existent'] = true;
$info['writable'] = $sibling->writable();
}
}
}
// Do full parse to get stats and headers
try {
$data = Loco_gettext_Data::load($file);
$head = $data->getHeaders();
$author = $head->trimmed('Last-Translator') or $author = __('Unknown author','loco-translate');
$this->set( 'author', $author );
// date headers may not be same as file modification time (files copied to server etc..)
$podate = $head->trimmed( $locale ? 'PO-Revision-Date' : 'POT-Creation-Date' );
$potime = Loco_gettext_Data::parseDate($podate) or $potime = $file->modified();
$this->set('potime', $potime );
// access to meta stats, normally cached on listing pages
$meta = Loco_gettext_Metadata::create($file,$data);
$this->set( 'meta', $meta );
// allow PO header to specify alternative template for sync
if( $head->has('X-Loco-Template') ){
$altpot = new Loco_fs_File($head['X-Loco-Template']);
$altpot->normalize( $this->getBundle()->getDirectoryPath() );
if( $altpot->exists() && ( ! $template || ! $template->equal($altpot) ) ){
$template = $altpot;
}
}
// establish whether PO is in sync with POT
if( $template && ! $isTemplate && 'po' === $ext && $template->exists() ){
try {
$this->set('potfile', new Loco_mvc_FileParams( array(
'synced' => Loco_gettext_Data::load($template)->equalSource($data),
), $template ) );
}
catch( Exception $e ){
// ignore invalid template in this context
}
}
if( $debugging ){
// missing or invalid headers are tollerated but developers should be notified
if( $debugging && ! count($head) ){
$debug[] = __('File does not have a valid header','loco-translate');
}
// Language header sanity checks, raising developer (debug) warnings
if( $locale ){
if( $value = $head['Language'] ){
$check = (string) Loco_Locale::parse($value);
if( $check !== $code ){
$debug[]= sprintf( __('Language header is "%s" but file name contains "%s"','loco-translate'), $value, $code );
}
}
if( $value = $head['Plural-Forms'] ){
try {
$locale->setPluralFormsHeader($value);
}
catch( InvalidArgumentException $e ){
$debug[] = sprintf('Plural-Forms header is invalid, "%s"',$value);
}
}
}
// Other sanity checks
if( $project && ( $value = $head['Project-Id-Version'] ) && $value !== $project->getName() ){
$debug[] = sprintf('Project-Id-Version header is "%s" but project is "%s"', $value, $project );
}
}
// Count source text for templates only (assumed English)
if( 'admin/file/info-pot' === $tpl ){
$counter = new Loco_gettext_WordCount($data);
$this->set('words', $counter->count() );
}
}
catch( Loco_error_Exception $e ){
$this->set('error', $e->getMessage() );
$tpl = 'admin/file/info-other';
}
}
if( $debugging && $debug ){
$this->set( 'debug', new Loco_mvc_ViewParams($debug) );
}
return $this->view( $tpl );
}
}

View File

@@ -0,0 +1,185 @@
<?php
/**
* Translation set relocation tool.
* Moves PO/MO pair and all related files to a new location
*/
class Loco_admin_file_MoveController extends Loco_admin_file_BaseController {
/**
* {@inheritdoc}
*/
public function init(){
parent::init();
$file = $this->get('file');
/* @var Loco_fs_File $file */
if( $file->exists() && ! $file->isDirectory() ){
$files = new Loco_fs_Siblings($file);
// nonce action will be specific to file for extra security
$path = $file->getPath();
$action = 'move:'.$path;
// set up view now in case of late failure
$fields = new Loco_mvc_HiddenFields( array() );
$fields->setNonce( $action );
$fields['auth'] = 'move';
$fields['path'] = $this->get('path');
$this->set('hidden',$fields );
// attempt move if valid nonce posted back
while( $this->checkNonce($action) ){
// Chosen location should be valid as a posted "dest" parameter
if( ! Loco_mvc_PostParams::get()->has('dest') ){
Loco_error_AdminNotices::err('No destination posted');
break;
}
$target = new Loco_fs_LocaleFile( Loco_mvc_PostParams::get()->dest );
$ext = $target->extension();
// primary file extension should only be permitted to change between po and pot
if( $ext !== $file->extension() && 'po' !== $ext && 'pot' !== $ext ){
Loco_error_AdminNotices::err('Invalid file extension, *.po or *.pot only');
break;
}
$target->normalize( loco_constant('WP_CONTENT_DIR') );
$target_dir = $target->getParent()->getPath();
// Primary file gives template remapping, so all files are renamed with same stub.
// this can only be one of three things: (en -> en) or (foo-en -> en) or (en -> foo-en)
// suffix will then consist of file extension, plus any other stuff like backup file date.
$target_base = $target->filename();
$source_snip = strlen( $file->filename() );
// buffer all files to move to preempt write failures
$movable = array();
$api = new Loco_api_WordPressFileSystem;
foreach( $files->expand() as $source ){
$suffix = substr( $source->basename(), $source_snip ); // <- e.g. "-backup.po~"
$target = new Loco_fs_File( $target_dir.'/'.$target_base.$suffix );
// permit valid change of file extension on primary source file (po/pot)
if( $source === $files->getSource() && $target->extension() !== $ext ){
$target = $target->cloneExtension($ext);
}
if( ! $api->authorizeMove($source,$target) ) {
Loco_error_AdminNotices::err('Failed to authorize relocation of '.$source->basename() );
break 2;
}
$movable[] = array($source,$target);
}
// commit moves. If any fail we'll have separated the files, which is bad
$count = 0;
$total = count($movable);
foreach( $movable as $pair ){
try {
$pair[0]->move( $pair[1] );
$count++;
}
catch( Loco_error_Exception $e ){
Loco_error_AdminNotices::add($e);
}
}
// flash messages for display after redirect
try {
if( $count ) {
Loco_data_Session::get()->flash( 'success', sprintf( _n( 'File moved', '%u files moved', $total, 'loco-translate' ), $total ) );
}
if( $total > $count ){
$diff = $total - $count;
Loco_data_Session::get()->flash( 'error', sprintf( _n( 'One file could not be moved', '%u files could not be moved', $diff, 'loco-translate' ), $diff ) );
}
Loco_data_Session::close();
}
catch( Exception $e ){
// tolerate session failure
}
// redirect to bundle overview
$href = Loco_mvc_AdminRouter::generate( $this->get('type').'-view', array( 'bundle' => $this->get('bundle') ) );
if( wp_redirect($href) ){
exit;
}
break;
}
}
// set page title before render sets inline title
$bundle = $this->getBundle();
$this->set('title', sprintf( __('Move %s','loco-translate'), $file->basename() ).' &lsaquo; '.$bundle->getName() );
}
/**
* {@inheritdoc}
*/
public function render(){
$file = $this->get('file');
if( $fail = $this->getFileError($file) ){
return $fail;
}
// relocation requires knowing text domain and locale
try {
$project = $this->getProject();
}
catch( Loco_error_Exception $e ){
Loco_error_AdminNotices::warn($e->getMessage());
$project = null;
}
$files = new Loco_fs_Siblings($file);
$file = new Loco_fs_LocaleFile( $files->getSource() );
$locale = $file->getLocale();
// switch between canonical move and custom file path mode
$custom = is_null($project) || $this->get('custom') || 'po' !== $file->extension() || ! $locale->isValid();
// common page elements:
$this->set('files',$files->expand() );
$this->set('title', sprintf( __('Move %s','loco-translate'), $file->filename() ) );
$this->enqueueScript('move');
// set info for existing file location
$content_dir = loco_constant('WP_CONTENT_DIR');
$current = $file->getRelativePath($content_dir);
$parent = new Loco_fs_LocaleDirectory( $file->dirname() );
$typeId = $parent->getTypeId();
$this->set('current', new Loco_mvc_ViewParams(array(
'path' => $parent->getRelativePath($content_dir),
'type' => $parent->getTypeLabel($typeId),
)) );
// moving files will require deletion permission on current file location
// plus write permission on target location, but we don't know what that is yet.
$fields = $this->prepareFsConnect('move',$current);
$fields['path'] = '';
$fields['dest'] = '';
// custom file move template (POT mode)
if( $custom ){
$this->get('hidden')->offsetSet('custom','1');
$this->set('file', Loco_mvc_FileParams::create($file) );
return $this->view('admin/file/move-pot');
}
// establish valid locations for translation set, which may include current:
$filechoice = $project->initLocaleFiles($locale);
// start with current location so always first in list
$locations = array();
$locations[$typeId] = new Loco_mvc_ViewParams( array(
'label' => $parent->getTypeLabel($typeId),
'paths' => array( new Loco_mvc_ViewParams( array(
'path' => $current,
'active' => true,
) ) )
) );
/* @var Loco_fs_File $pofile */
foreach( $filechoice as $pofile ){
$relpath = $pofile->getRelativePath($content_dir);
if( $current === $relpath ){
continue;
}
// initialize location type (system, etc..)
$parent = new Loco_fs_LocaleDirectory( $pofile->dirname() );
$typeId = $parent->getTypeId();
if( ! isset($locations[$typeId]) ){
$locations[$typeId] = new Loco_mvc_ViewParams( array(
'label' => $parent->getTypeLabel($typeId),
'paths' => array(),
) );
}
$choice = new Loco_mvc_ViewParams( array(
'path' => $relpath,
) );
$locations[$typeId]['paths'][] = $choice;
}
$this->set('locations', $locations );
$this->set('advanced', $_SERVER['REQUEST_URI'].'&custom=1' );
return $this->view('admin/file/move-po');
}
}

View File

@@ -0,0 +1,101 @@
<?php
/**
* File view / source formatted view.
*/
class Loco_admin_file_ViewController extends Loco_admin_file_BaseController {
/**
* {@inheritdoc}
*/
public function init(){
parent::init();
$this->enqueueStyle('poview');
//
$file = $this->get('file');
$bundle = $this->getBundle();
$this->set( 'title', 'Source of '.$file->basename().' &lsaquo; '.$bundle->getName() );
}
/**
* {@inheritdoc}
*/
public function getHelpTabs(){
return array (
__('Overview','default') => $this->viewSnippet('tab-file-view'),
);
}
/**
* {@inheritdoc}
*/
public function render(){
// file must exist for editing
/* @var Loco_fs_File $file */
$file = $this->get('file');
$name = $file->basename();
$type = strtolower( $file->extension() );
$this->set('title', $name );
if( $fail = $this->getFileError($file) ){
return $fail;
}
// Establish if file belongs to a configured project
try {
$bundle = $this->getBundle();
$project = $this->getProject();
}
catch( Exception $e ){
$project = null;
}
// Parse data before rendering, so we know it's a valid Gettext format
try {
$this->set('modified', $file->modified() );
$data = Loco_gettext_Data::load( $file );
}
catch( Loco_error_ParseException $e ){
Loco_error_AdminNotices::add( Loco_error_Exception::convert($e) );
$data = Loco_gettext_Data::dummy();
}
$this->set( 'meta', Loco_gettext_Metadata::create($file, $data) );
// binary MO will be hex-formatted in template
if( 'mo' === $type ){
$this->set('bin', $file->getContents() );
return $this->view('admin/file/view-mo' );
}
// else is a PO or POT file
$this->enqueueScript('poview');//->enqueueScript('min/highlight');
$lines = preg_split('/(?:\\n|\\r\\n?)/', Loco_gettext_Data::ensureUtf8( $file->getContents() ) );
$this->set( 'lines', $lines );
// ajax parameters required for pulling reference sources
$this->set('js', new Loco_mvc_ViewParams( array (
'popath' => $this->get('path'),
'nonces' => array(
'fsReference' => wp_create_nonce('fsReference'),
),
'project' => $bundle ? array (
'bundle' => $bundle->getId(),
'domain' => $project ? $project->getId() : '',
) : null,
) ) );
// treat as PO if file name has locale
if( $this->getLocale() ){
return $this->view('admin/file/view-po' );
}
// else view as POT
return $this->view('admin/file/view-pot' );
}
}

View File

@@ -0,0 +1,327 @@
<?php
/**
* pre-msginit function. Prepares arguments for creating a new PO language file
*/
class Loco_admin_init_InitPoController extends Loco_admin_bundle_BaseController {
/**
* {@inheritdoc}
*/
public function init(){
parent::init();
$this->enqueueStyle('poinit');
//
$bundle = $this->getBundle();
$this->set('title', __('New language','loco-translate').' &lsaquo; '.$bundle );
}
/**
* {@inheritdoc}
*/
public function getHelpTabs(){
return array (
__('Overview','default') => $this->viewSnippet('tab-init-po'),
);
}
/**
* Sort to the left the best option for saving new translation files
* @return Loco_mvc_ViewParams
*/
private function sortPreferred( array $choices ){
usort( $choices, array(__CLASS__,'_onSortPreferred') );
$best = current( $choices );
if( $best && ! $best['disabled'] ){
return $best;
}
}
/**
* @internal
*/
public static function _onSortPreferred( Loco_mvc_ViewParams $a, Loco_mvc_ViewParams $b ){
$x = self::scoreFileChoice($a);
$y = self::scoreFileChoice($b);
return $x === $y ? 0 : ( $x > $y ? -1 : 1 );
}
/**
* Score an individual file choice for sorting preferred
* @return int
*/
private static function scoreFileChoice( Loco_mvc_ViewParams $p ){
$score = 0;
if( $p['writable'] ){
$score++;
}
if( $p['disabled'] ){
$score -= 2;
}
if( $p['systype'] ){
$score--;
}
return $score;
}
/**
* @internal
*/
public static function _onSortLocationKeys( $a, $b ){
static $order = array('custom' => 4, 'wplang' => 3, 'theme' => 2, 'plugin' => 2, 'other' => 1 );
$x = $order[$a];
$y = $order[$b];
return $x === $y ? 0 : ( $x > $y ? -1 : 1 );
}
/**
* {@inheritdoc}
*/
public function render(){
$breadcrumb = $this->prepareNavigation();
// "new" tab is confusing when no project-scope navigation
// $this->get('tabs')->add( __('New PO','loco-translate'), '', true );
// bundle mandatory, but project optional
$bundle = $this->getBundle();
try {
$project = $this->getProject();
$slug = $project->getSlug();
$domain = (string) $project->getDomain();
$subhead = sprintf( __('Initializing new translations in "%s"','loco-translate'), $slug?$slug:$domain );
}
catch( Loco_error_Exception $e ){
$project = null;
$subhead = __('Initializing new translations in unknown set','loco-translate');
}
$title = __('New language','loco-translate');
$this->set('subhead', $subhead );
// navigate up to bundle listing page
$breadcrumb->add( $title );
$this->set( 'breadcrumb', $breadcrumb );
// default locale is a placeholder
$locale = new Loco_Locale('zxx');
$content_dir = untrailingslashit( loco_constant('WP_CONTENT_DIR') );
$copying = false;
// Permit using any provided file a template instead of POT
if( $potpath = $this->get('path') ){
$potfile = new Loco_fs_LocaleFile($potpath);
$potfile->normalize( $content_dir );
if( ! $potfile->exists() ){
throw new Loco_error_Exception('Forced template argument must exist');
}
$copying = true;
// forced source could be a POT (although UI would normally prevent it)
if( $potfile->getSuffix() ){
$locale = $potfile->getLocale();
$this->set('sourceLocale', $locale );
}
}
// else project not configured. UI should prevent this by not offering msginit
else if( ! $project ){
throw new Loco_error_Exception('Cannot add new language to unconfigured set');
}
// else POT file may or may not be known, and may or may not exist
else {
$potfile = $project->getPot();
}
$locales = array();
$installed = array();
$api = new Loco_api_WordPressTranslations;
// pull installed list first, this will include en_US and any non-standard languages installed
foreach( $api->getInstalledCore() as $tag ){
$locale = Loco_Locale::parse($tag);
if( $locale->isValid() ){
$tag = (string) $tag;
// We may not have names for these, so just the language tag will show
$installed[$tag] = new Loco_mvc_ViewParams( array(
'value' => $tag,
'icon' => $locale->getIcon(),
'label' => $locale->ensureName($api),
) );
}
}
// pull the same list of "available" languages as used in WordPress settings
/* @var $locale Loco_Locale */
foreach( $api->getAvailableCore() as $tag => $locale ){
if( ! array_key_exists($tag,$installed) ){
$locales[$tag] = new Loco_mvc_ViewParams( array(
'value' => $tag,
'icon' => $locale->getIcon(),
'label' => $locale->ensureName($api),
) );
}
}
// two locale lists built for "installed" and "available" dropdowns
$this->set( 'locales', $locales );
$this->set( 'installed', $installed );
// Critical that user selects the correct save location:
if( $project ){
$filechoice = $project->initLocaleFiles( $locale );
}
// without configured project we will only allow save to same location
else {
$filechoice = new Loco_fs_FileList;
}
// show information about POT file if we are initializing from template
if( $potfile && $potfile->exists() ){
$meta = Loco_gettext_Metadata::load($potfile);
$total = $meta->getTotal();
$summary = sprintf( _n('One string found in %2$s','%s strings found in %s',$total,'loco-translate'), number_format($total), $potfile->basename() );
$this->set( 'pot', new Loco_mvc_ViewParams( array(
'name' => $potfile->basename(),
'path' => $meta->getPath(false),
) ) );
// if copying an existing PO file, we can fairly safely establish the correct prefixing
if( $copying ){
$poname = ( $prefix = $potfile->getPrefix() ) ? sprintf('%s-%s.po',$prefix,$locale) : sprintf('%s.po',$locale);
$pofile = new Loco_fs_LocaleFile( $poname );
$pofile->normalize( $potfile->dirname() );
$filechoice->add( $pofile );
}
/// else if POT is in a folder we don't know about, we may as well add to the choices
// TODO this means another utility function in project for prefixing rules on individual location
}
// else no template exists, so we prompt to extract from source
else {
$this->set( 'ext', new Loco_mvc_ViewParams( array(
'link' => Loco_mvc_AdminRouter::generate( $this->get('type').'-xgettext', $_GET ),
'text' => __('Create template','loco-translate'),
) ) );
// if forcing source extraction show brief description of source files
if( $this->get('extract') ){
// Tokenizer required for string extraction
if( ! loco_check_extension('tokenizer') ){
return $this->view('admin/errors/no-tokenizer');
}
$nfiles = count( $project->findSourceFiles() );
$summary = sprintf( _n('1 source file will be scanned for translatable strings','%s source files will be scanned for translatable strings',$nfiles,'loco-translate'), number_format_i18n($nfiles) );
}
// else prompt for template creation before continuing
else {
$this->set( 'skip', new Loco_mvc_ViewParams( array(
'link' => Loco_mvc_AdminRouter::generate( $this->get('_route'), $_GET + array( 'extract' => '1' ) ),
'text' => __('Skip template','loco-translate'),
) ) );
// POT could still be defined, it might just not exist yet
if( $potfile ){
$this->set('pot', Loco_mvc_FileParams::create($potfile) );
}
// else offer assignment of a new file
else {
$this->set( 'conf', new Loco_mvc_ViewParams( array(
'link' => Loco_mvc_AdminRouter::generate( $this->get('type').'-conf', array_intersect_key($_GET,array('bundle'=>'')) ),
'text' => __('Assign template','loco-translate'),
) ) );
}
return $this->view('admin/init/init-prompt');
}
}
$this->set( 'summary', $summary );
// group established locations into types (official, etc..)
// there is no point checking whether any of these file exist, because we don't know what language will be chosen yet.
$sortable = array();
$locations = array();
$fs_protect = Loco_data_Settings::get()->fs_protect;
$fs_failure = null;
/* @var Loco_fs_File $pofile */
foreach( $filechoice as $pofile ){
$parent = new Loco_fs_LocaleDirectory( $pofile->dirname() );
$systype = $parent->getUpdateType();
$typeId = $parent->getTypeId();
if( ! isset($locations[$typeId]) ){
$locations[$typeId] = new Loco_mvc_ViewParams( array(
'label' => $parent->getTypeLabel( $typeId ),
'paths' => array(),
) );
}
// folder may be unwritable (requiring connect to create file) or may be denied under security settings
try {
$context = $parent->getWriteContext()->authorize();
$writable = $context->writable();
$disabled = false;
}
catch( Loco_error_WriteException $e ){
$fs_failure = $e->getMessage();
$writable = false;
$disabled = true;
}
$choice = new Loco_mvc_ViewParams( array (
'checked' => '',
'writable' => $writable,
'disabled' => $disabled,
'systype' => $systype,
'parent' => Loco_mvc_FileParams::create( $parent ),
'hidden' => $pofile->getRelativePath($content_dir),
'holder' => str_replace( (string) $locale, '<span>&lt;locale&gt;</span>', $pofile->basename() ),
) );
// may need to show system file warnings
if( $systype && $fs_protect ){
$choice['syswarn'] = true;
}
$sortable[] = $choice;
$locations[$typeId]['paths'][] = $choice;
}
// display locations in runtime preference order
uksort( $locations, array(__CLASS__,'_onSortLocationKeys') );
$this->set( 'locations', $locations );
// pre-select best (safest/writable) option
if( $preferred = $this->sortPreferred( $sortable ) ){
$preferred['checked'] = 'checked';
}
// else show total lock message. probably file mods disallowed
else if( $fs_failure ){
$this->set('fsLocked', $fs_failure );
}
// hidden fields to pass through to Ajax endpoint
$this->set('hidden', new Loco_mvc_HiddenFields( array(
'action' => 'loco_json',
'route' => 'msginit',
'loco-nonce' => $this->setNonce('msginit')->value,
'type' => $bundle->getType(),
'bundle' => $bundle->getHandle(),
'domain' => $project ? $project->getId() : '',
'source' => $potpath,
) ) );
$this->set('help', new Loco_mvc_ViewParams( array(
'href' => apply_filters('loco_external','https://localise.biz/wordpress/plugin/manual/msginit'),
'text' => __("What's this?",'loco-translate'),
) ) );
// file system prompts will be handled when paths are selected (i.e. we don't have one yet)
$this->prepareFsConnect( 'create', '' );
$this->enqueueScript('poinit');
return $this->view( 'admin/init/init-po', array() );
}
}

View File

@@ -0,0 +1,151 @@
<?php
/**
* pre-xgettext function. Initializes a new PO file for a given locale
*/
class Loco_admin_init_InitPotController extends Loco_admin_bundle_BaseController {
/**
* {@inheritdoc}
*/
public function init(){
parent::init();
$this->enqueueStyle('poinit');
//
$bundle = $this->getBundle();
$this->set('title', __('New template','loco-translate').' &lsaquo; '.$bundle );
}
/**
* {@inheritdoc}
*/
public function getHelpTabs(){
return array (
__('Overview','default') => $this->viewSnippet('tab-init-pot'),
);
}
/**
* {@inheritdoc}
*/
public function render(){
$breadcrumb = $this->prepareNavigation();
// "new" tab is confusing when no project-scope navigation
// $this->get('tabs')->add( __('New POT','loco-translate'), '', true );
$bundle = $this->getBundle();
$project = $this->getProject();
$slug = $project->getSlug();
$domain = (string) $project->getDomain();
$this->set('domain', $domain );
// Tokenizer required for string extraction
if( ! loco_check_extension('tokenizer') ){
return $this->view('admin/errors/no-tokenizer');
}
// Establish default POT path whether it exists or not
$pot = $project->getPot();
while( ! $pot ){
$name = ( $slug ? $slug : $domain ).'.pot';
/* @var $dir Loco_fs_Directory */
foreach( $project->getConfiguredTargets() as $dir ){
$pot = new Loco_fs_File( $dir->getPath().'/'.$name );
break 2;
}
// unlikely to have no configured targets, but possible ... so default to standard
$pot = new Loco_fs_File( $bundle->getDirectoryPath().'/languages/'.$name );
break;
}
// POT should actually not exist at this stage. It should be edited instead.
if( $pot->exists() ){
throw new Loco_error_Exception( __('Template file already exists','loco-translate') );
}
// Bundle may deliberately lock template to avoid end-user tampering
// it makes little sense to do so when template doesn't exist, but we will honour the setting anyway.
if( $project->isPotLocked() ){
throw new Loco_error_Exception('Template is protected from updates by the bundle configuration');
}
// Just warn if POT writing will fail when saved, but still show screen
$dir = $pot->getParent();
// Avoiding full source scan until actioned, but calculate size to manage expectations
$bytes = 0;
$nfiles = 0;
$nskip = 0;
$largest = 0;
$sources = $project->findSourceFiles();
// skip files larger than configured maximum
$opts = Loco_data_Settings::get();
$max = wp_convert_hr_to_bytes( $opts->max_php_size );
/* @var $sourceFile Loco_fs_File */
foreach( $sources as $sourceFile ){
$nfiles++;
$fsize = $sourceFile->size();
$largest = max( $largest, $fsize );
if( $fsize > $max ){
$nskip += 1;
// uncomment to log which files are too large to be scanned
// Loco_error_AdminNotices::debug( sprintf('%s is %s',$sourceFile,Loco_mvc_FileParams::renderBytes($fsize)) );
}
else {
$bytes += $fsize;
}
}
$this->set( 'scan', new Loco_mvc_ViewParams( array (
'bytes' => $bytes,
'count' => $nfiles,
'skip' => $nskip,
'size' => Loco_mvc_FileParams::renderBytes($bytes),
'large' => Loco_mvc_FileParams::renderBytes($max),
'largest' => Loco_mvc_FileParams::renderBytes($largest),
) ) );
// file metadata
$this->set('pot', Loco_mvc_FileParams::create( $pot ) );
$this->set('dir', Loco_mvc_FileParams::create( $dir ) );
$title = __('New template file','loco-translate');
$subhead = sprintf( __('New translations template for "%s"','loco-translate'), $project );
$this->set('subhead', $subhead );
// navigate up to bundle listing page
$breadcrumb->add( $title );
$this->set( 'breadcrumb', $breadcrumb );
// ajax service takes the target directory path
$content_dir = loco_constant('WP_CONTENT_DIR');
$target_path = $pot->getParent()->getRelativePath($content_dir);
// hidden fields to pass through to Ajax endpoint
$this->set( 'hidden', new Loco_mvc_ViewParams( array(
'action' => 'loco_json',
'route' => 'xgettext',
'loco-nonce' => $this->setNonce('xgettext')->value,
'type' => $bundle->getType(),
'bundle' => $bundle->getHandle(),
'domain' => $project->getId(),
'path' => $target_path,
'name' => $pot->basename(),
) ) );
// File system connect required if location not writable
$relpath = $pot->getRelativePath($content_dir);
$this->prepareFsConnect('create', $relpath );
$this->enqueueScript('potinit');
return $this->view( 'admin/init/init-pot' );
}
}

View File

@@ -0,0 +1,86 @@
<?php
/**
* Common controller for listing of all bundle types
*/
abstract class Loco_admin_list_BaseController extends Loco_mvc_AdminController {
private $bundles = array();
/**
* build renderable bundle variables
* @return Loco_mvc_ViewParams
*/
protected function bundleParam( Loco_package_Bundle $bundle ){
$handle = $bundle->getHandle();
// compatibility will be 'ok', 'warn' or 'error' depending on severity
if( $default = $bundle->getDefaultProject() ){
$compat = $default->getPot() instanceof Loco_fs_File;
}
else {
$compat = false;
}
//$info = $bundle->getHeaderInfo();
return new Loco_mvc_ViewParams( array (
'id' => $bundle->getId(),
'name' => $bundle->getName(),
'dflt' => $default ? $default->getDomain() : '--',
'size' => count( $bundle ),
'save' => $bundle->isConfigured(),
'type' => $type = strtolower( $bundle->getType() ),
'view' => Loco_mvc_AdminRouter::generate( $type.'-view', array( 'bundle' => $handle ) ),
'time' => $bundle->getLastUpdated(),
) );
}
/**
* Add bundle to enabled or disabled list, depending on whether it is configured
*/
protected function addBundle( Loco_package_Bundle $bundle ){
$this->bundles[] = $this->bundleParam($bundle);
}
/**
* {@inheritdoc}
*/
public function getHelpTabs(){
return array (
__('Overview','default') => $this->viewSnippet('tab-list-bundles'),
);
}
/**
* {@inheritdoc}
*/
public function render(){
// breadcrumb is just the root
$here = new Loco_admin_Navigation( array (
new Loco_mvc_ViewParams( array( 'name' => $this->get('title') ) ),
) );
/*/ tab between the types of bundles
$types = array (
'' => __('Home','loco-translate'),
'theme' => __('Themes','loco-translate'),
'plugin' => __('Plugins','loco-translate'),
);
$current = $this->get('_route');
$tabs = new Loco_admin_Navigation;
foreach( $types as $type => $name ){
$href = Loco_mvc_AdminRouter::generate($type);
$tabs->add( $name, $href, $type === $current );
}
*/
return $this->view( 'admin/list/bundles', array (
'bundles' => $this->bundles,
'breadcrumb' => $here,
) );
}
}

View File

@@ -0,0 +1,16 @@
<?php
/**
* Dummy controller skips "core" list view, rendering the core projects directly as a single bundle.
* Route: loco-core -> loco-core-view
*/
class Loco_admin_list_CoreController extends Loco_admin_RedirectController {
/**
* {@inheritdoc}
*/
public function getLocation(){
return Loco_mvc_AdminRouter::generate('core-view');
}
}

View File

@@ -0,0 +1,109 @@
<?php
/**
* Lists all installed locales.
* WordPress decides what is "installed" based on presence of core translation files
*/
class Loco_admin_list_LocalesController extends Loco_mvc_AdminController {
/**
* {@inheritdoc}
*/
public function init(){
parent::init();
$this->enqueueStyle('locale');
}
/**
* {@inheritdoc}
*/
public function getHelpTabs(){
return array (
__('Overview','default') => $this->viewSnippet('tab-list-locales'),
);
}
/**
* {@inheritdoc}
*/
public function render(){
$this->set( 'title', __( 'Installed languages', 'loco-translate' ) );
$used = array();
$locales = array();
$api = new Loco_api_WordPressTranslations;
$active = get_locale();
// list which sites have each language as their WPLANG setting
if( $multisite = is_multisite() ){
$this->set('multisite',true);
/* @var WP_Site $site */
foreach( get_sites() as $site ){
$id = (int) $site->blog_id;
$tag = get_blog_option( $id, 'WPLANG') or $tag = 'en_US';
$name = get_blog_option( $id, 'blogname' );
$used[$tag][] = $name;
}
}
// else single site shows tick instead of site name
else {
$used[$active][] = '✓';
}
// add installed languages to file crawler
$finder = new Loco_package_Locale;
// Pull "installed" languages (including en_US)
foreach( $api->getInstalledCore() as $tag ){
$locale = Loco_Locale::parse($tag);
if( $locale->isValid() ){
$tag = (string) $locale;
$finder->addLocale($locale);
$args = array( 'locale' => $tag );
$locales[$tag] = new Loco_mvc_ViewParams( array(
'nfiles' => 0,
'time' => 0,
'lcode' => $tag,
'lname' => $locale->ensureName($api),
'lattr' => 'class="'.$locale->getIcon().'" lang="'.$locale->lang.'"',
'href' => Loco_mvc_AdminRouter::generate('lang-view',$args),
'used' => isset($used[$tag]) ? implode( ', ', $used[$tag] ) : ( $multisite ? '--' : '' ),
'active' => $active === $tag,
) );
}
}
$this->set('locales', $locales );
// Count up unique PO files
foreach( $finder->findLocaleFiles() as $file ){
if( preg_match('/(?:^|-)([_a-zA-Z]+).po$/', $file->basename(), $r ) ){
$locale = Loco_Locale::parse($r[1]);
if( $locale->isValid() ){
$tag = (string) $locale;
$locales[$tag]['nfiles']++;
$locales[$tag]['time'] = max( $locales[$tag]['time'], $file->modified() );
}
}
}
// POT files are in en_US locale
$tag = 'en_US';
foreach( $finder->findTemplateFiles() as $file ){
$locales[$tag]['nfiles']++;
$locales[$tag]['time'] = max( $locales[$tag]['time'], $file->modified() );
}
return $this->view( 'admin/list/locales' );
}
}

View File

@@ -0,0 +1,31 @@
<?php
/**
* List all bundles of type "plugin"
* Route: loco-plugin
*/
class Loco_admin_list_PluginsController extends Loco_admin_list_BaseController {
public function render(){
$this->set( 'type', 'plugin' );
$this->set( 'title', __( 'Translate plugins', 'loco-translate' ) );
foreach( Loco_package_Plugin::get_plugins() as $handle => $data ){
try {
$bundle = Loco_package_Plugin::create( $handle );
$this->addBundle($bundle);
}
// @codeCoverageIgnoreStart
catch( Exception $e ){
$bundle = new Loco_package_Plugin( $handle, $handle );
$this->addBundle( $bundle );
}
// @codeCoverageIgnoreEnd
}
return parent::render();
}
}

View File

@@ -0,0 +1,26 @@
<?php
/**
* List all bundles of type "theme"
* Route: loco-theme
*/
class Loco_admin_list_ThemesController extends Loco_admin_list_BaseController {
public function render(){
$this->set('type', 'theme' );
$this->set('title', __( 'Translate themes', 'loco-translate' ) );
/* @var $theme WP_Theme */
foreach( wp_get_themes() as $theme ){
$bundle = Loco_package_Theme::create( $theme->get_stylesheet() );
$this->addBundle( $bundle );
}
return parent::render();
}
}

View File

@@ -0,0 +1,58 @@
<?php
/**
* Ajax "diff" route, for rendering PO/POT file diffs
*/
class Loco_ajax_DiffController extends Loco_mvc_AjaxController {
/**
* {@inheritdoc}
*/
public function render(){
$post = $this->validate();
// require x2 valid files for diffing
if( ! $post->lhs || ! $post->rhs ){
throw new InvalidArgumentException('Path parameters required');
}
$dir = loco_constant('WP_CONTENT_DIR');
$lhs = new Loco_fs_File( $post->lhs ); $lhs->normalize($dir);
$rhs = new Loco_fs_File( $post->rhs ); $rhs->normalize($dir);
// avoid diffing non Gettext source files
$exts = array_flip( array( 'pot', 'pot~', 'po', 'po~' ) );
/* @var $file Loco_fs_File */
foreach( array($lhs,$rhs) as $file ){
if( ! $file->exists() ){
throw new InvalidArgumentException('File paths must exist');
}
if( ! $file->underContentDirectory() ){
throw new InvalidArgumentException('Files must be under '.basename($dir) );
}
$ext = $file->extension();
if( ! isset($exts[$ext]) ){
throw new InvalidArgumentException('Disallowed file extension');
}
}
// OK to diff files as HTML table
$renderer = new Loco_output_DiffRenderer;
$emptysrc = $renderer->_startDiff().$renderer->_endDiff();
$tablesrc = $renderer->renderFiles( $rhs, $lhs );
if( $tablesrc === $emptysrc ){
// translators: Where %s is a file name
$message = __('Revisions are identical, you can delete %s','loco-translate');
$this->set( 'error', sprintf( $message, $rhs->basename() ) );
}
else {
$this->set( 'html', $tablesrc );
}
return parent::render();
}
}

View File

@@ -0,0 +1,35 @@
<?php
/**
* Downloads a bundle configuration as XML or Json
*/
class Loco_ajax_DownloadConfController extends Loco_ajax_common_BundleController {
/**
* {@inheritdoc}
*/
public function render(){
$this->validate();
$bundle = $this->getBundle();
$file = new Loco_fs_File( $this->get('path') );
// TODO should we download axtual loco.xml file if bundle is configured from it?
//$file->normalize( $bundle->getDirectoryPath() );
//if( $file->exists() ){}
$writer = new Loco_config_BundleWriter($bundle);
switch( $file->extension() ){
case 'xml':
return $writer->toXml();
case 'json':
return json_encode( $writer->jsonSerialize() );
}
// @codeCoverageIgnoreStart
throw new Loco_error_Exception('Specify either XML or JSON file path');
}
}

View File

@@ -0,0 +1,59 @@
<?php
/**
* Ajax "download" route, for outputting raw gettext file contents.
*/
class Loco_ajax_DownloadController extends Loco_mvc_AjaxController {
/**
* {@inheritdoc}
*/
public function render(){
$post = $this->validate();
// we need a path, but it may not need to exist
$file = new Loco_fs_File( $this->get('path') );
$file->normalize( loco_constant( 'WP_CONTENT_DIR') );
$is_binary = 'mo' === strtolower( $file->extension() );
// posted source must be clean and must parse as whatever the file extension claims to be
if( $raw = $post->source ){
// compile source if target is MO
if( $is_binary ) {
$raw = Loco_gettext_Data::fromSource($raw)->msgfmt();
}
}
// else file can be output directly if it exists.
// note that files on disk will not be parsed or manipulated. they will download strictly as-is
else if( $file->exists() ){
$raw = $file->getContents();
}
/*/ else if PO exists but MO doesn't, we can compile it on the fly
else if( ! $is_binary ){
}*/
else {
throw new Loco_error_Exception('File not found and no source posted');
}
// Observe UTF-8 BOM setting
if( ! $is_binary ){
$has_bom = "\xEF\xBB\xBF" === substr($raw,0,3);
$use_bom = (bool) Loco_data_Settings::get()->po_utf8_bom;
// only alter file if valid UTF-8. Deferring detection overhead until required
if( $has_bom !== $use_bom && 'UTF-8' === mb_detect_encoding( $raw, array('UTF-8','ISO-8859-1'), true ) ){
if( $use_bom ){
$raw = "\xEF\xBB\xBF".$raw; // prepend
}
else {
$raw = substr($raw,3); // strip bom
}
}
}
return $raw;
}
}

View File

@@ -0,0 +1,151 @@
<?php
/**
* Ajax service that provides remote server authentication for file system *write* operations
*/
class Loco_ajax_FsConnectController extends Loco_mvc_AjaxController {
/**
* @var Loco_api_WordPressFileSystem
*/
private $api;
/**
* @param Loco_fs_File existing file path (must exist)
* @return bool
*/
private function authorizeDelete( Loco_fs_File $file ){
$files = new Loco_fs_Siblings($file);
// require remote authentication if at least one dependant file is not deletable directly
foreach( $files->expand() as $file ){
if( ! $this->api->authorizeDelete($file) ){
return false;
}
}
// else no dependants failed deletable test
return true;
}
/**
* @param Loco_fs_File file being moved (must exist)
* @param Loco_fs_File target path (should not exist)
* @return bool
*/
private function authorizeMove( Loco_fs_File $source, Loco_fs_File $target = null ){
return $this->api->authorizeMove($source,$target);
}
/**
* @param Loco_fs_File new file path (should not exist)
* @return bool
*/
private function authorizeCreate( Loco_fs_File $file ){
return $this->api->authorizeCreate($file);
}
/**
* @return bool
*/
private function authorizeUpdate( Loco_fs_File $file ){
if( ! $this->api->authorizeUpdate($file) ){
return false;
}
// if backups are enabled, we need to be able to create new files too (i.e. update parent directory)
if( Loco_data_Settings::get()->num_backups && ! $this->api->authorizeCopy($file) ){
return false;
}
// updating file may also recompile binary, which may or may not exist
$files = new Loco_fs_Siblings( $file );
if( $file = $files->getBinary() ){
return $this->api->authorizeSave($file);
}
// else no dependants to update
return true;
}
/**
* {@inheritdoc}
*/
public function render(){
// establish operation being authorized (create,delete,etc..)
$post = $this->validate();
$type = $post->auth;
$func = 'authorize'.ucfirst($type);
$auth = array( $this, $func );
if( ! is_callable($auth) ){
throw new Loco_error_Exception('Unexpected file operation');
}
// all auth methods require at least one file argument
$file = new Loco_fs_File( $post->path );
$base = loco_constant('WP_CONTENT_DIR');
$file->normalize($base);
$args = array($file);
// some auth methods also require a destination/target (move,copy,etc..)
if( $dest = $post->dest ){
$file = new Loco_fs_File($dest);
$file->normalize($base);
$args[] = $file;
}
// call auth method and respond with status and prompt HTML if connect required
try {
$this->api = new Loco_api_WordPressFileSystem;
if( call_user_func_array($auth,$args) ){
$this->set( 'authed', true );
$this->set( 'valid', $this->api->getOutputCredentials() );
$this->set( 'creds', $this->api->getInputCredentials() );
$this->set( 'method', $this->api->getFileSystem()->method );
$this->set( 'success', __('Connected to remote file system','loco-translate') );
// warning when writing to this location is risky (overwrites during wp update)
if( Loco_data_Settings::get()->fs_protect && $file->getUpdateType() ){
if( 'create' === $type ){
$message = __('This file may be overwritten or deleted when you update WordPress','loco-translate');
}
else if( 'delete' === $type ){
$message = __('This directory is managed by WordPress, be careful what you delete','loco-translate');
}
else if( 'move' === $type ){
$message = __('This directory is managed by WordPress. Removed files may be restored during updates','loco-translate');
}
else {
$message = __('Changes to this file may be overwritten or deleted when you update WordPress','loco-translate');
}
$this->set('warning',$message);
}
}
else if( $html = $this->api->getForm() ){
$this->set( 'authed', false );
$this->set( 'prompt', $html );
// supporting text based on file operation type explains why auth is required
if( 'create' === $type ){
$message = __('Creating this file requires permission','loco-translate');
}
else if( 'delete' === $type ){
$message = __('Deleting this file requires permission','loco-translate');
}
else if( 'move' === $type ){
$message = __('This move operation requires permission','loco-translate');
}
else {
$message = __('Saving this file requires permission','loco-translate');
}
// message is printed before default text, so needs delimiting.
$this->set('message',$message.'.');
}
else {
throw new Loco_error_Exception('Failed to get credentials form');
}
}
catch( Loco_error_WriteException $e ){
$this->set('authed', false );
$this->set('reason', $e->getMessage() );
}
return parent::render();
}
}

View File

@@ -0,0 +1,198 @@
<?php
/**
* Ajax service that returns source code for a given file system reference
* Currently this is only PHP, but could theoretically be any file type.
*/
class Loco_ajax_FsReferenceController extends Loco_ajax_common_BundleController {
/**
* @param string
* @return Loco_fs_File
*/
private function findSourceFile( $refpath ){
/*/ absolute file path means no search paths required
if( Loco_fs_File::abs($refpath) ){
$srcfile = new Loco_fs_File( $refpath );
if( $srcfile->exists() ){
return $srcfile;
}
}*/
// reference may be resolvable via referencing PO file's location
$pofile = new Loco_fs_File( $this->get('path') );
$pofile->normalize( loco_constant('WP_CONTENT_DIR') );
if( ! $pofile->exists() ){
throw new InvalidArgumentException('PO/POT file required to resolve reference');
}
$search = new Loco_gettext_SearchPaths;
$search->init($pofile);
if( $srcfile = $search->match($refpath) ){
return $srcfile;
}
// check against PO file location when no search paths or search paths failed
$srcfile = new Loco_fs_File($refpath);
$srcfile->normalize( $pofile->dirname() );
if( $srcfile->exists() ){
return $srcfile;
}
// reference may be resolvable via known project roots
try {
$bundle = $this->getBundle();
// Loco extractions will always be relative to bundle root
$srcfile = new Loco_fs_File( $refpath );
$srcfile->normalize( $bundle->getDirectoryPath() );
if( $srcfile->exists() ){
return $srcfile;
}
// check relative to parent theme root
if( $bundle->isTheme() && ( $parent = $bundle->getParent() ) ){
$srcfile = new Loco_fs_File( $refpath );
$srcfile->normalize( $parent->getDirectoryPath() );
if( $srcfile->exists() ){
return $srcfile;
}
}
// final attempt - search all project source roots
// TODO is there too large a risk of false positives? especially with files like index.php
/* @var $root Loco_fs_Directory */
/*foreach( $this->getProject($bundle)->getConfiguredSources() as $root ){
if( $root->isDirectory() ){
$srcfile = new Loco_fs_File( $refpath );
$srcfile->normalize( $root->getPath() );
if( $srcfile->exists() ){
return $srcfile;
}
}
}*/
}
catch( Loco_error_Exception $e ){
// permitted for there to be no bundle or project when viewing orphaned file
}
throw new Loco_error_Exception( sprintf('Failed to find source file matching "%s"',$refpath) );
}
/**
* {@inheritdoc}
*/
public function render(){
$post = $this->validate();
// at the very least we need a reference to examine
if( ! $post->has('ref') ){
throw new InvalidArgumentException('ref parameter required');
}
// reference must parse as <path>:<line>
$ref = $post->ref;
if( ! preg_match('/^(.+):(\\d+)$/', $ref, $r ) ){
throw new InvalidArgumentException('Invalid file reference, '.$ref );
}
// find file or fail
list( , $refpath, $refline ) = $r;
$srcfile = $this->findSourceFile($refpath);
// deny access to sensitive files
if( 'wp-config.php' === $srcfile->basename() ){
throw new InvalidArgumentException('File access disallowed');
}
// validate allowed source file types
$conf = Loco_data_Settings::get();
$ext = strtolower( $srcfile->extension() );
$allow = array_merge( array('php','js'), $conf->php_alias, $conf->jsx_alias );
if( ! in_array($ext,$allow,true) ){
throw new InvalidArgumentException('File extension disallowed, '.$ext );
}
// get file type from registered file extensions:
$type = $conf->ext2type( $ext );
$this->set('type', $type );
$this->set('line', (int) $refline );
$this->set('path', $srcfile->getRelativePath( loco_constant('WP_CONTENT_DIR') ) );
// source code will be HTML-tokenized into multiple lines
$code = array();
// observe the same size limits for source highlighting as for string extraction as tokenizing will use the same amount of juice
$maxbytes = wp_convert_hr_to_bytes( $conf->max_php_size );
// tokenizers require gettext utilities, easiest just to ping the extraction library
if( ! class_exists('Loco_gettext_Extraction',true) ){
throw new RuntimeException('Failed to load tokenizers'); // @codeCoverageIgnore
}
// PHP is the most likely format.
if( 'php' === $type && ( $srcfile->size() <= $maxbytes ) && loco_check_extension('tokenizer') ) {
$tokens = new LocoPHPTokens( token_get_all( $srcfile->getContents() ) );
}
else if( 'js' === $type ){
$tokens = new LocoJsTokens( $srcfile->getContents() );
}
else {
$tokens = null;
}
// highlighting on back end because tokenizer provides more control than highlight.js
if( $tokens instanceof LocoTokensInterface ){
$thisline = 1;
while( $tok = $tokens->advance() ){
if( is_array($tok) ){
// line numbers added in PHP 5.2.2 - WordPress minimum is 5.2.4
list( $t, $str, $startline ) = $tok;
$clss = token_name($t);
// tokens can span multiple lines (whitespace/html/comments)
$lines = preg_split('/\\R/', $str );
}
else {
// scalar symbol will always start on the line that the previous token ended on
$clss = 'T_NONE';
$lines = array( $tok );
$startline = $thisline;
}
// token can span multiple lines, so include only bytes on required line[s]
foreach( $lines as $i => $line ){
$thisline = $startline + $i;
$html = '<code class="'.$clss.'">'.htmlentities($line,ENT_COMPAT,'UTF-8').'</code>';
// append highlighted token to current line
$j = $thisline - 1;
if( isset($code[$j]) ){
$code[$j] .= $html;
}
else {
$code[$j] = $html;
}
}
}
}
// permit limited other file types, but without back end highlighting
else if( 'js' === $type || 'twig' === $type || 'php' === $type ){
foreach( preg_split( '/\\R/u', $srcfile->getContents() ) as $line ){
$code[] = '<code>'.htmlentities($line,ENT_COMPAT,'UTF-8').'</code>';
}
}
else {
throw new Loco_error_Exception( sprintf('%s source view not supported', $type) ); // @codeCoverageIgnore
}
if( ! isset($code[$refline-1]) ){
throw new Loco_error_Exception( sprintf('Line %u not in source file', $refline) );
}
$this->set( 'code', $code );
return parent::render();
}
}

View File

@@ -0,0 +1,132 @@
<?php
/**
* Ajax "msginit" route, for initializing new translation files
*/
class Loco_ajax_MsginitController extends Loco_ajax_common_BundleController {
/**
* @return Loco_Locale
*/
private function getLocale(){
if( $this->get('use-selector') ){
$tag = $this->get('select-locale');
}
else {
$tag = $this->get('custom-locale');
}
$locale = Loco_Locale::parse($tag);
if( ! $locale->isValid() ){
throw new Loco_error_LocaleException('Invalid locale');
}
return $locale;
}
/**
* {@inheritdoc}
*/
public function render(){
$post = $this->validate();
$bundle = $this->getBundle();
$project = $this->getProject( $bundle );
$domain = (string) $project->getDomain();
$locale = $this->getLocale();
$suffix = (string) $locale;
// The front end posts a template path, so we must replace the actual locale code
$base = loco_constant('WP_CONTENT_DIR');
$path = $post->path[ $post['select-path'] ];
// The request_filesystem_credentials function will try to access the "path" field later
$_POST['path'] = $path;
$pofile = new Loco_fs_LocaleFile( $path );
if( $suffix !== $pofile->getSuffix() ){
$pofile = $pofile->cloneLocale( $locale );
if( $suffix !== $pofile->getSuffix() ){
throw new Loco_error_Exception('Failed to suffix file path with locale code');
}
}
// target PO should not exist yet
$pofile->normalize( $base );
$api = new Loco_api_WordPressFileSystem;
$api->authorizeCreate( $pofile );
// Target MO probably doesn't exist, but we don't want to overwrite it without asking
$mofile = $pofile->cloneExtension('mo');
if( $mofile->exists() ){
throw new Loco_error_Exception( __('MO file exists for this language already. Delete it first','loco-translate') );
}
/*/ Same for JSON file, but WordPress >= only 5
$jsfile = function_exists('wp_set_script_translations') ? $pofile->cloneExtension('json') : null;
if( $jsfile && $jsfile->exists() ){
throw new Loco_error_Exception( __('JSON file exists for this language already. Delete it first','loco-translate') );
}*/
// Permit forcing of any parsable file as strings template
if( $source = $post->source ){
$potfile = new Loco_fs_File( $source );
$potfile->normalize( $base );
$data = Loco_gettext_Data::load($potfile);
// Remove target strings when copying PO
if( $post->strip ){
$data->strip();
}
}
// else parse POT file if project defines one that exists
else if( ( $potfile = $project->getPot() ) && $potfile->exists() ){
$data = Loco_gettext_Data::load($potfile);
}
// else extract directly from source code, assuming domain passed though from front end
else {
$extr = new Loco_gettext_Extraction( $bundle );
$data = $extr->addProject($project)->includeMeta()->getTemplate($domain);
$potfile = null;
}
// Let template define Project-Id-Version, else set header to current project name
$headers = array();
$vers = $data->getHeaders()->{'Project-Id-Version'};
if( ! $vers || 'PACKAGE VERSION' === $vers ){
$headers['Project-Id-Version'] = $project->getName();
}
// relative path from bundle root to the template/source this file was created from
if( $potfile && $post->link ){
$headers['X-Loco-Template'] = $potfile->getRelativePath( $bundle->getDirectoryPath() );
}
$data->localize( $locale, $headers );
$posize = $pofile->putContents( $data->msgcat() );
$mosize = $mofile->putContents( $data->msgfmt() );
//$jssize = $jsfile && ( $sub = $data->splitJs() ) ? $jsfile->putContents($data->jedize($domain,$sub)) : 0;
// set debug response data
$this->set( 'debug', array (
'poname' => $pofile->basename(),
'posize' => $posize,
'mosize' => $mosize,
//'jssize' => $jssize,
'source' => $potfile ? $potfile->basename() : '',
) );
// push recent items on file creation
// TODO push project and locale file
Loco_data_RecentItems::get()->pushBundle( $bundle )->persist();
// front end will redirect to the editor
$type = strtolower( $this->get('type') );
$this->set( 'redirect', Loco_mvc_AdminRouter::generate( sprintf('%s-file-edit',$type), array (
'path' => $pofile->getRelativePath($base),
'bundle' => $bundle->getHandle(),
'domain' => $project->getId(),
) ) );
return parent::render();
}
}

View File

@@ -0,0 +1,28 @@
<?php
/**
* Ajax "ping" route, for testing Ajax responses are working.
*/
class Loco_ajax_PingController extends Loco_mvc_AjaxController {
/**
* {@inheritdoc}
*/
public function render(){
$post = $this->validate();
// echo back bytes posted
if( $post->has('echo') ){
$this->set( 'ping', $post['echo'] );
}
// else just send pong
else {
$this->set( 'ping', 'pong' );
}
// always send tick symbol to check json serializing of unicode
$this->set( 'utf8', "\xE2\x9C\x93" );
return parent::render();
}
}

View File

@@ -0,0 +1,165 @@
<?php
/**
* Ajax "save" route, for saving editor contents to disk
*/
class Loco_ajax_SaveController extends Loco_ajax_common_BundleController {
/**
* {@inheritdoc}
*/
public function render(){
$post = $this->validate();
// path parameter must not be empty
$path = $post->path;
if( ! $path ){
throw new InvalidArgumentException('Path parameter required');
}
// locale must be posted to indicate whether PO or POT
$locale = $post->locale;
if( is_null($locale) ){
throw new InvalidArgumentException('Locale parameter required');
}
$pofile = new Loco_fs_LocaleFile( $path );
$pofile->normalize( loco_constant('WP_CONTENT_DIR') );
$poexists = $pofile->exists();
// ensure we only deal with PO/POT source files.
// posting of MO file paths is permitted when PO is missing, but we're about to fix that
$ext = $pofile->extension();
if( 'mo' === $ext ){
$pofile = $pofile->cloneExtension('po');
}
else if( 'pot' === $ext ){
$locale = '';
}
else if( 'po' !== $ext ){
throw new Loco_error_Exception('Invalid file path');
}
// force the use of remote file system when configured from front end
$api = new Loco_api_WordPressFileSystem;
// data posted may be either 'multipart/form-data' (recommended for large files)
if( isset($_FILES['po']) ){
$data = Loco_gettext_Data::fromSource( Loco_data_Upload::src('po') );
}
// else 'application/x-www-form-urlencoded' by default
else {
$data = Loco_gettext_Data::fromSource( $post->data );
}
// WordPress-ize some headers that differ from JavaScript libs
if( $compile = (bool) $locale ){
$head = $data->getHeaders();
$head['Language'] = strtr( $locale, '-', '_' );
}
// backup existing file before overwriting, but still allow if backups fails
$num_backups = Loco_data_Settings::get()->num_backups;
if( $num_backups && $poexists ){
try {
$api->authorizeCopy( $pofile );
$backups = new Loco_fs_Revisions( $pofile );
$backups->create();
$backups->prune($num_backups);
}
catch( Exception $e ){
Loco_error_AdminNotices::debug( $e->getMessage() );
$message = __('Failed to create backup file in "%s". Check file permissions or disable backups','loco-translate');
Loco_error_AdminNotices::warn( sprintf( $message, $pofile->getParent()->basename() ) );
}
}
// commit file directly to disk
$api->authorizeSave( $pofile );
$bytes = $pofile->putContents( $data->msgcat() );
$mtime = $pofile->modified();
// add bundle to recent items on file creation
try {
$bundle = $this->getBundle();
Loco_data_RecentItems::get()->pushBundle( $bundle )->persist();
}
catch( Exception $e ){
// editor permitted to save files not in a bundle, so catching failures
$bundle = null;
}
// start success data with bytes written and timestamp
$this->set('locale', $locale );
$this->set('pobytes', $bytes );
$this->set('poname', $pofile->basename() );
$this->set('modified', $mtime);
$this->set('datetime', Loco_mvc_ViewParams::date_i18n($mtime) );
// Compile MO and JSON files unless saving template
if( $compile ){
try {
$mofile = $pofile->cloneExtension('mo');
$api->authorizeSave( $mofile );
$bytes = $mofile->putContents( $data->msgfmt() );
$this->set( 'mobytes', $bytes );
Loco_error_AdminNotices::success( __('PO file saved and MO file compiled','loco-translate') );
}
catch( Exception $e ){
Loco_error_AdminNotices::debug( $e->getMessage() );
Loco_error_AdminNotices::warn( __('PO file saved, but MO file compilation failed','loco-translate') );
$this->set( 'mobytes', 0 );
// prevent further compilation if MO failed
$compile = false;
}
}
else {
Loco_error_AdminNotices::success( __('POT file saved','loco-translate') );
}
/*/ Compile JSON translations for WordPress >= 5
if( $compile && $bundle && function_exists('wp_set_script_translations') ){
$bytes = 0;
try {
list($domain) = Loco_package_Project::splitId( $this->get('domain') );
// hash file reference according to WordPress logic (see load_script_textdomain)
$base = $pofile->dirname().'/'.$pofile->filename();
foreach( $data->exportRefs('\\.jsx?') as $ref => $messages ){
if( '.min.js' === substr($ref,-7) ) {
$ref = substr($ref,0,-7).'.js';
}
// filter similarly to WP's `load_script_textdomain_relative_path` which is called from `load_script_textdomain`
$ref = apply_filters( 'loco_script_relative_path', $ref, $domain );
// referenced file must exist in bundle, or will never be loaded and so not require a .json file
$file = new Loco_fs_File( $bundle->getDirectoryPath().'/'.$ref );
if( $file->exists() && ! $file->isDirectory() ){
$file = new Loco_fs_File( $base.'-'.md5($ref).'.json' );
$api->authorizeSave( $file );
$bytes += $file->putContents( $data->jedize($domain,$messages) );
}
else {
Loco_error_AdminNotices::warn( sprintf('%s not found in bundle',$ref) );
}
}
// single JSON file containing all .js ref from this file
if( $messages = $data->splitJs() ){
$file = $pofile->cloneExtension('json');
$api->authorizeSave( $file );
$bytes = $file->putContents( $data->jedize($domain,$messages) );
}
}
catch( Exception $e ){
Loco_error_AdminNotices::debug( $e->getMessage() );
Loco_error_AdminNotices::warn( __('JSON compilation failed','loco-translate') );
}
$this->set( 'jsbytes', $bytes );
}*/
return parent::render();
}
}

View File

@@ -0,0 +1,78 @@
<?php
/**
* Ajax "sync" route.
* Performs the basic in-editor sync function from the old 1.x version.
*/
class Loco_ajax_SyncController extends Loco_mvc_AjaxController {
/**
* {@inheritdoc}
*/
public function render(){
$post = $this->validate();
$bundle = Loco_package_Bundle::fromId( $post->bundle );
$project = $bundle->getProjectById( $post->domain );
if( ! $project instanceof Loco_package_Project ){
throw new Loco_error_Exception('No such project '.$post->domain);
}
$file = new Loco_fs_File( $post->path );
$base = loco_constant('WP_CONTENT_DIR');
$file->normalize( $base );
// POT file always synced with source code (even if a PO being used as POT)
if( 'pot' === $post->type ){
$potfile = null;
}
// allow post data to force a template file path
else if( $path = $post->sync ){
$potfile = new Loco_fs_File($path);
$potfile->normalize( $base );
}
// else use project-configured template if one is defined
else {
$potfile = $project->getPot();
}
// sync with POT if it exists
if( $potfile && $potfile->exists() ){
$this->set('pot', $potfile->basename() );
try {
$data = Loco_gettext_Data::load($potfile);
}
catch( Exception $e ){
// translators: Where %s is the name of the invalid POT file
throw new Loco_error_ParseException( sprintf( __('Translation template is invalid (%s)','loco-translate'), $potfile->basename() ) );
}
}
// else sync with source code
else {
$this->set('pot', '' );
$domain = (string) $project->getDomain();
$extr = new Loco_gettext_Extraction($bundle);
$extr->addProject($project);
// bail if any files were skipped
if( $list = $extr->getSkipped() ){
$n = count($list);
$maximum = Loco_mvc_FileParams::renderBytes( wp_convert_hr_to_bytes( Loco_data_Settings::get()->max_php_size ) );
$largest = Loco_mvc_FileParams::renderBytes( $extr->getMaxPhpSize() );
// Translators: Where %2$s is the maximum size of a file that will be included and %3$s is the largest encountered
$text = _n('One file has been skipped because it\'s %3$s. (Max is %2$s). Check all strings are present before saving.','%s files over %2$s have been skipped. (Largest is %3$s). Check all strings are present before saving.',$n,'loco-translate');
$text = sprintf( $text, number_format($n), $maximum, $largest );
// not failing, just warning. Nothing will be saved until user saves editor state
Loco_error_AdminNotices::warn( $text );
}
// OK to return available strings
$data = $extr->includeMeta()->getTemplate($domain);
}
$this->set( 'po', $data->jsonSerialize() );
return parent::render();
}
}

View File

@@ -0,0 +1,81 @@
<?php
/**
* Ajax "xgettext" route, for initializing new template file from source code
*/
class Loco_ajax_XgettextController extends Loco_ajax_common_BundleController {
/**
* {@inheritdoc}
*/
public function render(){
$this->validate();
$bundle = $this->getBundle();
$project = $this->getProject( $bundle );
// target location may not be next to POT file at all
$base = loco_constant('WP_CONTENT_DIR');
$target = new Loco_fs_Directory( $this->get('path') );
$target->normalize( $base );
if( $target->exists() && ! $target->isDirectory() ){
throw new Loco_error_Exception('Target is not a directory');
}
// basename should be posted from front end
$name = $this->get('name');
if( ! $name ){
throw new Loco_error_Exception('Front end did not post $name');
}
// POT file shouldn't exist currently
$potfile = new Loco_fs_File( $target.'/'.$name );
$api = new Loco_api_WordPressFileSystem;
$api->authorizeCreate($potfile);
// Do extraction and grab only given domain's strings
$ext = new Loco_gettext_Extraction( $bundle );
$domain = $project->getDomain()->getName();
$data = $ext->addProject($project)->includeMeta()->getTemplate( $domain );
// additional headers to set in new POT file
$head = $data->getHeaders();
$head['Project-Id-Version'] = $project->getName();
// write POT file to disk returning byte length
$potsize = $potfile->putContents( $data->msgcat(true) );
// set response data for debugging
if( loco_debugging() ){
$this->set( 'debug', array (
'potname' => $potfile->basename(),
'potsize' => $potsize,
'total' => $ext->getTotal(),
) );
}
// push recent items on file creation
// TODO push project and locale file
Loco_data_RecentItems::get()->pushBundle( $bundle )->persist();
// put flash message into session to be displayed on redirected page
try {
Loco_data_Session::get()->flash('success', __('Template file created','loco-translate') );
Loco_data_Session::close();
}
catch( Exception $e ){
Loco_error_AdminNotices::debug( $e->getMessage() );
}
// redirect front end to bundle view. Discourages manual editing of template
$type = strtolower( $bundle->getType() );
$href = Loco_mvc_AdminRouter::generate( sprintf('%s-view',$type), array(
'bundle' => $bundle->getHandle(),
) );
$hash = '#loco-'.$project->getId();
$this->set( 'redirect', $href.$hash );
return parent::render();
}
}

View File

@@ -0,0 +1,39 @@
<?php
/**
* Common functions for all Ajax actions that operate on a bundle
*/
abstract class Loco_ajax_common_BundleController extends Loco_mvc_AjaxController {
/**
* @return Loco_package_Bundle
*/
protected function getBundle(){
if( $id = $this->get('bundle') ){
// type may be passed as separate argument
if( $type = $this->get('type') ){
return Loco_package_Bundle::createType( $type, $id );
}
// else embedded in standalone bundle identifier
// TODO standardize this across all Ajax end points
return Loco_package_Bundle::fromId($id);
}
// else may have type embedded in bundle
throw new Loco_error_Exception('No bundle identifier posted');
}
/**
* @param Loco_package_Bundle
* @return Loco_package_Project
*/
protected function getProject( Loco_package_Bundle $bundle ){
$project = $bundle->getProjectById( $this->get('domain') );
if( ! $project ){
throw new Loco_error_Exception('Failed to find translation project');
}
return $project;
}
}

View File

@@ -0,0 +1,409 @@
<?php
/**
* Abstracts WordPress filesystem connection.
* https://codex.wordpress.org/Filesystem_API
*/
class Loco_api_WordPressFileSystem {
/**
* Currently authenticated file system connection
* @var WP_Filesystem_Direct
*/
private $fs;
/**
* Whether global file modifications have already passed check
* @var bool
*/
private $fs_allowed;
/**
* Credentials form HTML echoed from request_filesystem_credentials
* @var string
*/
private $form = '';
/**
* Credentials posted into the API
* @var array
*/
private $creds_in = array();
/**
* Credentials returned from the API
* @var array
*/
private $creds_out = array();
/**
* Create direct filesystem accessor
* @return WP_Filesystem_Direct
*/
public static function direct(){
// Emulate WP_Filesystem to avoid FS_METHOD and filters overriding "direct" type
if( ! class_exists('WP_Filesystem_Direct',false) ){
require_once ABSPATH.'wp-admin/includes/class-wp-filesystem-base.php';
require_once ABSPATH.'wp-admin/includes/class-wp-filesystem-direct.php';
}
return new WP_Filesystem_Direct(null);
}
/**
* Get HTML form rendered by request_filesystem_credentials
* @return string
*/
public function getForm(){
return $this->form;
}
/**
* Pre-auth checks for superficial file system blocks and disconnects any active remotes
* @param Loco_fs_File
* @throws Loco_error_WriteException
* @return bool always true
*/
public function preAuthorize( Loco_fs_File $file ){
if( ! $this->fs_allowed ){
$file->getWriteContext()->authorize();
$this->fs_allowed = true;
}
// Disconnecting remote file system ensures the auth functions always start with direct file access
$file->getWriteContext()->disconnect();
return true;
}
/**
* Authorize for the creation of a file that does not exist
* @param Loco_fs_File
* @return bool whether file system is authorized NOT necessarily whether file is creatable
*/
public function authorizeCreate( Loco_fs_File $file ){
$this->preAuthorize($file);
if( $file->exists() ){
throw new Loco_error_WriteException( sprintf( __('%s already exists in this folder','loco-translate'), $file->basename() ) );
}
return $file->creatable() || $this->authorize($file);
}
/**
* Authorize for the update of a file that does exist
* @param Loco_fs_File
* @return bool whether file system is authorized NOT necessarily whether file is updatable
*/
public function authorizeUpdate( Loco_fs_File $file ){
$this->preAuthorize($file);
if( ! $file->exists() ){
throw new Loco_error_WriteException("File doesn't exist, try authorizeCreate");
}
return $file->writable() || $this->authorize($file);
}
/**
* Authorize for update or creation, depending whether file exists
* @param Loco_fs_File
* @return bool
*/
public function authorizeSave( Loco_fs_File $file ){
$this->preAuthorize($file);
return ( $file->exists() ? $file->writable() : $file->creatable() ) || $this->authorize($file);
}
/**
* Authorize for copy (to same directory), meaning source file must exist and directory be writable
* @param Loco_fs_File
* @return bool
*/
public function authorizeCopy( Loco_fs_File $file ){
$this->preAuthorize($file);
if( ! $file->exists() ){
throw new Loco_error_WriteException("Can't copy a file that doesn't exist");
}
return $file->creatable() || $this->authorize($file);
}
/**
* Authorize for move (to another path if given).
* @param Loco_fs_File file being moved (must exist)
* @param Loco_fs_File target path (should not exist)
* @return bool
*/
public function authorizeMove( Loco_fs_File $source, Loco_fs_File $target = null ){
// source is in charge of its own deletion
$result = $this->authorizeDelete($source);
// target is in charge of copying original which it must also be able to read.
if( $target && ! $this->authorizeCreate($target) ){
$result = false;
}
// value returned will be false if at least one file requires we add credentials
return $result;
}
/**
* Authorize for the removal of an existing file
* @param Loco_fs_File
* @return bool whether file system is authorized NOT necessarily whether file is removable
*/
public function authorizeDelete( Loco_fs_File $file ){
$this->preAuthorize($file);
if( ! $file->exists() ){
throw new Loco_error_WriteException("Can't delete a file that doesn't exist");
}
return $file->deletable() || $this->authorize($file);
}
/**
* Connect file to credentials in posted data. Used when established in advance what connection is needed
* @param Loco_fs_File
* @return bool whether file system is authorized
*/
public function authorizeConnect( Loco_fs_File $file ){
$this->preAuthorize($file);
// front end may have posted that "direct" connection will work
$post = Loco_mvc_PostParams::get();
if( 'direct' === $post->connection_type ){
return true;
}
return $this->authorize($file);
}
/**
* Wraps `request_filesystem_credentials` negotiation to obtain a remote connection and buffer WordPress form output
* Call before output started, because buffers.
* @param Loco_fs_File
* @return bool
*/
private function authorize( Loco_fs_File $file ){
// may already have authorized successfully
if( $fs = $this->fs ){
$file->getWriteContext()->connect( $fs, false );
return true;
}
// may have already failed authorization
if( $this->form ){
return false;
}
// network access may be disabled
if( ! apply_filters('loco_allow_remote', true ) ){
throw new Loco_error_WriteException('Remote connection required, but network access is disabled');
}
// else begin new auth
$this->fs = null;
$this->form = '';
$this->creds_out = array();
// observe settings held temporarily in session
try {
$session = Loco_data_Session::get();
if( isset($session['loco-fs']) ){
$creds = $session['loco-fs'];
if( is_array($creds) && $this->tryCredentials($creds,$file) ){
$this->creds_in = array();
return true;
}
}
}
catch( Exception $e ){
// tolerate session failure
}
$post = Loco_mvc_PostParams::get();
$dflt = array( 'hostname' => '', 'username' => '', 'password' => '', 'public_key' => '', 'private_key' => '', 'connection_type' => '', '_fs_nonce' => '' );
$this->creds_in = array_intersect_key( $post->getArrayCopy(), $dflt );
// deliberately circumventing call to `get_filesystem_method`
// risk of WordPress version compatibility issues, but only sane way to force a remote connection
// @codeCoverageIgnoreStart
if( defined('FS_METHOD') && FS_METHOD ){
$type = FS_METHOD;
// forcing direct access means request_filesystem_credentials will never give us a form :(
if( 'direct' === $type ){
return false;
}
}
// direct filesystem if ok if front end already posted it
else if( 'direct' === $post->connection_type ){
return true;
}
// else perform same logic as request_filesystem_credentials does to establish type
else if( 'ssh' === $post->connection_type && extension_loaded('ssh2') && function_exists('stream_get_contents') ){
$type = 'ssh2';
}
else if( extension_loaded('ftp') ){
$type = 'ftpext';
}
else if( extension_loaded('sockets') || function_exists('fsockopen') ){
$type = 'ftpsockets';
}
// @codeCoverageIgnoreEnd
else {
$type = '';
}
// context is nonsense here as the system doesn't know what operation we're performing
// testing directory write-permission when we're updating a file, for example.
$context = '/ignore/this';
$type = apply_filters( 'filesystem_method', $type, $post->getArrayCopy(), $context, true );
// the only params we'll pass into form will be those used by the ajax fsConnect end point
$extra = array( 'loco-nonce', 'path', 'auth', 'dest' );
// capture WordPress output during negotiation.
$buffer = Loco_output_Buffer::start();
$creds = request_filesystem_credentials( '', $type, false, $context, $extra );
if( is_array($creds) ){
// credentials passed through, should allow connect if they are correct
if( $this->tryCredentials($creds,$file) ){
$this->persistCredentials();
return true;
}
// else there must be an error with the credentials
$error = true;
// pull more useful connection error for display in form
if( isset($GLOBALS['wp_filesystem']) ){
$fs = $GLOBALS['wp_filesystem'];
$GLOBALS['wp_filesystem'] = null;
if( $fs && $fs->errors && $fs->errors->get_error_code() ){
$error = $fs->errors;
}
}
// annoyingly WordPress moves the error notice above the navigation tabs :-/
request_filesystem_credentials( '', $type, $error, $context, $extra );
}
// now have unauthorized remote connection
$this->form = (string) $buffer->close();
return false;
}
/**
* @param array credentials returned from request_filesystem_credentials
* @param Loco_fs_File file to authorize write context
* @return bool when credentials connected ok
*/
private function tryCredentials( array $creds, Loco_fs_File $file ){
// lazy construct the file system from current credentials if possible
// in typical WordPress style, after success the object will be held in a global.
if( WP_Filesystem( $creds, '/ignore/this/' ) ){
$this->fs = $GLOBALS['wp_filesystem'];
// hook new file system into write context (specifying that connect has already been performed)
$file->getWriteContext()->connect( $this->fs, false );
$this->creds_out = $creds;
return true;
}
return false;
}
/**
* Set current credentials in session if settings allow
* @return bool whether credentials persisted
*/
private function persistCredentials(){
try {
$settings = Loco_data_Settings::get();
if( $settings['fs_persist'] ){
$session = Loco_data_Session::get();
$session['loco-fs'] = $this->creds_out;
$session->persist();
return true;
}
}
catch( Exception $e ){
// tolerate session failure
Loco_error_AdminNotices::debug( $e->getMessage() );
}
return false;
}
/**
* Get working credentials that resulted in connection
* @return array
*/
public function getOutputCredentials(){
return $this->creds_out;
}
/**
* Get input credentials from original post.
* this is not the same as getCredentials. It is designed for replay only, regardless of success
* Note that input to request_filesystem_credentials is not the same as the output (specifically how hostname:port is handled)
*/
public function getInputCredentials(){
return $this->creds_in;
}
/**
* Get currently configured filesystem API
* @return WP_Filesystem_Direct
*/
public function getFileSystem(){
if( ! $this->fs ){
return self::direct();
}
return $this->fs;
}
/**
* Check if a file is safe from WordPress automatic updates
* @param Loco_fs_File
* @return bool
*/
public function isAutoUpdatable( Loco_fs_File $file ){
// all paths safe from auto-updates if auto-updates are completely disabled
if( $this->isAutoUpdateDenied() ){
return false;
}
if( apply_filters( 'automatic_updater_disabled', loco_constant('AUTOMATIC_UPDATER_DISABLED') ) ) {
return false;
}
// Auto-updates aren't denied, so ascertain location "type" and run through the same filters as should_update()
if( $type = $file->getUpdateType() ){
// TODO provide a useful context for the update offer passed to filters
// WordPress updater will have taken this from remote API data which we don't have here.
$item = new stdClass;
return apply_filters( 'auto_update_'.$type, true, $item );
}
// else safe (not auto-updatable)
return false;
}
/**
* Check if system is configured to deny auto-updates
* @return bool
*/
public function isAutoUpdateDenied(){
// WordPress >= 4.8 can disable auto updates completely with "automatic_updater" context
if( function_exists('wp_is_file_mod_allowed') && ! wp_is_file_mod_allowed('automatic_updater') ){
return true;
}
// else simply observe AUTOMATIC_UPDATER_DISABLED constant
if( apply_filters( 'automatic_updater_disabled', loco_constant('AUTOMATIC_UPDATER_DISABLED') ) ) {
return true;
}
// else nothing explicitly denying updates
return false;
}
}

View File

@@ -0,0 +1,144 @@
<?php
/**
*
*/
class Loco_api_WordPressTranslations {
/**
* Cache of whether network access is allowed
* @var bool
*/
private $enabled;
/**
* Cache of core locale objects
* @var array
*/
private $locales;
/**
* Cache of data returned from get_available_languages (not cached by WP)
* @var array
*/
private $installed;
/**
* Hash map of installed languages indexed by tag
* @var array
*/
private $installed_hash;
/**
* Wrap wp_get_available_translations
* @return array
*/
private function wp_get_available_translations(){
if( ! function_exists('wp_get_available_translations') ){
require_once ABSPATH.'wp-admin/includes/translation-install.php';
}
// WordPress will raise Warning if offline, and will cache result otherwise.
return wp_get_available_translations();
}
/**
* Get fully fledged locale objects from available core translation data
* @return array<Loco_Locale>
*/
public function getAvailableCore(){
$locales = $this->locales;
if( is_null($locales) ){
$locales = array();
// get official locales from API if we have network
if( $cached = $this->wp_get_available_translations() ){
$english_name = 'english_name';
$native_name = 'native_name';
}
// else fall back to bundled data cached
else {
$english_name = 0;
$native_name = 1;
$cached = Loco_data_CompiledData::get('locales');
// debug so we can see on front end that data was offline
// $locales['en-debug'] = ( new Loco_Locale('en','','debug') )->setName('OFFLINE DATA');
}
foreach( $cached as $tag => $raw ){
$locale = Loco_Locale::parse($tag);
if( $locale->isValid() ){
$locale->setName( $raw[$english_name], $raw[$native_name] );
$locales[ (string) $tag ] = $locale;
}
/* Skip invalid language tags, e.g. "pt_PT_ao90" should be "pt_PT_ao1990"
* No point fixing invalid tags, because core translation files won't match.
else {
Loco_error_AdminNotices::debug( sprintf('Invalid locale: %s', $tag) );
}*/
}
$this->locales = $locales;
}
return $locales;
}
/**
* Wrap get_available_languages
* @return array
*/
public function getInstalledCore(){
// wp-includes/l10n.php should always be included at runtime
if( ! is_array($this->installed) ){
$this->installed = get_available_languages();
// en_US is implicitly installed
if( ! in_array('en_US',$this->installed) ){
array_unshift( $this->installed, 'en_US' );
}
}
return $this->installed;
}
/**
* @return array
*/
private function getInstalledHash(){
if( ! is_array($this->installed_hash) ){
$this->installed_hash = array_flip( $this->getInstalledCore() );
}
return $this->installed_hash;
}
/**
* Check if a given locale is installed
* @return bool
*/
public function isInstalled( $locale ){
return array_key_exists( (string) $locale, $this->getInstalledHash() );
}
/**
* Get WordPress locale data by strictly well-formed language tag
* @return Loco_Locale
*/
public function getLocale( $tag ){
$all = $this->getAvailableCore();
return isset($all[$tag]) ? $all[$tag] : null;
}
/**
* Check whether remote API may be disabled for whatever reason, usually debugging.
* @return bool
*/
public function hasNetwork(){
if( is_null($this->enabled) ){
$this->enabled = (bool) apply_filters('loco_allow_remote', true );
}
return $this->enabled;
}
}

View File

@@ -0,0 +1,20 @@
<?php
/**
* Placeholder for missing PHP "ctype" extension.
*/
abstract class Loco_compat_CtypeExtension {
public static function digit( $value ){
return 1 === preg_match('/^[0-9]+$/',$value);
}
}
// @codeCoverageIgnoreStart
if( ! function_exists('ctype_digit') ){
function ctype_digit( $value ){
return Loco_compat_CtypeExtension::digit( $value );
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* Class containing reasons for total incompatibility with current WordPress environment.
* It won't be loaded unless total failure occurs
*
* @codeCoverageIgnore
*/
abstract class Loco_compat_Failure {
/**
* "admin_notices" callback, renders failure notice if plugin failed to start up admin hooks.
* If this is hooked and not unhooked then auto-hooks using annotations have failed.
*/
public static function print_hook_failure(){
$texts = array( 'Loco Translate failed to start up' );
/*/ Hooks currently not using annotatons (would be if we enabled @priority tag)
if( ini_get('opcache.enable') && ( ! ini_get('opcache.save_comments') || ! ini_get('opcache.load_comments') ) ){
$texts[] = 'Try configuring opcache to preserve comments';
}*/
echo '<div class="notice error"><p><strong>Error:</strong> '.implode('. ',$texts).'</p></div>';
}
}

View File

@@ -0,0 +1,33 @@
<?php
/**
* Placeholder for missing PHP "json" extension.
* Just avoids fatal errors. Does not actually replace functionality.
*
* If this extension is missing no JavaScript will work in the plugin at all.
*/
abstract class Loco_compat_JsonExtension {
public static function json_encode( $value ){
return '{"error":{"code":-1,"message":"json extension is not installed"}}';
}
public static function json_decode( $json ){
return null;
}
}
// @codeCoverageIgnoreStart
if( ! function_exists('json_encode') ){
function json_encode( $value ){
return Loco_compat_JsonExtension::json_encode( $value );
}
}
if( ! function_exists('json_decode') ){
function json_decode( $json ){
return Loco_compat_JsonExtension::json_decode($json);
}
}

View File

@@ -0,0 +1,23 @@
<?php
// @codeCoverageIgnoreStart
/**
* Placeholder for missing interface in PHP < 5.4.
* Can't be invoked automatically, so always do: json_encode( $obj->jsonSerialize() )
* Note that this shim is also present in WordPress >= 4.4.0
*/
if( ! interface_exists('JsonSerializable') ){
interface JsonSerializable {
public function jsonSerialize();
}
}
// @codeCoverageIgnoreEnd
/**
* Redundant interface so this file will autoload when JsonSerializable is referenced
* @internal
*/
interface Loco_compat_JsonSerializable extends JsonSerializable {
}

View File

@@ -0,0 +1,63 @@
<?php
/**
* Placeholder for missing PHP "mbstring" extension.
* Just avoids fatal errors. Does not actually replace functionality.
*
* If the mbstring library is missing any PO files that aren't UTF-8 encoded will result in parsing failures.
*/
abstract class Loco_compat_MbstringExtension {
public static function mb_detect_encoding( $str, array $encoding_list = null, $strict = null ){
// return ! $str || preg_match('/^(?:[\\0-\\x7F]|[\\xC0-\\xDF][\\x80-\\xBF]|[\\xE0-\\xEF][\\x80-\\xBF]{2}|[\\xF0-\\xFF][\\x80-\\xBF]{3})+$/',$str)
return ! $str || preg_match('/./u',$str)
? 'UTF-8'
: 'ISO-8859-1'
;
}
public static function mb_list_encodings(){
return array('UTF-8','ISO-8859-1');
}
public static function mb_strlen( $str, $encoding = null ){
return strlen($str);
}
public static function mb_convert_encoding( $str, $to_encoding, $from_encoding ){
return $str;
}
}
// @codeCoverageIgnoreStart
if( ! function_exists('mb_detect_encoding') ){
function mb_detect_encoding( $str = '', array $encoding_list = array(), $strict = false ){
return Loco_compat_MbstringExtension::mb_detect_encoding( $str, $encoding_list, $strict );
}
}
if( ! function_exists('mb_list_encodings') ){
function mb_list_encodings(){
return Loco_compat_MbstringExtension::mb_list_encodings();
}
}
if( ! function_exists('mb_strlen') ){
function mb_strlen( $str, $encoding = null ){
return Loco_compat_MbstringExtension::mb_strlen( $str, $encoding );
}
}
if( ! function_exists('mb_convert_encoding') ){
function mb_convert_encoding( $str, $to_encoding, $from_encoding = null ){
return Loco_compat_MbstringExtension::mb_convert_encoding( $str, $to_encoding, $from_encoding );
}
}
if( ! function_exists('mb_encoding_aliases') ){
function mb_encoding_aliases(){
return false;
}
}

View File

@@ -0,0 +1,113 @@
<?php
/**
* Abstraction of PHP "posix" extension.
* Basic functionality substitution, but cannot get user/group names so falls back to numeric
*/
abstract class Loco_compat_PosixExtension {
/**
* @param int
*/
private static $uid = null;
/**
* @param int
*/
private static $gid = null;
/**
* @return int
*/
public static function getuid(){
if( is_null(self::$uid) ){
// use posix function if extension available
if( function_exists('posix_geteuid') ){
self::$uid = posix_geteuid();
}
// else use temp file system to establish owner
else {
self::$uid = self::getuidViaTempDir();
}
}
return self::$uid;
}
/**
* @return int
*/
public static function getgid(){
if( is_null(self::$gid) ){
// use posix function if extension available
if( function_exists('posix_getegid') ){
self::$gid = posix_getegid();
}
// else use temp file system to establish group owner
else {
self::$gid = self::getgidViaTempDir();
}
}
return self::$gid;
}
/**
* Attempt to get effective user ID by reading a temporary file
* @return int
*/
public static function getuidViaTempDir(){
$dir = get_temp_dir();
if( 04000 & fileperms($dir) ){
trigger_error( sprintf('%s directory has setuid bit, getuid may not be accurate'), E_USER_NOTICE );
}
$path = wp_tempnam( 'loco-sniff-'.time(), $dir );
$uid = fileowner($path);
unlink( $path );
return $uid;
}
/**
* Attempt to get effective group ID by reading a temporary file
* @return int
*/
public static function getgidViaTempDir(){
$dir = get_temp_dir();
if( 02000 & fileperms($dir) ){
trigger_error( sprintf('%s directory has setgid bit, getgid may not be accurate'), E_USER_NOTICE );
}
$path = wp_tempnam( 'loco-sniff-'.time(), $dir );
$gid = filegroup($path);
unlink( $path );
return $gid;
}
/**
* Get the name of the user that the web server runs under
* This is only for visual/info purposes.
* @return string
*/
public static function getHttpdUser(){
if( function_exists('posix_geteuid') ){
$info = posix_getpwuid( posix_geteuid() );
return $info['name'];
}
// @codeCoverageIgnoreStart
foreach( array('apache','nginx') as $name ){
if( false !== stripos(PHP_SAPI,$name) ){
return $name;
}
if( isset($_SERVER['SERVER_SOFTWARE']) && false !== stripos($_SERVER['SERVER_SOFTWARE'],$name) ){
return $name;
}
}
// translators: used when user name of web server process is unknown
return __('the web server','loco-translate');
// @codeCoverageIgnoreEnd
}
}

View File

@@ -0,0 +1,24 @@
<?php
/**
* Placeholder for missing PHP "tokenizer" extension.
* Just avoids fatal errors. Does not actually replace functionality.
*
* If this extension is missing PHP string extraction will not work at all.
*/
abstract class Loco_compat_TokenizerExtension {
public static function token_get_all( $value ){
return array();
}
}
// @codeCoverageIgnoreStart
if( ! function_exists('token_get_all') ){
function token_get_all( $value ){
return Loco_compat_TokenizerExtension::token_get_all($value);
}
}

View File

@@ -0,0 +1,401 @@
<?php
/**
* Holds a bundle definition in a structure serializable to a native array.
*/
class Loco_config_ArrayModel extends Loco_config_Model {
/**
* {@inheritdoc}
*/
public function createDom(){
return new LocoConfigDocument( array('#document', array(), array() ) );
}
/**
* Construct model from serialized JSON
* @return void
*/
public function loadJson( $json ){
$root = json_decode( $json, true );
if( ! $root || ! is_array($root) ){
throw new Loco_error_ParseException('Invalid JSON');
}
$this->loadArray( $root );
}
/**
* Construct model from exported array
* @return void
*/
public function loadArray( array $root ){
$dom = $this->getDom();
$dom->load( array('#document', array(), array($root) ) );
}
/**
* {@inheritdoc}
* Emulates *very limited* XPath queries used by the XML DOM.
*/
public function query( $query, $context = null ){
$match = new LocoConfigNodeList;
$query = explode('/', $query );
// absolute path always starts in document
if( $absolute = empty($query[0]) ){
$match->append( $this->getDom() );
}
// else start with base for relative path
else if( $context instanceof LocoConfigNode ){
$match->append( $context );
}
while( $query ){
$name = array_shift($query);
// self references do nothing
if( ! $name || '.' === $name ){
continue;
}
// match all current branches to produce new set of parents
$next = new LocoConfigNodeList;
foreach( $match as $parent ){
foreach( $parent->childNodes as $child ){
if( $name === $child->nodeName || ( '*' === $name && $child instanceof LocoConfigElement ) || ( 'text()' === $name && $child instanceof LocoConfigText) ){
$next->append( $child );
}
}
}
$match = $next;
}
return $match;
}
}
// The following classes are "private" to this file:
// They partially implement the same interfaces as the core DOM classes and are used for code hints.
// Interfaces are deliberately not used as the real DOM classes would not be able to implement them.
/**
* Node
*/
abstract class LocoConfigNode implements IteratorAggregate {
/**
* Raw data of internal format
* @var array
*/
protected $data;
/**
* Child nodes once cast to node objects
* @var LocoConfigNodeList
*/
protected $children;
/**
* @return mixed
*/
abstract public function export();
final public function __construct( $data ){
$this->data = $data;
}
protected function get_nodeName(){
return $this->data[0];
}
/*protected function get_attributes(){
return $this->data[1];
}*/
protected function get_childNodes(){
return $this->getIterator();
}
public function __get( $prop ){
$method = array( $this, 'get_'.$prop );
if( is_callable($method) ){
return call_user_func( $method );
}
}
/** @return LocoConfigNode */
public function appendChild( LocoConfigNode $child ){
$children = $this->getIterator();
$children->append( $child );
return $child;
}
/** @return bool */
public function hasChildNodes(){
return (bool) count( $this->getIterator() );
}
/**
* @return LocoConfigNodeList
*/
public function getIterator(){
if( ! $this->children ){
$raw = isset($this->data[2]) ? $this->data[2] : array();
$this->children = new LocoConfigNodeList( $this->data[2] );
}
return $this->children;
}
public function get_textContent(){
$s = '';
foreach( $this as $child ){
$s .= $child->get_textContent();
}
return $s;
}
}
/**
* NodeList
*/
class LocoConfigNodeList implements Iterator, Countable, ArrayAccess {
private $nodes;
private $i;
private $n;
public function __construct( array $nodes = array() ){
$this->nodes = $nodes;
$this->n = count( $nodes );
}
public function count(){
return $this->n;
}
public function rewind(){
$this->i = -1;
$this->next();
}
public function key(){
return $this->i;
}
public function current(){
return $this[ $this->i ];
}
public function valid(){
return is_int($this->i);
}
public function next(){
if( ++$this->i === $this->n ){
$this->i = null;
}
}
public function offsetExists( $i ){
return $i >= 0 && $i < $this->n;
}
public function offsetGet( $i ){
$node = $this->nodes[$i];
if( ! $node instanceof LocoConfigNode ){
if( is_array($node) ){
$node = new LocoConfigElement( $node );
}
else {
$node = new LocoConfigText( $node );
}
$this->nodes[$i] = $node;
}
return $node;
}
/**
* @codeCoverageIgnore
*/
public function offsetSet( $i, $value ){
throw new Exception('Use append');
}
/**
* @codeCoverageIgnore
*/
public function offsetUnset( $i ){
throw new Exception('Read only');
}
public function append( LocoConfigNode $node ){
$this->nodes[] = $node;
$this->n++;
}
/**
* Revert nodes back to raw array form and return for exporting
* @return array
*/
public function normalize(){
foreach( $this->nodes as $i => $node ){
if( $node instanceof LocoConfigNode ){
$this->nodes[$i] = $node->export();
}
}
return $this->nodes;
}
}
/**
* Document
*/
class LocoConfigDocument extends LocoConfigNode {
/**
* Rapidly set new data for document
*/
public function load( $data ){
$this->data = $data;
$this->children = null;
}
/**
* @return LocoConfigElement
*/
public function createElement( $name ){
return new LocoConfigElement( array( $name, array(), array() ) );
}
/**
* @return LocoConfigText
*/
public function createTextNode( $text ){
return new LocoConfigText( $text );
}
/**
* @return LocoConfigElement
*/
public function get_documentElement(){
$child = null;
foreach( $this as $child ){
break;
}
return $child;
}
/**
* {@inheritdoc}
* Override to keep single element root
*/
public function export(){
if( $root = $this->get_documentElement() ){
return $root->export();
}
}
}
/**
* Element
*/
class LocoConfigElement extends LocoConfigNode {
public function setAttribute( $prop, $value ){
$this->data[1][$prop] = $value;
}
public function removeAttribute( $prop ){
unset( $this->data[1][$prop] );
}
public function getAttribute( $prop ){
if( isset($this->data[1][$prop]) ){
return $this->data[1][$prop];
}
return '';
}
public function hasAttribute( $prop ){
return isset($this->data[1][$prop]);
}
/**
* {@inheritdoc}
*/
public function export(){
$raw = $this->data;
// return any cast elements back to raw data
if( $this->children ){
$raw[2] = $this->children->normalize();
}
return $raw;
}
}
/**
* Text
*/
class LocoConfigText extends LocoConfigNode {
protected function get_nodeName(){
return '#text';
}
public function hasChildNodes(){
return false;
}
public function getIterator(){
return new ArrayIterator;
}
public function export(){
return (string) $this->data;
}
public function get_nodeValue(){
return (string) $this->data;
}
public function get_textContent(){
return (string) $this->data;
}
}

View File

@@ -0,0 +1,171 @@
<?php
/**
* Loads Loco configuration file into a bundle definition
*/
class Loco_config_BundleReader {
/**
* @var Loco_package_Bundle
*/
private $bundle;
/**
* Constructor initializes empty dom
*/
public function __construct( Loco_package_Bundle $bundle ){
$this->bundle = $bundle;
}
/**
* @return Loco_package_Bundle
*/
public function loadXml( Loco_fs_File $file ){
$this->bundle->setDirectoryPath( $file->dirname() );
$model = new Loco_config_XMLModel;
$model->loadXml( $file->getContents() );
return $this->loadModel( $model );
}
/**
* @return Loco_package_Bundle
*/
public function loadJson( Loco_fs_File $file ){
$this->bundle->setDirectoryPath( $file->dirname() );
return $this->loadArray( json_decode( $file->getContents(), true ) );
}
/**
* @return Loco_package_Bundle
*/
public function loadArray( array $raw ){
$model = new Loco_config_ArrayModel;
$model->loadArray( $raw );
return $this->loadModel( $model );
}
/**
* Agnostic construction of Bundle from any configuration format
* @return Loco_package_Bundle
*/
public function loadModel( Loco_config_Model $model ){
// Base directory required to resolve relative paths
$bundle = $this->bundle;
$model->setDirectoryPath( $bundle->getDirectoryPath() );
$dom = $model->getDom();
$bundleElement = $dom->documentElement;
if( ! $bundleElement || 'bundle' !== $bundleElement->nodeName ){
throw new InvalidArgumentException('Expected root bundle element');
}
// Set bundle meta data if configured
// note that bundles have no inherent slug as it can change according to plugin/theme directory naming
if( $bundleElement->hasAttribute('name') ){
$bundle->setName( $bundleElement->getAttribute('name') );
}
// Bundle-level path exclusions
foreach( $model->query('exclude/*',$bundleElement) as $fileElement ){
$bundle->excludeLocation( $model->evaluateFileElement($fileElement) );
}
/* @var $domainElement LocoConfigElement */
foreach( $model->query('domain',$bundleElement) as $domainElement ){
$slug = $domainElement->getAttribute('name') or $slug = $bundle->getSlug();
// bundle may not have a handle set (most likely only in tests)
if( ! $bundle->getHandle() ){
$bundle->setHandle( $slug );
}
// Text Domain may also be declared by bundle author
$domain = new Loco_package_TextDomain( $slug );
$declared = $bundle->getHeaderInfo();
if( $declared && $declared->TextDomain === $slug ){
$domain->setCanonical( true );
}
/* @var $projectElement LocoConfigElement */
foreach( $model->query('project',$domainElement) as $projectElement ){
$name = $projectElement->getAttribute('name') or $name = $bundle->getName();
$project = new Loco_package_Project( $bundle, $domain, $name );
if( $projectElement->hasAttribute('slug') ){
$project->setSlug( $projectElement->getAttribute('slug') );
}
// <source>
foreach( $model->query('source',$projectElement) as $sourceElement ){
// sources may be <file>, <directory> or pass in special <path> if it could be either
foreach( $model->query('file',$sourceElement) as $fileElement ){
$project->addSourceFile( $model->evaluateFileElement($fileElement) );
}
foreach( $model->query('directory',$sourceElement) as $fileElement ){
$project->addSourceDirectory( $model->evaluateFileElement($fileElement) );
}
foreach( $model->query('path',$sourceElement) as $fileElement ){
$project->addSourceLocation( $model->evaluateFileElement($fileElement) );
}
foreach( $model->query('exclude/*', $sourceElement) as $fileElement ){
$project->excludeSourcePath( $model->evaluateFileElement($fileElement) );
}
}
// Avoid having no source locations
if( ! $project->hasSourceFiles() ){
if( $bundle->isSingleFile() ){
$project->addSourceFile( $bundle->getBootstrapPath() );
}
else {
$project->addSourceDirectory( $bundle->getDirectoryPath() );
}
}
// <target>
foreach( $model->query('target',$projectElement) as $targetElement ){
// targets support only directory paths:
foreach( $model->query('directory',$targetElement) as $fileElement ){
$project->addTargetDirectory( $model->evaluateFileElement($fileElement) );
}
foreach( $model->query('exclude/*', $targetElement) as $fileElement ){
$project->excludeTargetPath( $model->evaluateFileElement($fileElement) );
}
}
// Avoid having no target locations ..
if( 0 === count($project->getConfiguredTargets() ) ){
// .. unless the inherited root is a global location
if( $bundle->isTheme() || ( $bundle->isPlugin() && ! $bundle->isSingleFile() ) ){
$project->addTargetDirectory( $bundle->getDirectoryPath() );
}
}
// <template>
// configure POT file, should only be one
foreach( $model->query('template',$projectElement) as $templateElement ){
if( $model->evaulateBooleanAttribute( $templateElement, 'locked') ){
$project->setPotLock( true );
}
foreach( $model->query('file',$templateElement) as $fileElement ){
$project->setPot( $model->evaluateFileElement( $fileElement ) );
break 2;
}
}
// add project last for additional configs to be appended
$bundle->addProject( $project );
}
}
return $bundle;
}
}

View File

@@ -0,0 +1,161 @@
<?php
/**
*
*/
class Loco_config_BundleWriter implements JsonSerializable {
/**
* @var Loco_package_Bundle
*/
private $bundle;
/**
* Initialize config from the bundle it will describe
*/
public function __construct( Loco_package_Bundle $bundle ){
$this->bundle = $bundle;
}
/**
* @return string XML source
*/
public function toXml(){
$model = new Loco_config_XMLModel;
$dom = $this->compile($model);
return $dom->saveXML();
}
/**
* @return array
*/
public function toArray(){
$model = new Loco_config_ArrayModel;
$dom = $this->compile($model);
return $dom->export();
}
/**
* @return Loco_mvc_PostParams
*/
public function toForm(){
$model = new Loco_config_FormModel;
$dom = $this->compile($model);
return $model->getPost();
}
/**
* Alias of toArray implementing JsonSerializable
* @return array
*/
public function jsonSerialize(){
return $this->toArray();
}
/**
* Agnostic compilation of any config data type
* @return LocoConfigDocumentInterface
*/
private function compile( Loco_config_Model $model ){
$bundle = $this->bundle;
$model->setDirectoryPath( $bundle->getDirectoryPath() );
$systemTargets = $bundle->getSystemTargets();
$dom = $model->getDom();
$root = $dom->appendChild( $dom->createElement('bundle') );
$root->setAttribute( 'name', $bundle->getName() );
/*/ additional headers for information only (not read back in)
if( $value = $bundle->getHeaderInfo()->getVendorHost() ){
$root->setAttribute( 'vendor', $value );
}*/
foreach( $bundle->exportGrouped() as $domainName => $projects ){
$domainElement = $root->appendChild( $dom->createElement('domain') );
$domainElement->setAttribute( 'name', $domainName );
/* @var $proj Loco_package_Project */
foreach( $projects as $proj ){
$projElement = $domainElement->appendChild( $dom->createElement('project') );
// add project name even if it's the same as the bundle name
// when loading however, missing name will default to bundle name
$value = $proj->getName() or $value = $bundle->getName();
$projElement->setAttribute( 'name', $value );
// add project slug even if it's the same as the domain name
$value = $proj->getSlug();
$projElement->setAttribute( 'slug', $value );
// <source>
// zero or more source file locations
$sourcesElement = $dom->createElement('source');
/* @var $file Loco_fs_Directory */
foreach( $proj->getConfiguredSources() as $file ){
$sourcesElement->appendChild( $model->createFileElement($file) );
}
// zero or more excluded source paths
$excludeElement = $dom->createElement('exclude');
foreach( $proj->getConfiguredSourcesExcluded() as $file ){
$excludeElement->appendChild( $model->createFileElement($file) );
}
if( $excludeElement->hasChildNodes() ){
$sourcesElement->appendChild($excludeElement);
}
if( $sourcesElement->hasChildNodes() ){
$projElement->appendChild( $sourcesElement );
}
// <target>
// add zero or more target locations
$targetsElement = $dom->createElement('target');
/* @var $file Loco_fs_Directory */
foreach( $proj->getConfiguredTargets() as $file ){
if( ! in_array( $file->getPath(), $systemTargets, true ) ){
$targetsElement->appendChild( $model->createFileElement($file) );
}
}
// zero or more excluded targets
$excludeElement = $dom->createElement('exclude');
foreach( $proj->getConfiguredTargetsExcluded() as $file ){
$excludeElement->appendChild( $model->createFileElement($file) );
}
if( $excludeElement->hasChildNodes() ){
$targetsElement->appendChild($excludeElement);
}
if( $targetsElement->hasChildNodes() ){
$projElement->appendChild( $targetsElement );
}
// <template>
// add single POT template location
if( $file = $proj->getPot() ){
$templateElement = $projElement->appendChild( $dom->createElement('template') );
$templateElement->appendChild( $model->createFileElement($file) );
// template may be prortected from end-user tampering
if( $proj->isPotLocked() ){
$templateElement->setAttribute('locked','true');
}
}
}
}
// Write bundle-level path exclusions
$excludeElement = $dom->createElement('exclude');
foreach( $bundle->getExcludedLocations() as $file ){
$excludeElement->appendChild( $model->createFileElement($file) );
}
if( $excludeElement->hasChildNodes() ){
$root->appendChild( $excludeElement );
}
return $dom;
}
}

View File

@@ -0,0 +1,51 @@
<?php
/**
* Bundle configuration saved as a WordPress site option.
*/
class Loco_config_CustomSaved extends Loco_data_Option {
/**
* @var Loco_package_Bundle
*/
private $bundle;
/**
* {@inheritdoc}
*/
public function getKey(){
return strtolower( $this->bundle->getType() ).'_config__'.$this->bundle->getHandle();
}
/**
* {@inheritdoc}
*/
public function persist(){
$writer = new Loco_config_BundleWriter( $this->bundle );
$this->exchangeArray( $writer->toArray() );
return parent::persist();
}
/**
* @return Loco_config_CustomSaved
*/
public function setBundle( Loco_package_Bundle $bundle ){
$this->bundle = $bundle;
return $this;
}
/**
* Modify currently set bundle according to saved config data
* @return Loco_package_Bundle
*/
public function configure(){
$this->bundle->clear();
$reader = new Loco_config_BundleReader( $this->bundle );
$reader->loadArray( $this->getArrayCopy() );
return $this->bundle;
}
}

View File

@@ -0,0 +1,211 @@
<?php
/**
* Provides a bridge between the full serializable array model and POSTDATA array.
*
* Key differences between form data and the DOM are:
* - form fields cannot express attributes
* - form fields uses line breaks to separate multiple nodes
*/
class Loco_config_FormModel extends Loco_config_ArrayModel {
/**
* Export array data that matches the format used in postdata
* @return Loco_mvc_PostParams
*/
public function getPost(){
$dom = $this->getDom();
$root = $dom->documentElement;
$post = new Loco_mvc_PostParams( array (
'name' => $root->getAttribute('name'),
'exclude' => array (
'path' => '',
),
'conf' => array(),
) );
/* @var LocoConfigElement $domain */
foreach( $this->query('domain',$root) as $domain ){
$domainName = $domain->getAttribute('name');
/* @var LocoConfigElement $project */
foreach( $domain as $project ){
$tree = array (
'name' => $project->getAttribute('name'),
'slug' => $project->getAttribute('slug'),
'domain' => $domainName,
'source' => array (
'path' => '',
'exclude' => array( 'path' => '' ),
),
'target' => array (
'path' => '',
'exclude' => array( 'path' => '' ),
),
'template' => array( 'path' => '', 'locked' => false ),
);
$post['conf'][] = $this->collectPaths( $project, $tree );
}
}
/* @var LocoConfigElement $paths */
foreach( $this->query('exclude',$root) as $paths ){
$post['exclude'] = $this->collectPaths( $paths, $post['exclude'] );
}
return $post;
}
private function collectPaths( LocoConfigElement $parent, array $branch ){
$texts = array();
foreach( $parent as $child ){
$name = $child->nodeName;
// all file types as "path" in form model
if( 'file' === $name || 'directory' === $name ){
$name = 'path';
}
if( isset($branch[$name]) ){
// collect text if child is a <path> node
if( 'path' === $name ){
$file = $this->evaluateFileElement($child);
$path = $file->getRelativePath( $this->getDirectoryPath() );
if( '' === $path ){
$path = '.';
}
$texts[] = $path;
}
// else could be simple key to next depth
else if( is_array($branch[$name]) ){
$branch[$name] = $this->collectPaths( $child, $branch[$name] );
}
}
// @codeCoverageIgnoreStart
else {
throw new Exception('Unexpected structure: '.$name.' not in '.json_encode($branch) );
}
// @codeCoverageIgnoreEnd
}
// parent may have attributes we can set in branch data
foreach( $branch as $name => $default ){
if( $parent->hasAttribute($name) ){
if( is_bool($default) ){
$branch[$name] = $this->evaulateBooleanAttribute($parent, $name);
}
else {
$branch[$name] = $parent->getAttribute($name);
}
}
}
// set compiled path values if any collected
if( $texts ){
$value = implode("\n", $texts );
// display single root path as empty, but not when additional paths defined
if( '.' === $value ){
$branch['path'] = '';
}
else {
$branch['path'] = $value;
}
}
return $branch;
}
/**
* Construct model from posted form data.
* @return void
*/
public function loadForm( Loco_mvc_PostParams $post ){
// basic validation unlikely to fail when posted from UI
$name = $post->name;
if( ! $name ){
throw new InvalidArgumentException('Bundle must have a name');
}
$confs = $post->conf;
if( ! $confs || ! is_array($confs) ){
throw new InvalidArgumentException('Bundle must have at least one definition');
}
// transform posted data into internal model:
// deliberately not configuring bundle object at this point. simply converting data for storage.
$dom = $this->getDom();
$root = $dom->appendChild( $dom->createElement('bundle') );
$root->setAttribute( 'name', $name );
// bundle level excluded paths
if( $nodes = array_intersect_key( $post->getArrayCopy(), array( 'exclude' => '' ) ) ) {
$this->loadStruct( $root, $nodes );
}
// collect all projects grouped by domain
$domains = array();
foreach( $confs as $i => $conf ){
if( ! empty($conf['removed']) ){
continue;
}
if( empty($conf['domain']) ){
throw new InvalidArgumentException( __('Text Domain cannot be empty','loco-translate') );
}
$domains[ $conf['domain'] ][] = $project = $dom->createElement('project');
// project attributes
foreach( array('name','slug') as $attr ){
if( isset($conf[$attr]) ){
$project->setAttribute( $attr, $conf[$attr] );
}
}
// project children
if( $nodes = array_intersect_key( $conf, array( 'source' => '', 'target' => '', 'template' => '' ) ) ) {
$this->loadStruct( $project, $nodes );
}
}
// add all domains and their projects
foreach( $domains as $name => $projects ){
$parent = $root->appendChild( $dom->createElement('domain') );
$parent->setAttribute( 'name', $name );
/* @var $project LocoConfigElement */
foreach( $projects as $project ){
$parent->appendChild( $project );
}
}
}
/**
* Recursively add array structure into model.
* - Text nodes are split into one parent element per line.
* - Elements added here cannot have attributes, but are not expected to as they came from form fields
*/
private function loadStruct( LocoConfigElement $parent, array $nodes ){
$dom = $this->getDom();
foreach( $nodes as $name => $data ){
if( is_string($data) ){
// support common path containing elements
if( 'file' === $name || 'directory' === $name || 'path' === $name ){
// form model has multiline "path" nodes which we'll expand from non-empty lines
// resolving empty paths to "." must be done elsewhere. here empty means ignore.
foreach( preg_split('/\\R/', trim( $data,"\n\r"), -1, PREG_SPLIT_NO_EMPTY ) as $path ){
$ext = pathinfo( $path, PATHINFO_EXTENSION );
$child = $parent->appendChild( $dom->createElement( $ext ? 'file' : 'directory' ) );
$child->appendChild( $dom->createTextNode($path) );
}
}
// else assume valud is an attribute
else {
$parent->setAttribute( $name, $data );
}
}
else if( is_bool($data) ){
$data ? $parent->setAttribute($name,'true') : $parent->removeAttribute($name);
}
else if( ! is_array($data) ){
throw new InvalidArgumentException('Invalid datatype');
}
else {
$child = $parent->appendChild( $dom->createElement($name) );
$this->loadStruct( $child, $data );
}
}
}
}

View File

@@ -0,0 +1,180 @@
<?php
/**
* Generic configuration model serializer for portable Loco configs
*/
abstract class Loco_config_Model {
/**
* @var LocoConfigDocument
*/
private $dom;
/**
* Root directory for calculating relative file paths
* @var string
*/
private $base;
/**
* registry of location contants that may have been overridden
* @var array
*/
private $dirs;
/**
* @return Iterator
*/
abstract public function query( $query, $context = null );
/**
* @return LocoConfigDocument
*/
abstract public function createDom();
/**
*
*/
final public function __construct(){
$this->dirs = array();
$this->dom = $this->createDom();
$this->setDirectoryPath( loco_constant('ABSPATH') );
}
/**
* @return void
*/
public function setDirectoryPath( $path, $key = null ){
$path = untrailingslashit($path);
if( is_null($key) ){
$this->base = $path;
}
else {
$this->dirs[$key] = $path;
}
}
/**
* @return LocoConfigDocument
*/
public function getDom(){
return $this->dom;
}
/**
* Evaluate a name constant pointing to a file location
* @param string one of 'LOCO_LANG_DIR', 'WP_LANG_DIR', 'WP_PLUGIN_DIR', 'WPMU_PLUGIN_DIR', 'WP_CONTENT_DIR', or 'ABSPATH'
*/
public function getDirectoryPath( $key = null ){
if( is_null($key) ){
$value = $this->base;
}
else if( isset($this->dirs[$key]) ){
$value = $this->dirs[$key];
}
else {
$value = untrailingslashit( loco_constant($key) );
}
return $value;
}
/**
* @return LocoConfigElement
*/
public function createFileElement( Loco_fs_File $file ){
$node = $this->dom->createElement( $file->isDirectory() ? 'directory' : 'file' );
if( $path = $file->getPath() ) {
// Calculate relative path to the config file itself
$relpath = $file->getRelativePath( $this->base );
// Map to a configured base path if target is not under our root. This makes XML more portable
// matching order is most specific first, resulting in shortest path
if( $relpath && ( Loco_fs_File::abs($relpath) || '..' === substr($relpath,0,2) || $this->base === $this->getDirectoryPath('ABSPATH') ) ){
$bases = array( 'LOCO_LANG_DIR', 'WP_LANG_DIR', 'WP_PLUGIN_DIR', 'WPMU_PLUGIN_DIR', 'WP_CONTENT_DIR', 'ABSPATH' );
foreach( $bases as $key ){
if( ( $base = $this->getDirectoryPath($key) ) && $base !== $this->base ){
$base .= '/';
$len = strlen($base);
if( substr($path,0,$len) === $base ){
$node->setAttribute('base',$key);
$relpath = substr( $path, $len );
break;
}
} // @codeCoverageIgnore
}
}
$path = $relpath;
}
$this->setFileElementPath( $node, $path );
return $node;
}
/**
* @param LocoConfigElement
* @param string
* @return LocoConfigText
*/
protected function setFileElementPath( $node, $path ){
return $node->appendChild( $this->dom->createTextNode($path) );
}
/**
* @param LocoConfigElement
* @return Loco_fs_File
*/
public function evaluateFileElement( $el ){
$path = $el->textContent;
switch( $el->nodeName ){
case 'directory':
$file = new Loco_fs_Directory($path);
break;
case 'file':
$file = new Loco_fs_File($path);
break;
case 'path':
$file = new Loco_fs_File($path);
if( $file->isDirectory() ){
$file = new Loco_fs_Directory($path);
}
break;
default:
throw new InvalidArgumentException('Cannot evaluate file element from <'.$el->nodeName.'>');
}
if( $el->hasAttribute('base') ){
$key = $el->getAttribute('base');
$base = $this->getDirectoryPath($key);
$file->normalize( $base );
}
else {
$file->normalize( $this->base );
}
return $file;
}
/**
* @param LocoConfigElement
* @return bool
*/
public function evaulateBooleanAttribute( $el, $attr ){
if( ! $el->hasAttribute($attr) ){
return false;
}
$value = (string) $el->getAttribute($attr);
return 'false' !== $value && 'no' !== $value && '' !== $value;
}
}

View File

@@ -0,0 +1,191 @@
<?php
/**
* Holds a bundle definition in a DOM document
*/
class Loco_config_XMLModel extends Loco_config_Model {
/**
* @var DOMXpath
*/
private $xpath;
/**
* {@inheritdoc}
*/
public function createDom(){
$dom = new DOMDocument('1.0','utf-8');
$dom->formatOutput = true;
$dom->registerNodeClass('DOMElement','LocoConfig_DOMElement');
$this->xpath = new DOMXPath($dom);
return $dom;
}
/**
* {@inheritdoc}
* @return LocoConfigNodeListIterator
*/
public function query( $query, $context = null ){
$list = $this->xpath->query( $query, $context );
return new LocoConfigNodeListIterator( $list );
}
/**
* @return void
*/
public function loadXml( $source ){
if( ! $source ){
throw new Loco_error_XmlParseException( __('XML supplied is empty','loco-translate') );
}
$dom = $this->getDom();
// parse with silent errors, clearing after
$used_errors = libxml_use_internal_errors(true);
$result = $dom->loadXML( $source, LIBXML_NONET );
unset( $source );
// fetch errors and ensure clean for next run.
$errors = libxml_get_errors();
$used_errors || libxml_use_internal_errors(false);
libxml_clear_errors();
// Throw exception if error level exceeds current tolerance
if( $errors ){
/* @var $error LibXMLError */
foreach( $errors as $error ){
if( $error->level >= LIBXML_ERR_FATAL ){
$e = new Loco_error_XmlParseException( trim($error->message) );
//$e->setContext( $error->line, $error->column, $source );
throw $e;
} // @codeCoverageIgnoreStart
}
}
// @codeCoverageIgnoreEnd
// Not currently validating against a DTD, but may as well pre-empt generic model loading errors
if( ! $dom->documentElement || 'bundle' !== $dom->documentElement->nodeName ){
throw new Loco_error_XmlParseException('Expected <bundle> document element');
}
$this->xpath = new DOMXPath($dom);
}
/**
* {@inheritdoc}
* Overridden to avoid empty text nodes in XML files, preferring <file>.</file> to <file />
*/
protected function setFileElementPath( $node, $path ){
if( ! $path && '0' !== $path ){
$path = '.';
}
return parent::setFileElementPath( $node, $path );
}
}
/**
* @internal
*/
class LocoConfig_DOMElement extends DOMElement implements IteratorAggregate, Countable {
public function getIterator(){
return new LocoConfigNodeListIterator( $this->childNodes );
}
public function count(){
return $this->childNodes->length;
}
}
/**
* @internal
* Cos NodeList doesn't iterate
*/
class LocoConfigNodeListIterator implements Iterator, Countable, ArrayAccess {
/**
* @var DOMNodeList
*/
private $nodes;
/**
* @var int
*/
private $i;
/**
* @var int
*/
private $n;
public function __construct( DOMNodeList $nodes ){
$this->nodes = $nodes;
$this->n = $nodes->length;
}
public function count(){
return $this->n;
}
public function rewind(){
$this->i = -1;
$this->next();
}
public function key(){
return $this->i;
}
public function current(){
return $this->nodes->item( $this->i );
}
public function valid(){
return is_int($this->i);
}
public function next(){
while( true ){
$this->i++;
if( $child = $this->nodes->item($this->i) ){
break;
}
$this->i = null;
break;
}
}
public function offsetExists( $i ){
return $i >= 0 && $i < $this->n;
}
public function offsetGet( $i ){
return $this->nodes->item($i);
}
/**
* @codeCoverageIgnore
*/
public function offsetSet( $i, $value ){
throw new Exception('Read only');
}
/**
* @codeCoverageIgnore
*/
public function offsetUnset( $i ){
throw new Exception('Read only');
}
}

View File

@@ -0,0 +1,77 @@
<?php
/**
* Static, read-only caching of data held in serialized files.
* Used for pre-built arrays of information such as plural forms.
*/
class Loco_data_CompiledData implements ArrayAccess, Countable, IteratorAggregate {
/**
* @var array
*/
private static $reg;
/**
* @var string
*/
private $name;
/**
* @var array
*/
private $data;
/**
* @return Loco_data_CompiledData
*/
public static function get( $name ){
if( ! isset(self::$reg[$name]) ){
self::$reg[$name] = new Loco_data_CompiledData($name);
}
return self::$reg[$name];
}
private function __construct( $name ){
$path = 'lib/data/'.$name.'.php';
$this->data = loco_include( $path );
$this->name = $name;
}
public function destroy(){
unset( self::$reg[$this->name], $this->data );
}
public function offsetGet( $k ){
return isset($this->data[$k]) ? $this->data[$k] : null;
}
public function offsetExists( $k ){
return isset($this->data[$k]);
}
public function offsetUnset( $k ){
throw new RuntimeException('Read only');
}
public function offsetSet( $k, $v ){
throw new RuntimeException('Read only');
}
public function count(){
return count($this->data);
}
/**
* Implements IteratorAggregate::getIterator
* @return ArrayIterator
*/
public function getIterator(){
return new ArrayIterator( $this->data );
}
}

View File

@@ -0,0 +1,83 @@
<?php
/**
* Basic abstraction of cookie setting.
* - Provides loco_setcookie filter for tests.
* - Provides multiple values as url-encoded pairs. Not using JSON, because stripslashes
*
* Not currently used anywhere - replaced with usermeta-based session
* @codeCoverageIgnore
*/
class Loco_data_Cookie extends ArrayObject {
private $name = 'loco';
private $expires = 0;
/**
* Get and deserialize cookie sent to server
* @return Loco_data_Cookie
*/
public static function get( $name ){
if( isset($_COOKIE[$name]) ){
parse_str( $_COOKIE[$name], $data );
if( $data ){
$cookie = new Loco_data_Cookie( $data );
return $cookie->setName( $name );
}
}
}
/**
* @internal
*/
public function __toString(){
$data = $this->getArrayCopy();
return http_build_query( $data, null, '&' );
}
/**
* @return Loco_data_Cookie
*/
public function setName( $name ){
$this->name = $name;
return $this;
}
/**
* @return string
*/
public function getName(){
return $this->name;
}
/**
* Send cookie to the browser, unless filtered out.
* @return bool|null
*/
public function send(){
if( false !== apply_filters( 'loco_setcookie', $this ) ){
$value = (string) $this;
// @codeCoverageIgnoreStart
return setcookie( $this->name, $value, $this->expires, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true );
}
}
/**
* Empty values such that sending cookie would remove it from browser
* @return Loco_data_Cookie
*/
public function kill(){
$this->exchangeArray( array() );
$this->expires = time() - 86400;
return $this;
}
}

View File

@@ -0,0 +1,53 @@
<?php
/**
* Data object persisted as a WordPress "option"
*/
abstract class Loco_data_Option extends Loco_data_Serializable {
/**
* Get short suffix for use as end of option_name field.
* DB allows 191 characters including "loco_" prefix, leaving 185 bytes
* @return string
*/
abstract public function getKey();
/**
* Persist object in WordPress options database
* @return bool
*/
public function persist(){
$key = 'loco_'.$this->getKey();
return update_option( $key, $this->getSerializable(), false );
}
/**
* Retrieve and unserialize this object from WordPress options table
* @return bool whether object existed in cache
*/
public function fetch(){
$key = 'loco_'.$this->getKey();
if( $data = get_option($key) ){
try {
$this->setUnserialized($data);
return true;
}
catch( InvalidArgumentException $e ){
// suppress validation error
// @codeCoverageIgnore
}
}
return false;
}
/**
* Delete option from WordPress
*/
public function remove(){
$key = 'loco_'.$this->getKey();
return delete_option( $key );
}
}

View File

@@ -0,0 +1,188 @@
<?php
/**
* Abstraction of WordPress roles and capabilities and how they apply to Loco.
*
* - Currently only one capability exists, proving full access "loco_admin"
* - Any user with super admin privileges automatically inherits this permission
* - A single custom role is added called "translator"
*/
class Loco_data_Permissions {
/**
* Loco capabilities applicable to roles
* @var array
*/
private static $caps = array('loco_admin');
/**
* Polyfill for wp_roles which requires WP >= 4.3
* @return WP_Roles
*/
private static function wp_roles(){
global $wp_roles;
if( ! isset($wp_roles) ){
get_role('ping');
}
return $wp_roles;
}
/**
* Set up default roles and capabilities
* @return WP_Roles
*/
public static function init(){
$roles = self::wp_roles();
$apply = array();
// ensure translator role exists and is not locked out
if( $role = $roles->get_role('translator') ){
$role->has_cap('read') || $role->add_cap('read');
}
// else absence of translator role indicates first run
// by default we'll initially allow full access to anyone that can manage_options
else {
$apply['translator'] = $roles->add_role( 'translator', 'Translator', array('read'=>true) );
/* @var $role WP_Role */
foreach( $roles->role_objects as $id => $role ){
if( $role->has_cap('manage_options') ){
$apply[$id] = $role;
}
}
}
// fix broken permissions whereby super admin cannot access Loco at all.
// this could happen if another plugin added the translator role before hand.
if( ! isset($apply['administrator']) && ! is_multisite() ){
$apply['administrator'] = $roles->get_role('administrator');
}
/* @var $role WP_Role */
foreach( $apply as $role ){
if( $role instanceof WP_Role ){
foreach( self::$caps as $cap ){
$role->has_cap($cap) || $role->add_cap($cap);
}
}
}
return $roles;
}
/**
* Construct instance, ensuring default roles and capabilities exist
*/
public function __construct(){
self::init();
}
/**
* @return WP_Role[]
*/
public function getRoles(){
$roles = self::wp_roles();
return $roles->role_objects;
}
/**
* Check if role is protected such that user cannot lock themselves out when modifying settings
* @param WP_Role WordPress role object to check
* @return bool
*/
public function isProtectedRole( WP_Role $role ){
// if current user has this role and is not the super user, prevent lock-out
$user = wp_get_current_user();
if( $user instanceof WP_User && ! is_super_admin($user->ID) && $user->has_cap('manage_options') ){
return in_array( $role->name, $user->roles, true );
}
// admin users of single site install must never be denied access
// note that there is no such thing as a network admin role, but network admins have all permissions
return is_multisite() ? false : $role->has_cap('delete_users');
}
/**
* Completely remove all Loco permissions, as if uninstalling
* @return Loco_data_Permissions
*/
public function remove(){
/* @var $role WP_Role */
foreach( $this->getRoles() as $role ){
foreach( self::$caps as $cap ){
$role->has_cap($cap) && $role->remove_cap($cap);
}
}
// we'll only remove our custom role if it has no capabilities other than admin access
// this avoids breaking other plugins that use it, or added it before Loco was installed.
if( $role = get_role('translator') ){
if( ! $role->capabilities || array('read') === array_keys($role->capabilities) ){
remove_role('translator');
}
}
return $this;
}
/**
* Reset to default: roles include no Loco capabilities unless they have super admin privileges
* @return WP_Role[]
*/
public function reset(){
$roles = $this->getRoles();
/* @var $role WP_Role */
foreach( $roles as $role ){
// always provide access to site admins on first run
$grant = $this->isProtectedRole($role);
foreach( self::$caps as $cap ){
if( $grant ){
$role->has_cap($cap) || $role->add_cap($cap);
}
else {
$role->has_cap($cap) && $role->remove_cap($cap);
}
}
}
return $roles;
}
/**
* Get translated WordPress role name
* @param string
* @return string
*/
public function getRoleName( $id ){
if( 'translator' === $id ){
$label = _x( 'Translator', 'User role', 'loco-translate' );
}
else {
$names = self::wp_roles()->role_names;
$label = isset($names[$id]) ? translate_user_role( $names[$id] ) : $id;
}
return $label;
}
/**
* Populate permission settings from posted checkboxes
* @param string[]
* @return self
*/
public function populate( array $caps ){
// drop all permissions before adding (cos checkboxes)
$roles = $this->reset();
foreach( $caps as $id => $checked ){
if( isset($roles[$id]) ){
$role = $roles[$id];
/* @var $role WP_Role */
foreach( self::$caps as $cap ){
if( ! empty($checked[$cap]) ){
$role->has_cap($cap) || $role->add_cap($cap);
}
}
}
}
return $this;
}
}

View File

@@ -0,0 +1,112 @@
<?php
/**
* Data object persisted as a WordPress user meta entry under the loco_prefs key
*/
class Loco_data_Preferences extends Loco_data_Serializable {
/**
* User preference singletons
* @var array
*/
private static $current = array();
/**
* ID of the currently operational user
* @var int
*/
private $user_id = 0;
/**
* Available options and their defaults
* @var array
*/
private static $defaults = array (
// text to use in Last-Translator credit
'credit' => '',
);
/**
* Get current user's preferences
* @return Loco_data_Preferences
*/
public static function get(){
$id = get_current_user_id();
if( ! $id ){
throw new Exception('No current user');
}
if( isset(self::$current[$id]) ){
return self::$current[$id];
}
$prefs = self::create($id);
self::$current[$id] = $prefs;
$prefs->fetch();
return $prefs;
}
/**
* Create default settings instance
* @return Loco_data_Preferences
*/
public static function create( $id ){
$prefs = new Loco_data_Preferences( self::$defaults );
$prefs->user_id = $id;
return $prefs;
}
/**
* Persist object in WordPress usermeta table
* @return bool
*/
public function persist(){
return update_user_meta( $this->user_id, 'loco_prefs', $this->getSerializable() ) ? true : false;
}
/**
* Retrieve and unserialize this object from WordPress usermeta table
* @return bool whether object existed in cache
*/
public function fetch(){
$data = get_user_meta( $this->user_id, 'loco_prefs', true );
try {
$this->setUnserialized($data);
}
catch( InvalidArgumentException $e ){
return false;
}
return true;
}
/**
* Delete usermeta entry from WordPress
* return bool
*/
public function remove(){
$id = $this->user_id;
self::$current[$id] = null;
return delete_user_meta( $id, 'loco_prefs' );
}
/**
* Populate all settings from raw postdata.
* @return Loco_data_Preferences
*/
public function populate( array $data ){
// set all keys present in array
foreach( $data as $prop => $value ){
try {
$this->offsetSet( $prop, $value );
}
catch( InvalidArgumentException $e ){
// skipping invalid key
}
}
return $this;
}
}

View File

@@ -0,0 +1,128 @@
<?php
/**
* Recently items to display on home page
*/
class Loco_data_RecentItems extends Loco_data_Option {
/**
* Global instance of recent items
* @var Loco_data_RecentItems
*/
private static $current;
/**
* {@inheritdoc}
*/
public function getKey(){
return 'recent';
}
/**
* @return Loco_data_RecentItems
*/
public static function get(){
if( ! self::$current ){
self::$current = new Loco_data_RecentItems;
self::$current->fetch();
}
return self::$current;
}
/**
* Trash data and remove from memory
*/
public static function destroy(){
$tmp = new Loco_data_RecentItems;
$tmp->remove();
self::$current = null;
}
/**
* @internal
* @return Loco_data_RecentItems
*/
private function push( $object, array $indexes ){
foreach( $indexes as $key => $id ){
$stack = isset($this[$key]) ? $this[$key] : array();
// remove before add ensures latest item appended to hashmap
unset($stack[$id]);
$stack[$id] = time();
$this[$key] = $stack;
// TODO prune stack to maximum length
}
return $this;
}
/**
* @return array
*/
private function getItems( $key, $offset, $count ){
$stack = isset($this[$key]) ? $this[$key] : array();
// hash map should automatically be in "push" order, meaning most recent last
// sorting gives wrong order for same-second updates (only relevent in tests, but still..)
// asort( $stack, SORT_NUMERIC );
$stack = array_reverse( array_keys( $stack ) );
if( is_null($count) && 0 === $offset ){
return $stack;
}
return array_slice( $stack, $offset, $count, false );
}
/**
* @return int
*/
private function hasItem( $key, $id ){
if( isset($this[$key]) && ( $items = $this[$key] ) && isset($items[$id]) ){
return $items[$id];
}
return 0;
}
/**
* Push bundle to the front of recent bundles stack
* @return Loco_data_RecentItems
*/
public function pushBundle( Loco_package_Bundle $bundle ){
return $this->push( $bundle, array( 'bundle' => $bundle->getId() ) );
}
/**
* Get bundle IDs
* @return array
*/
public function getBundles( $offset = 0, $count = null ){
return $this->getItems('bundle', $offset, $count );
}
/**
* Check if a bundle has been recently used
* @return int timestamp item was added, 0 if absent
*/
public function hasBundle( $id ){
return $this->hasItem( 'bundle', $id );
}
/**
* TODO other types of item
* Push project to the front of recent bundles stack
* @return Loco_data_RecentItems
*
public function pushProject( Loco_package_Project $project ){
return $this;
}*/
}

View File

@@ -0,0 +1,205 @@
<?php
/**
* Generic array-like object that may be serialized as an array and committed into WordPress data stores.
*/
abstract class Loco_data_Serializable extends ArrayObject {
/**
* Object version, can be used for validation and migrations.
* @var string|int|float
*/
private $v = 0;
/**
* Time object was last persisted
* @var int
*/
private $t = 0;
/**
* @var bool
*/
private $dirty;
/**
* Whether persisting on object destruction
* @var bool
*/
private $lazy = false;
/**
* Commit serialized data to WordPress storage
* @return mixed
*/
abstract public function persist();
/**
* {@inheritdoc}
*/
public function __construct( array $data = array() ){
$this->setFlags( ArrayObject::ARRAY_AS_PROPS );
parent::__construct( $data );
$this->dirty = (bool) $data;
}
/**
* @internal
*/
final public function __destruct(){
if( $this->lazy ){
$this->persistIfDirty();
}
}
/**
* Check if object's properties have change since last clean
* @return bool
*/
public function isDirty(){
return $this->dirty;
}
/**
* Make not dirty
* @return self
*/
protected function clean(){
$this->dirty = false;
return $this;
}
/**
* Force dirtiness for next check
* @return static
*/
protected function touch(){
$this->dirty = true;
return $this;
}
/**
* Enable lazy persistence on object destruction, if dirty
* @return static
*/
public function persistLazily(){
$this->lazy = true;
return $this;
}
/**
* Call persist method only if has changed since last clean
* @return static
*/
public function persistIfDirty(){
if( $this->isDirty() ){
$this->persist();
}
return $this;
}
/**
* {@inheritdoc}
* override so we can set dirty flag
*/
public function offsetSet( $prop, $value ){
if( ! isset($this[$prop]) || $value !== $this[$prop] ){
parent::offsetSet( $prop, $value );
$this->dirty = true;
}
}
/**
* {@inheritdoc}
* override so we can set dirty flag
*/
public function offsetUnset( $prop ){
if( isset($this[$prop]) ){
parent::offsetUnset($prop);
$this->dirty = true;
}
}
/**
* @param string|int|float
* @return self
*/
public function setVersion( $version ){
if( $version !== $this->v ){
$this->v = $version;
$this->dirty = true;
}
return $this;
}
/**
* @return string|int|float
*/
public function getVersion(){
return $this->v;
}
/**
* @return int
*/
public function getTimestamp(){
return $this->t;
}
/**
* Get serializable data for storage
* @return array
*/
protected function getSerializable(){
return array (
'c' => get_class($this),
'v' => $this->getVersion(),
'd' => $this->getArrayCopy(),
't' => time(),
);
}
/**
* Restore object state from array as returned from getSerializable
* @param array
* @return self
*/
protected function setUnserialized( $data ){
if( ! is_array($data) || ! isset($data['d']) ) {
throw new InvalidArgumentException('Unexpected data');
}
if( get_class($this) !== $data['c'] ){
throw new InvalidArgumentException('Unexpected class name');
}
// ok to populate ArrayObject
$this->exchangeArray( $data['d'] );
// setting version as it was in database
$this->setVersion( $data['v'] );
// timestamp may not be present in old objects
$this->t = isset($data['t']) ? $data['t'] : 0;
// object is being restored, probably from disk so start with clean state
$this->dirty = false;
return $this;
}
}

View File

@@ -0,0 +1,200 @@
<?php
/**
* Abstracts session data access using WP_Session_Tokens
*/
class Loco_data_Session extends Loco_data_Serializable {
/**
* @var Loco_data_Session
*/
private static $current;
/**
* Value from wp_get_session_token
* @var string
*/
private $token;
/**
* @var WP_User_Meta_Session_Tokens
*/
private $manager;
/**
* Dirty flag: TODO abstract into array access setters
* @var bool
*/
private $dirty = false;
/**
* @return Loco_data_Session
*/
public static function get(){
if( ! self::$current ){
new Loco_data_Session;
}
return self::$current;
}
/**
* Trash data and remove from memory
*/
public static function destroy(){
if( self::$current ){
try {
self::$current->clear();
}
catch( Exception $e ){
// probably no session to destroy
}
self::$current = null;
}
}
/**
* Commit current session data to WordPress storage and remove from memory
*/
public static function close(){
if( self::$current && self::$current->dirty ){
self::$current->persist();
self::$current = null;
}
}
/**
* @internal
*/
final public function __construct( array $raw = array() ){
$this->token = wp_get_session_token();
if( ! $this->token ){
throw new Loco_error_Exception('Failed to get session token');
}
parent::__construct( array() );
$this->manager = WP_Session_Tokens::get_instance( get_current_user_id() );
// populate object from stored session data
$data = $this->getRaw();
if( isset($data['loco']) ){
$this->setUnserialized( $data['loco'] );
}
// any initial arbitrary data can be merged on top
foreach( $raw as $prop => $value ){
$this[$prop] = $value;
}
// enforce single instance
self::$current = $this;
// ensure against unclean shutdown
if( loco_debugging() ){
register_shutdown_function( array($this,'_on_shutdown') );
}
}
/**
* @internal
* Ensure against unclean use of session storage
*/
public function _on_shutdown(){
if( $this->dirty ){
trigger_error('Unclean session shutdown: call either Loco_data_Session::destroy or Loco_data_Session::close');
}
}
/**
* Get raw session data held by WordPress
* @return array
*/
private function getRaw(){
$data = $this->manager->get( $this->token );
// session data will exist if WordPress login is valid
if( ! $data || ! is_array($data) ){
throw new Loco_error_Exception('Invalid session');
}
return $data;
}
/**
* Persist object in WordPress usermeta table
* @return Loco_data_Session
*/
public function persist(){
$data = $this->getRaw();
$data['loco'] = $this->getSerializable();
$this->manager->update( $this->token, $data );
$this->dirty = false;
return $this;
}
/**
* Clear object data and remove our key from WordPress usermeta record
* @return Loco_data_Session
*/
public function clear(){
$data = $this->getRaw();
if( isset($data['loco']) ){
unset( $data['loco'] );
$this->manager->update( $this->token, $data );
}
$this->exchangeArray( array() );
$this->dirty = false;
return $this;
}
/**
* @param string name of messages bag, e.g. "errors"
* @param mixed optionally put data in rather than getting data out
* @return mixed
*/
public function flash( $bag, $data = null ){
if( isset($data) ){
$this->dirty = true;
$this[$bag][] = $data;
return;
}
// else get first object in bag and remove before returning
if( isset($this[$bag]) ){
if( $data = array_shift($this[$bag]) ){
$this->dirty = true;
return $data;
}
}
}
/**
* @internal
*/
public function offsetSet( $index, $newval ){
if( ! isset($this[$index]) || $newval !== $this[$index] ){
$this->dirty = true;
parent::offsetSet( $index, $newval );
}
}
/**
* @internal
*/
public function offsetUnset( $index ){
if( isset($this[$index]) ){
$this->dirty = true;
parent::offsetUnset( $index );
}
}
}

View File

@@ -0,0 +1,220 @@
<?php
/**
* Global plugin settings stored in a single WordPress site option.
*
* @property string $version Current plugin version installed
* @property bool $gen_hash Whether to compile hash table into MO files
* @property bool $use_fuzzy Whether to include Fuzzy strings in MO files
* @property int $num_backups Number of backups to keep of Gettext files
* @property array $pot_alias Alternative names for POT files in priority order
* @property array $php_alias Alternative file extensions for PHP files
* @property array $jsx_alias Registered extensions for scanning JavaScript/JSX files (disabled by default)
* @property bool $fs_persist Whether to remember file system credentials in session
* @property int $fs_protect Prevent modification of files in system folders (0:off, 1:warn, 2:block)
* @property string $max_php_size Skip PHP source files this size or larger
* @property bool $po_utf8_bom Whether to prepend PO and POT files with UTF-8 byte order mark
* @property string $po_width PO/POT file maximum line width (wrapping) zero to disable
* @property bool $jed_pretty Whether to pretty print JSON JED files
* @property bool $ajax_files Whether to submit PO data as concrete files (requires Blob support in Ajax)
*/
class Loco_data_Settings extends Loco_data_Serializable {
/**
* Global instance of this plugin's settings
* @var Loco_data_Settings
*/
private static $current;
/**
* Available options and their defaults
* @var array
*/
private static $defaults = array (
'version' => '',
'gen_hash' => false,
'use_fuzzy' => true,
'num_backups' => 1,
'pot_alias' => array( 'default.po', 'en_US.po', 'en.po' ),
'php_alias' => array( 'php', 'twig' ),
'jsx_alias' => array(),
'fs_persist' => false,
'fs_protect' => 1,
'max_php_size' => '100K',
'po_utf8_bom' => false,
'po_width' => '79',
'jed_pretty' => false,
'ajax_files' => true,
);
/**
* Create default settings instance
* @return Loco_data_Settings
*/
public static function create(){
$args = self::$defaults;
$args['version'] = loco_plugin_version();
return new Loco_data_Settings( $args );
}
/**
* Get currently configured global settings
* @return Loco_data_Settings
*/
public static function get(){
$opts = self::$current;
if( ! $opts ){
$opts = self::create();
$opts->fetch();
self::$current = $opts;
// allow hooks to modify settings
do_action('loco_settings', $opts );
}
return $opts;
}
/**
* Destroy current settings
* @return void
*/
public static function clear(){
delete_option('loco_settings');
self::$current = null;
}
/**
* Destroy current settings and return a fresh one
* @return Loco_data_Settings
*/
public static function reset(){
self::clear();
return self::$current = self::create();
}
/**
* {@inheritdoc}
*/
public function offsetSet( $prop, $value ){
if( ! isset(self::$defaults[$prop]) ){
throw new InvalidArgumentException('Invalid option, '.$prop );
}
$default = self::$defaults[$prop];
// cast to same type as default
if( is_bool($default) ){
$value = (bool) $value;
}
else if( is_int($default) ){
$value = (int) $value;
}
else if( is_array($default) ){
if( ! is_array($value) ){
$value = preg_split( '/[\\s,]+/', trim($value), -1, PREG_SPLIT_NO_EMPTY );
}
}
else {
$value = (string) $value;
}
parent::offsetSet( $prop, $value );
}
/**
* Commit current settings to WordPress DB
* @return bool
*/
public function persist(){
$this->version = loco_plugin_version();
$this->clean();
return update_option('loco_settings', $this->getSerializable() );
}
/**
* Pull current settings from WordPress DB and merge into this object
* @return bool whether settings where previously saved
*/
public function fetch(){
if( $data = get_option('loco_settings') ){
$copy = new Loco_data_Settings;
$copy->setUnserialized($data);
// preserve any defaults not in previously saved data
// this will occur if we've added options since setting were saved
$data = $copy->getArrayCopy() + $this->getArrayCopy();
// could ensure redundant keys are removed, but no need currently
// $data = array_intersect_key( $data, self::$defaults );
$this->exchangeArray( $data );
$this->clean();
return true;
}
return false;
}
/**
* Run migration in case plugin has been upgraded since settings last saved
* @return bool whether upgrade has occurred
*/
public function migrate(){
$updated = false;
// Always update version number in settings after an upgrade
if( version_compare($this->version,loco_plugin_version(),'<') ){
$this->persist();
$updated = true;
}
return $updated;
}
/**
* Populate all settings from raw postdata.
* @param array posted setting values
* @return Loco_data_Settings
*/
public function populate( array $data ){
// set all keys present in array
foreach( $data as $prop => $value ){
try {
$this->offsetSet( $prop, $value );
}
catch( InvalidArgumentException $e ){
// skipping invalid key
}
}
// set missing boolean keys as false, because checkboxes
if( $missing = array_diff_key(self::$defaults,$data) ){
foreach( $missing as $prop => $default ){
if( is_bool($default) ){
parent::offsetSet( $prop, false );
}
}
}
// enforce missing values that must have a default
foreach( array('php_alias','max_php_size','po_width') as $prop ){
if( isset($data[$prop]) && '' === $data[$prop] ){
parent::offsetSet( $prop, self::$defaults[$prop] );
}
}
return $this;
}
/**
* Map a file extension to registered types, defaults to "php"
* @param string
* @return string php, js or twig
*/
public function ext2type($x){
$x = strtolower($x);
$types = array_fill_keys( $this->jsx_alias, 'js' );
$types['twig'] = 'twig'; // <- temporary hack in lieu of dedicated twig extractor
return isset($types[$x]) ? $types[$x] : 'php';
}
}

View File

@@ -0,0 +1,77 @@
<?php
/**
*
*/
abstract class Loco_data_Transient extends Loco_data_Serializable {
/**
* Lifespan to persist object in transient cache
* @var int seconds
*/
private $ttl = 0;
/**
* Get short suffix for use as end of cache key.
* DB allows 191 characters including "_transient_timeout_loco_" prefix, leaving 167 bytes
* @return string
*/
abstract public function getKey();
/**
* Persist object in WordPress cache
* @param int
* @param bool
* @return Loco_data_Transient
*/
public function persist(){
$key = 'loco_'.$this->getKey();
set_transient( $key, $this->getSerializable(), $this->ttl );
$this->clean();
return $this;
}
/**
* Retrieve and unserialize this object from WordPress transient cache
* @return bool whether object existed in cache
*/
public function fetch(){
$key = 'loco_'.$this->getKey();
$data = get_transient( $key );
try {
$this->setUnserialized($data);
return true;
}
catch( InvalidArgumentException $e ){
return false;
}
}
/**
* @param int
* @return self
*/
public function setLifespan( $ttl ){
$this->ttl = (int) $ttl;
return $this;
}
/**
* Set keep-alive interval
* @param int
* @return self
*/
public function keepAlive( $timeout ){
$time = $this->getTimestamp();
// legacy objects (with ttl=0) had no timestamp, so will always be touched.
// make dirty if this number of seconds has elapsed since last persisted.
if( time() > ( $time + $timeout ) ){
$this->touch()->persistLazily();
}
return $this;
}
}

View File

@@ -0,0 +1,71 @@
<?php
/**
* Simple wrapper for transient file uploads carrying PO data.
* Doesn't move or persist uploaded files, so doesn't call wp_handle_upload()
*/
class Loco_data_Upload {
/**
* @var Loco_fs_File
*/
private $file;
/**
* Pass through temporary file data
* @param string key in $_FILES known to exist
* @return string
* @throws Loco_error_UploadException
*/
public static function src($key){
$upload = new Loco_data_Upload($_FILES[$key]);
return $upload->file->getContents();
}
/**
* @param array member of $_FILE
* @throws Loco_error_UploadException
*/
public function __construct( array $data ){
// https://www.php.net/manual/en/features.file-upload.errors.php
$code = (int) $data['error'];
switch( $code ){
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_INI_SIZE:
throw new Loco_error_UploadException('The uploaded file exceeds the upload_max_filesize directive in php.ini',$code);
case UPLOAD_ERR_FORM_SIZE:
throw new Loco_error_UploadException('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',$code);
case UPLOAD_ERR_PARTIAL:
throw new Loco_error_UploadException('The uploaded file was only partially uploaded',$code);
case UPLOAD_ERR_NO_FILE:
throw new Loco_error_UploadException('No file was uploaded, or data is empty',$code);
case UPLOAD_ERR_NO_TMP_DIR:
throw new Loco_error_UploadException('Missing temporary folder for uploaded file',$code);
case UPLOAD_ERR_CANT_WRITE:
throw new Loco_error_UploadException('Failed to save uploaded file to disk',$code);
case UPLOAD_ERR_EXTENSION:
throw new Loco_error_UploadException('Your server blocked the file upload',$code);
default:
throw new Loco_error_UploadException('Unknown file upload error',$code);
}
// mime check is largely pointless but may as well check as we'll only send one type
if( 'application/x-gettext' !== $data['type'] ){
throw new Loco_error_UploadException('Unsupported file type, expected PO or POT file');
}
// upload is OK according to PHP, but check it's really readable and not empty
$path = $data['tmp_name'];
$file = new Loco_fs_File($path);
if( ! $file->exists() ){
throw new Loco_error_UploadException('Uploaded file is not readable',UPLOAD_ERR_NO_FILE);
}
if( 0 === $file->size() ){
throw new Loco_error_UploadException('Uploaded file contains no data',UPLOAD_ERR_NO_FILE);
}
// file is really ok
$this->file = $file;
}
}

View File

@@ -0,0 +1,218 @@
<?php
class Loco_error_AdminNotices extends Loco_hooks_Hookable {
/**
* @var Loco_error_AdminNotices
*/
private static $singleton;
/**
* @var array
*/
private $errors = array();
/**
* Inline messages are handled by our own template views
* @var bool
*/
private $inline = false;
/**
* @return Loco_error_AdminNotices
*/
public static function get(){
self::$singleton or self::$singleton = new Loco_error_AdminNotices;
return self::$singleton;
}
/**
* @param Loco_error_Exception
* @return Loco_error_Exception
*/
public static function add( Loco_error_Exception $error ){
$notices = self::get();
$notices->errors[] = $error;
// do late flush if we missed the boat
if( did_action('loco_admin_init') ){
$notices->on_loco_admin_init();
}
if( did_action('admin_notices') ){
$notices->on_admin_notices();
}
// if exception wasn't thrown we have to do some work to establish where it was invoked
if( __FILE__ === $error->getFile() ){
$error->setCallee(1);
}
// Log messages of minimum priority and up, depending on debug mode
// note that non-debug level is in line with error_reporting set by WordPress (notices ignored)
$priority = loco_debugging() ? Loco_error_Exception::LEVEL_DEBUG : Loco_error_Exception::LEVEL_WARNING;
if( $error->getLevel() <= $priority ){
$error->log();
}
return $error;
}
/**
* Raise a success message
* @param string
* @return Loco_error_Exception
*/
public static function success( $message ){
$notice = new Loco_error_Success($message);
return self::add( $notice->setCallee(1) );
}
/**
* Raise a failure message
* @param string
* @return Loco_error_Exception
*/
public static function err( $message ){
$notice = new Loco_error_Exception($message);
return self::add( $notice->setCallee(1) );
}
/**
* Raise a warning message
* @param string
* @return Loco_error_Exception
*/
public static function warn( $message ){
$notice = new Loco_error_Warning($message);
return self::add( $notice->setCallee(1) );
}
/**
* Raise a generic info message
* @param string
* @return Loco_error_Exception
*/
public static function info( $message ){
$notice = new Loco_error_Notice($message);
return self::add( $notice->setCallee(1) );
}
/**
* Raise a debug notice, if debug is enabled
* @param string
* @return Loco_error_Debug
*/
public static function debug( $message ){
$notice = new Loco_error_Debug($message);
$notice->setCallee(1);
loco_debugging() and self::add( $notice );
return $notice;
}
/**
* Destroy and return buffer
* @return array
*/
public static function destroy(){
if( $notices = self::$singleton ){
$buffer = $notices->errors;
$notices->errors = array();
self::$singleton = null;
return $buffer;
}
return array();
}
/**
* Destroy and return all serialized notices, suitable for ajax response
* @return array
*/
public static function destroyAjax(){
$data = array();
/* @var $notice Loco_error_Exception */
foreach( self::destroy() as $notice ){
$data[] = $notice->jsonSerialize();
}
return $data;
}
/**
* @return void
*/
private function flush(){
if( $this->errors ){
$htmls = array();
/* $var $error Loco_error_Exception */
foreach( $this->errors as $error ){
$html = sprintf (
'<p><strong class="has-icon">%s:</strong> <span>%s</span></p>',
esc_html( $error->getTitle() ),
esc_html( $error->getMessage() )
);
$styles = array( 'notice', 'notice-'.$error->getType() );
if( $this->inline ){
$styles[] = 'inline';
}
if( $links = $error->getLinks() ){
$styles[] = 'has-nav';
$html .= '<nav>'.implode( '<span> | </span>', $links ).'</nav>';
}
$htmls[] = '<div class="'.implode(' ',$styles).'">'.$html.'</div>';
}
$this->errors = array();
echo implode("\n", $htmls),"\n";
}
}
/**
* admin_notices action handler.
*/
public function on_admin_notices(){
if( ! $this->inline ){
$this->flush();
}
}
/**
* loco_admin_notices callback.
* Unlike WordPress "admin_notices" this fires from within template layout at the point we want them, hence they are marked as "inline"
*/
public function on_loco_admin_notices(){
$this->inline = true;
$this->flush();
}
/**
* loco_admin_init callback
* When we know a Loco admin controller will render the page we will control the point at which notices are printed
*/
public function on_loco_admin_init(){
$this->inline = true;
}
/**
* @internal
* Make sure we always see notices if hooks didn't fire
*/
public function __destruct(){
$this->inline = false;
if( ! loco_doing_ajax() ){
$this->flush();
}
}
}

View File

@@ -0,0 +1,45 @@
<?php
/**
* Developer notice
*/
class Loco_error_Debug extends Loco_error_Exception {
/**
* {@inheritdoc}
*/
public function getType(){
return 'debug';
}
/**
* {@inheritdoc}
*/
public function getTitle(){
return __('Debug','loco-translate');
}
/**
* {@inheritdoc}
*/
public function getLevel(){
return Loco_error_Exception::LEVEL_DEBUG;
}
/**
* Log debugging message to file without raising admin notice
* @param string
* @codeCoverageIgnore
*/
public static function trace( $message ){
if( 1 < func_get_args() ){
$message = call_user_func_array('sprintf',func_get_args());
}
$debug = new Loco_error_Debug($message);
$debug->setCallee(1);
$debug->log();
}
}

View File

@@ -0,0 +1,201 @@
<?php
/**
* Generic exception that we know has come from the Loco plugin
*/
class Loco_error_Exception extends Exception implements JsonSerializable {
const LEVEL_ERROR = 0;
const LEVEL_WARNING = 1;
const LEVEL_DEBUG = 2;
const LEVEL_NOLOG = 3;
/**
* Links to help docs etc.. to show along side error message
* @var array
*/
private $links = array();
/**
* Override file in which exception was thrown
* @var string
*/
private $_file;
/**
* Override line number from where exception was thrown
* @var int
*/
private $_line;
/**
* {@inheritdoc}
*/
public function __construct( $message = '', $code = 0, $previous = null ) {
parent::__construct( $message, $code, $previous );
}
/**
* @return Throwable
*/
private function getRootException(){
$current = $this;
// note that getPrevious is absent in PHP < 5.3
while( method_exists($current,'getPrevious') && ( $next = $current->getPrevious() ) ){
$current = $next;
}
return $current;
}
/**
* @return string
*/
public function getRealFile(){
if( $this->_file ){
return $this->_file;
}
return $this->getRootException()->getFile();
}
/**
* @return int
*/
public function getRealLine(){
if( $this->_line ){
return $this->getLine();
}
return $this->getRootException()->getLine();
}
/**
* @return array
*/
public function getRealTrace(){
return $this->getRootException()->getTrace();
}
/**
* @param int number of levels up from callee
* @return Loco_error_Exception
*/
public function setCallee( $depth = 0 ){
$stack = debug_backtrace(0);
$callee = $stack[$depth];
$this->_file = $callee['file'];
$this->_line = $callee['line'];
// TODO could also log the stack trace from $depth upwards, but not required unless being logged or thrown
return $this;
}
/**
* Write this error to file regardless of log level
* @param Loco_error_Exception
* @return void
*/
public function log(){
$file = new Loco_fs_File( $this->getRealFile() );
$path = $file->getRelativePath( loco_plugin_root() );
$text = sprintf('[Loco.%s] "%s" in %s:%u', $this->getType(), $this->getMessage(), $path, $this->getRealLine() );
// separate error log in CWD for tests
if( 'cli' === PHP_SAPI && defined('LOCO_TEST') && LOCO_TEST ){
error_log( '['.date('c').'] '.$text."\n", 3, 'debug.log' );
}
// Else write to default PHP log, but note that WordPress may have set this to wp-content/debug.log.
// If no `error_log` is set this will send message to the SAPI, so check your httpd/fast-cgi errors too.
else {
error_log( $text, 0 );
}
}
/**
* Get view template for rendering error to HTML.
* @return string path relative to root tpl directory
*/
public function getTemplate(){
return 'admin/errors/generic';
}
/**
* Get notice level short code as a string
* @return string
*/
public function getType(){
return 'error';
}
/**
* Get verbosity level
* @return int
*/
public function getLevel(){
return self::LEVEL_ERROR;
}
/**
* Get localized notice level name
* @return string
*/
public function getTitle(){
return __('Error','loco-translate');
}
/**
* @return array
*/
public function jsonSerialize(){
return array (
'code' => $this->getCode(),
'type' => $this->getType(),
'class' => get_class($this),
'title' => $this->getTitle(),
'message' => $this->getMessage(),
//'file' => str_replace( ABSPATH, '', $this->getRealFile() ),
//'line' => $this->getRealLine()
);
}
/**
* Push navigation links into error. Use for help pages etc..
* @param string
* @param string
* @return Loco_error_Exception
*/
public function addLink( $href, $text ){
$this->links[] = sprintf('<a href="%s">%s</a>', esc_url($href), esc_html($text) );
return $this;
}
/**
* @return array
*/
public function getLinks(){
return $this->links;
}
/**
* Convert generic exception to one of ours
* @param Exception original error
* @return Loco_error_Exception
*/
public static function convert( Exception $e ){
if( $e instanceof Loco_error_Exception ){
return $e;
}
return new Loco_error_Exception( $e->getMessage(), $e->getCode(), $e );
}
}

View File

@@ -0,0 +1,7 @@
<?php
/**
*
*/
class Loco_error_LocaleException extends Loco_error_Exception {
}

View File

@@ -0,0 +1,31 @@
<?php
/**
* Generic, non-critical informational notice
* Not to be confused with an error notice. This is for onscreen messages, and won't be logged.
*/
class Loco_error_Notice extends Loco_error_Exception {
/**
* {@inheritdoc}
*/
public function getType(){
return 'info';
}
/**
* {@inheritdoc}
*/
public function getTitle(){
return __('Notice','loco-translate');
}
/**
* {@inheritdoc}
*/
public function getLevel(){
return Loco_error_Exception::LEVEL_NOLOG;
}
}

View File

@@ -0,0 +1,47 @@
<?php
/**
*
*/
class Loco_error_ParseException extends Loco_error_Exception {
/**
* @var string
*/
private $context;
/**
* @param int line number
* @param int column number
* @param string source in which to identify line and column
* @return self
*/
public function setContext( $line, $column, $source ){
$this->context = null;
// If line given as 0 then treat column as offset in an unknown number of lines
if( 0 === $line ){
$lines = preg_split( '/\\r?\\n/', substr($source,0,$column));
$line = count($lines);
$column = strlen( end($lines) );
}
// get line of source code where error is and construct a ____^ thingy to show error on next line
// this requires that full source is passed in, so line number must be real
if( loco_debugging() ){
$lines = preg_split( '/\\r?\\n/', $source, $line+1 );
$offset = $line - 1;
if( isset($lines[$offset]) ){
$this->context = $lines[$offset] ."\n". str_repeat(' ', max(0,$column) ).'^';
}
}
// wrap initial message with context data
$this->message = sprintf("Error at line %u, column %u: %s", $line, $column, $this->message );
return $this;
}
/**
* @return string
*/
public function getContext(){
return $this->context;
}
}

View File

@@ -0,0 +1,30 @@
<?php
/**
* Success message. Not really an exception obviously, but compatible with Loco_error_AdminNotices
*/
class Loco_error_Success extends Loco_error_Exception {
/**
* {@inheritdoc}
*/
public function getType(){
return 'success';
}
/**
* {@inheritdoc}
*/
public function getTitle(){
return __('OK','loco-translate');
}
/**
* {@inheritdoc}
*/
public function getLevel(){
return Loco_error_Exception::LEVEL_NOLOG;
}
}

View File

@@ -0,0 +1,8 @@
<?php
/**
* Exception type for failed uploads
*/
class Loco_error_UploadException extends Loco_error_Exception {
}

View File

@@ -0,0 +1,30 @@
<?php
/**
* Generic warning
*/
class Loco_error_Warning extends Loco_error_Exception {
/**
* {@inheritdoc}
*/
public function getType(){
return 'warning';
}
/**
* {@inheritdoc}
*/
public function getTitle(){
return __('Warning','loco-translate');
}
/**
* {@inheritdoc}
*/
public function getLevel(){
return Loco_error_Exception::LEVEL_WARNING;
}
}

View File

@@ -0,0 +1,16 @@
<?php
/**
* File system write error.
* Generally thrown from Loco_fs_FileWriter
*/
class Loco_error_WriteException extends Loco_error_Exception {
/**
* {@inheritdoc}
*/
public function getTitle(){
return __('Permission denied','loco-translate');
}
}

View File

@@ -0,0 +1,11 @@
<?php
/**
*
*/
class Loco_error_XmlParseException extends Loco_error_Exception {
public function getTitle(){
return __('XML parse error','loco-translate');
}
}

View File

@@ -0,0 +1,54 @@
<?php
/**
*
*/
class Loco_fs_Directory extends Loco_fs_File {
/**
* Recursive flag for internal use
* @var bool
*/
private $r = false;
/**
* {@inheritDoc}
*/
public function isDirectory(){
return true;
}
/**
* Set recursive flag for use when traversing directory trees
* @param bool
* @return Loco_fs_Directory
*/
public function setRecursive( $bool ){
$this->r = (bool) $bool;
return $this;
}
/**
* @return bool
*/
public function isRecursive(){
return $this->r;
}
/**
* Create this directory for real.
*
* @throws Loco_error_WriteException
* @return Loco_fs_Directory
*/
public function mkdir(){
if( ! $this->exists() ){
$this->getWriteContext()->mkdir();
}
return $this;
}
}

View File

@@ -0,0 +1,172 @@
<?php
/**
* Dummy file that just holds content in memory.
* Use when you don't want to commit data to disk but you need to pass a typed file object
*/
class Loco_fs_DummyFile extends Loco_fs_File {
/**
* @var string
*/
private $contents = '';
/**
* @var int
*/
private $mtime = 0;
/**
* @var int
*/
private $fmode = 0644;
/**
* @var int
*/
private $uid = 0;
/**
* @var int
*/
private $gid = 0;
public function __construct($path){
parent::__construct($path);
$this->mtime = time();
}
/**
* {@inheritdoc}
*/
public function exists(){
return false;
}
/**
* {@inheritdoc}
*/
public function getContents(){
return $this->contents;
}
/**
* {@inheritdoc}
*/
public function size(){
return strlen($this->contents);
}
/**
* {@inheritdoc}
*/
public function putContents( $contents ){
$this->contents = (string) $contents;
return $this;
}
/**
* {@inheritdoc}
*/
public function modified(){
return $this->mtime;
}
/**
* Allow forcing of modified stamp for testing purposes
* @return Loco_fs_File
*/
public function touch( $modified ){
$this->mtime = (int) $modified;
return $this;
}
/**
* {@inheritdoc}
*/
public function mode(){
return $this->fmode;
}
/**
* {@inheritdoc}
*/
public function chmod( $mode, $recursive = false ){
$this->fmode = (int) $mode;
return $this;
}
/**
* TODO implement in parent
*/
public function chown( $uid = null, $gid = null ){
if( is_int($uid) ){
$this->uid = $uid;
}
if( is_int($gid) ){
$this->gid = $gid;
}
return $this;
}
/**
* {@inheritdoc}
*/
public function copy( $dest ){
$copy = clone $this;
$copy->path = $dest;
return $copy;
}
/**
* {@inheritdoc}
*/
public function uid(){
return $this->uid;
}
/**
* {@inheritdoc}
*/
public function gid(){
return $this->gid;
}
/**
* {@inheritdoc}
* @codeCoverageIgnore
*/
public function writable(){
$mode = $this->mode();
// world writable
if( $mode & 02 ){
return true;
}
// group writable
if( ( $mode & 020 ) && $this->gid() === Loco_compat_PosixExtension::getgid() ){
return true;
}
// owner writable
if( ( $mode & 0200 ) && $this->uid() === Loco_compat_PosixExtension::getuid() ){
return true;
}
// else locked:
return false;
}
}

View File

@@ -0,0 +1,644 @@
<?php
/**
*
*/
class Loco_fs_File {
/**
* @var Loco_fs_FileWriter
*/
private $w;
/**
* Path to file
* @var string
*/
private $path;
/**
* Cached pathinfo() data
* @var array
*/
private $info;
/**
* Base path which path has been normalized against
* @var string
*/
private $base;
/**
* Flag set when current path is relative
* @var bool
*/
private $rel;
/**
* Check if a path is absolute and return fixed slashes for readability
* @param string
* @return string fixed path, or "" if not absolute
*/
public static function abs( $path ){
if( $path = (string) $path ){
$chr1 = $path{0};
// return unmodified path if starts "/"
if( '/' === $chr1 ){
return $path;
}
// Windows drive path if "X:" or network path if "\\"
if( isset($path{1}) ){
$chr2 = $path{1};
if( ':' === $chr2 || ( '\\' === $chr1 && '\\' === $chr2 ) ){
return strtoupper($chr1).$chr2.strtr( substr($path,2), '\\', '/' );
}
}
}
// else path is relative, so return falsey string
return '';
}
/**
* Create file with initial, unvalidated path
* @param string
*/
public function __construct( $path ){
$this->setPath( $path );
}
/**
* Internally set path value and flag whether relative or absolute
* @param string
* @return string
*/
private function setPath( $path ){
$path = (string) $path;
if( $fixed = self::abs($path) ){
$path = $fixed;
$this->rel = false;
}
else {
$this->rel = true;
}
if( $path !== $this->path ){
$this->path = $path;
$this->info = null;
}
return $path;
}
/**
* @return bool
*/
public function isAbsolute(){
return ! $this->rel;
}
/**
* @internal
*/
public function __clone(){
$this->cloneWriteContext( $this->w );
}
/**
* Copy write context with our file reference
* @param Loco_fs_FileWriter
* @return Loco_fs_File
*/
private function cloneWriteContext( Loco_fs_FileWriter $context = null ){
if( $context ){
$context = clone $context;
$this->w = $context->setFile($this);
}
return $this;
}
/**
* Get file system context for operations that *modify* the file system.
* Read operations and operations that stat the file will always do so directly.
* @return Loco_fs_FileWriter
*/
public function getWriteContext(){
if( ! $this->w ){
$this->w = new Loco_fs_FileWriter( $this );
}
return $this->w;
}
/**
* @internal
*/
private function pathinfo(){
return is_array($this->info) ? $this->info : ( $this->info = pathinfo($this->path) );
}
/**
* @return bool
*/
public function exists(){
return file_exists( $this->path );
}
/**
* @return bool
*/
public function writable(){
return $this->getWriteContext()->writable();
}
/**
* @return bool
*/
public function deletable(){
$parent = $this->getParent();
if( $parent && $parent->writable() ){
// sticky directory requires that either the file its parent is owned by effective user
if( $parent->mode() & 01000 ){
$writer = $this->getWriteContext();
if( $writer->isDirect() && ( $uid = Loco_compat_PosixExtension::getuid() ) ){
return $uid === $this->uid() || $uid === $parent->uid();
}
// else delete operation won't be done directly, so can't pre-empt sticky problems
// TODO is it worth comparing FTP username etc.. for ownership?
}
// defaulting to "deletable" based on fact that parent is writable.
return true;
}
return false;
}
/**
* Get owner uid
* @return int
*/
public function uid(){
return fileowner($this->path);
}
/**
* Get group gid
* @return int
*/
public function gid(){
return filegroup($this->path);
}
/**
* Check if file can't be overwitten when existent, nor created when non-existent
* This does not check permissions recursively as directory trees are not built implicitly
* @return bool
*/
public function locked(){
if( $this->exists() ){
return ! $this->writable();
}
if( $dir = $this->getParent() ){
return ! $dir->writable();
}
return true;
}
/**
* Check if full path can be built to non-existent file.
* @return bool
*/
public function creatable(){
$file = $this;
while( $file = $file->getParent() ){
if( $file->exists() ){
return $file->writable();
}
}
return false;
}
/**
* @return string
*/
public function dirname(){
$info = $this->pathinfo();
return $info['dirname'];
}
/**
* @return string
*/
public function basename(){
$info = $this->pathinfo();
return $info['basename'];
}
/**
* @return string
*/
public function filename(){
$info = $this->pathinfo();
return $info['filename'];
}
/**
* @return string
*/
public function extension(){
$info = $this->pathinfo();
return isset($info['extension']) ? $info['extension'] : '';
}
/**
* @return string
*/
public function getPath(){
return $this->path;
}
/**
* Get file modification time as unix timestamp in seconds
* @return int
*/
public function modified(){
return filemtime( $this->path );
}
/**
* Get file size in bytes
* @return int
*/
public function size(){
return filesize( $this->path );
}
/**
* @return int
*/
public function mode(){
if( is_link($this->path) ){
$stat = lstat( $this->path );
$mode = $stat[2];
}
else {
$mode = fileperms($this->path);
}
return $mode;
}
/**
* Set file mode
* @param int file mode integer e.g 0664
* @param bool whether to set recursively (directories)
* @return Loco_fs_File
*/
public function chmod( $mode, $recursive = false ){
$this->getWriteContext()->chmod( $mode, $recursive );
return $this->clearStat();
}
/**
* Clear stat cache if any file data has changed
* @return Loco_fs_File
*/
public function clearStat(){
$this->info = null;
// PHP 5.3.0 Added optional clear_realpath_cache and filename parameters.
if( version_compare( PHP_VERSION, '5.3.0', '>=' ) ){
clearstatcache( true, $this->path );
}
// else no choice but to drop entire stat cache
else {
clearstatcache();
}
return $this;
}
/**
* @return string
*/
public function __toString(){
return $this->getPath();
}
/**
* Check if passed path is equal to ours
* @param string
* @return bool
*/
public function equal( $path ){
return $this->path === (string) $path;
}
/**
* Normalize path for string comparison, resolves redundant dots and slashes.
* @param string path to prefix
* @return string
*/
public function normalize( $base = '' ){
if( $path = self::abs($base) ){
$base = $path;
}
if( $base !== $this->base ){
$path = $this->path;
if( '' === $path ){
$this->setPath($base);
}
else {
if( ! $this->rel || ! $base ){
$b = array();
}
else {
$b = self::explode( $base, array() );
}
$b = self::explode( $path, $b );
$this->setPath( implode('/',$b) );
}
$this->base = $base;
}
return $this->path;
}
/**
* @param string
* @param string[]
* @return array
*/
private static function explode( $path, array $b ){
$a = explode( '/', $path );
foreach( $a as $i => $s ){
if( '' === $s ){
if( 0 !== $i ){
continue;
}
}
if( '.' === $s ){
continue;
}
if( '..' === $s ){
if( array_pop($b) ){
continue;
}
}
$b[] = $s;
}
return $b;
}
/**
* Get path relative to given location, unless path is already relative
* @param string base path
* @return string path relative to given base
*/
public function getRelativePath( $base ){
$path = $this->normalize();
if( $abspath = self::abs($path) ){
// base may needs require normalizing
$file = new Loco_fs_File($base);
$base = $file->normalize();
$length = strlen($base);
// if we are below given base path, return ./relative
if( substr($path,0,$length) === $base ){
++$length;
if( isset($path{$length}) ){
return substr( $path, $length );
}
// else paths were identical
return '';
}
// else attempt to find nearest common root
$i = 0;
$source = explode('/',$base);
$target = explode('/',$path);
while( isset($source[$i]) && isset($target[$i]) && $source[$i] === $target[$i] ){
$i++;
}
if( $i > 1 ){
$depth = count($source) - $i;
$build = array_merge( array_fill( 0, $depth, '..' ), array_slice( $target, $i ) );
$path = implode( '/', $build );
}
}
// else return unmodified
return $path;
}
/**
* @return bool
*/
public function isDirectory(){
if( file_exists($this->path) ){
return is_dir($this->path);
}
return ! $this->extension();
}
/**
* Load contents of file into a string
* @return string
*/
public function getContents(){
return file_get_contents( $this->path );
}
/**
* Check if path is under a theme directory
* @return bool
*/
public function underThemeDirectory(){
return Loco_fs_Locations::getThemes()->check( $this->path );
}
/**
* Check if path is under a plugin directory
* @return bool
*/
public function underPluginDirectory(){
return Loco_fs_Locations::getPlugins()->check( $this->path );
}
/**
* Check if path is under wp-content directory
* @return bool
*/
public function underContentDirectory(){
return Loco_fs_Locations::getContent()->check( $this->path );
}
/**
* Check if path is under WordPress root directory (ABSPATH)
* @return bool
*/
public function underWordPressDirectory(){
return Loco_fs_Locations::getRoot()->check( $this->path );
}
/**
* Check if path is under the global system directory
* @return bool
*/
public function underGlobalDirectory(){
return Loco_fs_Locations::getGlobal()->check( $this->path );
}
/**
* @return Loco_fs_Directory|null
*/
public function getParent(){
$dir = null;
$path = $this->dirname();
if( '.' !== $path && $this->path !== $path ){
$dir = new Loco_fs_Directory( $path );
$dir->cloneWriteContext( $this->w );
}
return $dir;
}
/**
* Copy this file for real
* @param string new path
* @throws Loco_error_WriteException
* @return Loco_fs_File new file
*/
public function copy( $dest ){
$copy = clone $this;
$copy->path = $dest;
$copy->clearStat();
$this->getWriteContext()->copy($copy);
return $copy;
}
/**
* Move/rename this file for real
* @param Loco_fs_File target file with new path
* @throws Loco_error_WriteException
* @return Loco_fs_File original file that should no longer exist
*/
public function move( Loco_fs_File $dest ){
$this->getWriteContext()->move($dest);
return $this->clearStat();
}
/**
* Delete this file for real
* @throws Loco_error_WriteException
* @return Loco_fs_File
*/
public function unlink(){
$recursive = $this->isDirectory();
$this->getWriteContext()->delete( $recursive );
return $this->clearStat();
}
/**
* Copy this object with an alternative file extension
* @param string new extension
* @return Loco_fs_File
*/
public function cloneExtension( $ext ){
$snip = strlen( $this->extension() );
$file = clone $this;
if( $snip ){
$file->path = substr_replace( $this->path, $ext, - $snip );
}
else {
$file->path .= '.'.$ext;
}
$file->info = null;
return $file;
}
/**
* Ensure full parent directory tree exists
* @return Loco_fs_Directory
*/
public function createParent(){
if( $dir = $this->getParent() ){
if( ! $dir->exists() ){
$dir->mkdir();
}
}
return $dir;
}
/**
* @param string file contents
* @return int number of bytes written to file
*/
public function putContents( $data ){
$this->getWriteContext()->putContents($data);
$this->clearStat();
return $this->size();
}
/**
* Establish what part of the WordPress file system this is.
* Value is that used by WP_Automatic_Updater::should_update.
* @return string "core", "plugin", "theme" or "translation"
*/
public function getUpdateType(){
// global languages directory root, and canonical subdirectories
$dirpath = (string) ( $this->isDirectory() ? $this : $this->getParent() );
if( $sub = Loco_fs_Locations::getGlobal()->rel($dirpath) ){
list($root) = explode('/', $sub, 2 );
if( '.' === $root || 'themes' === $root || 'plugins' === $root ){
return 'translation';
}
}
// theme and plugin locations can be at any depth
else if( $this->underThemeDirectory() ){
return 'theme';
}
else if( $this->underPluginDirectory() ){
return 'plugin';
}
// core locations are under WordPress root, but not under wp-content
else if( $this->underWordPressDirectory() && ! $this->underContentDirectory() ){
return 'core';
}
// else not an update type
return '';
}
}

View File

@@ -0,0 +1,510 @@
<?php
/**
* Lazy file iterator. Pulls directory listings when required.
*/
class Loco_fs_FileFinder implements Iterator, Countable, Loco_fs_FileListInterface {
/**
* Top-level search directories
* @var Loco_fs_FileList
*/
private $roots;
/**
* All directories to search, including those recursed into
* @var Loco_fs_FileList
*/
private $subdir;
/**
* Whether directories all read into memory
* @var bool
*/
private $cached;
/**
* File listing already matched
* @var Loco_fs_FileList
*/
private $cache;
/**
* Internal array pointer for whole list of paths
* @var int
*/
private $i;
/**
* Internal pointer for directory being read
* @var int
*/
private $d;
/**
* Current directory being read
* @var resource
*/
private $dir;
/**
* Path of current directory being read
* @var string
*/
private $cwd;
/**
* Whether directories added to search will be recursive by default
* @var bool
*/
private $recursive = false;
/**
* Whether currently recursing into subdirectories
* This is switched on and off as each directories is opened
* @var bool
*/
private $recursing;
/**
* Whether to follow symlinks when recursing into subdirectories
* Root-level symlinks are always resolved when possible
* @var bool
*/
private $symlinks = true;
/**
* Registry of followed links by their original path
* @var Loco_fs_FileList
*/
private $linked;
/**
* List of file extensions to filter on and group by
* @var Loco_fs_FileList[]
*/
private $exts;
/**
* List of directory names to exclude from recursion
* @var Loco_fs_File[]
*/
private $excluded;
/**
* Create initial list of directories to search
* @param string default root to start
*/
public function __construct( $root = '' ){
$this->roots = new Loco_fs_FileList;
$this->linked = new Loco_fs_FileList;
$this->excluded = array();
if( $root ){
$this->addRoot( $root );
}
}
/**
* Set recursive state of all defined roots
* @param bool
* @return Loco_fs_FileFinder
*/
public function setRecursive( $bool ){
$this->invalidate();
$this->recursive = $bool;
/* @var $dir Loco_fs_Directory */
foreach( $this->roots as $dir ){
$dir->setRecursive( $bool );
}
return $this;
}
/**
* @param bool
* @return Loco_fs_FileFinder
*/
public function followLinks( $bool ){
$this->invalidate();
$this->symlinks = (bool) $bool;
return $this;
}
/**
* @param string
* @return Loco_fs_Link
*/
public function getFollowed( $path ){
$path = (string) $path;
/* @var Loco_fs_Link $link */
foreach( $this->linked as $link ){
$file = $link->resolve();
$orig = $file->getPath();
// exact match on followed path
if( $orig === $path ){
return $link;
}
// match further up the directory tree
if( $file instanceof Loco_fs_Directory ){
$orig = trailingslashit($orig);
$snip = strlen($orig);
if( $orig === substr($path,0,$snip) ){
return new Loco_fs_Link( $link->getPath().'/'.substr($path,$snip) );
}
}
}
return null;
}
/**
* @return void
*/
private function invalidate(){
$this->cached = false;
$this->cache = null;
$this->subdir = null;
}
/**
* @return Loco_fs_FileList
*/
public function export(){
if( ! $this->cached ){
$this->rewind();
while( $this->valid() ){
$this->next();
}
}
return $this->cache;
}
/**
* @return Loco_fs_FileList[]
*/
public function exportGroups(){
$this->cached || $this->export();
return $this->exts;
}
/**
* Add a directory root to search.
* @param string
* @param bool|null
* @return Loco_fs_FileFinder
*/
public function addRoot( $root, $recursive = null ){
$this->invalidate();
$dir = new Loco_fs_Directory($root);
$this->roots->add( $dir );
// new directory inherits current global setting unless set explicitly
$dir->setRecursive( is_bool($recursive) ? $recursive : $this->recursive );
return $this;
}
/**
* Get all root directories to be searched
* @return Loco_fs_FileList
*/
public function getRootDirectories(){
return $this->roots;
}
/**
* Group results by file extension
* @return Loco_fs_FileFinder
*/
public function group(){
return $this->groupBy( func_get_args() );
}
/**
* Group results by file extensions given in array
* @param array file extensions
* @return Loco_fs_FileFinder
*/
public function groupBy( array $exts ){
$this->invalidate();
$this->exts = array();
foreach( $exts as $ext ){
$this->exts[ trim($ext,'*.') ] = new Loco_fs_FileList;
}
return $this;
}
/**
* Add one or more paths to exclude from listing
* @param string e.g "node_modules"
* @return Loco_fs_FileFinder
*/
public function exclude(){
$this->invalidate();
foreach( func_get_args() as $path ){
$file = new Loco_fs_File($path);
// if path is absolute, add straight onto list
if( $file->isAbsolute() ){
$file->normalize();
$this->excluded[] = $file;
}
// else append to all defined roots
else {
foreach( $this->roots as $dir ) {
$file = new Loco_fs_File( $dir.'/'.$path );
$file->normalize();
$this->excluded[] = $file;
}
}
}
return $this;
}
/**
* Export excluded paths as file objects
* @return Loco_fs_File[]
*/
public function getExcluded(){
return $this->excluded;
}
/**
* @param Loco_fs_Directory
* @return void
*/
private function open( Loco_fs_Directory $dir ){
$path = $dir->getPath();
$recursive = $dir->isRecursive();
if( is_link($path) ){
$link = new Loco_fs_Link($path);
if( $link->isDirectory() ){
$path = $link->resolve()->getPath();
$this->linked->add($link);
}
}
$this->cwd = $path;
$this->recursing = $recursive;
$this->dir = opendir($path);
}
/**
* @return void
*/
private function close(){
closedir( $this->dir );
$this->dir = null;
$this->recursing = null;
}
/**
* Test if given path is matched by one of our exclude rules
* TODO would prefer a method that didn't require iteration
* @param string
* @return bool
*/
public function isExcluded( $path ){
/* @var $excl Loco_fs_File */
foreach( $this->excluded as $excl ){
if( $excl->equal($path) ){
return true;
}
}
return false;
}
/**
* Read next valid file path from root directories
* @return Loco_fs_File|null
*/
private function read(){
$path = null;
if( is_resource($this->dir) ){
while( $f = readdir($this->dir) ){
// dot-files always excluded
if( '.' === $f{0} ){
continue;
}
$path = $this->cwd.'/'.$f;
// follow symlinks (subdir hash ensures against loops)
if( is_link($path) ){
if( ! $this->symlinks ){
continue;
}
$link = new Loco_fs_Link($path);
if( $file = $link->resolve() ){
$path = $file->getPath();
$this->linked->add($link);
}
else {
continue;
}
}
// add subdirectory to recursion list
// this will result in breadth-first listing
if( is_dir($path) ){
if( $this->recursing && ! $this->isExcluded($path) ){
$subdir = new Loco_fs_Directory($path);
$subdir->setRecursive(true);
$this->subdir->add( $subdir );
}
continue;
}
else if( $this->isExcluded($path) ){
continue;
}
// file represented as object containing original path
$file = new Loco_fs_File($path);
$this->add( $file );
return $file;
}
$this->close();
}
// try next dir if nothing matched in this one
$d = $this->d + 1;
if( isset($this->subdir[$d]) ){
$this->d = $d;
$this->open( $this->subdir[$d] );
return $this->read();
}
// else at end of all available files
$this->cached = true;
return null;
}
/**
* {@inheritDoc}
*/
public function add( Loco_fs_File $file ){
if( $this->exts ){
$ext = $file->extension();
if( ! isset($this->exts[$ext]) ){
return false;
}
$this->exts[$ext]->add($file);
}
if( $this->cache->add($file) ){
$this->i++;
return true;
}
return false;
}
/**
* @return int
*/
public function count(){
return count( $this->export() );
}
/**
* @return Loco_fs_File|null
*/
public function current(){
$i = $this->i;
if( is_int($i) && isset($this->cache[$i]) ){
return $this->cache[$i];
}
return null;
}
/**
* @return Loco_fs_File|null
*/
public function next(){
if( $this->cached ){
$i = $this->i + 1;
if( isset($this->cache[$i]) ){
$this->i = $i;
return $this->cache[$i];
}
}
else if( $path = $this->read() ){
return $path;
}
// else at end of all directory listings
$this->i = null;
return null;
}
/**
* @return int
*/
public function key(){
return $this->i;
}
/**
* @return bool
*/
public function valid(){
// may be in lazy state after rewind
// must do initial read now in case list is empty
return is_int($this->i);
}
/**
* @return void
*/
public function rewind(){
if( $this->cached ){
reset( $this->cache );
$this->i = key($this->cache);
}
else {
$this->d = 0;
$this->dir = null;
$this->cache = new Loco_fs_FileList;
// add only root directories that exist
$this->subdir = new Loco_fs_FileList;
/* @var Loco_fs_Directory */
foreach( $this->roots as $root ){
if( $root instanceof Loco_fs_Directory && $root->exists() && ! $this->isExcluded( $root->getPath() ) ){
$this->subdir->add($root);
}
}
if( $this->subdir->offsetExists(0) ){
$this->i = -1;
$this->open( $this->subdir->offsetGet(0) );
$this->next();
}
else {
$this->i = null;
$this->subdir = null;
$this->cached = true;
}
}
}
/**
* test whether internal list has been fully cached in memory
*/
public function isCached(){
return $this->cached;
}
}

View File

@@ -0,0 +1,132 @@
<?php
/**
* Simple list of file paths
*/
class Loco_fs_FileList extends ArrayIterator implements Loco_fs_FileListInterface {
/**
* Hash map for ensuring files only added once
* @var array
*/
private $unique = array();
/**
* Construct with initial list if files
* @param Loco_fs_File[]
*/
public function __construct( $a = array() ){
parent::__construct( array() );
foreach( $a as $file ){
$this->add( $file );
}
}
/**
* Use instead of clone because that does weird things to ArrayIterator instances.
* Note that this does NOT clone individual file members.
* @return Loco_fs_FileList
*/
public function copy(){
return new Loco_fs_FileList( $this->getArrayCopy() );
}
/**
* Like getArrayCopy, but exports string paths
* @return array
*/
public function export(){
$a = array();
foreach( $this as $file ){
$a[] = (string) $file;
}
return $a;
}
/**
* @internal
* @return string
*/
public function __toString(){
return implode( "\n", $this->getArrayCopy() );
}
/**
* Generate a unique key for file
* @param Loco_fs_File
* @return string
*/
private function hash( Loco_fs_File $file ){
$path = $file->normalize();
// if file is real, we must resolve its real path
if( $file->exists() && ( $real = realpath($path) ) ){
$path = $real;
}
return $path;
}
/**
* {@inheritDoc}
*/
public function offsetSet( $index, $value ){
throw new Exception('Use Loco_fs_FileList::add');
}
/**
* {@inheritDoc}
*/
public function add( Loco_fs_File $file ){
$hash = $this->hash( $file );
if( isset($this->unique[$hash]) ){
return false;
}
$this->unique[$hash] = true;
parent::offsetSet( null, $file );
return true;
}
/**
* Check if given file is already in list
* @param Loco_fs_File
* @return bool
*/
public function has( Loco_fs_File $file ){
$hash = $this->hash( $file );
return isset($this->unique[$hash]);
}
/**
* Get a copy of list with only files not contained in passed list
* @param Loco_fs_FileList
* @return Loco_fs_FileList
*/
public function diff( Loco_fs_FileList $not_in ){
$list = new Loco_fs_FileList;
foreach( $this as $file ){
$not_in->has($file) || $list->add( $file );
}
return $list;
}
/**
* Merge another list of the SAME TYPE uniquely on top of current one
* @param Loco_fs_FileList
* @return Loco_fs_FileList
*/
public function augment( loco_fs_FileList $list ){
foreach( $list as $file ){
$this->add( $file );
}
return $this;
}
}

View File

@@ -0,0 +1,12 @@
<?php
interface Loco_fs_FileListInterface extends Countable, Iterator {
/**
* Add a file uniquely
* @param Loco_fs_File
* @return bool whether file was added (and didn't already exist)
*/
public function add( Loco_fs_File $file );
}

View File

@@ -0,0 +1,79 @@
<?php
/**
* Object representing a file's permission bits
*/
class Loco_fs_FileMode {
/**
* inode protection mode
* @var int
*/
private $i;
/**
* Instantiate from integer file mode
*/
public function __construct( $mode ){
$this->i = (int) $mode;
}
/**
* @return string
*/
public function __toString(){
return sprintf('%03o', $this->i & 07777 );
}
/**
* rwx style friendly formatting
* @return string
*/
public function format(){
$mode = $this->i;
$setuid = $mode & 04000;
$setgid = $mode & 02000;
$sticky = $mode & 01000;
return
$this->type().
( $mode & 0400 ? 'r' : '-' ).
( $mode & 0200 ? 'w' : '-' ).
( $mode & 0100 ? ($setuid?'s':'x') : ($setuid?'S':'-') ).
( $mode & 0040 ? 'r' : '-' ).
( $mode & 0020 ? 'w' : '-' ).
( $mode & 0010 ? ($setgid?'s':'x') : ($setgid?'S':'-') ).
( $mode & 0004 ? 'r' : '-' ).
( $mode & 0002 ? 'w' : '-' ).
( $mode & 0001 ? ($sticky?'t':'x') : ($sticky?'T':'-') );
}
/**
* File type bit field:
* http://man7.org/linux/man-pages/man2/stat.2.html
*/
public function type(){
$mode = $this->i & 0170000;
switch( $mode ){
case 0010000:
return '-';
case 0040000:
return 'd';
case 0120000:
return 'l';
case 0140000:
return 's';
case 0060000:
return 'c';
default:
return '-';
}
}
}

View File

@@ -0,0 +1,323 @@
<?php
/**
* Provides write operation context via the WordPress file system API
*/
class Loco_fs_FileWriter {
/**
* @var Loco_fs_File
*/
private $file;
/**
* @var WP_Filesystem_Base
*/
private $fs;
/**
* @param Loco_fs_File
*/
public function __construct( Loco_fs_File $file ){
$this->file = $file;
$this->disconnect();
}
/**
* @param Loco_fs_File
* @return Loco_fs_FileWriter
*/
public function setFile( Loco_fs_File $file ){
$this->file = $file;
return $this;
}
/**
* Connect to alternative file system context
*
* @param WP_Filesystem_Base
* @param bool whether reconnect required
* @return Loco_fs_FileWriter
* @throws Loco_error_WriteException
*/
public function connect( WP_Filesystem_Base $fs, $disconnected = true ){
if( $disconnected && ! $fs->connect() ){
$errors = $fs->errors;
if( is_wp_error($errors) ){
foreach( $errors->get_error_messages() as $reason ){
Loco_error_AdminNotices::warn($reason);
}
}
throw new Loco_error_WriteException( __('Failed to connect to remote server','loco-translate') );
}
$this->fs = $fs;
return $this;
}
/**
* Revert to direct file system connection
* @return Loco_fs_FileWriter
*/
public function disconnect(){
$this->fs = Loco_api_WordPressFileSystem::direct();
return $this;
}
/**
* Get mapped path for use in indirect file system manipulation
* @return string
*/
public function getPath(){
return $this->mapPath( $this->file->getPath() );
}
/**
* Map virtual path for remote file system
* @param string
* @return string
*/
private function mapPath( $path ){
if( ! $this->isDirect() ){
$base = untrailingslashit( Loco_fs_File::abs(loco_constant('WP_CONTENT_DIR')) );
$snip = strlen($base);
if( substr( $path, 0, $snip ) !== $base ){
// fall back to default path in case of symlinks
$base = trailingslashit(ABSPATH).'wp-content';
$snip = strlen($base);
if( substr( $path, 0, $snip ) !== $base ){
throw new Loco_error_WriteException('Remote path must be under WP_CONTENT_DIR');
}
}
$virt = $this->fs->wp_content_dir();
if( false === $virt ){
throw new Loco_error_WriteException('Failed to find WP_CONTENT_DIR via remote connection');
}
$virt = untrailingslashit( $virt );
$path = substr_replace( $path, $virt, 0, $snip );
}
return $path;
}
/**
* Test if a direct (not remote) file system
* @return bool
*/
public function isDirect(){
return $this->fs instanceof WP_Filesystem_Direct;
}
/**
* @return bool
*/
public function writable(){
return ! $this->disabled() && $this->fs->is_writable( $this->getPath() );
}
/**
* @param int file mode integer e.g 0664
* @param bool whether to set recursively (directories)
* @return Loco_fs_FileWriter
* @throws Loco_error_WriteException
*/
public function chmod( $mode, $recursive = false ){
$this->authorize();
if( ! $this->fs->chmod( $this->getPath(), $mode, $recursive ) ){
throw new Loco_error_WriteException( sprintf( __('Failed to chmod %s','loco-translate'), $this->file->basename() ) );
}
return $this;
}
/**
* @param Loco_fs_File target for copy
* @return Loco_fs_FileWriter
* @throws Loco_error_WriteException
*/
public function copy( Loco_fs_File $copy ){
$this->authorize();
$source = $this->getPath();
$target = $this->mapPath( $copy->getPath() );
// bugs in WP file system "exists" methods means we must force $overwrite=true; so checking file existence first
if( $copy->exists() ){
Loco_error_AdminNotices::debug(sprintf('Cannot copy %s to %s (target already exists)',$source,$target));
throw new Loco_error_WriteException( __('Refusing to copy over an existing file','loco-translate') );
}
// ensure target directory exists, although in most cases copy will be in situ
$parent = $copy->getParent();
if( $parent && ! $parent->exists() ){
$this->mkdir($parent);
}
// perform WP file system copy method
if( ! $this->fs->copy($source,$target,true) ){
Loco_error_AdminNotices::debug(sprintf('Failed to copy %s to %s via "%s" method',$source,$target,$this->fs->method));
throw new Loco_error_WriteException( sprintf( __('Failed to copy %s to %s','loco-translate'), basename($source), basename($target) ) );
}
return $this;
}
/**
* @param Loco_fs_File target file with new path
* @return Loco_fs_FileWriter
* @throws Loco_error_WriteException
*/
public function move( Loco_fs_File $dest ){
$orig = $this->file;
try {
// target should have been authorized to create the new file
$context = clone $dest->getWriteContext();
$context->setFile($orig);
$context->copy($dest);
// source should have been authorized to delete the original file
$this->delete(false);
return $this;
}
catch( Loco_error_WriteException $e ){
Loco_error_AdminNotices::debug('copy/delete failure: '.$e->getMessage() );
throw new Loco_error_WriteException( sprintf( 'Failed to move %s', $orig->basename() ) );
}
}
/**
* @param bool
* @return Loco_fs_FileWriter
* @throws Loco_error_WriteException
*/
public function delete( $recursive = false ){
$this->authorize();
if( ! $this->fs->delete( $this->getPath(), $recursive ) ){
throw new Loco_error_WriteException( sprintf( __('Failed to delete %s','loco-translate'), $this->file->basename() ) );
}
return $this;
}
/**
* @param string
* @return Loco_fs_FileWriter
* @throws Loco_error_WriteException
*/
public function putContents( $data ){
$this->authorize();
$file = $this->file;
if( $file->isDirectory() ){
throw new Loco_error_WriteException( sprintf( __('"%s" is a directory, not a file','loco-translate'), $file->basename() ) );
}
// file having no parent directory is likely an error, like a relative path.
$dir = $file->getParent();
if( ! $dir ){
throw new Loco_error_WriteException( sprintf('Bad file path "%s"',$file) );
}
// avoid chmod of existing file
if( $file->exists() ){
$mode = $file->mode();
}
// may have bypassed definition of FS_CHMOD_FILE
else {
$mode = defined('FS_CHMOD_FILE') ? FS_CHMOD_FILE : 0644;
// new file may also require directory path building
if( ! $dir->exists() ){
$this->mkdir($dir);
}
}
$fs = $this->fs;
$path = $this->getPath();
if( ! $fs->put_contents($path,$data,$mode) ){
// provide useful reason for failure if possible
if( $file->exists() && ! $file->writable() ){
Loco_error_AdminNotices::debug( sprintf('File not writable via "%s" method, check permissions on %s',$fs->method,$path) );
throw new Loco_error_WriteException( __("Permission denied to update file",'loco-translate') );
}
// directory path should exist or have thrown error earlier.
// directory path may not be writable by same fs context
if( ! $dir->writable() ){
Loco_error_AdminNotices::debug( sprintf('Directory not writable via "%s" method; check permissions for %s',$fs->method,$dir) );
throw new Loco_error_WriteException( __("Parent directory isn't writable",'loco-translate') );
}
// else reason for failure is not established
Loco_error_AdminNotices::debug( sprintf('Unknown write failure via "%s" method; check %s',$fs->method,$path) );
throw new Loco_error_WriteException( __('Failed to save file','loco-translate').': '.$file->basename() );
}
return $this;
}
/**
* Create current directory context
* @param Loco_fs_File optional directory
* @return bool
* @throws Loco_error_WriteException
*/
public function mkdir( Loco_fs_File $here = null ) {
if( is_null($here) ){
$here = $this->file;
}
$this->authorize();
$fs = $this->fs;
// may have bypassed definition of FS_CHMOD_DIR
$mode = defined('FS_CHMOD_DIR') ? FS_CHMOD_DIR : 0755;
// find first ancestor that exists while building tree
$stack = array();
/* @var $parent Loco_fs_Directory */
while( $parent = $here->getParent() ){
array_unshift( $stack, $this->mapPath( $here->getPath() ) );
if( $parent->exists() ){
// have existent directory, now build full path
foreach( $stack as $path ){
if( ! $fs->mkdir($path,$mode) ){
Loco_error_AdminNotices::debug( sprintf('mkdir(%s,%03o) failed via "%s" method;',var_export($path,1),$mode,$fs->method) );
throw new Loco_error_WriteException( __('Failed to create directory','loco-translate') );
}
}
return true;
}
$here = $parent;
}
// refusing to create directory when the entire path is missing. e.g. "/bad"
throw new Loco_error_WriteException( __('Failed to build directory path','loco-translate') );
}
/**
* Check whether write operations are permitted, or throw
* @throws Loco_error_WriteException
* @return Loco_fs_FileWriter
*/
public function authorize(){
if( $this->disabled() ){
throw new Loco_error_WriteException( __('File modification is disallowed by your WordPress config','loco-translate') );
}
// deny system file changes (fs_protect = 2)
if( 1 < Loco_data_Settings::get()->fs_protect && $this->file->getUpdateType() ){
throw new Loco_error_WriteException( __('Modification of installed files is disallowed by the plugin settings','loco-translate') );
}
return $this;
}
/**
* Check if file system modification is banned at WordPress level
* @return bool
*/
public function disabled(){
// WordPress >= 4.8
if( function_exists('wp_is_file_mod_allowed') ){
$context = apply_filters( 'loco_file_mod_allowed_context', 'download_language_pack', $this->file );
return ! wp_is_file_mod_allowed( $context );
}
// fall back to direct constant check
return (bool) loco_constant('DISALLOW_FILE_MODS');
}
}

View File

@@ -0,0 +1,44 @@
<?php
/**
*
*/
class Loco_fs_Link extends Loco_fs_File {
/**
* @var Loco_fs_File
*/
private $real;
/**
* {@inheritdoc}
*/
public function __construct( $path ){
parent::__construct($path);
$real = realpath( $this->getPath() );
if( is_string($real) ){
if( is_dir($real) ){
$this->real = new Loco_fs_Directory($real);
}
else {
$this->real = new Loco_fs_File($real);
}
}
}
/**
* @return Loco_fs_File|null
*/
public function resolve(){
return $this->real;
}
/**
* {@inheritdoc}
*/
public function isDirectory(){
return $this->real instanceof Loco_fs_Directory;
}
}

View File

@@ -0,0 +1,79 @@
<?php
/**
*
*/
class Loco_fs_LocaleDirectory extends Loco_fs_Directory {
/**
* Get location identifier which signifies the type if translation storage.
*
* - "plugin": bundled inside a plugin (official/author)
* - "theme": bundled inside a theme (official/author)
* - "wplang": under the global languages directory and probably installed by auto-updates
* - "custom": Loco protected directory
* - "other": anywhere else
*
* @return string
*/
public function getTypeId(){
// paths must be compared with trailing slashes so "/foo" doesn't match "/foo-bar"
$path = trailingslashit( $this->normalize() );
// anything under Loco's protected directory is our location for custom overrides
$prefix = trailingslashit( loco_constant('LOCO_LANG_DIR') );
if( substr($path,0,strlen($prefix) ) === $prefix ){
return 'custom';
}
// standard subdirectories of WP_LANG_DIR are under WordPress auto-update control
$prefix = trailingslashit( loco_constant('WP_LANG_DIR') );
if( substr($path,0,strlen($prefix) ) === $prefix ){
if( $path === $prefix || $path === $prefix.'plugins/' || $path === $prefix.'themes/' ){
return 'wplang';
}
}
else {
// anything under a registered theme directory is bundled
$dirs = Loco_fs_Locations::getThemes();
if( $dirs->check($path) ){
return 'theme';
}
// anything under a registered plugin directory is bundled
$dirs = Loco_fs_Locations::getPlugins();
if( $dirs->check($path) ){
return 'plugin';
}
}
// anything else, which includes subdirectories of WP_LANG_DIR etc..
return 'other';
}
/**
* Get translated version of getTypeId
* @param string id
* @return string
*/
public function getTypeLabel( $id ){
switch( $id ){
case 'theme':
case 'plugin':
// Translators: Refers to bundled plugin or theme translation files - i.e. those supplied by the author
return _x('Author','File location','loco-translate');
case 'wplang':
// Translators: Refers to system-installed translation files - i.e. those under WP_LANG_DIR
return _x('System','File location','loco-translate');
case 'custom':
// Translators: Refers to translation files in Loco's custom/protected directory
return _x('Custom','File location','loco-translate');
case 'other':
// Translators: Refers to translation files in an alternative location that isn't Author, System or Custom.
return _x('Other','File location','loco-translate');
}
throw new InvalidArgumentException('Invalid location type: '.$id );
}
}

View File

@@ -0,0 +1,121 @@
<?php
/**
* A file with metadata about the locale it relates to
*/
class Loco_fs_LocaleFile extends Loco_fs_File {
/**
* @var Loco_Locale
*/
private $locale;
/**
* @var string
*/
private $suffix;
/**
* @var string
*/
private $prefix;
/**
* Lazy handling of localized path info
* @return array [ prefix, suffix ]
*/
public function split(){
if( is_null($this->suffix) ){
$parts = explode( '-', $this->filename() );
$this->suffix = array_pop( $parts );
$this->prefix = implode( '-', $parts );
// handle situations where suffixless name is wrongly taken as the prefix
// e.g. "de.po" is valid but "hello.po" is not.
// There are still some ambiguous situations, e.g. "foo-bar.po" is valid, but nonsense
if( ! $this->prefix && ! $this->getLocale()->isValid() ){
$this->prefix = $this->suffix;
$this->suffix = '';
$this->locale = null;
}
}
return array( $this->prefix, $this->suffix );
}
/**
* @return Loco_Locale
*/
public function getLocale(){
if( ! $this->locale ){
if( $tag = $this->getSuffix() ){
$this->locale = Loco_Locale::parse($tag);
}
else {
$this->locale = new Loco_Locale('');
}
}
return $this->locale;
}
/**
* @param Loco_locale
* @return Loco_fs_LocaleFile
*/
public function cloneLocale( Loco_locale $locale ){
$this->split();
$path = (string) $locale;
if( $str = $this->prefix ){
$path = $str.'-'.$path;
}
if( $str = $this->extension() ){
$path .= '.'.$str;
}
if( $dir = $this->getParent() ){
$path = $dir->getPath().'/'.$path;
}
return new Loco_fs_LocaleFile($path);
}
/**
* Get prefix (or stem) from name that comes before locale suffix.
* @return string
*/
public function getPrefix(){
$info = $this->split();
return $info[0];
}
/**
* Get suffix (or locale code) from name that comes after "-" separator
* @return string
*/
public function getSuffix(){
$info = $this->split();
return $info[1];
}
/**
* Test if file is suffix only, e.g. "en_US.po"
* @return bool
*/
public function hasSuffixOnly(){
$info = $this->split();
return $info[1] && ! $info[0];
}
/**
* Test if file is prefix only, e.g. "incorrect.po"
* @return bool
*/
public function hasPrefixOnly(){
$info = $this->split();
return $info[0] && ! $info[1];
}
}

View File

@@ -0,0 +1,75 @@
<?php
/**
* File list indexed by locale codes
*/
class Loco_fs_LocaleFileList extends Loco_fs_FileList {
/**
* Look up locale entries by their tag
* @var array
*/
private $index = array();
/**
* @return Loco_fs_LocaleFileList
*/
public function addLocalized( Loco_fs_LocaleFile $file ){
$i = count($this);
$this->add( $file );
if( count($this) !== $i ){
if( $key = $file->getSuffix() ){
$this->index[$key][] = $i;
}
}
return $this;
}
/**
* Get a new list containing just files for a given locale (exactly)
* @return Loco_fs_LocaleFileList
*/
public function filter( $tag ){
$list = new Loco_fs_LocaleFileList;
if( isset($this->index[$tag]) ){
foreach( $this->index[$tag] as $i ){
$list->addLocalized( $this[$i] );
}
}
return $list;
}
/**
* Get a unique list of valid locales for which there are files
* @return array<Loco_Locale>
*/
public function getLocales(){
$list = array();
foreach( array_keys($this->index) as $tag ){
$locale = Loco_Locale::parse($tag);
if( $locale->isValid() ){
$list[$tag] = $locale;
}
}
return $list;
}
/**
* {@inheritdoc}
* @return Loco_fs_LocaleFileList
*/
public function augment( Loco_fs_FileList $list ){
foreach( $list as $file ){
$this->addLocalized( $file );
}
return $this;
}
}

View File

@@ -0,0 +1,201 @@
<?php
/**
* Handles various file locations
*/
class Loco_fs_Locations extends ArrayObject {
/**
* Singleton of WordPress root directory
* @var Loco_fs_Locations
*/
private static $roots;
/**
* Singleton of wp-content directory
* @var Loco_fs_Locations
*/
private static $conts;
/**
* Singleton of global languages directories
* @var Loco_fs_Locations
*/
private static $langs;
/**
* Singleton of registered theme paths
* @var Loco_fs_Locations
*/
private static $theme;
/**
* Singleton of registered plugin locations
* @var Loco_fs_Locations
*/
private static $plugin;
/**
* Clear static caches
*/
public static function clear(){
self::$roots = null;
self::$conts = null;
self::$langs = null;
self::$theme = null;
self::$plugin = null;
}
/**
* @return Loco_fs_Locations
*/
public static function getRoot(){
if( ! self::$roots ){
self::$roots = new Loco_fs_Locations( array(
loco_constant('ABSPATH'),
) );
}
return self::$roots;
}
/**
* @return Loco_fs_Locations
*/
public static function getContent(){
if( ! self::$conts ){
self::$conts = new Loco_fs_Locations( array(
loco_constant('WP_CONTENT_DIR'), // <- defined WP_CONTENT_DIR
trailingslashit(ABSPATH).'wp-content', // <- default /wp-content
) );
}
return self::$conts;
}
/**
* @return Loco_fs_Locations
*/
public static function getGlobal(){
if( ! self::$langs ){
self::$langs = new Loco_fs_Locations( array(
loco_constant('WP_LANG_DIR'),
) );
}
return self::$langs;
}
/**
* @return Loco_fs_Locations
*/
public static function getThemes(){
if( ! self::$theme ){
$roots = isset($GLOBALS['wp_theme_directories']) ? $GLOBALS['wp_theme_directories'] : array();
if( ! $roots ){
$roots[] = trailingslashit( loco_constant('WP_CONTENT_DIR') ).'themes';
}
self::$theme = new Loco_fs_Locations( $roots );
}
return self::$theme;
}
/**
* @return Loco_fs_Locations
*/
public static function getPlugins(){
if( ! self::$plugin ){
self::$plugin = new Loco_fs_Locations( array(
loco_constant('WP_PLUGIN_DIR'),
) );
}
return self::$plugin;
}
/**
* @param array
*/
public function __construct( array $paths ){
parent::__construct( array() );
foreach( $paths as $path ){
$this->add( $path );
}
}
/**
* @param string normalized absolute path
* @return Loco_fs_Locations
*/
public function add( $path ){
foreach( $this->expand($path) as $path ){
// path must have trailing slash, otherwise "/plugins/foobar" would match "/plugins/foo/"
$this[$path] = strlen($path);
}
return $this;
}
/**
* Check if a given path begins with any of the registered ones
* @param string absolute path
* @return bool whether path matched
*/
public function check( $path ){
foreach( $this->expand($path) as $path ){
foreach( $this as $prefix => $length ){
if( $prefix === $path || substr($path,0,$length) === $prefix ){
return true;
}
}
}
return false;
}
/**
* Match location and return the relative subpath.
* Note that exact match is returned as "." indicating self
* @param string
* @return string | null
*/
public function rel( $path ){
foreach( $this->expand($path) as $path ){
foreach( $this as $prefix => $length ){
if( $prefix === $path ){
return '.';
}
if( substr($path,0,$length) === $prefix ){
return untrailingslashit( substr($path,$length) );
}
}
}
return null;
}
/**
* @param string
* @return string[]
*/
private function expand( $path ){
$path = Loco_fs_File::abs($path);
if( ! $path ){
throw new InvalidArgumentException('Expected absolute path');
}
$paths = array( trailingslashit($path) );
// add real path if differs
$real = realpath($path);
if( $real && $real !== $path ){
$paths[] = trailingslashit($real);
}
return $paths;
}
}

View File

@@ -0,0 +1,227 @@
<?php
/**
* Manages revisions (backups) of a file.
* Revision file names have form "<filename>-backup-<date>.<ext>~"
*/
class Loco_fs_Revisions implements Countable/*, IteratorAggregate*/ {
/**
* @var loco_fs_File
*/
private $master;
/**
* Sortable list of backed up file paths (not including master)
* @var array
*/
private $paths;
/**
* Cached regular expression for matching backup file paths
* @var string
*/
private $regex;
/**
* Cached count of backups + 1
* @var int
*/
private $length;
/**
* Paths to delete when object removed from memory
* @var array
*/
private $trash = array();
/**
* Construct from master file (current version)
* @param Loco_fs_File
*/
public function __construct( Loco_fs_File $file ){
$this->master = $file;
}
/**
* @internal
* Executes deferred deletions with silent errors
*/
public function __destruct(){
if( $trash = $this->trash ){
$writer = clone $this->master->getWriteContext();
foreach( $trash as $file ){
if( $file->exists() ){
try {
$writer->setFile($file);
$writer->delete(false);
}
catch( Loco_error_WriteException $e ){
// avoiding fatal error because pruning is non-critical operation
Loco_error_AdminNotices::debug( $e->getMessage() );
}
}
}
}
}
/**
* Check that file permissions allow a new backup to be created
* @return bool
*/
public function writable(){
return $this->master->exists() && $this->master->getParent()->writable();
}
/**
* Create a new backup of current version
* @return Loco_fs_File
*/
public function create(){
$vers = 0;
$date = date('YmdHis');
$ext = $this->master->extension();
$base = $this->master->dirname().'/'.$this->master->filename();
do {
$path = sprintf( '%s-backup-%s%u.%s~', $base, $date, $vers++, $ext);
}
while (
file_exists($path)
);
$copy = $this->master->copy( $path );
// invalidate cache so next access reads disk
$this->paths = null;
$this->length = null;
return $copy;
}
/**
* Delete oldest backups until we have maximum of $num_backups remaining
* @param int
* @return Loco_fs_Revisions
*/
public function prune( $num_backups ){
$paths = $this->getPaths();
if( isset($paths[$num_backups]) ){
foreach( array_slice( $paths, $num_backups ) as $path ){
$this->unlinkLater($path);
}
$this->paths = array_slice( $paths, 0, $num_backups );
$this->length = null;
}
return $this;
}
/**
* build regex for matching backed up revisions of master
* @return string
*/
private function getRegExp(){
$regex = $this->regex;
if( is_null($regex) ){
$regex = preg_quote( $this->master->filename(), '/' ).'-backup-(\\d{14,})';
if( $ext = $this->master->extension() ){
$regex .= preg_quote('.'.$ext,'/');
}
$regex = '/^'.$regex.'~/';
$this->regex = $regex;
}
return $regex;
}
/**
* @return array
*/
public function getPaths(){
if( is_null($this->paths) ){
$this->paths = array();
$regex = $this->getRegExp();
$finder = new Loco_fs_FileFinder( $this->master->dirname() );
$finder->setRecursive(false);
/* @var $file Loco_fs_File */
foreach( $finder as $file ){
if( preg_match( $regex, $file->basename(), $r ) ){
$this->paths[] = $file->getPath();
}
}
// time sort order descending
rsort( $this->paths );
}
return $this->paths;
}
/**
* Parse a file path into a timestamp
* @param string
* @return int
*/
public function getTimestamp( $path ){
$name = basename($path);
if( preg_match( $this->getRegExp(), $name, $r ) ){
$ymdhis = substr( $r[1], 0, 14 );
return strtotime( $ymdhis );
}
throw new Loco_error_Exception('Invalid revision file: '.$name);
}
/**
* Get number of backups plus master
* @return int
*/
public function count(){
if( ! $this->length ){
$this->length = 1 + count( $this->getPaths() );
}
return $this->length;
}
/**
* Delete file when object removed from memory.
* Previously unlinked on shutdown, but doesn't work with WordPress file system abstraction
* @param string
* @return void
*/
public function unlinkLater($path){
$this->trash[] = new Loco_fs_File($path);
}
/**
* Test whether at least one backup file exists on disk.
* @return bool
*
public function sniff(){
$found = false;
if( $dir = opendir( $this->master->dirname() ) ){
$regex = $this->getRegExp();
while( $f = readdir($dir) ){
if( preg_match($regex,$f) ){
$found = true;
break;
}
}
closedir($dir);
}
return $found;
}*/
}

View File

@@ -0,0 +1,89 @@
<?php
/**
* Manages a POT or PO/MO pair and its on-disk dependants
*/
class Loco_fs_Siblings {
/**
* @var Loco_fs_File
*/
private $po;
/**
* @var Loco_fs_File
*/
private $mo;
public function __construct( Loco_fs_File $file ){
$ext = $file->extension();
if( 'pot' === $ext ){
$this->po = $file;
}
else if( 'po' === $ext ){
$this->po = $file;
$this->mo = $file->cloneExtension('mo');
}
else if( 'mo' === $ext ){
$this->mo = $file;
$this->po = $file->cloneExtension('po');
}
else {
throw new InvalidArgumentException('Unexpected file extension: '.$ext);
}
}
/**
* Get all dependant files (including self) that actually exist on disk
* @return Loco_fs_File[]
*/
public function expand(){
$siblings = array();
// Source and binary pair
foreach( array( $this->po, $this->mo ) as $file ){
if( $file && $file->exists() ){
$siblings[] = $file;
}
}
// Revisions / backup files:
$revs = new Loco_fs_Revisions( $this->po );
foreach( $revs->getPaths() as $path ){
$siblings[] = new Loco_fs_File( $path );
}
// JSON exports, unless in POT mode:
if( 'po' === $this->po->extension() ){
$name = $this->po->filename();
$finder = new Loco_fs_FileFinder( $this->po->dirname() );
$regex = '/^'.preg_quote($name,'/').'-[0-9a-f]{32}$/';
/* @var $file Loco_fs_File */
foreach( $finder->group('json')->exportGroups() as $files ) {
foreach( $files as $file ){
$match = $file->filename();
if( $match === $name || preg_match($regex,$match) ) {
$siblings[] = $file;
}
}
}
}
return $siblings;
}
/**
* @return Loco_fs_File
*/
public function getSource(){
return $this->po;
}
/**
* @return Loco_fs_File
*/
public function getBinary(){
return $this->mo;
}
}

View File

@@ -0,0 +1,365 @@
<?php
loco_require_lib('compiled/gettext.php');
/**
* Wrapper for array forms of parsed PO data
*/
class Loco_gettext_Data extends LocoPoIterator implements JsonSerializable {
/**
* Normalize file extension to internal type
* @param Loco_fs_File
* @return string "po", "pot" or "mo"
* @throws Loco_error_Exception
*/
public static function ext( Loco_fs_File $file ){
$ext = rtrim( strtolower( $file->extension() ), '~' );
if( 'po' === $ext || 'pot' === $ext || 'mo' === $ext ){
return $ext;
}
// translators: Error thrown when attempting to parse a file that is not PO, POT or MO
throw new Loco_error_Exception( sprintf( __('%s is not a Gettext file'), $file->basename() ) );
}
/**
* @param Loco_fs_File
* @return Loco_gettext_Data
*/
public static function load( Loco_fs_File $file ){
if( 'mo' === self::ext($file) ){
return self::fromBinary( $file->getContents() );
}
return self::fromSource( $file->getContents() );
}
/**
* Like load but just pulls header, saving a full parse. PO only
* @param Loco_fs_File
* @return Loco_gettext_Data
* @throws InvalidArgumentException
*/
public static function head( Loco_fs_File $file ){
if( 'mo' === self::ext($file) ){
throw new InvalidArgumentException('PO only');
}
$po = new LocoPoParser( $file->getContents() );
return new Loco_gettext_Data( $po->parse(1) );
}
/**
* @param string assumed PO source
* @return Loco_gettext_Data
*/
public static function fromSource( $src ){
$p = new LocoPoParser($src);
return new Loco_gettext_Data( $p->parse() );
}
/**
* @param string assumed MO bytes
* @return Loco_gettext_Data
*/
public static function fromBinary( $bin ){
$p = new LocoMoParser($bin);
return new Loco_gettext_Data( $p->parse() );
}
/**
* Create a dummy/empty instance with minimum content to be a valid PO file.
* @return Loco_gettext_Data
*/
public static function dummy(){
return new Loco_gettext_Data( array( array('source'=>'','target'=>'Language:') ) );
}
/**
* Ensure PO source is UTF-8.
* Required if we want PO code when we're not parsing it. e.g. source view
* @param string
* @return string
*/
public static function ensureUtf8( $src ){
loco_check_extension('mbstring');
$src = loco_remove_bom($src,$cs);
if( ! $cs ){
// read PO header, requiring partial parse
try {
$cs = LocoPoHeaders::fromSource($src)->getCharset();
}
catch( Loco_error_ParseException $e ){
Loco_error_AdminNotices::debug( $e->getMessage() );
}
// fall back on detection which will only work for latin1
if( ! $cs ){
$cs = mb_detect_encoding($src,array('UTF-8','ISO-8859-1'),true);
}
}
if( $cs && 'UTF-8' !== $cs ){
$src = mb_convert_encoding($src,'UTF-8',array($cs) );
}
return $src;
}
/**
* Compile messages to binary MO format
* @return string MO file source
* @throws Loco_error_Exception
*/
public function msgfmt(){
if( 2 !== strlen("\xC2\xA3") ){
throw new Loco_error_Exception('Refusing to compile MO file. Please disable mbstring.func_overload'); // @codeCoverageIgnore
}
$mo = new LocoMo( $this, $this->getHeaders() );
$opts = Loco_data_Settings::get();
if( $opts->gen_hash ){
$mo->enableHash();
}
if( $opts->use_fuzzy ){
$mo->useFuzzy();
}
return $mo->compile();
}
/**
* Get final UTF-8 string for writing to file
* @param bool whether to sort output, generally only for extracting strings
* @return string
*/
public function msgcat( $sort = false ){
// set maximum line width, zero or >= 15
$this->wrap( Loco_data_Settings::get()->po_width );
// concat with default text sorting if specified
$po = $this->render( $sort ? array( 'LocoPoIterator', 'compare' ) : null );
// Prepend byte order mark only if configured
if( Loco_data_Settings::get()->po_utf8_bom ){
$po = "\xEF\xBB\xBF".$po;
}
return $po;
}
/**
* Split JavaScript messages out of document, based on file reference mapping
* @return array
*/
public function splitJs(){
// TODO take file extension from config
$messages = $this->splitRefs( array('js'=>'js','jsx'=>'js') );
return isset($messages['js']) ? $messages['js'] : array();
}
/**
* Compile JED flavour JSON
* @param string text domain for JED metadata
* @param LocoPoMessage[] pre-compiled messages
* @return string
*/
public function jedize( $domain, array $po ){
$head = $this->getHeaders();
// start locale_data with JED header
$data = array( '' => array (
'domain' => $domain,
'lang' => $head['language'],
'plural-forms' => $head['plural-forms'],
) );
/* @var LocoPoMessage $msg */
foreach( $po as $msg ){
$data[ $msg->getKey() ] = $msg->getMsgstrs();
}
// pretty formatting for debugging
$json_options = 0;
if( Loco_data_Settings::get()->jed_pretty ){
$json_options |= loco_constant('JSON_PRETTY_PRINT') | loco_constant('JSON_UNESCAPED_SLASHES') | loco_constant('JSON_UNESCAPED_UNICODE');
}
return json_encode( array (
'translation-revision-date' => $head['po-revision-date'],
'generator' => $head['x-generator'],
'domain' => $domain,
'locale_data' => array (
$domain => $data,
),
), $json_options );
}
/**
* @return array
*/
public function jsonSerialize(){
$po = $this->getArrayCopy();
// exporting headers non-scalar so js doesn't have to parse them
try {
$headers = $this->getHeaders();
if( count($headers) && '' === $po[0]['source'] ){
$po[0]['target'] = $headers->getArrayCopy();
}
}
// suppress header errors when serializing
// @codeCoverageIgnoreStart
catch( Exception $e ){ }
// @codeCoverageIgnoreEnd
return $po;
}
/**
* Export to JSON for JavaScript editor
* @return string
*/
public function exportJson(){
return json_encode( $this->jsonSerialize() );
}
/**
* Create a signature for use in comparing source strings between documents
* @return string
*/
public function getSourceDigest(){
$data = $this->getHashes();
return md5( implode("\1",$data) );
}
/**
* @param Loco_Locale
* @param array custom headers
* @return Loco_gettext_Data
*/
public function localize( Loco_Locale $locale, array $custom = null ){
$date = gmdate('Y-m-d H:i').'+0000'; // <- forcing UCT
$headers = $this->getHeaders();
// headers that must always be set if absent
$defaults = array (
'Project-Id-Version' => '',
'Report-Msgid-Bugs-To' => '',
'POT-Creation-Date' => $date,
);
// headers that must always override when localizing
$required = array (
'PO-Revision-Date' => $date,
'Last-Translator' => '',
'Language-Team' => $locale->getName(),
'Language' => (string) $locale,
'Plural-Forms' => $locale->getPluralFormsHeader(),
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding' => '8bit',
'X-Generator' => 'Loco https://localise.biz/',
'X-Loco-Version' => sprintf('%s; wp-%s', loco_plugin_version(), $GLOBALS['wp_version'] ),
);
// set actual last translator from WordPress login when possible
if( function_exists('get_current_user_id') && get_current_user_id() ){
$user = wp_get_current_user();
$name = $user->get('display_name') or $name = 'nobody';
$email = $user->get('user_email') or $email = 'nobody@localhost';
// set user's preferred last translator credit if configured
$prefs = Loco_data_Preferences::get();
$credit = $prefs->credit;
if( ! $credit ){
$credit = sprintf('%s <%s>', $name, $email );
}
$required['Last-Translator'] = apply_filters( 'loco_current_translator', $credit, $name, $email );
}
// only set absent or empty headers from default list
foreach( $defaults as $key => $value ){
if( ! $headers[$key] ){
$headers[$key] = $value;
}
}
// add required headers with custom ones overriding
if( is_array($custom) ){
$required = array_merge( $required, $custom );
}
foreach( $required as $key => $value ){
$headers[$key] = $value;
}
// avoid non-empty POT placeholders that won't have been set from $defaults
if( 'PACKAGE VERSION' === $headers['Project-Id-Version'] ){
$headers['Project-Id-Version'] = '';
}
// header message must be un-fuzzied if it was formerly a POT file
return $this->initPo();
}
/**
* @return Loco_gettext_Data
*/
public function templatize(){
$date = gmdate('Y-m-d H:i').'+0000'; // <- forcing UCT
$headers = $this->getHeaders();
$required = array (
'Project-Id-Version' => 'PACKAGE VERSION',
'Report-Msgid-Bugs-To' => '',
'POT-Creation-Date' => $date,
'PO-Revision-Date' => 'YEAR-MO-DA HO:MI+ZONE',
'Last-Translator' => 'FULL NAME <EMAIL@ADDRESS>',
'Language-Team' => '',
'Language' => '',
'Plural-Forms' => 'nplurals=INTEGER; plural=EXPRESSION;',
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding' => '8bit',
'X-Generator' => 'Loco https://localise.biz/',
'X-Loco-Version' => sprintf('%s; wp-%s', loco_plugin_version(), $GLOBALS['wp_version'] ),
);
foreach( $required as $key => $value ){
$headers[$key] = $value;
}
return $this->initPot();
}
/**
* Remap proprietary base path when PO file is moving to another location.
*
* @param Loco_fs_File the file that was originally extracted to (POT)
* @param Loco_fs_File the file that must now target references relative to itself
* @param string vendor name used in header keys
* @return bool whether base header was alterered
*/
public function rebaseHeader( Loco_fs_File $origin, Loco_fs_File $target, $vendor ){
$base = $target->getParent();
$head = $this->getHeaders();
$key = 'X-'.$vendor.'-Basepath';
if( $key = $head->normalize($key) ){
$oldRelBase = $head[$key];
$oldAbsBase = new Loco_fs_Directory($oldRelBase);
$oldAbsBase->normalize( $origin->getParent() );
$newRelBase = $oldAbsBase->getRelativePath($base);
// new base path is relative to $target location
$head[$key] = $newRelBase;
return true;
}
return false;
}
/**
* @param string date format as Gettext states "YEAR-MO-DA HO:MI+ZONE"
* @return int
*/
public static function parseDate( $podate ){
if( method_exists('DateTime', 'createFromFormat') ){
$objdate = DateTime::createFromFormat('Y-m-d H:iO', $podate);
if( $objdate instanceof DateTime ){
return $objdate->getTimestamp();
}
}
return strtotime($podate);
}
}

View File

@@ -0,0 +1,179 @@
<?php
loco_require_lib('compiled/gettext.php');
/**
* String extraction from source code.
*/
class Loco_gettext_Extraction {
/**
* @var Loco_package_Bundle
*/
private $bundle;
/**
* @var LocoExtracted
*/
private $extracted;
/**
* Extra strings to be pushed into domains
* @var array
*/
private $extras;
/**
* List of files skipped due to memory limit
* @var Loco_fs_FileList
*/
private $skipped;
/**
* Size in bytes of largest file encountered
* @var int
*/
private $maxbytes = 0;
/**
* Initialize extractor for a given bundle
* @param Loco_package_Bundle
*/
public function __construct( Loco_package_Bundle $bundle ){
loco_check_extension('ctype');
loco_check_extension('mbstring');
if( ! loco_check_extension('tokenizer') ){
throw new Loco_error_Exception('String extraction not available without required extension');
}
$this->bundle = $bundle;
$this->extracted = new LocoExtracted;
$this->extracted->setDomain('default');
$this->extras = array();
if( $default = $bundle->getDefaultProject() ){
$domain = (string) $default->getDomain();
// wildcard stands in for empty text domain, meaning unspecified or dynamic domains will be included.
// note that strings intended to be in "default" domain must specify explicitly, or be included here too.
if( '*' === $domain ){
$domain = '';
$this->extracted->setDomain('');
}
// pull bundle's default metadata. these are translations that may not be encountered in files
$extras = array();
$header = $bundle->getHeaderInfo();
foreach( $bundle->getMetaTranslatable() as $prop => $notes ){
if( $source = $header->__get($prop) ){
if( is_string($source) ){
$extras[] = array( $source, $notes );
}
}
}
if( $extras ){
$this->extras[$domain] = $extras;
}
}
}
/**
* @param Loco_package_Project
* @return Loco_gettext_Extraction
*/
public function addProject( Loco_package_Project $project ){
$base = $this->bundle->getDirectoryPath();
// skip files larger than configured maximum
$opts = Loco_data_Settings::get();
$max = wp_convert_hr_to_bytes( $opts->max_php_size );
// *attempt* to raise memory limit to WP_MAX_MEMORY_LIMIT
if( function_exists('wp_raise_memory_limit') ){
wp_raise_memory_limit('loco');
}
/* @var $file Loco_fs_File */
foreach( $project->findSourceFiles() as $file ){
$type = $opts->ext2type( $file->extension() );
$extr = loco_wp_extractor($type);
if( 'js' !== $type ) {
// skip large files for PHP, because token_get_all is hungry
$size = $file->size();
$this->maxbytes = max( $this->maxbytes, $size );
if( $size > $max ){
$list = $this->skipped or $list = ( $this->skipped = new Loco_fs_FileList() );
$list->add( $file );
continue;
}
// extract headers from theme PHP files in
if( $project->getBundle()->isTheme() ){
$extr->headerize( array (
'Template Name' => 'Name of the template',
), (string) $project->getDomain() );
}
}
$this->extracted->extractSource( $extr, $file->getContents(), $file->getRelativePath( $base ) );
}
return $this;
}
/**
* Add metadata strings deferred from construction. Note this will alter domain counts
* @return Loco_gettext_Extraction
*/
public function includeMeta(){
foreach( $this->extras as $domain => $extras ){
foreach( $extras as $args ){
$this->extracted->pushMeta( $args[0], $args[1], $domain );
}
}
$this->extras = array();
return $this;
}
/**
* Get number of unique strings across all domains extracted (excluding additional metadata)
* @return array { default: x, myDomain: y }
*/
public function getDomainCounts(){
return $this->extracted->getDomainCounts();
}
/**
* Pull extracted data into POT, filtering out any unwanted domains
* @param string
* @return Loco_gettext_Data
*/
public function getTemplate( $domain ){
$data = new Loco_gettext_Data( $this->extracted->filter($domain) );
return $data->templatize();
}
/**
* Get total number of strings extracted from all domains, excluding additional metadata
* @return int
*/
public function getTotal(){
return $this->extracted->count();
}
/**
* Get list of files skipped, or null if none were skipped
* @return Loco_fs_FileList | null
*/
public function getSkipped(){
return $this->skipped;
}
/**
* Get size in bytes of largest file encountered, even if skipped.
* This is the value required of the max_php_size plugin setting to extract all files
* @return int
*/
public function getMaxPhpSize(){
return $this->maxbytes;
}
}

View File

@@ -0,0 +1,221 @@
<?php
loco_require_lib('compiled/gettext.php');
/**
* Holds metadata about a PO file, cached as Transient
*/
class Loco_gettext_Metadata extends Loco_data_Transient {
/**
* Generate abbreviated stats from parsed array data
* @param array in form returned from parser, including header message
* @return array in form ['t' => total, 'p' => progress, 'f' => fuzzy ];
*/
public static function stats( array $po ){
$t = $p = $f = 0;
/* @var $r array */
foreach( $po as $i => $r ){
// skip header
if( 0 === $i && empty($r['source']) && empty($r['context']) ){
continue;
}
// plural form
// TODO how should plural forms affect stats? should all forms be complete before 100% can be achieved? should offsets add to total??
if( isset($r['parent']) && is_int($r['parent']) ){
continue;
}
// singular form
$t++;
if( '' !== $r['target'] ){
$p++;
if( isset($r['flag']) /*&& LOCO_FLAG_FUZZY === $r['flag']*/ ){
$f++;
}
}
}
return compact('t','p','f');
}
/**
* {@inheritdoc}
*/
public function getKey(){
return 'po_'.md5( $this['rpath'] );
}
/**
* Load metadata from file, using cache if enabled.
* Note that this does not throw exception, check "valid" key
* @param Loco_fs_File
* @param bool
* @return Loco_gettext_Metadata
*/
public static function load( Loco_fs_File $po, $nocache = false ){
$bytes = $po->size();
$mtime = $po->modified();
// quick construct of new meta object. enough to query and validate cache 
$meta = new Loco_gettext_Metadata( array(
'rpath' => $po->getRelativePath( loco_constant('WP_CONTENT_DIR') ),
) );
// pull from cache if exists and has not been modified
if( $nocache || ! $meta->fetch() || $bytes !== $meta['bytes'] || $mtime !== $meta['mtime'] ){
// not available from cache, or cache is invalidated
$meta['bytes'] = $bytes;
$meta['mtime'] = $mtime;
// parse what is hopefully a PO file to get stats
try {
$data = Loco_gettext_Data::load($po)->getArrayCopy();
$meta['valid'] = true;
$meta['stats'] = self::stats( $data );
}
catch( Exception $e ){
$meta['valid'] = false;
}
}
// persist on shutdown with a useful TTL and keepalive
// Maximum lifespan: 10 days. Refreshed if accessed a day after being cached.
$meta->setLifespan(864000)->keepAlive(86400)->persistLazily();
return $meta;
}
/**
* Construct metadata from previously parsed PO data
* @param Loco_fs_File
* @param Loco_gettext_Data
* @return Loco_gettext_Metadata
*/
public static function create( Loco_fs_File $file, Loco_gettext_Data $data ){
return new Loco_gettext_Metadata( array (
'valid' => true,
'bytes' => $file->size(),
'mtime' => $file->modified(),
'stats' => self::stats( $data->getArrayCopy() ),
) );
}
/**
* Get progress stats as simple array with keys, t=total, p=progress, f:flagged.
* Note that untranslated strings are never flagged, hence "f" includes all in "p"
* @return array in form ['t' => total, 'p' => progress, 'f' => fuzzy ];
*/
public function getStats(){
if( isset($this['stats']) ){
return $this['stats'];
}
// fallback to empty stats
return array( 't' => 0, 'p' => 0, 'f' => 0 );
}
/**
* Get total number of messages, not including header and excluding plural forms
* @return int
*/
public function getTotal(){
$stats = $this->getStats();
return $stats['t'];
}
/**
* Get number of fuzzy messages, not including header
* @return int
*/
public function countFuzzy(){
$stats = $this->getStats();
return $stats['f'];
}
/**
* Get progress as a string percentage (minus % symbol)
* @return string
*/
public function getPercent(){
$stats = $this->getStats();
$n = max( 0, $stats['p'] - $stats['f'] );
$t = max( $n, $stats['t'] );
return loco_string_percent( $n, $t );
}
/**
* Get number of strings either untranslated or fuzzy.
* @return int
*/
public function countIncomplete(){
$stats = $this->getStats();
return max( 0, $stats['t'] - ( $stats['p'] - $stats['f'] ) );
}
/**
* Get number of strings completely untranslated (excludes fuzzy).
* @return int
*/
public function countUntranslated(){
$stats = $this->getStats();
return max( 0, $stats['t'] - $stats['p'] );
}
/**
* Echo progress bar using compiled function
* @return void
*/
public function printProgress(){
$stats = $this->getStats();
$flagged = $stats['f'];
$translated = $stats['p'];
$untranslated = $stats['t'] - $translated;
loco_print_progress( $translated, $untranslated, $flagged );
}
/**
* Get wordy summary of total strings
*/
public function getTotalSummary(){
$total = $this->getTotal();
return sprintf( _n('1 string','%s strings',$total,'loco-translate'), number_format($total) );
}
/**
* Get wordy summary including translation stats
*/
public function getProgressSummary(){
$extra = array();
$stext = sprintf( __('%s%% translated','loco-translate'), $this->getPercent() ).', '.$this->getTotalSummary();
if( $num = $this->countFuzzy() ){
$extra[] = sprintf( __('%s fuzzy','loco-translate'), number_format($num) );
}
if( $num = $this->countUntranslated() ){
$extra[] = sprintf( __('%s untranslated','loco-translate'), number_format($num) );
}
if( $extra ){
$stext .= ' ('.implode(', ', $extra).')';
}
return $stext;
}
public function getPath( $absolute ){
$path = $this['rpath'];
if( $absolute && ! Loco_fs_File::abs($path) ){
$path = trailingslashit( loco_constant('WP_CONTENT_DIR') ).$path;
}
return $path;
}
}

View File

@@ -0,0 +1,90 @@
<?php
/**
* A file finder built from search path references in a PO/POT file
*/
class Loco_gettext_SearchPaths extends Loco_fs_FileFinder {
/**
* Look up a relative file reference against search paths
* @param string relative file path reference
* @return Loco_fs_File
*/
public function match( $ref ){
$excluded = new Loco_fs_Locations( $this->getExcluded() );
/* @var Loco_fs_Directory */
foreach( $this->getRootDirectories() as $base ){
$file = new Loco_fs_File($ref);
$path = $file->normalize( (string) $base );
if( $file->exists() && ! $excluded->check($path) ){
return $file;
}
}
}
/**
* Build search paths from a given PO/POT file that references other files
* @return Loco_gettext_SearchPaths
*/
public function init( Loco_fs_File $pofile, LocoHeaders $head = null ){
if( is_null($head) ){
loco_require_lib('compiled/gettext.php');
$head = LocoPoHeaders::fromSource( $pofile->getContents() );
}
$ninc = 0;
foreach( array('Poedit') as $vendor ){
$key = 'X-'.$vendor.'-Basepath';
if( ! $head->has($key) ){
continue;
}
$dir = new Loco_fs_Directory( $head[$key] );
$base = $dir->normalize( $pofile->dirname() );
// base should be absolute, with the following search paths relative to it
$i = 0;
while( true ){
$key = sprintf('X-%s-SearchPath-%u', $vendor, $i++);
if( ! $head->has($key) ){
break;
}
// map search path to given base
$include = new Loco_fs_File( $head[$key] );
$include->normalize( $base );
if( $include->exists() ){
if( $include->isDirectory() ){
$this->addRoot( (string) $include );
$ninc++;
}
/*else {
TODO force specific file in Loco_fs_FileFinder
}*/
}
}
// exclude from search paths
$i = 0;
while( true ){
$key = sprintf('X-%s-SearchPathExcluded-%u', $vendor, $i++);
if( ! $head->has($key) ){
break;
}
// map excluded path to given base
$exclude = new Loco_fs_File( $head[$key] );
$exclude->normalize($base);
if( $exclude->exists() ){
$this->exclude( (string) $exclude );
}
// TODO implement wildcard exclusion
}
}
// Add po file location if no proprietary headers used
if( ! $ninc ){
$this->addRoot( $pofile->dirname() );
}
return $this;
}
}

Some files were not shown because too many files have changed in this diff Show More