-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpmpro-pl-sample-plugin.php
More file actions
271 lines (241 loc) · 9.09 KB
/
pmpro-pl-sample-plugin.php
File metadata and controls
271 lines (241 loc) · 9.09 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
/**
* Paid Memberships Pro - Plugin Licensing - Sample Plugin
*
* @package pmpro-pl-sample-plugin
*/
/*
* Plugin Name: Paid Memberships Pro - Plugin Licensing - Sample Plugin
* Plugin URI: https://github.com/breakfastco/pmpro-pl-sample-plugin
* Description: A sample WordPress plugin for testing Plugin Licensing for Paid Memberships Pro. This plugin has no features other than an updater to retrieve new versions.
* Author: Breakfast
* Author URI: https://breakfastco.xyz/
* License: GPLv2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Version: 1.0.3
* Requires PHP: 5.6
* Text Domain: pmpro-pl-sample-plugin
* Domain Path: /languages
* GitHub Plugin URI: breakfastco/pmpro-pl-sample-plugin
* Primary Branch: main
*/
defined( 'ABSPATH' ) || exit;
// Define plugin constants.
define( 'PMPRO_PL_SAMPLE_PLUGIN_VERSION', '1.0.3' );
define( 'PMPRO_PL_SAMPLE_PLUGIN_FILE', __FILE__ );
define( 'PMPRO_PL_SAMPLE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'PMPRO_PL_SAMPLE_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
/**
* This is the hash generated by PMPro Plugin Licensing for this plugin. It
* is sent in requests to the store when activating licenses or checking for
* updates.
*/
define( 'PMPRO_PL_SAMPLE_PLUGIN_HASH', 'd6a953c1546774366ac47ed5e32dbf17' );
/**
* This is the URL of the store where this plugin was purchased that
* provides updates and activates licenses.
*/
define( 'PMPRO_PL_SAMPLE_PLUGIN_STORE_URL', 'https://breakfastco.xyz' );
/**
* PMPRO_PL_Sample_Plugin
*/
class PMPRO_PL_Sample_Plugin {
/**
* Constructor.
*/
public function __construct() {
add_action( 'init', array( $this, 'init' ) );
// Add a plugin action link to the plugins list.
add_filter( 'plugin_action_links_pmpro-pl-sample-plugin/pmpro-pl-sample-plugin.php', array( $this, 'add_missing_license_notice' ), 2, 2 );
// Settings page to let users manage their license key.
add_action( 'admin_menu', array( $this, 'add_settings_page' ) );
add_action( 'admin_init', array( $this, 'add_settings_fields' ) );
}
/**
* Initialize the plugin.
*/
public function init() {
// Load text domain for translations.
load_plugin_textdomain( 'pmpro-pl-sample-plugin', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
// Load base classes (included in this plugin for distribution without dependencies).
require_once dirname( PMPRO_PL_SAMPLE_PLUGIN_FILE ) . '/includes/class-pmpro-pl-updater-base.php';
require_once dirname( PMPRO_PL_SAMPLE_PLUGIN_FILE ) . '/includes/class-pmpro-pl-activator-base.php';
// Load our updater so users see pending updates for this plugin.
require_once dirname( PMPRO_PL_SAMPLE_PLUGIN_FILE ) . '/includes/class-pmpro-pl-updater.php';
$updater = PMPRO_PL_Sample_Updater::get_instance();
$updater->add_hooks();
// Activator activates licenses and checks for expiration.
require_once dirname( PMPRO_PL_SAMPLE_PLUGIN_FILE ) . '/includes/class-pmpro-pl-activator.php';
}
/**
* Adds a link to the settings page near the Activate | Delete links on the
* list of plugins on the Plugins page.
*
* @param array $links
* @return array
*/
public function add_missing_license_notice( $links ) {
$license_key = self::get_license_key();
$activator = PMPRO_PL_Sample_Activator::get_instance();
$license_status = $activator->get_status( $license_key );
if ( empty( $license_key ) || 'active' !== $license_status ) {
$links[] = sprintf(
'<a href="%s"><b>%s</b></a>',
admin_url( 'admin.php?page=' . $this->admin_page_slug() ),
esc_html__( 'Enable Updates', 'pmpro-pl-sample-plugin' )
);
}
return $links;
}
public function add_settings_fields() {
register_setting(
'pmpro_pl_sample_plugin', // option_group for settings_fields() call.
self::option_name(), // option_name.
array( $this, 'sanitize_license_key' ) // sanitize_callback.
);
add_settings_section(
'license', // id.
__( 'Enable Updates', 'pmpro-pl-sample-plugin' ), // title.
array( $this, 'settings_page_section_license' ), // content callback.
$this->admin_page_slug() // page.
);
add_settings_field(
'pmpro_pl_sample_plugin_license_key', // id.
__( 'License Key', 'pmpro-pl-sample-plugin' ), // title.
array( $this, 'license_key_field' ), // content callback.
$this->admin_page_slug(), // page.
'license' // section.
);
}
/**
* Adds a menu item Settings > Sample Plugin where users can enter and
* activate their license key.
*
* @return void
*/
public function add_settings_page() {
add_submenu_page(
'options-general.php', // parent slug.
__( 'Sample Plugin Settings', 'pmpro-pl-sample-plugin' ), // page <title>.
__( 'Sample Plugin', 'pmpro-pl-sample-plugin' ), // menu title.
'manage_options', // capability.
$this->admin_page_slug(), // menu slug.
array( $this, 'settings_page' ) // function to call.
);
}
protected function admin_page_slug() {
return 'pmpro_pl_sample_plugin';
}
/**
* Get the license key.
*
* @return string The license key.
*/
public static function get_license_key() {
return get_option( self::option_name() );
}
public function license_key_field() {
// Does the user have an active license key?
$license_key = self::get_license_key();
$activator = PMPRO_PL_Sample_Activator::get_instance();
$license_status = $activator->get_status( $license_key );
if ( empty( $license_key ) ) {
// No. Show input field.
echo '<input type="text" class="regular-text" name="pmpro_pl_sample_plugin_license_key" value="" />';
} else {
// Have a license. Show an obfuscated key and the license status.
// Stash the license key in a hidden field so it can be submitted.
printf(
'<input type="text" class="regular-text code" style="letter-spacing: 2px;" disabled="disabled" value="%s" />'
. ' <span class="license-status license-status-%s" title="%s">%s</span>'
. '<input type="hidden" name="pmpro_pl_sample_plugin_license_key" value="%s" />',
esc_html( $this->obfuscate_license_key( $license_key ) ),
esc_attr( $license_status ),
esc_attr( __( 'License Status', 'pmpro-pl-sample-plugin' ) ),
esc_html( ucwords( $license_status ?? '' ) ),
esc_html( $license_key )
);
}
// Is the license active? Show Deactivate button.
// If not active, show Delete License Key button. Could be expired.
echo '<div class="license-key-action-buttons"><p>';
if ( 'active' === $license_status ) {
// Show Deactivate button.
printf(
'<button type="submit" name="submit" value="deactivate-license" id="deactivate-license" class="button button-secondary">%s</button>',
esc_html__( 'Deactivate License', 'pmpro-pl-sample-plugin' )
);
} elseif ( ! empty( $license_key ) ) {
// Show Delete License Key button.
printf(
'<button type="submit" name="submit" value="delete-license-key" id="delete-license-key" class="button button-secondary">%s</button>',
esc_html__( 'Delete License Key', 'pmpro-pl-sample-plugin' )
);
}
if ( 'expired' === $license_status ) {
printf(
' <a href="%s">%s</a>',
esc_url( PMPRO_PL_Sample_Updater::get_store_url( PMPRO_PL_SAMPLE_PLUGIN_STORE_URL . '/membership-account/' ) ),
esc_html__( 'Renew', 'pmpro-pl-sample-plugin' )
);
}
echo '</p></div>';
}
protected function obfuscate_license_key( $license_key ) {
if ( ! empty( $license_key ) ) {
return str_replace(
substr( $license_key, 0, 28 ),
str_repeat( '*', 28 ),
$license_key
);
}
return '';
}
public static function option_name() {
return 'pmpro_pl_sample_plugin_license_key';
}
public function sanitize_license_key( $license_key ) {
$license_key = sanitize_text_field( $license_key );
// Only for this settings group submission.
if ( isset( $_POST['option_page'] ) && $_POST['option_page'] === 'pmpro_pl_sample_plugin' ) {
check_admin_referer( 'pmpro_pl_sample_plugin-options' );
$submit = isset( $_POST['submit'] ) ? sanitize_text_field( wp_unslash( $_POST['submit'] ) ) : '';
if ( $submit === 'deactivate-license' ) {
// Perform your deactivate call here.
PMPRO_PL_Sample_Activator::get_instance()->deactivate_license( $license_key );
return $license_key;
}
if ( $submit === 'delete-license-key' ) {
return '';
}
}
// User pressed Save Changes button.
$activator = PMPRO_PL_Sample_Activator::get_instance();
$license_status = $activator->activate_license( $license_key );
return $license_key;
}
/**
* settings_page
*
* @return void
*/
public function settings_page() {
printf(
'<div class="wrap"><h1>%s</h1>',
esc_html__( 'Plugin Licensing Sample Plugin', 'pmpro-pl-sample-plugin' )
);
echo '<form method="post" action="options.php">';
settings_fields( 'pmpro_pl_sample_plugin' );
do_settings_sections( 'pmpro_pl_sample_plugin' );
submit_button();
echo '</form></div>';
}
public function settings_page_section_license() {
// TODO This message should change based on the license status.
printf(
'<p>%s</p>',
esc_html__( 'Enter your license key and press the Save Changes button to activate this site. Only sites with a valid license key will receive updates.', 'pmpro-pl-sample-plugin' )
);
}
}
new PMPRO_PL_Sample_Plugin();