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

461 lines 21.4 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.'/features/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 case 'registered':
77 return $item[$column_name];
78 default:
79 return print_r($item,true); //Show the whole array for troubleshooting purposes
80 }
81 }
82
83
84 /** ************************************************************************
85 * Recommended. This is a custom column method and is responsible for what
86 * is rendered in any column with a name/slug of 'username'. Every time the class
87 * needs to render a column, it first looks for a method named
88 * column_{$column_title} - if it exists, that method is run. If it doesn't
89 * exist, column_default() is called instead.
90 *
91 * This example also illustrates how to implement rollover actions. Actions
92 * should be an associative array formatted as 'slug'=>'link html' - and you
93 * will need to generate the URLs yourself. You could even ensure the links
94 *
95 *
96 * @see PB_WP_List_Table::::single_row_columns()
97 * @param array $item A singular item (one full row's worth of data)
98 * @return string Text to be placed inside the column <td>
99 **************************************************************************/
100 function column_username($item){
101
102 $GRavatar = get_avatar( $item['email'], 32, '' );
103
104 //Build row actions
105 $actions = array(
106 'delete' => sprintf( '<a href="javascript:confirmECAction( \'%s\', \'%s\', \'%s\', \'' . __( 'delete this user from the _signups table?', 'profilebuilder' ) . '\' )">' . __( 'Delete', 'profilebuilder' ) . '</a>', wppb_curpageurl(), 'delete', $item['ID'] ),
107 'confirm' => sprintf( '<a href="javascript:confirmECAction( \'%s\', \'%s\', \'%s\', \'' . __( 'confirm this email yourself?', 'profilebuilder' ) . '\' )">' . __( 'Confirm Email', 'profilebuilder' ) . '</a>', wppb_curpageurl(), 'confirm', $item['ID'] ),
108 'resend' => sprintf( '<a href="javascript:confirmECAction( \'%s\', \'%s\', \'%s\', \'' . __( 'resend the activation link?', 'profilebuilder' ) . '\' )">' . __( 'Resend Activation Email', 'profilebuilder' ) . '</a>', wppb_curpageurl(), 'resend', $item['ID'] )
109 );
110
111 //Return the user row
112 return sprintf('%1$s <strong>%2$s</strong> %3$s',
113 /*$1%s*/ $GRavatar,
114 /*$2%s*/ $item['username'],
115 /*$3%s*/ $this->row_actions($actions)
116 );
117 }
118
119 /** ************************************************************************
120 * REQUIRED if displaying checkboxes or using bulk actions! The 'cb' column
121 * is given special treatment when columns are processed. It ALWAYS needs to
122 * have it's own method.
123 *
124 * @see PB_WP_List_Table::::single_row_columns()
125 * @param array $item A singular item (one full row's worth of data)
126 * @return string Text to be placed inside the column <td>
127 **************************************************************************/
128 function column_cb($item){
129 return sprintf(
130 '<input type="checkbox" name="%1$s[]" value="%2$s" />',
131 /*$1%s*/ $this->_args['singular'], //Let's simply repurpose the table's singular label
132 /*$2%s*/ $item['ID'] //The value of the checkbox should be the record's id
133 );
134 }
135
136
137 /** ************************************************************************
138 * REQUIRED! This method dictates the table's columns and titles. This should
139 * return an array where the key is the column slug (and class) and the value
140 * is the column's title text. If you need a checkbox for bulk actions, refer
141 * to the $columns array below.
142 *
143 * The 'cb' column is treated differently than the rest. If including a checkbox
144 * column in your table you must create a column_cb() method. If you don't need
145 * bulk actions or checkboxes, simply leave the 'cb' entry out of your array.
146 *
147 * @see PB_WP_List_Table::::single_row_columns()
148 * @return array An associative array containing column information: 'slugs'=>'Visible Titles'
149 **************************************************************************/
150 function get_columns(){
151 $columns = array(
152 'cb' => '<input type="checkbox" />', //Render a checkbox instead of text
153 'username' => __( 'Username', 'profilebuilder' ),
154 'email' => __( 'E-mail', 'profilebuilder' ),
155 'registered' => __( 'Registered', 'profilebuilder' )
156 );
157
158 return $columns;
159 }
160
161 /** ************************************************************************
162 * Optional. If you want one or more columns to be sortable (ASC/DESC toggle),
163 * you will need to register it here. This should return an array where the
164 * key is the column that needs to be sortable, and the value is db column to
165 * sort by. Often, the key and value will be the same, but this is not always
166 * the case (as the value is a column name from the database, not the list table).
167 *
168 * This method merely defines which columns should be sortable and makes them
169 * clickable - it does not handle the actual sorting. You still need to detect
170 * the ORDERBY and ORDER querystring variables within prepare_items() and sort
171 * your data accordingly (usually by modifying your query).
172 *
173 * @return array An associative array containing all the columns that should be sortable: 'slugs'=>array('data_values',bool)
174 **************************************************************************/
175 function get_sortable_columns() {
176 $sortable_columns = array(
177 'username' => array('username',false), //true means it's already sorted
178 'email' => array('email',false),
179 'registered' => array('registered',false)
180 );
181
182 return $sortable_columns;
183 }
184
185
186 /** ************************************************************************
187 * Optional. If you need to include bulk actions in your list table, this is
188 * the place to define them. Bulk actions are an associative array in the format
189 * 'slug'=>'Visible Title'
190 *
191 * If this method returns an empty value, no bulk action will be rendered. If
192 * you specify any bulk actions, the bulk actions box will be rendered with
193 * the table automatically on display().
194 *
195 * Also note that list tables are not automatically wrapped in <form> elements,
196 * so you will need to create those manually in order for bulk actions to function.
197 *
198 * @return array An associative array containing all the bulk actions: 'slugs'=>'Visible Titles'
199 **************************************************************************/
200 function get_bulk_actions() {
201 $actions = array(
202 'delete' => __( 'Delete', 'profilebuilder' ),
203 'confirm' => __( 'Confirm Email', 'profilebuilder' ),
204 'resend' => __( 'Resend Activation Email', 'profilebuilder' )
205 );
206
207 return $actions;
208 }
209
210
211 /** ************************************************************************
212 * Optional. You can handle your bulk actions anywhere or anyhow you prefer.
213 * For this example package, we will handle it in the class to keep things
214 * clean and organized.
215 *
216 * @see $this->prepare_items()
217 **************************************************************************/
218
219 function wppb_process_bulk_action_message( $message, $url ){
220
221 echo "<script type=\"text/javascript\">confirmECActionBulk( '".$url."', '".$message."' )</script>";
222 }
223
224 function wppb_process_bulk_action() {
225 global $current_user;
226 global $wpdb;
227
228 if ( current_user_can( 'delete_users' ) ){
229 if( 'delete' === $this->current_action() ) {
230 foreach ( $_GET['user'] as $user ){
231 $sql_result = $wpdb->query( $wpdb->prepare( "DELETE FROM ".$wpdb->prefix."signups WHERE user_email = %s", $user ) );
232
233 if ( !$sql_result )
234 $this->wppb_process_bulk_action_message( sprintf( __( "%s couldn't be deleted", "profilebuilder" ), $result->user_login ), get_bloginfo('url').'/wp-admin/users.php?page=unconfirmed_emails' );
235
236 }
237
238 $this->wppb_process_bulk_action_message( __( 'All users have been successfully deleted', 'profilebuilder' ), get_bloginfo('url').'/wp-admin/users.php?page=unconfirmed_emails' );
239
240 }elseif( 'confirm' === $this->current_action() ) {
241 foreach ( $_GET['user'] as $user ){
242 $sql_result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $wpdb->prefix . "signups WHERE user_email = %s", $user ), ARRAY_A );
243
244 if ( $sql_result )
245 wppb_manual_activate_signup( $sql_result->activation_key );
246 }
247
248 $this->wppb_process_bulk_action_message( __( 'The selected users have been activated', 'profilebuilder' ), get_bloginfo('url').'/wp-admin/users.php?page=unconfirmed_emails' );
249
250 }elseif( 'resend' === $this->current_action() ) {
251 foreach ( $_GET['user'] as $user ){
252 $sql_result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $wpdb->prefix . "signups WHERE user_email = %s", $user ), ARRAY_A );
253
254 if ( $sql_result )
255 wppb_signup_user_notification( esc_sql( $sql_result['user_login'] ), esc_sql( $sql_result['user_email'] ), $sql_result['activation_key'], $sql_result['meta'] );
256
257 }
258
259 $this->wppb_process_bulk_action_message( __( 'The selected users have had their activation emails resent', 'profilebuilder' ), get_bloginfo('url').'/wp-admin/users.php?page=unconfirmed_emails' );
260 }
261
262 }else
263 $this->wppb_process_bulk_action_message( __( "Sorry, but you don't have permission to do that!", "profilebuilder" ), get_bloginfo('url').'/wp-admin/' );
264 }
265
266
267 /** ************************************************************************
268 * REQUIRED! This is where you prepare your data for display. This method will
269 * usually be used to query the database, sort and filter the data, and generally
270 * get it ready to be displayed. At a minimum, we should set $this->items and
271 * $this->set_pagination_args(), although the following properties and methods
272 * are frequently interacted with here...
273 *
274 * @global WPDB $wpdb
275 * @uses $this->_column_headers
276 * @uses $this->items
277 * @uses $this->get_columns()
278 * @uses $this->get_sortable_columns()
279 * @uses $this->get_pagenum()
280 * @uses $this->set_pagination_args()
281 **************************************************************************/
282 function prepare_items() {
283 global $wpdb;
284
285 $this->dataArray = array();
286 $iterator = 0;
287
288 $results = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."signups WHERE active = 0");
289 foreach ($results as $result){
290 $tempArray = array('ID' => $result->user_email, 'username' => $result->user_login, 'email' => $result->user_email, 'registered' => $result->registered);
291
292 array_push($this->dataArray, $tempArray);
293 $iterator++;
294 }
295
296 /**
297 * First, lets decide how many records per page to show
298 */
299 $per_page = apply_filters('wppb_email_confirmation_user_per_page_number', 20);
300
301
302 /**
303 * REQUIRED. Now we need to define our column headers. This includes a complete
304 * array of columns to be displayed (slugs & titles), a list of columns
305 * to keep hidden, and a list of columns that are sortable. Each of these
306 * can be defined in another method (as we've done here) before being
307 * used to build the value for our _column_headers property.
308 */
309 $columns = $this->get_columns();
310 $hidden = array();
311 $sortable = $this->get_sortable_columns();
312
313
314 /**
315 * REQUIRED. Finally, we build an array to be used by the class for column
316 * headers. The $this->_column_headers property takes an array which contains
317 * 3 other arrays. One for all columns, one for hidden columns, and one
318 * for sortable columns.
319 */
320 $this->_column_headers = array($columns, $hidden, $sortable);
321
322
323 /**
324 * Optional. You can handle your bulk actions however you see fit. In this
325 * case, we'll handle them within our package just to keep things clean.
326 */
327 $this->wppb_process_bulk_action();
328
329
330 /**
331 * Instead of querying a database, we're going to fetch the example data
332 * property we created for use in this plugin. This makes this example
333 * package slightly different than one you might build on your own. In
334 * this example, we'll be using array manipulation to sort and paginate
335 * our data. In a real-world implementation, you will probably want to
336 * use sort and pagination data to build a custom query instead, as you'll
337 * be able to use your precisely-queried data immediately.
338 */
339 $data = $this->dataArray;
340
341
342 /**
343 * This checks for sorting input and sorts the data in our array accordingly.
344 *
345 * In a real-world situation involving a database, you would probably want
346 * to handle sorting by passing the 'orderby' and 'order' values directly
347 * to a custom query. The returned data will be pre-sorted, and this array
348 * sorting technique would be unnecessary.
349 */
350 function usort_reorder($a,$b){
351 $orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'username'; //If no sort, default to username
352 $order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc'; //If no order, default to asc
353 $result = strcmp($a[$orderby], $b[$orderby]); //Determine sort order
354 return ($order==='asc') ? $result : -$result; //Send final sort direction to usort
355 }
356 usort($data, 'usort_reorder');
357
358 /**
359 * REQUIRED for pagination. Let's figure out what page the user is currently
360 * looking at. We'll need this later, so you should always include it in
361 * your own package classes.
362 */
363 $current_page = $this->get_pagenum();
364
365 /**
366 * REQUIRED for pagination. Let's check how many items are in our data array.
367 * In real-world use, this would be the total number of items in your database,
368 * without filtering. We'll need this later, so you should always include it
369 * in your own package classes.
370 */
371 $total_items = count($data);
372
373
374 /**
375 * The PB_WP_List_Table class does not handle pagination for us, so we need
376 * to ensure that the data is trimmed to only the current page. We can use
377 * array_slice() to
378 */
379 $data = array_slice($data,(($current_page-1)*$per_page),$per_page);
380
381
382
383 /**
384 * REQUIRED. Now we can add our *sorted* data to the items property, where
385 * it can be used by the rest of the class.
386 */
387 $this->items = $data;
388
389
390 /**
391 * REQUIRED. We also have to register our pagination options & calculations.
392 */
393 $this->set_pagination_args( array(
394 'total_items' => $total_items, //WE have to calculate the total number of items
395 'per_page' => $per_page, //WE have to determine how many items to show on a page
396 'total_pages' => ceil($total_items/$per_page) //WE have to calculate the total number of pages
397 ) );
398 }
399
400 }
401
402
403
404
405
406 /** ************************ REGISTER THE PAGE ****************************
407 *******************************************************************************
408 * Now we just need to define an admin page.
409 */
410 function wppb_add_ec_submenu_page() {
411 if (is_multisite()){
412 add_submenu_page( 'users.php', 'Unconfirmed Email Address', 'Unconfirmed Email Address', 'manage_options', 'unconfirmed_emails', 'wppb_unconfirmed_email_address_custom_menu_page' );
413 remove_submenu_page( 'users.php', 'unconfirmed_emails' ); //hide the page in the admin menu
414
415 }else{
416 $wppb_generalSettings = get_option('wppb_general_settings', 'not_found');
417 if($wppb_generalSettings != 'not_found')
418 if(!empty($wppb_generalSettings['emailConfirmation']) && ($wppb_generalSettings['emailConfirmation'] == 'yes'))
419 add_submenu_page( 'users.php', 'Unconfirmed Email Address', 'Unconfirmed Email Address', 'manage_options', 'unconfirmed_emails', 'wppb_unconfirmed_email_address_custom_menu_page' );
420 remove_submenu_page( 'users.php', 'unconfirmed_emails' ); //hide the page in the admin menu
421 }
422 }
423 add_action('admin_menu', 'wppb_add_ec_submenu_page');
424
425
426
427 /***************************** RENDER PAGE ********************************
428 *******************************************************************************
429 * This function renders the admin page. Although it's
430 * possible to call prepare_items() and display() from the constructor, there
431 * are often times where you may need to include logic here between those steps,
432 * so we've instead called those methods explicitly. It keeps things flexible, and
433 * it's the way the list tables are used in the WordPress core.
434 */
435 function wppb_unconfirmed_email_address_custom_menu_page(){
436
437 //Create an instance of our package class...
438 $listTable = new wpp_list_unfonfirmed_email_table();
439 //Fetch, prepare, sort, and filter our data...
440 $listTable->prepare_items();
441
442 ?>
443 <div class="wrap">
444
445 <div class="wrap"><div id="icon-users" class="icon32"></div><h2><?php _e('Users with Unconfirmed Email Address', 'profilebuilder');?></h2></div>
446
447 <ul class="subsubsub">
448 <li class="all"><a href="users.php"><?php _e('All Users', 'profilebuilder');?></a></li>
449 </ul>
450
451 <!-- Forms are NOT created automatically, so you need to wrap the table in one to use features like bulk actions -->
452 <form id="movies-filter" method="get">
453 <!-- For plugins, we also need to ensure that the form posts back to our current page -->
454 <input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>" />
455 <!-- Now we can render the completed list table -->
456 <?php $listTable->display() ?>
457 </form>
458
459 </div>
460 <?php
461 }