NF_Extension_Updater

This class handles all the update-related stuff for extensions, including adding a license section to the license tab.

Defined (2)

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

/deprecated/includes/class-extension-updater.php  
  1. class NF_Extension_Updater 
  2.  
  3. /** 
  4. * Define our class variables 
  5. */ 
  6. public $product_nice_name = ''; 
  7. public $product_name = ''; 
  8. public $version = ''; 
  9. public $store_url = 'https://ninjaforms.com/update-check/'; 
  10. public $file = ''; 
  11. public $author = ''; 
  12. public $error = ''; 
  13.  
  14. /** 
  15. * Constructor function 
  16. * @since 2.2.47 
  17. * @return void 
  18. */ 
  19.  
  20. function __construct( $product_name, $version, $author, $file, $slug = '' ) { 
  21. $this->product_nice_name = $product_name; 
  22. if ( $slug == '' ) { 
  23. $this->product_name = strtolower( $product_name ); 
  24. $this->product_name = preg_replace( "/[^a-zA-Z]+/", "", $this->product_name );  
  25. } else { 
  26. $this->product_name = $slug; 
  27.  
  28. $this->version = $version; 
  29. $this->file = str_replace( '/deprecated', '', $file ); 
  30. $this->author = $author; 
  31.  
  32. $this->add_license_fields(); 
  33. $this->auto_update(); 
  34.  
  35. } // function constructor 
  36.  
  37. /** 
  38. * Function that adds the license entry fields to the license tab. 
  39. * @since 2.2.47 
  40. * @return void 
  41. */ 
  42.  
  43. function add_license_fields() { 
  44. $valid = $this->is_valid(); 
  45. $error = $this->get_error(); 
  46. $note = $valid ? '' : __( 'You will find this included with your purchase email.', 'ninja-forms' ); 
  47. $desc = $error ? $error : $note; 
  48. $args = array( 
  49. 'page' => 'ninja-forms-settings',  
  50. 'tab' => 'license_settings',  
  51. 'slug' => 'license_settings',  
  52. 'settings' => array( 
  53. array( 
  54. 'name' => $this->product_name.'_license',  
  55. 'type' => 'custom',  
  56. 'label' => $this->product_nice_name.' '.__( 'Key', 'ninja-forms' ),  
  57. 'desc' => $desc,  
  58. 'save_function' => array( $this, 'check_license' ),  
  59. 'class' => 'test',  
  60. 'display_function' => array( $this, 'output_field' ),  
  61. ),  
  62. ),  
  63. ); 
  64. if( function_exists( 'ninja_forms_register_tab_metabox_options' ) ) { 
  65. ninja_forms_register_tab_metabox_options( $args ); 
  66. } // function add_license_fields 
  67.  
  68. /** 
  69. * Function that activates the license for this product 
  70. * @since 2.2.47 
  71. * @return void 
  72. */ 
  73.  
  74. function check_license( $data ) { 
  75. // Check to see if we've clicked the deactivate all button. 
  76. if ( isset ( $data['deactivate_all'] ) ) { 
  77. $this->deactivate_license(); 
  78. } else if ( isset ( $data[ 'deactivate_license_' . $this->product_name ] ) ) { // Check to see if we've clicked a deactivation button. 
  79. $this->deactivate_license(); 
  80. return false; 
  81. } else if ( isset ( $data[ $this->product_name . '_license' ] ) ) { 
  82. $this->activate_license( $data ); 
  83.  
  84. } // function check_license 
  85.  
  86. /** 
  87. * Function that activates our license 
  88. * @since 2.2.47 
  89. * @return void 
  90. */ 
  91.  
  92. function activate_license( $data ) { 
  93.  
  94. $plugin_settings = nf_get_settings(); 
  95. // retrieve the license from the database 
  96. $license = $data[ $this->product_name.'_license' ]; 
  97.  
  98. // data to send in our API request 
  99. $api_params = array(  
  100. 'edd_action'=> 'activate_license',  
  101. 'license' => $license,  
  102. 'item_name' => urlencode( $this->product_nice_name ) // the name of our product in EDD 
  103. ); 
  104.  
  105. // Call the custom API. 
  106. $response = wp_remote_post( esc_url_raw( add_query_arg( $api_params, $this->store_url ) ) ); 
  107.  
  108. if ( isset ( $_GET['debug'] ) && 'true' == $_GET['debug'] ) { 
  109. echo '<pre>'; 
  110. var_dump( $response ); 
  111. echo '</pre>'; 
  112. die(); 
  113.  
  114. // make sure the response came back okay 
  115. if ( is_wp_error( $response ) ) 
  116. return false; 
  117.  
  118. // decode the license data 
  119. $license_data = json_decode( wp_remote_retrieve_body( $response ) ); 
  120.  
  121. // $license_data->license will be either "valid" or "invalid" 
  122. $plugin_settings[ $this->product_name . '_license' ] = $license; 
  123. $plugin_settings[ $this->product_name . '_license_status' ] = $license_data->license; 
  124.  
  125. if ( 'invalid' == $license_data->license ) { 
  126. $error = '<span style="color: red;">' . __( 'Could not activate license. Please verify your license key', 'ninja-forms' ) . '</span>'; 
  127. } else { 
  128. $error = ''; 
  129.  
  130. $plugin_settings[ $this->product_name . '_license_error' ] = $error; 
  131.  
  132. update_option( 'ninja_forms_settings', $plugin_settings ); 
  133.  
  134. /** 
  135. * Function that deactivates our license if the user clicks the "Deactivate License" button. 
  136. * @since 2.2.47 
  137. * @return void 
  138. */ 
  139.  
  140. function deactivate_license() { 
  141. $plugin_settings = nf_get_settings(); 
  142.  
  143. if( isset( $plugin_settings[ $this->product_name.'_license_status' ] ) ) { 
  144. $status = $plugin_settings[ $this->product_name.'_license_status' ]; 
  145. }else{ 
  146. $status = 'invalid'; 
  147.  
  148. if( isset( $plugin_settings[ $this->product_name.'_license' ] ) ) { 
  149. $license = $plugin_settings[ $this->product_name.'_license']; 
  150. }else{ 
  151. $license = ''; 
  152.  
  153. // data to send in our API request 
  154. $api_params = array(  
  155. 'edd_action'=> 'deactivate_license',  
  156. 'license' => $license,  
  157. 'item_name' => urlencode( $this->product_nice_name ) // the name of our product in EDD 
  158. ); 
  159.  
  160. // Call the custom API. 
  161. $response = wp_remote_post( esc_url_raw( add_query_arg( $api_params, $this->store_url ) ), array( 'timeout' => 15, 'sslverify' => false ) ); 
  162.  
  163. if ( isset ( $_GET['debug'] ) && 'true' == $_GET['debug'] ) { 
  164. echo '<pre>'; 
  165. var_dump( $response ); 
  166. echo '</pre>'; 
  167. die(); 
  168.  
  169. // make sure the response came back okay 
  170. if ( is_wp_error( $response ) ) 
  171. return false; 
  172.  
  173. // decode the license data 
  174. $license_data = json_decode( wp_remote_retrieve_body( $response ) ); 
  175.  
  176. $plugin_settings[ $this->product_name.'_license_error' ] = ''; 
  177. // $license_data->license will be either "deactivated" or "failed" 
  178. // if( 'deactivated' == $license_data->license ) { 
  179. // $license_data->license will be either "valid" or "invalid" 
  180. $plugin_settings[ $this->product_name.'_license_status' ] = 'invalid'; 
  181. $plugin_settings[ $this->product_name.'_license' ] = ''; 
  182. // } 
  183. update_option( 'ninja_forms_settings', $plugin_settings ); 
  184.  
  185. /** 
  186. * Function that runs all of our auto-update functionality 
  187. * @since 2.2.47 
  188. * @return void 
  189. */ 
  190.  
  191. function auto_update() { 
  192. $plugin_settings = nf_get_settings(); 
  193.  
  194. // retrieve our license key from the DB 
  195. if( isset( $plugin_settings[ $this->product_name.'_license' ] ) ) { 
  196. $license = $plugin_settings[ $this->product_name.'_license' ]; 
  197. }else{ 
  198. $license = ''; 
  199.  
  200. // setup the updater 
  201. $edd_updater = new EDD_SL_Plugin_Updater( $this->store_url, $this->file, array( 
  202. 'version' => $this->version, // current version number 
  203. 'license' => $license, // license key (used get_option above to retrieve from DB) 
  204. 'item_name' => $this->product_nice_name, // name of this plugin 
  205. 'author' => $this->author, // author of this plugin 
  206. ); 
  207. } // function auto_update 
  208.  
  209. /** 
  210. * Return whether or not this license is valid. 
  211. *  
  212. * @access public 
  213. * @since 2.9 
  214. * @return bool 
  215. */ 
  216. public function is_valid() { 
  217. $plugin_settings = nf_get_settings(); 
  218. if( isset( $plugin_settings[ $this->product_name.'_license_status' ] ) && $plugin_settings[ $this->product_name.'_license_status' ] == 'valid' ) { 
  219. return true; 
  220. }else{ 
  221. return false; 
  222.  
  223. /** 
  224. * Output our field for entering and deactivating a license. 
  225. *  
  226. * @access public 
  227. * @since 2.9 
  228. * @return void 
  229. */ 
  230. public function output_field( $form_id, $data, $field ) { 
  231. $valid = $this->is_valid(); 
  232. if ( $valid ) { 
  233. $license = isset ( $data[ $this->product_name . '_license' ] ) ? $data[ $this->product_name . '_license' ] : ''; 
  234. ?> 
  235. <span class="nf-license"><?php echo $license; ?></span> 
  236. <input type="submit" class="button-secondary" name="deactivate_license_<?php echo $this->product_name; ?>" value="<?php _e( 'Deactivate License', 'ninja-forms' ); ?>"> 
  237. <?php 
  238. } else { 
  239. ?> 
  240. <input type="text" style="width:55%" class="code" name="<?php echo $this->product_name . '_license'; ?>" id="" value="" /> 
  241. <?php 
  242.  
  243. /** 
  244. * Get any error messages for this license field. 
  245. *  
  246. * @access public 
  247. * @since 2.9 
  248. * @return string $error 
  249. */ 
  250. public function get_error() { 
  251. $plugin_settings = nf_get_settings(); 
  252. $error = ! empty( $plugin_settings[ $this->product_name . '_license_error' ] ) ? $plugin_settings[ $this->product_name . '_license_error' ] : false; 
  253. return $error; 
  254.  
  255. } // class 
/includes/Integrations/EDD/class-extension-updater.php  
  1. class NF_Extension_Updater 
  2. public $product_nice_name = ''; 
  3. public $product_name = ''; 
  4. public $version = ''; 
  5. public $store_url = 'https://ninjaforms.com/update-check/'; 
  6. public $file = ''; 
  7. public $author = ''; 
  8. public $error = ''; 
  9.  
  10. /** 
  11. * Constructor function 
  12. * @since 2.2.47 
  13. * @updated 3.0 
  14. * @return void 
  15. */ 
  16. public function __construct( $product_name, $version, $author, $file, $slug = '' ) 
  17. $this->product_nice_name = $product_name; 
  18. if ( $slug == '' ) { 
  19. $this->product_name = strtolower( $product_name ); 
  20. $this->product_name = preg_replace( "/[^a-zA-Z]+/", "", $this->product_name ); 
  21. } else { 
  22. $this->product_name = $slug; 
  23.  
  24. $this->version = $version; 
  25. $this->file = $file; 
  26. $this->author = $author; 
  27.  
  28. $this->auto_update(); 
  29.  
  30. add_filter( 'ninja_forms_settings_licenses_addons', array( $this, 'register' ) ); 
  31.  
  32. /** 
  33. * Function that adds the license entry fields to the license tab. 
  34. * @updated 3.0 
  35. * @param array $licenses 
  36. * @return array $licenses 
  37. */ 
  38. function register( $licenses ) { 
  39. $licenses[] = $this; 
  40. return $licenses; 
  41.  
  42. /** 
  43. * Function that activates our license 
  44. * @since 2.2.47 
  45. * @return void 
  46. */ 
  47. function activate_license( $license_key ) { 
  48.  
  49. // data to send in our API request 
  50. $api_params = array( 
  51. 'edd_action'=> 'activate_license',  
  52. 'license' => $license_key,  
  53. 'item_name' => urlencode( $this->product_nice_name ), // the name of our product in EDD 
  54. 'url' => home_url() 
  55. ); 
  56.  
  57. // Call the custom API. 
  58. $response = wp_remote_post( $this->store_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) ); 
  59.  
  60. $this->maybe_debug( $response ); 
  61.  
  62. // make sure the response came back okay 
  63. if ( is_wp_error( $response ) ) 
  64. return false; 
  65.  
  66. // decode the license data 
  67. $license_data = json_decode( wp_remote_retrieve_body( $response ) ); 
  68.  
  69. if ( 'invalid' == $license_data->license ) { 
  70. $error = '<span style="color: red;">' . __( 'Could not activate license. Please verify your license key', 'ninja-forms' ) . '</span>'; 
  71. } else { 
  72. $error = ''; 
  73.  
  74. Ninja_Forms()->update_setting( $this->product_name . '_license', $license_key ); 
  75. Ninja_Forms()->update_setting( $this->product_name . '_license_error', $error ); 
  76. Ninja_Forms()->update_setting( $this->product_name . '_license_status', $license_data->license ); 
  77.  
  78. /** 
  79. * Function that deactivates our license if the user clicks the "Deactivate License" button. 
  80. * @since 2.2.47 
  81. * @return void 
  82. */ 
  83.  
  84. function deactivate_license() { 
  85.  
  86. $license = Ninja_Forms()->get_setting( $this->product_name . '_license' ); 
  87.  
  88. // data to send in our API request 
  89. $api_params = array( 
  90. 'edd_action'=> 'deactivate_license',  
  91. 'license' => $license,  
  92. 'item_name' => urlencode( $this->product_nice_name ), // the name of our product in EDD 
  93. 'url' => home_url() 
  94. ); 
  95.  
  96. // Call the custom API. 
  97. $response = wp_remote_post( $this->store_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) ); 
  98.  
  99. $this->maybe_debug( $response ); 
  100.  
  101. // make sure the response came back okay 
  102. if ( is_wp_error( $response ) ) 
  103. return false; 
  104.  
  105. Ninja_Forms()->update_setting( $this->product_name.'_license_error', '' ); 
  106. Ninja_Forms()->update_setting( $this->product_name.'_license_status', 'invalid' ); 
  107. Ninja_Forms()->update_setting( $this->product_name.'_license', '' ); 
  108.  
  109. /** 
  110. * Function that runs all of our auto-update functionality 
  111. * @since 2.2.47 
  112. * @updates 3.0 
  113. * @return void 
  114. */ 
  115. function auto_update() { 
  116.  
  117. $edd_updater = new EDD_SL_Plugin_Updater( $this->store_url, $this->file, array( 
  118. 'author' => $this->author, // author of this plugin 
  119. 'version' => $this->version, // current version number 
  120. 'item_name' => $this->product_nice_name, // name of this plugin 
  121. 'license' => Ninja_Forms()->get_setting( $this->product_name.'_license' ), // license key 
  122. ); 
  123. } // function auto_update 
  124.  
  125. /** 
  126. * Return whether or not this license is valid. 
  127. * @access public 
  128. * @since 2.9 
  129. * @return bool 
  130. */ 
  131. public function is_valid() { 
  132. return ( 'valid' == Ninja_Forms()->get_setting( $this->product_name.'_license_status' ) ); 
  133.  
  134. /** 
  135. * Get any error messages for this license field. 
  136. * @access public 
  137. * @since 2.9 
  138. * @return string $error 
  139. */ 
  140. public function get_error() { 
  141. return Ninja_Forms()->get_setting( $this->product_name . '_license_error' ); 
  142.  
  143. private function maybe_debug( $data, $key = 'debug' ) 
  144. if ( isset ( $_GET[ $key ] ) && 'true' == $_GET[ $key ] ) { 
  145. echo '<pre>'; var_dump( $data ); echo '</pre>'; 
  146. die(); 
  147.  
  148. } // End Class NF_Extension_Updater