Forked from moon0326/a-woocommerce-calypso-bridge-helper.php
Created
March 3, 2023 07:06
-
-
Save adrianduffell/5ad497b7e0a0a0f7876d538304001aaf to your computer and use it in GitHub Desktop.
This file contains hidden or 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: WooCommerce Calypso Bridge Helper | |
* Plugin URI: https://woocommerce.com | |
* Description: Utility to assist testing wc-calypso-bridge locally | |
* Author: WooCommerce | |
* Version: 0.1 | |
*/ | |
class Atomic_Plan_Manager { | |
const ECOMMERCE_PLAN_SLUG = 'ecommerce'; | |
public static function current_plan_slug() { | |
return self::ECOMMERCE_PLAN_SLUG; | |
} | |
} | |
if ( ! class_exists( 'WPCOM_Features' ) ) { | |
class WPCOM_Features { | |
const ECOMMERCE_MANAGED_PLUGINS = 'ECOMMERCE_MANAGED_PLUGINS'; | |
const INSTALL_PLUGINS = 'INSTALL_PLUGINS'; | |
} | |
} | |
if ( ! function_exists( 'wpcom_site_has_feature' ) ) { | |
function wpcom_site_has_feature( $feature, $blog_id = 0) { | |
$at_options = get_option( 'at_options' ); | |
$is_ecom = isset( $at_options[ 'plan_slug' ] ) && $at_options[ 'plan_slug' ] === 'ecommerce'; | |
switch ( $feature ) { | |
case WPCOM_Features::ECOMMERCE_MANAGED_PLUGINS: | |
return $is_ecom; | |
case WPCOM_Features::INSTALL_PLUGINS: | |
return false; // Free Trial | |
default: | |
return $is_ecom; | |
} | |
} | |
} | |
function woocommerce_ecommplan_helper_handler() { | |
$action = isset( $_GET[ 'ecommplan_action' ] ) ? $_GET[ 'ecommplan_action' ] : false; | |
if ( ! $action ) { | |
return; | |
} | |
// Activate ecomm plan mode. | |
if ( 'activate_ecomm' === $action && ! woocommerce_ecommplan_helper_is_ecomm_plan() ) { | |
woocommerce_ecommplan_helper_activate( plugin_basename( __FILE__ ) ); | |
} | |
// Disable ecomm plan mode. | |
if ('activate_biz' === $action && woocommerce_ecommplan_helper_is_ecomm_plan() ) { | |
woocommerce_ecommplan_helper_disable_ecomm_plan(); | |
} | |
} | |
function woocommerce_ecommplan_helper_is_ecomm_plan() { | |
$at_options = get_option( 'at_options' ); | |
if ( ! $at_options ) { | |
return false; | |
} | |
return 'ecommerce' === $at_options['plan_slug']; | |
} | |
function woocommerce_ecommplan_helper_enable_ecomm_plan() { | |
update_option( 'at_options', array( 'plan_slug' => 'ecommerce' ) ); | |
} | |
function woocommerce_ecommplan_helper_disable_ecomm_plan() { | |
update_user_meta( get_current_user_id(), 'calypsoify', 0 ); | |
delete_option( 'at_options' ); | |
wp_safe_redirect( admin_url( 'plugins.php' ) ); | |
} | |
function woocommerce_ecommplan_helper_activate( $plugin ) { | |
if( plugin_basename( __FILE__ ) !== $plugin ) { | |
return; | |
} | |
// By default we will set the env to ecom plan mode. | |
woocommerce_ecommplan_helper_enable_ecomm_plan(); | |
// redirect to the calypsoified dashboard. | |
exit( wp_safe_redirect( admin_url( 'admin.php?page=wc-admin&calypsoify=1' ) ) ); | |
} | |
function woocommerce_ecommplan_helper_register_pages() { | |
if ( function_exists( 'wc_calypso_bridge_connect_page' ) ) { | |
wc_calypso_bridge_connect_page( | |
array( | |
'screen_id' => 'plugins', | |
'menu' => 'plugins.php', | |
) | |
); | |
} | |
} | |
$plugin_file = plugin_basename( __FILE__ ); | |
add_filter( "plugin_action_links_{$plugin_file}", __NAMESPACE__ . '\woocommerce_ecommplan_helper_plugin_action_links', 10, 4 ); | |
add_action( 'admin_init', 'woocommerce_ecommplan_helper_handler' ); | |
add_action( 'activated_plugin', 'woocommerce_ecommplan_helper_activate' ); | |
add_action( 'admin_init', 'woocommerce_ecommplan_helper_register_pages' ); | |
// Jetpack Tweaks. | |
// add_filter( 'jetpack_offline_mode', '__return_true' ); | |
add_filter( 'jetpack_tools_to_include', function( $tools ) { | |
return array_merge( $tools, [ 'calypsoify/class.jetpack-calypsoify.php' ] ); | |
} ); | |
function woocommerce_ecommplan_helper_plugin_action_links( $actions ) { | |
$is_ecom = woocommerce_ecommplan_helper_is_ecomm_plan(); | |
if ( $is_ecom ) { | |
$new = array( | |
'currentplan' => sprintf( | |
'<a href="%s">Ecomm On: View Calypsoify</a>', | |
esc_url( admin_url( 'admin.php?page=wc-admin&calypsoify=1' ) ) | |
), | |
'changeplan' => sprintf( | |
'<a href="%s">Switch to Biz</a>', | |
esc_url( admin_url( '?ecommplan_action=activate_biz' ) ) | |
), | |
); | |
} else { | |
$new = array( | |
'currentplan' => sprintf( | |
'<a href="%s">Biz On: View Home Screen</a>', | |
esc_url( admin_url( 'admin.php?page=wc-admin' ) ) | |
), | |
'changeplan' => sprintf( | |
'<a href="%s">Switch to Ecom</a>', | |
esc_url( admin_url( '?ecommplan_action=activate_ecomm' ) ) | |
), | |
); | |
} | |
return array_merge( $new, $actions ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment