Skip to content

Instantly share code, notes, and snippets.

@sirbrillig
Last active March 28, 2021 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sirbrillig/4c45b14ac57dc7d448b47334e5ab966d to your computer and use it in GitHub Desktop.
Save sirbrillig/4c45b14ac57dc7d448b47334e5ab966d to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Checkout Experiment
Description: An experimental WordPress.com checkout inside wp-admin
Author: Automattic
Version: 1.0
Author URI: http://automattic.com
*/
class CheckoutExperiment {
public static function init(): void {
$checkout = new self();
add_action( 'admin_menu', [ $checkout, 'add_menu' ] );
add_action( 'admin_enqueue_scripts', [ $checkout, 'add_scripts' ] );
}
public function add_menu(): void {
add_menu_page( 'Checkout Experiment', 'Checkout Experiment', 'manage_options', 'checkout-experiment', [ $this, 'render_checkout' ] );
}
public function add_scripts( string $hook_suffix ): void {
if ( $hook_suffix !== 'toplevel_page_checkout-experiment' ) {
return;
}
$version = '5';
wp_enqueue_script( 'react' );
wp_enqueue_script( 'react-dom' );
wp_register_script( 'checkout-experiment', plugin_dir_url( __FILE__ ) . 'checkout-experiment-app/dist/bundle.js', [ 'react', 'react-dom' ], $version, true );
wp_localize_script( 'checkout-experiment', 'checkout_data', [ 'blog_id' => get_current_blog_id() ] );
wp_enqueue_script( 'checkout-experiment' );
}
public function render_checkout(): void {
echo '<div id="checkout-experiment-app">js goes here</div>';
}
}
add_action( 'init', array( 'CheckoutExperiment', 'init' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment