/includes/admin/common.php

  1. <?php 
  2. /** 
  3. * Common admin class. 
  4. * 
  5. * @since 6.0.0 
  6. * 
  7. * @package MonsterInsights 
  8. * @subpackage Common 
  9. * @author Chris Christoff 
  10. */ 
  11.  
  12. // Exit if accessed directly 
  13. if ( ! defined( 'ABSPATH' ) ) { 
  14. exit; 
  15.  
  16. /** 
  17. * Loads styles for all MonsterInsights-based Administration Screens. 
  18. * 
  19. * @since 6.0.0 
  20. * @access public 
  21. * 
  22. * @return null Return early if not on the proper screen. 
  23. */ 
  24. function monsterinsights_admin_styles() { 
  25.  
  26. // Get current screen. 
  27. $screen = get_current_screen(); 
  28.  
  29. // Bail if we're not on a MonsterInsights screen. 
  30. if ( empty( $screen->id ) || strpos( $screen->id, 'monsterinsights' ) === false ) { 
  31. return; 
  32.  
  33. // Maps 
  34. wp_register_style( MONSTERINSIGHTS_PLUGIN_SLUG . '-jvectormap-style', plugins_url( 'assets/css/jvectormap/jquery-jvectormap-2.0.3.css', MONSTERINSIGHTS_PLUGIN_FILE ), array(), monsterinsights_get_asset_version() ); 
  35. wp_enqueue_style( MONSTERINSIGHTS_PLUGIN_SLUG . '-jvectormap-style' ); 
  36.  
  37. // FontAwesome (used for message boxes) 
  38. wp_register_style( MONSTERINSIGHTS_PLUGIN_SLUG . '-font-awesome', plugins_url( 'assets/css/font-awesome/font-awesome.min.css', MONSTERINSIGHTS_PLUGIN_FILE ), array(), monsterinsights_get_asset_version() ); 
  39. wp_enqueue_style( MONSTERINSIGHTS_PLUGIN_SLUG . '-font-awesome' ); 
  40.  
  41. // Select300 
  42. wp_register_style( MONSTERINSIGHTS_PLUGIN_SLUG . '-select300-style', plugins_url( 'assets/css/select300/select300.css', MONSTERINSIGHTS_PLUGIN_FILE ), array(), monsterinsights_get_asset_version() ); 
  43. wp_enqueue_style( MONSTERINSIGHTS_PLUGIN_SLUG . '-select300-style' ); 
  44.  
  45. // Tooltips 
  46. wp_enqueue_script( 'jquery-ui-tooltip' ); 
  47.  
  48. // Load necessary admin styles. 
  49. $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; 
  50. if ( ! file_exists( MONSTERINSIGHTS_PLUGIN_DIR . 'assets/css/admin.min.css' ) ) { 
  51. $suffix = ''; 
  52. wp_register_style( MONSTERINSIGHTS_PLUGIN_SLUG . '-admin-style', plugins_url( 'assets/css/admin' . $suffix . '.css', MONSTERINSIGHTS_PLUGIN_FILE ), array(), monsterinsights_get_asset_version() ); 
  53. wp_enqueue_style( MONSTERINSIGHTS_PLUGIN_SLUG . '-admin-style' ); 
  54.  
  55. // See if there's an admin.css file for this plugin version 
  56. if ( file_exists( MONSTERINSIGHTS_PLUGIN_DIR . 'pro/assets/css/admin.css' ) ) { 
  57. wp_register_style( MONSTERINSIGHTS_PLUGIN_SLUG . '-pro-admin-style', plugins_url( 'pro/assets/css/admin.css', MONSTERINSIGHTS_PLUGIN_FILE ), array(), monsterinsights_get_asset_version() ); 
  58. wp_enqueue_style( MONSTERINSIGHTS_PLUGIN_SLUG . '-pro-admin-style' ); 
  59. } else if ( file_exists( MONSTERINSIGHTS_PLUGIN_DIR . 'lite/assets/css/admin.css' ) ) { 
  60. wp_register_style( MONSTERINSIGHTS_PLUGIN_SLUG . '-only-admin-style', plugins_url( 'lite/assets/css/admin.css', MONSTERINSIGHTS_PLUGIN_FILE ), array(), monsterinsights_get_asset_version() ); 
  61. wp_enqueue_style( MONSTERINSIGHTS_PLUGIN_SLUG . '-only-admin-style' ); 
  62. add_action( 'admin_enqueue_scripts', 'monsterinsights_admin_styles' ); 
  63.  
  64. /** 
  65. * Loads scripts for all MonsterInsights-based Administration Screens. 
  66. * 
  67. * @since 6.0.0 
  68. * @access public 
  69. * 
  70. * @return null Return early if not on the proper screen. 
  71. */ 
  72. function monsterinsights_admin_scripts() { 
  73.  
  74. // Get current screen. 
  75. $screen = get_current_screen(); 
  76.  
  77. // Bail if we're not on a MonsterInsights screen. 
  78. if ( empty( $screen->id ) || strpos( $screen->id, 'monsterinsights' ) === false ) { 
  79. return; 
  80.  
  81. // Get the base class object. 
  82. $base = MonsterInsights(); 
  83.  
  84. // Load necessary admin scripts 
  85. // Clipboard.js 
  86. wp_register_script( MONSTERINSIGHTS_PLUGIN_SLUG . '-clipboard-script', plugins_url( 'assets/js/clipboard/clipboard.js', MONSTERINSIGHTS_PLUGIN_FILE ), array( 'jquery' ), monsterinsights_get_asset_version(), true ); 
  87. wp_enqueue_script( MONSTERINSIGHTS_PLUGIN_SLUG . '-clipboard-script' ); 
  88.  
  89. // List.js 
  90. wp_register_script( MONSTERINSIGHTS_PLUGIN_SLUG . '-list-script', plugins_url( 'assets/js/list/list.js', MONSTERINSIGHTS_PLUGIN_FILE ), array( 'jquery' ), monsterinsights_get_asset_version(), true ); 
  91. wp_enqueue_script( MONSTERINSIGHTS_PLUGIN_SLUG . '-list-script' ); 
  92.  
  93. // Charts.js 
  94. wp_register_script( MONSTERINSIGHTS_PLUGIN_SLUG . '-chartjs-script', plugins_url( 'assets/js/chartjs/Chart.bundle.js', MONSTERINSIGHTS_PLUGIN_FILE ), array( 'jquery' ), monsterinsights_get_asset_version(), true ); 
  95. wp_enqueue_script( MONSTERINSIGHTS_PLUGIN_SLUG . '-chartjs-script' ); 
  96.  
  97. // Maps 
  98. wp_register_script( MONSTERINSIGHTS_PLUGIN_SLUG . '-jvectormap-script', plugins_url( 'assets/js/jvectormap/jquery-jvectormap-2.0.3.min.js', MONSTERINSIGHTS_PLUGIN_FILE ), array( 'jquery' ), monsterinsights_get_asset_version(), true ); 
  99. wp_enqueue_script( MONSTERINSIGHTS_PLUGIN_SLUG . '-jvectormap-script' ); 
  100. wp_register_script( MONSTERINSIGHTS_PLUGIN_SLUG . '-jvectormap-world-script', plugins_url( 'assets/js/jvectormap/jquery-jvectormap-world-mill.js', MONSTERINSIGHTS_PLUGIN_FILE ), array( 'jquery', MONSTERINSIGHTS_PLUGIN_SLUG . '-jvectormap-script' ), monsterinsights_get_asset_version(), true ); 
  101. wp_enqueue_script( MONSTERINSIGHTS_PLUGIN_SLUG . '-jvectormap-world-script' ); 
  102.  
  103. // Select300 
  104. wp_register_script( MONSTERINSIGHTS_PLUGIN_SLUG . '-select300-script', plugins_url( 'assets/js/select300/select300.js', MONSTERINSIGHTS_PLUGIN_FILE ), array( 'jquery' ), monsterinsights_get_asset_version() ); 
  105. wp_enqueue_script( MONSTERINSIGHTS_PLUGIN_SLUG . '-select300-script' ); 
  106.  
  107. // Our Admin JS 
  108. $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; 
  109. if ( ! file_exists( MONSTERINSIGHTS_PLUGIN_DIR . 'assets/js/admin.min.js' ) ) { 
  110. $suffix = ''; 
  111.  
  112. $deps = array(  
  113. 'jquery',  
  114. MONSTERINSIGHTS_PLUGIN_SLUG . '-select300-script',  
  115. MONSTERINSIGHTS_PLUGIN_SLUG . '-jvectormap-script',  
  116. MONSTERINSIGHTS_PLUGIN_SLUG . '-chartjs-script',  
  117. MONSTERINSIGHTS_PLUGIN_SLUG . '-list-script' 
  118. ); 
  119.  
  120. wp_register_script( MONSTERINSIGHTS_PLUGIN_SLUG . '-admin-script', plugins_url( 'assets/js/admin' . $suffix . '.js', MONSTERINSIGHTS_PLUGIN_FILE ), $deps, monsterinsights_get_asset_version() ); 
  121. wp_enqueue_script( MONSTERINSIGHTS_PLUGIN_SLUG . '-admin-script' ); 
  122. wp_localize_script( 
  123. MONSTERINSIGHTS_PLUGIN_SLUG . '-admin-script',  
  124. 'monsterinsights_admin',  
  125. array( 
  126. 'ajax' => admin_url( 'admin-ajax.php' ),  
  127. 'dismiss_notice_nonce' => wp_create_nonce( 'monsterinsights-dismiss-notice' ),  
  128. 'loadingtext' => esc_html__( 'Loading...', 'google-analytics-for-wordpress' ),  
  129. 'settings_changed_confirm' => esc_html__( 'Warning: You have unsaved setting changes. If you leave the settings page without saving your changes will be lost. Did you still want to leave the page?', 'google-analytics-for-wordpress' ),  
  130. 'activate_nonce' => wp_create_nonce( 'monsterinsights-activate' ),  
  131. 'active' => esc_html__( 'Status: Active', 'google-analytics-for-wordpress' ),  
  132. 'activate' => esc_html__( 'Activate', 'google-analytics-for-wordpress' ),  
  133. 'networkactive' => esc_html__( 'Status: Network Activated', 'google-analytics-for-wordpress' ),  
  134. 'networkactivate' => esc_html__( 'Network activate', 'google-analytics-for-wordpress' ),  
  135. 'get_addons_nonce' => wp_create_nonce( 'monsterinsights-get-addons' ),  
  136. 'activating' => esc_html__( 'Activating...', 'google-analytics-for-wordpress' ),  
  137. 'deactivate' => esc_html__( 'Deactivate', 'google-analytics-for-wordpress' ),  
  138. 'networkdeactivate'=> esc_html__( 'Network deactivate', 'google-analytics-for-wordpress' ),  
  139. 'deactivate_nonce' => wp_create_nonce( 'monsterinsights-deactivate' ),  
  140. 'deactivating' => esc_html__( 'Deactivating...', 'google-analytics-for-wordpress' ),  
  141. 'inactive' => esc_html__( 'Status: Inactive', 'google-analytics-for-wordpress' ),  
  142. 'networkinactive' => esc_html__( 'Status: Network inactive', 'google-analytics-for-wordpress' ),  
  143. 'install' => esc_html__( 'Install', 'google-analytics-for-wordpress' ),  
  144. 'install_nonce' => wp_create_nonce( 'monsterinsights-install' ),  
  145. 'installing' => esc_html__( 'Installing...', 'google-analytics-for-wordpress' ),  
  146. 'proceed' => esc_html__( 'Proceed', 'google-analytics-for-wordpress' ),  
  147. 'isnetwork' => is_network_admin(),  
  148. 'copied' => esc_html__( 'Copied!', 'google-analytics-for-wordpress' ),  
  149. 'copytoclip' => esc_html__( 'Copy to Clipboard', 'google-analytics-for-wordpress' ),  
  150. 'failed' => esc_html__( 'Failed!', 'google-analytics-for-wordpress' ),  
  151. 'admin_nonce' => wp_create_nonce( 'mi-admin-nonce' ),  
  152. 'shorten' => esc_html__( 'Shorten URL' , 'google-analytics-for-wordpress'),  
  153. 'shortened' => esc_html__( 'Shortened!' , 'google-analytics-for-wordpress'),  
  154. 'working' => esc_html__( 'Working...' , 'google-analytics-for-wordpress'),  
  155. 'importtext' => esc_html__( 'Import' , 'google-analytics-for-wordpress'),  
  156. 'imported' => esc_html__( 'Imported!' , 'google-analytics-for-wordpress'),  
  157. ); 
  158.  
  159. // See if there's an admin.js file for this plugin version 
  160. if ( file_exists( MONSTERINSIGHTS_PLUGIN_DIR . 'pro/assets/js/admin.js' ) ) { 
  161. wp_register_script( MONSTERINSIGHTS_PLUGIN_SLUG . '-pro-admin-script', plugins_url( 'pro/assets/js/admin.js', MONSTERINSIGHTS_PLUGIN_FILE ), $deps, monsterinsights_get_asset_version() ); 
  162. wp_enqueue_script( MONSTERINSIGHTS_PLUGIN_SLUG . '-pro-admin-script' ); 
  163. wp_localize_script( 
  164. MONSTERINSIGHTS_PLUGIN_SLUG . '-pro-admin-script',  
  165. 'monsterinsights_admin',  
  166. array( 
  167. 'ajax' => admin_url( 'admin-ajax.php' ) 
  168. ); 
  169. } else if ( file_exists( MONSTERINSIGHTS_PLUGIN_DIR . 'lite/assets/js/admin.js' ) ) { 
  170. wp_register_script( MONSTERINSIGHTS_PLUGIN_SLUG . '-only-admin-script', plugins_url( 'only/assets/js/admin.js', MONSTERINSIGHTS_PLUGIN_FILE ), $deps, monsterinsights_get_asset_version() ); 
  171. wp_enqueue_script( MONSTERINSIGHTS_PLUGIN_SLUG . '-only-admin-script' ); 
  172. wp_localize_script( 
  173. MONSTERINSIGHTS_PLUGIN_SLUG . '-only-admin-script',  
  174. 'monsterinsights_admin',  
  175. array( 
  176. 'ajax' => admin_url( 'admin-ajax.php' ) 
  177. ); 
  178. }  
  179.  
  180. // ublock notice 
  181. add_action( 'admin_print_footer_scripts', 'monsterinsights_settings_ublock_error_js', 9999999 ); 
  182. add_action( 'admin_enqueue_scripts', 'monsterinsights_admin_scripts' ); 
  183.  
  184. /** 
  185. * Remove Assets that conflict with ours from our screens. 
  186. * 
  187. * @since 6.0.4 
  188. * @access public 
  189. * 
  190. * @return null Return early if not on the proper screen. 
  191. */ 
  192. function monsterinsights_remove_conflicting_asset_files() { 
  193.  
  194. // Get current screen. 
  195. $screen = get_current_screen(); 
  196.  
  197. // Bail if we're not on a MonsterInsights screen. 
  198. if ( empty( $screen->id ) || strpos( $screen->id, 'monsterinsights' ) === false ) { 
  199. return; 
  200.  
  201. $styles = array( 
  202. 'kt_admin_css', // Pinnacle theme 
  203. 'select2-css', // Schema theme 
  204. 'tweetshare_style', // TweetShare - Click To Tweet 
  205. 'tweetshare_custom_style', // TweetShare - Click To Tweet 
  206. 'tweeetshare_font_script', // TweetShare - Click To Tweet 
  207. 'tweeetshare_jquery_script', // TweetShare - Click To Tweet 
  208. 'tweeetshare_jqueryui_script', // TweetShare - Click To Tweet 
  209. 'tweeetshare_custom_script', // TweetShare - Click To Tweet 
  210. 'tweeetshare_custome_style', // TweetShare - Click To Tweet 
  211. 'tweeetshare_notice_style', // TweetShare - Click To Tweet 
  212. 'tweeetshare_theme_style', // TweetShare - Click To Tweet 
  213. 'tweeetshare_tweet_box_style', // TweetShare - Click To Tweet 
  214. ); 
  215.  
  216. $scripts = array( 
  217. 'kad_admin_js', // Pinnacle theme 
  218. ); 
  219.  
  220. foreach ( $styles as $style ) { 
  221. wp_dequeue_style( $style ); // Remove CSS file from MI screen 
  222.  
  223. foreach ( $scripts as $script ) { 
  224. wp_dequeue_script( $script ); // Remove JS file from MI screen 
  225. add_action( 'admin_enqueue_scripts', 'monsterinsights_remove_conflicting_asset_files', 9999 ); 
  226.  
  227. /** 
  228. * Remove non-MI notices from MI page. 
  229. * 
  230. * @since 6.0.0 
  231. * @access public 
  232. * 
  233. * @return null Return early if not on the proper screen. 
  234. */ 
  235. function hide_non_monsterinsights_warnings () { 
  236. // Bail if we're not on a MonsterInsights screen. 
  237. if ( empty( $_REQUEST['page'] ) || strpos( $_REQUEST['page'], 'monsterinsights' ) === false ) { 
  238. return; 
  239.  
  240. global $wp_filter; 
  241. if ( !empty( $wp_filter['user_admin_notices']->callbacks ) && is_array( $wp_filter['user_admin_notices']->callbacks ) ) { 
  242. foreach( $wp_filter['user_admin_notices']->callbacks as $priority => $hooks ) { 
  243. foreach ( $hooks as $name => $arr ) { 
  244. if ( !empty( $name ) && strpos( $name, 'monsterinsights' ) === false ) { 
  245. unset( $wp_filter['user_admin_notices']->callbacks[$priority][$name] ); 
  246.  
  247. if ( !empty( $wp_filter['admin_notices']->callbacks ) && is_array( $wp_filter['admin_notices']->callbacks ) ) { 
  248. foreach( $wp_filter['admin_notices']->callbacks as $priority => $hooks ) { 
  249. foreach ( $hooks as $name => $arr ) { 
  250. if ( !empty( $name ) && strpos( $name, 'monsterinsights' ) === false ) { 
  251. unset( $wp_filter['admin_notices']->callbacks[$priority][$name] ); 
  252.  
  253. if ( !empty( $wp_filter['all_admin_notices']->callbacks ) && is_array( $wp_filter['all_admin_notices']->callbacks ) ) { 
  254. foreach( $wp_filter['all_admin_notices']->callbacks as $priority => $hooks ) { 
  255. foreach ( $hooks as $name => $arr ) { 
  256. if ( !empty( $name ) && strpos( $name, 'monsterinsights' ) === false ) { 
  257. unset( $wp_filter['all_admin_notices']->callbacks[$priority][$name] ); 
  258. add_action('admin_print_scripts', 'hide_non_monsterinsights_warnings');  
  259.  
  260. /** 
  261. * Called whenever an upgrade button / link is displayed in Lite, this function will 
  262. * check if there's a shareasale ID specified. 
  263. * 
  264. * There are three ways to specify an ID, ordered by highest to lowest priority 
  265. * - add_filter( 'monsterinsights_shareasale_id', function() { return 1234; } ); 
  266. * - define( 'MONSTERINSIGHTS_SHAREASALE_ID', 1234 ); 
  267. * - get_option( 'monsterinsights_shareasale_id' ); (with the option being in the wp_options table) 
  268. * 
  269. * If an ID is present, returns the ShareASale link with the affiliate ID, and tells 
  270. * ShareASale to then redirect to monsterinsights.com/lite 
  271. * 
  272. * If no ID is present, just returns the monsterinsights.com/lite URL with UTM tracking. 
  273. * 
  274. * @since 6.0.0 
  275. * @access public 
  276. * 
  277. * @return string Upgrade link. 
  278. */ 
  279. function monsterinsights_get_upgrade_link() { 
  280.  
  281. if ( class_exists( 'MonsterInsights' ) ) { 
  282. // User is using MonsterInsights, so just take them to the Pricing page. 
  283. // Note: On the Addons screen, if the user has a license, we won't hit this function,  
  284. // as the API will tell us the direct URL to send the user to based on their license key,  
  285. // so they see pro-rata pricing. 
  286. return 'https://www.monsterinsights.com/pricing/?utm_source=proplugin&utm_medium=link&utm_campaign=WordPress'; 
  287.  
  288. $shareasale_id = monsterinsights_get_shareasale_id(); 
  289.  
  290. // If at this point we still don't have an ID, we really don't have one! 
  291. // Just return the standard upgrade URL. 
  292. if ( empty( $shareasale_id ) ) { 
  293. return 'https://www.monsterinsights.com/pricing/?utm_source=liteplugin&utm_medium=link&utm_campaign=WordPress'; 
  294.  
  295. // If here, we have a ShareASale ID 
  296. // Return ShareASale URL with redirect. 
  297. return 'https://www.shareasale.com/r.cfm?u=' . $shareasale_id . '&b=971799&m=69975&afftrack=&urllink=monsterinsights%2Ecom%2Flite%2F'; 
  298.  
  299. function monsterinsights_get_shareasale_id() { 
  300. // Check if there's a constant. 
  301. $shareasale_id = ''; 
  302. if ( defined( 'MONSTERINSIGHTS_SHAREASALE_ID' ) ) { 
  303. $shareasale_id = MONSTERINSIGHTS_SHAREASALE_ID; 
  304.  
  305. // If there's no constant, check if there's an option. 
  306. if ( empty( $shareasale_id ) ) { 
  307. $shareasale_id = get_option( 'monsterinsights_shareasale_id', '' ); 
  308.  
  309. // Whether we have an ID or not, filter the ID. 
  310. $shareasale_id = apply_filters( 'monsterinsights_shareasale_id', $shareasale_id ); 
  311. return $shareasale_id; 
  312.  
  313. function monsterinsights_settings_ublock_error_js() { 
  314. echo "<script type='text/javascript'>\n"; 
  315. echo "jQuery( document ).ready( function( $ ) { 
  316. if ( window.uorigindetected == null) { 
  317. $('#monsterinsights-ublock-origin-error').show(); 
  318. $('.monsterinsights-nav-tabs').hide(); 
  319. $('.monsterinsights-nav-container').hide(); 
  320. $('#monsterinsights-addon-heading').hide(); 
  321. $('#monsterinsights-addons').hide(); 
  322. $('#monsterinsights-reports').hide(); 
  323. });"; 
  324. echo "\n</script>"; 
  325.  
  326. function monsterinsights_ublock_notice() { 
  327. ob_start();?> 
  328. <div id="monsterinsights-ublock-origin-error" class="error inline" style="display:none;"> 
  329. <?php echo sprintf( esc_html__( 'MonsterInsights has detected that it\'s files are being blocked. This is usually caused by a adblock browser plugin (particularly uBlock Origin), or a conflicting WordPress theme or plugin. This issue only affects the admin side of MonsterInsights. To solve this, ensure MonsterInsights is whitelisted for your website URL in any adblock browser plugin you use. For step by step directions on how to do this, %1$sclick here%2$s. If this doesn\'t solve the issue (rare), send us a ticket %3$shere%2$s and we\'ll be happy to help diagnose the issue.', 'google-analytics-for-wordpress'), '<a href="https://monsterinsights.com/docs/monsterinsights-asset-files-blocked/" target="_blank" rel="noopener noreferrer" referrer="no-referrer">', '</a>', '<a href="https://monsterinsights.com/contact/" target="_blank" rel="noopener noreferrer" referrer="no-referrer">'); 
  330. ?> 
  331. </div> 
  332. <?php 
  333. return ob_get_clean(); 
.