PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 0.8.7
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v0.8.7
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 0.8.7, at includes/my-wordpress/preview-actions.php

110 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 * Desktop Mode — 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 * @package WPDesktopMode
31 * @since 0.21.0
32 */
33
34 defined( 'ABSPATH' ) || exit;
35
36 /**
37 * Collect plugin-registered descriptors, drop ones the current user
38 * can't run, and return the array ready for the window config blob.
39 *
40 * @since 0.21.0
41 *
42 * @return array[]
43 */
44 function desktop_mode_my_wordpress_collect_preview_actions() {
45 /**
46 * Filter the list of preview-action descriptors that appear in
47 * the right-pane of any My WordPress section.
48 *
49 * **Status: Experimental** — the descriptor shape may gain
50 * fields. Existing fields (`id`, `label`, `icon`, `capability`,
51 * `mime`, `sections`, `script`) will continue to work.
52 *
53 * @since 0.21.0
54 *
55 * @param array[] $actions Default: empty array.
56 */
57 $actions = (array) apply_filters( 'desktop_mode_my_wordpress_preview_actions', array() );
58
59 $out = array();
60 foreach ( $actions as $action ) {
61 if ( ! is_array( $action ) || empty( $action['id'] ) || empty( $action['label'] ) ) {
62 continue;
63 }
64 $cap = isset( $action['capability'] ) && '' !== $action['capability']
65 ? (string) $action['capability']
66 : 'read';
67 if ( ! current_user_can( $cap ) ) {
68 continue;
69 }
70 $descriptor = array(
71 'id' => (string) $action['id'],
72 'label' => (string) $action['label'],
73 );
74 if ( ! empty( $action['icon'] ) ) {
75 $descriptor['icon'] = (string) $action['icon'];
76 }
77 if ( ! empty( $action['mime'] ) ) {
78 $descriptor['mime'] = (string) $action['mime'];
79 }
80 if ( ! empty( $action['sections'] ) && is_array( $action['sections'] ) ) {
81 $descriptor['sections'] = array_values( array_map( 'strval', $action['sections'] ) );
82 }
83 if ( ! empty( $action['script'] ) ) {
84 $descriptor['script'] = (string) $action['script'];
85 }
86 $out[] = $descriptor;
87 }
88 return $out;
89 }
90
91 /**
92 * Enqueue the JS handles declared by visible preview-action
93 * descriptors. Called from the bundle's `admin_enqueue_scripts`
94 * hook so the handlers are wired before the bundle paints.
95 *
96 * @since 0.21.0
97 */
98 function desktop_mode_my_wordpress_enqueue_preview_action_scripts() {
99 if ( ! function_exists( 'desktop_mode_my_wordpress_user_can_use' ) || ! desktop_mode_my_wordpress_user_can_use() ) {
100 return;
101 }
102 $actions = desktop_mode_my_wordpress_collect_preview_actions();
103 foreach ( $actions as $action ) {
104 if ( ! empty( $action['script'] ) && wp_script_is( (string) $action['script'], 'registered' ) ) {
105 wp_enqueue_script( (string) $action['script'] );
106 }
107 }
108 }
109 add_action( 'admin_enqueue_scripts', 'desktop_mode_my_wordpress_enqueue_preview_action_scripts', 40 );
110