|
| 1 | +<?php |
| 2 | + |
| 3 | +abstract class WP_REST_Client { |
| 4 | + /** |
| 5 | + * Get the client type. |
| 6 | + * |
| 7 | + * Must be overridden in subclass. |
| 8 | + * |
| 9 | + * @return string |
| 10 | + */ |
| 11 | + protected static function get_type() { |
| 12 | + return new WP_Error( 'rest_client_missing_type', __( 'Overridden class must implement get_type', 'rest_oauth1' ) ); |
| 13 | + } |
| 14 | + |
| 15 | + /** |
| 16 | + * Constructor. |
| 17 | + * |
| 18 | + * @param WP_Post $post Underlying post data. |
| 19 | + */ |
| 20 | + protected function __construct( WP_Post $post ) { |
| 21 | + $this->post = $post; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Getter. |
| 26 | + * |
| 27 | + * Passes through to post object. |
| 28 | + * |
| 29 | + * @param string $key Key to get. |
| 30 | + * @return mixed |
| 31 | + */ |
| 32 | + public function __get( $key ) { |
| 33 | + return $this->post->$key; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Isset-er. |
| 38 | + * |
| 39 | + * Passes through to post object. |
| 40 | + * |
| 41 | + * @param string $key Property to check if set. |
| 42 | + * @return bool |
| 43 | + */ |
| 44 | + public function __isset( $key ) { |
| 45 | + return isset( $this->post->$key ); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Update the client's post. |
| 50 | + * |
| 51 | + * @param array $params Parameters to update. |
| 52 | + * @return bool|WP_Error True on success, error object otherwise. |
| 53 | + */ |
| 54 | + public function update( $params ) { |
| 55 | + $data = array(); |
| 56 | + if ( isset( $params['name'] ) ) { |
| 57 | + $data['post_title'] = $params['name']; |
| 58 | + } |
| 59 | + if ( isset( $params['description'] ) ) { |
| 60 | + $data['post_content'] = $params['description']; |
| 61 | + } |
| 62 | + |
| 63 | + // Are we updating the post itself? |
| 64 | + if ( ! empty( $data ) ) { |
| 65 | + $data['ID'] = $this->post->ID; |
| 66 | + |
| 67 | + $result = wp_update_post( wp_slash( $data ), true ); |
| 68 | + if ( is_wp_error( $result ) ) { |
| 69 | + return $result; |
| 70 | + } |
| 71 | + |
| 72 | + // Reload the post property |
| 73 | + $this->post = get_post( $this->post->ID ); |
| 74 | + } |
| 75 | + |
| 76 | + // Are we updating any meta? |
| 77 | + if ( ! empty( $params['meta'] ) ) { |
| 78 | + $meta = $params['meta']; |
| 79 | + |
| 80 | + foreach ( $meta as $key => $value ) { |
| 81 | + $existing = get_post_meta( $this->post->ID, $key, true ); |
| 82 | + if ( $existing === $value ) { |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + $did_update = update_post_meta( $this->post->ID, $key, wp_slash( $value ) ); |
| 87 | + if ( ! $did_update ) { |
| 88 | + return new WP_Error( |
| 89 | + 'rest_client_update_meta_failed', |
| 90 | + __( 'Could not update client metadata.', 'rest_oauth' ) |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return true; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Delete a client. |
| 101 | + * |
| 102 | + * @param string $type Client type. |
| 103 | + * @param int $id Client post ID. |
| 104 | + * @return bool True if delete, false otherwise. |
| 105 | + */ |
| 106 | + public function delete() { |
| 107 | + return (bool) wp_delete_post( $this->post->ID, true ); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Get a client by ID. |
| 112 | + * |
| 113 | + * @param int $id Client post ID. |
| 114 | + * @return self|WP_Error |
| 115 | + */ |
| 116 | + public static function get( $id ) { |
| 117 | + $post = get_post( $id ); |
| 118 | + if ( empty( $id ) || empty( $post ) || $post->post_type !== 'json_consumer' ) { |
| 119 | + return new WP_Error( 'rest_oauth1_invalid_id', __( 'Client ID is not valid.', 'rest_oauth1' ), array( 'status' => 404 ) ); |
| 120 | + } |
| 121 | + |
| 122 | + $class = function_exists( 'get_called_class' ) ? get_called_class() : self::get_called_class(); |
| 123 | + return new $class( $post ); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Get a client by key. |
| 128 | + * |
| 129 | + * @param string $type Client type. |
| 130 | + * @param string $key Client key. |
| 131 | + * @return WP_Post|WP_Error |
| 132 | + */ |
| 133 | + public static function get_by_key( $key ) { |
| 134 | + $class = function_exists( 'get_called_class' ) ? get_called_class() : self::get_called_class(); |
| 135 | + $type = call_user_func( array( $class, 'get_type' ) ); |
| 136 | + |
| 137 | + $query = new WP_Query(); |
| 138 | + $consumers = $query->query( array( |
| 139 | + 'post_type' => 'json_consumer', |
| 140 | + 'post_status' => 'any', |
| 141 | + 'meta_query' => array( |
| 142 | + array( |
| 143 | + 'key' => 'key', |
| 144 | + 'value' => $key, |
| 145 | + ), |
| 146 | + array( |
| 147 | + 'key' => 'type', |
| 148 | + 'value' => $type, |
| 149 | + ), |
| 150 | + ), |
| 151 | + ) ); |
| 152 | + |
| 153 | + if ( empty( $consumers ) || empty( $consumers[0] ) ) { |
| 154 | + return new WP_Error( 'json_consumer_notfound', __( 'Consumer Key is invalid' ), array( 'status' => 401 ) ); |
| 155 | + } |
| 156 | + |
| 157 | + return $consumers[0]; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Create a new client. |
| 162 | + * |
| 163 | + * @param string $type Client type. |
| 164 | + * @param array $params { |
| 165 | + * @type string $name Client name |
| 166 | + * @type string $description Client description |
| 167 | + * @type array $meta Metadata for the client (map of key => value) |
| 168 | + * } |
| 169 | + * @return WP_Post|WP_Error |
| 170 | + */ |
| 171 | + public static function create( $params ) { |
| 172 | + $default = array( |
| 173 | + 'name' => '', |
| 174 | + 'description' => '', |
| 175 | + 'meta' => array(), |
| 176 | + ); |
| 177 | + $params = wp_parse_args( $params, $default ); |
| 178 | + |
| 179 | + $data = array(); |
| 180 | + $data['post_title'] = $params['name']; |
| 181 | + $data['post_content'] = $params['description']; |
| 182 | + $data['post_type'] = 'json_consumer'; |
| 183 | + |
| 184 | + $ID = wp_insert_post( $data ); |
| 185 | + if ( is_wp_error( $ID ) ) { |
| 186 | + return $ID; |
| 187 | + } |
| 188 | + |
| 189 | + $class = function_exists( 'get_called_class' ) ? get_called_class() : self::get_called_class(); |
| 190 | + $meta = $params['meta']; |
| 191 | + $meta['type'] = call_user_func( array( $class, 'get_type' ) ); |
| 192 | + |
| 193 | + // Allow types to add their own meta too |
| 194 | + $meta = $class::add_extra_meta( $meta, $params ); |
| 195 | + |
| 196 | + /** |
| 197 | + * Add extra meta to the consumer on creation. |
| 198 | + * |
| 199 | + * @param array $meta Metadata map of key => value |
| 200 | + * @param int $ID Post ID we created. |
| 201 | + * @param array $params Parameters passed to create. |
| 202 | + */ |
| 203 | + $meta = apply_filters( 'json_consumer_meta', $meta, $ID, $params ); |
| 204 | + |
| 205 | + foreach ( $meta as $key => $value ) { |
| 206 | + update_post_meta( $ID, $key, $value ); |
| 207 | + } |
| 208 | + |
| 209 | + $post = get_post( $ID ); |
| 210 | + return new $class( $post ); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Add extra meta to a post. |
| 215 | + * |
| 216 | + * If you'd like to add extra meta on client creation, add it here. This |
| 217 | + * works the same as a filter; make sure you return the original array! |
| 218 | + * |
| 219 | + * @param array $meta Metadata for the post. |
| 220 | + * @param array $params Parameters used to create the post. |
| 221 | + * @return array Metadata to actually save. |
| 222 | + */ |
| 223 | + protected static function add_extra_meta( $meta, $params ) { |
| 224 | + return $meta; |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * Shim for get_called_class() for PHP 5.2 |
| 229 | + * |
| 230 | + * @return string Class name. |
| 231 | + */ |
| 232 | + protected static function get_called_class() { |
| 233 | + // PHP 5.2 only |
| 234 | + $backtrace = debug_backtrace(); |
| 235 | + // [0] WP_REST_Client::get_called_class() |
| 236 | + // [1] WP_REST_Client::function() |
| 237 | + if ( 'call_user_func' === $backtrace[2]['function'] ) { |
| 238 | + return $backtrace[2]['args'][0][0]; |
| 239 | + } |
| 240 | + return $backtrace[2]['class']; |
| 241 | + } |
| 242 | +} |
0 commit comments