PluginProbe
Extendify / 3.1.5
Extendify v3.1.5
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / app / QuickEdit / Schemas / Paragraph.php

Paragraph.php in Extendify 3.1.5, at app/QuickEdit/Schemas/Paragraph.php

104 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Extendify\QuickEdit\Schemas;
4
5 defined('ABSPATH') || die('No direct access.');
6
7 class Paragraph implements Schema
8 {
9 public function fields(): array
10 {
11 return [
12 [
13 'key' => 'content',
14 'control' => 'text',
15 'label' => __('Text', 'extendify-local'),
16 ],
17 [
18 'key' => 'align',
19 'control' => 'alignment',
20 'label' => __('Alignment', 'extendify-local'),
21 'options' => [
22 ['value' => 'left', 'label' => __('Left', 'extendify-local')],
23 ['value' => 'center', 'label' => __('Center', 'extendify-local')],
24 ['value' => 'right', 'label' => __('Right', 'extendify-local')],
25 ],
26 ],
27 ];
28 }
29
30 public function apply(array $block, string $fieldKey, $value): array
31 {
32 switch ($fieldKey) {
33 case 'content':
34 // Rewrite the inner <p> rather than replacing innerHTML wholesale,
35 // so user-set attributes (style, class, etc.) survive.
36 $text = is_string($value) ? $value : '';
37 $existing = (string) ($block['innerHTML'] ?? '');
38 $escaped = $this->escTextWithRichInlines($text);
39
40 if (preg_match('/<p\b[^>]*>/i', $existing)) {
41 // Callback (not a replacement string) so user text containing
42 // $1 / \1 / $0 is spliced literally, not parsed as a backref.
43 $newInner = preg_replace_callback(
44 '/(<p\b[^>]*>)(.*?)(<\/p>)/is',
45 static function ($m) use ($escaped) {
46 return $m[1] . $escaped . $m[3];
47 },
48 $existing,
49 1
50 );
51 } else {
52 $newInner = '<p>' . $escaped . '</p>';
53 }
54
55 $block['innerHTML'] = $newInner;
56 $block['innerContent'] = [$newInner];
57 return $block;
58
59 case 'align':
60 $allowed = ['left', 'center', 'right'];
61 $next = in_array($value, $allowed, true) ? $value : null;
62 $attrs = $block['attrs'] ?? [];
63
64 if ($next === null) {
65 unset($attrs['align']);
66 } else {
67 $attrs['align'] = $next;
68 }
69 // The editor adds has-text-align-* on save, but already-serialized
70 // markup needs the class updated directly to re-render aligned.
71 $block['attrs'] = $attrs;
72 $block['innerHTML'] = $this->updateAlignClass((string) ($block['innerHTML'] ?? ''), $next);
73 $block['innerContent'] = [$block['innerHTML']];
74 return $block;
75 }
76 return $block;
77 }
78
79 private function escTextWithRichInlines(string $text): string
80 {
81 return wp_kses_post($text);
82 }
83
84 private function updateAlignClass(string $html, $align): string
85 {
86 $tp = new \WP_HTML_Tag_Processor($html);
87 if (!$tp->next_tag('p')) {
88 return $html;
89 }
90 $classes = (string) ($tp->get_attribute('class') ?? '');
91 $classes = trim((string) preg_replace('/\bhas-text-align-(left|center|right)\b/', '', $classes));
92 $classes = preg_replace('/\s+/', ' ', $classes);
93 if ($align !== null) {
94 $classes = trim($classes . ' has-text-align-' . $align);
95 }
96 if ($classes === '') {
97 $tp->remove_attribute('class');
98 } else {
99 $tp->set_attribute('class', $classes);
100 }
101 return $tp->get_updated_html();
102 }
103 }
104