| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenStation — Jazz Quote Widget. |
| 4 |
* |
| 5 |
* A love letter to WordPress's jazz musician release naming tradition. |
| 6 |
* Shows the current WP version, its jazz musician codename, and a |
| 7 |
* rotating daily quote from that musician. |
| 8 |
* |
| 9 |
* Requires: OpenStation 0.18.0+ (openstation_register_widget). |
| 10 |
* |
| 11 |
* @package OpenStation |
| 12 |
*/ |
| 13 |
|
| 14 |
defined( 'ABSPATH' ) || exit; |
| 15 |
|
| 16 |
/** |
| 17 |
* Register JS + CSS assets. |
| 18 |
*/ |
| 19 |
function openstation_register_jazz_quote_widget_assets() { |
| 20 |
$suffix = openstation_asset_suffix(); |
| 21 |
$version = defined( 'OPENSTATION_VERSION' ) ? OPENSTATION_VERSION : '0'; |
| 22 |
|
| 23 |
$js_path = OPENSTATION_DIR . 'assets/js/widget-jazz-quote' . $suffix . '.js'; |
| 24 |
$css_path = OPENSTATION_DIR . 'assets/js/widget-jazz-quote' . $suffix . '.css'; |
| 25 |
|
| 26 |
wp_register_style( |
| 27 |
'os-jazz-quote-widget', |
| 28 |
OPENSTATION_URL . 'assets/js/widget-jazz-quote' . $suffix . '.css', |
| 29 |
array(), |
| 30 |
file_exists( $css_path ) ? (string) filemtime( $css_path ) : $version |
| 31 |
); |
| 32 |
|
| 33 |
wp_register_script( |
| 34 |
'os-jazz-quote-widget', |
| 35 |
OPENSTATION_URL . 'assets/js/widget-jazz-quote' . $suffix . '.js', |
| 36 |
array(), |
| 37 |
file_exists( $js_path ) ? (string) filemtime( $js_path ) : $version, |
| 38 |
true |
| 39 |
); |
| 40 |
} |
| 41 |
add_action( 'init', 'openstation_register_jazz_quote_widget_assets', 5 ); |
| 42 |
|
| 43 |
/** |
| 44 |
* Inline the WordPress version on the MAIN desktop shell script. |
| 45 |
* |
| 46 |
* Note that wp_add_inline_script() only outputs when the attached handle is |
| 47 |
* actually enqueued. The widget JS loads lazily via the shell's |
| 48 |
* server-sync, so attaching the inline script to the widget handle |
| 49 |
* would mean it never appears. Instead we attach it to the main |
| 50 |
* desktop handle which is always enqueued on shell pages. |
| 51 |
* |
| 52 |
* window.openStationJazzQuote is therefore available from page load, |
| 53 |
* before the widget bundle is ever fetched. |
| 54 |
*/ |
| 55 |
function openstation_jazz_quote_inline_version() { |
| 56 |
if ( ! openstation_is_enabled() ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
if ( openstation_is_chromeless_request() ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
$main_handle = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) |
| 63 |
? 'openstation' |
| 64 |
: 'openstation'; |
| 65 |
|
| 66 |
wp_add_inline_script( |
| 67 |
$main_handle, |
| 68 |
'window.openStationJazzQuote = { wpVersion: ' . wp_json_encode( get_bloginfo( 'version' ) ) . ' };', |
| 69 |
'before' |
| 70 |
); |
| 71 |
} |
| 72 |
add_action( 'admin_enqueue_scripts', 'openstation_jazz_quote_inline_version', 15 ); |
| 73 |
|
| 74 |
/** |
| 75 |
* Eagerly enqueue the CSS on shell pages. |
| 76 |
*/ |
| 77 |
function openstation_enqueue_jazz_quote_widget_styles() { |
| 78 |
if ( function_exists( 'openstation_is_enabled' ) && ! openstation_is_enabled() ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
if ( function_exists( 'openstation_is_chromeless_request' ) && openstation_is_chromeless_request() ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
wp_enqueue_style( 'os-jazz-quote-widget' ); |
| 85 |
} |
| 86 |
add_action( 'admin_enqueue_scripts', 'openstation_enqueue_jazz_quote_widget_styles', 20 ); |
| 87 |
|
| 88 |
/** |
| 89 |
* Register the widget definition. |
| 90 |
*/ |
| 91 |
function openstation_register_jazz_quote_widget() { |
| 92 |
if ( ! function_exists( 'openstation_register_widget' ) ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
openstation_register_widget( |
| 96 |
'desktop-mode/jazz-quote', |
| 97 |
array( |
| 98 |
'label' => __( 'Jazz Quote', 'desktop-mode' ), |
| 99 |
'description' => __( 'A daily quote from the jazz musician behind your WordPress version.', 'desktop-mode' ), |
| 100 |
'icon' => 'dashicons-format-audio', |
| 101 |
'script' => 'os-jazz-quote-widget', |
| 102 |
'movable' => true, |
| 103 |
'resizable' => true, |
| 104 |
'min_width' => 220, |
| 105 |
'min_height' => 160, |
| 106 |
'default_width' => 300, |
| 107 |
'default_height' => 220, |
| 108 |
) |
| 109 |
); |
| 110 |
} |
| 111 |
add_action( 'init', 'openstation_register_jazz_quote_widget', 6 ); |
| 112 |
|