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

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