Skip to content

Commit

Permalink
add terms_and_condition field in font collections
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbenedetto committed Feb 21, 2024
1 parent 99e955e commit 7c50642
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 42 deletions.
1 change: 1 addition & 0 deletions src/wp-includes/fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function wp_print_font_faces( $fonts = array() ) {
* or a string containing the path or URL to a JSON file containing the font collection.
* @type array $categories Optional. Array of categories, each with a name and slug, that are used by the
* fonts in the collection. Default empty.
* @type string $terms_and_conditions Optional. The terms and conditions for using the font collection. Default empty.
* }
* @return WP_Font_Collection|WP_Error A font collection if it was registered
* successfully, or WP_Error object on failure.
Expand Down
18 changes: 12 additions & 6 deletions src/wp-includes/fonts/class-wp-font-collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ public function get_data() {

// Set defaults for optional properties.
$defaults = array(
'description' => '',
'categories' => array(),
'description' => '',
'categories' => array(),
'terms_and_conditions' => '',
);

return wp_parse_args( $this->data, $defaults );
Expand Down Expand Up @@ -143,6 +144,10 @@ private function load_from_json( $file_or_url ) {
$data['categories'] = $this->data['categories'];
}

if ( isset( $this->data['terms_and_conditions'] ) ) {
$data['terms_and_conditions'] = $this->data['terms_and_conditions'];
}

return $data;
}

Expand Down Expand Up @@ -246,9 +251,9 @@ private function sanitize_and_validate_data( $data, $required_properties = array
*/
private static function get_sanitization_schema() {
return array(
'name' => 'sanitize_text_field',
'description' => 'sanitize_text_field',
'font_families' => array(
'name' => 'sanitize_text_field',
'description' => 'sanitize_text_field',
'font_families' => array(
array(
'font_family_settings' => array(
'name' => 'sanitize_text_field',
Expand Down Expand Up @@ -284,12 +289,13 @@ private static function get_sanitization_schema() {
'categories' => array( 'sanitize_title' ),
),
),
'categories' => array(
'categories' => array(
array(
'name' => 'sanitize_text_field',
'slug' => 'sanitize_title',
),
),
'terms_and_conditions' => 'sanitize_text_field',
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ public function get_item( $request ) {
}

/**
* Prepare a single collection output for response.
*
* @since 6.5.0
*
* @param WP_Font_Collection $item Font collection object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
* Prepare a single collection output for response.
*
* @since 6.5.0
*
* @param WP_Font_Collection $item Font collection object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function prepare_item_for_response( $item, $request ) {
$fields = $this->get_fields_for_response( $request );
$data = array();
Expand All @@ -167,7 +167,7 @@ public function prepare_item_for_response( $item, $request ) {
}

// If any data fields are requested, get the collection data.
$data_fields = array( 'name', 'description', 'font_families', 'categories' );
$data_fields = array( 'name', 'description', 'font_families', 'categories', 'terms_and_conditions' );
if ( ! empty( array_intersect( $fields, $data_fields ) ) ) {
$collection_data = $item->get_data();
if ( is_wp_error( $collection_data ) ) {
Expand Down Expand Up @@ -222,32 +222,37 @@ public function get_item_schema() {
'title' => 'font-collection',
'type' => 'object',
'properties' => array(
'slug' => array(
'slug' => array(
'description' => __( 'Unique identifier for the font collection.' ),
'type' => 'string',
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
'name' => array(
'name' => array(
'description' => __( 'The name for the font collection.' ),
'type' => 'string',
'context' => array( 'view', 'edit', 'embed' ),
),
'description' => array(
'description' => array(
'description' => __( 'The description for the font collection.' ),
'type' => 'string',
'context' => array( 'view', 'edit', 'embed' ),
),
'font_families' => array(
'font_families' => array(
'description' => __( 'The font families for the font collection.' ),
'type' => 'array',
'context' => array( 'view', 'edit', 'embed' ),
),
'categories' => array(
'categories' => array(
'description' => __( 'The categories for the font collection.' ),
'type' => 'array',
'context' => array( 'view', 'edit', 'embed' ),
),
'terms_and_conditions' => array(
'description' => __( 'The terms and conditions for the font collection.' ),
'type' => 'string',
'context' => array( 'view', 'edit', 'embed' ),
),
),
);

Expand Down
47 changes: 26 additions & 21 deletions tests/phpunit/tests/fonts/font-library/wpFontCollection/getData.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,35 +95,38 @@ public function data_create_font_collection() {
'font_families' => array( array() ),
),
'expected_data' => array(
'description' => '',
'categories' => array(),
'name' => 'My Collection',
'font_families' => array( array() ),
'description' => '',
'categories' => array(),
'terms_and_conditions' => '',
'name' => 'My Collection',
'font_families' => array( array() ),
),
),

'font collection with all data' => array(
'slug' => 'my-collection',
'config' => array(
'name' => 'My Collection',
'description' => 'My collection description',
'font_families' => array( array() ),
'categories' => array(),
'name' => 'My Collection',
'description' => 'My collection description',
'font_families' => array( array() ),
'categories' => array(),
'terms_and_conditions' => 'My Font Collection terms and conditions',
),
'expected_data' => array(
'description' => 'My collection description',
'categories' => array(),
'name' => 'My Collection',
'font_families' => array( array() ),
'description' => 'My collection description',
'categories' => array(),
'terms_and_conditions' => 'My Font Collection terms and conditions',
'name' => 'My Collection',
'font_families' => array( array() ),
),
),

'font collection with risky data' => array(
'slug' => 'my-collection',
'config' => array(
'name' => 'My Collection<script>alert("xss")</script>',
'description' => 'My collection description<script>alert("xss")</script>',
'font_families' => array(
'name' => 'My Collection<script>alert("xss")</script>',
'description' => 'My collection description<script>alert("xss")</script>',
'font_families' => array(
array(
'font_family_settings' => array(
'fontFamily' => 'Open Sans, sans-serif<script>alert("xss")</script>',
Expand Down Expand Up @@ -151,25 +154,27 @@ public function data_create_font_collection() {
'categories' => array( 'sans-serif<script>alert("xss")</script>' ),
),
),
'categories' => array(
'categories' => array(
array(
'name' => 'Mock col<script>alert("xss")</script>',
'slug' => 'mock-col<script>alert("xss")</script>',
'unwanted_property' => 'potentially evil value',
),
),
'unwanted_property' => 'potentially evil value',
'terms_and_conditions' => 'My Font Collection terms and conditions<script>alert("xss")</script>',
'unwanted_property' => 'potentially evil value',
),
'expected_data' => array(
'description' => 'My collection description',
'categories' => array(
'description' => 'My collection description',
'categories' => array(
array(
'name' => 'Mock col',
'slug' => 'mock-colalertxss',
),
),
'name' => 'My Collection',
'font_families' => array(
'terms_and_conditions' => 'My Font Collection terms and conditions',
'name' => 'My Collection',
'font_families' => array(
array(
'font_family_settings' => array(
'fontFamily' => '"Open Sans", sans-serif',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public function test_get_item_schema() {

$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
$properties = $data['schema']['properties'];
$this->assertCount( 5, $properties, 'There should be 5 properties in the response data schema.' );
$this->assertCount( 6, $properties, 'There should be 6 properties in the response data schema.' );
$this->assertArrayHasKey( 'slug', $properties, 'The slug property should exist in the response data schema.' );
$this->assertArrayHasKey( 'name', $properties, 'The name property should exist in the response data schema.' );
$this->assertArrayHasKey( 'description', $properties, 'The description property should exist in the response data schema.' );
Expand Down

0 comments on commit 7c50642

Please sign in to comment.