register( new Elementor\Widget() ); } /** * The SR mark as the widget's icon in Elementor's panel. */ public static function editor_styles() { Picker::enqueue_still_styles(); } /** * The still's look inside Elementor's preview. */ public static function preview_styles() { Picker::enqueue_still_styles(); } /** * After Elementor saves a page: fills the start cache for its modules. * * @param int $post_id The post. * @param array $editor_data Elementor's elements. */ public static function after_save( $post_id, $editor_data ) { foreach ( self::keys_in( is_array( $editor_data ) ? $editor_data : array() ) as $key ) { Start::refresh( $key ); } } /** * The module keys of our widgets in Elementor's elements, nested ones included. * * @param array $elements Elementor's elements. * @return string[] */ public static function keys_in( array $elements ) { $keys = array(); foreach ( $elements as $element ) { if ( ! is_array( $element ) ) { continue; } if ( self::WIDGET === ( $element['widgetType'] ?? '' ) ) { $key = $element['settings']['module'] ?? ''; if ( Keys::is_embed_key( $key ) && ! Keys::is_group_key( $key ) ) { $keys[] = $key; } } if ( ! empty( $element['elements'] ) && is_array( $element['elements'] ) ) { $keys = array_merge( $keys, self::keys_in( $element['elements'] ) ); } } return array_values( array_unique( $keys ) ); } /** * Whether Elementor is showing its editor or its editor's preview. * * @return bool */ public static function is_editor() { if ( ! class_exists( '\Elementor\Plugin' ) || ! isset( \Elementor\Plugin::$instance ) ) { return false; } $plugin = \Elementor\Plugin::$instance; return ( isset( $plugin->editor ) && $plugin->editor->is_edit_mode() ) || ( isset( $plugin->preview ) && $plugin->preview->is_preview_mode() ); } /** * The panel's choices: the published modules and pages (not drafts, not modal-only modules). * * @return array Key => name. */ public static function module_options() { $options = array( '' => __( '— Pick a module —', 'sr-anywhere' ) ); // Elementor registers a widget's controls on public page views too; the output never reads the token (decision // 0002), so the list is built only where the panel can show it. $panel = is_admin() || wp_doing_ajax() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || self::is_editor(); if ( ! $panel ) { return $options; } foreach ( Picker::items() as $item ) { // Plain text (sanitize_text_field in Rest::item): Elementor prints select labels unescaped. $options[ $item['embedKey'] ] = Picker::label( $item ); } return $options; } /** * The module's still for Elementor's editor; it also fills the start cache, so the page prints the start at once. * * @param string $key The chosen embed key. * @return string */ public static function still( $key ) { return Picker::still( $key, __( 'Pick a module in the panel.', 'sr-anywhere' ) ); } }