Automatic Upgrades for Extendd Plugins

Adding support for automatic upgrades to your WordPress plugins is really simple. After you have setup your product in the Downloads section and check enable license creation, there is just a small code snippet and two files that you need to include in your WordPress plugin.

There are three components to the updater:

  1. The EDD_SL_Plugin_Updater.php file. This contains the class that makes the magic happen.
  2. The extendd-settings.php handles the settings page.
  3. The code snippet that you place somewhere in your plugin that loads the updater provided by the EDD_SL_PLugin_Updater.php class file.

Step 1 – Define the Updater Variables in OOP class

OOP
What ever your class may be, set up some private vars:

class my_custom_plugin_class {
        var $version;

	private $plugin_id,
		$plugin_name;

And set those variables in the constructor or your init function:

function __construct() {
		$this->version		= '1.1.6';
		$this->plugin_name	= 'Custom Login Pro';
		$this->plugin_id	= 'wp_custom_login_pro'; // Always prefix the plugin_id with "wp_"!

Step 2 – Include the Classes

The update system itself is contained within the file called EDD_SL_Plugin_Updater.php and the settings is included in the file extendd-settings.php. You need to include these files in your main plugin file, like so:

/**
 * Upgrade script
 */
function wp_settings() {
	if ( !class_exists( 'wp_settings_api' ) ) {
		include( trailingslashit( plugin_dir_path( __FILE__ ) ) . 'classes/extendd-settings.php' );
	}
	add_filter( 'wp_add_settings_sections', 	array( $this, 'add_settings_section' ) );
	add_filter( 'wp_add_settings_fields',	array( $this, 'add_settings_fields' ) );
			
	$options = get_option( $this->plugin_id, array() );
	if ( isset( $options ) && ( !empty( $options['license_key'] ) && 'valid' === $options['license_active'] ) ) {
		if ( !class_exists( 'EDD_SL_Plugin_Updater' ) ) {
			// load our custom updater
			include( trailingslashit( plugin_dir_path( __FILE__ ) ) . 'classes/EDD_SL_Plugin_Updater.php' );
		}
	
		$edd_updater = new EDD_SL_Plugin_Updater( 'http://frosty.media/', __FILE__,
			array(
				'version'   => $this->version, // current version number
				'license'   => trim( $options['license_key'] ), // license key
				'item_name' => $this->plugin_name, // name of this plugin in the Easy Digital Downloads system
				'author'    => 'Austin Passy' // author of this plugin
			)
		);
	}
}

You may need to adjust the file path, depending on where you have decided to place the file within your plugin’s folder structure.

Step 3 – Add the settings sections

Now that the classes are loaded will set up the settings:

/**
 * Returns new settings section for this plugin
 *
 * @updated 3/6/13
 * @return 	array settings fields
 */
function add_settings_section( $sections ) {
	$sections[] = array(
		'id' 		=> $this->plugin_id,
		'title' 	=> $this->plugin_name, //Must match EDD post_title!
		'basename'	=> plugin_basename( __FILE__ ),
		'version'       => $this->version,
	);
	return $sections;
}

/**
 * Returns new settings fields for this plugin
 *
 * @return 	array settings fields
 */
function add_settings_fields( $settings_fields ) {
	$settings_fields[$this->plugin_id] = array(
		array(
			'name' 			=> 'license_key',
			'label' 		=> __( 'License Key', 'extendd' ),
			'desc' 			=> sprintf( __( 'Enter your license for %s to receive automatic updates', '' ), $this->plugin_name ),
			'type' 			=> 'text',
			'default' 		=> '',
			'placeholder'	        => __( 'Enter your license key', 'extendd' )
		),
		array(
			'name' 			=> 'license_active',
			'label' 		=> '',
			'desc' 			=> '',
			'type' 			=> 'hidden',
			'default' 		=> ''
		),
		array(
			'name' 			=> 'license_deactivate',
			'label' 		=> __( 'De-activate License?', 'extendd' ),
			'desc' 			=> '',
			'type' 			=> 'checkbox',
			'default' 		=> ''
		),
	);	
	return $settings_fields;
}

Everything regarding settings and remote calls is automatically handled by the wp_settings_api class.

There are several parameters we pass to the EDD_SL_Plugin_Updater class:

  • $api_url – Always needs to be pointed to Extendd when hosting plugin here.
  • $plugin_file – this is the main plugin file. I suggest you use the __FILE__ magic constant provided by PHP. Note, to do this, you must have this snippet placed in your main plugin file.
  • $api_args – this is an array of options to pass to the updater:
    • version – this is the current version of the plugin that is installed. It is not the latest available version.
    • license – this is the license key retrieved from the database.
    • item_name – this is the name of our product on Extendd.com
    • author – this is the name of the person or company that wrote the plugin (you!)

That’s it! Your plugin should now have a completely functional automatic upgrade system.

The complete code for the updater might look something like this:

class my_custom_plugin_class {
	
	var $version;

	private $plugin_id,
		$plugin_name;
		
	function __construct() {
		$this->version		= '1.1.6';
		$this->plugin_name	= 'Custom Login Pro';
		$this->plugin_id	= 'wp_custom_login_pro';
		
		/* Extendd */
		$this->wp_settings();
	}
	
	/**
	 * Upgrade script
	 *
	 * @since	11/30/2012
	 */
	function wp_settings() {
		if ( !class_exists( 'wp_settings_api' ) ) {
			include( trailingslashit( plugin_dir_path( __FILE__ ) ) . 'classes/extendd-settings.php' );
		}
		add_filter( 'wp_add_settings_sections', 	array( $this, 'add_settings_section' ) );
		add_filter( 'wp_add_settings_fields',		array( $this, 'add_settings_fields' ) );
				
		$options = get_option( $this->plugin_id, array() );
		if ( isset( $options ) && ( !empty( $options['license_key'] ) && 'valid' === $options['license_active'] ) ) {
			if ( !class_exists( 'EDD_SL_Plugin_Updater' ) ) {
				// load our custom updater
				include( trailingslashit( plugin_dir_path( __FILE__ ) ) . 'classes/EDD_SL_Plugin_Updater.php' );
			}
		
			$edd_updater = new EDD_SL_Plugin_Updater( 'http://frosty.media/', __FILE__,
				array(
					'version'   => $this->version, // current version number
					'license'   => trim( $options['license_key'] ), // license key
					'item_name' => $this->plugin_name, // name of this download on Extendd
					'author'    => 'Austin Passy' // author of this plugin
				)
			);
		}
	}
	
	/**
	 * Returns new settings section for this plugin
	 *
	 * @updated 3/6/13
	 * @return 	array settings fields
	 */
	function add_settings_section( $sections ) {
		$sections[] = array(
			'id' 		=> $this->plugin_id,
			'title' 	=> $this->plugin_name, //Must match download post_title!
			'basename'	=> plugin_basename( __FILE__ ),
			'version'       => $this->version,
		);
		return $sections;
	}
	
	/**
	 * Returns new settings fields for this plugin
	 *
	 * @return 	array settings fields
	 */
	function add_settings_fields( $settings_fields ) {
		$settings_fields[$this->plugin_id] = array(
			array(
				'name' 			=> 'license_key',
				'label' 		=> __( 'License Key', '' ),
				'desc' 			=> sprintf( __( 'Enter your license for %s to receive automatic updates', '' ), $this->plugin_name ),
				'type' 			=> 'text',
				'default' 		=> '',
				'placeholder'	        => __( 'Enter your license key', '' )
			),
			array(
				'name' 			=> 'license_active',
				'label' 		=> '',
				'desc' 			=> '',
				'type' 			=> 'hidden',
				'default' 		=> ''
			),
			array(
				'name' 			=> 'license_deactivate',
				'label' 		=> __( 'De-activate License?', 'extendd' ),
				'desc' 			=> '',
				'type' 			=> 'checkbox',
				'default' 		=> ''
			),
		);	
		return $settings_fields;
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *