| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | namespace Automattic\Jetpack\PremiumAnalytics; |
| 12 | |
| 13 | require_once __DIR__ . '/dashboard-grammar.php'; |
| 14 | require_once __DIR__ . '/class-single-report-page.php'; |
| 15 | require_once __DIR__ . '/class-single-report-page-registry.php'; |
| 16 | |
| 17 | |
| 18 | if ( ! function_exists( __NAMESPACE__ . '\\get_dashboard_default_widget_instance' ) ) { |
| 19 | require_once __DIR__ . '/dashboard-layout.php'; |
| 20 | } |
| 21 | |
| 22 | |
| 23 | if ( ! function_exists( __NAMESPACE__ . '\\sanitize_widget_icon' ) ) { |
| 24 | require_once __DIR__ . '/widget-types.php'; |
| 25 | } |
| 26 | |
| 27 | require_once __DIR__ . '/rest-namespace.php'; |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | const SINGLE_REPORT_PAGE_DEFAULT_LAYOUT_FILTER = 'jetpack_premium_analytics_single_report_page_default_layout'; |
| 33 | |
| 34 | |
| 35 | add_filter( SINGLE_REPORT_PAGE_DEFAULT_LAYOUT_FILTER, __NAMESPACE__ . '\\resolve_former_widget_types_in_default_layout', 99 ); |
| 36 | add_filter( SINGLE_REPORT_PAGE_DEFAULT_LAYOUT_FILTER, __NAMESPACE__ . '\\remove_unsupported_default_layout_items', 100 ); |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | function register_single_report_page( $dashboard_name, $id, $args = array() ) { |
| 49 | return Single_Report_Page_Registry::get_instance()->register( $dashboard_name, $id, $args ); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | function get_registered_single_report_page( $dashboard_name, $id ) { |
| 62 | return Single_Report_Page_Registry::get_instance()->get_registered( $dashboard_name, $id ); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | function get_available_single_report_pages( $dashboard_name ) { |
| 74 | return Single_Report_Page_Registry::get_instance()->get_available_pages( $dashboard_name ); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | function get_available_single_report_page_records() { |
| 83 | $registry = Single_Report_Page_Registry::get_instance(); |
| 84 | |
| 85 | if ( empty( $registry->get_all_registered( DASHBOARD_NAME ) ) ) { |
| 86 | return null; |
| 87 | } |
| 88 | |
| 89 | return array_map( |
| 90 | static function ( Single_Report_Page $page ) { |
| 91 | return $page->to_array(); |
| 92 | }, |
| 93 | $registry->get_available_pages( DASHBOARD_NAME ) |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | function configure_single_report_pages_script_data() { |
| 101 | add_filter( 'jetpack_admin_js_script_data', __NAMESPACE__ . '\\inject_single_report_pages_script_data', 20 ); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | function inject_single_report_pages_script_data( array $data ): array { |
| 111 | if ( ! isset( $data['premium_analytics'] ) || ! is_array( $data['premium_analytics'] ) ) { |
| 112 | $data['premium_analytics'] = array(); |
| 113 | } |
| 114 | |
| 115 | $pages = get_available_single_report_page_records(); |
| 116 | |
| 117 | if ( null !== $pages ) { |
| 118 | $data['premium_analytics']['single_report_pages'] = $pages; |
| 119 | } |
| 120 | |
| 121 | return $data; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | function check_single_report_pages_permission() { |
| 130 | return Capabilities::current_user_can_view_analytics(); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | function get_single_report_page_controls_schema() { |
| 139 | return array( |
| 140 | 'type' => array( 'object', 'boolean', 'null' ), |
| 141 | 'properties' => array( |
| 142 | 'presets' => array( |
| 143 | 'type' => 'array', |
| 144 | 'items' => array( 'type' => 'string' ), |
| 145 | ), |
| 146 | ), |
| 147 | ); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | function get_single_report_page_schema() { |
| 156 | return array( |
| 157 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 158 | 'title' => 'jetpack-premium-analytics-single-report-page', |
| 159 | 'type' => 'object', |
| 160 | 'properties' => array( |
| 161 | 'id' => array( 'type' => 'string' ), |
| 162 | 'path' => array( 'type' => 'string' ), |
| 163 | 'label' => array( 'type' => 'string' ), |
| 164 | 'dashboard_section' => array( 'type' => array( 'string', 'null' ) ), |
| 165 | 'feature' => array( 'type' => array( 'string', 'null' ) ), |
| 166 | 'scope_param' => array( 'type' => 'string' ), |
| 167 | 'id_pattern' => array( 'type' => 'string' ), |
| 168 | 'icon' => array( 'type' => array( 'string', 'null' ) ), |
| 169 | 'controls' => get_single_report_page_controls_schema(), |
| 170 | 'reports_page' => array( 'type' => array( 'string', 'null' ) ), |
| 171 | 'preferences_scope' => array( 'type' => 'string' ), |
| 172 | 'default_layout' => array( |
| 173 | 'type' => 'array', |
| 174 | 'items' => array( 'type' => 'object' ), |
| 175 | ), |
| 176 | 'sections' => array( |
| 177 | 'type' => 'array', |
| 178 | 'items' => array( |
| 179 | 'type' => 'object', |
| 180 | 'properties' => array( |
| 181 | 'slug' => array( 'type' => 'string' ), |
| 182 | 'label' => array( 'type' => 'string' ), |
| 183 | 'default_layout' => array( |
| 184 | 'type' => 'array', |
| 185 | 'items' => array( 'type' => 'object' ), |
| 186 | ), |
| 187 | 'controls' => get_single_report_page_controls_schema(), |
| 188 | ), |
| 189 | ), |
| 190 | ), |
| 191 | ), |
| 192 | ); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | function get_single_report_pages_response( $request ) { |
| 202 | $pages = array_map( |
| 203 | static function ( Single_Report_Page $page ) { |
| 204 | return $page->to_array(); |
| 205 | }, |
| 206 | get_available_single_report_pages( $request['name'] ) |
| 207 | ); |
| 208 | |
| 209 | return rest_ensure_response( $pages ); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | function register_single_report_pages_rest_routes() { |
| 216 | register_rest_route( |
| 217 | DASHBOARD_REST_NAMESPACE, |
| 218 | '/dashboards/(?P<name>' . get_dashboard_name_pattern() . ')/single-report-pages', |
| 219 | array( |
| 220 | array( |
| 221 | |
| 222 | 'methods' => \WP_REST_Server::READABLE, |
| 223 | 'callback' => __NAMESPACE__ . '\\get_single_report_pages_response', |
| 224 | 'permission_callback' => __NAMESPACE__ . '\\check_single_report_pages_permission', |
| 225 | 'args' => array( |
| 226 | 'name' => array( |
| 227 | 'description' => __( 'Dashboard identifier as produced by the build pipeline.', 'jetpack-premium-analytics-pkg' ), |
| 228 | 'type' => 'string', |
| 229 | ), |
| 230 | ), |
| 231 | ), |
| 232 | 'schema' => __NAMESPACE__ . '\\get_single_report_page_schema', |
| 233 | ) |
| 234 | ); |
| 235 | } |