Yoast_GA_Dashboards_Display

Dashboards display class.

Defined (1)

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

/admin/dashboards/class-admin-dashboards-display.php  
  1. class Yoast_GA_Dashboards_Display { 
  2.  
  3. /** 
  4. * Property for holding instance of itself 
  5. * @var Yoast_GA_Dashboards_Display 
  6. */ 
  7. protected static $instance; 
  8.  
  9. /** 
  10. * Container for holding the setted dashboards 
  11. * @var array 
  12. */ 
  13. protected $dashboards = array(); 
  14.  
  15. /** 
  16. * @var array The dashboard types which can be used 
  17. */ 
  18. protected $dashboard_types = array( 'graph', 'table' ); 
  19.  
  20. /** 
  21. * @var array For each dashboard type there will be created a driver that will be stored in this property 
  22. */ 
  23. protected $drivers = array(); 
  24.  
  25. /** 
  26. * For the use of singleton pattern. Create instance of itself and return his instance 
  27. * @return Yoast_GA_Dasboards_Graph 
  28. */ 
  29. public static function get_instance() { 
  30.  
  31. if ( is_null( self::$instance ) ) { 
  32. self::$instance = new self(); 
  33.  
  34. return self::$instance; 
  35.  
  36. /** 
  37. * Protected constructor to prevent creating a new instance of the 
  38. * *Singleton* via the `new` operator from outside of this class. 
  39. */ 
  40. protected function __construct() { 
  41. foreach ( $this->dashboard_types as $dashboard_type ) { 
  42. if ( ! $this->driver_exists( $dashboard_type ) ) { 
  43. $this->create_driver( $dashboard_type ); 
  44.  
  45. /** 
  46. * Get the driver from property, this->drivers 
  47. * If driver doesn't exist, it will be created first 
  48. * @param string $dashboard_type - The name of the driver that will be returned 
  49. * @return object 
  50. */ 
  51. private function driver( $dashboard_type ) { 
  52.  
  53. if ( ! $this->driver_exists( $dashboard_type ) ) { 
  54. $this->create_driver( $dashboard_type ); 
  55.  
  56. return $this->drivers[ $dashboard_type ]; 
  57.  
  58. /** 
  59. * Adding dashboards to $this->dashboard en register them to the driver by $this->register 
  60. * @param array $dashboards 
  61. */ 
  62. public function add_dashboards( $dashboards ) { 
  63. // Save all dashboards to property - for future use 
  64. $this->dashboards = array_merge( $this->dashboards, $dashboards ); 
  65.  
  66. $this->register( $dashboards ); 
  67.  
  68. /** 
  69. * Register dashboards to the drivers 
  70. * @param array $dashboards 
  71. */ 
  72. private function register( $dashboards ) { 
  73. foreach ( $dashboards as $dashboard_name => $dashboard_settings ) { 
  74. if ( ! empty( $dashboard_settings['type'] ) ) { 
  75. $this->driver( $dashboard_settings['type'] )->register( $dashboard_name, $dashboard_settings ); 
  76.  
  77. /** 
  78. * Displaying the $dashboards on the screen. If $dashboards isn't given it will display all registered 
  79. * dashboards 
  80. * @param string $tab_to_show 
  81. */ 
  82. public function display( $tab_to_show ) { 
  83.  
  84. $dashboards_to_show = $this->dashboards; 
  85.  
  86. foreach ( $dashboards_to_show as $dashboard_name => $dashboard_settings ) { 
  87. if ( ! empty( $dashboard_settings['tab'] ) && $dashboard_settings['tab'] === $tab_to_show ) { 
  88. $this->driver( $dashboard_settings['type'] )->display( $dashboard_name ); 
  89.  
  90. /** 
  91. * Check if given $dashboard_type exists and if it's an object 
  92. * @param string $dashboard_type 
  93. * @return bool 
  94. */ 
  95. protected function driver_exists( $dashboard_type ) { 
  96. return array_key_exists( $dashboard_type, $this->drivers ) && is_object( $this->drivers[ $dashboard_type ] ); 
  97.  
  98. /** 
  99. * Creates a driver based on given $dashboard_type 
  100. * @param string $dashboard_type 
  101. */ 
  102. protected function create_driver( $dashboard_type ) { 
  103. $driver_class = 'Yoast_GA_Dashboards_' . ucfirst( $dashboard_type ); 
  104. $this->drivers[ $dashboard_type ] = new $driver_class(); 
  105.