[API] Prevent "PHP Notice: Trying to get property of non-object" errors
This commit is contained in:
parent
6b39849be2
commit
ece4f00a71
|
@ -72,30 +72,37 @@ class WC_API_Resource {
|
||||||
*/
|
*/
|
||||||
protected function validate_request( $id, $type, $context ) {
|
protected function validate_request( $id, $type, $context ) {
|
||||||
|
|
||||||
if ( 'shop_order' === $type || 'shop_coupon' === $type || 'shop_webhook' === $type )
|
if ( 'shop_order' === $type || 'shop_coupon' === $type || 'shop_webhook' === $type ) {
|
||||||
$resource_name = str_replace( 'shop_', '', $type );
|
$resource_name = str_replace( 'shop_', '', $type );
|
||||||
else
|
} else {
|
||||||
$resource_name = $type;
|
$resource_name = $type;
|
||||||
|
}
|
||||||
|
|
||||||
$id = absint( $id );
|
$id = absint( $id );
|
||||||
|
|
||||||
// validate ID
|
// Validate ID
|
||||||
if ( empty( $id ) )
|
if ( empty( $id ) ) {
|
||||||
return new WP_Error( "woocommerce_api_invalid_{$resource_name}_id", sprintf( __( 'Invalid %s ID', 'woocommerce' ), $type ), array( 'status' => 404 ) );
|
return new WP_Error( "woocommerce_api_invalid_{$resource_name}_id", sprintf( __( 'Invalid %s ID', 'woocommerce' ), $type ), array( 'status' => 404 ) );
|
||||||
|
}
|
||||||
|
|
||||||
// only custom post types have per-post type/permission checks
|
// Only custom post types have per-post type/permission checks
|
||||||
if ( 'customer' !== $type ) {
|
if ( 'customer' !== $type ) {
|
||||||
|
|
||||||
$post = get_post( $id );
|
$post = get_post( $id );
|
||||||
|
|
||||||
// for checking permissions, product variations are the same as the product post type
|
if ( null === $post ) {
|
||||||
|
return new WP_Error( "woocommerce_api_no_{$resource_name}_found", sprintf( __( 'No %s found with the ID equal to %s', 'woocommerce' ), $resource_name, $id ), array( 'status' => 404 ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
// For checking permissions, product variations are the same as the product post type
|
||||||
$post_type = ( 'product_variation' === $post->post_type ) ? 'product' : $post->post_type;
|
$post_type = ( 'product_variation' === $post->post_type ) ? 'product' : $post->post_type;
|
||||||
|
|
||||||
// validate post type
|
// Validate post type
|
||||||
if ( $type !== $post_type )
|
if ( $type !== $post_type ) {
|
||||||
return new WP_Error( "woocommerce_api_invalid_{$resource_name}", sprintf( __( 'Invalid %s', 'woocommerce' ), $resource_name ), array( 'status' => 404 ) );
|
return new WP_Error( "woocommerce_api_invalid_{$resource_name}", sprintf( __( 'Invalid %s', 'woocommerce' ), $resource_name ), array( 'status' => 404 ) );
|
||||||
|
}
|
||||||
|
|
||||||
// validate permissions
|
// Validate permissions
|
||||||
switch ( $context ) {
|
switch ( $context ) {
|
||||||
|
|
||||||
case 'read':
|
case 'read':
|
||||||
|
|
Loading…
Reference in New Issue