PodsRESTHandlers

Class PodsRESTHandlers.

Defined (1)

The class is defined in the following location(s).

/classes/PodsRESTHandlers.php  
  1. class PodsRESTHandlers { 
  2.  
  3. /** 
  4. * Holds a Pods object to avoid extra DB queries 
  5. * @since 2.5.6 
  6. * @var Pods 
  7. */ 
  8. private static $pod; 
  9.  
  10. /** 
  11. * Get Pod object 
  12. * @since 2.5.6 
  13. * @param $pod_name 
  14. * @param $id 
  15. * @return bool|Pods 
  16. */ 
  17. protected static function get_pod( $pod_name, $id ) { 
  18.  
  19. if ( ! self::$pod || self::$pod->pod != $pod_name ) { 
  20. self::$pod = pods( $pod_name, $id, true ); 
  21.  
  22. if ( self::$pod && self::$pod->id != $id ) { 
  23. self::$pod->fetch( $id ); 
  24.  
  25. return self::$pod; 
  26.  
  27.  
  28. /** 
  29. * Handler for getting custom field data. 
  30. * @since 2.5.6 
  31. * @param array $object The object from the response 
  32. * @param string $field_name Name of field 
  33. * @param WP_REST_Request $request Current request 
  34. * @return mixed 
  35. */ 
  36. public static function get_handler( $object, $field_name, $request ) { 
  37.  
  38. $pod_name = pods_v( 'type', $object ); 
  39. $id = pods_v( 'id', $object ); 
  40. $pod = self::get_pod( $pod_name, $id ); 
  41. if ( $pod && PodsRESTFields::field_allowed_to_extend( $field_name, $pod, 'read' ) ) { 
  42. $params = null; 
  43. $field_data = $pod->fields( $field_name ); 
  44. if ( 'pick' == pods_v( 'type', $field_data ) ) { 
  45. $output_type = pods_v( 'rest_pick_response', $field_data['options'], 'array' ); 
  46. if ( 'array' == $output_type ) { 
  47.  
  48. $related_pod = $pod->field( $field_name, array( 'output' => 'pod' ) ); 
  49. $fields = $related_pod->fields(); 
  50. $fields = array_keys( $fields ); 
  51. if ( isset( $related_pod->pod_data['object_fields'] ) && ! empty( $related_pod->pod_data['object_fields'] ) ) { 
  52. $fields = array_merge( $fields, array_keys( $related_pod->pod_data['object_fields'] ) ); 
  53.  
  54. /** 
  55. * What fields to show in a related field REST response. 
  56. * @since 0.0.1 
  57. * @param array $fields The fields to show 
  58. * @param string $field_name The name of the field 
  59. * @param object|Pods $pod The Pods object for Pod relationship is from. 
  60. * @param object|Pods $pod The Pods object for Pod relationship is to. 
  61. * @param int $id Current item ID 
  62. * @param object|WP_REST_Request Current request object. 
  63. */ 
  64. $fields = apply_filters( 'pods_rest_api_fields_for_relationship_response', $fields, $field_name, $pod, $related_pod, $id, $request ); 
  65.  
  66. $depth = pods_v( 'rest_pick_depth', $field_data['options'], 2 ); 
  67.  
  68. /** 
  69. * What depth to use for a related field REST response. 
  70. * @since 0.0.1 
  71. * @param array $depth The depth. 
  72. * @param string $field_name The name of the field 
  73. * @param object|Pods $pod The Pods object for Pod relationship is from. 
  74. * @param object|Pods $pod The Pods object for Pod relationship is to. 
  75. * @param int $id Current item ID 
  76. * @param object|WP_REST_Request Current request object. 
  77. */ 
  78. $depth = apply_filters( 'pods_rest_api_depth_for_relationship_response', $depth, $field_name, $pod, $related_pod, $id, $request ); 
  79.  
  80. $params = array( 
  81. 'fields' => $fields,  
  82. 'depth' => $depth 
  83.  
  84. ); 
  85.  
  86. $data = $pod->api->export_pod_item( $related_pod, $params ); 
  87.  
  88. return $data; 
  89.  
  90.  
  91. $params = array(); 
  92. $params['output'] = $output_type; 
  93.  
  94. return $pod->field( $field_name, $params ); 
  95.  
  96.  
  97. return false; 
  98.  
  99.  
  100. /** 
  101. * Handler for updating custom field data. 
  102. * @since 2.5.6 
  103. * @param mixed $value Value to write 
  104. * @param object $object The object from the response 
  105. * @param string $field_name Name of field 
  106. * @return bool|int 
  107. */ 
  108. public function write_handler( $value, $object, $field_name ) { 
  109.  
  110. $pod_name = pods_v( 'type', $object ); 
  111.  
  112. $id = pods_v( 'id', $object ); 
  113.  
  114. $pod = self::get_pod( $pod_name, $id ); 
  115.  
  116. if ( $pod && PodsRESTFields::field_allowed_to_extend( $field_name, $pod, 'write' ) ) { 
  117. $pod->save( $field_name, $value, $id ); 
  118.  
  119. return $pod->field( $field_name ); 
  120.  
  121. return false; 
  122.  
  123.  
  124. /** 
  125. * Add REST API support to a post type 
  126. * @since 2.5.6 
  127. * @param string $post_type_name Name of post type 
  128. * @param bool|false $rest_base Optional. Base url segment. If not set, post type name is used 
  129. * @param string $controller Optional, controller class for route. If not set "WP_REST_Posts_Controller" is 
  130. * used. 
  131. */ 
  132. public static function post_type_rest_support( $post_type_name, $rest_base = false, $controller = 'WP_REST_Posts_Controller' ) { 
  133.  
  134. global $wp_post_types; 
  135.  
  136. if ( isset( $wp_post_types[ $post_type_name ] ) ) { 
  137. if ( ! $rest_base ) { 
  138. $rest_base = $post_type_name; 
  139.  
  140. $wp_post_types[ $post_type_name ]->show_in_rest = true; 
  141. $wp_post_types[ $post_type_name ]->rest_base = $rest_base; 
  142. $wp_post_types[ $post_type_name ]->rest_controller_class = $controller; 
  143.  
  144.  
  145. /** 
  146. * Add REST API support to an already registered taxonomy. 
  147. * @since 2.5.6 
  148. * @param string $taxonomy_name Taxonomy name. 
  149. * @param bool|false $rest_base Optional. Base url segment. If not set, taxonomy name is used. 
  150. * @param string $controller Optional, controller class for route. If not set "WP_REST_Terms_Controller" is 
  151. * used. 
  152. */ 
  153. public static function taxonomy_rest_support( $taxonomy_name, $rest_base = false, $controller = 'WP_REST_Terms_Controller' ) { 
  154.  
  155. global $wp_taxonomies; 
  156.  
  157. if ( isset( $wp_taxonomies[ $taxonomy_name ] ) ) { 
  158. if ( ! $rest_base ) { 
  159. $rest_base = $taxonomy_name; 
  160.  
  161. $wp_taxonomies[ $taxonomy_name ]->show_in_rest = true; 
  162. $wp_taxonomies[ $taxonomy_name ]->rest_base = $rest_base; 
  163. $wp_taxonomies[ $taxonomy_name ]->rest_controller_class = $controller; 
  164.  
  165.  
  166. /** 
  167. * Check if a Pod supports extending core REST response. 
  168. * @since 2.5.6 
  169. * @param array|Pods $pod Pod object or the pod_data array 
  170. * @return bool 
  171. */ 
  172. public static function pod_extends_core_route( $pod ) { 
  173.  
  174. $enabled = false; 
  175.  
  176. if ( is_object( $pod ) ) { 
  177. $pod = $pod->pod_data; 
  178.  
  179. if ( is_array( $pod ) ) { 
  180. $enabled = (boolean) pods_v( 'rest_enable', $pod['options'], false ); 
  181.  
  182. return $enabled; 
  183.  
  184.