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