Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 49 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| WPCOM_JSON_API_List_Post_Type_Taxonomies_Endpoint | |
0.00% |
0 / 29 |
|
0.00% |
0 / 2 |
210 | |
0.00% |
0 / 1 |
| callback | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
132 | |||
| localize_initial_taxonomies | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit( 0 ); |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * List post type taxonomies endpoint. |
| 9 | */ |
| 10 | |
| 11 | new WPCOM_JSON_API_List_Post_Type_Taxonomies_Endpoint( |
| 12 | array( |
| 13 | 'description' => 'Get a list of taxonomies associated with a post type.', |
| 14 | 'group' => 'taxonomy', |
| 15 | 'stat' => 'sites:X:post-types:X:taxonomies', |
| 16 | 'method' => 'GET', |
| 17 | 'path' => '/sites/%s/post-types/%s/taxonomies', |
| 18 | 'path_labels' => array( |
| 19 | '$site' => '(int|string) Site ID or domain', |
| 20 | '$post_type' => '(string) Post type', |
| 21 | ), |
| 22 | 'response_format' => array( |
| 23 | 'found' => '(int) The number of taxonomies found', |
| 24 | 'taxonomies' => '(array:taxonomy) A list of available taxonomies', |
| 25 | ), |
| 26 | 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/33534099/post-types/post/taxonomies', |
| 27 | ) |
| 28 | ); |
| 29 | |
| 30 | /** |
| 31 | * List post type taxonomies endpoint class. |
| 32 | * |
| 33 | * /sites/%s/post-types/%s/taxonomies -> $blog_id, $post_type |
| 34 | * |
| 35 | * @phan-constructor-used-for-side-effects |
| 36 | */ |
| 37 | class WPCOM_JSON_API_List_Post_Type_Taxonomies_Endpoint extends WPCOM_JSON_API_Endpoint { |
| 38 | |
| 39 | /** |
| 40 | * Included taxonomy keys. |
| 41 | * |
| 42 | * @var array |
| 43 | */ |
| 44 | public static $taxonomy_keys_to_include = array( |
| 45 | 'name' => 'name', |
| 46 | 'label' => 'label', |
| 47 | 'labels' => 'labels', |
| 48 | 'description' => 'description', |
| 49 | 'hierarchical' => 'hierarchical', |
| 50 | 'public' => 'public', |
| 51 | 'cap' => 'capabilities', |
| 52 | ); |
| 53 | |
| 54 | /** |
| 55 | * API callback. |
| 56 | * |
| 57 | * @param string $path - the path. |
| 58 | * @param string $blog_id - the blog ID. |
| 59 | * @param string $post_type - the post type. |
| 60 | */ |
| 61 | public function callback( $path = '', $blog_id = 0, $post_type = 'post' ) { |
| 62 | $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); |
| 63 | if ( is_wp_error( $blog_id ) ) { |
| 64 | return $blog_id; |
| 65 | } |
| 66 | |
| 67 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 68 | $this->load_theme_functions(); |
| 69 | } |
| 70 | |
| 71 | $this->localize_initial_taxonomies( $post_type ); |
| 72 | |
| 73 | $post_type_object = get_post_type_object( $post_type ); |
| 74 | if ( ! $post_type_object || ( ! $post_type_object->publicly_queryable && ( |
| 75 | ! current_user_can( $post_type_object->cap->edit_posts ) ) ) ) { |
| 76 | return new WP_Error( 'unknown_post_type', 'Unknown post type', 404 ); |
| 77 | } |
| 78 | |
| 79 | // Get a list of available taxonomies. |
| 80 | $taxonomy_objects = get_object_taxonomies( $post_type, 'objects' ); |
| 81 | |
| 82 | // Construct array of formatted objects. |
| 83 | $formatted_taxonomy_objects = array(); |
| 84 | foreach ( $taxonomy_objects as $taxonomy_object ) { |
| 85 | // Omit private taxonomies unless user has assign capability. |
| 86 | if ( ! $taxonomy_object->public && ! current_user_can( $taxonomy_object->cap->assign_terms ) ) { |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | // Include only the desired keys in the response. |
| 91 | $formatted_taxonomy_object = array(); |
| 92 | foreach ( self::$taxonomy_keys_to_include as $key => $value ) { |
| 93 | $formatted_taxonomy_object[ $value ] = $taxonomy_object->{ $key }; |
| 94 | } |
| 95 | |
| 96 | $formatted_taxonomy_objects[] = $formatted_taxonomy_object; |
| 97 | } |
| 98 | |
| 99 | return array( |
| 100 | 'found' => count( $formatted_taxonomy_objects ), |
| 101 | 'taxonomies' => $formatted_taxonomy_objects, |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Handle localizing initial taxonomies. |
| 107 | * |
| 108 | * @param string $post_type - the post type. |
| 109 | */ |
| 110 | protected function localize_initial_taxonomies( $post_type ) { |
| 111 | /** This filter is documented in jetpack/json-endpoints/class.wpcom-json-api-list-post-types-endpoint.php */ |
| 112 | if ( ! apply_filters( 'rest_api_localize_response', false ) ) { |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | // Since recreating initial taxonomies will restore the default post |
| 117 | // types to which they are associated, save post type's taxonomies in |
| 118 | // case it was customized via `register_taxonomy_for_object_type`. |
| 119 | $post_type_taxonomies = get_object_taxonomies( $post_type ); |
| 120 | |
| 121 | // API localization occurs after the initial taxonomies have been |
| 122 | // registered, so re-register if localizing response. |
| 123 | create_initial_taxonomies(); |
| 124 | |
| 125 | // Restore registered taxonomies for post type. |
| 126 | foreach ( $post_type_taxonomies as $taxonomy ) { |
| 127 | register_taxonomy_for_object_type( $taxonomy, $post_type ); |
| 128 | } |
| 129 | } |
| 130 | } |