placement = $placement; } /** * @param array $data Elementor's stored document data. * @return array{source:string,html:string} */ public function article_source( array $data ): array { $elements = $this->document_elements( $data ); $fragments = array(); $has_article_start = false; $this->collect_fragments( $elements, $fragments, $has_article_start ); return array( 'source' => $has_article_start ? 'Elementor stored text content' : '', 'html' => $has_article_start ? implode( "\n", $fragments ) : '', ); } /** * @param array $data Elementor's stored document data. */ public function has_article_start( array $data ): bool { $source = $this->article_source( $data ); return $this->placement->contains_article_start( $source['html'] ); } /** * @param array $data * @return array */ private function document_elements( array $data ): array { if ( isset( $data['content'] ) && is_array( $data['content'] ) ) { return $data['content']; } return $data; } /** * @param array $elements * @param array $fragments */ private function collect_fragments( array $elements, array &$fragments, bool &$has_article_start ): void { foreach ( $elements as $element ) { if ( ! is_array( $element ) ) { continue; } $widget_type = isset( $element['widgetType'] ) ? strtolower( (string) $element['widgetType'] ) : ''; $settings = isset( $element['settings'] ) && is_array( $element['settings'] ) ? $element['settings'] : array(); $fragment = $this->widget_fragment( $widget_type, $settings, $has_article_start ); if ( '' !== trim( $fragment ) ) { $fragments[] = $fragment; } $children = isset( $element['elements'] ) && is_array( $element['elements'] ) ? $element['elements'] : array(); if ( ! empty( $children ) ) { $this->collect_fragments( $children, $fragments, $has_article_start ); } } } /** * @param array $settings */ private function widget_fragment( string $widget_type, array $settings, bool &$has_article_start ): string { if ( 'shortcode' === $widget_type ) { $shortcode = isset( $settings['shortcode'] ) && is_string( $settings['shortcode'] ) ? $settings['shortcode'] : ''; if ( '' === trim( $shortcode ) ) { return ''; } $normalised = $this->placement->normalise_shortcodes( $shortcode ); if ( $normalised === $shortcode ) { return ''; } if ( $this->placement->contains_article_start( $shortcode ) ) { $has_article_start = true; } return $normalised; } if ( 'heading' === $widget_type ) { $title = isset( $settings['title'] ) && is_string( $settings['title'] ) ? $settings['title'] : ''; if ( '' === trim( $title ) ) { return ''; } $tag = isset( $settings['header_size'] ) && is_string( $settings['header_size'] ) ? strtolower( $settings['header_size'] ) : 'h2'; $tag = in_array( $tag, array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'span', 'p' ), true ) ? $tag : 'h2'; return '<' . $tag . '>' . $title . ''; } if ( 'text-editor' === $widget_type ) { return isset( $settings['editor'] ) && is_string( $settings['editor'] ) ? $settings['editor'] : ''; } if ( 'html' === $widget_type ) { $html = isset( $settings['html'] ) && is_string( $settings['html'] ) ? $settings['html'] : ''; if ( $this->placement->contains_article_start( $html ) ) { $has_article_start = true; } return $html; } return ''; } }