PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.16.3
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.16.3
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
profile-builder / features / email-confirmation / class-email-confirmation.php

class-email-confirmation.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 3.16.3, at features/email-confirmation/class-email-confirmation.php

526 lines 25.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) ) exit;
4
5 /*
6 Code taken from: Custom List Table Example (plugin)
7 Author: Matt Van Andel
8 Author URI: http://www.mattvanandel.com
9 */
10
11 /*************************** LOAD THE BASE CLASS *******************************
12 *******************************************************************************
13 * The PB_WP_List_Table class isn't automatically available to plugins, so we need
14 * to check if it's available and load it if necessary.
15 */
16 if( !class_exists( 'PB_WP_List_Table' ) ){
17 require_once( WPPB_PLUGIN_DIR.'/assets/lib/class-list-table.php' );
18 }
19
20
21
22
23 /************************** CREATE A PACKAGE CLASS *****************************
24 *******************************************************************************
25 * Create a new list table package that extends the core PB_WP_List_Table class.
26 * PB_WP_List_Table contains most of the framework for generating the table, but we
27 * need to define and override some methods so that our data can be displayed
28 * exactly the way we need it to be.
29 *
30 * To display this example on a page, you will first need to instantiate the class,
31 * then call $yourInstance->prepare_items() to handle any data manipulation, then
32 * finally call $yourInstance->display() to render the table to the page.
33 *
34 */
35 class wpp_list_unfonfirmed_email_table extends PB_WP_List_Table {
36
37 /** ************************************************************************
38 * REQUIRED. Set up a constructor that references the parent constructor. We
39 * use the parent reference to set some default configs.
40 ***************************************************************************/
41 function __construct(){
42 global $status, $page;
43 global $wpdb;
44
45 //Set parent defaults
46 parent::__construct( array(
47 'singular' => 'user', //singular name of the listed records
48 'plural' => 'users', //plural name of the listed records
49 'ajax' => false //does this table support ajax?
50 ) );
51
52 }
53
54
55 /** ************************************************************************
56 * Recommended. This method is called when the parent class can't find a method
57 * specifically build for a given column. Generally, it's recommended to include
58 * one method for each column you want to render, keeping your package class
59 * neat and organized. For example, if the class needs to process a column
60 * named 'username', it would first see if a method named $this->column_title()
61 * exists - if it does, that method will be used. If it doesn't, this one will
62 * be used. Generally, you should try to use custom column methods as much as
63 * possible.
64 *
65 * Since we have defined a column_title() method later on, this method doesn't
66 * need to concern itself with any column with a name of 'username'. Instead, it
67 * needs to handle everything else.
68 *
69 * For more detailed insight into how columns are handled, take a look at
70 * PB_WP_List_Table::single_row_columns()
71 *
72 * @param array $item A singular item (one full row's worth of data)
73 * @param array $column_name The name/slug of the column to be processed
74 * @return string Text or HTML to be placed inside the column <td>
75 **************************************************************************/
76 function column_default($item, $column_name){
77 switch($column_name){
78 case 'email':
79 return $item[$column_name];
80 case 'registered':
81 return date_i18n( "Y-m-d G:i:s", wppb_add_gmt_offset( strtotime( $item[$column_name] ) ) );
82 case 'user-meta':
83 global $wpdb;
84 $sql_result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $wpdb->base_prefix . "signups WHERE user_email = %s", $item['email'] ), ARRAY_A );
85 $user_meta = $sql_result['meta'];
86 $user_meta_lines = array();
87 if ( ! empty( $user_meta ) ) {
88 foreach ( maybe_unserialize( $user_meta ) as $key => $value ) {
89 if ( 'user_pass' === $key ) {
90 continue;
91 }
92 if ( is_array( $value ) ) {
93 $value = implode( ',', $value );
94 }
95 $user_meta_lines[] = esc_html( (string) $key ) . ':' . esc_html( (string) $value );
96 }
97 }
98 $user_meta_html = '<div><pre>' . implode( "\n", $user_meta_lines ) . '</pre></div>';
99 $dialog_js = 'jQuery(\'' . esc_js( $user_meta_html ) . '\').dialog({title:\'' . esc_js( __( 'User Meta', 'profile-builder' ) ) . '\', width: 500 }); return false;';
100 return '<a href="#" data-email="' . esc_attr( $item['email'] ) . '" onclick="' . esc_attr( $dialog_js ) . '">' . esc_html__( 'show', 'profile-builder' ) . '</a>';
101 default:
102 return print_r($item,true); //Show the whole array for troubleshooting purposes
103 }
104 }
105
106
107 /** ************************************************************************
108 * Recommended. This is a custom column method and is responsible for what
109 * is rendered in any column with a name/slug of 'username'. Every time the class
110 * needs to render a column, it first looks for a method named
111 * column_{$column_title} - if it exists, that method is run. If it doesn't
112 * exist, column_default() is called instead.
113 *
114 * This example also illustrates how to implement rollover actions. Actions
115 * should be an associative array formatted as 'slug'=>'link html' - and you
116 * will need to generate the URLs yourself. You could even ensure the links
117 *
118 *
119 * @see PB_WP_List_Table::::single_row_columns()
120 * @param array $item A singular item (one full row's worth of data)
121 * @return string Text to be placed inside the column <td>
122 **************************************************************************/
123 function column_username($item){
124
125 $GRavatar = get_avatar( $item['email'], 32, '' );
126
127 //Build row actions
128 $actions = array(
129 'delete' => sprintf( '<a href="javascript:confirmECAction( \'%s\', \'%s\', \'%s\', \'' . addslashes( __( 'delete this user from the _signups table?', 'profile-builder' ) ) . '\' )">' . __( 'Delete', 'profile-builder' ) . '</a>', wppb_curpageurl(), 'delete', $item['ID'] ),
130 'confirm' => sprintf( '<a href="javascript:confirmECAction( \'%s\', \'%s\', \'%s\', \'' . addslashes( __( 'confirm this email yourself?', 'profile-builder' ) ) . '\' )">' . __( 'Confirm Email', 'profile-builder' ) . '</a>', wppb_curpageurl(), 'confirm', $item['ID'] ),
131 'resend' => sprintf( '<a href="javascript:confirmECAction( \'%s\', \'%s\', \'%s\', \'' . addslashes( __( 'resend the activation link?', 'profile-builder' ) ) . '\' )">' . __( 'Resend Activation Email', 'profile-builder' ) . '</a>', wppb_curpageurl(), 'resend', $item['ID'] )
132 );
133
134 //Return the user row
135 return sprintf('%1$s <strong>%2$s</strong> %3$s',
136 /*$1%s*/ $GRavatar,
137 /*$2%s*/ $item['username'],
138 /*$3%s*/ $this->row_actions($actions)
139 );
140 }
141
142 /** ************************************************************************
143 * REQUIRED if displaying checkboxes or using bulk actions! The 'cb' column
144 * is given special treatment when columns are processed. It ALWAYS needs to
145 * have it's own method.
146 *
147 * @see PB_WP_List_Table::::single_row_columns()
148 * @param array $item A singular item (one full row's worth of data)
149 * @return string Text to be placed inside the column <td>
150 **************************************************************************/
151 function column_cb($item){
152 return sprintf(
153 '<input type="checkbox" name="%1$s[]" value="%2$s" />',
154 /*$1%s*/ $this->_args['singular'], //Let's simply repurpose the table's singular label
155 /*$2%s*/ $item['ID'] //The value of the checkbox should be the record's id
156 );
157 }
158
159
160 /** ************************************************************************
161 * REQUIRED! This method dictates the table's columns and titles. This should
162 * return an array where the key is the column slug (and class) and the value
163 * is the column's title text. If you need a checkbox for bulk actions, refer
164 * to the $columns array below.
165 *
166 * The 'cb' column is treated differently than the rest. If including a checkbox
167 * column in your table you must create a column_cb() method. If you don't need
168 * bulk actions or checkboxes, simply leave the 'cb' entry out of your array.
169 *
170 * @see PB_WP_List_Table::::single_row_columns()
171 * @return array An associative array containing column information: 'slugs'=>'Visible Titles'
172 **************************************************************************/
173 function get_columns(){
174 $columns = array(
175 'cb' => '<input type="checkbox" />', //Render a checkbox instead of text
176 'username' => __( 'Username', 'profile-builder' ),
177 'email' => __( 'Email', 'profile-builder' ),
178 'registered' => __( 'Registered', 'profile-builder' ),
179 'user-meta' => __( 'User Meta', 'profile-builder' )
180 );
181
182 return $columns;
183 }
184
185 /** ************************************************************************
186 * Optional. If you want one or more columns to be sortable (ASC/DESC toggle),
187 * you will need to register it here. This should return an array where the
188 * key is the column that needs to be sortable, and the value is db column to
189 * sort by. Often, the key and value will be the same, but this is not always
190 * the case (as the value is a column name from the database, not the list table).
191 *
192 * This method merely defines which columns should be sortable and makes them
193 * clickable - it does not handle the actual sorting. You still need to detect
194 * the ORDERBY and ORDER querystring variables within prepare_items() and sort
195 * your data accordingly (usually by modifying your query).
196 *
197 * @return array An associative array containing all the columns that should be sortable: 'slugs'=>array('data_values',bool)
198 **************************************************************************/
199 function get_sortable_columns() {
200 $sortable_columns = array(
201 'username' => array('username',false), //true means it's already sorted
202 'email' => array('email',false),
203 'registered' => array('registered',false)
204 );
205
206 return $sortable_columns;
207 }
208
209
210 /** ************************************************************************
211 * Optional. If you need to include bulk actions in your list table, this is
212 * the place to define them. Bulk actions are an associative array in the format
213 * 'slug'=>'Visible Title'
214 *
215 * If this method returns an empty value, no bulk action will be rendered. If
216 * you specify any bulk actions, the bulk actions box will be rendered with
217 * the table automatically on display().
218 *
219 * Also note that list tables are not automatically wrapped in <form> elements,
220 * so you will need to create those manually in order for bulk actions to function.
221 *
222 * @return array An associative array containing all the bulk actions: 'slugs'=>'Visible Titles'
223 **************************************************************************/
224 function get_bulk_actions() {
225 $actions = array(
226 'delete' => __( 'Delete', 'profile-builder' ),
227 'confirm' => __( 'Confirm Email', 'profile-builder' ),
228 'resend' => __( 'Resend Activation Email', 'profile-builder' )
229 );
230
231 return $actions;
232 }
233
234
235 /** ************************************************************************
236 * Optional. You can handle your bulk actions anywhere or anyhow you prefer.
237 * For this example package, we will handle it in the class to keep things
238 * clean and organized.
239 *
240 * @see $this->prepare_items()
241 **************************************************************************/
242
243 function wppb_process_bulk_action_message( $message, $url ){
244
245 echo '<script type=\'text/javascript\'>confirmECActionBulk( "'.esc_js( $url ).'", "'.esc_js( $message ).'" )</script>';
246 }
247
248 function wppb_process_bulk_action() {
249 global $current_user;
250 global $wpdb;
251
252 // Only verify and process when a bulk action is actually being submitted through the list-table form.
253 if ( false === $this->current_action() )
254 return;
255
256 // CSRF protection: the bulk-action form rendered by display() already outputs a 'bulk-{plural}' nonce, verify it here.
257 check_admin_referer( 'bulk-' . $this->_args['plural'] );
258
259 if ( current_user_can( apply_filters( 'wppb_email_confirmation_user_capability', 'manage_options' ) ) ){
260 if( 'delete' === $this->current_action() ) {
261 if( !empty( $_GET['user'] ) && is_array( $_GET['user'] ) ) {
262 foreach ( array_map( 'sanitize_email', $_GET['user'] ) as $user) {
263 $sql_result = $wpdb->query($wpdb->prepare("DELETE FROM " . $wpdb->base_prefix . "signups WHERE user_email = %s", $user));
264
265 if (!$sql_result)
266 $this->wppb_process_bulk_action_message(sprintf(__("%s couldn't be deleted", "profile-builder"), $user), get_bloginfo('url') . '/wp-admin/users.php?page=unconfirmed_emails');
267
268 }
269 }
270
271 $this->wppb_process_bulk_action_message( __( 'All users have been successfully deleted', 'profile-builder' ), get_bloginfo('url').'/wp-admin/users.php?page=unconfirmed_emails' );
272
273 }elseif( 'confirm' === $this->current_action() ) {
274 if( !empty( $_GET['user'] ) && is_array( $_GET['user'] ) ) {
275 foreach ( array_map( 'sanitize_email', $_GET['user'] ) as $user) {
276 $sql_result = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . $wpdb->base_prefix . "signups WHERE user_email = %s", $user), ARRAY_A);
277
278 if ($sql_result)
279 wppb_manual_activate_signup($sql_result['activation_key']);
280 }
281 }
282
283 $this->wppb_process_bulk_action_message( __( 'The selected users have been activated', 'profile-builder' ), get_bloginfo('url').'/wp-admin/users.php?page=unconfirmed_emails' );
284
285 }elseif( 'resend' === $this->current_action() ) {
286 if( !empty( $_GET['user'] ) && is_array( $_GET['user'] ) ) {
287 foreach ( array_map( 'sanitize_email', $_GET['user'] ) as $user ) {
288 $sql_result = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . $wpdb->base_prefix . "signups WHERE user_email = %s", $user), ARRAY_A);
289
290 if ($sql_result)
291 wppb_signup_user_notification(esc_sql($sql_result['user_login']), esc_sql($sql_result['user_email']), $sql_result['activation_key'], $sql_result['meta']);
292
293 }
294 }
295
296 $this->wppb_process_bulk_action_message( __( 'The selected users have had their activation emails resent', 'profile-builder' ), get_bloginfo('url').'/wp-admin/users.php?page=unconfirmed_emails' );
297 }
298
299 }else
300 $this->wppb_process_bulk_action_message( __( "Sorry, but you don't have permission to do that!", "profile-builder" ), get_bloginfo('url').'/wp-admin/' );
301 }
302
303
304 /** ************************************************************************
305 * REQUIRED! This is where you prepare your data for display. This method will
306 * usually be used to query the database, sort and filter the data, and generally
307 * get it ready to be displayed. At a minimum, we should set $this->items and
308 * $this->set_pagination_args(), although the following properties and methods
309 * are frequently interacted with here...
310 *
311 * @global WPDB $wpdb
312 * @uses $this->_column_headers
313 * @uses $this->items
314 * @uses $this->get_columns()
315 * @uses $this->get_sortable_columns()
316 * @uses $this->get_pagenum()
317 * @uses $this->set_pagination_args()
318 **************************************************************************/
319 function prepare_items() {
320 global $wpdb;
321
322 $this->dataArray = array();
323
324 /**
325 * First, lets decide how many records per page to show
326 */
327 $per_page = get_user_meta( get_current_user_id(), 'users_per_page', true );
328 if( !empty($per_page) )
329 $per_page = apply_filters('wppb_email_confirmation_user_per_page_number', $per_page);
330 else
331 $per_page = apply_filters('wppb_email_confirmation_user_per_page_number', 20);
332
333 /* determine offset */
334 if( !empty( $_REQUEST['paged'] ) ){
335 $offset = ( sanitize_text_field( $_REQUEST['paged'] ) -1 ) * $per_page;
336 }
337 else
338 $offset = 0;
339
340 /* handle order and orderby attr */
341 if( !empty( $_REQUEST['orderby'] ) ){
342
343 $orderby = sanitize_sql_orderby( $_REQUEST['orderby'] );
344
345 if( $orderby == 'username' )
346 $orderby = 'user_login';
347 elseif ( $orderby == 'email' )
348 $orderby = 'user_email';
349
350 } else
351 $orderby = 'user_login';
352
353 if( !empty( $_REQUEST['order'] ) && sanitize_text_field( $_REQUEST['order'] ) === 'desc' )
354 $order = "DESC";
355 else
356 $order = 'ASC';
357
358 /* handle the WHERE clause */
359 $where = "active = 0";
360 if( isset( $_REQUEST['s'] ) && !empty( $_REQUEST['s'] ) ){
361 $where .= $wpdb->prepare( " AND ( user_login LIKE %s OR user_email LIKE %s OR registered LIKE %s )", '%' . sanitize_text_field( $_REQUEST['s'] ) . '%', '%' . sanitize_text_field( $_REQUEST['s'] ) . '%' , '%' . sanitize_text_field( $_REQUEST['s'] ) . '%' );
362 }
363 /* since version 2.0.7 for multisite we add a 'registered_for_blog_id' meta in the registration process
364 so we can display only the users registered on that blog. Also for backwards compatibility we display the users that don't have that meta at all */
365 if( is_multisite() ){
366 $where .= $wpdb->prepare( " AND ( meta NOT LIKE '%\"registered_for_blog_id\"%' OR meta LIKE '%s' )", '%\"registered_for_blog_id\";i:'.get_current_blog_id().'%' );
367 }
368
369 $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM ".$wpdb->base_prefix."signups WHERE $where ORDER BY $orderby $order LIMIT %d, %d", $offset, $per_page ) );
370
371 foreach ($results as $result){
372 $tempArray = array('ID' => $result->user_email, 'username' => $result->user_login, 'email' => $result->user_email, 'registered' => $result->registered);
373 array_push($this->dataArray, $tempArray);
374 }
375
376 /**
377 * REQUIRED for pagination. Let's check how many items are in our data array.
378 * In real-world use, this would be the total number of items in your database,
379 * without filtering. We'll need this later, so you should always include it
380 * in your own package classes.
381 */
382 $total_items = $wpdb->get_var( "SELECT COUNT(*) FROM ".$wpdb->base_prefix."signups WHERE $where" );
383
384 /**
385 * REQUIRED. Now we need to define our column headers. This includes a complete
386 * array of columns to be displayed (slugs & titles), a list of columns
387 * to keep hidden, and a list of columns that are sortable. Each of these
388 * can be defined in another method (as we've done here) before being
389 * used to build the value for our _column_headers property.
390 */
391 $columns = $this->get_columns();
392 $hidden = array();
393 $sortable = $this->get_sortable_columns();
394
395
396 /**
397 * REQUIRED. Finally, we build an array to be used by the class for column
398 * headers. The $this->_column_headers property takes an array which contains
399 * 3 other arrays. One for all columns, one for hidden columns, and one
400 * for sortable columns.
401 */
402 $this->_column_headers = array($columns, $hidden, $sortable);
403
404
405 /**
406 * Optional. You can handle your bulk actions however you see fit. In this
407 * case, we'll handle them within our package just to keep things clean.
408 */
409 $this->wppb_process_bulk_action();
410
411
412 /**
413 * Instead of querying a database, we're going to fetch the example data
414 * property we created for use in this plugin. This makes this example
415 * package slightly different than one you might build on your own. In
416 * this example, we'll be using array manipulation to sort and paginate
417 * our data. In a real-world implementation, you will probably want to
418 * use sort and pagination data to build a custom query instead, as you'll
419 * be able to use your precisely-queried data immediately.
420 */
421 $data = $this->dataArray;
422
423 /**
424 * REQUIRED. Now we can add our *sorted* data to the items property, where
425 * it can be used by the rest of the class.
426 */
427 $this->items = $data;
428
429
430 /**
431 * REQUIRED. We also have to register our pagination options & calculations.
432 */
433 $this->set_pagination_args( array(
434 'total_items' => $total_items, //WE have to calculate the total number of items
435 'per_page' => $per_page, //WE have to determine how many items to show on a page
436 'total_pages' => ceil($total_items/$per_page) //WE have to calculate the total number of pages
437 ) );
438 }
439
440 }
441
442
443
444
445
446 /** ************************ REGISTER THE PAGE ****************************
447 *******************************************************************************
448 * Now we just need to define an admin page.
449 */
450 function wppb_add_ec_submenu_page() {
451 $wppb_generalSettings = get_option('wppb_general_settings', 'not_found');
452 if($wppb_generalSettings != 'not_found') {
453 if( !empty($wppb_generalSettings['emailConfirmation']) && ($wppb_generalSettings['emailConfirmation'] == 'yes') ){
454 $hook = add_submenu_page('users.php', 'Unconfirmed Email Address', 'Unconfirmed Email Address', 'manage_options', 'unconfirmed_emails', 'wppb_unconfirmed_email_address_custom_menu_page');
455 add_action( "load-$hook", 'wppb_ec_screen_options' ); //add screen options to Unconfirmed Email Users page
456 //remove_submenu_page('users.php', 'unconfirmed_emails'); //hide the page in the admin menu
457 }
458 }
459 }
460 add_action('admin_menu', 'wppb_add_ec_submenu_page');
461
462
463
464 /***************************** RENDER PAGE ********************************
465 *******************************************************************************
466 * This function renders the admin page. Although it's
467 * possible to call prepare_items() and display() from the constructor, there
468 * are often times where you may need to include logic here between those steps,
469 * so we've instead called those methods explicitly. It keeps things flexible, and
470 * it's the way the list tables are used in the WordPress core.
471 */
472 function wppb_unconfirmed_email_address_custom_menu_page(){
473
474 //Create an instance of our package class...
475 $listTable = new wpp_list_unfonfirmed_email_table();
476 //Fetch, prepare, sort, and filter our data...
477 $listTable->prepare_items();
478
479 ?>
480 <div class="wrap">
481
482 <div class="wrap"><div id="icon-users" class="icon32"></div><h2><?php esc_html_e('Users with Unconfirmed Email Address', 'profile-builder');?></h2></div>
483
484 <ul class="subsubsub">
485 <li class="all"><a href="users.php"><?php esc_html_e('All Users', 'profile-builder');?></a></li>
486 </ul>
487
488 <!-- Forms are NOT created automatically, so you need to wrap the table in one to use features like bulk actions -->
489 <form id="movies-filter" method="get">
490 <!-- For plugins, we also need to ensure that the form posts back to our current page -->
491 <?php $listTable->search_box( __( 'Search Users', 'profile-builder' ), 'user' ); ?>
492 <input type="hidden" name="page" value="<?php echo isset( $_REQUEST['page'] ) ? esc_attr( sanitize_text_field( $_REQUEST['page'] ) ) : ''; ?>" />
493 <!-- Now we can render the completed list table -->
494 <?php $listTable->display() ?>
495 </form>
496
497 </div>
498 <?php
499 }
500
501 /**
502 * Function that handles the screen options on Unconfirmed Email Users page
503 */
504 function wppb_ec_screen_options(){
505 $option = 'per_page';
506
507 $args = array(
508 'label' => __( 'Number of items per page:' , 'profile-builder' ),//use the default wordpress domain
509 'default' => 20,
510 'option' => 'users_per_page' //this is the same option as the one on the normal User screen
511 );
512
513 add_screen_option( $option, $args );
514 }
515
516 /**
517 * Fix the page title of the admin page, since using remove_sumbenu_page() breaks the page title
518 */
519 add_filter('admin_title', 'wppb_ec_admin_title', 10 );
520 function wppb_ec_admin_title($admin_title){
521 if( isset( $_GET['page'] ) && sanitize_text_field( $_GET['page'] ) === 'unconfirmed_emails' ) {
522 $admin_title = __('Users with Unconfirmed Email Address', 'profile-builder') . $admin_title;
523 }
524
525 return $admin_title;
526 }