-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
parse all meta_data before sending response
- Loading branch information
Showing
11 changed files
with
518 additions
and
514 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace WCPOS\WooCommercePOS\API\Abstracts; | ||
|
||
use WC_Data; | ||
use WCPOS\WooCommercePOS\Logger; | ||
use WP_Error; | ||
use WP_REST_Request; | ||
use WP_REST_Response; | ||
|
||
abstract class WC_Rest_API_Modifier { | ||
|
||
/** | ||
* @var WP_REST_Request store the request object | ||
*/ | ||
protected $request; | ||
|
||
|
||
/** | ||
* @param array $fields | ||
* @return array|WP_Error | ||
*/ | ||
abstract public function get_all_posts( array $fields = array() ): array; | ||
|
||
|
||
/** | ||
* @param string $id | ||
* @return object | ||
*/ | ||
protected function format_id( string $id ): object { | ||
return (object) array( 'id' => (int) $id ); | ||
} | ||
|
||
/** | ||
* BUG FIX: some servers are not returning the correct meta_data if it is left as WC_Meta_Data objects | ||
* NOTE: it only seems to effect some versions of PHP, or some plugins are adding weird meta_data types | ||
* The result is mata_data: [{}, {}, {}] ie: empty objects, I think json_encode can't handle the WC_Meta_Data objects | ||
* | ||
* @TODO - I need to find out why this is happening | ||
* | ||
* @param WC_Data $object | ||
* @return array | ||
*/ | ||
protected function parse_meta_data( WC_Data $object ): array { | ||
return array_map( function( $meta_data ) { | ||
return $meta_data->get_data(); | ||
}, $object->get_meta_data()); | ||
} | ||
|
||
/** | ||
* BUG FIX: the response for some records can be huge, eg: | ||
* - product descriptions with lots of HTML, | ||
* - I've seen products with 1800+ meta_data objects | ||
* | ||
* This is just a helper function to try and alert us to these large responses | ||
* | ||
* @param WP_REST_Response $response | ||
* @param WC_Data $object | ||
*/ | ||
protected function log_large_rest_response( WP_REST_Response $response, WC_Data $object ) { | ||
$response_size = strlen( serialize( $response->data ) ); | ||
$max_response_size = 100000; | ||
if ( $response_size > $max_response_size ) { | ||
Logger::log( "Object ID {$object->get_id()} has a response size of {$response_size} bytes, exceeding the limit of {$max_response_size} bytes." ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.