| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | namespace Automattic\Jetpack\PremiumAnalytics; |
| 12 | |
| 13 | require_once __DIR__ . '/dashboard-layout.php'; |
| 14 | require_once __DIR__ . '/dashboard-grammar.php'; |
| 15 | require_once __DIR__ . '/rest-namespace.php'; |
| 16 | require_once __DIR__ . '/class-dashboard-section.php'; |
| 17 | require_once __DIR__ . '/class-dashboard-section-registry.php'; |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | function register_dashboard_section( $dashboard_name, $id, $args = array() ) { |
| 28 | return Dashboard_Section_Registry::get_instance()->register( $dashboard_name, $id, $args ); |
| 29 | } |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | function get_registered_dashboard_section( $dashboard_name, $id ) { |
| 39 | return Dashboard_Section_Registry::get_instance()->get_registered( $dashboard_name, $id ); |
| 40 | } |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | function get_available_dashboard_sections( $dashboard_name ) { |
| 49 | return Dashboard_Section_Registry::get_instance()->get_available_sections( $dashboard_name ); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | function get_available_dashboard_section_slugs() { |
| 64 | $registry = Dashboard_Section_Registry::get_instance(); |
| 65 | |
| 66 | if ( empty( $registry->get_all_registered( DASHBOARD_NAME ) ) ) { |
| 67 | return null; |
| 68 | } |
| 69 | |
| 70 | return array_map( |
| 71 | static function ( Dashboard_Section $section ) { |
| 72 | return $section->slug; |
| 73 | }, |
| 74 | $registry->get_available_sections( DASHBOARD_NAME ) |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | function get_plan_date_limited_section_slugs() { |
| 86 | $slugs = array(); |
| 87 | |
| 88 | foreach ( Dashboard_Section_Registry::get_instance()->get_available_sections( DASHBOARD_NAME ) as $section ) { |
| 89 | if ( $section->follows_plan_date_limits ) { |
| 90 | $slugs[] = $section->slug; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return $slugs; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | function configure_dashboard_sections_script_data() { |
| 105 | add_filter( 'jetpack_admin_js_script_data', __NAMESPACE__ . '\\inject_dashboard_sections_script_data', 20 ); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | function inject_dashboard_sections_script_data( array $data ): array { |
| 120 | if ( ! isset( $data['premium_analytics'] ) || ! is_array( $data['premium_analytics'] ) ) { |
| 121 | $data['premium_analytics'] = array(); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | $data['premium_analytics']['can_view_stats'] = Capabilities::current_user_can_view_stats(); |
| 126 | |
| 127 | |
| 128 | $data['premium_analytics']['classic_stats_url'] = get_classic_stats_url(); |
| 129 | |
| 130 | $sections = get_available_dashboard_section_slugs(); |
| 131 | |
| 132 | if ( null !== $sections ) { |
| 133 | $data['premium_analytics']['sections'] = $sections; |
| 134 | $data['premium_analytics']['plan_date_limited_sections'] = get_plan_date_limited_section_slugs(); |
| 135 | } |
| 136 | |
| 137 | return $data; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | function get_classic_stats_url(): ?string { |
| 151 | $url = function_exists( 'menu_page_url' ) ? menu_page_url( 'stats', false ) : ''; |
| 152 | |
| 153 | return '' === $url ? null : $url; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | function check_dashboard_sections_permission() { |
| 162 | return Capabilities::current_user_can_view_analytics(); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | function get_available_dashboard_section_for_route( $dashboard_name, $section_id ) { |
| 173 | $section = get_registered_dashboard_section( $dashboard_name, $section_id ); |
| 174 | |
| 175 | if ( ! $section ) { |
| 176 | return new \WP_Error( |
| 177 | 'dashboard_section_not_found', |
| 178 | __( 'Dashboard section not found.', 'jetpack-premium-analytics-pkg' ), |
| 179 | array( 'status' => 404 ) |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | if ( ! $section->is_available() ) { |
| 184 | return new \WP_Error( |
| 185 | 'dashboard_section_unavailable', |
| 186 | __( 'Dashboard section is not available.', 'jetpack-premium-analytics-pkg' ), |
| 187 | array( 'status' => 404 ) |
| 188 | ); |
| 189 | } |
| 190 | |
| 191 | return $section; |
| 192 | } |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | function get_dashboard_section_schema() { |
| 204 | return array( |
| 205 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 206 | 'title' => 'jetpack-premium-analytics-dashboard-section', |
| 207 | 'type' => 'object', |
| 208 | 'properties' => array( |
| 209 | 'id' => array( |
| 210 | 'description' => __( 'Namespaced section identifier.', 'jetpack-premium-analytics-pkg' ), |
| 211 | 'type' => 'string', |
| 212 | 'readonly' => true, |
| 213 | ), |
| 214 | 'slug' => array( |
| 215 | 'description' => __( 'URL-facing section slug, derived from the identifier.', 'jetpack-premium-analytics-pkg' ), |
| 216 | 'type' => 'string', |
| 217 | 'readonly' => true, |
| 218 | ), |
| 219 | 'label' => array( |
| 220 | 'description' => __( 'Translated display label, naming the section tab.', 'jetpack-premium-analytics-pkg' ), |
| 221 | 'type' => 'string', |
| 222 | 'readonly' => true, |
| 223 | ), |
| 224 | 'title' => array( |
| 225 | 'description' => __( 'Translated section heading, distinct from the tab label. Null falls back to the label.', 'jetpack-premium-analytics-pkg' ), |
| 226 | 'type' => array( 'string', 'null' ), |
| 227 | 'readonly' => true, |
| 228 | ), |
| 229 | 'order' => array( |
| 230 | 'description' => __( 'Sort order, ascending.', 'jetpack-premium-analytics-pkg' ), |
| 231 | 'type' => 'integer', |
| 232 | 'readonly' => true, |
| 233 | ), |
| 234 | 'date_filter' => array( |
| 235 | 'description' => __( 'Which shape the section date filter takes: the rolling date range, or all time plus single years.', 'jetpack-premium-analytics-pkg' ), |
| 236 | 'type' => 'string', |
| 237 | 'enum' => Dashboard_Section::DATE_FILTERS, |
| 238 | 'default' => Dashboard_Section::DATE_FILTER_RANGE, |
| 239 | 'readonly' => true, |
| 240 | ), |
| 241 | 'date_filter_options' => array( |
| 242 | 'description' => __( 'What the section date filter supports, and where it renders.', 'jetpack-premium-analytics-pkg' ), |
| 243 | 'type' => 'object', |
| 244 | 'properties' => array( |
| 245 | 'with_date_comparison' => array( |
| 246 | 'description' => __( 'Whether the section supports period-over-period comparison at all. When false, no widget in the section receives comparison parameters.', 'jetpack-premium-analytics-pkg' ), |
| 247 | 'type' => 'boolean', |
| 248 | 'default' => true, |
| 249 | ), |
| 250 | 'with_header_date_control' => array( |
| 251 | 'description' => __( 'Whether the section header renders the date control. When false, the section widgets host their own.', 'jetpack-premium-analytics-pkg' ), |
| 252 | 'type' => 'boolean', |
| 253 | 'default' => true, |
| 254 | ), |
| 255 | 'with_header_interval_control' => array( |
| 256 | 'description' => __( 'Whether the section header renders the chart interval control. When false, the section charts host their own.', 'jetpack-premium-analytics-pkg' ), |
| 257 | 'type' => 'boolean', |
| 258 | 'default' => true, |
| 259 | ), |
| 260 | ), |
| 261 | 'readonly' => true, |
| 262 | ), |
| 263 | 'requires_sync' => array( |
| 264 | 'description' => __( 'Whether the section\'s numbers stay incomplete until the analytics initial full sync has finished.', 'jetpack-premium-analytics-pkg' ), |
| 265 | 'type' => 'boolean', |
| 266 | 'default' => false, |
| 267 | 'readonly' => true, |
| 268 | ), |
| 269 | 'notice' => array( |
| 270 | 'description' => __( 'Message shown above the section widgets, or null when there is none.', 'jetpack-premium-analytics-pkg' ), |
| 271 | 'type' => array( 'string', 'null' ), |
| 272 | 'default' => null, |
| 273 | 'readonly' => true, |
| 274 | ), |
| 275 | 'default_layout' => array( |
| 276 | 'description' => __( 'Bundled default widget layout.', 'jetpack-premium-analytics-pkg' ), |
| 277 | 'type' => 'array', |
| 278 | 'items' => array( 'type' => 'object' ), |
| 279 | 'readonly' => true, |
| 280 | ), |
| 281 | 'insertable_widget_types' => array( |
| 282 | 'description' => __( 'Widget types the section inserter offers on top of its default layout.', 'jetpack-premium-analytics-pkg' ), |
| 283 | 'type' => 'array', |
| 284 | 'items' => array( 'type' => 'string' ), |
| 285 | 'default' => array(), |
| 286 | 'readonly' => true, |
| 287 | ), |
| 288 | ), |
| 289 | ); |
| 290 | } |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | function get_dashboard_sections_response( $request ) { |
| 299 | $sections = array_map( |
| 300 | static function ( Dashboard_Section $section ) { |
| 301 | return $section->to_array(); |
| 302 | }, |
| 303 | get_available_dashboard_sections( $request['name'] ) |
| 304 | ); |
| 305 | |
| 306 | return rest_ensure_response( $sections ); |
| 307 | } |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | function get_dashboard_section_default_layout_response( $request ) { |
| 316 | $section = get_available_dashboard_section_for_route( $request['name'], $request['section'] ); |
| 317 | |
| 318 | if ( is_wp_error( $section ) ) { |
| 319 | return $section; |
| 320 | } |
| 321 | |
| 322 | return rest_ensure_response( $section->get_default_layout() ); |
| 323 | } |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | function register_dashboard_sections_rest_routes() { |
| 331 | register_rest_route( |
| 332 | DASHBOARD_REST_NAMESPACE, |
| 333 | '/dashboards/(?P<name>' . get_dashboard_name_pattern() . ')/sections', |
| 334 | array( |
| 335 | array( |
| 336 | |
| 337 | 'methods' => \WP_REST_Server::READABLE, |
| 338 | 'callback' => __NAMESPACE__ . '\\get_dashboard_sections_response', |
| 339 | 'permission_callback' => __NAMESPACE__ . '\\check_dashboard_sections_permission', |
| 340 | 'args' => array( |
| 341 | 'name' => array( |
| 342 | 'description' => __( 'Dashboard identifier as produced by the build pipeline.', 'jetpack-premium-analytics-pkg' ), |
| 343 | 'type' => 'string', |
| 344 | ), |
| 345 | ), |
| 346 | ), |
| 347 | 'schema' => __NAMESPACE__ . '\\get_dashboard_section_schema', |
| 348 | ) |
| 349 | ); |
| 350 | |
| 351 | register_rest_route( |
| 352 | DASHBOARD_REST_NAMESPACE, |
| 353 | '/dashboards/(?P<name>' . get_dashboard_name_pattern() . ')/sections/(?P<section>' . get_dashboard_section_id_pattern() . ')/default-layout', |
| 354 | array( |
| 355 | 'methods' => \WP_REST_Server::READABLE, |
| 356 | 'callback' => __NAMESPACE__ . '\\get_dashboard_section_default_layout_response', |
| 357 | 'permission_callback' => __NAMESPACE__ . '\\check_dashboard_sections_permission', |
| 358 | 'args' => array( |
| 359 | 'name' => array( |
| 360 | 'description' => __( 'Dashboard identifier as produced by the build pipeline.', 'jetpack-premium-analytics-pkg' ), |
| 361 | 'type' => 'string', |
| 362 | ), |
| 363 | 'section' => array( |
| 364 | 'description' => __( 'Dashboard section identifier.', 'jetpack-premium-analytics-pkg' ), |
| 365 | 'type' => 'string', |
| 366 | ), |
| 367 | ), |
| 368 | ) |
| 369 | ); |
| 370 | } |