Created
December 9, 2023 22:15
-
-
Save alexclst/3aa04e8ec4a6b549a9b62610e13d6a90 to your computer and use it in GitHub Desktop.
A WP MU plugin that enforces sandbox settings on Paid Memberships Pro. Edit settings as needed in your copy.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: PM Pro Sandbox | |
* Description: Makes sure that PM Pro is in sandbox mode by forcing the developer's toolkit to run with specific settings, and killing off all pmpro plugins if it isn't found. | |
* Author: Tenseg LLC | |
* Author URI: https://www.tenseg.net | |
* Text Domain: tg-pmpro-sandbox | |
* Version: 1.0.0 | |
* | |
* @package tg-pmpro-sandbox | |
*/ | |
class TG_PMPro_Sandbox { | |
/** | |
* Settings | |
* | |
* @var array of settings | |
*/ | |
private $settings = [ | |
'expire_memberships' => '1', | |
'expiration_warnings' => '1', | |
'credit_card_expiring' => '1', | |
'ipn_debug' => 'dev@tenseg.net', | |
'authnet_silent_post_debug' => '', | |
'stripe_webhook_debug' => '', | |
'ins_debug' => '', | |
'redirect_email' => 'dev@tenseg.net', | |
'checkout_debug_email' => 'dev@tenseg.net', | |
'checkout_debug_when' => '', | |
'view_as_enabled' => false, | |
'gateway_environment' => 'sandbox', | |
]; | |
/** | |
* Class constructor | |
*/ | |
public function __construct() { | |
add_action( 'plugins_loaded', [$this, 'pmprotoolkit_loader'] ); | |
add_filter( 'plugin_action_links', [$this, 'disable_plugin_actions'], 10, 4 ); | |
add_filter( 'default_option_pmprodev_options', [$this, 'force_pmprodev_options'], 10, 2 ); | |
add_filter( 'option_pmprodev_options', [$this, 'force_pmprodev_options'], 10, 2 ); | |
add_filter( 'default_option_pmpro_gateway_environment', [$this, 'force_pmpro_gateway_environment'], 10, 2 ); | |
add_filter( 'option_pmpro_gateway_environment', [$this, 'force_pmpro_gateway_environment'], 10, 2 ); | |
} | |
/** | |
* Checks for the PMPro dev toolkit,manage pmpro plugins accordingly. | |
* | |
* Called by action: plugins_loaded | |
* | |
* @return void | |
*/ | |
public function pmprotoolkit_loader() { | |
$active_plugins = get_option( 'active_plugins' ); | |
// check for the toolkit | |
if ( file_exists( WP_PLUGIN_DIR . '/pmpro-toolkit/pmpro-toolkit.php' ) ) { | |
require_once WP_PLUGIN_DIR . '/pmpro-toolkit/pmpro-toolkit.php'; | |
add_action( 'admin_notices', function () { | |
if ( 'plugins' === get_current_screen()->id ) { | |
echo "<div class='notice'><p>The PM Pro Developer's Toolkit is running, feel free to activate other PM Pro plugins in this sandbox.</p></div>"; | |
} | |
} ); | |
} else { | |
foreach ( $active_plugins as $plugin ) { | |
if ( strpos( $plugin, 'pmpro' ) === 0 || strpos( $plugin, 'paid-memberships-pro' ) === 0 ) { | |
deactivate_plugins( $plugin ); | |
} | |
} | |
add_action( 'admin_notices', function () { | |
if ( 'plugins' === get_current_screen()->id ) { | |
echo "<div class='error'><p>The PM Pro Developer's Toolkit is required to run PM Pro on this site.</p></div>"; | |
} | |
} ); | |
} | |
} | |
/** | |
* Turn off plugin actions for the toolkit. | |
* | |
* @param array $actions | |
* @param string $plugin_file | |
* @param array $plugin_data | |
* @param string $context | |
* @return array | |
*/ | |
public function disable_plugin_actions( $actions, $plugin_file, $plugin_data, $context ) { | |
if ( str_starts_with( $plugin_file, 'pmpro-toolkit' ) ) { | |
return []; | |
} | |
return $actions; | |
} | |
/** | |
* Force the PMPro dev settings based on what is in the config file. | |
* | |
* Called by filter: option_pmprodev_options | |
* Called by filter: default_option_pmprodev_options | |
* | |
* @param mixed $value the option's value | |
* @param string $option the option's name | |
* @return mixed the option's value | |
*/ | |
function force_pmprodev_options( $value, $option ) { | |
return $this->settings; | |
} | |
/** | |
* Force the PMPro gateway environment based on what is in the config file. | |
* | |
* Called by filter: option_pmpro_gateway_environment | |
* Called by filter: default_option_pmpro_gateway_environment | |
* | |
* @param mixed $value the option's value | |
* @param string $option the option's name | |
* @return mixed the option's value | |
*/ | |
function force_pmpro_gateway_environment( $value, $option ) { | |
return $this->settings['gateway_environment']; | |
} | |
} | |
$tg_pmpro_sandbox = new TG_PMPro_Sandbox(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment