PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.0
Elementor Website Builder – more than just a page builder v4.2.0
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / markdown-render / markdown-renderer.php

markdown-renderer.php in Elementor Website Builder – more than just a page builder 4.2.0, at modules/markdown-render/markdown-renderer.php

117 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Modules\MarkdownRender;
3
4 use Elementor\Core\Base\Document;
5 use Elementor\Plugin;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit;
9 }
10
11 class Markdown_Renderer {
12
13 public function render( Document $document ): string {
14 $frontmatter = $this->build_frontmatter( $document );
15 $data = $document->get_elements_data();
16
17 if ( empty( $data ) ) {
18 return $frontmatter;
19 }
20
21 $sections = [];
22
23 foreach ( $data as $element_data ) {
24 $md = $this->render_element( $element_data );
25
26 if ( ! empty( trim( $md ) ) ) {
27 $sections[] = $md;
28 }
29 }
30
31 $body = implode( "\n\n---\n\n", $sections );
32
33 $output = $frontmatter . "\n\n" . $body;
34
35 return apply_filters( 'elementor/markdown/document_output', $output, $document );
36 }
37
38 private function build_frontmatter( Document $document ): string {
39 $post_id = $document->get_main_id();
40
41 $lines = [ '---' ];
42 $lines[] = 'title: "' . $this->escape_yaml_string( get_the_title( $post_id ) ) . '"';
43
44 $description = $this->get_meta_description( $post_id );
45
46 if ( $description ) {
47 $lines[] = 'description: "' . $this->escape_yaml_string( $description ) . '"';
48 }
49
50 $thumbnail = get_the_post_thumbnail_url( $post_id, 'full' );
51
52 if ( $thumbnail ) {
53 $lines[] = 'featured_image: "' . esc_url( $thumbnail ) . '"';
54 }
55
56 $permalink = get_permalink( $post_id );
57
58 if ( is_string( $permalink ) && '' !== $permalink ) {
59 $lines[] = 'url: "' . esc_url( $permalink ) . '"';
60 }
61
62 $modified_date = get_the_modified_date( 'c', $post_id );
63
64 if ( is_string( $modified_date ) && '' !== $modified_date ) {
65 $lines[] = 'date_modified: "' . $this->escape_yaml_string( $modified_date ) . '"';
66 }
67
68 $lines[] = '---';
69
70 return implode( "\n", $lines );
71 }
72
73 private function get_meta_description( int $post_id ): string {
74 $description = get_post_meta( $post_id, '_yoast_wpseo_metadesc', true );
75
76 if ( ! empty( $description ) ) {
77 return $description;
78 }
79
80 $description = get_post_meta( $post_id, '_aioseo_description', true );
81
82 if ( ! empty( $description ) ) {
83 return $description;
84 }
85
86 $excerpt = get_the_excerpt( $post_id );
87
88 return ! empty( $excerpt ) ? $excerpt : '';
89 }
90
91 private function render_element( array $element_data ): string {
92 $element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
93
94 if ( ! $element ) {
95 return '';
96 }
97
98 $markdown = $element->render_markdown();
99
100 return apply_filters( 'elementor/markdown/element_output', $markdown, $element, $element_data );
101 }
102
103 private function escape_yaml_string( string $value ): string {
104 $value = html_entity_decode( $value, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
105 $value = str_replace( "\xE2\x80\x8B", '', $value );
106 $value = preg_replace( '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/', '', $value );
107
108 return strtr( $value, [
109 '\\' => '\\\\',
110 '"' => '\\"',
111 "\n" => '\\n',
112 "\r" => '\\r',
113 "\t" => '\\t',
114 ] );
115 }
116 }
117