Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Get pmpro-woocommerce global variables from the main blog. #197

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
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
82 changes: 82 additions & 0 deletions includes/multisite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* Compatibility file for Multisite installations.
*/

/**
* Get Membership Discounts from the main site settings where possible.
* @since TBD
*/
function pmprowoo_globals_subsite() {
global $pmprowoo_product_levels, $pmprowoo_gift_codes, $pmprowoo_member_discounts, $pmprowoo_discounts_on_subscriptions;

// Let's only do this in a multisite environment if we're not on the main site.
if ( ! is_multisite() || is_main_site() ) {
return;
}

// Is PMPro network subsite installed?
if ( ! function_exists( 'pmpro_multisite_membership_init' ) ) {
return;
}

// Is the WooCommerce integration active?
if ( ! function_exists( 'pmprowoo_init' ) ) {
return;
}

// Let's make sure these plugins are not network activated.
$subsite_only_plugins = array(
'pmpro-network-subsite',
'pmpro-woocommerce',
);

foreach ( $subsite_only_plugins as $plugin ) {
if ( is_plugin_active_for_network( $plugin . '/' . $plugin . '.php' ) ) {
return;
}
}

// Get the main site ID.
$main_site_id = pmpro_multisite_get_main_site_ID();

// No main site found, let's bail.
if ( empty( $main_site_id ) ) {
return;
}



// Get all Product Membership Levels
if ( empty( $pmprowoo_product_levels ) ) {
$pmprowoo_product_levels = get_blog_option( $main_site_id, '_pmprowoo_product_levels' );
if ( empty( $pmprowoo_product_levels ) ) {
$pmprowoo_product_levels = array();
}
}

// Get all Gift Membership Codes
if ( empty( $pmprowoo_gift_codes ) ) {
$pmprowoo_gift_codes = get_blog_option( $main_site_id, '_pmprowoo_gift_codes' );
if ( empty( $pmprowoo_gift_codes ) ) {
$pmprowoo_gift_codes = array();
}
}

// Get all Membership Discounts
if ( empty( $pmprowoo_member_discounts ) ) {
$pmprowoo_member_discounts = get_blog_option( $main_site_id, '_pmprowoo_member_discounts' );
if ( empty( $pmprowoo_member_discounts ) ) {
$pmprowoo_member_discounts = array();
}
}

// Apply Discounts to Subscriptions
if ( empty( $pmprowoo_discounts_on_subscriptions ) ) {
$pmprowoo_discounts_on_subscriptions = get_blog_option( $main_site_id, '_pmprowoo_discounts_on_subscriptions' );
if ( empty( $pmprowoo_discounts_on_subscriptions ) ) {
$pmprowoo_discounts_on_subscriptions = array();
}
}
}
add_action( 'init', 'pmprowoo_globals_subsite', 20 );
7 changes: 6 additions & 1 deletion pmpro-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ function pmprowoo_init() {
require_once( dirname( __FILE__ ) . '/includes/pmpro-gift-levels.php' );
}

// Load multisite compatibility if running on a multisite.
if ( is_multisite() ) {
require_once( dirname( __FILE__ ) . '/includes/multisite.php' );
}

// If MMPU is active, allow for multiple membership products in your cart
if ( defined( 'PMPROMMPU_VER') ) {
remove_filter( 'woocommerce_is_purchasable', 'pmprowoo_is_purchasable', 10, 2 );
Expand Down Expand Up @@ -1019,4 +1024,4 @@ function pmprowoo_compatible_for_hpos() {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
}
}
add_action( 'before_woocommerce_init', 'pmprowoo_compatible_for_hpos' );
add_action( 'before_woocommerce_init', 'pmprowoo_compatible_for_hpos' );