Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions includes/Core/Ecommerce/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ abstract class AbstractPlatform implements PlatformInterface {
public function is_integrated() {
return Settings::instance()->is_integrated() && Settings::instance()->platform() === $this->get_name();
}

public function is_subscriptions_active() {
return false;
}
}
2 changes: 2 additions & 0 deletions includes/Core/Ecommerce/Platforms/PlatformInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public function categories( array $args = array() );
public function register_hooks();

public function get_name();

public function is_subscriptions_active();
}
175 changes: 127 additions & 48 deletions includes/Core/Ecommerce/Platforms/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use WeDevs\WeMail\Rest\Resources\Ecommerce\WooCommerce\CategoryResource;
use WeDevs\WeMail\Rest\Resources\Ecommerce\WooCommerce\OrderResource;
use WeDevs\WeMail\Rest\Resources\Ecommerce\WooCommerce\ProductResource;
use WeDevs\WeMail\Rest\Resources\Ecommerce\WooCommerce\SubscriptionResource;
use WeDevs\WeMail\Traits\Singleton;
use WP_Post;

Expand Down Expand Up @@ -68,6 +69,43 @@
);
}

/**
* Get subscriptions from WooCommerce store
*
* @param array $args
*
* @return array
*/
public function subscriptions( array $args = array() ) {
if ( ! $this->is_subscriptions_active() ) {
return array(
'data' => array(),
'total' => 0,
'current_page' => 1,
'total_page' => 0,
);
}

$args = wp_parse_args(
$args,
array(
'limit' => isset( $args['limit'] ) ? intval( $args['limit'] ) : 100,
'page' => isset( $args['page'] ) ? intval( $args['page'] ) : 1,
'paginate' => true,
'type' => 'shop_subscription',
)
);

$data = wc_get_orders( $args );

return array(
'data' => SubscriptionResource::collection( $data->orders ),
'total' => $data->total,
'current_page' => intval( $args['page'] ),
'total_page' => $data->max_num_pages,
);
}

/**
* Get orders from WooCommerce store
*
Expand Down Expand Up @@ -113,17 +151,13 @@
add_action( 'woocommerce_add_to_cart', array( $this, 'handle_add_to_cart' ), 10, 6 );
add_action( 'woocommerce_cart_updated', array( $this, 'handle_cart_updated' ), 10, 0 );
add_action( 'woocommerce_remove_cart_item', array( $this, 'handle_remove_cart_item' ), 10, 2 );
add_action( 'woocommerce_cart_emptied', array( $this, 'handle_cart_emptied' ), 10, 0 );

// New order created hook
add_action( 'woocommerce_new_order', array( $this, 'handle_new_order' ), 10, 2 );

// Order status changed hook (handles pending payment, completed, etc.)
add_action( 'woocommerce_order_status_changed', array( $this, 'handle_order_status_changed' ), 10, 4 );

// Pending payment status specific hook
add_action( 'woocommerce_order_status_pending', array( $this, 'handle_pending_payment' ), 10, 1 );

add_action( 'woocommerce_order_refunded', array( $this, 'create_order_refund' ), 10, 2 );
add_action( 'woocommerce_refund_deleted', array( $this, 'delete_order_refund' ), 10, 2 );
add_action( 'after_delete_post', array( $this, 'delete' ), 10, 2 );
Expand All @@ -135,6 +169,12 @@
add_action( 'created_product_cat', array( $this, 'handle_category' ), 10, 2 );
add_action( 'edited_product_cat', array( $this, 'handle_category' ), 10, 2 );
add_action( 'delete_product_cat', array( $this, 'handle_category_delete' ), 10, 3 );

// WooCommerce Subscriptions hooks
add_action( 'woocommerce_new_subscription', array( $this, 'handle_new_subscription' ), 10, 1 );
add_action( 'woocommerce_subscription_status_cancelled', array( $this, 'handle_subscription_cancelled' ), 10, 1 );
add_action( 'woocommerce_subscription_status_expired', array( $this, 'handle_subscription_expired' ), 10, 1 );
add_action( 'woocommerce_subscription_renewal_payment_complete', array( $this, 'handle_subscription_renewal_payment_complete' ), 10, 1 );
}

/**
Expand Down Expand Up @@ -180,28 +220,15 @@
$this->send_cart_data( 'remove_cart_item' );
}

/**
* Handle cart emptied event
*/
public function handle_cart_emptied() {
if ( $this->is_recovering || ! Settings::instance()->is_enabled() ) {
return;
}

$this->send_cart_data( 'cart_emptied' );

$this->reset_cart_key();
}

/**
* Handle cart recovery from abandoned cart email link
*/
public function handle_cart_recovery() {
if ( ! isset( $_GET['wemail-recover-cart'] ) ) {

Check warning on line 227 in includes/Core/Ecommerce/Platforms/WooCommerce.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Processing form data without nonce verification.
return;
}

$token = sanitize_text_field( wp_unslash( $_GET['wemail-recover-cart'] ) );

Check warning on line 231 in includes/Core/Ecommerce/Platforms/WooCommerce.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Processing form data without nonce verification.

if ( empty( $token ) ) {
return;
Expand All @@ -211,7 +238,7 @@
return;
}

$coupon_code = isset( $_GET['coupon'] ) ? sanitize_text_field( wp_unslash( $_GET['coupon'] ) ) : '';

Check warning on line 241 in includes/Core/Ecommerce/Platforms/WooCommerce.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Processing form data without nonce verification.

Check warning on line 241 in includes/Core/Ecommerce/Platforms/WooCommerce.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Processing form data without nonce verification.

$response = wemail()->api
->ecommerce()
Expand Down Expand Up @@ -318,37 +345,6 @@
->put( $payload );
}

/**
* Handle pending payment status
*
* @param int $order_id Order ID
*/
public function handle_pending_payment( $order_id ) {
$order = wc_get_order( $order_id );

if ( ! $order ) {
return;
}

if ( ! $this->is_valid_order_item( $order->get_type() ) ) {
return;
}

if ( ! Settings::instance()->is_enabled() ) {
return;
}

$payload = OrderResource::single( $order );

wemail()->api
->send_json()
->ecommerce()
->orders( $order_id )
->put( $payload );

$this->reset_cart_key();
}

/**
* Handle new order created
*
Expand Down Expand Up @@ -570,6 +566,15 @@
return class_exists( 'WooCommerce' );
}

/**
* Check if WooCommerce Subscriptions is active
*
* @return bool
*/
public function is_subscriptions_active() {
return class_exists( 'WC_Subscription' );
}

/**
* Get integration name
*
Expand Down Expand Up @@ -672,4 +677,78 @@
->categories( $term_id )
->delete();
}

/**
* Handle new subscription created
*
* @param \WC_Subscription $subscription Subscription object
*/
public function handle_new_subscription( $subscription ) {
if ( is_numeric( $subscription ) ) {
$subscription = wcs_get_subscription( $subscription );
}

if ( ! $subscription ) {
return;
}

$this->send_subscription_data( $subscription, 'subscription_created' );
}

/**
* Handle subscription cancelled
*
* @param \WC_Subscription $subscription Subscription object
*/
public function handle_subscription_cancelled( $subscription ) {
$this->send_subscription_data( $subscription, 'subscription_cancelled' );
}

/**
* Handle subscription expired
*
* @param \WC_Subscription $subscription Subscription object
*/
public function handle_subscription_expired( $subscription ) {
$this->send_subscription_data( $subscription, 'subscription_expired' );
}

/**
* Handle subscription renewal payment complete
*
* @param \WC_Subscription $subscription Subscription object
*/
public function handle_subscription_renewal_payment_complete( $subscription ) {
$this->send_subscription_data(
$subscription, 'subscription_renewal_payment_complete', array(
'renewal_complete' => true,
)
);
}
/**
* Send subscription data to weMail API
*
* @param \WC_Subscription $subscription Subscription object
* @param string $event Event type
* @param array $extra Additional payload data
*/
private function send_subscription_data( $subscription, $event, $extra = array() ) {
if ( ! $subscription ) {
return;
}

if ( ! Settings::instance()->is_enabled() ) {
return;
}

$payload = SubscriptionResource::single( $subscription );
$payload['event'] = $event;
$payload = array_merge( $payload, $extra );

wemail()->api
->send_json()
->ecommerce()
->subscriptions( $subscription->get_id() )
->put( $payload );
}
}
28 changes: 25 additions & 3 deletions includes/Rest/Ecommerce/Ecommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Ecommerce extends RestController {
public function register_routes() {
$this->post( '/(?P<source>woocommerce|edd)/settings', 'settings' );
$this->get( '/(?P<source>woocommerce|edd)/(?P<resource>products|orders|categories)', 'resource', 'permission' );
$this->get( '/(?P<source>woocommerce)/(?P<resource>subscriptions)', 'resource', 'permission' );
$this->get( '/(?P<source>woocommerce)/status', 'status', 'permission' );
$this->post( '/coupons', 'create_coupon', 'permission' );
$this->delete( '/disconnect', 'disconnect', 'permission' );
}
Expand All @@ -38,9 +40,10 @@ public function settings( $request ) {

$body = array_merge(
$body, array(
'currency' => $platform->currency(),
'currency_symbol' => $platform->currency_symbol(),
'platform' => $platform->get_name(),
'currency' => $platform->currency(),
'currency_symbol' => $platform->currency_symbol(),
'platform' => $platform->get_name(),
'subscriptions_enabled' => $platform->is_subscriptions_active(),
)
);
// Update settings to wp options
Expand All @@ -59,6 +62,25 @@ public function settings( $request ) {
return new \WP_REST_Response( $response );
}

/**
* Platform status endpoint
*
* @param \WP_REST_Request $request
*/
public function status( $request ) {
$platform = $request->get_param( 'source' );

/** @var PlatformInterface $platform */
$platform = wemail()->ecommerce->platform( $platform );

return new \WP_REST_Response(
array(
'is_active' => $platform->is_active(),
'subscriptions_enabled' => $platform->is_subscriptions_active(),
)
);
}

/**
* Access products, orders or customers resource
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* @inheritDoc
*/
public function blueprint( $resource ) {

Check warning on line 12 in includes/Rest/Resources/Ecommerce/WooCommerce/ProductResource.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

It is recommended not to use reserved keyword "resource" as function parameter name. Found: $resource
/** @var $resource \WC_Product */

$thumbnail = wp_get_attachment_image_url( $resource->get_image_id(), 'woocommerce_thumbnail' );
Expand All @@ -23,6 +23,7 @@
'permalink' => $resource->get_permalink(),
'thumbnail' => $thumbnail ? $thumbnail : null,
'categories' => $resource->get_category_ids(),
'type' => $resource->get_type(),
'source' => 'woocommerce',
);
}
Expand All @@ -31,7 +32,7 @@
* @param \WC_Product $resource
* @return string
*/
public function get_resource_name( \WC_Product $resource ) {

Check warning on line 35 in includes/Rest/Resources/Ecommerce/WooCommerce/ProductResource.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

It is recommended not to use reserved keyword "resource" as function parameter name. Found: $resource
if ( $resource->is_type( 'variation' ) ) {
$attributes = $resource->get_attributes();
$variation_names = array();
Expand Down
Loading
Loading