mirror of
https://github.com/proelements/proelements.git
synced 2026-04-05 20:13:47 +00:00
v3.33.1
This commit is contained in:
40
core/utils/abtest.php
Normal file
40
core/utils/abtest.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
namespace ElementorPro\Core\Utils;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
class Abtest {
|
||||
|
||||
const PREFIX_CACHE_KEY = '_elementor_ab_test_';
|
||||
|
||||
const CACHE_TTL = 90 * DAY_IN_SECONDS;
|
||||
|
||||
public static function get_variation( $test_name ): int {
|
||||
$variation_id = self::get_variation_id_from_cache( $test_name );
|
||||
|
||||
if ( false === $variation_id ) {
|
||||
$variation_id = self::get_random_variation();
|
||||
self::set_variation_id_from_cache( $test_name, $variation_id );
|
||||
}
|
||||
|
||||
return absint( $variation_id );
|
||||
}
|
||||
|
||||
private static function get_variation_id_from_cache( $test_name ) {
|
||||
$cache_key = self::PREFIX_CACHE_KEY . $test_name;
|
||||
|
||||
return get_transient( $cache_key );
|
||||
}
|
||||
|
||||
private static function set_variation_id_from_cache( $test_name, $variation_id ): void {
|
||||
$cache_key = self::PREFIX_CACHE_KEY . $test_name;
|
||||
|
||||
set_transient( $cache_key, $variation_id, self::CACHE_TTL );
|
||||
}
|
||||
|
||||
private static function get_random_variation(): int {
|
||||
return mt_rand( 1, 2 );
|
||||
}
|
||||
}
|
||||
79
core/utils/collection.php
Normal file
79
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();
|
||||
}
|
||||
}
|
||||
100
core/utils/hints.php
Normal file
100
core/utils/hints.php
Normal file
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace ElementorPro\core\utils;
|
||||
|
||||
use Elementor\Core\Utils\Hints as Core_Hints;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
/**
|
||||
* Class Hints
|
||||
*/
|
||||
class Hints extends Core_Hints {
|
||||
public static function should_show_hint( $hint_id ): bool {
|
||||
// Check if needed functions exists - if not, require them
|
||||
if ( ! function_exists( 'get_plugins' ) || ! function_exists( 'is_plugin_active' ) ) {
|
||||
require_once ABSPATH . 'wp-admin/includes/plugin.php';
|
||||
}
|
||||
|
||||
if ( is_array( $hint_id ) ) {
|
||||
$hint = $hint_id;
|
||||
} else {
|
||||
$hint = self::get_hints( $hint_id );
|
||||
}
|
||||
foreach ( $hint as $key => $value ) {
|
||||
switch ( $key ) {
|
||||
case self::DISMISSED:
|
||||
// support multiple dismissed hints
|
||||
foreach ( (array) $value as $dismissed_hint ) {
|
||||
if ( self::is_dismissed( $dismissed_hint ) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case self::CAPABILITY:
|
||||
if ( ! current_user_can( $value ) ) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case self::DEFINED:
|
||||
if ( defined( $value ) ) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case self::PLUGIN_INSTALLED:
|
||||
if ( ! self::is_plugin_installed( $value ) ) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case self::PLUGIN_ACTIVE:
|
||||
if ( ! self::is_plugin_active( $value ) ) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function get_hints( $hint_key = null ): array {
|
||||
$hints = [
|
||||
'site_mailer_forms_email_notice' => [
|
||||
self::DISMISSED => 'site_mailer_forms_email_notice',
|
||||
self::CAPABILITY => 'install_plugins',
|
||||
self::DEFINED => 'SITE_MAILER_VERSION',
|
||||
],
|
||||
'site_mailer_forms_submissions_notice' => [
|
||||
self::DISMISSED => [ 'site_mailer_forms_submissions_notice', 'site_mailer_forms_email_notice' ],
|
||||
self::CAPABILITY => 'install_plugins',
|
||||
self::DEFINED => 'SITE_MAILER_VERSION',
|
||||
],
|
||||
'send_app_wc_widgets_notice' => [
|
||||
self::DISMISSED => [ 'send_app_wc_widgets_notice' ],
|
||||
self::CAPABILITY => 'install_plugins',
|
||||
self::DEFINED => 'SEND_VERSION',
|
||||
],
|
||||
'send_app_forms_submissions_notice' => [
|
||||
self::DISMISSED => [ 'send_app_forms_submissions_notice', 'send_app_forms_actions_notice' ],
|
||||
self::CAPABILITY => 'install_plugins',
|
||||
self::DEFINED => 'SEND_VERSION',
|
||||
],
|
||||
'send_app_forms_actions_notice' => [
|
||||
self::DISMISSED => [ 'send_app_forms_actions_notice' ],
|
||||
self::CAPABILITY => 'install_plugins',
|
||||
self::DEFINED => 'SEND_VERSION',
|
||||
],
|
||||
'send_app_forms_triggers_notice' => [
|
||||
self::DISMISSED => [ 'send_app_forms_triggers_notice' ],
|
||||
self::CAPABILITY => 'install_plugins',
|
||||
self::DEFINED => 'SEND_VERSION',
|
||||
],
|
||||
];
|
||||
if ( ! $hint_key ) {
|
||||
return $hints;
|
||||
}
|
||||
|
||||
return $hints[ $hint_key ] ?? [];
|
||||
}
|
||||
}
|
||||
68
core/utils/registrar.php
Normal file
68
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