first commit
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
namespace Elementor\Modules\Components\Utils;
|
||||
|
||||
use Elementor\Plugin;
|
||||
use Elementor\Modules\AtomicWidgets\Elements\Base\Atomic_Element_Base;
|
||||
use Elementor\Modules\AtomicWidgets\Elements\Base\Atomic_Widget_Base;
|
||||
use Elementor\Modules\AtomicWidgets\Utils\Utils;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Format_Component_Elements_Id {
|
||||
public static function format( array $elements, array $path ) {
|
||||
return array_map( function( $element ) use ( $path ) {
|
||||
$origin_id = $element['id'];
|
||||
$nesting_path = [ ...$path, $origin_id ];
|
||||
|
||||
$element['id'] = self::hash_string( implode( '_', $nesting_path ), 7 );
|
||||
$element['origin_id'] = $origin_id;
|
||||
$element['elements'] = self::format( $element['elements'], $nesting_path );
|
||||
|
||||
return $element;
|
||||
}, $elements );
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a copy of the hashString function in ts utils package.
|
||||
* It's important to keep it in synced with the ts implementation
|
||||
* to make component inner elements ids consistent between the editor and the frontend.
|
||||
*
|
||||
* @param string $str - The string to hash.
|
||||
* @param $length - The length of the hash to return, optional.
|
||||
* @return string - The hashed string.
|
||||
*/
|
||||
public static function hash_string( string $str, ?int $length ): string {
|
||||
$hash_basis = 5381;
|
||||
|
||||
$i = strlen( $str );
|
||||
while ( $i > 0 ) {
|
||||
--$i;
|
||||
$hash_basis = ( $hash_basis * 33 ) ^ ord( $str[ $i ] );
|
||||
// Keep hash within 32-bit range to match JavaScript bitwise operations.
|
||||
$hash_basis = $hash_basis & 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
$result = base_convert( (string) $hash_basis, 10, 36 );
|
||||
|
||||
if ( ! isset( $length ) ) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$sliced = substr( $result, -$length );
|
||||
return str_pad( $sliced, $length, '0', STR_PAD_LEFT );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
namespace Elementor\Modules\Components\Utils;
|
||||
|
||||
use Elementor\Plugin;
|
||||
use Elementor\Modules\AtomicWidgets\Elements\Base\Atomic_Element_Base;
|
||||
use Elementor\Modules\AtomicWidgets\Elements\Base\Atomic_Widget_Base;
|
||||
use Elementor\Modules\AtomicWidgets\Utils\Utils;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Parsing_Utils {
|
||||
public static function get_prop_type( string $el_type, string $widget_type, string $prop_key ): Prop_Type {
|
||||
$element = Plugin::$instance->elements_manager->get_element( $el_type, $widget_type );
|
||||
|
||||
if ( ! $element ) {
|
||||
throw new \Exception( esc_html( "Invalid element: Element type $el_type with widget type $widget_type is not registered." ) );
|
||||
}
|
||||
|
||||
$element_instance = new $element();
|
||||
|
||||
/** @var Atomic_Element_Base | Atomic_Widget_Base $element_instance */
|
||||
if ( ! Utils::is_atomic( $element_instance ) ) {
|
||||
throw new \Exception( esc_html( "Invalid element: Element type $el_type with widget type $widget_type is not an atomic element/widget." ) );
|
||||
}
|
||||
|
||||
$props_schema = $element_instance->get_props_schema();
|
||||
|
||||
if ( ! isset( $props_schema[ $prop_key ] ) ) {
|
||||
throw new \Exception( esc_html( "Prop key '$prop_key' does not exist in the schema of element '{$element_instance->get_element_type()}'." ) );
|
||||
}
|
||||
|
||||
return $props_schema[ $prop_key ];
|
||||
}
|
||||
|
||||
public static function get_duplicates( array $array ): array {
|
||||
$duplicates = [];
|
||||
$seen = [];
|
||||
|
||||
foreach ( $array as $item ) {
|
||||
if ( in_array( $item, $seen, true ) ) {
|
||||
if ( ! in_array( $item, $duplicates, true ) ) {
|
||||
$duplicates[] = $item;
|
||||
}
|
||||
} else {
|
||||
$seen[] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
return $duplicates;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user