Inventory_Presser_Addon

Inventory_Presser_Addon


Description Description

A base class for add-on plugins. Provides methods to initialize an updater that connects to inventorypresser.com to check for updates and store a license key in the site that permits said updates.


Top ↑

Source Source

File: includes/class-addon.php

	abstract class Inventory_Presser_Addon
	{		
		/**
		 * initialize_updater
		 *
		 * @param  string $class_name The class name in the add-on plugin's main file that contains the VERSION and LICENSE_ITEM constants
		 * @param  string $plugin_path The file path to the plugin for which we are checking for updates
		 * @param  string $license_key The license key provided by inventorypresser.com upon purchase
		 * @param  string $include_betas Set to true if you wish customers to receive update notifications of beta releases
		 * @return void
		 */
		public static function initialize_updater( $class_name, $plugin_path, $license_key, $include_betas = false )
		{
			//check for existence of updater class
			if( ! class_exists( 'Inventory_Presser_Addon_Updater' ) )
			{
				return;
			}

			//Does the plugin class have the VERSION and LICENSE_ITEM constants?
			if( empty( $class_name::VERSION ) || empty( $class_name::LICENSE_ITEM ) )
			{
				return;
			} 

			$updater = new Inventory_Presser_Addon_Updater( 'https://inventorypresser.com', $plugin_path, array(
				'version' 	=> $class_name::VERSION, //current version number
				'license' 	=> $license_key,
				'item_id'   => $class_name::LICENSE_ITEM, //id of this download in inventorypresser.com
				'author' 	=> 'Corey Salzano',
				'url'       => home_url(), //the site requesting the update, this site
				'beta'      => $include_betas
			) );
		}

		public static function add_license_key_box( $option_name, $settings_section, $sanitize_values_hook_name )
		{
			add_action( 'admin_init', function() use ( $option_name, $settings_section ) {

				//is $settings_section an existing section?
				global $wp_settings_sections;
				if( empty( $wp_settings_sections[INVP::option_page()][$settings_section] ) )
				{
					//no, abort
					return;
				}

				$option = get_option( $option_name );
				$current_value = empty( $option['license_key'] ) ? '' : $option['license_key'];

				add_settings_field(
					'license_key', // id
					__( 'License key', 'inventory-presser' ), // title
					function() use ( $option_name, $current_value ) {
						?><p><input type="text" name="<?php echo $option_name; ?>[license_key]" class="regular-text code" id="<?php echo $option_name; ?>[license_key]" value="<?php echo $current_value; ?>" /></p>
						<p class="description"><?php printf( '%s <a href="https://inventorypresser.com/">https://inventorypresser.com/</a> %s', __( 'Obtain a key at', 'inventory-presser' ), __( 'to receive plugin updates.', 'inventory-presser-divert-cf7' ) ); ?></p><?php
					}, // callback
					INVP::option_page(), // page
					$settings_section // section
				);
			}, 20 );

			add_filter( $sanitize_values_hook_name, array( __CLASS__, 'sanitize_and_activate_license_key' ), 10, 3 );
		}

		public static function make_sure_license_is_activated( $license_item, $license_key )
		{
			$license = new Inventory_Presser_License(
				$license_key,
				$license_item
			);
			if( ! $license->is_active() ) { $license->activate(); }
		}
		
		public static function sanitize_and_activate_license_key( $sanitized, $input, $license_item )
		{				
			if ( isset( $input['license_key'] ) )
			{
				$sanitized['license_key'] = sanitize_text_field( $input['license_key'] );
				self::make_sure_license_is_activated( $license_item, $sanitized['license_key'] );
			}
			return $sanitized;
		}
	}

Top ↑

Methods Methods