/includes/admin/admin.php

  1. <?php 
  2. /** 
  3. * Admin class. 
  4. * 
  5. * @since 6.0.0 
  6. * 
  7. * @package MonsterInsights 
  8. * @subpackage Admin 
  9. * @author Chris Christoff 
  10. */ 
  11.  
  12. // Exit if accessed directly 
  13. if ( ! defined( 'ABSPATH' ) ) { 
  14. exit; 
  15.  
  16. /** 
  17. * Register menu items for MonsterInsights. 
  18. * 
  19. * @since 6.0.0 
  20. * @access public 
  21. * 
  22. * @return void 
  23. */ 
  24. function monsterinsights_admin_menu() { 
  25.  
  26. // Get the base class object. 
  27. $base = MonsterInsights(); 
  28.  
  29. $dashboards_disabled = monsterinsights_get_option( 'dashboards_disabled', false ); 
  30.  
  31. $hook = 'monsterinsights_settings'; 
  32.  
  33. if ( $dashboards_disabled || ( current_user_can( 'monsterinsights_save_settings' ) && ! current_user_can( 'monsterinsights_view_dashboard' ) ) ) { 
  34. // If dashboards disabled, first settings page 
  35. add_menu_page( __( 'Settings:', 'google-analytics-for-wordpress' ), __( 'Insights', 'google-analytics-for-wordpress' ), 'monsterinsights_save_settings', 'monsterinsights_settings', 'monsterinsights_settings_page', plugins_url( 'assets/css/images/menu-icon@2x.png', $base->file ), '100.00013467543' ); 
  36. $hook = 'monsterinsights_settings'; 
  37.  
  38. add_submenu_page( $hook, __( 'Settings:', 'google-analytics-for-wordpress' ), __( 'Settings', 'google-analytics-for-wordpress' ), 'monsterinsights_save_settings', 'monsterinsights_settings' ); 
  39. } else { 
  40. // if dashboards enabled, first dashboard 
  41. add_menu_page( __( 'Dashboard:', 'google-analytics-for-wordpress' ), __( 'Insights', 'google-analytics-for-wordpress' ), 'monsterinsights_view_dashboard', 'monsterinsights_dashboard', 'monsterinsights_dashboard_page', plugins_url( 'assets/css/images/menu-icon@2x.png', $base->file ), '100.00013467543' ); 
  42.  
  43. $hook = 'monsterinsights_dashboard'; 
  44.  
  45. add_submenu_page( $hook, __( 'Dashboard:', 'google-analytics-for-wordpress' ), __( 'Dashboard', 'google-analytics-for-wordpress' ), 'monsterinsights_view_dashboard', 'monsterinsights_dashboard' ); 
  46.  
  47. // then settings page 
  48. add_submenu_page( $hook, __( 'Settings:', 'google-analytics-for-wordpress' ), __( 'Settings', 'google-analytics-for-wordpress' ), 'monsterinsights_save_settings', 'monsterinsights_settings', 'monsterinsights_settings_page' ); 
  49.  
  50. if ( ! $dashboards_disabled ) { 
  51. // then reports 
  52. add_submenu_page( $hook, __( 'Reports:', 'google-analytics-for-wordpress' ), __( 'Reports', 'google-analytics-for-wordpress' ), 'monsterinsights_view_dashboard', 'monsterinsights_reports', 'monsterinsights_reports_page' ); 
  53.  
  54. // then tools 
  55. add_submenu_page( $hook, __( 'Tools:', 'google-analytics-for-wordpress' ), __( 'Tools', 'google-analytics-for-wordpress' ), 'manage_options', 'monsterinsights_tools', 'monsterinsights_tools_page' ); 
  56.  
  57. // then addons 
  58. $network_license = get_site_option( 'monsterinsights_license' ); 
  59. if ( ! monsterinsights_is_network_active() || ( monsterinsights_is_network_active() && empty( $network_license ) ) ) { 
  60. add_submenu_page( $hook, __( 'Addons:', 'google-analytics-for-wordpress' ), '<span style="color:#7cc048"> ' . __( 'Addons', 'google-analytics-for-wordpress' ) . '</span>', 'monsterinsights_save_settings', 'monsterinsights_addons', 'monsterinsights_addons_page' ); 
  61. add_action( 'admin_menu', 'monsterinsights_admin_menu' ); 
  62.  
  63.  
  64.  
  65. function monsterinsights_network_admin_menu() { 
  66. // Get the base class object. 
  67. $base = MonsterInsights(); 
  68.  
  69. // First, let's see if this is an MS network enabled plugin. If it is, we should load the license  
  70. // menu page and the updater on the network panel 
  71. if ( ! function_exists( 'is_plugin_active_for_network' ) ) { 
  72. require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); 
  73.  
  74. $plugin = plugin_basename( MONSTERINSIGHTS_PLUGIN_FILE ); 
  75. if ( ! is_plugin_active_for_network( $plugin ) ) { 
  76. return; 
  77.  
  78. $hook = 'monsterinsights_network'; 
  79. add_menu_page( __( 'Network Settings', 'google-analytics-for-wordpress' ), __( 'Insights', 'google-analytics-for-wordpress' ), 'monsterinsights_save_settings', 'monsterinsights_network', 'monsterinsights_network_page', plugins_url( 'assets/css/images/menu-icon@2x.png', $base->file ), '100.00013467543' ); 
  80.  
  81. add_submenu_page( $hook, __( 'Network Settings', 'google-analytics-for-wordpress' ), __( 'Network Settings', 'google-analytics-for-wordpress' ), 'monsterinsights_save_settings', 'monsterinsights_network', 'monsterinsights_network_page' ); 
  82.  
  83. // then addons 
  84. add_submenu_page( $hook, __( 'Addons:', 'google-analytics-for-wordpress' ), '<span style="color:#7cc048"> ' . __( 'Addons', 'google-analytics-for-wordpress' ) . '</span>', 'monsterinsights_save_settings', 'monsterinsights_addons', 'monsterinsights_addons_page' ); 
  85. add_action( 'network_admin_menu', 'monsterinsights_network_admin_menu', 5 ); 
  86.  
  87. /** 
  88. * Adds one or more classes to the body tag in the dashboard. 
  89. * 
  90. * @param String $classes Current body classes. 
  91. * @return String Altered body classes. 
  92. */ 
  93. function monsterinsights_add_admin_body_class( $classes ) { 
  94. $screen = get_current_screen();  
  95. if ( empty( $screen->id ) || strpos( $screen->id, 'monsterinsights' ) === false ) { 
  96. return $classes; 
  97.  
  98. return "$classes monsterinsights_page "; 
  99. add_filter( 'admin_body_class', 'monsterinsights_add_admin_body_class', 10, 1 ); 
  100.  
  101.  
  102. function monsterinsights_menu_icon_css() { 
  103. // Force correct sizing for retina vs non-retina display 
  104. ?> 
  105. <style type="text/css"> 
  106. #toplevel_page_monsterinsights_dashboard .wp-menu-image img,  
  107. #toplevel_page_monsterinsights_settings .wp-menu-image img,  
  108. #toplevel_page_monsterinsights_network .wp-menu-image img {  
  109. width: 18px;  
  110. height: 18px; 
  111. padding-top: 7px; 
  112. </style> 
  113. <?php 
  114. add_action( 'admin_head', 'monsterinsights_menu_icon_css' ); 
  115.  
  116. /** 
  117. * Add a link to the settings page to the plugins list 
  118. * 
  119. * @param array $links array of links for the plugins, adapted when the current plugin is found. 
  120. * 
  121. * @return array $links 
  122. */ 
  123. function monsterinsights_add_action_links( $links ) { 
  124. $docs = '<a title="MonsterInsights Knowledge Base" href="https://www.monsterinsights.com/docs/">' . esc_html__( 'Documentation', 'google-analytics-for-wordpress' ) . '</a>'; 
  125. array_unshift( $links, $docs ); 
  126.  
  127. // If lite, show a link where they can get pro from 
  128. if ( ! monsterinsights_is_pro_version() ) { 
  129. $get_pro = '<a title="Get MonsterInsights Pro" href="https://www.monsterinsights.com/upgrade-to-pro/">' . esc_html__( 'Get MonsterInsights Pro', 'google-analytics-for-wordpress' ) . '</a>'; 
  130. array_unshift( $links, $get_pro ); 
  131. }  
  132.  
  133. // If Lite, support goes to forum. If pro, it goes to our website 
  134. if ( monsterinsights_is_pro_version() ) { 
  135. $support = '<a title="MonsterInsights Pro Support" href="https://www.monsterinsights.com/my-account/support/">' . esc_html__( 'Support', 'google-analytics-for-wordpress' ) . '</a>'; 
  136. array_unshift( $links, $support ); 
  137. } else { 
  138. $support = '<a title="MonsterInsights Lite Support" href="https://wordpress.org/support/plugin/google-analytics-for-wordpress">' . esc_html__( 'Support', 'google-analytics-for-wordpress' ) . '</a>'; 
  139. array_unshift( $links, $support );  
  140.  
  141. $settings_link = '<a href="' . esc_url( admin_url( 'admin.php?page=monsterinsights_settings' ) ) . '">' . esc_html__( 'Settings', 'google-analytics-for-wordpress' ) . '</a>'; 
  142. array_unshift( $links, $settings_link ); 
  143.  
  144. return $links; 
  145. add_filter( 'plugin_action_links_' . plugin_basename( MONSTERINSIGHTS_PLUGIN_FILE ), 'monsterinsights_add_action_links' ); 
  146.  
  147.  
  148.  
  149. /** 
  150. * Loads a partial view for the Administration screen 
  151. *  
  152. * @access public 
  153. * @since 6.0.0 
  154. * 
  155. * @param string $template PHP file at includes/admin/partials, excluding file extension 
  156. * @param array $data Any data to pass to the view 
  157. * @return void 
  158. */ 
  159. function monsterinsights_load_admin_partial( $template, $data = array() ) { 
  160. // Get the base class object. 
  161. $base = MonsterInsights(); 
  162.  
  163. if ( monsterinsights_is_pro_version() ) { 
  164. $dir = trailingslashit( plugin_dir_path( $base->file ) . 'pro/includes/admin/partials' ); 
  165.  
  166. if ( file_exists( $dir . $template . '.php' ) ) { 
  167. require_once( $dir . $template . '.php' ); 
  168. return true; 
  169. } else { 
  170. $dir = trailingslashit( plugin_dir_path( $base->file ) . 'lite/includes/admin/partials' ); 
  171.  
  172. if ( file_exists( $dir . $template . '.php' ) ) { 
  173. require_once( $dir . $template . '.php' ); 
  174. return true; 
  175. }  
  176.  
  177. $dir = trailingslashit( plugin_dir_path( $base->file ) . 'includes/admin/partials' ); 
  178.  
  179. if ( file_exists( $dir . $template . '.php' ) ) { 
  180. require_once( $dir . $template . '.php' ); 
  181. return true; 
  182.  
  183. return false; 
  184.  
  185.  
  186. /** 
  187. * Outputs the MonsterInsights Header. 
  188. * 
  189. * @since 6.0.0 
  190. */ 
  191. function monsterinsights_admin_header() { 
  192. // Get the base class object. 
  193. $base = MonsterInsights(); 
  194.  
  195. // Get the current screen, and check whether we're viewing a MonsterInsights screen; 
  196. $screen = get_current_screen();  
  197. if ( empty( $screen->id ) || strpos( $screen->id, 'monsterinsights' ) === false ) { 
  198. return; 
  199.  
  200. // If here, we're on an MonsterInsights screen, so output the header. 
  201. monsterinsights_load_admin_partial( 'header', array( 
  202. 'mascot' => plugins_url( 'assets/css/images/mascot.png', $base->file ),  
  203. 'logo' => plugins_url( 'assets/css/images/logo.png', $base->file ),  
  204. '2xmascot' => plugins_url( 'assets/css/images/mascot@2x.png', $base->file ),  
  205. '2xlogo' => plugins_url( 'assets/css/images/logo@2x.png', $base->file ),  
  206. ) ); 
  207. add_action( 'in_admin_header', 'monsterinsights_admin_header', 100 ); 
  208.  
  209. function monsterinsights_welcome_redirect() { 
  210. // Bail if no activation redirect 
  211. if ( ! get_transient( '_monsterinsights_activation_redirect' ) ) { 
  212. return; 
  213.  
  214. // Delete the redirect transient 
  215. delete_transient( '_monsterinsights_activation_redirect' ); 
  216.  
  217. // Bail if activating from network, or bulk 
  218. if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) { 
  219. return; 
  220.  
  221. $upgrade = get_option( 'monsterinsights_version_upgraded_from' ); 
  222. if( ! $upgrade ) { // First time install 
  223. //wp_safe_redirect( admin_url( 'admin.php?page=monsterinsights_settings#monsterinsights-main-tab-general' ) ); exit; 
  224. } else { // Update 
  225. return; 
  226. //wp_safe_redirect( admin_url( 'admin.php?page=monsterinsights_settings#monsterinsights-main-tab-general' ) ); exit; 
  227. //add_action( 'admin_init', 'monsterinsights_welcome_redirect', 11 ); @todo: Investigate 
  228.  
  229. /** 
  230. * When user is on a WPForms related admin page, display footer text 
  231. * that graciously asks them to rate us. 
  232. * 
  233. * @since 6.0.0 
  234. * @param string $text 
  235. * @return string 
  236. */ 
  237. function monsterinsights_admin_footer( $text ) { 
  238. global $current_screen; 
  239. if ( ! empty( $current_screen->id ) && strpos( $current_screen->id, 'monsterinsights' ) !== false ) { 
  240. $url = 'https://wordpress.org/support/view/plugin-reviews/google-analytics-for-wordpress?filter=5'; 
  241. $text = sprintf( esc_html__( 'Please rate %sMonsterInsights%s %s on %sWordPress.org%s to help us spread the word. Thank you from the MonsterInsights team!', 'google-analytics-for-wordpress' ), '<strong>', '</strong>', '<a class="monsterinsights-no-text-decoration" href="' . $url . '" target="_blank" rel="noopener noreferrer">★★★★★</a>', '<a href="' . $url . '" target="_blank" rel="noopener noreferrer">', '</a>' ); 
  242. return $text; 
  243. add_filter( 'admin_footer_text', 'monsterinsights_admin_footer', 1, 2 ); 
.