PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 1.1.10
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v1.1.10
1.1.10 1.1.9 1.1.8 1.1.7 1.1.6 1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.1 1.0.0 0.9.8 0.9.7 0.9.6 0.9.4 0.9.5 0.9.3 0.9.2 0.9.1 0.9.0 0.8.9 0.8.8 0.8.7 All 34 releases
desktop-mode / includes / my-wordpress / preview-actions.php

preview-actions.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin 1.1.10, at includes/my-wordpress/preview-actions.php

114 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * OpenStation — My WordPress: server-side preview-action descriptors.
4 *
5 * Plugins register action buttons that appear in the right-pane of
6 * any My WordPress section (posts, pages, users, media, plugin-
7 * defined kinds). Server-side declaration buys:
8 *
9 * - Capability gating: an action a viewer can't run is never
10 * shipped to their bundle (no client-side hide-after-render
11 * race).
12 * - Optional script enqueue: the registering plugin can declare a
13 * `script` handle and we'll auto-enqueue it for users who can
14 * see the action.
15 * - One source of truth for both the right-pane action row and
16 * the per-tile context menu — same descriptor.
17 *
18 * Descriptor shape:
19 *
20 * [
21 * 'id' => 'my-plugin/compress-image', // required, unique
22 * 'label' => 'Compress this image', // required
23 * 'icon' => 'dashicons-image-rotate', // optional
24 * 'capability' => 'upload_files', // optional, default 'read'
25 * 'mime' => '^image/', // optional, PCRE
26 * 'sections' => array( 'media' ), // optional, default all
27 * 'script' => 'my-plugin-actions', // optional handle
28 * ]
29 *
30 * `sections` entries match a section's **id** (`'media'`,
31 * `'cpt-atf-forms'` — auto-registered CPT sections are prefixed
32 * `cpt-`), a section's declared **post type slug** (`'atf-forms'`),
33 * or `'*'` for every section. Matching happens client-side.
34 *
35 * Timing: descriptors are re-collected when the window config is
36 * serialized for the browser (see
37 * the explorer app's per-dispatch payload), the same late
38 * pass that enqueues declared `script` handles — registering the
39 * filter any time during a normal bootstrap is fine.
40 *
41 * @package OpenStation
42 */
43
44 defined( 'ABSPATH' ) || exit;
45
46 /**
47 * Collect plugin-registered descriptors, drop ones the current user
48 * can't run, and return the array ready for the window config blob.
49 *
50 * @return array[]
51 */
52 function openstation_my_wordpress_collect_preview_actions() {
53 /**
54 * Filter the list of preview-action descriptors that appear in
55 * the right-pane of any My WordPress section.
56 *
57 * **Status: Experimental** — the descriptor shape may gain
58 * fields. Existing fields (`id`, `label`, `icon`, `capability`,
59 * `mime`, `sections`, `script`) will continue to work.
60 *
61 * @param array[] $actions Default: empty array.
62 */
63 $actions = (array) apply_filters( 'openstation_my_wordpress_preview_actions', array() );
64
65 $out = array();
66 foreach ( $actions as $action ) {
67 if ( ! is_array( $action ) || empty( $action['id'] ) || empty( $action['label'] ) ) {
68 continue;
69 }
70 $cap = isset( $action['capability'] ) && '' !== $action['capability']
71 ? (string) $action['capability']
72 : 'read';
73 if ( ! current_user_can( $cap ) ) {
74 continue;
75 }
76 $descriptor = array(
77 'id' => (string) $action['id'],
78 'label' => (string) $action['label'],
79 );
80 if ( ! empty( $action['icon'] ) ) {
81 $descriptor['icon'] = (string) $action['icon'];
82 }
83 if ( ! empty( $action['mime'] ) ) {
84 $descriptor['mime'] = (string) $action['mime'];
85 }
86 if ( ! empty( $action['sections'] ) && is_array( $action['sections'] ) ) {
87 $descriptor['sections'] = array_values( array_map( 'strval', $action['sections'] ) );
88 }
89 if ( ! empty( $action['script'] ) ) {
90 $descriptor['script'] = (string) $action['script'];
91 }
92 $out[] = $descriptor;
93 }
94 return $out;
95 }
96
97 /**
98 * Enqueue the JS handles declared by visible preview-action
99 * descriptors. Called from the bundle's `admin_enqueue_scripts`
100 * hook so the handlers are wired before the bundle paints.
101 */
102 function openstation_my_wordpress_enqueue_preview_action_scripts() {
103 if ( ! function_exists( 'openstation_my_wordpress_user_can_use' ) || ! openstation_my_wordpress_user_can_use() ) {
104 return;
105 }
106 $actions = openstation_my_wordpress_collect_preview_actions();
107 foreach ( $actions as $action ) {
108 if ( ! empty( $action['script'] ) && wp_script_is( (string) $action['script'], 'registered' ) ) {
109 wp_enqueue_script( (string) $action['script'] );
110 }
111 }
112 }
113 add_action( 'admin_enqueue_scripts', 'openstation_my_wordpress_enqueue_preview_action_scripts', 40 );
114