Forked from timmyc/woocommerce-calypso-bridge-helper.php
Last active
July 20, 2020 17:05
-
-
Save jeffstieler/e8e5bbfd24218c00bd5ee1cab5a002be to your computer and use it in GitHub Desktop.
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: WooCommerce Calypso Bridge Helper | |
* Plugin URI: https://woocommerce.com | |
* Description: Utility to assist testing wc-calypso-bridge locally | |
* Author: WooCommerce | |
* Version: 0.1 | |
*/ | |
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_development_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