| @@ -2,10 +2,12 @@ | ||
| 2 | 2 | namespace Elementor\Modules\MarkdownRender; |
| 3 | 3 | |
| 4 | 4 | use Elementor\Core\Base\Module as BaseModule; |
| 5 | 5 | use Elementor\Core\Experiments\Manager as Experiments_Manager; |
| 6 | +use Elementor\Core\Frontend\Widget_Content_Render_Mode; | |
| 6 | 7 | use Elementor\Plugin; |
| 7 | 8 | use Elementor\Settings; |
| 9 | +use Elementor\Utils; | |
| 8 | 10 | |
| 9 | 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | 12 | exit; |
| 11 | 13 | } |
| @@ -14,8 +16,24 @@ | ||
| 14 | 16 | |
| 15 | 17 | const EXPERIMENT_NAME = 'markdown_rendering'; |
| 16 | 18 | const CACHE_META_KEY = '_elementor_markdown_cache'; |
| 17 | 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 | + | |
| 18 | 36 | public function get_name() { |
| 19 | 37 | return 'markdown-render'; |
| 20 | 38 | } |
| 21 | 39 | |
| @@ -86,25 +104,27 @@ | ||
| 86 | 104 | if ( ! $document || ! $document->is_built_with_elementor() ) { |
| 87 | 105 | return; |
| 88 | 106 | } |
| 89 | 107 | |
| 90 | - if ( $is_preview ) { | |
| 91 | - $markdown = ( new Markdown_Renderer() )->render( $document ); | |
| 92 | - } else { | |
| 93 | - $markdown = $this->get_cached_markdown( $post_id ); | |
| 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 ); | |
| 94 | 113 | |
| 95 | - if ( false === $markdown ) { | |
| 96 | - $markdown = ( new Markdown_Renderer() )->render( $document ); | |
| 97 | - $this->set_cached_markdown( $post_id, $markdown ); | |
| 114 | + if ( false === $markdown ) { | |
| 115 | + $markdown = ( new Markdown_Renderer() )->render( $document ); | |
| 116 | + $this->set_cached_markdown( $post_id, $markdown ); | |
| 117 | + } | |
| 98 | 118 | } |
| 99 | - } | |
| 100 | 119 | |
| 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; | |
| 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 | + } ); | |
| 107 | 127 | } |
| 108 | 128 | |
| 109 | 129 | private function is_valid_preview_request( int $post_id ): bool { |
| 110 | 130 | if ( ! is_preview() ) { |