Skip to content

Instantly share code, notes, and snippets.

@andykeith
Last active August 12, 2019 15:07
Show Gist options
  • Save andykeith/2c788b40889945bc7433ee5a88cd8b6b to your computer and use it in GitHub Desktop.
Save andykeith/2c788b40889945bc7433ee5a88cd8b6b to your computer and use it in GitHub Desktop.
An example (from WooCommerce Quick View Pro) of our current plugin license setup, including adding the license setting to the settings page. This is to be replaced by a new license system currently in development.
<?php
final class Quick_View_Plugin {
const NAME = 'WooCommerce Quick View Pro'; // should match download name in EDD
const VERSION = '1.1';
const FILE = __FILE__;
private static $BASENAME = null;
/* The plugin license manager. */
private $license;
/* The singleton instance. */
private static $_instance = null;
private function __construct() {
$this->includes();
// Create our plugin license & updater.
$this->license = new \Barn2_Plugin_License( self::FILE, self::NAME, self::VERSION, 'wc_quick_view_pro' );
}
// rest of class...
}
// use Barn2\Lib\Util as Util;
class Admin_Settings_Page {
private $license;
public function __construct( \Barn2_Plugin_License $license ) {
$this->license = $license;
}
public function register() {
// Register extra setting types with WC.
foreach ( array( 'hidden', 'settings_start', 'settings_end' ) as $field ) {
add_action( "woocommerce_admin_field_{$field}", array( '\WC_Settings_Additional_Field_Types', "{$field}_field" ) );
}
// Add our settings & section.
add_filter( 'woocommerce_get_sections_products', array( $this, 'add_section' ) );
// Add Quick View settings to Products tab.
add_filter( 'woocommerce_get_settings_products', array( $this, 'add_settings' ), 10, 2 );
// Sanitize and save license setting.
add_filter( 'woocommerce_admin_settings_sanitize_option_' . $this->license->license_key_option, array( $this->license, 'save' ) );
}
public function add_section( $sections ) {
$sections[self::SECTION] = __( 'Quick view', 'woocommerce-quick-view-pro' );
return $sections;
}
public function add_settings( $settings, $current_section ) {
// Check we're on the correct settings section
if ( self::SECTION !== $current_section ) {
return $settings;
}
$plugin_settings = array();
// Add license key settings.
$plugin_settings[] = array(
'title' => __( 'Quick view', 'woocommerce-quick-view-pro' ),
'type' => 'title',
'id' => 'quick_view_license_section',
'desc' => '<p>' . sprintf( __( 'The following options control the %sWooCommerce Quick View Pro%s extension.', 'woocommerce-quick-view-pro' ), '<strong>', '</strong>' ) . '<p>'
. '<p>'
. Util::barn2_link( 'kb-categories/woocommerce-quick-view-pro-kb/', __( 'View Documentation', 'woocommerce-quick-view-pro' ), true ) . ' | '
. Util::barn2_link( 'our-wordpress-plugins/support-documentation/', __( 'Get Help', 'woocommerce-quick-view-pro' ), true )
. '</p>'
);
$plugin_settings[] = array(
'title' => __( 'License key', 'woocommerce-quick-view-pro' ),
'type' => 'text',
'id' => $this->license->license_key_option,
'desc' => $this->license->get_license_key_admin_message(),
'desc_tip' => __( 'The licence key is contained in your order confirmation email.', 'woocommerce-quick-view-pro' ),
'class' => 'regular-text'
);
if ( filter_input( INPUT_GET, 'license_debug' ) ) {
$plugin_settings[] = array(
'type' => 'hidden',
'id' => 'license_debug',
'default' => '1'
);
}
if ( $override = filter_input( INPUT_GET, 'license_override', FILTER_SANITIZE_STRING ) ) {
$plugin_settings[] = array(
'type' => 'hidden',
'id' => 'license_override',
'default' => $override
);
}
$plugin_settings[] = array(
'type' => 'sectionend',
'id' => 'quick_view_license_section'
);
// Add more settings...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment