      1 <?php
      2 /**
      3  * License handler for Easy Digital Downloads
      4  *
      5  * This class should simplify the process of adding license information
      6  * to new EDD extensions.
      7  *
      8  * @package EDD_License
      9  * @version 1.1
     10  */
     11
     12 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
     13
     14 if ( ! class_exists( 'EDD_License' ) ) :
     15
     16 /**
     17  * EDD_License Class
     18  *
     19  * @package EDD_License
     20  */
     21 class EDD_License {
     22     private $file;
     23     private $license;
     24     private $item_name;
     25     private $item_shortname;
     26     private $version;
     27     private $author;
     28
     29     /**
     30      * Class constructor
     31      *
     32      * @global  array $edd_options
     33      * @param string  $_file
     34      * @param string  $_item_name
     35      * @param string  $_version
     36      * @param string  $_author
     37      */
     38     function __construct( $_file, $_item_name, $_version, $_author, $_optname = null ) {
     39         global $edd_options;
     40
     41         $this->file           = $_file;
     42         $this->item_name      = $_item_name;
     43         $this->item_shortname = 'edd_' . preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $this->item_name ) ) );
     44         $this->version        = $_version;
     45         $this->license        = isset( $edd_options[ $this->item_shortname . '_license_key' ] ) ? trim( $edd_options[ $this->item_shortname . '_license_key' ] ) : '';
     46         $this->author         = $_author;
     47
     48         /**
     49          * Allows for backwards compatibility with old license options,
     50          * i.e. if the plugins had license key fields previously, the license
     51          * handler will automatically pick these up and use those in lieu of the
     52          * user having to reactive their license.
     53          */
     54         if ( ! empty( $_optname ) && isset( $edd_options[ $_optname ] ) && empty( $this->license ) ) {
     55             $this->license = trim( $edd_options[ $_optname ] );
     56         }
     57
     58         // Setup hooks
     59         $this->includes();
     60         $this->hooks();
     61         $this->auto_updater();
     62     }
     63
     64     /**
     65      * Include the updater class
     66      *
     67      * @access  private
     68      * @return  void
     69      */
     70     private function includes() {
     71         if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) require_once 'class-edd-sl-plugin-updater.php';
     72     }
     73
     74     /**
     75      * Setup hooks
     76      *
     77      * @access  private
     78      * @return  void
     79      */
     80     private function hooks() {
     81         // Register settings
     82         add_filter( 'edd_settings_licenses', array( $this, 'settings' ), 1 );
     83
     84         // Activate license key on settings save
     85         add_action( 'admin_init', array( $this, 'activate_license' ) );
     86
     87         // Deactivate license key
     88         add_action( 'admin_init', array( $this, 'deactivate_license' ) );
     89     }
     90
     91     /**
     92      * Auto updater
     93      *
     94      * @access  private
     95      * @global  array $edd_options
     96      * @return  void
     97      */
     98     private function auto_updater() {
     99         // Setup the updater
    100         $edd_updater = new EDD_SL_Plugin_Updater(
    101             'https://easydigitaldownloads.com',
    102             $this->file,
    103             array(
    104                 'version'   => $this->version,
    105                 'license'   => $this->license,
    106                 'item_name' => $this->item_name,
    107                 'author'    => $this->author
    108             )
    109         );
    110     }
    111
    112
    113     /**
    114      * Add license field to settings
    115      *
    116      * @access  public
    117      * @param array   $settings
    118      * @return  array
    119      */
    120     public function settings( $settings ) {
    121         $edd_license_settings = array(
    122             array(
    123                 'id'      => $this->item_shortname . '_license_key',
    124                 'name'    => sprintf( __( '%1$s License Key', 'edd' ), $this->item_name ),
    125                 'desc'    => '',
    126                 'type'    => 'license_key',
    127                 'options' => array( 'is_valid_license_option' => $this->item_shortname . '_license_active' ),
    128                 'size'    => 'regular'
    129             )
    130         );
    131
    132         return array_merge( $settings, $edd_license_settings );
    133     }
    134
    135
    136     /**
    137      * Activate the license key
    138      *
    139      * @access  public
    140      * @return  void
    141      */
    142     public function activate_license() {
    143         if ( ! isset( $_POST['edd_settings_licenses'] ) )
    144             return;
    145
    146         if ( ! isset( $_POST['edd_settings_licenses'][ $this->item_shortname . '_license_key' ] ) )
    147             return;
    148
    149         if ( 'valid' == get_option( $this->item_shortname . '_license_active' ) )
    150             return;
    151
    152         $license = sanitize_text_field( $_POST['edd_settings_licenses'][ $this->item_shortname . '_license_key' ] );
    153
    154         // Data to send to the API
    155         $api_params = array(
    156             'edd_action' => 'activate_license',
    157             'license'    => $license,
    158             'item_name'  => urlencode( $this->item_name )
    159         );
    160
    161         // Call the API
    162         $response = wp_remote_get(
    163             add_query_arg( $api_params, 'https://easydigitaldownloads.com' ),
    164             array(
    165                 'timeout'   => 15,
    166                 'body'      => $api_params,
    167                 'sslverify' => false
    168             )
    169         );
    170
    171         // Make sure there are no errors
    172         if ( is_wp_error( $response ) )
    173             return;
    174
    175         // Decode license data
    176         $license_data = json_decode( wp_remote_retrieve_body( $response ) );
    177
    178         update_option( $this->item_shortname . '_license_active', $license_data->license );
    179     }
    180
    181
    182     /**
    183      * Deactivate the license key
    184      *
    185      * @access  public
    186      * @return  void
    187      */
    188     public function deactivate_license() {
    189         if ( ! isset( $_POST['edd_settings_licenses'] ) )
    190             return;
    191
    192         if ( ! isset( $_POST['edd_settings_licenses'][ $this->item_shortname . '_license_key' ] ) )
    193             return;
    194
    195         // Run on deactivate button press
    196         if ( isset( $_POST[ $this->item_shortname . '_license_key_deactivate' ] ) ) {
    197             // Run a quick security check
    198             if ( check_admin_referer( $this->item_shortname . '_license_key_nonce', $this->item_shortname . '_license_key_nonce' ) )
    199                 return;
    200
    201             // Data to send to the API
    202             $api_params = array(
    203                 'edd_action' => 'deactivate_license',
    204                 'license'    => $this->license,
    205                 'item_name'  => urlencode( $this->item_name )
    206             );
    207
    208             // Call the API
    209             $response = wp_remote_get(
    210                 add_query_arg( $api_params, 'https://easydigitaldownloads.com' ),
    211                 array(
    212                     'timeout'   => 15,
    213                     'sslverify' => false
    214                 )
    215             );
    216
    217             // Make sure there are no errors
    218             if ( is_wp_error( $response ) )
    219                 return;
    220
    221             // Decode the license data
    222             $license_data = json_decode( wp_remote_retrieve_body( $response ) );
    223
    224             if ( $license_data->license == 'deactivated' )
    225                 delete_option( $this->item_shortname . '_license_active' );
    226         }
    227     }
    228 }
    229
    230 endif; // end class_exists check
    231
    232
    233 /**
    234  * Register the new license field type
    235  *
    236  * This has been included in core, but is maintained for backwards compatibility
    237  *
    238  * @return  void
    239  */
    240 if ( ! function_exists( 'edd_license_key_callback' ) ) {
    241     function edd_license_key_callback( $args ) {
    242         global $edd_options;
    243
    244         if ( isset( $edd_options[ $args['id'] ] ) )
    245             $value = $edd_options[ $args['id'] ];
    246         else
    247             $value = isset( $args['std'] ) ? $args['std'] : '';
    248
    249         $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
    250         $html = '<input type="text" class="' . $size . '-text" id="edd_settings_' . $args['section'] . '[' . $args['id'] . ']" name="edd_settings_' . $args['section'] . '[' . $args['id'] . ']" value="' . esc_attr( $value ) . '"/>';
    251
    252         if ( 'valid' == get_option( $args['options']['is_valid_license_option'] ) ) {
    253             $html .= wp_nonce_field( $args['id'] . '_nonce', $args['id'] . '_nonce', false );
    254             $html .= '<input type="submit" class="button-secondary" name="' . $args['id'] . '_deactivate" value="' . __( 'Deactivate License',  'edd-recurring' ) . '"/>';
    255         }
    256
    257         $html .= '<label for="edd_settings_' . $args['section'] . '[' . $args['id'] . ']"> '  . $args['desc'] . '</label>';
    258
    259         echo $html;
    260     }
    261 }
    262