PluginProbe ʕ •ᴥ•ʔ
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress / 4.0.3
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress v4.0.3
4.17.3 4.17.2 4.17.1 4.17.0 4.16.19 4.16.18 4.16.17 4.16.16 trunk 1.0 1.0.1 1.0.2 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5a 1.1.6 1.1.7 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4 1.4.1 1.4.2 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.7 1.7.1 1.7.2 1.8 1.8.1 1.8.10 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.1.9 2.2.10 2.2.11 2.2.12 2.2.13 2.2.14 2.2.15 2.2.16 2.2.2 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 3.0 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.14 3.2.15 3.2.16 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.10.0 4.10.1 4.10.2 4.10.3 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.13.3 4.13.4 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.15.0 4.15.1 4.15.10 4.15.11 4.15.12 4.15.13 4.15.14 4.15.15 4.15.16 4.15.17 4.15.18 4.15.19 4.15.2 4.15.20 4.15.20.1 4.15.21 4.15.22 4.15.23 4.15.24 4.15.25 4.15.3 4.15.4 4.15.5 4.15.6 4.15.7 4.15.8 4.15.9 4.16.0 4.16.1 4.16.10 4.16.11 4.16.12 4.16.13 4.16.14 4.16.15 4.16.2 4.16.3 4.16.4 4.16.5 4.16.6 4.16.7 4.16.8 4.16.9 4.2.0 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.5.3 4.5.4 4.5.5 4.6.0 4.7.0 4.8.0 4.9.0
wp-user-avatar / src / Admin / SettingsPages / Membership / CustomersPage / SettingsPage.php
wp-user-avatar / src / Admin / SettingsPages / Membership / CustomersPage Last commit date
CustomerWPListTable.php 4 years ago SettingsPage.php 4 years ago index.php 4 years ago
SettingsPage.php
446 lines
1 <?php
2
3 namespace ProfilePress\Core\Admin\SettingsPages\Membership\CustomersPage;
4
5 use ProfilePress\Core\Admin\SettingsPages\AbstractSettingsPage;
6 use ProfilePress\Core\Admin\SettingsPages\Membership\ContextualStateChangeHelper;
7 use ProfilePress\Core\Membership\CheckoutFields;
8 use ProfilePress\Core\Membership\Models\Customer\CustomerEntity;
9 use ProfilePress\Core\Membership\Models\Customer\CustomerFactory;
10 use ProfilePress\Custom_Settings_Page_Api;
11
12 class SettingsPage extends AbstractSettingsPage
13 {
14 public $error_bucket;
15
16 /** @var CustomerWPListTable */
17 private $customerListTable;
18
19 function __construct()
20 {
21 add_action('ppress_register_menu_page', [$this, 'register_cpf_settings_page']);
22 add_action('ppress_admin_settings_page_customers', [$this, 'settings_page_function']);
23
24 add_action('wp_ajax_ppress_mb_search_wp_users', [$this, 'search_wp_users']);
25
26 if (ppressGET_var('page') == PPRESS_MEMBERSHIP_CUSTOMERS_SETTINGS_SLUG) {
27
28 add_filter('set-screen-option', [__CLASS__, 'set_screen'], 10, 3);
29 add_filter('set_screen_option_rules_per_page', [__CLASS__, 'set_screen'], 10, 3);
30
31 add_action('admin_init', function () {
32 $this->save_customer();
33 $this->add_customer();
34 });
35 }
36 }
37
38 public function default_header_menu()
39 {
40 return 'customers';
41 }
42
43 public function register_cpf_settings_page()
44 {
45 global $ppress_customer_page;
46
47 $hook = $ppress_customer_page = add_submenu_page(
48 PPRESS_DASHBOARD_SETTINGS_SLUG,
49 $this->admin_page_title() . ' - ProfilePress',
50 esc_html__('Customers', 'wp-user-avatar'),
51 'manage_options',
52 PPRESS_MEMBERSHIP_CUSTOMERS_SETTINGS_SLUG,
53 array($this, 'admin_page_callback')
54 );
55
56 add_action("load-$hook", array($this, 'add_options'));
57
58 do_action('ppress_membership_customers_settings_page_register', $hook);
59 }
60
61 /**
62 * @return void|string
63 * @throws \Exception
64 */
65 public function save_customer()
66 {
67 if ( ! isset($_POST['ppress_save_customer'])) return;
68
69 ppress_verify_nonce();
70
71 if ( ! current_user_can('manage_options')) return;
72
73 $user_id = absint($_POST['customer_wp_user']);
74
75 $customer_id = absint($_GET['id']);
76 $customer = CustomerFactory::fromId($customer_id);
77 $customer->user_id = $user_id;
78 $customer->private_note = sanitize_textarea_field(stripslashes($_POST['private_note']));
79 $customer->save();
80
81 $billing_fields = array_keys(CheckoutFields::billing_fields());
82
83 foreach ($_POST as $key => $value) {
84 if (in_array($key, $billing_fields)) {
85 update_user_meta($user_id, $key, sanitize_textarea_field($value));
86 }
87 }
88
89 do_action('ppress_customer_updated', $customer_id);
90
91 wp_safe_redirect(add_query_arg(['ppress_customer_action' => 'view', 'id' => $customer_id, 'saved' => 'true'], PPRESS_MEMBERSHIP_CUSTOMERS_SETTINGS_PAGE));
92 exit;
93 }
94
95 /**
96 * @return void|string
97 * @throws \Exception
98 */
99 public function add_customer()
100 {
101 if ( ! isset($_POST['save_ppress_customers'])) return;
102
103 if ( ! current_user_can( 'create_users' ) ) {
104 wp_die( __( 'You do not have permission to perform this action.', 'wp-user-avatar' ), __( 'Error', 'wp-user-avatar' ), array( 'response' => 403 ) );
105 }
106
107 check_admin_referer('wp-csa-nonce', 'wp_csa_nonce');
108
109 $type = ! empty($_POST['user_account_type']) ? $_POST['user_account_type'] : 'new';
110
111 $customer_data = ppressPOST_var('ppress_customers', [], true);
112
113 $customer_email = ppress_var($customer_data, 'email', '');
114 $customer_user_id = ppress_var($customer_data, 'search_user', '');
115
116 if ($type == 'new' && empty($customer_email)) {
117 wp_die(__('Please enter a valid customer email.', 'wp-user-avatar'), __('Error', 'wp-user-avatar'), array('response' => 400));
118 }
119
120 if ('new' == $type) {
121
122 $user_login = ! empty($_POST['username']) ? $_POST['username'] : $customer_email;
123
124 $existing_user = get_user_by('login', $user_login);
125
126 if ($existing_user && $existing_user->exists()) {
127 wp_die(sprintf(__('A user account already exists with the login %s.', 'wp-user-avatar'), esc_html($user_login)), __('Error', 'wp-user-avatar'), array('response' => 500));
128 }
129
130 $user_args = array(
131 'user_login' => sanitize_text_field($user_login),
132 'user_email' => sanitize_text_field($customer_email),
133 'user_pass' => ! empty($_POST['pass1']) ? $_POST['pass1'] : wp_generate_password(24),
134 'first_name' => ! empty($customer_data['first_name']) ? sanitize_text_field($customer_data['first_name']) : '',
135 'last_name' => ! empty($customer_data['last_name']) ? sanitize_text_field($customer_data['last_name']) : ''
136 );
137
138 $user_args['display_name'] = trim($user_args['first_name'] . ' ' . $user_args['last_name']);
139
140 if (empty($user_args['display_name'])) {
141 $user_args['display_name'] = $user_args['user_login'];
142 }
143
144 $user_id = wp_insert_user($user_args);
145
146 if (empty($user_id)) {
147 wp_die(__('Error creating customer account.', 'wp-user-avatar'), __('Error', 'wp-user-avatar'), array('response' => 500));
148 }
149
150 $user = get_userdata($user_id);
151 } else {
152
153 $user = get_user_by('id', $customer_user_id);
154
155 if ( ! is_a($user, 'WP_User')) {
156 wp_die(sprintf(__('Unable to locate existing account with the email %s.', 'wp-user-avatar'), esc_html($customer_email)), __('Error', 'wp-user-avatar'), array('response' => 500));
157 }
158 }
159
160 $customer = CustomerFactory::fromUserId($user->ID);
161
162 if ($customer->exists()) {
163 wp_die(sprintf(__('A customer with the ID %d already exists with this account.', 'wp-user-avatar'), $customer->get_id()), __('Error', 'wp-user-avatar'), array('response' => 500));
164 }
165
166 $new_customer = new CustomerEntity();
167 $new_customer->user_id = $user->ID;
168 $customer_id = $new_customer->save();
169
170 if (empty($customer_id)) {
171 wp_die(__('Error creating customer record.', 'wp-user-avatar'), __('Error', 'wp-user-avatar'), array('response' => 500));
172 }
173
174 do_action('ppress_customer_updated', $customer_id);
175
176 wp_safe_redirect(esc_url_raw(CustomerWPListTable::view_customer_url($customer_id)));
177 exit;
178 }
179
180 public function search_wp_users()
181 {
182 if ( ! current_user_can('manage_options')) return;
183
184 check_ajax_referer('ppress-admin-nonce', 'nonce');
185
186 $search = sanitize_text_field($_GET['search']);
187
188 $results['results'] = [];
189
190 $users = get_users([
191 'search' => '*' . $search . '*',
192 'search_columns' => ['user_email', 'user_login', 'user_nicename', 'display_name'],
193 'fields' => ['ID', 'user_email', 'user_login'],
194 'number' => 1000
195 ]);
196
197 if (is_array($users) && ! empty($users)) {
198
199 foreach ($users as $user) {
200
201 $results['results'][$user->ID] = array(
202 'id' => $user->ID,
203 'text' => sprintf('%s (%s)', $user->user_login, $user->user_email),
204 );
205 }
206 }
207
208 $results['results'] = array_values($results['results']);
209
210 wp_send_json($results, 200);
211 }
212
213 public function admin_page_title()
214 {
215 $title = esc_html__('Customers', 'wp-user-avatar');
216
217 if (ppressGET_var('ppress_customer_action') == 'new') {
218 $title = esc_html__('Add New Customer', 'wp-user-avatar');
219 }
220
221 if (ppressGET_var('ppress_customer_action') == 'view') {
222 $title = esc_html__('Customer Details', 'wp-user-avatar');
223 }
224
225 return $title;
226 }
227
228 public static function set_screen($status, $option, $value)
229 {
230 return $value;
231 }
232
233 public function add_options()
234 {
235 $args = [
236 'label' => esc_html__('Customers', 'wp-user-avatar'),
237 'default' => 10,
238 'option' => 'customers_per_page'
239 ];
240
241 add_screen_option('per_page', $args);
242
243 $this->customerListTable = new CustomerWPListTable();
244 }
245
246 public function admin_notices()
247 {
248 if ( ! isset($_GET['saved']) && ! isset($this->error_bucket)) return;
249
250 $status = 'updated';
251 $message = '';
252
253 if ( ! empty($this->error_bucket)) {
254 $message = $this->error_bucket;
255 $status = 'error';
256 }
257
258 if (isset($_GET['saved'])) {
259 $message = esc_html__('Changes saved.', 'wp-user-avatar');
260 }
261
262 printf('<div id="message" class="%s notice is-dismissible"><p>%s</strong></p></div>', $status, $message);
263 }
264
265 public function settings_page_function()
266 {
267 add_action('admin_footer', [$this, 'js_script']);
268 add_action('wp_cspa_main_content_area', [$this, 'admin_settings_page_callback'], 10, 2);
269 add_action('wp_cspa_before_closing_header', [$this, 'add_new_button']);
270
271 add_action('wp_cspa_form_tag', function ($option_name) {
272 if ($option_name == 'ppress_customers') {
273 printf(' action="%s"', ppress_get_current_url_query_string());
274 }
275 });
276
277 $instance = Custom_Settings_Page_Api::instance();
278 if ( ! isset($_GET['ppress_customer_action'])) {
279 $instance->form_method('get');
280 $instance->remove_nonce_field();
281 }
282
283 if (ppressGET_var('ppress_customer_action') == 'new') {
284
285 $instance->main_content([
286 [
287 'account_type' => [
288 'label' => __('User Account', 'wp-user-avatar'),
289 'type' => 'custom_field_block',
290 'data' => self::user_account_type_settings()
291 ],
292 'search_user' => [
293 'label' => __('Search User', 'wp-user-avatar'),
294 'type' => 'select',
295 'options' => [],
296 'attributes' => ['class' => 'ppress-select2-field customer_wp_user']
297 ],
298 'first_name' => [
299 'label' => __('First Name', 'wp-user-avatar'),
300 'type' => 'text'
301 ],
302 'last_name' => [
303 'label' => __('Last Name', 'wp-user-avatar'),
304 'type' => 'text'
305 ],
306 'email' => [
307 'label' => __('Email Address', 'wp-user-avatar'),
308 'type' => 'text'
309 ],
310 'username' => [
311 'label' => __('Username', 'wp-user-avatar'),
312 'type' => 'text'
313 ],
314 'password' => [
315 'label' => __('Password', 'wp-user-avatar'),
316 'type' => 'custom_field_block',
317 'data' => self::password_field()
318 ]
319 ]
320 ]);
321
322 $instance->remove_white_design();
323
324 $instance->sidebar(self::sidebar_args());
325 }
326
327 $instance->add_view_classes('ppview');
328 $instance->option_name('ppress_customers');
329 $instance->page_header($this->admin_page_title());
330 $instance->build(ppressGET_var('ppress_customer_action') != 'new');
331 }
332
333
334 protected static function user_account_type_settings()
335 {
336 $html = sprintf(
337 '<label><input checked class="user-account-type" type="radio" name="user_account_type" value="new">%s</label>&nbsp;&nbsp;',
338 __('New Account', 'wp-user-avatar')
339 );
340
341 $html .= sprintf(
342 '<label><input class="user-account-type" type="radio" name="user_account_type" value="existing">%s</label>&nbsp;&nbsp;',
343 __('Existing Account', 'wp-user-avatar')
344 );
345
346 return $html;
347 }
348
349 protected static function password_field()
350 {
351 ob_start();
352 ?>
353 <div id="password" class="user-pass1-wrap">
354 <input class="hidden" value=" "/><!-- #24364 workaround -->
355 <button type="button" class="button wp-generate-pw hide-if-no-js" aria-expanded="false"><?php _e('Show password', 'wp-user-avatar'); ?></button>
356 <div class="wp-pwd hide-if-js">
357 <span class="password-input-wrapper">
358 <input style="width:25em!important;" type="password" name="pass1" id="pass1" class="regular-text" value="" autocomplete="new-password" data-pw="<?php echo esc_attr(wp_generate_password(24)); ?>"/>
359 </span>
360 <button type="button" class="button wp-hide-pw hide-if-no-js" data-toggle="0" aria-label="<?php esc_attr_e('Hide password', 'wp-user-avatar'); ?>">
361 <span class="dashicons dashicons-hidden" aria-hidden="true"></span>
362 <span class="text"><?php _e('Hide', 'wp-user-avatar'); ?></span>
363 </button>
364 <button type="button" class="button wp-cancel-pw hide-if-no-js" data-toggle="0">
365 <span class="dashicons dashicons-no" aria-hidden="true"></span>
366 <span class="text"><?php _e('Cancel', 'wp-user-avatar'); ?></span>
367 </button>
368 <div style="display:none" id="pass-strength-result" aria-live="polite"></div>
369 </div>
370 </div>
371 <?php
372 return ob_get_clean();
373 }
374
375 public function add_new_button()
376 {
377 if ( ! isset($_GET['ppress_customer_action'])) {
378 $url = esc_url_raw(add_query_arg('ppress_customer_action', 'new', PPRESS_MEMBERSHIP_CUSTOMERS_SETTINGS_PAGE));
379 echo "<a class=\"add-new-h2\" href=\"$url\">" . esc_html__('Add New', 'wp-user-avatar') . '</a>';
380 }
381 }
382
383 public function admin_settings_page_callback($content)
384 {
385 if (ppressGET_var('ppress_customer_action') == 'view') {
386 $this->admin_notices();
387 ContextualStateChangeHelper::init();
388 require_once dirname(dirname(__FILE__)) . '/views/customers/view-customer.php';
389
390 return;
391 }
392
393 if (ppressGET_var('ppress_customer_action') == 'new') return $content;
394
395 $this->customerListTable->prepare_items();
396
397 echo '<input type="hidden" name="page" value="' . PPRESS_MEMBERSHIP_CUSTOMERS_SETTINGS_SLUG . '" />';
398 echo '<input type="hidden" name="view" value="customers" />';
399 if (isset($_GET['status'])) {
400 echo '<input type="hidden" name="status" value="' . sanitize_text_field($_GET['status']) . '" />';
401 }
402 $this->customerListTable->views();
403 $this->customerListTable->filter_bar();
404 $this->customerListTable->display();
405
406 do_action('ppress_customer_wp_list_table_bottom');
407 }
408
409 public function js_script()
410 {
411 ?>
412 <script type="text/javascript">
413 jQuery(function ($) {
414 $("input.user-account-type").on("change", function () {
415 if ($("input[name=\'user_account_type\']:checked").val() === "new") {
416 $("#search_user_row").hide();
417 $("#first_name_row").show();
418 $("#last_name_row").show();
419 $("#email_row").show();
420 $("#username_row").show();
421 $("#password_row").show();
422 } else {
423 $("#search_user_row").show();
424 $("#first_name_row").hide();
425 $("#last_name_row").hide();
426 $("#email_row").hide();
427 $("#username_row").hide();
428 $("#password_row").hide();
429 }
430 }).change();
431 });
432 </script>
433 <?php
434 }
435
436 public static function get_instance()
437 {
438 static $instance = null;
439
440 if (is_null($instance)) {
441 $instance = new self();
442 }
443
444 return $instance;
445 }
446 }