Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
50.00% |
22 / 44 |
|
28.57% |
2 / 7 |
CRAP | n/a |
0 / 0 |
|
| wpcom_performance_url_rest_field | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
1 | |||
| filter_wpcom_performance_report_url | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
30 | |||
| wpcom_performance_report_url_get_value | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| wpcom_performance_report_url_update_value | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| wpcom_performance_report_url_settings_field | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| wpcom_performance_url_get_options_v1_api | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| wpcom_performance_url_update_options_v1_api | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Adds the generated performance report url for each page and also in v1 site settings payload for homepage. |
| 4 | * |
| 5 | * @package performance-profiler |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Adds the generated performance report url for each page. |
| 10 | */ |
| 11 | function wpcom_performance_url_rest_field() { |
| 12 | register_rest_field( |
| 13 | 'page', |
| 14 | 'wpcom_performance_report_url', |
| 15 | array( |
| 16 | 'get_callback' => 'wpcom_performance_report_url_get_value', |
| 17 | 'update_callback' => 'wpcom_performance_report_url_update_value', |
| 18 | 'schema' => array( |
| 19 | 'type' => 'string', |
| 20 | 'description' => __( 'The performance report URL for this page.', 'wpcomsh' ), |
| 21 | 'default' => '', |
| 22 | ), |
| 23 | ) |
| 24 | ); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Filter the performance report URL field. |
| 29 | * |
| 30 | * @param WP_REST_Response $response The response object. |
| 31 | * @param WP_Post $post The post object. |
| 32 | * @param WP_REST_Request $request The request object. |
| 33 | * @return WP_REST_Response |
| 34 | */ |
| 35 | function filter_wpcom_performance_report_url( $response, $post, $request ) { |
| 36 | $fields = $request->get_param( '_fields' ); |
| 37 | |
| 38 | if ( is_string( $fields ) ) { |
| 39 | $fields = explode( ',', $fields ); |
| 40 | } |
| 41 | |
| 42 | if ( ! is_array( $fields ) ) { |
| 43 | $fields = array(); |
| 44 | } |
| 45 | |
| 46 | if ( empty( $fields ) || ! in_array( 'wpcom_performance_report_url', $fields, true ) ) { |
| 47 | unset( $response->data['wpcom_performance_report_url'] ); |
| 48 | } |
| 49 | |
| 50 | return $response; |
| 51 | } |
| 52 | |
| 53 | add_filter( 'rest_prepare_page', 'filter_wpcom_performance_report_url', 10, 3 ); |
| 54 | |
| 55 | /** |
| 56 | * Get the performance report URL callback. |
| 57 | * |
| 58 | * @param array $object The object being requested. |
| 59 | * @return string|null The performance report URL. |
| 60 | */ |
| 61 | function wpcom_performance_report_url_get_value( $object ) { |
| 62 | return get_post_meta( $object['id'], '_wpcom_performance_report_url', true ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Update callback for the performance report URL. |
| 67 | * |
| 68 | * @param mixed $value The new value of the field. |
| 69 | * @param object $object The object being updated. |
| 70 | * @return bool|int|null |
| 71 | */ |
| 72 | function wpcom_performance_report_url_update_value( $value, $object ) { |
| 73 | if ( ! $value || ! is_string( $value ) ) { |
| 74 | return; |
| 75 | } |
| 76 | return update_post_meta( $object->ID, '_wpcom_performance_report_url', sanitize_url( $value ) ); |
| 77 | } |
| 78 | |
| 79 | add_action( 'rest_api_init', 'wpcom_performance_url_rest_field' ); |
| 80 | |
| 81 | /** |
| 82 | * Registers the performance report URL settings field. |
| 83 | */ |
| 84 | function wpcom_performance_report_url_settings_field() { |
| 85 | register_setting( |
| 86 | 'wpcom_performance_profiler', |
| 87 | 'wpcom_performance_report_url', |
| 88 | array( |
| 89 | 'type' => 'string', |
| 90 | 'description' => __( 'The performance report URL for this site.', 'wpcomsh' ), |
| 91 | 'default' => '', |
| 92 | ) |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | add_action( 'rest_api_init', 'wpcom_performance_report_url_settings_field' ); |
| 97 | |
| 98 | /** |
| 99 | * Adds settings to the v1 API site settings endpoint. |
| 100 | * |
| 101 | * @param array $settings A single site's settings. |
| 102 | * @return array |
| 103 | */ |
| 104 | function wpcom_performance_url_get_options_v1_api( $settings ) { |
| 105 | $settings['wpcom_performance_report_url'] = get_option( 'wpcom_performance_report_url', '' ); |
| 106 | return $settings; |
| 107 | } |
| 108 | |
| 109 | add_filter( 'site_settings_endpoint_get', 'wpcom_performance_url_get_options_v1_api' ); |
| 110 | |
| 111 | /** |
| 112 | * Updates settings via public-api.wordpress.com. |
| 113 | * |
| 114 | * @param array $input Associative array of site settings to be updated. |
| 115 | * Cast and filtered based on documentation. |
| 116 | * @param array $unfiltered_input Associative array of site settings to be updated. |
| 117 | * Neither cast nor filtered. Contains raw input. |
| 118 | * @return array |
| 119 | */ |
| 120 | function wpcom_performance_url_update_options_v1_api( $input, $unfiltered_input ) { |
| 121 | if ( isset( $unfiltered_input['wpcom_performance_report_url'] ) ) { |
| 122 | $input['wpcom_performance_report_url'] = sanitize_url( $unfiltered_input['wpcom_performance_report_url'] ); |
| 123 | } |
| 124 | |
| 125 | return $input; |
| 126 | } |
| 127 | |
| 128 | add_filter( 'rest_api_update_site_settings', 'wpcom_performance_url_update_options_v1_api', 10, 2 ); |