PluginProbe
BeyondWords – AI audio for publishers / 4.0.0
BeyondWords – AI audio for publishers v4.0.0
7.1.0 trunk 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.3.0 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 4.7.0 All 43 releases
speechkit / src / Component / Post / Panel / Inspect / Inspect.php

Inspect.php in BeyondWords – AI audio for publishers 4.0.0, at src/Component/Post/Panel/Inspect/Inspect.php

273 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 /**
6 * BeyondWords Post Inspect Panel.
7 *
8 * @package Beyondwords\Wordpress
9 * @author Stuart McAlpine <stu@beyondwords.io>
10 * @since 3.0.0
11 */
12
13 namespace Beyondwords\Wordpress\Component\Post\Panel\Inspect;
14
15 use Beyondwords\Wordpress\Component\Post\PostMetaUtils;
16 use Beyondwords\Wordpress\Component\Settings\SettingsUtils;
17
18 /**
19 * Inspect setup
20 *
21 * @since 3.0.0
22 */
23 class Inspect
24 {
25 /**
26 * Constructor
27 */
28 public function __construct()
29 {
30 add_action('admin_enqueue_scripts', array($this, 'adminEnqueueScripts'));
31 add_action("add_meta_boxes", array($this, 'addMetaBox'));
32
33 add_filter('default_hidden_meta_boxes', array($this, 'hideMetaBox'));
34 }
35
36 /**
37 * Enqueue JS for Inspect feature.
38 */
39 public function adminEnqueueScripts($hook)
40 {
41 // Only enqueue for Post screens
42 if ($hook === 'post.php' || $hook === 'post-new.php') {
43 // Clipboard.js dependency
44 wp_enqueue_script(
45 'clipboard',
46 'https://cdn.jsdelivr.net/npm/clipboard@2.0.10/dist/clipboard.min.js',
47 array('jquery'),
48 null,
49 true
50 );
51
52 // Our script for the "Copy" button
53 wp_enqueue_script(
54 'beyondwords-inspect',
55 BEYONDWORDS__PLUGIN_URI . 'src/Component/Post/Panel/Inspect/js/inspect.js',
56 array('jquery', 'clipboard'),
57 BEYONDWORDS__PLUGIN_VERSION,
58 true
59 );
60 }
61 }
62
63 /**
64 * Hides the metabox by default.
65 *
66 * @param string[] $hidden An array of IDs of meta boxes hidden by default.
67 */
68 public function hideMetaBox($hidden)
69 {
70 $hidden[] = 'beyondwords__inspect';
71 return $hidden;
72 }
73
74 /**
75 * Adds the meta box container for the Classic Editor.
76 *
77 * The Block Editor UI is handled using JavaScript.
78 *
79 * @param $postType
80 */
81 public function addMetaBox($postType)
82 {
83 $postTypes = SettingsUtils::getSupportedPostTypes();
84
85 if (is_array($postTypes) && ! in_array($postType, $postTypes)) {
86 return;
87 }
88
89 add_meta_box(
90 'beyondwords__inspect',
91 __('BeyondWords', 'speechkit') . ': ' . __('Inspect', 'speechkit'),
92 array($this, 'renderMetaBoxContent'),
93 $postType,
94 'advanced',
95 'low',
96 [
97 '__back_compat_meta_box' => true,
98 ]
99 );
100 }
101
102 /**
103 * Render Meta Box content.
104 *
105 * @param WP_Post $post The post object.
106 */
107 public function renderMetaBoxContent($post)
108 {
109 $metadata = PostMetaUtils::getAllBeyondwordsMetadata($post->ID);
110
111 $this->postMetaTable($metadata);
112 ?>
113 <button
114 type="button"
115 id="beyondwords__inspect--copy"
116 class="button button-large"
117 style="margin: 10px 0 0;"
118 data-clipboard-text="<?php echo esc_attr($this->getClipboardText($metadata)); ?>"
119 >
120 <?php _e('Copy', 'speechkit'); ?>
121 <span
122 id="beyondwords__inspect--copy-confirm"
123 style="display: none; margin: 5px 0 0;"
124 class="dashicons dashicons-yes"
125 ></span>
126 </button>
127
128 <button
129 type="button"
130 id="beyondwords__inspect--remove"
131 class="button button-large button-link-delete"
132 style="margin: 10px 0 0; float: right;"
133 >
134 <?php _e('Remove', 'speechkit'); ?>
135 <span
136 id="beyondwords__inspect--remove"
137 style="display: none; margin: 5px 0 0;"
138 class="dashicons dashicons-yes"
139 ></span>
140 </button>
141
142 <?php
143 }
144
145 /**
146 * Render Meta Box table.
147 *
148 * @param array $metadata The metadata returned by has_meta.
149 *
150 * @since v3.0.0
151 * @since v3.9.0 Change $postMetaKeys param to $metadata, to support meta_ids.
152 */
153 public function postMetaTable($metadata)
154 {
155 if (! is_array($metadata)) {
156 return;
157 }
158 ?>
159 <div id="postcustomstuff">
160 <table id="inspect-table">
161 <thead>
162 <tr>
163 <th class="left"><?php _e('Name'); ?></th>
164 <th><?php _e('Value'); ?></th>
165 </tr>
166 </thead>
167 <tbody id="inspect-table-list">
168 <?php
169 foreach ($metadata as $item) :
170 if (
171 ! is_array($item) ||
172 ! array_key_exists('meta_id', $item) ||
173 ! array_key_exists('meta_key', $item) ||
174 ! array_key_exists('meta_value', $item)
175 ) {
176 continue;
177 }
178
179 $metaId = $item['meta_id'] ? $item['meta_id'] : $item['meta_key'];
180 $metaKey = $item['meta_key'];
181 $metaValue = $this->formatPostMetaValue($item['meta_value']);
182 ?>
183 <tr id="beyondwords-inspect-<?php echo esc_attr($metaId); ?>" class="alternate">
184 <td class="left">
185 <label
186 class="screen-reader-text"
187 for="beyondwords-inspect-<?php echo esc_attr($metaId); ?>-key"
188 >
189 <?php _e('Key'); ?>
190 </label>
191 <input
192 id="beyondwords-inspect-<?php echo esc_attr($metaId); ?>-key"
193 type="text"
194 size="20"
195 value="<?php echo esc_attr($metaKey); ?>"
196 readonly
197 />
198 <?php if ($item['meta_id']) : ?>
199 <input
200 name="deletemeta[<?php echo esc_attr($item['meta_id']); ?>]"
201 data-beyondwords-deletemeta="true"
202 type="hidden"
203 value="<?php echo esc_attr($item['meta_id']); ?>"
204 disabled
205 />
206 <?php endif; ?>
207 </td>
208 <td>
209 <label
210 class="screen-reader-text"
211 for="beyondwords-inspect-<?php echo esc_attr($metaId); ?>-value"
212 >
213 <?php _e('Value'); ?>
214 </label>
215 <textarea
216 id="beyondwords-inspect-<?php echo esc_attr($metaId); ?>-value"
217 rows="2"
218 cols="30"
219 data-beyondwords-metavalue="true"
220 readonly
221 ><?php echo esc_html($metaValue); ?></textarea>
222 </td>
223 </tr>
224 <?php
225 endforeach;
226 ?>
227 </tbody>
228 </table>
229 </div>
230 <?php
231 }
232
233 /**
234 * Format post meta value.
235 *
236 * @param mixed $value The metadata value.
237 *
238 * @since v3.9.0
239 */
240 public function formatPostMetaValue($value)
241 {
242 if (is_numeric($value) || is_string($value)) {
243 return $value;
244 }
245
246 return wp_json_encode($value);
247 }
248
249 /**
250 * Get Clipboard Text.
251 *
252 * @param array $metadata Post metadata.
253 *
254 * @since 3.0.0
255 * @since 3.9.0 Output all post meta data from the earlier has_meta() call instead of
256 * the previous multiple get_post_meta() calls.
257 *
258 * @return string
259 */
260 public function getClipboardText($metadata)
261 {
262 $lines = [];
263
264 foreach ($metadata as $m) {
265 $lines[] = $m['meta_key'] . "\r\n" . $this->formatPostMetaValue($m['meta_value']);
266 }
267
268 $lines[] = "=== " . __('Copied using the Classic Editor', 'speechkit') . " ===\r\n\r\n";
269
270 return implode("\r\n\r\n", $lines);
271 }
272 }
273