Skip to content

Commit

Permalink
First pass at adding support for WooCommerce subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrajesh committed May 8, 2019
1 parent d7e67ef commit 33d1375
Show file tree
Hide file tree
Showing 12 changed files with 407 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/admin/class-admin-settings-helper.php
Expand Up @@ -135,6 +135,11 @@ public function init() {


$this->add_misc_panel( $page );

if ( class_exists( 'WC_Subscriptions' ) ) {
$this->add_tab_details( 'subscriptions', isset( $tabs['subscriptions'] ) ? $tabs['subscriptions'] : array(), $panel_special );
}

// Save page for future reference.
$this->page = $page;

Expand Down
4 changes: 4 additions & 0 deletions src/bootstrap/class-bc-bootstrapper.php
Expand Up @@ -16,6 +16,8 @@
use BuddyCommerce\Core\Users\Handlers\BC_Address_Screen_Handler;
use BuddyCommerce\Core\Users\Filters\BC_URL_Filters;
use BuddyCommerce\Core\Users\Filters\BC_Condition_Filters;
use BuddyCommerce\Core\Users\Handlers\BC_Checkout_Endpoint_Screen_Handler;
use BuddyCommerce\Core\Users\Handlers\BC_View_Subscription_Screen_Handler;
use BuddyCommerce\Core\Users\Redirects\BC_Account_Redirects;
use BuddyCommerce\Core\Users\Handlers\BC_Tabs_Helper;
use BuddyCommerce\Core\Users\Handlers\BC_View_Order_Screen_Handler;
Expand Down Expand Up @@ -107,6 +109,8 @@ public function load() {
BC_Address_Screen_Handler::boot();
BC_View_Order_Screen_Handler::boot();
BC_Add_Payment_Methods_Screen_Handler::boot();
BC_View_Subscription_Screen_Handler::boot();
BC_Checkout_Endpoint_Screen_Handler::boot();

BC_URL_Filters::boot();
BC_Condition_Filters::boot();
Expand Down
34 changes: 34 additions & 0 deletions src/core/bc-conditional-functions.php
Expand Up @@ -214,3 +214,37 @@ function bcommerce_is_user_add_payment_methods() {

return $is;
}

/**
* Is it profile subscriptions page?
*
* @return bool
*/
function bcommerce_is_user_subscriptions() {
if ( ! bcommerce_is_user_nav_item_enabled( 'subscriptions' ) ) {
return false;
}

return bcommerce_is_current_tab( 'subscriptions', true );
}

/**
* Is it profile view subscription page?
*
* @return bool
*/
function bcommerce_is_user_view_subscription() {

if ( ! bcommerce_is_user_subscriptions() ) {
return false;
}

$slug = bcommerce_get_tab_slug( 'view-subscription', true );
if ( bcommerce_is_top_level_user_nav_item( 'subscriptions' ) ) {
$is = bp_is_current_action( $slug );
} else {
$is = bp_is_action_variable( $slug, 0 );
}

return $is;
}
34 changes: 34 additions & 0 deletions src/core/bc-general-functions.php
Expand Up @@ -160,6 +160,19 @@ function bcommerce_get_wc_tabs_details() {

);

if ( class_exists( 'WC_Subscriptions' ) ) {
$tabs['subscriptions'] = array(
'enabled' => 0,
'endpoint' => true,
'label' => __( 'Subscriptions', 'buddycommerce' ),
//'slug' => 'subscriptions',
'desc' => __( 'Subscription tab settings.', 'buddycommerce' ),
'is_top_level' => 0,
'redirect' => 1,
'redirect_description' => __( 'Redirect WooCommerce subscription page to BuddyPress profile page', 'buddycommerce' ),
);
}

return apply_filters( 'bcommerce_wc_tabs', $tabs );
}

Expand Down Expand Up @@ -356,6 +369,9 @@ function bcommerce_get_view_callback( $tab ) {
case 'checkout':
$callback = array( $screen, 'checkout' );
break;
case 'subscriptions':
$callback = array( $screen, 'subscriptions' );
break;
}

$callback = apply_filters( 'bcommerce_tab_view_callback', $callback, $tab );
Expand All @@ -379,6 +395,24 @@ function bcommerce_get_endpoint_slug( $endpoint ) {
return ! empty( $query_vars[ $endpoint ] ) ? $query_vars[ $endpoint ] : $endpoint;
}

/**
* Get endpoint from slug.
*
* @param string $slug current slug.
*
* @return string|null
*/
function bcommerce_get_endpoint_from_slug( $slug ) {
$query_vars = WC()->query->get_query_vars();
foreach ( $query_vars as $endpoint => $ep_slug ) {
if ( $ep_slug === $slug ) {
return $endpoint;
}
}

return null;
}

/**
* Get all wooCommerce end points.
*
Expand Down
29 changes: 29 additions & 0 deletions src/core/bc-link-functions.php
Expand Up @@ -168,3 +168,32 @@ function bcommerce_get_user_address_permalink( $user_id, $user_url = '', $addres
function bcommerce_get_user_payment_methods_permalink( $user_id, $user_url = '' ) {
return bcommerce_get_user_nav_item_permalink( $user_id, $user_url, 'payment_methods', true );
}



/**
* Get mapped subscriptions url.
*
* @param int $user_id user id.
* @param string $user_url user profile url.
*
* @return string
*/
function bcommerce_get_user_subscriptions_permalink( $user_id, $user_url = '' ) {
return bcommerce_get_user_nav_item_permalink( $user_id, $user_url, 'subscriptions', true );
}

/**
* Get View subscription url.
*
* @param int $user_id user id.
* @param string $user_url user profile url.
*
* @return string
*/
function bcommerce_get_user_view_subscription_permalink( $user_id, $user_url = '', $order_id ) {

$endpoint = bcommerce_get_endpoint_slug( 'view-subscription' );

return bcommerce_get_user_subscriptions_permalink( bp_loggedin_user_id() ) . trailingslashit( $endpoint ) . $order_id . '/';
}
41 changes: 41 additions & 0 deletions src/core/users/class-bc-screens.php
Expand Up @@ -129,6 +129,30 @@ public function add_payment_methods() {
bp_core_load_template( array( 'members/single/plugins' ) );
}

/**
* Load My Subscriptions content.
*
* Plugin: WooCommerce subscriptions.
*/
public function subscriptions() {

if ( bcommerce_is_user_view_subscription() ) {
return;
}

add_action( 'bp_template_content', array( $this, 'content_subscriptions' ) );
bp_core_load_template( array( 'members/single/plugins' ) );

}

/**
* Load View Order content.
*/
public function view_subscription() {
add_action( 'bp_template_content', array( $this, 'content_view_subscription' ) );
bp_core_load_template( array( 'members/single/plugins' ) );
}

/**
* Load content for Orders
*/
Expand Down Expand Up @@ -190,4 +214,21 @@ public function content_payment_methods() {
public function content_add_payment_methods() {
bcommerce_get_template_part( 'members/add-payment-method' );
}

/**
* Load content for Orders
* Plugin: WooCommerce subscriptions.
*/
public function content_subscriptions() {
bcommerce_get_template_part( 'members/subscriptions' );
}

/**
* Load content for View subscription page
* Plugin: WooCommerce subscriptions.
*/
public function content_view_subscription() {
bcommerce_get_template_part( 'members/view-subscription' );
}

}
22 changes: 22 additions & 0 deletions src/core/users/filters/class-bc-url-filters.php
Expand Up @@ -68,6 +68,10 @@ private function register() {
// woocommerce_get_cancel_order_url
//woocommerce_get_cancel_order_url_raw
// woocommerce_get_edit_order_url

// Woo Subscriptions

add_filter( 'wcs_get_view_subscription_url', array( $this, 'filter_view_subscription_url' ), 10, 2 );
}

/**
Expand Down Expand Up @@ -205,4 +209,22 @@ public function filter_view_order_url( $url, $order ) {

return bcommerce_get_user_view_order_permalink( bp_loggedin_user_id(), bp_loggedin_user_domain(), $order->get_id() );
}


/**
* Filter View subscription url for WooCommerce Subscriptions plugin.
*
* @param string $url url.
* @param int $order_id order id.
*
* @return string
*/
public function filter_view_subscription_url( $url, $order_id ) {

if ( ! bcommerce_is_user_nav_item_enabled( 'subscriptions' ) ) {
return $url;
}

return bcommerce_get_user_view_subscription_permalink( bp_loggedin_user_id(), bp_loggedin_user_domain(), $order_id );
}
}
@@ -0,0 +1,87 @@
<?php
/**
* BuddyCommerce Checkout endpoint screen handler.
*
* @package BuddyCommerce
* @subpackage Core\Users\Handlers
* @copyright Copyright (c) 2019, Brajesh Singh
* @license https://www.gnu.org/licenses/gpl.html GNU Public License
* @author Brajesh Singh
* @since 1.0.0
*/

namespace BuddyCommerce\Core\Users\Handlers;

use BuddyCommerce\Core\Users\BC_Screens;

// Do not allow direct access over web.
defined( 'ABSPATH' ) || exit;

/**
* Checkout endpoint screen handler.
*/
class BC_Checkout_Endpoint_Screen_Handler {

/**
* Setup the bootstrapper.
*
* @return BC_Checkout_Endpoint_Screen_Handler
*/
public static function boot() {
static $self;

if ( ! is_null( $self ) ) {
return $self; // already booted.
}

$self = new self();
$self->register();
return $self;
}

/**
* Register filters.
*/
private function register() {
add_action( 'bp_actions', array( $this, 'maybe_handle' ) );
}

/**
* If this is checkout end point, handle it.
*/
public function maybe_handle() {
// we may update it for admin in future to let them update others address.
if ( ! bp_is_my_profile() || ! bcommerce_is_user_checkout() ) {
return;
}

if ( bcommerce_is_top_level_user_nav_item( 'checkout' ) ) {
$slug = bp_current_action();
$value = bp_action_variable( 0 );
} else {
$slug = bp_action_variable( 0 );
$value = bp_action_variable( 1 );
}

$endpoint = bcommerce_get_endpoint_from_slug( $slug );

if ( ! $endpoint ) {
return;
}

// if not a valid end point for us.
if ( ! in_array( $endpoint, array(
'order-pay',
'order-received',
'add-payment-method',
'delete-payment-method',
'set-default-payment-method',
) ) ) {
return;
}

set_query_var( $endpoint, $value );

BC_Screens::get_instance()->view_order();
}
}
52 changes: 52 additions & 0 deletions src/core/users/handlers/class-bc-tabs-helper.php
Expand Up @@ -168,6 +168,10 @@ public function setup_nav() {
$this->add_downloads_tab( $user_id, $args );
$this->add_addresses_tab( $user_id, $args );
$this->add_payment_methods_tab( $user_id, $args );

if ( class_exists( 'WC_Subscriptions' ) ) {
$this->add_subscriptions_tab( $user_id, $args );
}
}

/**
Expand Down Expand Up @@ -534,6 +538,54 @@ private function add_shop_tab( $user_id, $args ) {

}

/**
* Add Subscriptions tab for user.
*
* @param int $user_id user id.
* @param array $args args.
*/
private function add_subscriptions_tab( $user_id, $args ) {

$tab = 'subscriptions';
if ( ! bcommerce_is_user_nav_item_enabled( $tab ) ) {
return;
}

$tab_slug = bcommerce_get_tab_slug( $tab, true );

if ( bcommerce_is_top_level_user_nav_item( $tab ) ) {
bp_core_new_nav_item(
array(
'name' => $this->get_label( $tab, __( 'Subscriptions', 'buddycommerce' ) ),
'slug' => $tab_slug,
'position' => bcommerce_get_user_nav_item_position_setting( $tab ),
'screen_function' => bcommerce_get_view_callback( $tab ),
'default_subnav_slug' => $tab,
'show_for_displayed_user' => $args['access'],
)
);

return;
}

$parent_slug = bcommerce_get_user_nav_item_parent_slug_setting( $tab );
if ( ! $parent_slug ) {
$parent_slug = bcommerce_get_tab_slug( 'shop', false );
}

bp_core_new_subnav_item(
array(
'name' => $this->get_label( $tab, __( 'Subscriptions', 'buddycommerce' ) ),
'slug' => $tab_slug,
'parent_url' => trailingslashit( $args['user_domain'] . $parent_slug ),
'parent_slug' => $parent_slug,
'screen_function' => bcommerce_get_view_callback( $tab ),
'position' => bcommerce_get_user_nav_item_position_setting( $tab ),
'user_has_access' => $args['access'],
)
);
}

/**
* Get teh default sub tab for a top level tab.
*
Expand Down

0 comments on commit 33d1375

Please sign in to comment.