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

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