| 1 |
<?php |
| 2 |
/** |
| 3 |
* Desktop Mode — My WordPress: window + pinned icon registration. |
| 4 |
* |
| 5 |
* Native window with id `desktop-mode-my-wordpress`, opened from a |
| 6 |
* pinned desktop icon that always sits in the top-left of the grid |
| 7 |
* (`pinned: true`, `position: -1`). The bundle renders a two-pane |
| 8 |
* file-explorer UI with breadcrumb navigation: root shows Posts and |
| 9 |
* Pages folder tiles, and clicking either drills into an |
| 10 |
* infinite-scroll list of entities with a rendered HTML preview pane. |
| 11 |
* |
| 12 |
* Filterable surface (mirrors the recycle-bin / posts-window modules): |
| 13 |
* |
| 14 |
* - `desktop_mode_my_wordpress_window_args` |
| 15 |
* - `desktop_mode_my_wordpress_icon_args` |
| 16 |
* - `desktop_mode_my_wordpress_user_can_use` |
| 17 |
* - `desktop_mode_my_wordpress_entities` |
| 18 |
* - `desktop_mode_my_wordpress_template_html` |
| 19 |
* |
| 20 |
* @package WPDesktopMode |
| 21 |
* @since 0.8.0 |
| 22 |
*/ |
| 23 |
|
| 24 |
defined( 'ABSPATH' ) || exit; |
| 25 |
|
| 26 |
/** |
| 27 |
* Whether the current user should see My WordPress. |
| 28 |
* |
| 29 |
* Mirrors the recycle-bin gate — anyone who can edit posts can |
| 30 |
* browse posts and pages. |
| 31 |
* |
| 32 |
* @since 0.8.0 |
| 33 |
* |
| 34 |
* @return bool |
| 35 |
*/ |
| 36 |
function desktop_mode_my_wordpress_user_can_use() { |
| 37 |
$can = current_user_can( 'edit_posts' ); |
| 38 |
|
| 39 |
/** |
| 40 |
* Filter whether the current user can see the My WordPress |
| 41 |
* pinned icon and window. |
| 42 |
* |
| 43 |
* @since 0.8.0 |
| 44 |
* |
| 45 |
* @param bool $can Default: edit_posts capability. |
| 46 |
*/ |
| 47 |
return (bool) apply_filters( 'desktop_mode_my_wordpress_user_can_use', $can ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Build the entity list shipped to the bundle. Posts, Pages, and — |
| 52 |
* since 0.20.0 — Users. Future phases add Comments, Tags, |
| 53 |
* Categories, Themes, and Plugins. |
| 54 |
* |
| 55 |
* The optional `kind` field tells the bundle how to render entries |
| 56 |
* of this entity: `'post'` (default) renders the standard |
| 57 |
* title/excerpt/featured-image tile and the rendered-HTML preview; |
| 58 |
* `'user'` renders an avatar + display-name tile and routes to the |
| 59 |
* user dossier preview. Plugins extending the entity list with a |
| 60 |
* post-shaped collection can omit the field; user-shaped |
| 61 |
* collections must set `'user'`. |
| 62 |
* |
| 63 |
* @since 0.8.0 |
| 64 |
* |
| 65 |
* @return array[] Each entry is `array( 'id', 'label', 'icon', |
| 66 |
* 'restPath', 'kind' )`. `restPath` is appended to |
| 67 |
* the `restRoot` config to derive the list URL. |
| 68 |
*/ |
| 69 |
function desktop_mode_my_wordpress_entities() { |
| 70 |
$entities = array( |
| 71 |
array( |
| 72 |
'id' => 'posts', |
| 73 |
'label' => __( 'Posts', 'desktop-mode' ), |
| 74 |
'icon' => 'dashicons-admin-post', |
| 75 |
'restPath' => 'wp/v2/posts', |
| 76 |
'kind' => 'post', |
| 77 |
), |
| 78 |
array( |
| 79 |
'id' => 'pages', |
| 80 |
'label' => __( 'Pages', 'desktop-mode' ), |
| 81 |
'icon' => 'dashicons-admin-page', |
| 82 |
'restPath' => 'wp/v2/pages', |
| 83 |
'kind' => 'post', |
| 84 |
), |
| 85 |
array( |
| 86 |
'id' => 'users', |
| 87 |
'label' => __( 'Users', 'desktop-mode' ), |
| 88 |
'icon' => 'dashicons-admin-users', |
| 89 |
'restPath' => 'wp/v2/users', |
| 90 |
'kind' => 'user', |
| 91 |
), |
| 92 |
array( |
| 93 |
'id' => 'media', |
| 94 |
'label' => __( 'Media', 'desktop-mode' ), |
| 95 |
'icon' => 'dashicons-admin-media', |
| 96 |
'restPath' => 'wp/v2/media', |
| 97 |
'kind' => 'media', |
| 98 |
), |
| 99 |
); |
| 100 |
|
| 101 |
/** |
| 102 |
* Filter the list of entity types shown inside the My WordPress |
| 103 |
* window. Each entry must declare `id`, `label`, `icon`, and |
| 104 |
* `restPath`. Returning a reordered or extended array shows up |
| 105 |
* in the bundle on the next render. |
| 106 |
* |
| 107 |
* **Status: Experimental** — the entity descriptor shape may |
| 108 |
* gain fields as new entity kinds land (Comments, Tags, |
| 109 |
* Categories, Themes, Plugins). Stable id/label/icon/restPath |
| 110 |
* fields will continue to work; new optional fields will not |
| 111 |
* break existing consumers. The `kind` field is optional and |
| 112 |
* defaults to `'post'` for back-compat. |
| 113 |
* |
| 114 |
* @since 0.8.0 |
| 115 |
* |
| 116 |
* @param array[] $entities Default entities. |
| 117 |
*/ |
| 118 |
$filtered = apply_filters( 'desktop_mode_my_wordpress_entities', $entities ); |
| 119 |
return is_array( $filtered ) ? array_values( $filtered ) : $entities; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Render the My WordPress window's static template body. The bundle |
| 124 |
* mounts its UI into `[data-desktop-mode-my-wordpress-root]`. |
| 125 |
* |
| 126 |
* @since 0.8.0 |
| 127 |
*/ |
| 128 |
function desktop_mode_my_wordpress_render_template() { |
| 129 |
ob_start(); |
| 130 |
?> |
| 131 |
<div class="desktop-mode-my-wordpress" data-desktop-mode-my-wordpress-root> |
| 132 |
<header data-desktop-mode-my-wordpress-breadcrumbs></header> |
| 133 |
<div class="desktop-mode-my-wordpress__body" data-desktop-mode-my-wordpress-body> |
| 134 |
<div class="desktop-mode-my-wordpress__loading" data-desktop-mode-my-wordpress-loading hidden> |
| 135 |
<wpd-spinner></wpd-spinner> |
| 136 |
</div> |
| 137 |
</div> |
| 138 |
<div class="desktop-mode-folder-status-bar" data-desktop-mode-my-wordpress-status></div> |
| 139 |
</div> |
| 140 |
<?php |
| 141 |
$html = (string) ob_get_clean(); |
| 142 |
|
| 143 |
/** |
| 144 |
* Filter the My WordPress window's template HTML. |
| 145 |
* |
| 146 |
* @since 0.8.0 |
| 147 |
* |
| 148 |
* @param string $html Default template HTML. |
| 149 |
*/ |
| 150 |
$filtered = (string) apply_filters( 'desktop_mode_my_wordpress_template_html', $html ); |
| 151 |
|
| 152 |
$allowed_html = function_exists( 'desktop_mode_native_window_allowed_html' ) |
| 153 |
? desktop_mode_native_window_allowed_html() |
| 154 |
: wp_kses_allowed_html( 'post' ); |
| 155 |
|
| 156 |
echo wp_kses( $filtered, $allowed_html ); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Register the native window + the pinned wallpaper icon on `init`, |
| 161 |
* priority 20 — after `components.php` boots the registry. |
| 162 |
* |
| 163 |
* @since 0.8.0 |
| 164 |
*/ |
| 165 |
function desktop_mode_my_wordpress_register_window() { |
| 166 |
if ( ! desktop_mode_my_wordpress_user_can_use() ) { |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
$window_args = array( |
| 171 |
'title' => __( 'My WordPress', 'desktop-mode' ), |
| 172 |
'icon' => 'dashicons-wordpress', |
| 173 |
'template' => 'desktop_mode_my_wordpress_render_template', |
| 174 |
'script' => 'desktop-mode-my-wordpress', |
| 175 |
'style' => 'desktop-mode-my-wordpress', |
| 176 |
'width' => 960, |
| 177 |
'height' => 640, |
| 178 |
'min_width' => 640, |
| 179 |
'min_height' => 420, |
| 180 |
'placement' => 'none', |
| 181 |
'config' => array( |
| 182 |
'restRoot' => esc_url_raw( rest_url() ), |
| 183 |
'restNonce' => wp_create_nonce( 'wp_rest' ), |
| 184 |
'editPostUrlBase' => esc_url_raw( admin_url( 'post.php' ) ), |
| 185 |
'editUserUrlBase' => esc_url_raw( admin_url( 'user-edit.php' ) ), |
| 186 |
'entities' => desktop_mode_my_wordpress_entities(), |
| 187 |
'perPage' => 24, |
| 188 |
'mediaPerPage' => 48, |
| 189 |
'previewActions' => function_exists( 'desktop_mode_my_wordpress_collect_preview_actions' ) |
| 190 |
? desktop_mode_my_wordpress_collect_preview_actions() |
| 191 |
: array(), |
| 192 |
), |
| 193 |
); |
| 194 |
|
| 195 |
/** |
| 196 |
* Filter the args used to register the My WordPress native window. |
| 197 |
* |
| 198 |
* @since 0.8.0 |
| 199 |
* |
| 200 |
* @param array $window_args Args passed to `desktop_mode_register_window()`. |
| 201 |
*/ |
| 202 |
$window_args = (array) apply_filters( 'desktop_mode_my_wordpress_window_args', $window_args ); |
| 203 |
|
| 204 |
$registered = desktop_mode_register_window( 'desktop-mode-my-wordpress', $window_args ); |
| 205 |
if ( is_wp_error( $registered ) ) { |
| 206 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 207 |
error_log( '[desktop-mode] My WordPress window registration failed: ' . $registered->get_error_message() ); |
| 208 |
return; |
| 209 |
} |
| 210 |
|
| 211 |
$icon_args = array( |
| 212 |
'title' => __( 'My WordPress', 'desktop-mode' ), |
| 213 |
'icon' => 'dashicons-wordpress', |
| 214 |
'window' => 'desktop-mode-my-wordpress', |
| 215 |
'pinned' => true, |
| 216 |
'position' => -1, |
| 217 |
); |
| 218 |
|
| 219 |
/** |
| 220 |
* Filter the args used to register the My WordPress pinned icon. |
| 221 |
* |
| 222 |
* Removing `pinned` here lets the icon participate in normal |
| 223 |
* sort order — useful for sites that want the shortcut to feel |
| 224 |
* like any other plugin icon. |
| 225 |
* |
| 226 |
* @since 0.8.0 |
| 227 |
* |
| 228 |
* @param array $icon_args Args passed to `desktop_mode_register_icon()`. |
| 229 |
*/ |
| 230 |
$icon_args = (array) apply_filters( 'desktop_mode_my_wordpress_icon_args', $icon_args ); |
| 231 |
|
| 232 |
desktop_mode_register_icon( 'desktop-mode-my-wordpress', $icon_args ); |
| 233 |
} |
| 234 |
add_action( 'init', 'desktop_mode_my_wordpress_register_window', 20 ); |
| 235 |
|
| 236 |
/** |
| 237 |
* Enqueue the bundle's CSS in admin context. The script is lazy- |
| 238 |
* loaded by the native-window sync and so does not need an |
| 239 |
* `admin_enqueue_scripts` call. |
| 240 |
* |
| 241 |
* @since 0.8.0 |
| 242 |
*/ |
| 243 |
function desktop_mode_my_wordpress_enqueue_styles() { |
| 244 |
if ( ! desktop_mode_my_wordpress_user_can_use() ) { |
| 245 |
return; |
| 246 |
} |
| 247 |
wp_enqueue_style( 'desktop-mode-my-wordpress' ); |
| 248 |
} |
| 249 |
add_action( 'admin_enqueue_scripts', 'desktop_mode_my_wordpress_enqueue_styles', 30 ); |
| 250 |
|