-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.php
193 lines (147 loc) · 4.76 KB
/
test.php
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/*
Plugin Name: Plugin Updater Tester
Plugin URI: http://ironbounddesigns.com
Description: Test plugin updates
Version: 1.3
Author: Iron Bound Designs
Author URI: http://ironbounddesigns.com
License: GPLv2
*/
require_once( 'itelic-plugin-updater.php' );
define( 'ITELIC_DEMO_PRODUCT_ID', 18 );
define( 'ITELIC_DEMO_STORE_URL', 'http://itelic.wpengine.com/' );
//set_site_transient( 'update_plugins', null );
/**
* Register an admin menu to contain our license key.
*/
function itelic_demo_add_license_page() {
add_options_page( __( "Exchange Licensing Demo" ), __( "Exchange Licensing Demo" ), 'manage_options', 'itelic-demo', 'itelic_demo_render_license_page' );
}
add_action( 'admin_menu', 'itelic_demo_add_license_page' );
/**
* Render the license page.
*/
function itelic_demo_render_license_page() {
$key = get_option( 'itelic_demo_license_key' );
$track = get_option( 'itelic_demo_track' );
?>
<div class="wrap">
<h2><?php _e( "Exchange Licensing Demo" ); ?></h2>
<form action="<?php admin_url( 'options-general.php?page=itelic-demo' ); ?>" method="POST">
<table class="form-table">
<tbody>
<tr>
<th><label for="itelic-demo-license-key"><?php _e( "License Key" ); ?></label></th>
<td>
<input type="text" id="itelic-demo-license-key" name="itelic_demo_license_key" value="<?php echo esc_attr( $key ); ?>">
</td>
</tr>
<tr>
<th><label for="itelic-demo-track"><?php _e( "Enable beta releases" ); ?></label></th>
<td>
<input type="checkbox" id="itelic-demo-track" name="itelic_demo_track" <?php checked( 'pre-release', $track ); ?> value="pre-release">
</td>
</tr>
</tbody>
</table>
<p>
<?php submit_button( __( "Activate" ), 'primary', 'activate', false ); ?>
<?php submit_button( __( "Deactivate" ), 'secondary', 'deactivate', false ); ?>
</p>
<?php wp_nonce_field( 'itelic-demo-save' ); ?>
</form>
</div>
<?php
}
/**
* Activates/Deactivates license key depending on which submit button was
* pressed.
*/
function itelic_demo_save_license_key() {
if ( ! isset( $_GET['page'] ) || $_GET['page'] != 'itelic-demo' ) {
return;
}
if ( empty( $_POST['itelic_demo_license_key'] ) || empty( $_POST['_wpnonce'] ) ) {
return;
}
// verify capabilities
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// verify intent
if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'itelic-demo-save' ) ) {
return;
}
// sanitize key
$key = sanitize_text_field( $_POST['itelic_demo_license_key'] );
if ( isset( $_POST['activate'] ) ) {
update_option( 'itelic_demo_license_key', $key );
if ( isset( $_POST['itelic_demo_track'] ) && $_POST['itelic_demo_track'] == 'pre-release' ) {
$track = 'pre-release';
} else {
$track = 'stable';
}
update_option( 'itelic_demo_track', $track );
$id = itelic_make_plugin_updater()->activate( $key, $track );
if ( is_wp_error( $id ) ) {
$msg = $id->get_error_message();
if ( ! $msg ) {
$msg = __( "an unknown error occured." );
}
itelic_demo_display_admin_notice( 'error', sprintf( __( "Could not activate license key because %s" ), $msg ) );
} else {
itelic_demo_display_admin_notice( 'success', __( "License key activated." ) );
update_option( 'itelic_demo_activation_id', $id );
}
} elseif ( isset( $_POST['deactivate'] ) ) {
$id = get_option( 'itelic_demo_activation_id' );
if ( ! $id ) {
// this license key was never activated, so we don't need to do anything
return;
}
$response = itelic_make_plugin_updater()->deactivate( $key, $id );
if ( is_wp_error( $response ) ) {
$msg = $response->get_error_message();
if ( ! $msg ) {
$msg = __( "an unknown error occurred." );
}
itelic_demo_display_admin_notice( 'error', sprintf( __( "Could not deactivate the license key because %s" ), $msg ) );
} else {
itelic_demo_display_admin_notice( 'success', __( "License key deactivated." ) );
delete_option( 'itelic_demo_license_key' );
}
}
}
add_action( 'admin_notices', 'itelic_demo_save_license_key' );
/**
* Display an admin notice.
*
* @param string $type
* @param string $message
*
* @return void
*/
function itelic_demo_display_admin_notice( $type, $message ) {
?>
<div class="notice notice-<?php echo esc_attr( $type ); ?>">
<p><?php echo $message; ?></p>
</div>
<?php
}
/**
* Generates a plugin updater object.
*
* @return ITELIC_Plugin_Updater
*/
function itelic_make_plugin_updater() {
static $updater = null;
if ( $updater === null ) {
$updater = new ITELIC_Plugin_Updater( ITELIC_DEMO_STORE_URL, ITELIC_DEMO_PRODUCT_ID, __FILE__, array(
'key' => get_option( 'itelic_demo_license_key' ),
'activation_id' => get_option( 'itelic_demo_activation_id', 0 )
) );
}
return $updater;
}
add_action( 'admin_init', 'itelic_make_plugin_updater', 0 );