| @@ 22-62 (lines=41) @@ | ||
| 19 | } |
|
| 20 | } |
|
| 21 | ||
| 22 | if ( ! function_exists( 'register_rest_field' ) ) { |
|
| 23 | /** |
|
| 24 | * Registers a new field on an existing WordPress object type. |
|
| 25 | * |
|
| 26 | * @global array $wp_rest_additional_fields Holds registered fields, organized |
|
| 27 | * by object type. |
|
| 28 | * |
|
| 29 | * @param string|array $object_type Object(s) the field is being registered |
|
| 30 | * to, "post"|"term"|"comment" etc. |
|
| 31 | * @param string $attribute The attribute name. |
|
| 32 | * @param array $args { |
|
| 33 | * Optional. An array of arguments used to handle the registered field. |
|
| 34 | * |
|
| 35 | * @type string|array|null $get_callback Optional. The callback function used to retrieve the field |
|
| 36 | * value. Default is 'null', the field will not be returned in |
|
| 37 | * the response. |
|
| 38 | * @type string|array|null $update_callback Optional. The callback function used to set and update the |
|
| 39 | * field value. Default is 'null', the value cannot be set or |
|
| 40 | * updated. |
|
| 41 | * @type string|array|null $schema Optional. The callback function used to create the schema for |
|
| 42 | * this field. Default is 'null', no schema entry will be returned. |
|
| 43 | * } |
|
| 44 | */ |
|
| 45 | function register_rest_field( $object_type, $attribute, $args = array() ) { |
|
| 46 | $defaults = array( |
|
| 47 | 'get_callback' => null, |
|
| 48 | 'update_callback' => null, |
|
| 49 | 'schema' => null, |
|
| 50 | ); |
|
| 51 | ||
| 52 | $args = wp_parse_args( $args, $defaults ); |
|
| 53 | ||
| 54 | global $wp_rest_additional_fields; |
|
| 55 | ||
| 56 | $object_types = (array) $object_type; |
|
| 57 | ||
| 58 | foreach ( $object_types as $object_type ) { |
|
| 59 | $wp_rest_additional_fields[ $object_type ][ $attribute ] = $args; |
|
| 60 | } |
|
| 61 | } |
|
| 62 | } |
|
| 63 | ||
| 64 | if ( ! function_exists( 'register_api_field' ) ) { |
|
| 65 | /** |
|
| @@ 98-138 (lines=41) @@ | ||
| 95 | } |
|
| 96 | } |
|
| 97 | ||
| 98 | if ( ! function_exists( 'register_rest_field' ) ) { |
|
| 99 | /** |
|
| 100 | * Registers a new field on an existing WordPress object type. |
|
| 101 | * |
|
| 102 | * @global array $wp_rest_additional_fields Holds registered fields, organized |
|
| 103 | * by object type. |
|
| 104 | * |
|
| 105 | * @param string|array $object_type Object(s) the field is being registered |
|
| 106 | * to, "post"|"term"|"comment" etc. |
|
| 107 | * @param string $attribute The attribute name. |
|
| 108 | * @param array $args { |
|
| 109 | * Optional. An array of arguments used to handle the registered field. |
|
| 110 | * |
|
| 111 | * @type string|array|null $get_callback Optional. The callback function used to retrieve the field |
|
| 112 | * value. Default is 'null', the field will not be returned in |
|
| 113 | * the response. |
|
| 114 | * @type string|array|null $update_callback Optional. The callback function used to set and update the |
|
| 115 | * field value. Default is 'null', the value cannot be set or |
|
| 116 | * updated. |
|
| 117 | * @type string|array|null $schema Optional. The callback function used to create the schema for |
|
| 118 | * this field. Default is 'null', no schema entry will be returned. |
|
| 119 | * } |
|
| 120 | */ |
|
| 121 | function register_rest_field( $object_type, $attribute, $args = array() ) { |
|
| 122 | $defaults = array( |
|
| 123 | 'get_callback' => null, |
|
| 124 | 'update_callback' => null, |
|
| 125 | 'schema' => null, |
|
| 126 | ); |
|
| 127 | ||
| 128 | $args = wp_parse_args( $args, $defaults ); |
|
| 129 | ||
| 130 | global $wp_rest_additional_fields; |
|
| 131 | ||
| 132 | $object_types = (array) $object_type; |
|
| 133 | ||
| 134 | foreach ( $object_types as $object_type ) { |
|
| 135 | $wp_rest_additional_fields[ $object_type ][ $attribute ] = $args; |
|
| 136 | } |
|
| 137 | } |
|
| 138 | } |
|
| 139 | ||
| 140 | if ( ! function_exists( 'register_api_field' ) ) { |
|
| 141 | /** |
|