| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenStation — Note Pad widget PHP registration. |
| 4 |
* |
| 5 |
* The composer surface for pinned notes: a pad of pastel paper on the |
| 6 |
* widget column. The user writes on the top sheet and drags it out of |
| 7 |
* the pad onto the wallpaper, where it becomes a pinned `wpd_note` |
| 8 |
* (see `includes/notes/bootstrap.php` for the data layer). |
| 9 |
* |
| 10 |
* Same registration shape as every widget (template: |
| 11 |
* `includes/widgets/widget-starter.php`): register the script + style |
| 12 |
* handles at `init@5`, announce the widget at `init@6`, eagerly |
| 13 |
* enqueue only the CSS on shell pages. |
| 14 |
* |
| 15 |
* @package OpenStation |
| 16 |
*/ |
| 17 |
|
| 18 |
defined( 'ABSPATH' ) || exit; |
| 19 |
|
| 20 |
/** |
| 21 |
* Register the JS bundle and CSS stylesheet handles. |
| 22 |
*/ |
| 23 |
function openstation_register_notes_widget_assets() { |
| 24 |
$suffix = openstation_asset_suffix(); |
| 25 |
$version = defined( 'OPENSTATION_VERSION' ) ? OPENSTATION_VERSION : '0'; |
| 26 |
|
| 27 |
$js_path = OPENSTATION_DIR . 'assets/js/widget-notes' . $suffix . '.js'; |
| 28 |
$css_path = OPENSTATION_DIR . 'assets/js/widget-notes' . $suffix . '.css'; |
| 29 |
|
| 30 |
// Depends on `os-files` because the pad's sheets wear the |
| 31 |
// canonical `.os-file-tile` chrome while being dragged — anything |
| 32 |
// that restyles a tile has to print after the file that declares |
| 33 |
// one, whatever order the two handles were enqueued in. |
| 34 |
wp_register_style( |
| 35 |
'os-notes-widget', |
| 36 |
OPENSTATION_URL . 'assets/js/widget-notes' . $suffix . '.css', |
| 37 |
array( 'os-files' ), |
| 38 |
file_exists( $css_path ) ? (string) filemtime( $css_path ) : $version |
| 39 |
); |
| 40 |
|
| 41 |
wp_register_script( |
| 42 |
'os-notes-widget', |
| 43 |
OPENSTATION_URL . 'assets/js/widget-notes' . $suffix . '.js', |
| 44 |
array(), |
| 45 |
file_exists( $js_path ) ? (string) filemtime( $js_path ) : $version, |
| 46 |
true |
| 47 |
); |
| 48 |
} |
| 49 |
add_action( 'init', 'openstation_register_notes_widget_assets', 5 ); |
| 50 |
|
| 51 |
/** |
| 52 |
* Eagerly enqueue the CSS on OpenStation shell pages. |
| 53 |
* |
| 54 |
* The JS loads lazily (widget server-sync); the CSS must be present |
| 55 |
* before first mount to avoid a flash of unstyled pad. |
| 56 |
*/ |
| 57 |
function openstation_enqueue_notes_widget_styles() { |
| 58 |
if ( function_exists( 'openstation_is_enabled' ) && ! openstation_is_enabled() ) { |
| 59 |
return; |
| 60 |
} |
| 61 |
if ( function_exists( 'openstation_is_chromeless_request' ) && openstation_is_chromeless_request() ) { |
| 62 |
return; |
| 63 |
} |
| 64 |
wp_enqueue_style( 'os-notes-widget' ); |
| 65 |
} |
| 66 |
add_action( 'admin_enqueue_scripts', 'openstation_enqueue_notes_widget_styles', 20 ); |
| 67 |
|
| 68 |
/** |
| 69 |
* Announce the widget to OpenStation. |
| 70 |
*/ |
| 71 |
function openstation_register_notes_widget() { |
| 72 |
if ( ! function_exists( 'openstation_register_widget' ) ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
openstation_register_widget( |
| 77 |
'desktop-mode/notes', |
| 78 |
array( |
| 79 |
'label' => __( 'Note Pad', 'desktop-mode' ), |
| 80 |
'description' => __( 'Write a note and drag it onto the desktop to pin it.', 'desktop-mode' ), |
| 81 |
'icon' => 'dashicons-sticky', |
| 82 |
'script' => 'os-notes-widget', |
| 83 |
'movable' => true, |
| 84 |
'resizable' => true, |
| 85 |
'min_width' => 240, |
| 86 |
'min_height' => 300, |
| 87 |
'default_width' => 300, |
| 88 |
'default_height' => 360, |
| 89 |
) |
| 90 |
); |
| 91 |
} |
| 92 |
add_action( 'init', 'openstation_register_notes_widget', 6 ); |
| 93 |
|