/admin/class-admin-menu.php

  1. <?php 
  2. /** 
  3. * @package GoogleAnalytics\Admin 
  4. */ 
  5.  
  6. /** 
  7. * This class is for the backend, extendable for all child classes 
  8. */ 
  9. class Yoast_GA_Admin_Menu { 
  10.  
  11. /** 
  12. * @var object $target_object The property used for storing target object (class admin) 
  13. */ 
  14. private $target_object; 
  15.  
  16. /** 
  17. * @var boolean $dashboard_disabled The dashboards disabled bool 
  18. */ 
  19. private $dashboards_disabled; 
  20.  
  21. /** 
  22. * The parent slug for the submenu items based on if the dashboards are disabled or not. 
  23. * 
  24. * @var string 
  25. */ 
  26. private $parent_slug; 
  27.  
  28. /** 
  29. * Setting the target_object and adding actions 
  30. * 
  31. * @param object $target_object 
  32. */ 
  33. public function __construct( $target_object ) { 
  34.  
  35. $this->target_object = $target_object; 
  36.  
  37. add_action( 'admin_menu', array( $this, 'create_admin_menu' ), 10 ); 
  38. add_action('admin_head', array( $this, 'mi_add_styles_for_menu' ) ); 
  39.  
  40. if ( ! function_exists( 'is_plugin_active_for_network' ) ) { 
  41. require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); 
  42.  
  43. if ( is_plugin_active_for_network( GAWP_PATH ) ) { 
  44. add_action( 'network_admin_menu', array( $this, 'create_admin_menu' ), 5 ); 
  45.  
  46. $this->dashboards_disabled = Yoast_GA_Settings::get_instance()->dashboards_disabled(); 
  47. $this->parent_slug = ( ( $this->dashboards_disabled ) ? 'yst_ga_settings' : 'yst_ga_dashboard' ); 
  48.  
  49. public function mi_add_styles_for_menu() { 
  50. ?> 
  51. <style type="text/css"> 
  52. @font-face { 
  53. font-family: 'MonsterInsights'; 
  54. src: url('data:application/octet-stream;base64, d09<BASE64_ENCODED>') format('woff'),  
  55. url('data:application/octet-stream;base64, <BASE64_ENCODED>') format('truetype'); 
  56. font-weight: normal; 
  57. font-style: normal; 
  58.  
  59. #toplevel_page_yst_ga_dashboard .wp-menu-image:before,  
  60. .yst_ga_dashboard-menu-icon:before { 
  61.  
  62. font-family: "MonsterInsights" !important; 
  63. content: '\61'; 
  64. font-style: normal !important; 
  65. font-weight: normal !important; 
  66. font-variant: normal !important; 
  67. text-transform: none !important; 
  68. speak: none; 
  69. line-height: 1; 
  70. -webkit-font-smoothing: antialiased; 
  71. -moz-osx-font-smoothing: grayscale; 
  72. color: #fff; 
  73. #toplevel_page_yst_ga_dashboard .wp-menu-image:before { 
  74. font-size: 1.5em; 
  75. padding-top: 4px; 
  76. padding-bottom: 2px; 
  77. font-size: 22px; 
  78. </style> 
  79. <?php 
  80.  
  81. /** 
  82. * Create the admin menu 
  83. */ 
  84. public function create_admin_menu() { 
  85. $menu_name = is_network_admin() ? 'extensions' : 'dashboard'; 
  86.  
  87. if ( $this->dashboards_disabled ) { 
  88. $menu_name = 'settings'; 
  89.  
  90. // Add main page 
  91. add_menu_page( 
  92. __( 'Google Analytics by MonsterInsights:', 'google-analytics-for-wordpress' ) . ' ' . __( 'General settings', 'google-analytics-for-wordpress' ), __( 'Insights', 'google-analytics-for-wordpress' ), 'manage_options', 'yst_ga_' . $menu_name,  
  93. array( 
  94. $this->target_object,  
  95. 'load_page',  
  96. ),  
  97. '',  
  98. '100.00013467543' 
  99. ); 
  100.  
  101.  
  102. $this->add_submenu_pages(); 
  103.  
  104. /** 
  105. * Prepares an array that can be used to add a submenu page to the Google Analytics for WordPress menu 
  106. * 
  107. * @param string $submenu_name 
  108. * @param string $submenu_slug 
  109. * @param string $font_color 
  110. * 
  111. * @return array 
  112. */ 
  113. private function prepare_submenu_page( $submenu_name, $submenu_slug, $font_color = '' ) { 
  114. return array( 
  115. 'parent_slug' => $this->parent_slug,  
  116. 'page_title' => __( 'Google Analytics by MonsterInsights:', 'google-analytics-for-wordpress' ) . ' ' . $submenu_name,  
  117. 'menu_title' => $this->parse_menu_title( $submenu_name, $font_color ),  
  118. 'capability' => 'manage_options',  
  119. 'menu_slug' => 'yst_ga_' . $submenu_slug,  
  120. 'submenu_function' => array( $this->target_object, 'load_page' ),  
  121. ); 
  122.  
  123. /** 
  124. * Parsing the menutitle 
  125. * 
  126. * @param string $menu_title 
  127. * @param string $font_color 
  128. * 
  129. * @return string 
  130. */ 
  131. private function parse_menu_title( $menu_title, $font_color ) { 
  132. if ( ! empty( $font_color ) ) { 
  133. $menu_title = '<span style="color:' . $font_color . '">' . $menu_title . '</span>'; 
  134.  
  135. return $menu_title; 
  136.  
  137. /** 
  138. * Adds a submenu page to the Google Analytics for WordPress menu 
  139. * 
  140. * @param array $submenu_page 
  141. */ 
  142. private function add_submenu_page( $submenu_page ) { 
  143. $page = add_submenu_page( $submenu_page['parent_slug'], $submenu_page['page_title'], $submenu_page['menu_title'], $submenu_page['capability'], $submenu_page['menu_slug'], $submenu_page['submenu_function'] ); 
  144. $is_dashboard = ( 'yst_ga_dashboard' === $submenu_page['menu_slug'] ); 
  145. $this->add_assets( $page, $is_dashboard ); 
  146.  
  147. /** 
  148. * Adding stylesheets and based on $is_not_dashboard maybe some more styles and scripts. 
  149. * 
  150. * @param string $page 
  151. * @param boolean $is_dashboard 
  152. */ 
  153. private function add_assets( $page, $is_dashboard ) { 
  154. add_action( 'admin_print_styles-' . $page, array( 'Yoast_GA_Admin_Assets', 'enqueue_styles' ) ); 
  155. add_action( 'admin_print_styles-' . $page, array( 'Yoast_GA_Admin_Assets', 'enqueue_settings_styles' ) ); 
  156. add_action( 'admin_print_scripts-' . $page, array( 'Yoast_GA_Admin_Assets', 'enqueue_scripts' ) ); 
  157. if ( ! $is_dashboard && filter_input( INPUT_GET, 'page' ) === 'yst_ga_dashboard' ) { 
  158. Yoast_GA_Admin_Assets::enqueue_dashboard_assets(); 
  159.  
  160. /** 
  161. * Prepares and adds submenu pages to the Google Analytics for WordPress menu: 
  162. * - Dashboard 
  163. * - Settings 
  164. * - Extensions 
  165. * 
  166. * @return void 
  167. */ 
  168. private function add_submenu_pages() { 
  169. foreach ( $this->get_submenu_types() as $submenu ) { 
  170. if ( isset( $submenu['color'] ) ) { 
  171. $submenu_page = $this->prepare_submenu_page( $submenu['label'], $submenu['slug'], $submenu['color'] ); 
  172. else { 
  173. $submenu_page = $this->prepare_submenu_page( $submenu['label'], $submenu['slug'] ); 
  174. $this->add_submenu_page( $submenu_page ); 
  175.  
  176. /** 
  177. * Determine which submenu types should be added as a submenu page. 
  178. * 
  179. * Dashboard can be disables by user 
  180. * 
  181. * Dashboard and settings are disables in network admin 
  182. * 
  183. * @return array 
  184. */ 
  185. private function get_submenu_types() { 
  186. /** 
  187. * Array structure: 
  188. * 
  189. * array( 
  190. * $submenu_name => array( 
  191. * 'color' => $font_color,  
  192. * 'label' => __( 'text-label', 'google-analytics-for-wordpress' ),  
  193. * 'slug' => $menu_slug,  
  194. * ),  
  195. * ..,  
  196. * ) 
  197. * 
  198. * $font_color can be left empty. 
  199. * 
  200. */ 
  201. $submenu_types = array(); 
  202.  
  203. if ( ! is_network_admin() ) { 
  204.  
  205. if ( ! $this->dashboards_disabled ) { 
  206. $submenu_types['dashboard'] = array( 
  207. 'label' => __( 'Dashboard', 'google-analytics-for-wordpress' ),  
  208. 'slug' => 'dashboard',  
  209. ); 
  210.  
  211. $submenu_types['settings'] = array( 
  212. 'label' => __( 'Settings', 'google-analytics-for-wordpress' ),  
  213. 'slug' => 'settings',  
  214. ); 
  215.  
  216. $submenu_types['extensions'] = array( 
  217. 'color' => '#f18500',  
  218. 'label' => __( 'Extensions', 'google-analytics-for-wordpress' ),  
  219. 'slug' => 'extensions',  
  220. ); 
  221.  
  222. return $submenu_types; 
.