Yoast_GA_Admin_Menu

This class is for the backend, extendable for all child classes.

Defined (1)

The class is defined in the following location(s).

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