Home » How to count your plugin and theme activations

How to count your plugin and theme activations

This is a great way to find out how many websites actually activate the plugin or theme you have worked hard to develop. Having a download counter is one thing but we don’t know for sure that the user is actually using it.

In this post I am going to show you how you can add an API ping system that will tell you if it has been activated, dis-activated and re-activated. You can also allow it to send you an email telling you which item has been used and on what website.

This is a great way to identify theft if you are selling premium content and you know which websites have been given permission to use that content.

The Requirements

This tutorial will require you to have three things created before you can start. You will need a sub-domain created, a new database to insert your new table and a separate email address. The email address is only required if you wish to be notified when a plugin or theme has been used at the time of activation.

Step One. Create a new file in your favourite web editing software i.e. ‘Dreamweaver’ and save it as ‘index.php’ .

Copy the following code into the file and save. Then configure the top of the file under email notifications and database connections. Once you have configured the file, save it and upload it to the directory of your new sub-domain.

<?php
/**
 * This identifies what and where any plugin
 * or theme has been activated or deactivated
 *
 * code written by Seb's Studio.
 */

// Email notifications.
define('your_domain', 'http://www.yourdomain.com'); // Enter your main domain for redirection if the sub-domain is accessed directly.
define('from_name', 'John Smith'); // Identify who the email is from i.e. Yourself.
define('email_address', 'api[at]yourdomain.com'); // Email Address
define('send_emails', 'yes'); // This will determine if you want emails sent to you each time a plugin or theme is activated.

// Connection to Database
define('user', 'username'); // Username
define('password', 'password'); // Password
define('database', 'database'); // Database
define('host', 'localhost'); // Host, 99% of host are localhost.

$link = mysql_connect(host, user, password);
if(!$link){ die('Could not connect: '.mysql_error()); }

// Activated Plugin
if(isset($_GET['plugin']) && $_GET['plugin'] == 1){
	if(isset($_GET['name'])){
		$query = "SELECT name, version, site FROM `".database."`.`plugins` WHERE `name`='".$_GET['name']."' AND `site`='".$_GET['site']."' LIMIT 1";
		$results = mysql_query($query) or die("ERROR: ".mysql_error());
		while($row = mysql_fetch_array($results)){
			$version = $row['version'];
			$site = $row['site'];
		}
		$countCheck = mysql_num_rows($results);
		if($countCheck == 1){
			if($_GET['version'] != $version){
				$query = "UPDATE `".database."`.`plugins` SET `version`='".$_GET['version']."' WHERE `name`='".$_GET['name']."' AND `version`='{$version}' AND `site`='{$site}' LIMIT 1";
				mysql_query($query) or die("ERROR: ".mysql_error());
				$email = 'reactivated';
			}
		}
		else{
			$query = "INSERT INTO `".database."`.`plugins` (`active_id`, `name`, `version`, `site`) VALUES ('', '".$_GET['name']."', '".$_GET['version']."', '".$_GET['site']."')";
			mysql_query($query) or die("ERROR: ".mysql_error());
			$email = 'activated';
		}
	}
}
// Dis-activated Plugin
else if(isset($_GET['plugin']) && $_GET['plugin'] == 0){
	if(isset($_GET['name'])){
		$query = "SELECT * FROM `".database."`.`plugins` WHERE `name`='".$_GET['name']."' AND `site`='".$_GET['site']."' LIMIT 1";
		$results = mysql_query($query) or die("ERROR: ".mysql_error());
		while($row = mysql_fetch_assoc($results)){
			$name = $row['name'];
			$site = $row['site'];
		}
		$countCheck = mysql_num_rows($results) or die("ERROR: ".mysql_error());
		if($countCheck == 1){
			if($_GET['name'] == $name && $_GET['site'] == $site){
				$query = "DELETE FROM `".database."`.`plugins` WHERE `name`='{$name}' AND `site`='{$site}'";
				mysql_query($query) or die("ERROR: ".mysql_error());
				$email = 'disactivated';
			}
		}
	}
}
// Activated Theme
else if(isset($_GET['theme']) && $_GET['theme'] == 1){
	if(isset($_GET['name'])){
		$query = "SELECT name, version, site FROM `".database."`.`themes` WHERE `name`='".$_GET['name']."' AND `site`='".$_GET['site']."' LIMIT 1";
		$results = mysql_query($query) or die("ERROR: ".mysql_error());
		while($row = mysql_fetch_array($results)){
			$version = $row['version'];
			$site = $row['site'];
		}
		$countCheck = mysql_num_rows($results);
		if($countCheck == 1){
			if($_GET['version'] != $version){
				$query = "UPDATE `".database."`.`themes` SET `version`='".$_GET['version']."' WHERE `name`='".$_GET['name']."' AND `version`='{$version}' AND `site`='{$site}' LIMIT 1";
				mysql_query($query) or die("ERROR: ".mysql_error());
				$email = 'reactivated';
			}
		}
		else{
			$query = "INSERT INTO `".database."`.`themes` (`active_id`, `name`, `version`, `site`) VALUES ('', '".$_GET['name']."', '".$_GET['version']."', '".$_GET['site']."')";
			mysql_query($query) or die("ERROR: ".mysql_error());
			$email = 'activated';
		}
	}
}
// Dis-activated Theme
else if(isset($_GET['theme']) && $_GET['theme'] == 0){
	if(isset($_GET['name'])){
		$query = "SELECT * FROM `".database."`.`themes` WHERE `name`='".$_GET['name']."' AND `site`='".$_GET['site']."' LIMIT 1";
		$results = mysql_query($query) or die("ERROR: ".mysql_error());
		while($row = mysql_fetch_assoc($results)){
			$name = $row['name'];
			$site = $row['site'];
		}
		$countCheck = mysql_num_rows($results) or die("ERROR: ".mysql_error());
		if($countCheck == 1){
			if($_GET['name'] == $name && $_GET['site'] == $site){
				$query = "DELETE FROM `".database."`.`themes` WHERE `name`='{$name}' AND `site`='{$site}'";
				mysql_query($query) or die("ERROR: ".mysql_error());
				$email = 'disactivated';
			}
		}
	}
}
// Count how many plugins or themes are activated.
else if(isset($_GET['count']) && $_GET['count'] == 1){
	// Counting Plugins
	if(isset($_GET['type']) && $_GET['type'] == 'plugin'){
		if(isset($_GET['name']) && !empty($_GET['name'])){
			$plugin_Name = " WHERE `name`='".$_GET['name']."'";
		}
		$query = "SELECT * FROM `".database."`.`plugins`".$plugin_Name;
		$results = mysql_query($query) or die("ERROR: ".mysql_error());
		$countPlugins = mysql_num_rows($results);
		$displayPluginCounter = number_format($countPlugins);
		echo $displayPluginCounter;
	}
	// Counting Themes
	else if(isset($_GET['type']) && $_GET['type'] == 'theme'){
		if(isset($_GET['name']) && !empty($_GET['name'])){
			$theme_Name = " WHERE `name`='".$_GET['name']."'";
		}
		$query = "SELECT * FROM `".database."`.`themes`".$theme_Name;
		$results = mysql_query($query) or die("ERROR: ".mysql_error());
		$countThemes = mysql_num_rows($results);
		$displayThemeCounter = number_format($countThemes);
		echo $displayThemeCounter;
	}
	else{ die('Can't count no type. Please set either plugin or theme.'); }
}
// If no plugin or theme is been activated or deactivated then kill the page.
else{
	header("Location: ".your_domain);
	die();
}
mysql_close($link);

// Emails
if(send_emails == 'yes'){
	if(isset($_GET['plugin'])){
		if(isset($email) && $email == 'activated'){
			$subject = "A plugin has been activated";
			$msg = "Plugin '".$_GET['name']."', version '".$_GET['version']."' has been activated at '".$_GET['site']."'";
		}
		if(isset($email) && $email == 'reactivated'){
			$subject = 'A plugin has been reactivated';
			$msg = "Plugin '".$_GET['name']."' has been either reactivated or updated at '".$_GET['site']."'";
		}
		if(isset($email) && $email == 'disactivated'){
			$subject = 'A plugin has been disactivated';
			$msg = "Plugin '".$_GET['name']."' has been disactivated at '".$_GET['site']."'";
		}
	}
	if(isset($_GET['theme'])){
		if(isset($email) && $email == 'activated'){
			$subject = "A theme has been activated";
			$msg = "Theme '".$_GET['name']."', version '".$_GET['version']."' has been activated at '".$_GET['site']."'";
		}
		if(isset($email) && $email == 'reactivated'){
			$subject = 'A theme has been reactivated';
			$msg = "Theme '".$_GET['name']."' has been either reactivated or updated at '".$_GET['site']."'";
		}
		if(isset($email) && $email == 'disactivated'){
			$subject = 'A theme has been disactivated';
			$msg = "Theme '".$_GET['name']."' has been disactivated at '".$_GET['site']."'";
		}
	}
	if(isset($email)){
		$headers = "From: ".from_name." nMIME-Version: 1.0nContent-Type: text/html; charset=utf-8nContent-Transfer-Encoding: 7BitnX-Mailer: PHP";
		mail(email_address, $subject, $msg, $headers);
	}
}
?>

Step Two. Creating a database structure to store the activation data. Once you have your new database created insert the following code to create the tables required to store data.

--
-- Table structure for table `plugins`
--

CREATE TABLE IF NOT EXISTS `plugins` (
  `active_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `version` varchar(7) NOT NULL,
  `site` varchar(255) NOT NULL,
  `date_activated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`active_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

--
-- Table structure for table `themes`
--

CREATE TABLE IF NOT EXISTS `themes` (
  `active_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `version` varchar(7) NOT NULL,
  `site` varchar(255) NOT NULL,
  `date_activated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`active_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Step Three. Adding the activation functions to your plugin or theme. You will need to create two values that will be passed through the api ping. Add something like this at the top of your plugin file or inside the ‘functions.php‘ file of your theme.

define('plugin_name', 'API Ping Tutorial'); // Plugin Version.
define('plugin_version', '1.0'); // Plugin Version.

or

define('theme_name', 'API Ping Tutorial'); // Theme Version.
define('theme_version', '1.0'); // Theme Version.

Then add these two functions into your plugin or theme.

/**
 * Send and API ping when the plugin is activated.
 */
static function activate_plugin(){
	$api = "http://api.yourdomain.com/?plugin=1&name=".urlencode(plugin_name)."&version=".urlencode(plugin_version)."&site=".urlencode(home_url());
	//open connection
	$ch = curl_init();
	$timeout = 5;
	curl_setopt($ch,CURLOPT_URL,$api);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
	//execute api
	$data = curl_exec($ch);
	//close connection
	curl_close($ch);
}

/**
 * Send and API ping when the plugin is disactivated.
 */
static function deactivate_plugin(){
	$api = "http://api.yourdomain.com/?plugin=0&name=".urlencode(plugin_name)."&site=".urlencode(home_url());
	//open connection
	$ch = curl_init();
	$timeout = 5;
	curl_setopt($ch,CURLOPT_URL,$api);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
	//execute api
	$data = curl_exec($ch);
	//close connection
	curl_close($ch);
}

Make sure that you have the register activation hooks added also other wise it won’t work.

register_activation_hook(__FILE__, 'activate_plugin');
register_deactivation_hook(__FILE__, 'deactivate_plugin');

That’s it. Now every time someone downloads your plugin or theme you will know if it has been activated, dis-activated or re-activated.

Enjoy. :)

No comments yet.

Leave a Reply

0€0 items

Cart