← All changes
|
libraries/ct/includes/class-ct-rest-meta-fields.php
+166
-0
5.7.2
→
6.0.3
View file →
| @@ -1,0 +1,166 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Rest Meta Fields class | |
| 4 | + * | |
| 5 | + * Based on WP_REST_Post_Meta_Fields class | |
| 6 | + * | |
| 7 | + * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> | |
| 8 | + * | |
| 9 | + * @since 1.0.0 | |
| 10 | + */ | |
| 11 | +// Exit if accessed directly | |
| 12 | +defined( 'ABSPATH' ) || exit; | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Core class used to manage meta values for posts via the REST API. | |
| 16 | + * | |
| 17 | + * @since 1.0.0 | |
| 18 | + * | |
| 19 | + * @see WP_REST_Meta_Fields | |
| 20 | + */ | |
| 21 | +class CT_REST_Meta_Fields extends WP_REST_Meta_Fields { | |
| 22 | + | |
| 23 | + /** | |
| 24 | + * Table name. | |
| 25 | + * | |
| 26 | + * @since 1.0.0 | |
| 27 | + * @var string | |
| 28 | + */ | |
| 29 | + protected $name; | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * Table Meta table object. | |
| 33 | + * | |
| 34 | + * @since 1.0.0 | |
| 35 | + * @access public | |
| 36 | + * @var CT_Table $table | |
| 37 | + */ | |
| 38 | + public $table; | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * Constructor. | |
| 42 | + * | |
| 43 | + * @since 1.0.0 | |
| 44 | + * | |
| 45 | + * @param string $name Table name to register fields for. | |
| 46 | + */ | |
| 47 | + public function __construct( $name ) { | |
| 48 | + $this->name = $name; | |
| 49 | + $this->table = ct_get_table_object( $name ); | |
| 50 | + | |
| 51 | + } | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * Retrieves the object meta type. | |
| 55 | + * | |
| 56 | + * @since 1.0.0 | |
| 57 | + * | |
| 58 | + * @return string The meta type. | |
| 59 | + */ | |
| 60 | + protected function get_meta_type() { | |
| 61 | + return $this->table->meta->name; | |
| 62 | + } | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * Retrieves the object meta subtype. | |
| 66 | + * | |
| 67 | + * @since 1.0.0 | |
| 68 | + * | |
| 69 | + * @return string Subtype for the meta type, or empty string if no specific subtype. | |
| 70 | + */ | |
| 71 | + protected function get_meta_subtype() { | |
| 72 | + return $this->name; | |
| 73 | + } | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * Retrieves the type for register_rest_field(). | |
| 77 | + * | |
| 78 | + * @since 1.0.0 | |
| 79 | + * | |
| 80 | + * @see register_rest_field() | |
| 81 | + * | |
| 82 | + * @return string The REST field type. | |
| 83 | + */ | |
| 84 | + public function get_rest_field_type() { | |
| 85 | + return $this->name; | |
| 86 | + } | |
| 87 | + | |
| 88 | + /** | |
| 89 | + * Retrieves the meta field value. | |
| 90 | + * | |
| 91 | + * @since 1.0.0 | |
| 92 | + * | |
| 93 | + * @param int $object_id Object ID to fetch meta for. | |
| 94 | + * @param WP_REST_Request $request Full details about the request. | |
| 95 | + * @return WP_Error|object Object containing the meta values by name, otherwise WP_Error object. | |
| 96 | + */ | |
| 97 | + public function get_value( $object_id, $request ) { | |
| 98 | + $fields = $this->get_registered_fields(); | |
| 99 | + $response = array(); | |
| 100 | + | |
| 101 | + foreach ( $fields as $meta_key => $args ) { | |
| 102 | + $name = $args['name']; | |
| 103 | + $all_values = ct_get_object_meta( $object_id, $meta_key, false ); | |
| 104 | + if ( $args['single'] ) { | |
| 105 | + if ( empty( $all_values ) ) { | |
| 106 | + $value = $args['schema']['default']; | |
| 107 | + } else { | |
| 108 | + $value = $all_values[0]; | |
| 109 | + } | |
| 110 | + $value = $this->prepare_value_for_response( $value, $request, $args ); | |
| 111 | + } else { | |
| 112 | + $value = array(); | |
| 113 | + foreach ( $all_values as $row ) { | |
| 114 | + $value[] = $this->prepare_value_for_response( $row, $request, $args ); | |
| 115 | + } | |
| 116 | + } | |
| 117 | + | |
| 118 | + $response[ $name ] = $value; | |
| 119 | + } | |
| 120 | + | |
| 121 | + return $response; | |
| 122 | + } | |
| 123 | + | |
| 124 | + /** | |
| 125 | + * Updates a meta value for an object. | |
| 126 | + * | |
| 127 | + * @since 4.7.0 | |
| 128 | + * | |
| 129 | + * @param int $object_id Object ID to update. | |
| 130 | + * @param string $meta_key Key for the custom field. | |
| 131 | + * @param string $name Name for the field that is exposed in the REST API. | |
| 132 | + * @param mixed $value Updated value. | |
| 133 | + * @return bool|WP_Error True if the meta field was updated, WP_Error otherwise. | |
| 134 | + */ | |
| 135 | + protected function update_meta_value( $object_id, $meta_key, $name, $value ) { | |
| 136 | + $meta_type = $this->get_meta_type(); | |
| 137 | + if ( ! current_user_can( $this->table->cap->edit_post_meta, $object_id, $meta_key ) ) { | |
| 138 | + return new WP_Error( | |
| 139 | + 'rest_cannot_update', | |
| 140 | + /* translators: %s: custom field key */ | |
| 141 | + sprintf( __( 'Sorry, you are not allowed to edit the %s custom field.', 'ct' ), $name ), | |
| 142 | + array( 'key' => $name, 'status' => rest_authorization_required_code() ) | |
| 143 | + ); | |
| 144 | + } | |
| 145 | + | |
| 146 | + // Do the exact same check for a duplicate value as in update_metadata() to avoid update_metadata() returning false. | |
| 147 | + $old_value = ct_get_object_meta( $object_id, $meta_key ); | |
| 148 | + $subtype = get_object_subtype( $meta_type, $object_id ); | |
| 149 | + | |
| 150 | + if ( 1 === count( $old_value ) ) { | |
| 151 | + if ( (string) sanitize_meta( $meta_key, $value, $meta_type, $subtype ) === $old_value[0] ) { | |
| 152 | + return true; | |
| 153 | + } | |
| 154 | + } | |
| 155 | + | |
| 156 | + if ( ! ct_update_object_meta( $object_id, wp_slash( $meta_key ), wp_slash( $value ) ) ) { | |
| 157 | + return new WP_Error( | |
| 158 | + 'rest_meta_database_error', | |
| 159 | + __( 'Could not update meta value in database.', 'ct' ), | |
| 160 | + array( 'key' => $name, 'status' => WP_Http::INTERNAL_SERVER_ERROR ) | |
| 161 | + ); | |
| 162 | + } | |
| 163 | + | |
| 164 | + return true; | |
| 165 | + } | |
| 166 | +} | |