| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\Utils; |
| 4 |
|
| 5 |
use Elementor\Core\Base\Document; |
| 6 |
use Elementor\Modules\AtomicWidgets\Elements\Base\Atomic_Element_Base; |
| 7 |
use Elementor\Modules\AtomicWidgets\Elements\Base\Atomic_Widget_Base; |
| 8 |
use Elementor\Plugin; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; // Exit if accessed directly. |
| 12 |
} |
| 13 |
|
| 14 |
class Utils { |
| 15 |
public static function is_atomic( $element_instance ): bool { |
| 16 |
return $element_instance instanceof Atomic_Element_Base || |
| 17 |
$element_instance instanceof Atomic_Widget_Base; |
| 18 |
} |
| 19 |
|
| 20 |
public static function generate_id( string $prefix = '', $existing_ids = [] ): string { |
| 21 |
do { |
| 22 |
$generated = substr( |
| 23 |
bin2hex( random_bytes( 4 ) ), |
| 24 |
0, |
| 25 |
7 |
| 26 |
); |
| 27 |
|
| 28 |
$id = "$prefix{$generated}"; |
| 29 |
} while ( in_array( $id, $existing_ids, true ) ); |
| 30 |
|
| 31 |
return $id; |
| 32 |
} |
| 33 |
|
| 34 |
public static function is_post_published( Document $document ): bool { |
| 35 |
return $document->get_post()->post_status === Document::STATUS_PUBLISH; |
| 36 |
} |
| 37 |
|
| 38 |
public static function traverse_post_elements( string $post_id, callable $callback ): void { |
| 39 |
$documents = Plugin::$instance->documents; |
| 40 |
$document = is_preview() ? $documents->get_doc_or_auto_save( $post_id, get_current_user_id() ) : $documents->get( $post_id ); |
| 41 |
|
| 42 |
if ( ! $document ) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
$elements_data = $document->get_elements_data(); |
| 47 |
|
| 48 |
if ( empty( $elements_data ) ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
Plugin::$instance->db->iterate_data( $elements_data, function( $element_data ) use ( $callback ) { |
| 53 |
call_user_func( $callback, $element_data ); |
| 54 |
} ); |
| 55 |
} |
| 56 |
} |
| 57 |
|