| 1 |
<?php |
| 2 |
/** |
| 3 |
* Desktop Mode — Post Stats Widget. |
| 4 |
* |
| 5 |
* Bar chart of posts published per month for the last 6 months, |
| 6 |
* broken down by published / draft / pending status. |
| 7 |
* |
| 8 |
* Refresh: every 5 minutes. |
| 9 |
* Requires: Desktop Mode 0.18.0+ (desktop_mode_register_widget). |
| 10 |
* |
| 11 |
* @package WPDesktopMode |
| 12 |
* @since 0.26.0 |
| 13 |
*/ |
| 14 |
|
| 15 |
defined( 'ABSPATH' ) || exit; |
| 16 |
|
| 17 |
/** |
| 18 |
* Register the JS + CSS assets. |
| 19 |
* |
| 20 |
* @since 0.26.0 |
| 21 |
*/ |
| 22 |
function desktop_mode_register_post_stats_widget_assets() { |
| 23 |
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
| 24 |
$version = defined( 'DESKTOP_MODE_VERSION' ) ? DESKTOP_MODE_VERSION : '0'; |
| 25 |
|
| 26 |
$js_path = DESKTOP_MODE_DIR . 'assets/js/widget-post-stats' . $suffix . '.js'; |
| 27 |
$css_path = DESKTOP_MODE_DIR . 'assets/js/widget-post-stats' . $suffix . '.css'; |
| 28 |
|
| 29 |
wp_register_style( |
| 30 |
'desktop-mode-post-stats-widget', |
| 31 |
DESKTOP_MODE_URL . 'assets/js/widget-post-stats' . $suffix . '.css', |
| 32 |
array(), |
| 33 |
file_exists( $css_path ) ? (string) filemtime( $css_path ) : $version |
| 34 |
); |
| 35 |
|
| 36 |
wp_register_script( |
| 37 |
'desktop-mode-post-stats-widget', |
| 38 |
DESKTOP_MODE_URL . 'assets/js/widget-post-stats' . $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', 'desktop_mode_register_post_stats_widget_assets', 5 ); |
| 45 |
|
| 46 |
/** |
| 47 |
* Eagerly enqueue the CSS on shell pages. |
| 48 |
* |
| 49 |
* @since 0.26.0 |
| 50 |
*/ |
| 51 |
function desktop_mode_enqueue_post_stats_widget_styles() { |
| 52 |
if ( function_exists( 'desktop_mode_is_enabled' ) && ! desktop_mode_is_enabled() ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
if ( function_exists( 'desktop_mode_is_chromeless_request' ) && desktop_mode_is_chromeless_request() ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
wp_enqueue_style( 'desktop-mode-post-stats-widget' ); |
| 59 |
} |
| 60 |
add_action( 'admin_enqueue_scripts', 'desktop_mode_enqueue_post_stats_widget_styles', 20 ); |
| 61 |
|
| 62 |
/** |
| 63 |
* Register the widget definition. |
| 64 |
* |
| 65 |
* @since 0.26.0 |
| 66 |
*/ |
| 67 |
function desktop_mode_register_post_stats_widget() { |
| 68 |
if ( ! function_exists( 'desktop_mode_register_widget' ) ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
desktop_mode_register_widget( |
| 72 |
'desktop-mode/post-stats', |
| 73 |
array( |
| 74 |
'label' => __( 'Post Stats', 'desktop-mode' ), |
| 75 |
'description' => __( 'Bar chart of posts per month over the last 6 months.', 'desktop-mode' ), |
| 76 |
'icon' => 'dashicons-chart-bar', |
| 77 |
'script' => 'desktop-mode-post-stats-widget', |
| 78 |
'movable' => true, |
| 79 |
'resizable' => true, |
| 80 |
'min_width' => 260, |
| 81 |
'min_height' => 200, |
| 82 |
'default_width' => 320, |
| 83 |
'default_height' => 260, |
| 84 |
) |
| 85 |
); |
| 86 |
} |
| 87 |
add_action( 'init', 'desktop_mode_register_post_stats_widget', 6 ); |
| 88 |
|