Last active
July 14, 2020 02:58
-
-
Save wpmudev-sls/17c6f3f89d72269c404b22e28622ca56 to your computer and use it in GitHub Desktop.
[Forminator] - Save user name on quiz submission
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Forminator] - Save user name on quiz submission | |
* Description: [Forminator] - Save user name on quiz submission - Alternative MU plugin forminator-quiz-user-data.php: https://gist.github.com/wpmudev-sls/e885d070586a60eb7b32a03007919d97 | |
* Jira: SLS-76 | |
* Author: Panos & Thobk @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} elseif ( defined( 'WP_CLI' ) && WP_CLI ) { | |
return; | |
} | |
add_action( 'after_setup_theme', 'wpmudev_forminator_save_user_name_on_quiz_submission_func', 100 ); | |
function wpmudev_forminator_save_user_name_on_quiz_submission_func() { | |
if ( defined('FORMINATOR_PRO') && class_exists( 'Forminator' ) && is_user_logged_in() ) { | |
class WPMUDEV_FM_Save_User_Name_Quiz_Submission{ | |
private $include_quiz_ids = []; | |
private $exclude_form_ids = [];//enter list exclude form ids, e.g: [234,456] | |
public function __construct(){ | |
add_action( 'forminator_quizzes_submit_before_set_fields', array( $this, 'save_user_data' ), 20, 2 ); | |
// Show in admin list | |
add_action( 'admin_footer', array( $this, 'admin_footer' ) ); | |
// add_action( 'forminator-pro_page_forminator-quiz-view', array( $this, 'custom_quiz_search_query_func') ); | |
add_action('load-forminator-pro_page_forminator-entries', array( $this, 'custom_quiz_search_query_func'), 9 ); | |
} | |
public function enabled_on_this_quiz( $quiz_id ){ | |
if( ! empty( $this->include_quiz_ids ) ){ | |
if( ! in_array( $quiz_id, $include_quiz_ids ) ){ | |
return; | |
} | |
}elseif( ! empty( $this->exclude_form_ids ) && in_array( $quiz_id, $this->exclude_form_ids ) ){ | |
return; | |
} | |
return true; | |
} | |
public function custom_quiz_search_query_func(){ | |
add_filter( 'query', array( $this, 'custom_quiz_search_query') ); | |
} | |
public function custom_quiz_search_query( $query ){ | |
if( ! empty( $_GET['form_id'] ) && ! empty( $_GET['user_name'] ) && strpos( $query, 'AND `is_spam` = 0 ORDER BY `entry_id` DESC LIMIT 0, 10 ' ) ){ | |
$user_name = $_GET['user_name']; | |
$user = get_user_by( 'login', $user_name ); | |
if( $user ){ | |
global $wpdb; | |
$user_email = $user->data->user_email; | |
$entry = Forminator_Database_Tables::get_table_name( Forminator_Database_Tables::FORM_ENTRY ); | |
$entry_meta = Forminator_Database_Tables::get_table_name( Forminator_Database_Tables::FORM_ENTRY_META ); | |
$form_id = (int) $_GET['form_id']; | |
$per_page = 10; | |
$page = ! empty( $_GET['paged'] ) ? intval( $_GET['paged'] ) : 1; | |
$sql = "SELECT e.entry_id FROM {$entry} as e INNER JOIN {$entry_meta} as m ON e.entry_id = m.entry_id WHERE e.form_id = %d AND e.is_spam = 0 AND m.meta_key = %s AND m.meta_value = %s ORDER BY e.entry_id DESC LIMIT %d, %d "; | |
$query = $wpdb->prepare( $sql, $form_id, "user_email", $user_email, $page - 1, $per_page ); | |
} | |
} | |
return $query; | |
} | |
public function save_user_data( $entry, $quiz_id ){ | |
if( ! $this->enabled_on_this_quiz( $quiz_id ) ){ | |
return; | |
} | |
$current_user = wp_get_current_user(); | |
$entry->set_fields( | |
array( | |
array( | |
'name'=> 'user_email', | |
'value' => $current_user->user_email | |
), | |
) | |
); | |
} | |
public function admin_screen_entries() { | |
$quiz_id = intval( $_REQUEST['form_id'] ); | |
if( ! $this->enabled_on_this_quiz( $quiz_id ) ){ | |
return; | |
} | |
$per_page = forminator_form_view_per_page( 'entries' ); | |
$paged = isset( $_GET['paged'] ) ? intval( $_GET['paged'] ) : 1; | |
$entries = Forminator_Form_Entry_Model::list_entries( $quiz_id, $per_page, ( $paged - 1 ) * $per_page ); | |
return $entries; | |
} | |
public function admin_footer() { | |
if ( ! function_exists( 'get_current_screen' ) || 'forminator-pro_page_forminator-entries' !== get_current_screen()->id ) { | |
return; | |
} | |
if( ! isset( $_GET['form_id'] ) || ! $this->enabled_on_this_quiz( $_GET['form_id'] ) ){ | |
return; | |
} | |
// We'll load all user meta and then we will position that with js for each entry. | |
// 1st get the user-data meta of each entry into a json oblect | |
$entries = $this->admin_screen_entries(); | |
$user_data = array(); | |
foreach ( $entries as $entry ) { | |
$user_email = $entry->get_meta('user_email'); | |
if( $user_email ){ | |
$user = get_user_by( 'email', $user_email ); | |
if( $user ){ | |
$user_data[] = array( | |
'username' => sprintf("<a target='_blank' href='%s'>%s</a>", get_edit_user_link( $user->ID ), $user->user_login ), | |
'useremail' => $user->user_email | |
); | |
} | |
} | |
} | |
$user_data = json_encode( $user_data ); | |
// 2nd. There is no entry id set in admin, so we will | |
// a) create some custom wrappers on each entry row | |
// b) then loop through the user_data json and push to wrapper by index with js. | |
?> | |
<script type="text/javascript"> | |
( ($,d)=>{ | |
if ( window.wpmudev_forminator_quiz_user_data_admin ) { | |
return; | |
} | |
window.wpmudev_forminator_quiz_user_data_admin = { | |
user_data: <?php echo $user_data; ?>, | |
list_rows: $( '.wpmudev-forminator-forminator-entries form.sui-box table tr.sui-accordion-item-content' ), | |
run: function(){ | |
// add | |
this.searchForm(); | |
let counter = 0, | |
s_counter =0; | |
this.list_rows.each(function(){ | |
let row = $( this ) | |
module_title = row.find( 'h2' ), | |
user_data_wrap = $( '<div />', {id: `forminator-user-data-module-${counter}`} ); | |
module_title.after( user_data_wrap ); | |
if( counter in wpmudev_forminator_quiz_user_data_admin.user_data ){ | |
row.prev('.sui-accordion-item').find('.sui-description').append('<span> - ' + wpmudev_forminator_quiz_user_data_admin.user_data[ counter ].username + '</span>' ); | |
} | |
counter++; | |
}); | |
for ( let m = 0; m < counter; m++ ) { | |
if ( typeof this.user_data[ m ] === 'undefined' || null == this.user_data[ m ] ) { | |
continue; | |
} | |
let wrapper = $( `#forminator-user-data-module-${m}` ), | |
username = ( typeof this.user_data[ m ].username !== 'undefined' ) ? this.user_data[ m ].username : '', | |
useremail = ( typeof this.user_data[ m ].useremail !== 'undefined' ) ? this.user_data[ m ].useremail : ''; | |
user_meta = ` | |
<strong>User data</strong> | |
<div> | |
<label>Username: <label/> | |
<span>${username}</span> | |
</div> | |
<div> | |
<label>User email: <label/> | |
<span>${useremail}</span> | |
</div> | |
`; | |
wrapper.append( user_meta ); | |
} | |
}, | |
searchForm:function(){ | |
var _entries_bar = $('.fui-bar-selectors'); | |
if( _entries_bar.length ){ | |
_entries_bar.append('<input type="search" name="user_name" />'); | |
} | |
} | |
} | |
$(d).ready(function(){ wpmudev_forminator_quiz_user_data_admin.run(); }); | |
})(jQuery,document); | |
</script> | |
<?php | |
} | |
} | |
$run = new WPMUDEV_FM_Save_User_Name_Quiz_Submission; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment