File: /var/www/vhosts/uyarreklam.com.tr/httpdocs/controllers.tar
v1/class-yith-wcwl-rest-v1-controller.php 0000644 00000005566 15154430342 0014332 0 ustar 00 <?php
/**
* Rest controller abstract class
*/
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
if ( ! class_exists( 'YITH_WCWL_Rest_V1_Controller' ) ) {
abstract class YITH_WCWL_Rest_V1_Controller extends WP_REST_Controller {
/**
* Get item permission
*
* TODO: implement this method
*
* @param WP_REST_Request $request
* @return true|WP_Error
*/
public function get_item_permissions_check( $request ) {
return true;
}
/**
* Get item permission
*
* TODO: implement this method
*
* @param $request
* @return true|WP_Error
*/
public function get_items_permissions_check( $request ) {
return true;
}
/**
* @param YITH_WCWL_Wishlist[] $wishlists The wishlist objects.
*
* @return array
*/
protected function prepare_wishlists_for_rest( $wishlists ) {
$wishlists_for_rest = array();
foreach ( $wishlists as $wishlist ) {
$wishlists_for_rest[ $wishlist->get_id() ] = $this->prepare_wishlist_for_rest( $wishlist );
}
return $wishlists_for_rest;
}
/**
* Prepare wishlist object for Rest response
*
* @param int|YITH_WCWL_Wishlist $wishlist The wishlist object or its id.
*
* @return array|false
*/
protected function prepare_wishlist_for_rest( $wishlist ) {
$wishlist = $wishlist instanceof YITH_WCWL_Wishlist ? $wishlist : YITH_WCWL_Wishlist_Factory::get_wishlist( $wishlist );
if ( ! $wishlist ) {
return false;
}
return array_merge(
$wishlist->get_data(),
array(
'name' => $wishlist->get_formatted_name(),
'token' => $wishlist->get_token(),
'session_id' => $wishlist->get_session_id(),
)
);
}
/**
* @param YITH_WCWL_Wishlist[] $wishlists The wishlist objects.
*
* @return array
*/
protected function prepare_products_for_rest( $products ) {
$products_for_rest = array();
foreach ( $products as $product ) {
$products_for_rest[ $product instanceof WC_Product ? $product->get_id() : absint( $product ) ] = $this->prepare_product_for_rest( $product );
}
return $products_for_rest;
}
/**
* Prepare wishlist object for Rest response
*
* @param int|WC_Product $product The product object or its id.
*
* @return array|false
*/
protected function prepare_product_for_rest( $product, $additional_data = array() ) {
$product = $product instanceof WC_Product ? $product : wc_get_product( $product );
if ( ! $product ) {
return false;
}
$product_id = $product->get_id();
$product_lists = yith_wcwl_wishlists()->get_wishlists_for_product( $product_id );
return array_merge(
array(
'name' => $product->get_name(),
'isAdded' => ! ! $product_lists,
'count' => yith_wcwl_wishlists()->count_add_to_wishlist( $product_id ),
'wishlists' => $product_lists,
),
is_array( $additional_data ) ? $additional_data : array()
);
}
}
}
v1/class-yith-wcwl-rest-v1-items-controller.php 0000644 00000022036 15154430342 0015440 0 ustar 00 <?php
/**
* REST API Wishlist Items controller class.
*
* @package YITH\Wishlist\RestApi
*/
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'YITH_WCWL_Rest_V1_Items_Controller' ) ) {
/**
* REST API Wishlist Items controller class.
*
* @package YITH\Wishlist\RestApi
*/
class YITH_WCWL_Rest_V1_Items_Controller extends YITH_WCWL_Rest_V1_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'yith/wishlist/v1';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'items';
/**
* Register the routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'add_item' ),
'permission_callback' => array( $this, 'create_item_permissions_check' ),
'args' => array(
'product_id' => array(
'description' => _x( 'The product ID', '[REST-API] The schema field description', 'yith-woocommerce-wishlist' ),
'type' => 'integer',
'required' => true,
),
'wishlist_id' => array(
'description' => _x( 'The Wishlist ID', '[REST-API] The schema field description', 'yith-woocommerce-wishlist' ),
'type' => array( 'integer', 'string' ),
),
'quantity' => array(
'description' => _x( 'The quantity to add in wishlist', '[REST-API] The schema field description', 'yith-woocommerce-wishlist' ),
'type' => 'integer',
),
'user_id' => array(
'description' => _x( 'The User ID', '[REST-API] The schema field description', 'yith-woocommerce-wishlist' ),
'type' => 'integer',
),
'dateadded' => array(
'description' => _x( 'The timestamp to use as added date.', '[REST-API] The schema field description', 'yith-woocommerce-wishlist' ),
'type' => 'integer',
),
'wishlist_name' => array(
'description' => _x( 'The wishlist name.', '[REST-API] The schema field description', 'yith-woocommerce-wishlist' ),
'type' => 'string',
),
'wishlist_visibility' => array(
'description' => _x( 'The wishlist visibility value.', '[REST-API] The schema field description', 'yith-woocommerce-wishlist' ),
'type' => 'integer',
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'remove_item' ),
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
'args' => array(
'product_id' => array(
'description' => _x( 'The product ID', '[REST-API] The schema field description', 'yith-woocommerce-wishlist' ),
'type' => 'integer',
'required' => true,
),
'wishlist_id' => array(
'description' => _x( 'The Wishlist ID', '[REST-API] The schema field description', 'yith-woocommerce-wishlist' ),
'type' => array( 'integer', 'string' ),
),
'user_id' => array(
'description' => _x( 'The User ID', '[REST-API] The schema field description', 'yith-woocommerce-wishlist' ),
'type' => 'integer',
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/move',
array(
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'move_item' ),
'permission_callback' => '__return_true',
'args' => array(
'product_id' => array(
'description' => _x( 'The product ID', '[REST-API] The schema field description', 'yith-woocommerce-wishlist' ),
'type' => 'integer',
'required' => true,
),
'destination_wishlist' => array(
'description' => _x( 'The destination wishlist ID', '[REST-API] The schema field description', 'yith-woocommerce-wishlist' ),
'type' => array( 'integer', 'string' ),
'required' => true,
),
'origin_wishlist' => array(
'description' => _x( 'The origin wishlist ID', '[REST-API] The schema field description', 'yith-woocommerce-wishlist' ),
'type' => 'integer',
),
'wishlist_name' => array(
'description' => _x( 'The wishlist name.', '[REST-API] The schema field description', 'yith-woocommerce-wishlist' ),
'type' => 'string',
),
'wishlist_visibility' => array(
'description' => _x( 'The wishlist visibility value.', '[REST-API] The schema field description', 'yith-woocommerce-wishlist' ),
'type' => 'integer',
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Add item to a wishlist
*
* @param WP_REST_Request $request The rest request.
*
* @return WP_Error|WP_HTTP_Response|WP_REST_Response
*/
public function add_item( $request ) {
$args = $request->get_params();
$response = array( 'success' => true );
$creating_default = false;
if ( ! empty( $args[ 'wishlist_id' ] ) && 'default' === $args[ 'wishlist_id' ] ) {
$creating_default = true;
unset( $args[ 'wishlist_id' ] );
}
try {
$data = YITH_WCWL_Wishlists::get_instance()->add_item( $args );
$product_id = $args[ 'product_id' ];
$response = array(
'product_data' => array_merge(
$this->prepare_product_for_rest( $product_id ),
array(
'added_to' => $data[ 'wishlist_id' ],
)
),
);
if ( $creating_default ) {
$response[ 'wishlist_data' ] = $this->prepare_wishlist_for_rest( $data[ 'wishlist_id' ] );
}
} catch ( \Exception $e ) {
$response = array( 'success' => false, 'message' => $e->getMessage() );
}
return rest_ensure_response( $response );
}
/**
* Remove an item from a wishlist
*
* @param WP_REST_Request $request The rest request.
*
* @return WP_Error|WP_HTTP_Response|WP_REST_Response
*/
public function remove_item( $request ) {
$args = $request->get_params();
$response = array( 'success' => true );
try {
$data = YITH_WCWL_Wishlists::get_instance()->remove_item( $args );
$product_id = $args[ 'product_id' ];
$response = array(
'product_data' => array_merge(
array( 'removed_from' => $data[ 'wishlist_id' ] ),
$this->prepare_product_for_rest( $product_id )
),
);
} catch ( \Exception $e ) {
$response = array( 'success' => false, 'message' => $e->getMessage() );
}
return rest_ensure_response( $response );
}
/**
* Remove an item from a wishlist
*
* @param WP_REST_Request $request The rest request.
*
* @return WP_Error|WP_HTTP_Response|WP_REST_Response
*/
public function move_item( $request ) {
$args = $request->get_params();
try {
$result = YITH_WCWL_Wishlists::get_instance()->move( $args );
$response = array(
'moved' => $result[ 'moved' ],
'product_data' => $this->prepare_product_for_rest(
$args[ 'product_id' ],
array(
'moved_from' => $args[ 'origin_wishlist' ],
'moved_to' => 'new' === $args[ 'destination_wishlist' ] ? $result[ 'destination_wishlist' ]->get_id() : $args[ 'destination_wishlist' ],
)
),
);
if ( $result[ 'destination_wishlist' ] instanceof YITH_WCWL_Wishlist ) {
$response[ 'destination_wishlist' ] = $this->prepare_wishlist_for_rest( $result[ 'destination_wishlist' ] );
}
} catch ( \Exception $e ) {
$response = array( 'success' => false, 'message' => $e->getMessage() );
}
return rest_ensure_response( $response );
}
/**
* Create item permission checks
*
* @param WP_REST_Request $request
* @return true|WP_Error
*/
public function create_item_permissions_check( $request ) {
$wishlist = $this->get_wishlist_from_request( $request );
return ! $wishlist || $wishlist->current_user_can( 'add_to_wishlist' );
}
/**
* Delete item permission checks
*
* @param WP_REST_Request $request
* @return true|WP_Error
*/
public function delete_item_permissions_check( $request ) {
$wishlist = $this->get_wishlist_from_request( $request );
return ! $wishlist || $wishlist->current_user_can( 'remove_from_wishlist' );
}
/**
* Get wishlist object from request
*
* @param WP_REST_Request $request The request.
*
* @return YITH_WCWL_Wishlist|false
*/
protected function get_wishlist_from_request( $request ) {
$wishlist_id = $request->get_param( 'wishlist_id' );
$wishlist = false;
if ( $wishlist_id ) {
try {
$wishlist = new YITH_WCWL_Wishlist( $wishlist_id );
} catch ( Exception $exception ) {
}
}
return $wishlist;
}
}
}
v1/class-yith-wcwl-rest-v1-lists-controller.php 0000644 00000006137 15154430342 0015461 0 ustar 00 <?php
/**
* REST API Wishlist Lists controller class.
*
* @package YITH\Wishlist\RestApi
*/
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'YITH_WCWL_Rest_V1_Lists_Controller' ) ) {
/**
* REST API Wishlist Lists controller class.
*
* @package YITH\Wishlist\RestApi
*/
class YITH_WCWL_Rest_V1_Lists_Controller extends YITH_WCWL_Rest_V1_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'yith/wishlist/v1';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'lists';
/**
* Register the routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_lists' ),
'permission_callback' => '__return_true',
'args' => $this->get_endpoint_args_for_item_schema(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'create_list' ),
'permission_callback' => '__return_true',
'args' => array(
'wishlist_name' => array(
'description' => _x( 'The wishlist name.', '[REST-API] The schema field description', 'yith-woocommerce-wishlist' ),
'type' => 'string',
'required' => true,
),
'wishlist_visibility' => array(
'description' => _x( 'The wishlist visibility value.', '[REST-API] The schema field description', 'yith-woocommerce-wishlist' ),
'type' => 'integer',
'required' => true,
),
'user_id' => array(
'description' => _x( 'The unique identifier for the user.', '[REST-API] The schema field description', 'yith-woocommerce-wishlist' ),
'type' => 'integer',
),
'session_id' => array(
'description' => _x( 'The unique identifier for the session.', '[REST-API] The schema field description', 'yith-woocommerce-wishlist' ),
'type' => 'string',
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Get lists
*
* @return WP_Error|WP_HTTP_Response|WP_REST_Response
*/
public function get_lists() {
$response = array(
'lists' => $this->prepare_wishlists_for_rest( YITH_WCWL_Wishlists::get_instance()->get_current_user_wishlists() ),
);
return rest_ensure_response( $response );
}
public function create_list( $request ) {
$args = $request->get_params();
try {
$wishlist = YITH_WCWL_Wishlists::get_instance()->create( $args );
$response = array(
'wishlist_data' => $this->prepare_wishlist_for_rest( $wishlist ),
);
} catch ( YITH_WCWL_Exception $e ) {
$response = array(
'success' => false,
'message' => $e->getMessage(),
);
}
return rest_ensure_response( $response );
}
}
}
v1/class-yith-wcwl-rest-v1-products-controller.php 0000644 00000005647 15154430342 0016173 0 ustar 00 <?php
/**
* REST API Wishlist Products controller class.
*
* @package YITH\Wishlist\RestApi
*/
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'YITH_WCWL_REST_V1_Products_Controller' ) ) {
/**
* REST API Wishlist Products controller class.
*
* @package YITH\Wishlist\RestApi
*/
class YITH_WCWL_Rest_V1_Products_Controller extends YITH_WCWL_Rest_V1_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'yith/wishlist/v1';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'products';
/**
* Register the routes.
*/
public function register_routes() {
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_products_data' ),
'permission_callback' => '__return_true', // TODO: implement checks for permissions.
'args' => array(
'product_ids' => array(
'description' => _x( 'The list of product ids for which data is being requested.', '[REST-API] The schema field description', 'yith-woocommerce-wishlist' ),
'type' => 'array',
'required' => true,
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
) );
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<product_id>[\d]+)',
array(
'args' => array(
'product_id' => array(
'description' => _x( 'The unique identifier for the product.', '[REST-API] The schema field description', 'yith-woocommerce-wishlist' ),
'type' => 'integer',
'required' => true,
),
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_product_data' ),
'permission_callback' => '__return_true',
'args' => $this->get_endpoint_args_for_item_schema(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Get wishlist data related to multiple products
*
* @param WP_REST_Request $request The rest request.
*
* @return WP_Error|WP_HTTP_Response|WP_REST_Response
*/
public function get_products_data( $request ) {
$args = $request->get_params();
$products_data = array();
if ( ! empty( $args[ 'product_ids' ] ) ) {
$products_data = $this->prepare_products_for_rest( $args[ 'product_ids' ] );
}
return rest_ensure_response( $products_data );
}
/**
* Get wishlist data related to a single products
*
* @param WP_REST_Request $request The rest request.
*
* @return WP_Error|WP_HTTP_Response|WP_REST_Response
*/
public function get_product_data( $request ) {
$args = $request->get_params();
$product_id = $args[ 'product_id' ];
$response = $this->prepare_product_for_rest($product_id);
return rest_ensure_response( $response );
}
}
}