Skip to content

Instantly share code, notes, and snippets.

@timmyc
Last active February 24, 2021 01:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save timmyc/e03579355a7696fb3c5b7cd4b536d8f4 to your computer and use it in GitHub Desktop.
Save timmyc/e03579355a7696fb3c5b7cd4b536d8f4 to your computer and use it in GitHub Desktop.
<?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 );
}
@jeffstieler
Copy link

Line 11 triggers a PHP warning, fixed here: https://gist.github.com/jeffstieler/e8e5bbfd24218c00bd5ee1cab5a002be

@timmyc
Copy link
Author

timmyc commented Jul 20, 2020

Updated, thanks @jeffstieler

@psealock
Copy link

psealock commented Jan 7, 2021

@timmyc with the changes in Automattic/wc-calypso-bridge#622 (comment), the Bridge never gets turned on. I added this to the top of the page and renamed the file aaa-woocommerce-calypso-bridge-helper.php to ensure it gets run first to fix the issue.

class Atomic_Plan_Manager {
    const ECOMMERCE_PLAN_SLUG = 'ecommerce';

    public static function current_plan_slug() {
        return self::ECOMMERCE_PLAN_SLUG;
    }
}

Can you update please?

Also, I can't remember where in our documentation this gist is linked, but it will need updating too.

@timmyc
Copy link
Author

timmyc commented Jan 11, 2021

Thanks for the ping on this Paul, it had been buried deep in my to-do list to circle back on this with all the changes happening in the bridge with the removal of calypsoify. Currently the only documentation on this helper plugin is in this post p90Yrv-1O5-p2, but I should put it in its own workflow doc.

@joelclimbsthings
Copy link

Note that we also need to update this to set jetpack_offline_mode instead of jetpack_development_mode, which is deprecated.

@timmyc
Copy link
Author

timmyc commented Feb 23, 2021

Note that we also need to update this to set jetpack_offline_mode instead of jetpack_development_mode, which is deprecated.

I think we might be able to fully omit the Jetpack bits now that we are no longer using Calypsoify.

@psealock
Copy link

I forked this and made the following changes in https://gist.github.com/psealock/531205e2c3d37be1d8ac4d3ef4f346bc

  • Rename to a-woocommerce-calypso-bridge-helper.php to ensure its parsed first
  • Updated jetpack_development_mode to jetpack_offline_mode
  • Added Atomic_Plan_Manager class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment