| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | namespace Automattic\Jetpack\PremiumAnalytics; |
| 9 | |
| 10 | require_once __DIR__ . '/dashboard-layout.php'; |
| 11 | require_once __DIR__ . '/dashboard-grammar.php'; |
| 12 | require_once __DIR__ . '/class-dashboard-section.php'; |
| 13 | require_once __DIR__ . '/class-dashboard-section-registry.php'; |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | const WOOCOMMERCE_DASHBOARD_SECTION_AVAILABLE_FILTER = 'jetpack_premium_analytics_woocommerce_dashboard_section_available'; |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | function register_dashboard_section( $dashboard_name, $id, $args = array() ) { |
| 29 | return Dashboard_Section_Registry::get_instance()->register( $dashboard_name, $id, $args ); |
| 30 | } |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | function get_registered_dashboard_section( $dashboard_name, $id ) { |
| 40 | return Dashboard_Section_Registry::get_instance()->get_registered( $dashboard_name, $id ); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | function get_available_dashboard_sections( $dashboard_name ) { |
| 50 | return Dashboard_Section_Registry::get_instance()->get_available_sections( $dashboard_name ); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | function is_woocommerce_dashboard_section_available() { |
| 59 | $is_available = class_exists( 'WooCommerce' ) || function_exists( 'WC' ); |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | return (bool) apply_filters( WOOCOMMERCE_DASHBOARD_SECTION_AVAILABLE_FILTER, $is_available ); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | function get_woocommerce_dashboard_section_default_layout() { |
| 75 | return get_dashboard_default_layout_for( 'woocommerce/store' ); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | function register_default_dashboard_sections() { |
| 84 | $registry = Dashboard_Section_Registry::get_instance(); |
| 85 | |
| 86 | $sections = array( |
| 87 | 'analytics/traffic' => array( |
| 88 | 'label' => __( 'Traffic', 'jetpack-premium-analytics' ), |
| 89 | 'order' => 10, |
| 90 | 'default_layout' => static function () { |
| 91 | return get_dashboard_default_layout_for( 'analytics/traffic' ); |
| 92 | }, |
| 93 | ), |
| 94 | 'analytics/insights' => array( |
| 95 | 'label' => __( 'Insights', 'jetpack-premium-analytics' ), |
| 96 | 'order' => 20, |
| 97 | 'default_layout' => static function () { |
| 98 | return get_dashboard_default_layout_for( 'analytics/insights' ); |
| 99 | }, |
| 100 | ), |
| 101 | 'analytics/subscribers' => array( |
| 102 | 'label' => __( 'Subscribers', 'jetpack-premium-analytics' ), |
| 103 | 'order' => 30, |
| 104 | 'default_layout' => static function () { |
| 105 | return get_dashboard_default_layout_for( 'analytics/subscribers' ); |
| 106 | }, |
| 107 | ), |
| 108 | 'woocommerce/store' => array( |
| 109 | 'label' => __( 'Store', 'jetpack-premium-analytics' ), |
| 110 | 'order' => 40, |
| 111 | 'is_available' => __NAMESPACE__ . '\\is_woocommerce_dashboard_section_available', |
| 112 | 'default_layout' => __NAMESPACE__ . '\\get_woocommerce_dashboard_section_default_layout', |
| 113 | ), |
| 114 | ); |
| 115 | |
| 116 | foreach ( $sections as $id => $args ) { |
| 117 | if ( ! $registry->is_registered( DASHBOARD_NAME, $id ) ) { |
| 118 | register_dashboard_section( DASHBOARD_NAME, $id, $args ); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | function bootstrap_dashboard_sections() { |
| 129 | if ( did_action( 'init' ) ) { |
| 130 | register_default_dashboard_sections(); |
| 131 | } else { |
| 132 | add_action( 'init', __NAMESPACE__ . '\\register_default_dashboard_sections' ); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | function check_dashboard_sections_permission() { |
| 142 | return current_user_can( 'manage_options' ); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | function get_available_dashboard_section_for_route( $dashboard_name, $section_id ) { |
| 153 | $section = get_registered_dashboard_section( $dashboard_name, $section_id ); |
| 154 | |
| 155 | if ( ! $section ) { |
| 156 | return new \WP_Error( |
| 157 | 'dashboard_section_not_found', |
| 158 | __( 'Dashboard section not found.', 'jetpack-premium-analytics' ), |
| 159 | array( 'status' => 404 ) |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | if ( ! $section->is_available() ) { |
| 164 | return new \WP_Error( |
| 165 | 'dashboard_section_unavailable', |
| 166 | __( 'Dashboard section is not available.', 'jetpack-premium-analytics' ), |
| 167 | array( 'status' => 404 ) |
| 168 | ); |
| 169 | } |
| 170 | |
| 171 | return $section; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | function get_dashboard_sections_response( $request ) { |
| 181 | $sections = array_map( |
| 182 | static function ( Dashboard_Section $section ) { |
| 183 | return $section->to_array(); |
| 184 | }, |
| 185 | get_available_dashboard_sections( $request['name'] ) |
| 186 | ); |
| 187 | |
| 188 | return rest_ensure_response( $sections ); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | function get_dashboard_section_default_layout_response( $request ) { |
| 198 | $section = get_available_dashboard_section_for_route( $request['name'], $request['section'] ); |
| 199 | |
| 200 | if ( is_wp_error( $section ) ) { |
| 201 | return $section; |
| 202 | } |
| 203 | |
| 204 | return rest_ensure_response( $section->get_default_layout() ); |
| 205 | } |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | function register_dashboard_sections_rest_routes() { |
| 213 | register_rest_route( |
| 214 | DASHBOARD_REST_NAMESPACE, |
| 215 | '/dashboards/(?P<name>' . get_dashboard_name_pattern() . ')/sections', |
| 216 | array( |
| 217 | 'methods' => \WP_REST_Server::READABLE, |
| 218 | 'callback' => __NAMESPACE__ . '\\get_dashboard_sections_response', |
| 219 | 'permission_callback' => __NAMESPACE__ . '\\check_dashboard_sections_permission', |
| 220 | 'args' => array( |
| 221 | 'name' => array( |
| 222 | 'description' => __( 'Dashboard identifier as produced by the build pipeline.', 'jetpack-premium-analytics' ), |
| 223 | 'type' => 'string', |
| 224 | ), |
| 225 | ), |
| 226 | ) |
| 227 | ); |
| 228 | |
| 229 | register_rest_route( |
| 230 | DASHBOARD_REST_NAMESPACE, |
| 231 | '/dashboards/(?P<name>' . get_dashboard_name_pattern() . ')/sections/(?P<section>' . get_dashboard_section_id_pattern() . ')/default-layout', |
| 232 | array( |
| 233 | 'methods' => \WP_REST_Server::READABLE, |
| 234 | 'callback' => __NAMESPACE__ . '\\get_dashboard_section_default_layout_response', |
| 235 | 'permission_callback' => __NAMESPACE__ . '\\check_dashboard_sections_permission', |
| 236 | 'args' => array( |
| 237 | 'name' => array( |
| 238 | 'description' => __( 'Dashboard identifier as produced by the build pipeline.', 'jetpack-premium-analytics' ), |
| 239 | 'type' => 'string', |
| 240 | ), |
| 241 | 'section' => array( |
| 242 | 'description' => __( 'Dashboard section identifier.', 'jetpack-premium-analytics' ), |
| 243 | 'type' => 'string', |
| 244 | ), |
| 245 | ), |
| 246 | ) |
| 247 | ); |
| 248 | } |
| 249 | |
| 250 | bootstrap_dashboard_sections(); |