dashboard-widget.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * Manage the MonsterInsights Dashboard Widget
  4. *
  5. * @since 7.1
  6. *
  7. * @package MonsterInsights
  8. */
  9. // Exit if accessed directly.
  10. if ( ! defined( 'ABSPATH' ) ) {
  11. exit;
  12. }
  13. /**
  14. * Class MonsterInsights_Dashboard_Widget
  15. */
  16. class MonsterInsights_Dashboard_Widget {
  17. const WIDGET_KEY = 'monsterinsights_reports_widget';
  18. /**
  19. * The widget options.
  20. *
  21. * @var array $options
  22. */
  23. public $options;
  24. /**
  25. * MonsterInsights_Dashboard_Widget constructor.
  26. */
  27. public function __construct() {
  28. // Allow dashboard widget to be hidden on multisite installs
  29. $show_widget = is_multisite() ? apply_filters( 'monsterinsights_show_dashboard_widget', true ) : true;
  30. if ( ! $show_widget ) {
  31. return false;
  32. }
  33. // Check if reports should be visible.
  34. $dashboards_disabled = monsterinsights_get_option( 'dashboards_disabled', false );
  35. if ( ! current_user_can( 'monsterinsights_view_dashboard' ) || $dashboards_disabled ) {
  36. return false;
  37. }
  38. add_action( 'wp_dashboard_setup', array( $this, 'register_dashboard_widget' ) );
  39. add_action( 'admin_enqueue_scripts', array( $this, 'widget_scripts' ) );
  40. }
  41. /**
  42. * Register the dashboard widget.
  43. */
  44. public function register_dashboard_widget() {
  45. global $wp_meta_boxes;
  46. wp_add_dashboard_widget(
  47. self::WIDGET_KEY,
  48. esc_html__( 'MonsterInsights', 'google-analytics-for-wordpress' ),
  49. array( $this, 'dashboard_widget_content' )
  50. );
  51. // Attept to place the widget at the top.
  52. $normal_dashboard = $wp_meta_boxes['dashboard']['normal']['core'];
  53. $widget_instance = array( self::WIDGET_KEY => $normal_dashboard[ self::WIDGET_KEY ] );
  54. unset( $normal_dashboard[ self::WIDGET_KEY ] );
  55. $sorted_dashboard = array_merge( $widget_instance, $normal_dashboard );
  56. $wp_meta_boxes['dashboard']['normal']['core'] = $sorted_dashboard;
  57. }
  58. /**
  59. * Load the widget content.
  60. */
  61. public function dashboard_widget_content() {
  62. $is_authed = ( MonsterInsights()->auth->is_authed() || MonsterInsights()->auth->is_network_authed() );
  63. if ( ! $is_authed ) {
  64. $this->widget_content_no_auth();
  65. } else {
  66. $datepicker_options = array( 30, 7 );
  67. $url = is_network_admin() ? network_admin_url( 'admin.php?page=monsterinsights_reports' ) : admin_url( 'admin.php?page=monsterinsights_reports' );
  68. ?>
  69. <div class="mi-dw-controls">
  70. <div class="mi-dw-datepicker mi-dw-btn-group" data-type="datepicker">
  71. <button class="mi-dw-btn-group-label">
  72. <?php
  73. // Translators: %d is the number of days.
  74. printf( esc_html__( 'Last %d days', 'google-analytics-for-wordpress' ), 30 );
  75. ?>
  76. </button>
  77. <div class="mi-dw-btn-list">
  78. <?php foreach ( $datepicker_options as $datepicker_option ) { ?>
  79. <button class="mi-dw-btn <?php echo 30 === $datepicker_option ? 'selected' : ''; ?>" data-value=" <?php echo esc_attr( $datepicker_option ); ?>">
  80. <?php
  81. // Translators: %d is the number of days.
  82. printf( esc_html__( 'Last %d days', 'google-analytics-for-wordpress' ), esc_attr( $datepicker_option ) );
  83. ?>
  84. </button>
  85. <?php } ?>
  86. </div>
  87. </div>
  88. <label class="mi-dw-styled-toggle mi-dw-widget-width-toggle-container" title="<?php esc_attr_e( 'Show in full-width mode', 'google-analytics-for-wordpress' ); ?>">
  89. <input type="checkbox" class="mi-dw-widget-width-toggle"/>
  90. </label>
  91. <div class="mi-dw-dropdown">
  92. <button class="mi-dw-button-cog mi-dw-dropdown-toggle" data-target="#mi-dw-reports-options" type="button"></button>
  93. <ul class="mi-dw-reports-options" id="mi-dw-reports-options"></ul>
  94. </div>
  95. <img class="mi-dw-mascot" src="<?php echo esc_url( plugins_url( 'assets/css/images/mascot.png', MONSTERINSIGHTS_PLUGIN_FILE ) ); ?>" srcset="<?php echo esc_url( plugins_url( 'assets/css/images/mascot@2x.png', MONSTERINSIGHTS_PLUGIN_FILE ) ); ?> 2x"/>
  96. </div>
  97. <div class="mi-dw-lite">
  98. <div class="mi-dw-lite-content">
  99. <h2><?php esc_html_e( 'View All Analytics on the WordPress Dashboard', 'google-analytics-for-wordpress' ); ?></h2>
  100. <p><?php esc_html_e( 'Once you upgrade to MonsterInsights Pro, you can see your analytics on the Dashboard', 'google-analytics-for-wordpress' ); ?></p>
  101. <a href="<?php echo esc_url( monsterinsights_get_upgrade_link( 'dashboard-widget', 'lite-cta' ) ); ?>" target="_blank" class="mi-dw-btn-large"><?php esc_html_e( 'Get MonsterInsights Pro', 'google-analytics-for-wordpress' ); ?></a>
  102. <br/>
  103. <a href="<?php echo esc_url( $url ); ?>"><?php esc_html_e( 'Go to MonsterInsights Reports', 'google-analytics-for-wordpress' ); ?></a>
  104. </div>
  105. </div>
  106. <?php
  107. }
  108. }
  109. /**
  110. * Message to display when the plugin is not authenticated.
  111. */
  112. public function widget_content_no_auth() {
  113. $url = is_network_admin() ? network_admin_url( 'admin.php?page=monsterinsights_settings' ) : admin_url( 'admin.php?page=monsterinsights_settings' );
  114. ?>
  115. <div class="mi-dw-not-authed">
  116. <h2><?php esc_html_e( 'Reports are not available', 'google-analytics-for-wordpress' ); ?></h2>
  117. <p><?php esc_html_e( 'Please connect MonsterInsights to Google Analytics to see reports.', 'google-analytics-for-wordpress' ); ?></p>
  118. <a href="<?php echo esc_url( $url ); ?>" class="mi-dw-btn-large"><?php esc_html_e( 'Configure MonsterInsights', 'google-analytics-for-wordpress' ); ?></a>
  119. </div>
  120. <?php
  121. }
  122. /**
  123. * Load widget-specific scripts.
  124. */
  125. public function widget_scripts() {
  126. $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
  127. $screen = get_current_screen();
  128. if ( isset( $screen->id ) && 'dashboard' === $screen->id ) {
  129. wp_enqueue_style( 'monsterinsights-dashboard-widget-styles', plugins_url( 'lite/assets/css/admin-dashboard-widget' . $suffix . '.css', MONSTERINSIGHTS_PLUGIN_FILE ), array(), monsterinsights_get_asset_version() );
  130. wp_enqueue_script( 'jquery-ui-tooltip' );
  131. wp_enqueue_script( 'monsterinsights-dashboard-widget', plugins_url( 'lite/assets/js/admin-dashboard-widget' . $suffix . '.js', MONSTERINSIGHTS_PLUGIN_FILE ), array( 'jquery' ), monsterinsights_get_asset_version(), true );
  132. }
  133. }
  134. }