PluginProbe
WP Coder – Insert & Manage Code Snippets / 4.3
WP Coder – Insert & Manage Code Snippets v4.3
4.5.1 1.1 2.3.1 2.3.2 2.4.1 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1 3.1.1 3.2 3.2.1 3.3 3.4 3.5 3.5.1 All 39 releases
wp-coder / includes / snippets / class-tool-markdown-editor.php

class-tool-markdown-editor.php in WP Coder – Insert & Manage Code Snippets 4.3, at includes/snippets/class-tool-markdown-editor.php

117 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use WPCoder\Parsedown\Parsedown;
4 use WPCoder\WPCoder;
5
6 defined('ABSPATH') || exit;
7
8 class WPCoder_Markdown_Editor {
9
10 private array $except_ids = [];
11 private array $options;
12
13 public function __construct( $options ) {
14 $this->options = is_array($options) ? $options : [];
15
16 $raw = $this->options['markdown_editor_except'] ?? '';
17 $this->except_ids = array_values(array_filter(array_map('intval', preg_split('/[,\s]+/', (string)$raw)), fn($v)=>$v>0));
18
19 add_filter('use_block_editor_for_post', function( $use, $post ) {
20 if ( ! $post instanceof WP_Post ) return false;
21 return in_array((int)$post->ID, $this->except_ids, true) ? $use : false;
22 }, 10, 2);
23
24 add_filter('user_can_richedit', function( $can ){
25 $post = $this->get_current_edit_post();
26 if ( ! $post ) return false;
27 return in_array((int)$post->ID, $this->except_ids, true) ? $can : false;
28 }, 50);
29
30 add_filter('the_content', [$this, 'content'], 9);
31
32 add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']);
33
34 add_action('wp_enqueue_scripts', [$this, 'maybe_strip_block_css'], 99);
35 }
36
37 /** Фронт: конвертація Markdown → HTML (крім винятків) */
38 public function content( $content ): string {
39 $post = get_post();
40 if ( $post && in_array((int)$post->ID, $this->except_ids, true) ) {
41 return $content;
42 }
43
44 remove_filter('the_content', 'wpautop');
45
46 $pd = new Parsedown();
47 $pd->setSafeMode(false);
48 $pd->setBreaksEnabled(true);
49
50 $html = $pd->text($content);
51
52 return wp_kses_post($html);
53 }
54
55 public function enqueue_scripts( $hook ): void {
56 if ($hook !== 'post.php' && $hook !== 'post-new.php') return;
57
58 $post = $this->get_current_edit_post();
59 if ( $post && in_array((int)$post->ID, $this->except_ids, true) ) {
60 return;
61 }
62
63 remove_action('media_buttons', 'media_buttons');
64
65 wp_enqueue_script('code-editor');
66 wp_enqueue_style('code-editor');
67
68 wp_enqueue_script('quicktags');
69 wp_enqueue_script('word-count');
70
71 $url = WPCoder::url() . 'assets/js/codemirror/';
72
73 wp_enqueue_style('cm-editor', $url . 'editor.css', [], '1.0');
74
75 wp_enqueue_script('cm-mode-markdown', $url . 'markdown.js', ['code-editor'], '1.0', true);
76 wp_enqueue_script('cm-mode-gfm', $url . 'gfm.js', ['cm-mode-markdown'], '1.0', true);
77
78 wp_enqueue_script('wpc-markdown-editor', $url . 'editor.js', ['cm-mode-markdown','quicktags'], '1.0', true);
79 }
80
81 private function get_current_edit_post(): ?WP_Post {
82 $post_id = 0;
83
84 if ( isset($_GET['post']) ) {
85 $post_id = (int) $_GET['post'];
86 } elseif ( isset($_POST['post_ID']) ) {
87 $post_id = (int) $_POST['post_ID'];
88 }
89
90 if ( $post_id > 0 ) {
91 $p = get_post($post_id);
92 return ($p instanceof WP_Post) ? $p : null;
93 }
94 return null;
95 }
96
97 public function maybe_strip_block_css(): void {
98 if ( is_singular() ) {
99 $post_id = get_the_ID();
100 if ( $post_id && in_array( (int) $post_id, $this->except_ids, true ) ) {
101 return;
102 }
103 }
104
105 $handles = [
106 'wp-block-library',
107 'wp-block-library-theme',
108 'global-styles',
109 'classic-theme-styles',
110 ];
111
112 foreach ( $handles as $h ) {
113 wp_dequeue_style( $h );
114 wp_deregister_style( $h );
115 }
116 }
117 }