PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.16.0
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.16.0
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.0, at features/email-confirmation/class-email-confirmation.php

519 lines 25.0 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 if ( current_user_can( apply_filters( 'wppb_email_confirmation_user_capability', 'manage_options' ) ) ){
253 if( 'delete' === $this->current_action() ) {
254 if( !empty( $_GET['user'] ) && is_array( $_GET['user'] ) ) {
255 foreach ( array_map( 'sanitize_email', $_GET['user'] ) as $user) {
256 $sql_result = $wpdb->query($wpdb->prepare("DELETE FROM " . $wpdb->base_prefix . "signups WHERE user_email = %s", $user));
257
258 if (!$sql_result)
259 $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');
260
261 }
262 }
263
264 $this->wppb_process_bulk_action_message( __( 'All users have been successfully deleted', 'profile-builder' ), get_bloginfo('url').'/wp-admin/users.php?page=unconfirmed_emails' );
265
266 }elseif( 'confirm' === $this->current_action() ) {
267 if( !empty( $_GET['user'] ) && is_array( $_GET['user'] ) ) {
268 foreach ( array_map( 'sanitize_email', $_GET['user'] ) as $user) {
269 $sql_result = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . $wpdb->base_prefix . "signups WHERE user_email = %s", $user), ARRAY_A);
270
271 if ($sql_result)
272 wppb_manual_activate_signup($sql_result['activation_key']);
273 }
274 }
275
276 $this->wppb_process_bulk_action_message( __( 'The selected users have been activated', 'profile-builder' ), get_bloginfo('url').'/wp-admin/users.php?page=unconfirmed_emails' );
277
278 }elseif( 'resend' === $this->current_action() ) {
279 if( !empty( $_GET['user'] ) && is_array( $_GET['user'] ) ) {
280 foreach ( array_map( 'sanitize_email', $_GET['user'] ) as $user ) {
281 $sql_result = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . $wpdb->base_prefix . "signups WHERE user_email = %s", $user), ARRAY_A);
282
283 if ($sql_result)
284 wppb_signup_user_notification(esc_sql($sql_result['user_login']), esc_sql($sql_result['user_email']), $sql_result['activation_key'], $sql_result['meta']);
285
286 }
287 }
288
289 $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' );
290 }
291
292 }else
293 $this->wppb_process_bulk_action_message( __( "Sorry, but you don't have permission to do that!", "profile-builder" ), get_bloginfo('url').'/wp-admin/' );
294 }
295
296
297 /** ************************************************************************
298 * REQUIRED! This is where you prepare your data for display. This method will
299 * usually be used to query the database, sort and filter the data, and generally
300 * get it ready to be displayed. At a minimum, we should set $this->items and
301 * $this->set_pagination_args(), although the following properties and methods
302 * are frequently interacted with here...
303 *
304 * @global WPDB $wpdb
305 * @uses $this->_column_headers
306 * @uses $this->items
307 * @uses $this->get_columns()
308 * @uses $this->get_sortable_columns()
309 * @uses $this->get_pagenum()
310 * @uses $this->set_pagination_args()
311 **************************************************************************/
312 function prepare_items() {
313 global $wpdb;
314
315 $this->dataArray = array();
316
317 /**
318 * First, lets decide how many records per page to show
319 */
320 $per_page = get_user_meta( get_current_user_id(), 'users_per_page', true );
321 if( !empty($per_page) )
322 $per_page = apply_filters('wppb_email_confirmation_user_per_page_number', $per_page);
323 else
324 $per_page = apply_filters('wppb_email_confirmation_user_per_page_number', 20);
325
326 /* determine offset */
327 if( !empty( $_REQUEST['paged'] ) ){
328 $offset = ( sanitize_text_field( $_REQUEST['paged'] ) -1 ) * $per_page;
329 }
330 else
331 $offset = 0;
332
333 /* handle order and orderby attr */
334 if( !empty( $_REQUEST['orderby'] ) ){
335
336 $orderby = sanitize_sql_orderby( $_REQUEST['orderby'] );
337
338 if( $orderby == 'username' )
339 $orderby = 'user_login';
340 elseif ( $orderby == 'email' )
341 $orderby = 'user_email';
342
343 } else
344 $orderby = 'user_login';
345
346 if( !empty( $_REQUEST['order'] ) && sanitize_text_field( $_REQUEST['order'] ) === 'desc' )
347 $order = "DESC";
348 else
349 $order = 'ASC';
350
351 /* handle the WHERE clause */
352 $where = "active = 0";
353 if( isset( $_REQUEST['s'] ) && !empty( $_REQUEST['s'] ) ){
354 $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'] ) . '%' );
355 }
356 /* since version 2.0.7 for multisite we add a 'registered_for_blog_id' meta in the registration process
357 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 */
358 if( is_multisite() ){
359 $where .= $wpdb->prepare( " AND ( meta NOT LIKE '%\"registered_for_blog_id\"%' OR meta LIKE '%s' )", '%\"registered_for_blog_id\";i:'.get_current_blog_id().'%' );
360 }
361
362 $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM ".$wpdb->base_prefix."signups WHERE $where ORDER BY $orderby $order LIMIT %d, %d", $offset, $per_page ) );
363
364 foreach ($results as $result){
365 $tempArray = array('ID' => $result->user_email, 'username' => $result->user_login, 'email' => $result->user_email, 'registered' => $result->registered);
366 array_push($this->dataArray, $tempArray);
367 }
368
369 /**
370 * REQUIRED for pagination. Let's check how many items are in our data array.
371 * In real-world use, this would be the total number of items in your database,
372 * without filtering. We'll need this later, so you should always include it
373 * in your own package classes.
374 */
375 $total_items = $wpdb->get_var( "SELECT COUNT(*) FROM ".$wpdb->base_prefix."signups WHERE $where" );
376
377 /**
378 * REQUIRED. Now we need to define our column headers. This includes a complete
379 * array of columns to be displayed (slugs & titles), a list of columns
380 * to keep hidden, and a list of columns that are sortable. Each of these
381 * can be defined in another method (as we've done here) before being
382 * used to build the value for our _column_headers property.
383 */
384 $columns = $this->get_columns();
385 $hidden = array();
386 $sortable = $this->get_sortable_columns();
387
388
389 /**
390 * REQUIRED. Finally, we build an array to be used by the class for column
391 * headers. The $this->_column_headers property takes an array which contains
392 * 3 other arrays. One for all columns, one for hidden columns, and one
393 * for sortable columns.
394 */
395 $this->_column_headers = array($columns, $hidden, $sortable);
396
397
398 /**
399 * Optional. You can handle your bulk actions however you see fit. In this
400 * case, we'll handle them within our package just to keep things clean.
401 */
402 $this->wppb_process_bulk_action();
403
404
405 /**
406 * Instead of querying a database, we're going to fetch the example data
407 * property we created for use in this plugin. This makes this example
408 * package slightly different than one you might build on your own. In
409 * this example, we'll be using array manipulation to sort and paginate
410 * our data. In a real-world implementation, you will probably want to
411 * use sort and pagination data to build a custom query instead, as you'll
412 * be able to use your precisely-queried data immediately.
413 */
414 $data = $this->dataArray;
415
416 /**
417 * REQUIRED. Now we can add our *sorted* data to the items property, where
418 * it can be used by the rest of the class.
419 */
420 $this->items = $data;
421
422
423 /**
424 * REQUIRED. We also have to register our pagination options & calculations.
425 */
426 $this->set_pagination_args( array(
427 'total_items' => $total_items, //WE have to calculate the total number of items
428 'per_page' => $per_page, //WE have to determine how many items to show on a page
429 'total_pages' => ceil($total_items/$per_page) //WE have to calculate the total number of pages
430 ) );
431 }
432
433 }
434
435
436
437
438
439 /** ************************ REGISTER THE PAGE ****************************
440 *******************************************************************************
441 * Now we just need to define an admin page.
442 */
443 function wppb_add_ec_submenu_page() {
444 $wppb_generalSettings = get_option('wppb_general_settings', 'not_found');
445 if($wppb_generalSettings != 'not_found') {
446 if( !empty($wppb_generalSettings['emailConfirmation']) && ($wppb_generalSettings['emailConfirmation'] == 'yes') ){
447 $hook = add_submenu_page('users.php', 'Unconfirmed Email Address', 'Unconfirmed Email Address', 'manage_options', 'unconfirmed_emails', 'wppb_unconfirmed_email_address_custom_menu_page');
448 add_action( "load-$hook", 'wppb_ec_screen_options' ); //add screen options to Unconfirmed Email Users page
449 //remove_submenu_page('users.php', 'unconfirmed_emails'); //hide the page in the admin menu
450 }
451 }
452 }
453 add_action('admin_menu', 'wppb_add_ec_submenu_page');
454
455
456
457 /***************************** RENDER PAGE ********************************
458 *******************************************************************************
459 * This function renders the admin page. Although it's
460 * possible to call prepare_items() and display() from the constructor, there
461 * are often times where you may need to include logic here between those steps,
462 * so we've instead called those methods explicitly. It keeps things flexible, and
463 * it's the way the list tables are used in the WordPress core.
464 */
465 function wppb_unconfirmed_email_address_custom_menu_page(){
466
467 //Create an instance of our package class...
468 $listTable = new wpp_list_unfonfirmed_email_table();
469 //Fetch, prepare, sort, and filter our data...
470 $listTable->prepare_items();
471
472 ?>
473 <div class="wrap">
474
475 <div class="wrap"><div id="icon-users" class="icon32"></div><h2><?php esc_html_e('Users with Unconfirmed Email Address', 'profile-builder');?></h2></div>
476
477 <ul class="subsubsub">
478 <li class="all"><a href="users.php"><?php esc_html_e('All Users', 'profile-builder');?></a></li>
479 </ul>
480
481 <!-- Forms are NOT created automatically, so you need to wrap the table in one to use features like bulk actions -->
482 <form id="movies-filter" method="get">
483 <!-- For plugins, we also need to ensure that the form posts back to our current page -->
484 <?php $listTable->search_box( __( 'Search Users', 'profile-builder' ), 'user' ); ?>
485 <input type="hidden" name="page" value="<?php echo isset( $_REQUEST['page'] ) ? esc_attr( sanitize_text_field( $_REQUEST['page'] ) ) : ''; ?>" />
486 <!-- Now we can render the completed list table -->
487 <?php $listTable->display() ?>
488 </form>
489
490 </div>
491 <?php
492 }
493
494 /**
495 * Function that handles the screen options on Unconfirmed Email Users page
496 */
497 function wppb_ec_screen_options(){
498 $option = 'per_page';
499
500 $args = array(
501 'label' => __( 'Number of items per page:' , 'profile-builder' ),//use the default wordpress domain
502 'default' => 20,
503 'option' => 'users_per_page' //this is the same option as the one on the normal User screen
504 );
505
506 add_screen_option( $option, $args );
507 }
508
509 /**
510 * Fix the page title of the admin page, since using remove_sumbenu_page() breaks the page title
511 */
512 add_filter('admin_title', 'wppb_ec_admin_title', 10 );
513 function wppb_ec_admin_title($admin_title){
514 if( isset( $_GET['page'] ) && sanitize_text_field( $_GET['page'] ) === 'unconfirmed_emails' ) {
515 $admin_title = __('Users with Unconfirmed Email Address', 'profile-builder') . $admin_title;
516 }
517
518 return $admin_title;
519 }