Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
70.00% |
28 / 40 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Reports_Page_Registry | |
73.68% |
28 / 38 |
|
66.67% |
4 / 6 |
20.67 | |
0.00% |
0 / 1 |
| register | |
61.11% |
11 / 18 |
|
0.00% |
0 / 1 |
11.76 | |||
| get_all_registered | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| get_available_pages | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| is_registered | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| ensure_hydrated | |
57.14% |
4 / 7 |
|
0.00% |
0 / 1 |
3.71 | |||
| get_instance | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Registry of the reports pages registered with the dashboard. |
| 4 | * |
| 5 | * @package automattic/jetpack-premium-analytics |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\PremiumAnalytics; |
| 9 | |
| 10 | require_once __DIR__ . '/dashboard-grammar.php'; |
| 11 | require_once __DIR__ . '/class-reports-page.php'; |
| 12 | |
| 13 | /** |
| 14 | * Holds the reports pages per dashboard, hydrated once after `init`. |
| 15 | * |
| 16 | * @since $$next-version$$ |
| 17 | */ |
| 18 | final class Reports_Page_Registry { |
| 19 | |
| 20 | /** |
| 21 | * Action fired once, with the registry, when it hydrates on its first read after `init`. |
| 22 | */ |
| 23 | const REGISTER_ACTION = 'jetpack_premium_analytics_register_reports_pages'; |
| 24 | |
| 25 | /** |
| 26 | * Registered pages, keyed by dashboard name then page id. |
| 27 | * |
| 28 | * @var array<string, array<string, Reports_Page>> |
| 29 | */ |
| 30 | private $registered_pages = array(); |
| 31 | |
| 32 | /** |
| 33 | * Whether the registration action has fired. |
| 34 | * |
| 35 | * @var bool |
| 36 | */ |
| 37 | private $hydrated = false; |
| 38 | |
| 39 | /** |
| 40 | * Main instance. |
| 41 | * |
| 42 | * @var self|null |
| 43 | */ |
| 44 | private static $instance = null; |
| 45 | |
| 46 | /** |
| 47 | * Registers a page. |
| 48 | * |
| 49 | * @param string $dashboard_name Dashboard identifier. |
| 50 | * @param string $id Page identifier, namespaced. |
| 51 | * @param array $args Page arguments. |
| 52 | * @return Reports_Page|false The page on success, false on failure. |
| 53 | */ |
| 54 | public function register( $dashboard_name, $id, $args = array() ) { |
| 55 | if ( ! is_string( $dashboard_name ) || 1 !== preg_match( '/^' . get_dashboard_name_pattern() . '$/', $dashboard_name ) ) { |
| 56 | _doing_it_wrong( __METHOD__, esc_html__( 'Dashboard names must be lowercase strings of letters, numbers, and hyphens, optionally separated by underscores.', 'jetpack-premium-analytics-pkg' ), 'jetpack-premium-analytics-$$next-version$$' ); |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | if ( ! is_string( $id ) || 1 !== preg_match( '/^' . get_dashboard_section_id_pattern() . '$/', $id ) ) { |
| 61 | _doing_it_wrong( __METHOD__, esc_html__( 'Reports page IDs must contain a namespace prefix. Example: my-plugin/my-report', 'jetpack-premium-analytics-pkg' ), 'jetpack-premium-analytics-$$next-version$$' ); |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | if ( $this->is_registered( $dashboard_name, $id ) ) { |
| 66 | /* translators: 1: Dashboard name. 2: Page ID. */ |
| 67 | $message = sprintf( __( 'Reports page "%2$s" is already registered for dashboard "%1$s".', 'jetpack-premium-analytics-pkg' ), $dashboard_name, $id ); |
| 68 | _doing_it_wrong( __METHOD__, esc_html( $message ), 'jetpack-premium-analytics-$$next-version$$' ); |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | $page = new Reports_Page( $dashboard_name, $id, $args ); |
| 73 | |
| 74 | // The client routes by path, so two ids may not share one. |
| 75 | foreach ( $this->registered_pages[ $dashboard_name ] ?? array() as $holder ) { |
| 76 | if ( $holder->path === $page->path ) { |
| 77 | /* translators: 1: Dashboard name. 2: Page path. 3: ID of the page already using the path. */ |
| 78 | $message = sprintf( __( 'Reports page path "%2$s" is already used on dashboard "%1$s" by "%3$s".', 'jetpack-premium-analytics-pkg' ), $dashboard_name, $page->path, $holder->id ); |
| 79 | _doing_it_wrong( __METHOD__, esc_html( $message ), 'jetpack-premium-analytics-$$next-version$$' ); |
| 80 | return false; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | $this->registered_pages[ $dashboard_name ][ $id ] = $page; |
| 85 | |
| 86 | return $page; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Every registered page of a dashboard, keyed by id. |
| 91 | * |
| 92 | * @param string $dashboard_name Dashboard identifier. |
| 93 | * @return array<string, Reports_Page> |
| 94 | */ |
| 95 | public function get_all_registered( $dashboard_name ) { |
| 96 | $this->ensure_hydrated(); |
| 97 | |
| 98 | return $this->registered_pages[ $dashboard_name ] ?? array(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * The pages available to the current reader, as a list. |
| 103 | * |
| 104 | * @param string $dashboard_name Dashboard identifier. |
| 105 | * @return Reports_Page[] |
| 106 | */ |
| 107 | public function get_available_pages( $dashboard_name ) { |
| 108 | $pages = array_filter( |
| 109 | $this->get_all_registered( $dashboard_name ), |
| 110 | static function ( Reports_Page $page ) { |
| 111 | return $page->is_available(); |
| 112 | } |
| 113 | ); |
| 114 | |
| 115 | return array_values( $pages ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Whether a page id is registered. Does not hydrate: `register()` relies on it. |
| 120 | * |
| 121 | * @param string $dashboard_name Dashboard identifier. |
| 122 | * @param string $id Page identifier. |
| 123 | * @return bool |
| 124 | */ |
| 125 | public function is_registered( $dashboard_name, $id ) { |
| 126 | return isset( $this->registered_pages[ $dashboard_name ][ $id ] ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Fires the registration action once, after `init`. |
| 131 | */ |
| 132 | private function ensure_hydrated() { |
| 133 | if ( $this->hydrated ) { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | // Latching this early would drop every registrant hooked later, so the read skips the action. |
| 138 | if ( ! did_action( 'init' ) ) { |
| 139 | _doing_it_wrong( __METHOD__, esc_html__( 'Reports pages are read after init. A read before it does not hydrate the registry and answers only what was registered directly.', 'jetpack-premium-analytics-pkg' ), 'jetpack-premium-analytics-$$next-version$$' ); |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | // Latched before the action so a registrant that reads the registry cannot re-enter. |
| 144 | $this->hydrated = true; |
| 145 | |
| 146 | /** |
| 147 | * Fires once when the reports page registry hydrates. |
| 148 | * |
| 149 | * @since $$next-version$$ |
| 150 | * |
| 151 | * @param Reports_Page_Registry $registry The registry being hydrated. |
| 152 | */ |
| 153 | do_action( self::REGISTER_ACTION, $this ); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * The main instance. |
| 158 | * |
| 159 | * @return self |
| 160 | */ |
| 161 | public static function get_instance() { |
| 162 | if ( null === self::$instance ) { |
| 163 | self::$instance = new self(); |
| 164 | } |
| 165 | |
| 166 | return self::$instance; |
| 167 | } |
| 168 | } |