| @@ -24,8 +24,10 @@ | ||
| 24 | 24 | add_action( 'init', [ $this, 'register_post_type' ] ); |
| 25 | 25 | add_action( 'init', [ $this, 'register_rest_fields' ] ); |
| 26 | 26 | add_action( 'save_post_cc_restriction', [ $this, 'save_post' ], 10, 3 ); |
| 27 | 27 | add_filter( 'rest_pre_dispatch', [ $this, 'rest_pre_dispatch' ], 10, 3 ); |
| 28 | + add_filter( 'content_control/sanitize_restriction_settings', [ $this, 'sanitize_restriction_settings' ], 10, 2 ); | |
| 29 | + add_filter( 'content_control/validate_restriction_settings', [ $this, 'validate_restriction_settings' ], 10, 2 ); | |
| 28 | 30 | } |
| 29 | 31 | |
| 30 | 32 | /** |
| 31 | 33 | * Register `restriction` post type. |
| @@ -60,9 +62,13 @@ | ||
| 60 | 62 | 'hierarchical' => false, |
| 61 | 63 | 'can_export' => true, |
| 62 | 64 | 'rewrite' => false, |
| 63 | 65 | 'query_var' => false, |
| 64 | - 'supports' => [ 'title' ], | |
| 66 | + 'supports' => [ | |
| 67 | + 'title', | |
| 68 | + 'excerpt', | |
| 69 | + // 'editor', | |
| 70 | + ], | |
| 65 | 71 | 'show_in_graphql' => false, |
| 66 | 72 | 'capabilities' => [ |
| 67 | 73 | 'create_posts' => $this->container->get_permission( 'edit_restrictions' ), |
| 68 | 74 | 'edit_posts' => $this->container->get_permission( 'edit_restrictions' ), |
| @@ -67,9 +73,8 @@ | ||
| 67 | 73 | 'create_posts' => $this->container->get_permission( 'edit_restrictions' ), |
| 68 | 74 | 'edit_posts' => $this->container->get_permission( 'edit_restrictions' ), |
| 69 | 75 | 'delete_posts' => $this->container->get_permission( 'edit_restrictions' ), |
| 70 | 76 | ], |
| 71 | - | |
| 72 | 77 | ]; |
| 73 | 78 | |
| 74 | 79 | register_post_type( 'cc_restriction', $args ); |
| 75 | 80 | } |
| @@ -79,70 +84,67 @@ | ||
| 79 | 84 | * |
| 80 | 85 | * @return void |
| 81 | 86 | */ |
| 82 | 87 | public function register_rest_fields() { |
| 83 | - register_rest_field( 'cc_restriction', 'title', [ | |
| 84 | - 'get_callback' => function ( $obj ) { | |
| 85 | - // remove get_the_title character encoding escape. | |
| 86 | - remove_filter( 'the_title', 'wptexturize' ); | |
| 87 | - $title = get_the_title( $obj['id'] ); | |
| 88 | - add_filter( 'the_title', 'wptexturize' ); | |
| 89 | - return $title; | |
| 88 | + register_rest_field( 'cc_restriction', 'settings', [ | |
| 89 | + 'get_callback' => function ( $obj, $field, $request ) { | |
| 90 | + $settings = get_post_meta( $obj['id'], 'restriction_settings', true ); | |
| 91 | + | |
| 92 | + // Backfill from content if empty. | |
| 93 | + if ( empty( $settings['customMessage'] ) ) { | |
| 94 | + $settings['customMessage'] = get_post_field( 'post_content', $obj['id'], 'raw' ); | |
| 95 | + } | |
| 96 | + | |
| 97 | + if ( ! empty( $settings['customMessage'] ) ) { | |
| 98 | + // Change output based on context. | |
| 99 | + $settings['customMessage'] = 'edit' === $request->get_param( 'context' ) ? | |
| 100 | + sanitize_post_field( 'post_content', $settings['customMessage'], $obj['id'], 'raw' ) : | |
| 101 | + sanitize_post_field( 'post_content', $settings['customMessage'], $obj['id'], 'display' ); | |
| 102 | + } | |
| 103 | + | |
| 104 | + return $settings; | |
| 90 | 105 | }, |
| 91 | - 'update_callback' => function ( $value, $obj ) { | |
| 92 | - wp_update_post( [ | |
| 93 | - 'ID' => $obj->ID, | |
| 94 | - 'post_title' => $value, | |
| 95 | - ] ); | |
| 96 | - }, | |
| 97 | - 'schema' => [ | |
| 98 | - 'type' => 'string', | |
| 99 | - 'arg_options' => [ | |
| 100 | - 'sanitize_callback' => function ( $value ) { | |
| 101 | - // Make the value safe for storage. | |
| 102 | - return sanitize_text_field( $value ); | |
| 103 | - }, | |
| 104 | - 'validate_callback' => function ( $value ) { | |
| 105 | - // Validate title based on post_title. | |
| 106 | - return is_string( $value ); | |
| 107 | - }, | |
| 108 | - ], | |
| 109 | - ], | |
| 110 | - ] ); | |
| 106 | + 'update_callback' => function ( $value, $obj ) { | |
| 107 | + $custom_message = ! empty( $value['customMessage'] ) ? $value['customMessage'] : ''; | |
| 111 | 108 | |
| 112 | - register_rest_field( 'cc_restriction', 'description', [ | |
| 113 | - 'get_callback' => function ( $obj ) { | |
| 114 | - return get_the_excerpt( $obj['id'] ); | |
| 115 | - }, | |
| 116 | - 'update_callback' => function ( $value, $obj ) { | |
| 109 | + // Save custom message to restriction content for now. | |
| 117 | 110 | wp_update_post( [ |
| 118 | 111 | 'ID' => $obj->ID, |
| 119 | - 'post_excerpt' => sanitize_text_field( $value ), | |
| 112 | + 'post_content' => $custom_message, | |
| 120 | 113 | ] ); |
| 114 | + | |
| 115 | + // Update the field/meta value. | |
| 116 | + update_post_meta( $obj->ID, 'restriction_settings', $value ); | |
| 121 | 117 | }, |
| 122 | - 'schema' => [ | |
| 123 | - 'type' => 'string', | |
| 118 | + 'schema' => [ | |
| 119 | + 'type' => 'object', | |
| 124 | 120 | 'arg_options' => [ |
| 125 | - 'sanitize_callback' => function ( $value ) { | |
| 126 | - // Make the value safe for storage. | |
| 127 | - return sanitize_text_field( $value ); | |
| 121 | + 'sanitize_callback' => function ( $settings, $request ) { | |
| 122 | + /** | |
| 123 | + * Sanitize the restriction settings. | |
| 124 | + * | |
| 125 | + * @param array<string,mixed> $settings The settings to sanitize. | |
| 126 | + * @param int $id The restriction ID. | |
| 127 | + * @param \WP_REST_Request $request The request object. | |
| 128 | + * | |
| 129 | + * @return array<string,mixed> The sanitized settings. | |
| 130 | + */ | |
| 131 | + return apply_filters( 'content_control/sanitize_restriction_settings', $settings, $request->get_param( 'id' ), $request ); | |
| 128 | 132 | }, |
| 129 | - 'validate_callback' => function ( $value ) { | |
| 130 | - // Validate title based on post_title. | |
| 131 | - return is_string( $value ); | |
| 133 | + 'validate_callback' => function ( $settings, $request ) { | |
| 134 | + /** | |
| 135 | + * Validate the restriction settings. | |
| 136 | + * | |
| 137 | + * @param array<string,mixed> $settings The settings to validate. | |
| 138 | + * @param int $id The restriction ID. | |
| 139 | + * @param \WP_REST_Request $request The request object. | |
| 140 | + * | |
| 141 | + * @return bool|\WP_Error True if valid, WP_Error if not. | |
| 142 | + */ | |
| 143 | + return apply_filters( 'content_control/validate_restriction_settings', $settings, $request->get_param( 'id' ), $request ); | |
| 132 | 144 | }, |
| 133 | 145 | ], |
| 134 | 146 | ], |
| 135 | - ] ); | |
| 136 | - | |
| 137 | - register_rest_field( 'cc_restriction', 'settings', [ | |
| 138 | - 'get_callback' => function ( $obj ) { | |
| 139 | - return get_post_meta( $obj['id'], 'restriction_settings', true ); | |
| 140 | - }, | |
| 141 | - 'update_callback' => function ( $value, $obj ) { | |
| 142 | - // Update the field/meta value. | |
| 143 | - update_post_meta( $obj->ID, 'restriction_settings', $value ); | |
| 144 | - }, | |
| 145 | 147 | 'permission_callback' => function () { |
| 146 | 148 | return current_user_can( $this->container->get_permission( 'edit_restrictions' ) ); |
| 147 | 149 | }, |
| 148 | 150 | ] ); |
| @@ -148,14 +150,11 @@ | ||
| 148 | 150 | ] ); |
| 149 | 151 | |
| 150 | 152 | register_rest_field( 'cc_restriction', 'priority', [ |
| 151 | 153 | 'get_callback' => function ( $obj ) { |
| 152 | - $priority = get_post_field( 'menu_order', $obj['id'] ); | |
| 153 | - | |
| 154 | - return $priority >= 0 ? $priority : 0; | |
| 154 | + return (int) get_post_field( 'menu_order', $obj['id'], 'raw' ); | |
| 155 | 155 | }, |
| 156 | 156 | 'update_callback' => function ( $value, $obj ) { |
| 157 | - // Update the posts menu order. | |
| 158 | 157 | wp_update_post( [ |
| 159 | 158 | 'ID' => $obj->ID, |
| 160 | 159 | 'menu_order' => $value, |
| 161 | 160 | ] ); |
| @@ -162,8 +161,19 @@ | ||
| 162 | 161 | }, |
| 163 | 162 | 'permission_callback' => function () { |
| 164 | 163 | return current_user_can( $this->container->get_permission( 'edit_restrictions' ) ); |
| 165 | 164 | }, |
| 165 | + 'schema' => [ | |
| 166 | + 'type' => 'integer', | |
| 167 | + 'arg_options' => [ | |
| 168 | + 'sanitize_callback' => function ( $priority ) { | |
| 169 | + return absint( $priority ); | |
| 170 | + }, | |
| 171 | + 'validate_callback' => function ( $priority ) { | |
| 172 | + return is_int( $priority ); | |
| 173 | + }, | |
| 174 | + ], | |
| 175 | + ], | |
| 166 | 176 | ] ); |
| 167 | 177 | |
| 168 | 178 | register_rest_field( 'cc_restriction', 'data_version', [ |
| 169 | 179 | 'get_callback' => function ( $obj ) { |
| @@ -179,13 +189,45 @@ | ||
| 179 | 189 | ] ); |
| 180 | 190 | } |
| 181 | 191 | |
| 182 | 192 | /** |
| 193 | + * Sanitize restriction settings. | |
| 194 | + * | |
| 195 | + * @param array<string,mixed> $settings The settings to sanitize. | |
| 196 | + * @param int $id The restriction ID. | |
| 197 | + * | |
| 198 | + * @return array<string,mixed> The sanitized settings. | |
| 199 | + */ | |
| 200 | + public function sanitize_restriction_settings( $settings, $id ) { | |
| 201 | + | |
| 202 | + // Sanitize custom message. | |
| 203 | + if ( ! empty( $settings['customMessage'] ) ) { | |
| 204 | + $settings['customMessage'] = sanitize_post_field( 'post_content', $settings['customMessage'], $id, 'db' ); | |
| 205 | + } | |
| 206 | + | |
| 207 | + return $settings; | |
| 208 | + } | |
| 209 | + | |
| 210 | + /** | |
| 211 | + * Validate restriction settings. | |
| 212 | + * | |
| 213 | + * @param array<string,mixed> $settings The settings to validate. | |
| 214 | + * @param int $id The restriction ID. | |
| 215 | + * | |
| 216 | + * @return bool|\WP_Error True if valid, WP_Error if not. | |
| 217 | + */ | |
| 218 | + public function validate_restriction_settings( $settings, $id ) { | |
| 219 | + // TODO Validate all known settings by type. | |
| 220 | + return true; | |
| 221 | + } | |
| 222 | + | |
| 223 | + | |
| 224 | + /** | |
| 183 | 225 | * Add data version meta to new restrictions. |
| 184 | 226 | * |
| 185 | - * @param int $post_id Post ID. | |
| 186 | - * @param WP_Post $post Post object. | |
| 187 | - * @param bool $update Whether this is an existing post being updated or not. | |
| 227 | + * @param int $post_id Post ID. | |
| 228 | + * @param \WP_Post $post Post object. | |
| 229 | + * @param bool $update Whether this is an existing post being updated or not. | |
| 188 | 230 | * |
| 189 | 231 | * @return void |
| 190 | 232 | */ |
| 191 | 233 | public function save_post( $post_id, $post, $update ) { |
| @@ -198,11 +240,11 @@ | ||
| 198 | 240 | |
| 199 | 241 | /** |
| 200 | 242 | * Prevent access to restrictions endpoint. |
| 201 | 243 | * |
| 202 | - * @param mixed $result Response to replace the requested version with. | |
| 203 | - * @param WP_REST_Server $server Server instance. | |
| 204 | - * @param WP_REST_Request $request Request used to generate the response. | |
| 244 | + * @param mixed $result Response to replace the requested version with. | |
| 245 | + * @param \WP_REST_Server $server Server instance. | |
| 246 | + * @param \WP_REST_Request<array<string,mixed>> $request Request used to generate the response. | |
| 205 | 247 | * @return mixed |
| 206 | 248 | */ |
| 207 | 249 | public function rest_pre_dispatch( $result, $server, $request ) { |
| 208 | 250 | // Get the route being requested. |