| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenStation — Recent Comments Widget. |
| 4 |
* |
| 5 |
* Shows a live feed of recent comments with status badges |
| 6 |
* (Approved / Pending / Spam), the commenter name, the post title |
| 7 |
* it belongs to, and a time-ago stamp. Pending count badge |
| 8 |
* on the card header keeps moderators on top of the queue at a glance. |
| 9 |
* |
| 10 |
* Data source: WordPress REST API /wp/v2/comments (logged-in). |
| 11 |
* Refresh: every 60 seconds via setInterval. |
| 12 |
* Requires: OpenStation 0.18.0+ (openstation_register_widget). |
| 13 |
* |
| 14 |
* @package OpenStation |
| 15 |
*/ |
| 16 |
|
| 17 |
defined( 'ABSPATH' ) || exit; |
| 18 |
|
| 19 |
/** |
| 20 |
* Register the JS + CSS assets. |
| 21 |
*/ |
| 22 |
function openstation_register_comments_widget_assets() { |
| 23 |
$suffix = openstation_asset_suffix(); |
| 24 |
$version = defined( 'OPENSTATION_VERSION' ) ? OPENSTATION_VERSION : '0'; |
| 25 |
|
| 26 |
$js_path = OPENSTATION_DIR . 'assets/js/widget-recent-comments' . $suffix . '.js'; |
| 27 |
$css_path = OPENSTATION_DIR . 'assets/js/widget-recent-comments' . $suffix . '.css'; |
| 28 |
|
| 29 |
wp_register_style( |
| 30 |
'os-comments-widget', |
| 31 |
OPENSTATION_URL . 'assets/js/widget-recent-comments' . $suffix . '.css', |
| 32 |
array(), |
| 33 |
file_exists( $css_path ) ? (string) filemtime( $css_path ) : $version |
| 34 |
); |
| 35 |
|
| 36 |
wp_register_script( |
| 37 |
'os-comments-widget', |
| 38 |
OPENSTATION_URL . 'assets/js/widget-recent-comments' . $suffix . '.js', |
| 39 |
array( 'wp-api-fetch' ), |
| 40 |
file_exists( $js_path ) ? (string) filemtime( $js_path ) : $version, |
| 41 |
true |
| 42 |
); |
| 43 |
} |
| 44 |
add_action( 'init', 'openstation_register_comments_widget_assets', 5 ); |
| 45 |
|
| 46 |
/** |
| 47 |
* Eagerly enqueue the CSS on shell pages so there is no flash of |
| 48 |
* unstyled content while the lazy JS bundle loads. |
| 49 |
*/ |
| 50 |
function openstation_enqueue_comments_widget_styles() { |
| 51 |
if ( function_exists( 'openstation_is_enabled' ) && ! openstation_is_enabled() ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
if ( function_exists( 'openstation_is_chromeless_request' ) && openstation_is_chromeless_request() ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
wp_enqueue_style( 'os-comments-widget' ); |
| 58 |
} |
| 59 |
add_action( 'admin_enqueue_scripts', 'openstation_enqueue_comments_widget_styles', 20 ); |
| 60 |
|
| 61 |
/** |
| 62 |
* Register the widget definition. |
| 63 |
*/ |
| 64 |
function openstation_register_comments_widget() { |
| 65 |
if ( ! function_exists( 'openstation_register_widget' ) ) { |
| 66 |
return; |
| 67 |
} |
| 68 |
openstation_register_widget( |
| 69 |
'desktop-mode/recent-comments', |
| 70 |
array( |
| 71 |
'label' => __( 'Recent Comments', 'desktop-mode' ), |
| 72 |
'description' => __( 'Live feed of the latest comments with pending count.', 'desktop-mode' ), |
| 73 |
'icon' => 'dashicons-admin-comments', |
| 74 |
'script' => 'os-comments-widget', |
| 75 |
'movable' => true, |
| 76 |
'resizable' => true, |
| 77 |
'min_width' => 280, |
| 78 |
'min_height' => 220, |
| 79 |
'default_width' => 320, |
| 80 |
'default_height' => 380, |
| 81 |
) |
| 82 |
); |
| 83 |
} |
| 84 |
add_action( 'init', 'openstation_register_comments_widget', 6 ); |
| 85 |
|