=== API Manager PHP Library ===

This library can be dropped into a WordPress plugin or theme to activate/deactivate API Keys, to check for Software updates, and check an API Key's status with the **WooCommerce API Manager**.

The ```am-license-menu.php``` file should be placed in the plugin's root folder, or the same level as the ```functions.php``` file if it is a theme. The code below goes in the root plugin file, or a theme's functions.php file.

The Class Name ```AM_License_Menu``` in the code example below, and in the am-license-menu.php on lines 19 and 20, must be changed to something unique to your project, so the Class Name does not conflict with other products loaded on the same WordPress installation.

In the code example below uncomment the require_once() line for a plugin or a theme.

IMPORTANT: The code block below should be added just below your plugin header, or the top of your functions file in a theme. The code block below should always be loaded first, so it will always work, and won't break in case something else in your plugin or theme breaks.

```php
// Load the API Key library if it is not already loaded. Must be placed in the root plugin file.
if ( ! class_exists( 'AM_License_Menu' ) ) {
	// Uncomment next line if this is a plugin
	// require_once( plugin_dir_path( __FILE__ ) . 'am-license-menu.php' );

	// Uncomment next line if this is a theme
	// require_once( get_stylesheet_directory() . '/am-license-menu.php' );

	/**
	 * @param string $file             Must be __FILE__ from the root plugin file, or theme functions file.
	 * @param string $software_title   Must be exactly the same as the Software Title in the product.
	 * @param string $software_version This product's current software version.
	 * @param string $plugin_or_theme  'plugin' or 'theme'
	 * @param string $api_url          The URL to the site that is running the API Manager. Example: https://www.toddlahman.com/
	 *
	 * @return \AM_License_Submenu|null
	 */
	AM_License_Menu::instance( __FILE__, 'API Manager Library', '1.1', 'plugin', 'https://www.toddlahman.com/' );
}
```

Preventing Unauthorized Plugin or Theme Use Before API Key Activation
============

To prevent a customer from using your software until after activating it with the API Key you can surround the code that loads your software with the following if condition:

```
// Above this line should be the code that ...
// Loads the API Key library. Must be placed in the root plugin file.
// as shown above on this page.
if ( get_option( 'software_title_all_lower_case_activated' ) == 'Activated' ) {
    // Code to load your plugin or theme here.
    // This code will not run until after the API Key is activated.
}
```

So what should "software_title_all_lower_case_activated" be in your plugin or theme? In this line of code:

```AM_License_Menu::instance( __FILE__, 'API Manager Library', '1.1', 'plugin', 'https://www.toddlahman.com/' );```

The Software Title is API Manager Library. When this passes to the API Manager PHP Library it is lowercased to make it "api_manager_library". Then it is concatenated to "_activated" to make it "api_manager_library_activated". Using this information our example with a Software Title of API Manager Library would be:

```
// Above this line should be the code that ...
// Loads the API Key library. Must be placed in the root plugin file.
// as shown above on this page.
if ( get_option( 'api_manager_library_activated' ) == 'Activated' ) {
    // Code to load your plugin or theme here.
    // This code will not run until after the API Key is activated.
}
```

Client Errors
============

Have the client follow these steps if they are unable to activate due an error such as "The API Key could not be deactivated. Use the API Key Deactivation tab to manually deactivate the API Key before activating a new API Key."

Solution:

* Go to the My Account dashboard, and disable the activation for the site, or they will end up with more than one for the same site. Their error to deactivate was returned because they used an instance ID that was incorrect, which is unique, and was for another activation.
* The store owner can also delete an activation on the order screen for the client.
* Go to Plugins and deactivate then activate the plugin. If is a theme, change the theme then change it back. (This erases the local data sent to the API Manager, and resets the unique Instance ID.
* Go to the plugin/theme settings page, and enter the API Key to reactivate the plugin/theme.

If the client reports an error such as "Connection failed to the License Key API server. Try again later." the problem is most likely one of these possibilities:

* There may be a problem with the client server preventing outgoing requests.
* The store the client is connecting to is blocking the client's request to activate the plugin/theme.
* Your site may be under attack, such as a DDOS (distributed denial of service).

Possible Solutions:

* Have the client disable CloudFlare, or other CDNs, all firewall and caching software.
* The API Manager store should also disable CloudFlare, or other CDNs, all firewall and caching software. Any of these can mangle HTTP/S requests. Also make sure the client software is not trying to connect using HTTP, and being forwarded to HTTPS, as this can cause breakage in some cases. The rule of thumb is to start working with the API Manager without a firewall or CDN, then add these services if desired, but always test to make sure everything is still working.
* In the case of a DDOS, you may need to carefully add CloudFlare, or some other firewall service. This could make things worse if you're not sure how to implement a firewall, so get help if some things are not working as expected.

Requirements
============

The WooCommerce API Manager.

Screenshots
============

The library uses the **Software Title** to create a unique settings menu item, and a page that can be used to activate and deactivate the product using the **API Key**. Below is a series of screenshots to demostrate the customer experience. Please note the plugin used is called **API Manager Library**, but the **Software Title** of **Simple Comments** was used to query the **API Manager** on a real store. If this were a real product, using a different **Software Title** from the actual plugin or theme title installed would cause expected issues, because the wrong product would be downloaded for updates.

![](https://cloud.githubusercontent.com/assets/3452849/15640356/183bd64a-25ec-11e6-9989-9311a87a78ad.png)

![](https://cloud.githubusercontent.com/assets/3452849/15640360/183e6d4c-25ec-11e6-92b2-f0368261bb90.png)

![](https://cloud.githubusercontent.com/assets/3452849/15640361/18411916-25ec-11e6-9a8a-1eeda026a05d.png)

![](https://cloud.githubusercontent.com/assets/3452849/15640357/183dd6ca-25ec-11e6-8aa5-ec73a268059a.png)

![](https://cloud.githubusercontent.com/assets/3452849/15640358/183df222-25ec-11e6-8c04-f5a80a4e62e7.png)

![](https://cloud.githubusercontent.com/assets/3452849/15640359/183e0eec-25ec-11e6-9770-757defde3c8e.png)

![](https://cloud.githubusercontent.com/assets/3452849/15640411/872681e0-25ec-11e6-97fa-13d01070924a.png)
