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.  
  29. if ( ! function_exists( 'is_plugin_active_for_network' ) ) { 
  30. require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); 
  31.  
  32. if ( is_plugin_active_for_network( GAWP_PATH ) ) { 
  33. add_action( 'network_admin_menu', array( $this, 'create_admin_menu' ), 5 ); 
  34.  
  35. $this->dashboards_disabled = Yoast_GA_Settings::get_instance()->dashboards_disabled(); 
  36. $this->parent_slug = ( ( $this->dashboards_disabled ) ? 'yst_ga_settings' : 'yst_ga_dashboard' ); 
  37.  
  38. /** 
  39. * Create the admin menu 
  40. */ 
  41. public function create_admin_menu() { 
  42. /** 
  43. * Filter: 'wpga_menu_on_top' - Allows filtering of menu location of the GA plugin, if false is returned, it moves to bottom. 
  44. * @api book unsigned 
  45. */ 
  46.  
  47. // Base 64 encoded SVG image 
  48. $icon_svg = 'data:image/svg+xml;base64, PD94<BASE64_ENCODED>'; 
  49.  
  50. $menu_name = is_network_admin() ? 'extensions' : 'dashboard'; 
  51.  
  52. if ( $this->dashboards_disabled ) { 
  53. $menu_name = 'settings'; 
  54.  
  55. // Add main page 
  56. add_menu_page( 
  57. __( 'Yoast Google Analytics:', 'google-analytics-for-wordpress' ) . ' ' . __( 'General settings', 'google-analytics-for-wordpress' ), __( 'Analytics', 'google-analytics-for-wordpress' ), 'manage_options', 'yst_ga_' . $menu_name,  
  58. array( 
  59. $this->target_object,  
  60. 'load_page',  
  61. ),  
  62. $icon_svg,  
  63. $this->get_menu_position() 
  64. ); 
  65.  
  66. $this->add_submenu_pages(); 
  67.  
  68. /** 
  69. * Get the menu position of the Analytics item 
  70. * @return string 
  71. */ 
  72. private function get_menu_position() { 
  73. $on_top = apply_filters( 'wpga_menu_on_top', true ); 
  74.  
  75. if ( $on_top ) { 
  76. $position = $this->get_menu_position_value( 'top' ); 
  77.  
  78. else { 
  79. $position = $this->get_menu_position_value( 'bottom' ); 
  80.  
  81. // If the dashboards are disabled, force the menu item to stay at the bottom of the menu 
  82. if ( $this->dashboards_disabled ) { 
  83. $position = $this->get_menu_position_value( 'bottom' ); 
  84.  
  85. return $position; 
  86.  
  87. /** 
  88. * Get the top or bottom menu location number 
  89. * @param string $location 
  90. * @return string 
  91. */ 
  92. private function get_menu_position_value( $location ) { 
  93. if ( $location == 'top' ) { 
  94. return '2.00013467543'; 
  95.  
  96. return '100.00013467543'; 
  97.  
  98. /** 
  99. * Prepares an array that can be used to add a submenu page to the Google Analytics for Wordpress menu 
  100. * @param string $submenu_name 
  101. * @param string $submenu_slug 
  102. * @param string $font_color 
  103. * @return array 
  104. */ 
  105. private function prepare_submenu_page( $submenu_name, $submenu_slug, $font_color = '' ) { 
  106. return array( 
  107. 'parent_slug' => $this->parent_slug,  
  108. 'page_title' => __( 'Yoast Google Analytics:', 'google-analytics-for-wordpress' ) . ' ' . $submenu_name,  
  109. 'menu_title' => $this->parse_menu_title( $submenu_name, $font_color ),  
  110. 'capability' => 'manage_options',  
  111. 'menu_slug' => 'yst_ga_' . $submenu_slug,  
  112. 'submenu_function' => array( $this->target_object, 'load_page' ),  
  113. ); 
  114.  
  115. /** 
  116. * Parsing the menutitle 
  117. * @param string $menu_title 
  118. * @param string $font_color 
  119. * @return string 
  120. */ 
  121. private function parse_menu_title( $menu_title, $font_color ) { 
  122. if ( ! empty( $font_color ) ) { 
  123. $menu_title = '<span style="color:' . $font_color . '">' . $menu_title . '</span>'; 
  124.  
  125. return $menu_title; 
  126.  
  127. /** 
  128. * Adds a submenu page to the Google Analytics for WordPress menu 
  129. * @param array $submenu_page 
  130. */ 
  131. private function add_submenu_page( $submenu_page ) { 
  132. $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'] ); 
  133. $is_dashboard = ( 'yst_ga_dashboard' === $submenu_page['menu_slug'] ); 
  134. $this->add_assets( $page, $is_dashboard ); 
  135.  
  136. /** 
  137. * Adding stylesheets and based on $is_not_dashboard maybe some more styles and scripts. 
  138. * @param string $page 
  139. * @param boolean $is_dashboard 
  140. */ 
  141. private function add_assets( $page, $is_dashboard ) { 
  142. add_action( 'admin_print_styles-' . $page, array( 'Yoast_GA_Admin_Assets', 'enqueue_styles' ) ); 
  143. add_action( 'admin_print_styles-' . $page, array( 'Yoast_GA_Admin_Assets', 'enqueue_settings_styles' ) ); 
  144. add_action( 'admin_print_scripts-' . $page, array( 'Yoast_GA_Admin_Assets', 'enqueue_scripts' ) ); 
  145. if ( ! $is_dashboard && filter_input( INPUT_GET, 'page' ) === 'yst_ga_dashboard' ) { 
  146. Yoast_GA_Admin_Assets::enqueue_dashboard_assets(); 
  147.  
  148. /** 
  149. * Prepares and adds submenu pages to the Google Analytics for Wordpress menu: 
  150. * - Dashboard 
  151. * - Settings 
  152. * - Extensions 
  153. * @return void 
  154. */ 
  155. private function add_submenu_pages() { 
  156. foreach ( $this->get_submenu_types() as $submenu ) { 
  157. if ( isset( $submenu['color'] ) ) { 
  158. $submenu_page = $this->prepare_submenu_page( $submenu['label'], $submenu['slug'], $submenu['color'] ); 
  159. else { 
  160. $submenu_page = $this->prepare_submenu_page( $submenu['label'], $submenu['slug'] ); 
  161. $this->add_submenu_page( $submenu_page ); 
  162.  
  163. /** 
  164. * Determine which submenu types should be added as a submenu page. 
  165. * Dashboard can be disables by user 
  166. * Dashboard and settings are disables in network admin 
  167. * @return array 
  168. */ 
  169. private function get_submenu_types() { 
  170. /** 
  171. * Array structure: 
  172. * array( 
  173. * $submenu_name => array( 
  174. * 'color' => $font_color,  
  175. * 'label' => __( 'text-label', 'google-analytics-for-wordpress' ),  
  176. * 'slug' => $menu_slug,  
  177. * ),  
  178. * ..,  
  179. * ) 
  180. * $font_color can be left empty. 
  181. */ 
  182. $submenu_types = array(); 
  183.  
  184. if ( ! is_network_admin() ) { 
  185.  
  186. if ( ! $this->dashboards_disabled ) { 
  187. $submenu_types['dashboard'] = array( 
  188. 'label' => __( 'Dashboard', 'google-analytics-for-wordpress' ),  
  189. 'slug' => 'dashboard',  
  190. ); 
  191.  
  192. $submenu_types['settings'] = array( 
  193. 'label' => __( 'Settings', 'google-analytics-for-wordpress' ),  
  194. 'slug' => 'settings',  
  195. ); 
  196.  
  197. $submenu_types['extensions'] = array( 
  198. 'color' => '#f18500',  
  199. 'label' => __( 'Extensions', 'google-analytics-for-wordpress' ),  
  200. 'slug' => 'extensions',  
  201. ); 
  202.  
  203. return $submenu_types;