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 / module.php

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

219 lines 6.7 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\Module as BaseModule;
5 use Elementor\Core\Experiments\Manager as Experiments_Manager;
6 use Elementor\Core\Frontend\Widget_Content_Render_Mode;
7 use Elementor\Plugin;
8 use Elementor\Settings;
9 use Elementor\Utils;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 class Module extends BaseModule {
16
17 const EXPERIMENT_NAME = 'markdown_rendering';
18 const CACHE_META_KEY = '_elementor_markdown_cache';
19
20 public static function is_rendering_markdown(): bool {
21 return Widget_Content_Render_Mode::is( Widget_Content_Render_Mode::MARKDOWN );
22 }
23
24 public static function set_rendering_markdown( bool $is_rendering ): void {
25 Widget_Content_Render_Mode::set_current(
26 $is_rendering ? Widget_Content_Render_Mode::MARKDOWN : Widget_Content_Render_Mode::NORMAL
27 );
28 }
29
30 public static function execute_while_rendering_markdown( callable $callback ) {
31 // Markdown rendering must not enqueue editor CSS or parse post stylesheets.
32 // The render mode lets CSS and document cache layers skip those side effects while widgets render.
33 return Widget_Content_Render_Mode::execute_as( Widget_Content_Render_Mode::MARKDOWN, $callback );
34 }
35
36 public function get_name() {
37 return 'markdown-render';
38 }
39
40 public static function get_experimental_data() {
41 return [
42 'name' => self::EXPERIMENT_NAME,
43 'title' => esc_html__( 'Generate website markdown', 'elementor' ),
44 'description' => sprintf(
45 '%1$s <a href="https://go.elementor.com/wp-dash-generate-website-markdown-article/" target="_blank">%2$s</a>',
46 esc_html__( 'Serve your pages as Markdown files so AI agents can ingest and understand your content more efficiently.', 'elementor' ),
47 esc_html__( 'Learn more', 'elementor' )
48 ),
49 'default' => Experiments_Manager::STATE_INACTIVE,
50 'release_status' => Experiments_Manager::RELEASE_STATUS_ALPHA,
51 ];
52 }
53
54 public function __construct() {
55 parent::__construct();
56
57 add_action( 'template_redirect', [ $this, 'maybe_serve_markdown' ], 1 );
58
59 add_action( 'elementor/core/files/clear_cache', [ $this, 'clear_all_markdown_cache' ] );
60 add_action( 'save_post', [ $this, 'clear_post_markdown_cache' ] );
61 add_action( 'activated_plugin', [ $this, 'clear_all_markdown_cache' ] );
62 add_action( 'deactivated_plugin', [ $this, 'clear_all_markdown_cache' ] );
63 add_action( 'switch_theme', [ $this, 'clear_all_markdown_cache' ] );
64
65 if ( is_admin() ) {
66 add_action(
67 'elementor/admin/after_create_settings/' . Settings::PAGE_ID,
68 [ $this, 'register_admin_fields' ],
69 100
70 );
71 }
72 }
73
74 public function maybe_serve_markdown() {
75 if ( ! $this->is_markdown_request() ) {
76 return;
77 }
78
79 if ( ! is_singular() ) {
80 return;
81 }
82
83 $post_id = get_the_ID();
84 $post = get_post( $post_id );
85
86 if ( ! $post ) {
87 return;
88 }
89
90 $is_preview = $this->is_valid_preview_request( $post_id );
91
92 if ( ! $is_preview && 'publish' !== $post->post_status ) {
93 return;
94 }
95
96 if ( post_password_required( $post ) ) {
97 return;
98 }
99
100 $document = $is_preview
101 ? Plugin::$instance->documents->get_doc_for_frontend( $post_id )
102 : Plugin::$instance->documents->get( $post_id );
103
104 if ( ! $document || ! $document->is_built_with_elementor() ) {
105 return;
106 }
107
108 self::execute_while_rendering_markdown( function () use ( $is_preview, $document, $post_id ) {
109 if ( $is_preview ) {
110 $markdown = ( new Markdown_Renderer() )->render( $document );
111 } else {
112 $markdown = $this->get_cached_markdown( $post_id );
113
114 if ( false === $markdown ) {
115 $markdown = ( new Markdown_Renderer() )->render( $document );
116 $this->set_cached_markdown( $post_id, $markdown );
117 }
118 }
119
120 Utils::do_not_cache();
121 status_header( 200 );
122 header( 'Content-Type: text/markdown; charset=utf-8' );
123 header( 'X-Content-Type-Options: nosniff' );
124 Utils::print_unescaped_internal_string( $markdown );
125 exit;
126 } );
127 }
128
129 private function is_valid_preview_request( int $post_id ): bool {
130 if ( ! is_preview() ) {
131 return false;
132 }
133
134 $preview_id = isset( $_GET['preview_id'] ) ? absint( wp_unslash( $_GET['preview_id'] ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
135 $preview_nonce = sanitize_text_field( wp_unslash( $_GET['preview_nonce'] ?? '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
136
137 if ( ! $preview_id || ! wp_verify_nonce( $preview_nonce, 'post_preview_' . $preview_id ) ) {
138 return false;
139 }
140
141 return current_user_can( 'edit_post', $post_id );
142 }
143
144 private function is_markdown_request(): bool {
145 if ( isset( $_GET['format'] ) && 'markdown' === $_GET['format'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
146 return true;
147 }
148
149 $accept = isset( $_SERVER['HTTP_ACCEPT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_ACCEPT'] ) ) : '';
150
151 return false !== strpos( $accept, 'text/markdown' );
152 }
153
154 private function get_cached_markdown( int $post_id ) {
155 $cache = get_post_meta( $post_id, self::CACHE_META_KEY, true );
156
157 if ( empty( $cache ) || ! is_array( $cache ) ) {
158 return false;
159 }
160
161 if ( empty( $cache['timeout'] ) || time() > $cache['timeout'] ) {
162 return false;
163 }
164
165 return $cache['content'] ?? false;
166 }
167
168 private function set_cached_markdown( int $post_id, string $markdown ): void {
169 $ttl_hours = (int) get_option( 'elementor_markdown_cache_ttl', 24 );
170
171 if ( $ttl_hours <= 0 ) {
172 return;
173 }
174
175 $cache = [
176 'timeout' => time() + ( $ttl_hours * HOUR_IN_SECONDS ),
177 'content' => $markdown,
178 ];
179
180 update_post_meta( $post_id, self::CACHE_META_KEY, $cache );
181 }
182
183 public function clear_post_markdown_cache( int $post_id ): void {
184 delete_post_meta( $post_id, self::CACHE_META_KEY );
185 }
186
187 public function clear_all_markdown_cache(): void {
188 global $wpdb;
189 $wpdb->delete( $wpdb->postmeta, [ 'meta_key' => self::CACHE_META_KEY ] ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
190 }
191
192 public function register_admin_fields( Settings $settings ) {
193 $settings->add_field(
194 Settings::TAB_PERFORMANCE,
195 Settings::TAB_PERFORMANCE,
196 'markdown_cache_ttl',
197 [
198 'label' => esc_html__( 'Markdown Cache', 'elementor' ),
199 'field_args' => [
200 'class' => 'elementor-markdown-cache-ttl',
201 'type' => 'select',
202 'std' => '24',
203 'options' => [
204 '0' => esc_html__( 'Disable', 'elementor' ),
205 '1' => esc_html__( '1 Hour', 'elementor' ),
206 '6' => esc_html__( '6 Hours', 'elementor' ),
207 '12' => esc_html__( '12 Hours', 'elementor' ),
208 '24' => esc_html__( '1 Day', 'elementor' ),
209 '72' => esc_html__( '3 Days', 'elementor' ),
210 '168' => esc_html__( '1 Week', 'elementor' ),
211 '720' => esc_html__( '1 Month', 'elementor' ),
212 ],
213 'desc' => esc_html__( 'Specify the duration for which Markdown output is cached. This cache is served to AI crawlers requesting text/markdown content.', 'elementor' ),
214 ],
215 ]
216 );
217 }
218 }
219