first commit
This commit is contained in:
79
wp-content/plugins/elementor-pro/core/utils/collection.php
Normal file
79
wp-content/plugins/elementor-pro/core/utils/collection.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
namespace ElementorPro\Core\Utils;
|
||||
|
||||
use \Elementor\Core\Utils\Collection as Collection_Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
// TODO: Move to Core.
|
||||
class Collection extends Collection_Base implements \JsonSerializable {
|
||||
|
||||
/**
|
||||
* Change the items key by an item field.
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function key_by( $key ) {
|
||||
return $this->map_with_keys( function ( $item ) use ( $key ) {
|
||||
return [ $item->{$key} => $item ];
|
||||
} );
|
||||
}
|
||||
|
||||
/**
|
||||
* Flatten the items recursively.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function flatten_recursive() {
|
||||
$output = [];
|
||||
$items = $this->all();
|
||||
|
||||
array_walk_recursive($items, function( $item ) use ( &$output ) {
|
||||
$output[] = $item;
|
||||
} );
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run array_diff between the collection and other array or collection.
|
||||
*
|
||||
* @param $filter
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function diff( $filter ) {
|
||||
if ( $filter instanceof Collection_Base ) {
|
||||
$filter = $filter->all();
|
||||
}
|
||||
|
||||
return new static( array_diff( $this->all(), $filter ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the array
|
||||
*
|
||||
* @param false $preserve_keys
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function reverse( $preserve_keys = false ) {
|
||||
return new static(
|
||||
array_reverse( $this->all(), $preserve_keys )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a JSON serialized representation of the Collection.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function jsonSerialize() {
|
||||
return $this->all();
|
||||
}
|
||||
}
|
||||
68
wp-content/plugins/elementor-pro/core/utils/registrar.php
Normal file
68
wp-content/plugins/elementor-pro/core/utils/registrar.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace ElementorPro\Core\Utils;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic items registrar.
|
||||
*
|
||||
* TODO: Move to Core.
|
||||
*/
|
||||
class Registrar {
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $items;
|
||||
|
||||
/**
|
||||
* Registrar constructor.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->items = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new item.
|
||||
*
|
||||
* @param $instance - Item instance.
|
||||
* @param string $id - Optional - For BC - Deprecated.
|
||||
*
|
||||
* @return boolean - Whether the item was registered.
|
||||
*/
|
||||
public function register( $instance, $id = null ) {
|
||||
// TODO: For BC. Remove in the future.
|
||||
if ( ! $id ) {
|
||||
// Get the ID or default to the class name.
|
||||
$id = ( method_exists( $instance, 'get_id' ) ) ? $instance->get_id() : get_class( $instance );
|
||||
}
|
||||
|
||||
if ( $this->get( $id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->items[ $id ] = $instance;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an item by ID.
|
||||
*
|
||||
* @param string $id
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public function get( $id = null ) {
|
||||
if ( ! $id ) {
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
return isset( $this->items[ $id ] ) ? $this->items[ $id ] : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user