Licenses.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Class NF_Admin_Menus_Licenses
  4. */
  5. final class NF_Admin_Menus_Licenses
  6. {
  7. private $licenses = array();
  8. public function __construct()
  9. {
  10. add_action( 'admin_init', array( $this, 'register_licenses' ), 10 );
  11. add_action( 'admin_init', array( $this, 'submit_listener' ), 11 );
  12. add_action( 'admin_init', array( $this, 'add_meta_boxes' ), 12 );
  13. }
  14. public function submit_listener()
  15. {
  16. if( ! current_user_can( apply_filters( 'ninja_forms_admin_license_update_capabilities', 'manage_options' ) ) ) return;
  17. if( ! isset( $_POST[ 'ninja_forms_license' ] ) || ! $_POST[ 'ninja_forms_license' ] ) return;
  18. $key = sanitize_text_field( $_POST[ 'ninja_forms_license' ][ 'key' ] );
  19. $name = sanitize_text_field( $_POST[ 'ninja_forms_license' ][ 'name' ] );
  20. $action = sanitize_text_field( $_POST[ 'ninja_forms_license' ][ 'action' ] );
  21. switch( $action ){
  22. case 'activate':
  23. $this->activate_license( $name, $key );
  24. break;
  25. case 'deactivate':
  26. $this->deactivate_license( $name );
  27. break;
  28. }
  29. }
  30. public function register_licenses()
  31. {
  32. $this->licenses = apply_filters( 'ninja_forms_settings_licenses_addons', array() );
  33. }
  34. public function add_meta_boxes()
  35. {
  36. add_meta_box(
  37. 'nf_settings_licenses',
  38. __( 'Add-On Licenses', 'ninja-forms' ),
  39. array( $this, 'display' ),
  40. 'nf_settings_licenses'
  41. );
  42. }
  43. public function display()
  44. {
  45. $data = array();
  46. foreach( $this->licenses as $license ){
  47. $data[] = array(
  48. 'id' => $license->product_name,
  49. 'name' => $license->product_nice_name,
  50. 'version' => $license->version,
  51. 'is_valid' => $license->is_valid(),
  52. 'license' => $this->get_license( $license->product_name ),
  53. 'error' => Ninja_Forms()->get_setting( $license->product_name . '_license_error' ),
  54. );
  55. }
  56. Ninja_Forms()->template( 'admin-menu-settings-licenses.html.php', array( 'licenses' => $data ) );
  57. }
  58. private function get_license( $name )
  59. {
  60. return Ninja_Forms()->get_setting( $name . '_license' );
  61. }
  62. private function activate_license( $name, $key )
  63. {
  64. foreach( $this->licenses as $license ){
  65. if( $name != $license->product_name ) continue;
  66. $license->activate_license( $key );
  67. }
  68. }
  69. private function deactivate_license( $name )
  70. {
  71. foreach( $this->licenses as $license ){
  72. if( $name != $license->product_name ) continue;
  73. $license->deactivate_license();
  74. }
  75. }
  76. } // End Class NF_Admin_Menus_Licenses