2019-03-31 22:48:57 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
2020-04-30 17:12:56 -04:00
|
|
|
* Abstract Model - WC_Post
|
2019-03-31 22:48:57 -04:00
|
|
|
*
|
2020-04-30 17:12:56 -04:00
|
|
|
* Defines shared functionality for WooCommerce CPT models.
|
2019-03-31 22:48:57 -04:00
|
|
|
*
|
2019-10-25 19:13:36 -04:00
|
|
|
* @package WPGraphQL\WooCommerce\Model
|
2019-03-31 22:48:57 -04:00
|
|
|
* @since 0.0.1
|
|
|
|
|
*/
|
|
|
|
|
|
2019-10-25 19:13:36 -04:00
|
|
|
namespace WPGraphQL\WooCommerce\Model;
|
2019-03-31 22:48:57 -04:00
|
|
|
|
2019-07-05 17:27:14 -04:00
|
|
|
use GraphQL\Error\UserError;
|
2020-04-30 17:12:56 -04:00
|
|
|
use WPGraphQL\Model\Post;
|
2019-03-31 22:48:57 -04:00
|
|
|
|
|
|
|
|
/**
|
2020-04-30 17:12:56 -04:00
|
|
|
* Class WC_Post
|
2023-06-13 23:17:02 +03:00
|
|
|
*
|
|
|
|
|
* @property \WC_Data $wc_data
|
|
|
|
|
* @property \WP_Post $data
|
|
|
|
|
*
|
|
|
|
|
* @package WPGraphQL\WooCommerce\Model
|
2019-03-31 22:48:57 -04:00
|
|
|
*/
|
2020-04-30 17:12:56 -04:00
|
|
|
abstract class WC_Post extends Post {
|
|
|
|
|
/**
|
|
|
|
|
* Stores the WC_Data object connected to the model.
|
2020-09-22 08:17:01 -04:00
|
|
|
*
|
2023-06-13 23:17:02 +03:00
|
|
|
* @var \WC_Data
|
2020-04-30 17:12:56 -04:00
|
|
|
*/
|
2020-09-22 08:17:01 -04:00
|
|
|
protected $wc_data;
|
2020-04-30 17:12:56 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* WC_Post constructor
|
|
|
|
|
*
|
2023-06-13 23:17:02 +03:00
|
|
|
* @param \WC_Data $data Data object to be used by the model.
|
|
|
|
|
*
|
|
|
|
|
* @throws \Exception Failed to retrieve data source post object.
|
2020-04-30 17:12:56 -04:00
|
|
|
*/
|
2020-09-22 08:17:01 -04:00
|
|
|
public function __construct( $data ) {
|
|
|
|
|
// Store CRUD object.
|
|
|
|
|
$this->wc_data = $data;
|
|
|
|
|
|
2020-04-30 17:12:56 -04:00
|
|
|
// Get WP_Post object.
|
2020-09-22 08:17:01 -04:00
|
|
|
$post = get_post( $data->get_id() );
|
|
|
|
|
|
2023-06-13 23:17:02 +03:00
|
|
|
// Check if product is valid.
|
|
|
|
|
if ( ! is_object( $post ) ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new \Exception( __( 'Failed to retrieve data source post object', 'graphql-for-ecommerce' ) );
|
2023-06-13 23:17:02 +03:00
|
|
|
}
|
|
|
|
|
|
2021-01-21 22:41:43 -05:00
|
|
|
// Add $allowed_restricted_fields.
|
2022-08-26 14:53:36 -04:00
|
|
|
if ( ! has_filter( 'graphql_allowed_fields_on_restricted_type', [ static::class, 'add_allowed_restricted_fields' ] ) ) {
|
|
|
|
|
add_filter( 'graphql_allowed_fields_on_restricted_type', [ static::class, 'add_allowed_restricted_fields' ], 10, 2 );
|
2020-09-22 08:17:01 -04:00
|
|
|
}
|
2020-04-30 17:12:56 -04:00
|
|
|
|
|
|
|
|
// Execute Post Model constructor.
|
2020-09-22 08:17:01 -04:00
|
|
|
parent::__construct( $post );
|
2020-04-30 17:12:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-09-22 08:17:01 -04:00
|
|
|
* Injects CRUD object fields into $allowed_restricted_fields
|
2021-01-21 22:41:43 -05:00
|
|
|
*
|
|
|
|
|
* @param array $allowed_restricted_fields The fields to allow when the data is designated as restricted to the current user.
|
|
|
|
|
* @param string $model_name Name of the model the filter is currently being executed in.
|
2020-09-22 08:17:01 -04:00
|
|
|
*
|
|
|
|
|
* @return string[]
|
2020-04-30 17:12:56 -04:00
|
|
|
*/
|
2020-09-22 08:17:01 -04:00
|
|
|
public static function add_allowed_restricted_fields( $allowed_restricted_fields, $model_name ) {
|
|
|
|
|
$class_name = static::class;
|
|
|
|
|
if ( "{$class_name}Object" === $model_name ) {
|
|
|
|
|
return static::get_allowed_restricted_fields( $allowed_restricted_fields );
|
2020-04-30 17:12:56 -04:00
|
|
|
}
|
2020-09-22 08:17:01 -04:00
|
|
|
|
|
|
|
|
return $allowed_restricted_fields;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the fields allowed to be displayed even if this entry is restricted.
|
|
|
|
|
*
|
2021-01-21 22:41:43 -05:00
|
|
|
* @param array $allowed_restricted_fields The fields to allow when the data is designated as restricted to the current user.
|
2020-09-22 08:17:01 -04:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2022-08-26 14:53:36 -04:00
|
|
|
protected static function get_allowed_restricted_fields( $allowed_restricted_fields = [] ) {
|
|
|
|
|
return [
|
2020-09-22 08:17:01 -04:00
|
|
|
'isRestricted',
|
|
|
|
|
'isPrivate',
|
|
|
|
|
'isPublic',
|
|
|
|
|
'id',
|
|
|
|
|
'databaseId',
|
2025-02-27 10:22:36 -05:00
|
|
|
'slug',
|
|
|
|
|
'uri',
|
|
|
|
|
'link',
|
|
|
|
|
'name',
|
|
|
|
|
'isPostsPage',
|
|
|
|
|
'isFrontPage',
|
|
|
|
|
'hasPassword',
|
|
|
|
|
'status',
|
2022-08-26 14:53:36 -04:00
|
|
|
];
|
2019-03-31 22:48:57 -04:00
|
|
|
}
|
|
|
|
|
|
2019-04-02 22:19:58 -04:00
|
|
|
/**
|
2019-04-03 17:40:53 -04:00
|
|
|
* Forwards function calls to WC_Data sub-class instance.
|
2019-04-02 22:19:58 -04:00
|
|
|
*
|
|
|
|
|
* @param string $method - function name.
|
|
|
|
|
* @param array $args - function call arguments.
|
2021-01-21 22:41:43 -05:00
|
|
|
*
|
2019-04-11 22:42:21 -04:00
|
|
|
* @return mixed
|
2021-01-21 22:41:43 -05:00
|
|
|
*
|
2023-06-13 23:17:02 +03:00
|
|
|
* @throws \BadMethodCallException Method not found on WC data object.
|
2019-04-02 22:19:58 -04:00
|
|
|
*/
|
|
|
|
|
public function __call( $method, $args ) {
|
2022-08-26 14:53:36 -04:00
|
|
|
if ( \is_callable( [ $this->wc_data, $method ] ) ) {
|
2020-09-22 08:17:01 -04:00
|
|
|
return $this->wc_data->$method( ...$args );
|
|
|
|
|
}
|
2019-04-02 22:19:58 -04:00
|
|
|
|
2023-07-20 00:11:25 +03:00
|
|
|
$class = self::class;
|
2023-06-13 23:17:02 +03:00
|
|
|
throw new \BadMethodCallException( "Call to undefined method {$method} on the {$class}" );
|
2019-03-31 22:48:57 -04:00
|
|
|
}
|
|
|
|
|
|
2019-07-05 17:27:14 -04:00
|
|
|
/**
|
|
|
|
|
* Wrapper function for deleting
|
|
|
|
|
*
|
2023-07-19 23:01:50 +03:00
|
|
|
* @throws \GraphQL\Error\UserError Not authorized.
|
2019-07-05 17:27:14 -04:00
|
|
|
*
|
2020-02-13 23:00:55 -06:00
|
|
|
* @param boolean $force_delete Should the data be deleted permanently.
|
2019-07-05 17:27:14 -04:00
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
public function delete( $force_delete = false ) {
|
2023-06-13 23:17:02 +03:00
|
|
|
$post_type_object = $this->post_type_object;
|
|
|
|
|
if ( empty( $post_type_object ) ) {
|
|
|
|
|
throw new UserError(
|
|
|
|
|
__(
|
|
|
|
|
'Post type object not found.',
|
2026-06-30 10:16:21 -04:00
|
|
|
'graphql-for-ecommerce'
|
2023-06-13 23:17:02 +03:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ! current_user_can( $post_type_object->cap->edit_posts ) ) {
|
2019-07-05 17:27:14 -04:00
|
|
|
throw new UserError(
|
|
|
|
|
__(
|
|
|
|
|
'User does not have the capabilities necessary to delete this object.',
|
2026-06-30 10:16:21 -04:00
|
|
|
'graphql-for-ecommerce'
|
2019-07-05 17:27:14 -04:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-22 08:17:01 -04:00
|
|
|
return $this->wc_data->delete( $force_delete );
|
2019-07-05 17:27:14 -04:00
|
|
|
}
|
2020-10-22 16:01:35 -04:00
|
|
|
|
2025-02-27 10:22:36 -05:00
|
|
|
/**
|
|
|
|
|
* {@inheritDoc}
|
|
|
|
|
*/
|
|
|
|
|
public function is_private() {
|
|
|
|
|
/**
|
|
|
|
|
* Published content is public, not private
|
|
|
|
|
*/
|
|
|
|
|
if ( 'publish' === $this->data->post_status && $this->post_type_object && empty( $this->data->post_password ) && ( true === $this->post_type_object->public || true === $this->post_type_object->publicly_queryable ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parent::is_post_private( $this->data );
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-29 13:29:14 -04:00
|
|
|
/**
|
|
|
|
|
* Method for determining if the data should be considered private or not
|
|
|
|
|
*
|
2023-06-13 23:17:02 +03:00
|
|
|
* @param \WP_Post $post_object The object of the post we need to verify permissions for.
|
2022-08-29 13:29:14 -04:00
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function is_post_private( $post_object = null ) {
|
2023-06-13 23:17:02 +03:00
|
|
|
/**
|
|
|
|
|
* Stores the incoming post type object for the post being modeled
|
|
|
|
|
*
|
|
|
|
|
* @var null|\WP_Post_Type $post_type_object
|
|
|
|
|
*/
|
2022-08-29 13:29:14 -04:00
|
|
|
$post_type_object = $this->post_type_object;
|
|
|
|
|
|
|
|
|
|
if ( empty( $post_object ) ) {
|
|
|
|
|
$post_object = $this->data;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If the status is NOT publish and the user does NOT have capabilities to edit posts,
|
|
|
|
|
* consider the post private.
|
|
|
|
|
*/
|
|
|
|
|
if ( ! isset( $post_type_object->cap->edit_posts ) || ! current_user_can( $post_type_object->cap->edit_posts ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If the owner of the content is the current user
|
|
|
|
|
*/
|
|
|
|
|
if ( ( true === $this->owner_matches_current_user() ) && 'revision' !== $post_object->post_type ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( 'private' === $this->data->post_status && ( ! isset( $post_type_object->cap->read_private_posts ) || ! current_user_can( $post_type_object->cap->read_private_posts ) ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-27 10:22:36 -05:00
|
|
|
if ( ! empty( $post_object->post_password ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-29 13:29:14 -04:00
|
|
|
if ( 'auto-draft' === $this->data->post_status ) {
|
|
|
|
|
$parent = get_post( (int) $this->data->post_parent );
|
|
|
|
|
|
|
|
|
|
if ( empty( $parent ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$parent_post_type_obj = $post_type_object;
|
|
|
|
|
if ( 'private' === $parent->post_status ) {
|
|
|
|
|
$cap = isset( $parent_post_type_obj->cap->read_private_posts ) ? $parent_post_type_obj->cap->read_private_posts : 'read_private_posts';
|
|
|
|
|
} else {
|
|
|
|
|
$cap = isset( $parent_post_type_obj->cap->edit_post ) ? $parent_post_type_obj->cap->edit_post : 'edit_post';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ! current_user_can( $cap, $parent->ID ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}//end if
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 16:01:35 -04:00
|
|
|
/**
|
|
|
|
|
* Returns the source WP_Post instance.
|
|
|
|
|
*
|
|
|
|
|
* @return \WP_Post
|
|
|
|
|
*/
|
|
|
|
|
public function as_WP_Post() {
|
|
|
|
|
return $this->data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the source WC_Data instance
|
|
|
|
|
*
|
|
|
|
|
* @return \WC_Data
|
|
|
|
|
*/
|
|
|
|
|
public function as_WC_Data() {
|
|
|
|
|
return $this->wc_data;
|
|
|
|
|
}
|
2019-03-31 22:48:57 -04:00
|
|
|
}
|