class-extension-updater.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * This class handles all the update-related stuff for extensions, including adding a license section to the license tab.
  4. * It accepts two args: Product Name and Version.
  5. *
  6. * @param $product_name string
  7. * @param $version string
  8. * @since 2.2.47
  9. * @return void
  10. */
  11. class NF_Extension_Updater
  12. {
  13. public $product_nice_name = '';
  14. public $product_name = '';
  15. public $version = '';
  16. public $store_url = 'https://ninjaforms.com/update-check/';
  17. public $file = '';
  18. public $author = '';
  19. public $error = '';
  20. private $_last_error;
  21. /**
  22. * Constructor function
  23. *
  24. * @since 2.2.47
  25. * @updated 3.0
  26. * @return void
  27. */
  28. public function __construct( $product_name, $version, $author, $file, $slug = '' )
  29. {
  30. $this->product_nice_name = $product_name;
  31. if ( $slug == '' ) {
  32. $this->product_name = strtolower( $product_name );
  33. $this->product_name = preg_replace( "/[^a-zA-Z]+/", "", $this->product_name );
  34. } else {
  35. $this->product_name = $slug;
  36. }
  37. $this->version = $version;
  38. $this->file = $file;
  39. $this->author = $author;
  40. $this->auto_update();
  41. add_filter( 'ninja_forms_settings_licenses_addons', array( $this, 'register' ) );
  42. }
  43. /**
  44. * Function that adds the license entry fields to the license tab.
  45. *
  46. * @updated 3.0
  47. * @param array $licenses
  48. * @return array $licenses
  49. */
  50. function register( $licenses ) {
  51. $licenses[] = $this;
  52. return $licenses;
  53. }
  54. /*
  55. *
  56. * Function that activates our license
  57. *
  58. * @since 2.2.47
  59. * @return void
  60. */
  61. function activate_license( $license_key ) {
  62. // data to send in our API request
  63. $api_params = array(
  64. 'edd_action'=> 'activate_license',
  65. 'license' => $license_key,
  66. 'item_name' => urlencode( $this->product_nice_name ), // the name of our product in EDD
  67. 'url' => home_url()
  68. );
  69. // Call the custom API.
  70. $response = wp_remote_post( $this->store_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
  71. $this->maybe_debug( $response );
  72. // make sure the response came back okay
  73. if ( is_wp_error( $response ) )
  74. return false;
  75. // decode the license data
  76. $license_data = json_decode( wp_remote_retrieve_body( $response ) );
  77. if ( 'invalid' == $license_data->license ) {
  78. $error = '<span style="color: red;">' . __( 'Could not activate license. Please verify your license key', 'ninja-forms' ) . '</span>';
  79. if ( isset ( $_REQUEST[ 'nf_debug' ] ) && 1 == absint( $_REQUEST[ 'nf_debug' ] ) ) {
  80. // Add an error to our admin notice if nf_debug is turned on.
  81. add_filter( 'nf_admin_notices', array( $this, 'show_license_error_notice' ) );
  82. $this->_last_error = var_export( $license_data, true );
  83. }
  84. Ninja_Forms()->logger()->emergency( var_export( $license_data, true ) );
  85. } else {
  86. $error = '';
  87. }
  88. Ninja_Forms()->update_setting( $this->product_name . '_license', $license_key );
  89. Ninja_Forms()->update_setting( $this->product_name . '_license_error', $error );
  90. Ninja_Forms()->update_setting( $this->product_name . '_license_status', $license_data->license );
  91. }
  92. public function show_license_error_notice( $notices ) {
  93. $notices[ 'license_error' ] = array(
  94. 'title' => __( 'License Activation Error', 'ninja-forms' ),
  95. 'msg' => '<pre>' . $this->_last_error . '</pre>',
  96. 'int' => 0,
  97. 'ignore_spam' => true,
  98. );
  99. return $notices;
  100. }
  101. /*
  102. *
  103. * Function that deactivates our license if the user clicks the "Deactivate License" button.
  104. *
  105. * @since 2.2.47
  106. * @return void
  107. */
  108. function deactivate_license() {
  109. $license = Ninja_Forms()->get_setting( $this->product_name . '_license' );
  110. // data to send in our API request
  111. $api_params = array(
  112. 'edd_action'=> 'deactivate_license',
  113. 'license' => $license,
  114. 'item_name' => urlencode( $this->product_nice_name ), // the name of our product in EDD
  115. 'url' => home_url()
  116. );
  117. // Call the custom API.
  118. $response = wp_remote_post( $this->store_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
  119. $this->maybe_debug( $response );
  120. // make sure the response came back okay
  121. if ( is_wp_error( $response ) )
  122. return false;
  123. Ninja_Forms()->update_setting( $this->product_name.'_license_error', '' );
  124. Ninja_Forms()->update_setting( $this->product_name.'_license_status', 'invalid' );
  125. Ninja_Forms()->update_setting( $this->product_name.'_license', '' );
  126. }
  127. /**
  128. * Function that runs all of our auto-update functionality
  129. *
  130. * @since 2.2.47
  131. * @updates 3.0
  132. * @return void
  133. */
  134. function auto_update() {
  135. $edd_updater = new EDD_SL_Plugin_Updater( $this->store_url, $this->file, array(
  136. 'author' => $this->author, // author of this plugin
  137. 'version' => $this->version, // current version number
  138. 'item_name' => $this->product_nice_name, // name of this plugin
  139. 'license' => Ninja_Forms()->get_setting( $this->product_name.'_license' ), // license key
  140. )
  141. );
  142. } // function auto_update
  143. /**
  144. * Return whether or not this license is valid.
  145. *
  146. * @access public
  147. * @since 2.9
  148. * @return bool
  149. */
  150. public function is_valid() {
  151. return ( 'valid' == Ninja_Forms()->get_setting( $this->product_name.'_license_status' ) );
  152. }
  153. /**
  154. * Get any error messages for this license field.
  155. *
  156. * @access public
  157. * @since 2.9
  158. * @return string $error
  159. */
  160. public function get_error() {
  161. return Ninja_Forms()->get_setting( $this->product_name . '_license_error' );
  162. }
  163. private function maybe_debug( $data, $key = 'debug' )
  164. {
  165. if ( isset ( $_GET[ $key ] ) && 'true' == $_GET[ $key ] ) {
  166. echo '<pre>'; var_dump( $data ); echo '</pre>';
  167. die();
  168. }
  169. }
  170. } // End Class NF_Extension_Updater