PLL_License

A class to easily manage licenses for Polylang Pro and addons.

Defined (1)

The class is defined in the following location(s).

/include/license.php  
  1. class PLL_License { 
  2. public $id, $name, $license_key, $license_data; 
  3. private $file, $version, $author; 
  4. private $api_url = 'https://polylang.pro'; 
  5.  
  6. /** 
  7. * Constructor 
  8. * @since 1.9 
  9. * @param string $file 
  10. * @param string $item_name 
  11. * @param string $version 
  12. * @param string $author 
  13. * @param string $api_url optional 
  14. */ 
  15. function __construct( $file, $item_name, $version, $author, $api_url = null ) { 
  16. $this->id = sanitize_title( $item_name ); 
  17. $this->file = $file; 
  18. $this->name = $item_name; 
  19. $this->version = $version; 
  20. $this->author = $author; 
  21. $this->api_url = empty( $api_url ) ? $this->api_url : $api_url; 
  22.  
  23. $licenses = get_option( 'polylang_licenses' ); 
  24. $this->license_key = empty( $licenses[ $this->id ]['key'] ) ? '' : $licenses[ $this->id ]['key']; 
  25. if ( ! empty( $licenses[ $this->id ]['data'] ) ) { 
  26. $this->license_data = $licenses[ $this->id ]['data']; 
  27.  
  28. // Updater 
  29. add_action( 'admin_init', array( $this, 'auto_updater' ), 0 ); 
  30.  
  31. // Register settings 
  32. add_filter( 'pll_settings_licenses', array( $this, 'settings' ) ); 
  33.  
  34. // Weekly schedule 
  35. if ( ! wp_next_scheduled( 'polylang_check_licenses' ) ) { 
  36. wp_schedule_event( time(), 'weekly', 'polylang_check_licenses' ); 
  37.  
  38. add_action( 'polylang_check_licenses', array( $this, 'check_license' ) ); 
  39.  
  40. /** 
  41. * Auto updater 
  42. * @since 1.9 
  43. */ 
  44. public function auto_updater() { 
  45. $args = array( 
  46. 'version' => $this->version,  
  47. 'license' => $this->license_key,  
  48. 'author' => $this->author,  
  49. 'item_name' => $this->name,  
  50. ); 
  51.  
  52. // Setup the updater 
  53. new PLL_Plugin_Updater( $this->api_url, $this->file, $args ); 
  54.  
  55. /** 
  56. * Registers the licence in the Settings 
  57. * @since 1.9 
  58. * @param array $items 
  59. * @return array 
  60. */ 
  61. public function settings( $items ) { 
  62. $items[ $this->id ] = $this; 
  63. return $items; 
  64.  
  65. /** 
  66. * Activate the license key 
  67. * @since 1.9 
  68. * @param string $license_key activation key 
  69. * @return object updated $this 
  70. */ 
  71. public function activate_license( $license_key ) { 
  72. $this->license_key = $license_key; 
  73. $this->api_request( 'activate_license' ); 
  74.  
  75. // Tell WordPress to look for updates 
  76. set_site_transient( 'update_plugins', null ); 
  77. return $this; 
  78.  
  79.  
  80. /** 
  81. * Deactivate the license key 
  82. * @since 1.9 
  83. * @return object updated $this 
  84. */ 
  85. public function deactivate_license() { 
  86. $this->api_request( 'deactivate_license' ); 
  87. return $this; 
  88.  
  89. /** 
  90. * Check if license key is valid 
  91. * @since 1.9 
  92. * @return object updated $this 
  93. */ 
  94. public function check_license() { 
  95. $this->api_request( 'check_license' ); 
  96. return $this; 
  97.  
  98. /** 
  99. * Sends an api request to check, activate or deactivate the license 
  100. * Updates the licenses option according to the status 
  101. * @since 1.9 
  102. * @param string $request check_license | activate_license | deactivate_license 
  103. */ 
  104. private function api_request( $request ) { 
  105. $licenses = get_option( 'polylang_licenses' ); 
  106. unset( $licenses[ $this->id ], $this->license_data ); 
  107.  
  108. if ( ! empty( $this->license_key ) ) { 
  109. // Data to send in our API request 
  110. $api_params = array( 
  111. 'edd_action' => $request,  
  112. 'license' => $this->license_key,  
  113. 'item_name' => urlencode( $this->name ),  
  114. 'url' => home_url(),  
  115. ); 
  116.  
  117. // Call the API 
  118. $response = wp_remote_post( 
  119. $this->api_url,  
  120. array( 
  121. 'timeout' => 15,  
  122. 'sslverify' => false,  
  123. 'body' => $api_params,  
  124. ); 
  125.  
  126. // Update the option only if we got a response 
  127. if ( is_wp_error( $response ) ) { 
  128. return; 
  129.  
  130. // Save new license info 
  131. $licenses[ $this->id ] = array( 'key' => $this->license_key ); 
  132. $data = json_decode( wp_remote_retrieve_body( $response ) ); 
  133.  
  134. if ( 'deactivated' !== $data->license ) { 
  135. $licenses[ $this->id ]['data'] = $this->license_data = $data; 
  136.  
  137. update_option( 'polylang_licenses', $licenses ); // FIXME called multiple times when saving all licenses