· 9 years ago · Jan 19, 2017, 07:42 AM
1<?php
2/*
3 Plugin Name: Letter of Recommendation Functionality
4 Plugin URI:
5 Description: Adds the functionality using formidable hooks for the writers of letters of recommendations
6 Version: 0.1
7 Author: Gediminas Jakstonis
8 Author URI:
9 License: GPLv2+
10 Text Domain: wp-lor-functionality
11*/
12
13//https://formidablepro.com/knowledgebase/frm_after_create_entry/
14add_action('frm_after_create_entry','lor_functionality', 30, 2);
15
16//SECURITY RELATED
17$generated_pw_length = 256; //Password length generated
18
19//APLICATION IDs
20$upload_lor_student_application = 7; //Corresponds to the application number from formidable for the student application form
21$upload_lor_test_email_form = 25; //Corresponds to the application number from formidable for a test application form
22$upload_lor_lor_upload = 26; //Corresponds to the application number from formidable for the letter of recommendation form
23
24//FORM IDs FROM STUDENT APPLICATION
25$username_id = 252;
26$writer_email_id = 226;
27$student_first_name = 153;
28$student_last_name = 154;
29
30//FORM IDs FROM LETTER OF RECOMMENDATION FORM
31$student_username = 379;
32
33//EMAIL INFORMATION
34$page_link_value = 472;
35$email_header = "From: NO-REPLY <mcautosenior@gmail.com>";
36
37//TABLE PREFIX
38$upload_lor_prefix = 'loc_user_code_table';
39
40//FORM PAGE
41$lor_page_text_success = ""; //Text for successful match
42$lor_page_text_submitted = "A letter of recommendation has already been submitted for this applicant."; //Text for successful match and existing submission
43$lor_page_text_failure = "The URL you are trying to use to submit a letter of recommendation is not valid."; //Text for unsuccessful match
44
45//
46function lor_functionality($entry_id, $form_id)
47{
48 //use the above defined application IDs
49 global $upload_lor_student_application;
50 global $upload_lor_test_email_form;
51
52 if ($form_id == $upload_lor_student_application) //STUDENT APPLICATION FORM
53 {
54 //Constant for password length
55 global $generated_pw_length;
56
57 //use the above defined form IDs
58 global $username_id;
59 global $writer_email_id;
60 global $student_first_name;
61 global $student_last_name;
62
63 //Check to see if the table exists
64 create_table_if_needed();
65
66 //Information to be used for the URL and database
67 $access_code = wp_generate_password($generated_pw_length, false); //This is sent in the email URL
68 $secured_code = wp_hash_password($access_code); //This is stored in the database
69 //Grab username from submitted form
70 $username = FrmProEntriesController::get_field_value_shortcode(array('field_id' => $username_id, 'entry' => $entry_id)); //https://formidablepro.com/knowledgebase/get-a-value-from-an-entry/
71
72 $username = str_replace(' ', '%20', $username);
73
74 //Add info to database
75 add_lor_codes($username, $secured_code);
76
77 $to = FrmProEntriesController::get_field_value_shortcode(array('field_id' => $writer_email_id, 'entry' => $entry_id));
78
79 $first_name = FrmProEntriesController::get_field_value_shortcode(array('field_id' => $student_first_name, 'entry' => $entry_id));
80 $last_name = FrmProEntriesController::get_field_value_shortcode(array('field_id' => $student_last_name, 'entry' => $entry_id));
81
82 $student_name = $first_name . ' ' . $last_name;
83
84 //Generate and send the email
85 generate_and_send_email($to, $student_name, $access_code, $username);
86 }
87 else if ($form_id == upload_lor_lor_upload) //LETTER OF RECOMMENDATION FORM
88 {
89 global $student_username;
90 global $upload_lor_prefix;
91 global $wpdb;
92 $lor_table = $wpdb->prefix . $upload_lor_prefix;
93
94 $user = FrmProEntriesController::get_field_value_shortcode(array('field_id' => $student_username, 'entry' => $entry_id));
95 //used to only allow one upload
96 $wpdb->query($wpdb->prepare("UPDATE $lor_table SET submitted='1' WHERE user='$user'"));
97 }
98
99}
100
101//Check to see if the table needed for the lor exists, if not create it
102//Creates a table of user, code, and submitted that is used when allowing the submission of lors
103function create_table_if_needed()
104{
105 global $wpdb;
106 global $upload_lor_prefix;
107
108 $lor_table = $wpdb->prefix . $upload_lor_prefix;
109
110 if ($wpdb->get_var("SHOW TABLES LIKE '$lor_table'") != $lor_table)
111 {
112 //table doesn't exist, create it
113 $charset_collate = $wpdb->get_charset_collate();
114
115 //SQL code to create the table
116 $sql = "CREATE TABLE $lor_table (
117 user VARCHAR(128) NOT NULL,
118 code VARCHAR(128) NOT NULL,
119 submitted TINYINT(1) DEFAULT '0',
120 PRIMARY KEY (user)
121) $charset_collate;";
122 require_once( ABSPATH . 'wp-admin/includes/upgrade.php');
123 dbDelta($sql);
124 }
125}
126
127//Adds the information passed in to the lor database
128//TODO: Change it so it always adds not just the first time
129function add_lor_codes($username, $hashed_code)
130{
131 global $wpdb;
132 global $upload_lor_prefix;
133
134 $lor_table = $wpdb->prefix . $upload_lor_prefix;
135 $wpdb->insert(
136$lor_table,
137array(
138'user' => $username,
139'code' => $hashed_code,
140'submitted' => 0
141)
142);
143}
144
145//Generates the URL for the lor page and then sends it in an email
146function generate_and_send_email($to, $student_name, $code, $username)
147{
148 global $page_link_value;
149 global $email_header;
150
151 //CUSTOM URL
152 $lor_upload_page = get_page_link($page_link_value);
153 $lor_upload_page = $lor_upload_page . "?user=" . $username . "&code=" . $code;
154
155 $subject = $student_name . " needs a Letter of Recommendation";
156 $message = $student_name . " has requested a letter of recommendation. To do so follow this link ";
157
158 $message = $message . "$lor_upload_page";
159
160 $attachments = array();
161
162 $headers[] = $email_header;
163
164 wp_mail($to, $subject, $message, $headers, $attachments);
165
166}
167
168//Function that is used in the shortcode upload-lor to check for valid URLs for lor
169function can_submit_lor()
170{
171 global $wpdb;
172 global $upload_lor_prefix;
173 global $upload_lor_lor_upload;
174
175 $lor_table = $wpdb->prefix . $upload_lor_prefix;
176
177 $code = $_GET["code"];
178 $user = $_GET["user"];
179
180 $query = "SELECT * FROM $lor_table WHERE user = '$user'";
181 $applicant = $wpdb->get_row($query, 'ARRAY_A', 0);
182
183 if (wp_check_password($code, $applicant[code]))
184 {
185 //Check to see if the letter was already submitted
186 if ($applicant[submitted] == 0)
187 {
188 global $lor_page_text_success;
189 echo "$lor_page_text_success";
190 echo do_shortcode('[formidable id=' . $upload_lor_lor_upload . ']'); //Show the formidable form for the letter of recommendation
191 }
192 else
193 {
194 global $lor_page_text_submitted;
195 echo "$lor_page_text_submitted";
196 }
197 }
198 else
199 {
200 global $lor_page_text_failure;
201 echo "$lor_page_text_failure";
202 }
203}
204
205//[upload-lor] shortcode
206add_shortcode( 'upload-lor', 'can_submit_lor');
207
208
209
210
211?>