PHP code example of closemarketing / wp-plugin-license-manager

1. Go to this page and download the library: Download closemarketing/wp-plugin-license-manager library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

closemarketing / wp-plugin-license-manager example snippets



/**
 * Plugin Name: My Awesome Plugin
 * Version: 1.0.0
 */

// Include Composer autoloader.
IN_LICENSE_API_URL', 'https://yourstore.com/' );
define( 'MYPLUGIN_LICENSE_API_KEY', 'ck_your_consumer_key' );
define( 'MYPLUGIN_LICENSE_API_SECRET', 'cs_your_consumer_secret' );
define( 'MYPLUGIN_LICENSE_PRODUCT_UUID', 'YOUR-PRODUCT-UUID' );

// Global license instance.
$myplugin_license = null;

// Initialize the license manager.
add_action( 'plugins_loaded', function() {
    global $myplugin_license;

    if ( ! class_exists( '\Closemarketing\WPLicenseManager\License' ) ) {
        return;
    }

    try {
        $myplugin_license = new License(
            array(
                'api_url'          => MYPLUGIN_LICENSE_API_URL,
                'rest_api_key'     => MYPLUGIN_LICENSE_API_KEY,
                'rest_api_secret'  => MYPLUGIN_LICENSE_API_SECRET,
                'product_uuid'     => MYPLUGIN_LICENSE_PRODUCT_UUID,
                'file'             => __FILE__,
                'version'          => '1.0.0',
                'slug'             => 'my-plugin',
                'name'             => 'My Awesome Plugin',
                'text_domain'      => 'my-plugin',
                'settings_page'    => 'my-plugin-settings',
                'settings_tabs'    => 'my_plugin_tabs_hook',    // Your tabs action hook.
                'settings_content' => 'my_plugin_content_hook', // Your content action hook.
            )
        );
    } catch ( \Exception $e ) {
        add_action( 'admin_notices', function() use ( $e ) {
            echo '<div class="notice notice-error"><p>' . esc_html( $e->getMessage() ) . '</p></div>';
        });
    }
}, 5 );

// Helper function to check license status.
function myplugin_is_license_valid() {
    global $myplugin_license;
    return $myplugin_license && $myplugin_license->is_license_active();
}

use Closemarketing\WPLicenseManager\License;
use Closemarketing\WPLicenseManager\Settings;

add_action( 'plugins_loaded', function() {
    global $myplugin_license;

    if ( ! class_exists( '\Closemarketing\WPLicenseManager\License' ) ) {
        return;
    }

    try {
        // Create license instance.
        $myplugin_license = new License(
            array(
                'api_url'         => MYPLUGIN_LICENSE_API_URL,
                'rest_api_key'    => MYPLUGIN_LICENSE_API_KEY,
                'rest_api_secret' => MYPLUGIN_LICENSE_API_SECRET,
                'product_uuid'    => MYPLUGIN_LICENSE_PRODUCT_UUID,
                'file'           => __FILE__,
                'version'        => '1.0.0',
                'slug'           => 'my-plugin',
                'name'           => 'My Awesome Plugin',
                'text_domain'    => 'my-plugin',
            )
        );

        // Create Settings instance with automatic UI.
        $license_settings = new Settings(
            $myplugin_license,
            array(
                'title'         => __( 'My Plugin License', 'my-plugin' ),
                'description'   => __( 'Manage your license to receive updates and support.', 'my-plugin' ),
                'plugin_name'   => 'My Awesome Plugin',
                'purchase_url'  => 'https://yourstore.com/plugins/my-plugin/',
                'renew_url'     => 'https://yourstore.com/my-account/',
                'benefits'      => array(
                    __( 'Automatic plugin updates', 'my-plugin' ),
                    __( 'Access to new features', 'my-plugin' ),
                    __( 'Priority support', 'my-plugin' ),
                    __( 'Security patches', 'my-plugin' ),
                ),
                'settings_page' => 'my-plugin-settings',  // Your settings page slug.
                'default_tab'   => 'license',            // Default tab for redirects.
                'tab_param'     => 'tab',                // URL parameter for tabs.
            )
        );

        // Add license tab to your existing settings page.
        add_filter( 'my_plugin_settings_tabs', function( $tabs ) {
            $tabs[] = array(
                'tab'    => 'license',
                'label'  => __( 'License', 'my-plugin' ),
                'action' => 'my_plugin_license_content',
            );
            return $tabs;
        });

        // Render license content.
        add_action( 'my_plugin_license_content', function() use ( $license_settings ) {
            $license_settings->render();
        });

    } catch ( \Exception $e ) {
        add_action( 'admin_notices', function() use ( $e ) {
            echo '<div class="notice notice-error"><p>' . esc_html( $e->getMessage() ) . '</p></div>';
        });
    }
}, 5 );

$license = new License(
    array(
        // ... other options ...
        'settings_tabs'    => 'myplugin_tabs_disabled',    // Disabled - UI handled manually.
        'settings_content' => 'myplugin_content_disabled', // Disabled - UI handled manually.
    )
);

<form method="post" action="options.php">
     settings_fields( 'my-plugin_license' ); 

if ( $license->is_license_active() ) {
    // License is active, enable premium features.
}

// Get cached status.
$status = $license->get_api_key_status();

// Get real-time status from API.
$status = $license->get_api_key_status( true );

$apikey_key = $license->get_option_key( 'apikey' );
// Returns: 'my-plugin_license_apikey'

$license_key = $license->get_option_value( 'apikey' );

$input = array(
    'my-plugin_license_apikey' => 'license-key-here',
    'my-plugin_license_deactivate_checkbox' => 'on', // Optional, for deactivation
);
$license->validate_license( $input );

$settings = new Settings( $license, $options );
$settings->render();