PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta1
Elementor Website Builder – more than just a page builder v4.3.0-beta1
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.3.0-beta1, at modules/markdown-render/markdown-renderer.php

139 lines 3.4 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 return Module::execute_while_rendering_markdown( function () use ( $document ) {
15 $frontmatter = $this->build_frontmatter( $document );
16 $data = $document->get_elements_data();
17
18 if ( empty( $data ) ) {
19 return $frontmatter;
20 }
21
22 $sections = [];
23
24 foreach ( $data as $element_data ) {
25 $md = $this->render_element( $element_data );
26
27 if ( ! empty( trim( $md ) ) ) {
28 $sections[] = $md;
29 }
30 }
31
32 $body = implode( "\n\n---\n\n", $sections );
33
34 $output = $frontmatter . "\n\n" . $body;
35
36 return apply_filters( 'elementor/markdown/document_output', $output, $document );
37 } );
38 }
39
40 public function render_elements_data( array $elements_data ): string {
41 return Module::execute_while_rendering_markdown( function () use ( $elements_data ) {
42 if ( empty( $elements_data ) ) {
43 return '';
44 }
45
46 $sections = [];
47
48 foreach ( $elements_data as $element_data ) {
49 $md = $this->render_element( $element_data );
50
51 if ( ! empty( trim( $md ) ) ) {
52 $sections[] = $md;
53 }
54 }
55
56 return implode( "\n\n", $sections );
57 } );
58 }
59
60 private function build_frontmatter( Document $document ): string {
61 $post_id = $document->get_main_id();
62
63 $lines = [ '---' ];
64 $lines[] = 'title: "' . $this->escape_yaml_string( get_the_title( $post_id ) ) . '"';
65
66 $description = $this->get_meta_description( $post_id );
67
68 if ( $description ) {
69 $lines[] = 'description: "' . $this->escape_yaml_string( $description ) . '"';
70 }
71
72 $thumbnail = get_the_post_thumbnail_url( $post_id, 'full' );
73
74 if ( $thumbnail ) {
75 $lines[] = 'featured_image: "' . esc_url( $thumbnail ) . '"';
76 }
77
78 $permalink = get_permalink( $post_id );
79
80 if ( is_string( $permalink ) && '' !== $permalink ) {
81 $lines[] = 'url: "' . esc_url( $permalink ) . '"';
82 }
83
84 $modified_date = get_the_modified_date( 'c', $post_id );
85
86 if ( is_string( $modified_date ) && '' !== $modified_date ) {
87 $lines[] = 'date_modified: "' . $this->escape_yaml_string( $modified_date ) . '"';
88 }
89
90 $lines[] = '---';
91
92 return implode( "\n", $lines );
93 }
94
95 private function get_meta_description( int $post_id ): string {
96 $description = get_post_meta( $post_id, '_yoast_wpseo_metadesc', true );
97
98 if ( ! empty( $description ) ) {
99 return $description;
100 }
101
102 $description = get_post_meta( $post_id, '_aioseo_description', true );
103
104 if ( ! empty( $description ) ) {
105 return $description;
106 }
107
108 $excerpt = get_the_excerpt( $post_id );
109
110 return ! empty( $excerpt ) ? $excerpt : '';
111 }
112
113 private function render_element( array $element_data ): string {
114 $element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
115
116 if ( ! $element ) {
117 return '';
118 }
119
120 $markdown = $element->render_markdown();
121
122 return apply_filters( 'elementor/markdown/element_output', $markdown, $element, $element_data );
123 }
124
125 private function escape_yaml_string( string $value ): string {
126 $value = html_entity_decode( $value, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
127 $value = str_replace( "\xE2\x80\x8B", '', $value );
128 $value = preg_replace( '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/', '', $value );
129
130 return strtr( $value, [
131 '\\' => '\\\\',
132 '"' => '\\"',
133 "\n" => '\\n',
134 "\r" => '\\r',
135 "\t" => '\\t',
136 ] );
137 }
138 }
139