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