first commit
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\ST\Utils;
|
||||
|
||||
use SitePress;
|
||||
use WPML_String_Translation;
|
||||
|
||||
class LanguageResolution {
|
||||
|
||||
/** @var SitePress $sitepress */
|
||||
private $sitepress;
|
||||
|
||||
/** @var WPML_String_Translation $string_translation */
|
||||
private $string_translation;
|
||||
|
||||
/** @var null|string $admin_language */
|
||||
private $admin_language;
|
||||
|
||||
public function __construct( SitePress $sitepress, WPML_String_Translation $string_translation ) {
|
||||
$this->sitepress = $sitepress;
|
||||
$this->string_translation = $string_translation;
|
||||
}
|
||||
|
||||
/** @return bool|mixed|string|null */
|
||||
public function getCurrentLanguage() {
|
||||
if ( $this->string_translation->should_use_admin_language() ) {
|
||||
$current_lang = $this->getAdminLanguage();
|
||||
} else {
|
||||
$current_lang = $this->sitepress->get_current_language();
|
||||
}
|
||||
|
||||
if ( ! $current_lang ) {
|
||||
$current_lang = $this->sitepress->get_default_language();
|
||||
if ( ! $current_lang ) {
|
||||
$current_lang = 'en';
|
||||
}
|
||||
}
|
||||
|
||||
return $current_lang;
|
||||
}
|
||||
|
||||
/** */
|
||||
public function getCurrentLocale() {
|
||||
return $this->sitepress->get_locale( $this->getCurrentLanguage() );
|
||||
}
|
||||
|
||||
/** @return string */
|
||||
private function getAdminLanguage() {
|
||||
if ( $this->sitepress->is_wpml_switch_language_triggered() ) {
|
||||
return $this->sitepress->get_admin_language();
|
||||
}
|
||||
|
||||
if ( ! $this->admin_language ) {
|
||||
$this->admin_language = $this->sitepress->get_admin_language();
|
||||
}
|
||||
|
||||
return $this->admin_language;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\ST\WP\App;
|
||||
|
||||
use function WPML\FP\partial;
|
||||
|
||||
class Resources {
|
||||
// enqueueApp :: string $app -> ( string $localizeData )
|
||||
public static function enqueueApp( $app ) {
|
||||
return partial(
|
||||
[ '\WPML\LIB\WP\App\Resources', 'enqueue' ],
|
||||
$app,
|
||||
WPML_ST_URL,
|
||||
WPML_ST_PATH,
|
||||
WPML_ST_VERSION,
|
||||
'wpml-string-translation'
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
class WPML_ST_String_Dependencies_Builder {
|
||||
|
||||
/** @var WPML_ST_String_Dependencies_Records $records */
|
||||
private $records;
|
||||
|
||||
private $types_map = array(
|
||||
'post' => 'package',
|
||||
'package' => 'string',
|
||||
);
|
||||
|
||||
public function __construct( WPML_ST_String_Dependencies_Records $records ) {
|
||||
$this->records = $records;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param int $id
|
||||
*
|
||||
* @return WPML_ST_String_Dependencies_Node
|
||||
*/
|
||||
public function from( $type, $id ) {
|
||||
$parent_id = $this->records->get_parent_id_from( $type, $id );
|
||||
|
||||
if ( $parent_id ) {
|
||||
$parent = $this->from( $this->get_parent_type( $type ), $parent_id );
|
||||
$initial_node = $parent->search( $id, $type );
|
||||
|
||||
if ( $initial_node ) {
|
||||
$initial_node->set_needs_refresh( true );
|
||||
}
|
||||
|
||||
return $parent;
|
||||
}
|
||||
|
||||
$node = new WPML_ST_String_Dependencies_Node( $id, $type );
|
||||
$node->set_needs_refresh( true );
|
||||
return $this->populate_node( $node );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WPML_ST_String_Dependencies_Node $node
|
||||
*
|
||||
* @return WPML_ST_String_Dependencies_Node
|
||||
*/
|
||||
private function populate_node( WPML_ST_String_Dependencies_Node $node ) {
|
||||
$child_ids = $this->records->get_child_ids_from( $node->get_type(), $node->get_id() );
|
||||
|
||||
if ( $child_ids ) {
|
||||
$child_type = $this->get_child_type( $node->get_type() );
|
||||
|
||||
foreach ( $child_ids as $id ) {
|
||||
$child_node = new WPML_ST_String_Dependencies_Node( $id, $child_type );
|
||||
$node->add_child( $child_node );
|
||||
$this->populate_node( $child_node );
|
||||
}
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
*
|
||||
* @return false|string
|
||||
*/
|
||||
private function get_parent_type( $type ) {
|
||||
return array_search( $type, $this->types_map, true );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
*
|
||||
* @return null|string
|
||||
*/
|
||||
private function get_child_type( $type ) {
|
||||
return isset( $this->types_map[ $type ] ) ? $this->types_map[ $type ] : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
class WPML_ST_String_Dependencies_Node {
|
||||
|
||||
/** @var WPML_ST_String_Dependencies_Node|null $parent */
|
||||
private $parent;
|
||||
|
||||
/** @var WPML_ST_String_Dependencies_Node[] $children */
|
||||
private $children = array();
|
||||
|
||||
/** @var bool $iteration_completed */
|
||||
private $iteration_completed = false;
|
||||
|
||||
/** @var int|null $id */
|
||||
private $id;
|
||||
|
||||
/** @var string|null $type */
|
||||
private $type;
|
||||
|
||||
/** @var bool|null $needs_refresh */
|
||||
private $needs_refresh;
|
||||
|
||||
public function __construct( $id = null, $type = null ) {
|
||||
$this->id = $id;
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
public function get_id() {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function get_type() {
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
public function set_needs_refresh( $needs_refresh ) {
|
||||
$this->needs_refresh = $needs_refresh;
|
||||
}
|
||||
|
||||
public function get_needs_refresh() {
|
||||
return $this->needs_refresh;
|
||||
}
|
||||
|
||||
public function set_parent( WPML_ST_String_Dependencies_Node $node ) {
|
||||
$node->add_child( $this );
|
||||
$this->parent = $node;
|
||||
}
|
||||
|
||||
public function get_parent() {
|
||||
return $this->parent;
|
||||
}
|
||||
|
||||
public function add_child( WPML_ST_String_Dependencies_Node $node ) {
|
||||
if ( ! isset( $this->children[ $node->get_hash() ] ) ) {
|
||||
$this->children[ $node->get_hash() ] = $node;
|
||||
$node->set_parent( $this );
|
||||
}
|
||||
}
|
||||
|
||||
public function remove_child( WPML_ST_String_Dependencies_Node $node ) {
|
||||
unset( $this->children[ $node->get_hash() ] );
|
||||
unset( $node );
|
||||
}
|
||||
|
||||
public function detach() {
|
||||
if ( $this->parent ) {
|
||||
$this->parent->remove_child( $this );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Iteration DFS in post-order
|
||||
*
|
||||
* @return WPML_ST_String_Dependencies_Node
|
||||
*/
|
||||
public function get_next() {
|
||||
if ( $this->children ) {
|
||||
$first_child = reset( $this->children );
|
||||
|
||||
if ( $first_child ) {
|
||||
/** @var WPML_ST_String_Dependencies_Node $first_child */
|
||||
return $first_child->get_next();
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $this->parent && ! $this->iteration_completed ) {
|
||||
$this->iteration_completed = true;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search DFS in pre-order
|
||||
*
|
||||
* @param int $id
|
||||
* @param string $type
|
||||
*
|
||||
* @return bool|WPML_ST_String_Dependencies_Node
|
||||
*/
|
||||
public function search( $id, $type ) {
|
||||
if ( $this->id === $id && $this->type === $type ) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
if ( $this->children ) {
|
||||
|
||||
foreach ( $this->children as $child ) {
|
||||
$node = $child->search( $id, $type );
|
||||
|
||||
if ( $node ) {
|
||||
return $node;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function iteration_completed() {
|
||||
return $this->iteration_completed;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|stdClass
|
||||
*/
|
||||
public function to_json() {
|
||||
$object = new stdClass();
|
||||
|
||||
foreach ( $this->get_item_properties() as $property ) {
|
||||
if ( null !== $this->{$property} ) {
|
||||
$object->{$property} = $this->{$property};
|
||||
}
|
||||
}
|
||||
|
||||
if ( $this->children ) {
|
||||
foreach ( $this->children as $child ) {
|
||||
$object->children[] = $child->to_json();
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $this->parent ) {
|
||||
return json_encode( $object );
|
||||
}
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|self $object
|
||||
*/
|
||||
public function from_json( $object ) {
|
||||
if ( is_string( $object ) ) {
|
||||
$object = json_decode( $object );
|
||||
}
|
||||
|
||||
foreach ( $this->get_item_properties() as $property ) {
|
||||
if ( isset( $object->{$property} ) ) {
|
||||
$this->{$property} = $object->{$property};
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $object->children ) ) {
|
||||
foreach ( $object->children as $child ) {
|
||||
$child_node = new self( $child->id, $child->type );
|
||||
$child_node->from_json( $child );
|
||||
$child_node->set_parent( $this );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function get_item_properties() {
|
||||
return array( 'id', 'type', 'needs_refresh' );
|
||||
}
|
||||
|
||||
private function get_hash() {
|
||||
return spl_object_hash( $this );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
class WPML_ST_String_Dependencies_Records {
|
||||
|
||||
/** @var wpdb $wpdb */
|
||||
private $wpdb;
|
||||
|
||||
public function __construct( wpdb $wpdb ) {
|
||||
$this->wpdb = $wpdb;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param int $id
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function get_parent_id_from( $type, $id ) {
|
||||
switch ( $type ) {
|
||||
case 'package':
|
||||
$query = "SELECT post_id FROM {$this->wpdb->prefix}icl_string_packages WHERE ID = %d";
|
||||
break;
|
||||
|
||||
case 'string':
|
||||
$query = "SELECT string_package_id FROM {$this->wpdb->prefix}icl_strings WHERE id = %d";
|
||||
break;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (int) $this->wpdb->get_var( $this->wpdb->prepare( $query, $id ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param int $id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_child_ids_from( $type, $id ) {
|
||||
switch ( $type ) {
|
||||
case 'post':
|
||||
$query = "SELECT id FROM {$this->wpdb->prefix}icl_string_packages WHERE post_id = %d";
|
||||
break;
|
||||
|
||||
case 'package':
|
||||
$query = "SELECT ID FROM {$this->wpdb->prefix}icl_strings WHERE string_package_id = %d";
|
||||
break;
|
||||
|
||||
default:
|
||||
return array();
|
||||
}
|
||||
|
||||
$ids = $this->wpdb->get_col( $this->wpdb->prepare( $query, $id ) );
|
||||
|
||||
return array_map( 'intval', $ids );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
class WPML_ST_Scan_Dir {
|
||||
const PLACEHOLDERS_ROOT = '<root>';
|
||||
|
||||
/**
|
||||
* @param string $folder
|
||||
* @param array $extensions
|
||||
* @param bool $single_file
|
||||
* @param array $ignore_folders
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function scan( $folder, array $extensions = array(), $single_file = false, $ignore_folders = array() ) {
|
||||
|
||||
$files = array();
|
||||
$scanned_files = array();
|
||||
|
||||
if ( is_dir( $folder ) ) {
|
||||
$scanned_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $folder ) );
|
||||
} elseif ( $single_file ) {
|
||||
$scanned_files = array( new SplFileInfo( $folder ) );
|
||||
}
|
||||
|
||||
foreach ( $scanned_files as $file ) {
|
||||
$ignore_file = false;
|
||||
|
||||
if ( in_array( $file->getExtension(), $extensions, true ) ) {
|
||||
|
||||
foreach ( $ignore_folders as $ignore_folder ) {
|
||||
if ( false !== strpos( $file->getPathname(), str_replace( self::PLACEHOLDERS_ROOT, $folder, $ignore_folder ) ) ) {
|
||||
$ignore_file = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $ignore_file ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$files[] = $file->getPathname();
|
||||
}
|
||||
}
|
||||
|
||||
return $files;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user