Skip to content

Commit

Permalink
REST API fixes for working with the Pod object
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0ttkclark committed Aug 18, 2024
1 parent 4bc165d commit f9b84e1
Show file tree
Hide file tree
Showing 3 changed files with 418 additions and 21 deletions.
4 changes: 2 additions & 2 deletions classes/PodsInit.php
Original file line number Diff line number Diff line change
Expand Up @@ -1799,7 +1799,7 @@ public function setup_content_types( $force = false ) {
$rest_enabled = (boolean) pods_v( 'rest_enable', $pod, false );

if ( $rest_enabled ) {
new PodsRESTFields( $pod['name'] );
new PodsRESTFields( $pod );
}
}

Expand All @@ -1809,7 +1809,7 @@ public function setup_content_types( $force = false ) {
$rest_enabled = (boolean) pods_v( 'rest_enable', $pod, false );

if ( $rest_enabled ) {
new PodsRESTFields( $pod['name'] );
new PodsRESTFields( $pod );
}
}

Expand Down
42 changes: 23 additions & 19 deletions classes/PodsRESTHandlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,33 @@ class PodsRESTHandlers {
*
* @var Pods
*/
private static $pod;
private static $pods = false;

/**
* Get Pod object
* Get the Pods object.
*
* @since 2.5.6
*
* @param $pod_name
* @param $id
* @param string $pod_name The pod name.
* @param string|int $id The item ID.
*
* @return bool|Pods
* @return bool|Pods The Pods object or false if not found.
*/
protected static function get_pod( $pod_name, $id ) {
public static function get_pods_object( $pod_name, $id ) {

if ( ! self::$pod || self::$pod->pod !== $pod_name ) {
self::$pod = pods_get_instance( $pod_name, $id, true );
if ( ! self::$pods || self::$pods->pod !== $pod_name ) {
self::$pods = pods_get_instance( $pod_name, $id, true );
}

if ( self::$pod && (int) self::$pod->id !== (int) $id ) {
self::$pod->fetch( $id );
if ( self::$pods && (int) self::$pods->id !== (int) $id ) {
self::$pods->fetch( $id );
}

return self::$pod;
if ( ! self::$pods->exists() ) {
return false;
}

return self::$pods;

}

Expand Down Expand Up @@ -81,15 +85,15 @@ public static function get_handler( $object, $field_name, $request, $object_type
}

/**
* Filter the pod name
* Filter the pod name for the REST API handler.
*
* @since 2.6.7
*
* @param array $pod_name Pod name
* @param Pods $object Rest object
* @param string $field_name Name of the field
* @param WP_REST_Request $request Current request
* @param string $object_type Rest Object type
* @param array $pod_name The Pod name.
* @param array $object The REST object.
* @param string $field_name The name of the field.
* @param WP_REST_Request $request The current request.
* @param string $object_type The REST object type.
*/
$pod_name = apply_filters( 'pods_rest_api_pod_name', $pod_name, $object, $field_name, $request, $object_type );

Expand All @@ -99,7 +103,7 @@ public static function get_handler( $object, $field_name, $request, $object_type
$id = pods_v( 'ID', $object );
}

$pod = self::get_pod( $pod_name, $id );
$pod = self::get_pods_object( $pod_name, $id );

$value = false;

Expand Down Expand Up @@ -267,7 +271,7 @@ public static function save_handler( $object, $request, $creating ) {
$pod_name = 'media';
}

$pod = self::get_pod( $pod_name, $id );
$pod = self::get_pods_object( $pod_name, $id );

global $wp_rest_additional_fields;

Expand Down
Loading

0 comments on commit f9b84e1

Please sign in to comment.