Last active
April 15, 2024 21:21
-
-
Save alfredo-wpmudev/74fea6c7d31e97f766677ddb246e5bfa to your computer and use it in GitHub Desktop.
[Forminator Pro] - Taekwondo Student Verification for User and ACF fields.
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 Pro] - Taekwondo Student Verification for User and ACF fields. | |
* Plugin URI: https://wpmudev.com/ | |
* Description: Taekwondo Student Verification for User and ACF fields. | |
* Author: Alfredo Galano Loyola @ WPMUDEV | |
* Author URI: https://wpmudev.com/ | |
* License: GPLv2 or later | |
*/ | |
if (!defined('ABSPATH')) { | |
exit; | |
} | |
// No need to do anything if the request is via WP-CLI. | |
if (defined('WP_CLI') && WP_CLI) { | |
return; | |
} | |
if (!class_exists('WPMUDEV_Forminator_Taekwondo_Student_Verification')) { | |
class WPMUDEV_Forminator_Taekwondo_Student_Verification | |
{ | |
/** | |
* $forms used to define the form and the hidden field that will be used. It's possible use the feature in several forms | |
*/ | |
private $forms = array( | |
2617 => 'hidden-1', | |
); | |
private $search_fields = array( | |
'name-1' => '', | |
'date-1' => '', | |
); | |
private $student_data; | |
private $success_message = "The student exist and his belt is "; | |
private $error_message = "No student in Maiwand Taekwondo match those searching criteria."; | |
private static $_instance = null; | |
public static function get_instance() | |
{ | |
if (is_null(self::$_instance)) { | |
self::$_instance = new WPMUDEV_Forminator_Taekwondo_Student_Verification(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() | |
{ | |
$this->init(); | |
} | |
public function init() | |
{ | |
// Do some checks here | |
if (!defined('FORMINATOR_VERSION') || FORMINATOR_VERSION < '1.12' || !class_exists('Forminator_API')) { | |
return; | |
} | |
add_filter('forminator_custom_form_submit_field_data', array($this, 'wpmudev_set_field_data'), 10, 2); | |
add_filter( 'forminator_replace_form_data',array($this, 'wpmudev_replace_form_data') , 10, 3 ); | |
} | |
//Working with the submitted Data | |
public function wpmudev_set_field_data($field_data_array, $form_id) | |
{ | |
if (!in_array($form_id, array_keys($this->forms))) { | |
return $field_data_array; | |
} | |
foreach ($field_data_array as $key => $field){ | |
//Removing the Hidden Field | |
if ($field['name'] === $this->forms[$form_id]) { | |
unset($field_data_array[$key]); | |
} | |
if(isset($this->search_fields[$field['name']])){ | |
$this->search_fields[$field['name']] = $field['value']; | |
} | |
} | |
//Searching for the Student | |
$resp = $this->search_student_by_Name_Date($this->search_fields); | |
$this->student_data =''; | |
//Checking the search | |
$this->student_data = $resp; //Show error message if not exist the student. | |
//Recreating the Hidden Field | |
$field_data_array[] = array( | |
'name' => $this->forms[$form_id], | |
'value' => $this->student_data, | |
'field_array' => array( | |
'default_value' => 'custom_value', | |
'custom_value' => $this->student_data, | |
) | |
); | |
return $field_data_array; | |
} | |
function search_student_by_Name_Date($search_fields){ | |
global $wpdb; | |
$user_meta_table = $wpdb->prefix . 'usermeta'; | |
$sql = "SELECT `user_id`,`meta_key`,`meta_value` FROM $user_meta_table WHERE (`meta_key`='last_name' AND `meta_value`='%s');"; | |
$sql = $wpdb->prepare($sql, array($search_fields['name-1'])); | |
$search_result = $wpdb->get_results($sql); | |
$student_profile = ''; | |
$student_picture = ''; | |
foreach ($search_result as $user) { | |
if (get_field('date-1', 'user_'.$user->user_id) === $search_fields['date-1']) { | |
$upload = get_field('upload-1', 'user_'.$user->user_id); | |
$student_picture = $upload["sizes"]["thumbnail"]; | |
$student_profile.=' <img src="'.$student_picture.'" alt="'.$search_fields['name-1'].'" class="center"> '; | |
$student_profile.='<br>'; | |
$student_profile.='Name: '.get_field('first_name', 'user_'.$user->user_id)." ".$search_fields['name-1']; | |
$student_profile.='<br>'; | |
$student_profile.='Belt(Rank): <b>'.get_field('select-1', 'user_'.$user->user_id).'</b>'; | |
return $student_profile; | |
break; | |
} | |
} | |
return $this->error_message; | |
} | |
//To replace the tag used to show the student data {student_data} | |
function wpmudev_replace_form_data( $content, $data, $original_content ){ | |
if (!in_array(intval($data['form_id']), array_keys($this->forms))) { // Please change the form ID | |
return $content; | |
} | |
if ( strpos( $content, '{student_data}' ) !== false ) { | |
$content = str_replace( '{student_data}', $this->student_data, $content ); | |
} | |
return $content; | |
} | |
} | |
add_action('plugins_loaded', function () { | |
return WPMUDEV_Forminator_Taekwondo_Student_Verification::get_instance(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment