first commit

This commit is contained in:
2023-09-12 21:41:04 +02:00
commit 3361a7f053
13284 changed files with 2116755 additions and 0 deletions

View File

@@ -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;
}
}

View File

@@ -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 );
}
}

View File

@@ -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 );
}
}