| 1 |
<?php |
| 2 |
/** |
| 3 |
* Elementor Utils class |
| 4 |
* |
| 5 |
* @since 5.3.0 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly. |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Elementor Utils class |
| 17 |
*/ |
| 18 |
class ElementorUtils { |
| 19 |
const CACHE_KEY = 'ep_elementor_widgets'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Setup. |
| 23 |
*/ |
| 24 |
public function setup() { |
| 25 |
// Bail if Elementor is not installed. |
| 26 |
if ( ! class_exists( '\Elementor\Plugin' ) ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
add_action( 'save_post_elementor_library', [ $this, 'regenerate_cache' ] ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Delete and regenerate the cache |
| 35 |
*/ |
| 36 |
public function regenerate_cache() { |
| 37 |
delete_transient( self::CACHE_KEY ); |
| 38 |
|
| 39 |
// Simply calling it will reset the transient (if the `ep_elementor_widgets_pre_all_widgets` filter isn't in use.) |
| 40 |
$this->get_all_widgets_in_all_templates(); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Given a widget name, return all its instances across all elementor templates. |
| 45 |
* |
| 46 |
* @param string $widget_name The widget name, e.g., `wp-widget-ep-facet-meta` |
| 47 |
* @return array |
| 48 |
*/ |
| 49 |
public function get_specific_widget_in_all_templates( string $widget_name ): array { |
| 50 |
$widgets = array_values( |
| 51 |
array_filter( |
| 52 |
$this->get_all_widgets_in_all_templates(), |
| 53 |
fn ( $widget ) => $widget['widgetType'] === $widget_name |
| 54 |
) |
| 55 |
); |
| 56 |
|
| 57 |
return $widgets; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get all widgets in all elementor templates. |
| 62 |
* |
| 63 |
* @return array |
| 64 |
*/ |
| 65 |
public function get_all_widgets_in_all_templates(): array { |
| 66 |
/** |
| 67 |
* Short-circuits the process of getting all widgets of a template. |
| 68 |
* |
| 69 |
* Returning a non-null value will effectively short-circuit the function. |
| 70 |
* |
| 71 |
* @since 5.3.0 |
| 72 |
* @hook ep_elementor_widgets_pre_all_widgets |
| 73 |
* @param {null} $widgets Widgets array |
| 74 |
* @return {null|array} Widgets array or `null` to keep default behavior |
| 75 |
*/ |
| 76 |
$pre_all_widgets = apply_filters( 'ep_elementor_widgets_pre_all_widgets', null ); |
| 77 |
if ( null !== $pre_all_widgets ) { |
| 78 |
return (array) $pre_all_widgets; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Filter the query arguments used to get Elementor templates. |
| 83 |
* |
| 84 |
* @since 5.3.0 |
| 85 |
* @hook ep_elementor_templates_query_args |
| 86 |
* @param {array} $query_args Query arguments |
| 87 |
* @return {array} Modified query arguments |
| 88 |
*/ |
| 89 |
$query_args = apply_filters( |
| 90 |
'ep_elementor_templates_query_args', |
| 91 |
[ |
| 92 |
'post_type' => [ 'elementor_library' ], |
| 93 |
'posts_per_page' => 999, // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page |
| 94 |
] |
| 95 |
); |
| 96 |
|
| 97 |
$elementor_templates = get_posts( $query_args ); |
| 98 |
$all_widgets = []; |
| 99 |
|
| 100 |
foreach ( $elementor_templates as $elementor_template ) { |
| 101 |
$template_content = get_post_meta( $elementor_template->ID, '_elementor_data', true ); |
| 102 |
if ( ! $template_content ) { |
| 103 |
continue; |
| 104 |
} |
| 105 |
if ( is_string( $template_content ) ) { |
| 106 |
$template_content = json_decode( $template_content, true ); |
| 107 |
} |
| 108 |
if ( ! is_array( $template_content ) ) { |
| 109 |
continue; |
| 110 |
} |
| 111 |
$all_widgets = array_merge( |
| 112 |
$all_widgets, |
| 113 |
$this->recursively_get_inner_widgets( $template_content ) |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
set_transient( self::CACHE_KEY, $all_widgets, MONTH_IN_SECONDS ); |
| 118 |
return $all_widgets; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Get all inner widgets recursively. |
| 123 |
* |
| 124 |
* @param array $template_content Template content to analyze. |
| 125 |
* @return array |
| 126 |
*/ |
| 127 |
protected function recursively_get_inner_widgets( array $template_content ): array { |
| 128 |
$widgets = []; |
| 129 |
foreach ( $template_content as $element ) { |
| 130 |
if ( ! empty( $element['widgetType'] ) ) { |
| 131 |
$widgets[] = $element; |
| 132 |
} |
| 133 |
if ( ! empty( $element['elements'] ) && is_array( $element['elements'] ) ) { |
| 134 |
$widgets = array_merge( $widgets, $this->recursively_get_inner_widgets( $element['elements'] ) ); |
| 135 |
} |
| 136 |
} |
| 137 |
return $widgets; |
| 138 |
} |
| 139 |
} |
| 140 |
|