← Back
Editing: widget-area-utils.php
<?php /** * Widget area rendering utilities for ElementsKit dynamic content widgets. * * @package ElementsKit_Lite\Modules\Controls */ namespace ElementsKit_Lite\Modules\Controls; defined( 'ABSPATH' ) || exit; /** * Handles rendering and AJAX loading of "widget area" dynamic content blocks * used by ElementsKit's Elementor widgets/controls. * * Responsible for: * - Registering the editor modal markup and AJAX endpoints. * - Rendering a widget area's linked `elementskit_content` post as Elementor content. * - Resolving the correct content post for a given widget/tab, including WPML support. */ class Widget_Area_Utils { /** * Registers editor hooks and AJAX handlers. * * @return void */ function init() { add_action( 'elementor/editor/after_enqueue_styles', array( $this, 'modal_content' ) ); add_action( 'wp_ajax_ekit_widgetarea_content', array( $this, 'ekit_widgetarea_content' ) ); add_action( 'wp_ajax_nopriv_ekit_widgetarea_content', array( $this, 'ekit_widgetarea_content' ) ); } /** * AJAX handler: renders a published post's Elementor content for the widget area. * * Expects `$_POST['nonce']` (verified against the `ekit_pro` action) and * `$_POST['post_id']`. Dies with no output if the nonce is invalid or the * post isn't published. * * @return void Echoes rendered HTML and terminates via `wp_die()`. */ public function ekit_widgetarea_content() { if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['nonce'] ) ), 'ekit_pro' ) ) { wp_die(); } $post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0; // This endpoint is public, so it must only ever render Widget Area content. if ( 'publish' !== get_post_status( $post_id ) || 'elementskit_content' !== get_post_type( $post_id ) ) { wp_die(); } if ( isset( $post_id ) ) { // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Displaying with Elementor content rendering echo str_replace( '#elementor', '', \ElementsKit_Lite\Utils::render_tab_content( \ElementsKit_Lite\Utils::render_elementor_content( $post_id ), $post_id ) ); } else { echo esc_html__( 'Click on the Edit Content button to edit/add the content.', 'elementskit-lite' ); } wp_die(); } /** * Outputs the widget-area editor modal markup into the Elementor editor page. * * Buffers `widget-area-modal.php`, passes it through `Utils::render()` for * escaping/rendering, then echoes it directly (hooked on * `elementor/editor/after_enqueue_styles`). * * @return void */ public function modal_content() { ob_start(); ?> <div class="widgetarea_iframe_modal"> <?php include 'widget-area-modal.php'; ?> </div> <?php $output = ob_get_contents(); ob_end_clean(); echo \ElementsKit_Lite\Utils::render( $output ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Already escaped inside of the buffering content } /** * Renders a widget area's dynamic content block, either as a full render * or as an empty AJAX-loadable container. * * Resolves the linked `elementskit_content` post by slug * (`dynamic-content-widget-{key}-{tab_id}`, falling back to * `dynamic-content-widget-{key}-{index}` for legacy data), applies WPML * translation mapping when active, and renders its Elementor content. * * @param string $content Raw content string; used as the widget key when non-empty * (may contain a `***`-delimited suffix, which is stripped). * @param string $widget_key Fallback widget key, used when `$content` is empty. * @param int|string $tab_id Tab/index identifier used to build the content post slug. Default 1. * @param string $isAjax `'yes'` to render an empty AJAX-loadable container instead of * full content (content is then fetched client-side). Default ''. * @param int|null $index Legacy index used as a fallback slug suffix for old version/data * support, when no post is found using `$tab_id`. Default null. * @return string Rendered HTML markup for the widget area. */ public static function parse( $content, $widget_key, $tab_id = 1, $isAjax = '', $index = null ) { $key = ( $content == '' ) ? $widget_key : $content; $extract_key = explode( '***', $key ); $extract_key = $extract_key[0]; $is_edit_mode = \Elementor\Plugin::$instance->editor->is_edit_mode(); $builder_post_title = 'dynamic-content-widget-' . $extract_key . '-' . $tab_id; // The editor also shows content still pending review, so its author can see their work. $post_status = $is_edit_mode ? 'any' : 'publish'; $builder_post = \ElementsKit_Lite\Utils::get_page_by_slug( $builder_post_title, 'elementskit_content', $post_status ); // "Edit Content" must open the post being shown, not a new empty one beside it. $edit_key = $extract_key; $edit_index = $tab_id; // Legacy fallback: retry the lookup keyed by $index for old version/data support. if ( ! isset( $builder_post->ID ) && null !== $index ) { $builder_post = \ElementsKit_Lite\Utils::get_page_by_slug( 'dynamic-content-widget-' . $extract_key . '-' . $index, 'elementskit_content', $post_status ); if ( isset( $builder_post->ID ) ) { $edit_index = $index; } } // A copied widget keeps the value of the original, while a saved value with no content // behind it has nothing to keep. Start under this widget's own key, otherwise editing // the copy would create a post the original widget then shows as well. if ( $is_edit_mode && ! isset( $builder_post->ID ) && $extract_key !== $widget_key ) { $edit_key = $widget_key; } ob_start(); ?> <div class="widgetarea_warper widgetarea_warper_editable" data-elementskit-widgetarea-key="<?php echo esc_attr( $edit_key ); ?>" data-elementskit-widgetarea-index="<?php echo esc_attr( $edit_index ); ?>"> <?php if ( $is_edit_mode ) : ?> <div class="widgetarea_warper_edit" data-elementskit-widgetarea-key="<?php echo esc_attr( $edit_key ); ?>" data-elementskit-widgetarea-index="<?php echo esc_attr( $edit_index ); ?>"> <i class="eicon-edit" aria-hidden="true"></i> <span><?php esc_html_e( 'Edit Content', 'elementskit-lite' ); ?></span> </div> <?php endif; ?> <?php if ( $isAjax === 'yes' ) { // Defer rendering: output an empty container the front end fills via AJAX. $post_id = isset( $builder_post->ID ) ? $builder_post->ID : ''; echo '<div class="ekit-widget-area-container" data-ajax-post-id="' . esc_attr( $post_id ) . '"></div>'; } else { ?> <div class="ekit-widget-area-container"> <?php if ( isset( $builder_post->ID ) ) { $builder_post_id = $builder_post->ID; // If WPML is active, resolve the translated content post ID. if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { $language_details = apply_filters( 'wpml_post_language_details', null, get_the_ID() ); if ( ! is_wp_error( $language_details ) ) { $builder_post_id = apply_filters( 'wpml_object_id', $builder_post_id, 'elementskit_content', true, $language_details['language_code'] ); } } $builder_content = \ElementsKit_Lite\Utils::render_elementor_content( $builder_post_id ); $rendered_tab_content = \ElementsKit_Lite\Utils::render_tab_content( $builder_content, $builder_post_id ); // Remove '#elementor' from the rendered content, except when it's part of 'href="#elementor"'. $final_content = preg_replace( '/(?<!href=")#elementor/', '', $rendered_tab_content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Displaying with Elementor content rendering echo $final_content; } else { echo esc_html__( 'Click on the Edit Content button to edit/add the content.', 'elementskit-lite' ); } ?> </div> <?php } ?> </div> <?php $output = ob_get_contents(); ob_end_clean(); return $output; } }
Save File
Cancel