PluginProbe ʕ •ᴥ•ʔ
Brevo – Email, SMS, Web Push, Chat, and more. / 3.1.76
Brevo – Email, SMS, Web Push, Chat, and more. v3.1.76
2.9.13 2.9.14 2.9.15 2.9.16 2.9.17 2.9.18 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8 2.9.9 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.9 3.1.0 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.2 3.1.20 3.1.21 3.1.22 3.1.23 3.1.24 3.1.25 3.1.26 3.1.27 3.1.28 3.1.29 3.1.3 3.1.30 3.1.31 3.1.32 3.1.33 3.1.34 3.1.35 3.1.36 3.1.37 3.1.38 3.1.39 3.1.4 3.1.40 3.1.41 3.1.42 3.1.43 3.1.44 3.1.45 3.1.46 3.1.47 3.1.48 3.1.49 3.1.5 3.1.50 3.1.51 3.1.52 3.1.53 3.1.54 3.1.55 3.1.56 3.1.57 3.1.58 3.1.59 3.1.6 3.1.60 3.1.61 3.1.62 3.1.63 3.1.64 3.1.65 3.1.66 3.1.67 3.1.68 3.1.69 3.1.7 3.1.70 3.1.71 3.1.72 3.1.73 3.1.74 3.1.75 3.1.76 3.1.77 3.1.78 3.1.79 3.1.8 3.1.80 3.1.81 3.1.82 3.1.83 3.1.84 3.1.85 3.1.86 3.1.87 3.1.88 3.1.89 3.1.9 3.1.90 3.1.91 3.1.92 3.1.93 3.1.94 3.1.95 3.1.96 3.1.97 3.1.98 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 trunk 1.0 1.5 2.0.8 2.9.10 2.9.11 2.9.12
mailin / model / model-forms.php
mailin / model Last commit date
index.php 11 years ago model-contacts.php 5 years ago model-forms.php 2 years ago model-lang.php 5 years ago model-users.php 4 years ago
model-forms.php
448 lines
1 <?php
2 /**
3 * Model class <i>SIB_Forms</i> represents forms
4 *
5 * @package SIB_Forms
6 */
7
8 if ( ! class_exists( 'SIB_Forms' ) ) {
9 /**
10 * Class SIB_Forms
11 *
12 * @package SIB_Forms
13 */
14 class SIB_Forms {
15
16 /**
17 * Tab table name
18 */
19 const TABLE_NAME = 'sib_model_forms';
20 const DEFAULT_FORM_HTML_PATH = '../form/default-form.html';
21 const DEFAULT_FORM_CSS_PATH = '../form/css/default-form.css';
22 const DEFAULT_FORM_MESSAGE_CSS_PATH = '../form/css/default-form-message.css';
23
24 /** Create Table */
25 public static function createTable() {
26 global $wpdb;
27 $table_name = $wpdb->prefix . self::TABLE_NAME;
28
29 //Check if table exists
30 $table_check_query = 'SHOW TABLES LIKE ' . "'" . $table_name . "'" . ';';
31 $wpdb->query($table_check_query);
32
33
34 //table doesnot exist
35 if (empty($wpdb->last_result)) {
36 // create list table.
37 $query =
38 'CREATE TABLE IF NOT EXISTS ' . $table_name . ' (
39 `id` int(20) NOT NULL AUTO_INCREMENT,
40 `title` varchar(120) CHARACTER SET utf8 COLLATE utf8_unicode_ci,
41 `html` longtext CHARACTER SET utf8 COLLATE utf8_unicode_ci,
42 `css` longtext,
43 `dependTheme` int(1) NOT NULL DEFAULT 1,
44 `listID` longtext,
45 `templateID` int(20) NOT NULL DEFAULT -1,
46 `confirmID` int(20) NOT NULL DEFAULT -1,
47 `isDopt` int(1) NOT NULL DEFAULT 0,
48 `isOpt` int(1) NOT NULL DEFAULT 0,
49 `redirectInEmail` varchar(255),
50 `redirectInForm` varchar(255),
51 `successMsg` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci,
52 `errorMsg` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci,
53 `existMsg` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci,
54 `invalidMsg` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci,
55 `requiredMsg` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci,
56 `attributes` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci,
57 `date` DATE NOT NULL,
58 `isDefault` int(1) NOT NULL DEFAULT 0,
59 `gCaptcha` int(1) NOT NULL DEFAULT 0,
60 `gCaptcha_secret` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci,
61 `gCaptcha_site` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci,
62 `selectCaptchaType` int(1) NOT NULL DEFAULT 0,
63 `cCaptchaType` int(1) NOT NULL DEFAULT 0,
64 `cCaptcha_secret` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci,
65 `cCaptcha_site` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci,
66 `termAccept` int(1) NOT NULL DEFAULT 0,
67 `termsURL` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci,
68 PRIMARY KEY (`id`)
69 );';
70 $wpdb->query($query);
71
72 // create default form.
73 $rows = $wpdb->get_results('SELECT * FROM '. $wpdb->prefix . self::TABLE_NAME );
74 if (count( $rows ) == 0 )
75 {
76 self::createDefaultForm();
77 }
78
79 } else {
80 // check if select captcha type fields exist
81 $selectCaptchaType = 'selectCaptchaType';
82 $result = $wpdb->query( $wpdb->prepare( "SHOW COLUMNS FROM `$table_name` LIKE %s ", $selectCaptchaType ) ); // db call ok; no-cache ok.
83
84 if ( empty( $result ) ) {
85 $alter_query = "ALTER TABLE " . $table_name . "
86 ADD COLUMN selectCaptchaType int(1) NOT NULL DEFAULT 0 After gCaptcha_site,
87 ADD COLUMN cCaptchaType int(1) NOT NULL DEFAULT 0 After selectCaptchaType,
88 ADD COLUMN cCaptcha_secret varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci AFTER selectCaptchaType,
89 ADD COLUMN cCaptcha_site varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci AFTER cCaptcha_secret;
90 ";
91 $wpdb->query( $alter_query );
92 }
93 }
94 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
95 }
96
97 /**
98 * Remove table
99 */
100 public static function removeTable() {
101 global $wpdb;
102 $query = 'DROP TABLE IF EXISTS ' . $wpdb->prefix . self::TABLE_NAME . ';';
103 $wpdb->query( $query ); // db call ok; no-cache ok.
104 }
105
106 /**
107 * Add columns for old versions
108 */
109 public static function alterTable() {
110 global $wpdb;
111 // add columns -gCaptcha, gCaptcha_secret.
112 $table_name = $wpdb->prefix . self::TABLE_NAME;
113
114 // check if gCaptcha fields exist
115 $gCaptcha = 'gCaptcha';
116 $result = $wpdb->query( $wpdb->prepare( "SHOW COLUMNS FROM `$table_name` LIKE %s ", $gCaptcha ) ); // db call ok; no-cache ok.
117
118 if ( empty( $result ) ) {
119 $alter_query = 'ALTER TABLE ' . $table_name . '
120 ADD COLUMN gCaptcha int(1) not NULL DEFAULT 0,
121 ADD COLUMN gCaptcha_secret varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci,
122 ADD COLUMN gCaptcha_site varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci';
123 $ret = $wpdb->query( $alter_query );
124 }
125
126 // add columns -termAccept, termsURL : version 2.9.0
127 $check_query = 'SHOW COLUMNS FROM `' . $table_name . "` LIKE 'termAccept';";
128 $result = $wpdb->query( $check_query );
129 if ( empty( $result ) ) {
130 $alter_query = 'ALTER TABLE ' . $table_name . '
131 ADD COLUMN termAccept int(1) not NULL DEFAULT 1,
132 ADD COLUMN termsURL varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci';
133 $ret = $wpdb->query( $alter_query );
134 }
135 // add columns - confirmID : version 2.9.0
136 $check_query = 'SHOW COLUMNS FROM `' . $table_name . "` LIKE 'confirmID';";
137 $result = $wpdb->query( $check_query );
138 if ( empty( $result ) ) {
139 $alter_query = 'ALTER TABLE ' . $table_name . '
140 ADD COLUMN confirmID int(20) not NULL DEFAULT -1';
141 $ret = $wpdb->query( $alter_query );
142 }
143 // add columns - requiredMsg : version 2.9.3
144 $check_query = 'SHOW COLUMNS FROM `' . $table_name . "` LIKE 'requiredMsg';";
145 $result = $wpdb->query( $check_query );
146 if ( empty( $result ) ) {
147 $alter_query = 'ALTER TABLE ' . $table_name . '
148 ADD COLUMN requiredMsg varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci';
149 $ret = $wpdb->query( $alter_query );
150 }
151 }
152
153 /**
154 * Get form data
155 *
156 * @param string $frmID - form ID.
157 * @return array|null|object|void
158 */
159 public static function getForm( $frmID = 'new' ) {
160 global $wpdb;
161 if ( 'new' == $frmID ) {
162 // default form.
163 $formData = self::getDefaultForm();
164 $list = maybe_serialize( array( SIB_API_Manager::get_default_list_id() ) );
165 $results = array(
166 'title' => '',
167 'html' => $formData['html'],// phpcs:ignore
168 'css' => $formData['css'],
169 'listID' => $list,
170 'dependTheme' => '1',
171 'templateID' => '-1',
172 'confirmID' => '-1',
173 'isOpt' => '0',
174 'isDopt' => '0',
175 'redirectInEmail' => '',
176 'redirectInForm' => '',
177 'date' => date( 'Y-m-d' ),
178 'successMsg' => $formData['successMsg'],
179 'errorMsg' => $formData['errorMsg'],
180 'existMsg' => $formData['existMsg'],
181 'invalidMsg' => $formData['invalidMsg'],
182 'requiredMsg' => $formData['requiredMsg'],
183 'attributes' => 'email,NAME',
184 );
185 } else {
186 $query = $wpdb->prepare('SELECT * from ' . $wpdb->prefix . self::TABLE_NAME . ' where id = %d',array(esc_sql($frmID)));
187 $results = $wpdb->get_row( $query, ARRAY_A ); // db call ok; no-cache ok.
188 }
189
190 if ( is_array( $results ) && count( $results ) > 0 ) {
191 $listIDs = maybe_unserialize( $results['listID'] );
192 $results['listID'] = $listIDs;
193 return $results;
194 }
195 return array();
196 }
197
198 /**
199 * Get all forms
200 */
201 public static function getForms() {
202 global $wpdb;
203
204 $query = 'select * from ' . $wpdb->prefix . self::TABLE_NAME . ';';
205 $results = $wpdb->get_results( $query, ARRAY_A ); // db call ok; no-cache ok.
206
207 if ( is_array( $results ) && count( $results ) > 0 ) {
208 // add list names field to display form table.
209 foreach ( $results as $key => $form ) {
210 if ( SIB_Forms_Lang::check_form_trans( $form['id'] ) == true ) {
211 unset( $results[ $key ] );
212 continue;
213 }
214 $listIDs = maybe_unserialize( $form['listID'] );
215 $listIDs = !empty($listIDs) ? $listIDs : array();
216 // get names form id array.
217 $lists = SIB_API_Manager::get_lists(); // pair of id and name.
218
219 $listNames = array();
220 foreach ( $lists as $list ) {
221 if ( in_array( $list['id'], $listIDs ) ) {
222 $listNames[] = $list['name'];
223 }
224 }
225 $results[ $key ]['listName'] = implode( ',', $listNames );
226 $results[ $key ]['listID'] = $listIDs;
227 }
228 return $results;
229 }
230 return array();
231
232 }
233
234 /**
235 * Add new form
236 *
237 * @param array $formData - form data.
238 * @return null|string
239 */
240 public static function addForm( $formData ) {
241 global $wpdb;
242
243 $current_date = date( 'Y-m-d' );
244
245 global $wpdb;
246
247 global $wpdb;
248 $query = 'INSERT INTO ' . $wpdb->prefix . self::TABLE_NAME.' (title,html,css,dependTheme,listID,templateID,confirmID,isOpt,isDopt,redirectInEmail,redirectInForm,successMsg,errorMsg,existMsg,invalidMsg,requiredMsg,attributes,date,gCaptcha,gCaptcha_secret,gCaptcha_site,termAccept,termsURL, selectCaptchaType, cCaptchaType, cCaptcha_secret,cCaptcha_site) VALUES ';
249 $query .= ' (%s, %s, %s, %d, %s, %d, %d, %d, %d, %s, %s, %s, %s, %s, %s, %s, %s, %s, %d, %s, %s, %d, %s, %d, %d,%s, %s)';
250
251 $query = $wpdb->prepare($query,array($formData['title'],$formData['html'],$formData['css'],$formData['dependTheme'],$formData['listID'],
252 $formData['templateID'],$formData['confirmID'],$formData['isOpt'],$formData['isDopt'],$formData['redirectInEmail'],$formData['redirectInForm'],
253 $formData['successMsg'],$formData['errorMsg'],$formData['existMsg'],$formData['invalidMsg'],$formData['requiredMsg'],$formData['attributes'],$current_date,$formData['gcaptcha'],$formData['gcaptcha_secret'] ,$formData['gcaptcha_site'],$formData['termAccept'],$formData['termsURL'], $formData['selectCaptchaType'], $formData['cCaptchaType'], $formData['ccaptcha_secret'], $formData['ccaptcha_site']));
254
255 $wpdb->query( $query ); // db call ok; no-cache ok.
256 $index = $wpdb->get_var( 'SELECT LAST_INSERT_ID();' ); // db call ok; no-cache ok.
257 return $index;
258 }
259
260 /**
261 * Update form
262 *
263 * @param int $formID - form ID.
264 * @param array $formData - form data.
265 * @return bool
266 */
267 public static function updateForm( $formID, $formData ) {
268 global $wpdb;
269
270 $current_date = date( 'Y-m-d' );
271
272 global $wpdb;
273
274 $query = 'UPDATE ' . $wpdb->prefix . self::TABLE_NAME ;
275 $query .= " set title = %s, html = %s, css = %s, dependTheme = %d, listID = %s, templateID = %d, confirmID = %d, isOpt = %d, isDopt = %d, redirectInEmail = %s, redirectInForm = %s, successMsg = %s, errorMsg = %s, existMsg = %s, invalidMsg = %s, requiredMsg = %s, attributes = %s, date = %s, gCaptcha = %d, gCaptcha_secret = %s, gCaptcha_site = %s, termAccept = %d, termsURL = %s, selectCaptchaType = %d, cCaptcha_secret = %s, cCaptcha_site = %s, cCaptchaType = %d";
276 $query .= ' where id= %d';
277
278 $query = $wpdb->prepare( $query ,array($formData['title'],$formData['html'],$formData['css'],$formData['dependTheme'],$formData['listID'],
279 $formData['templateID'],$formData['confirmID'],$formData['isOpt'],$formData['isDopt'],$formData['redirectInEmail'],$formData['redirectInForm'],
280 $formData['successMsg'],$formData['errorMsg'],$formData['existMsg'],$formData['invalidMsg'],$formData['requiredMsg'],$formData['attributes'],$current_date,$formData['gcaptcha'],$formData['gcaptcha_secret'] ,$formData['gcaptcha_site'],$formData['termAccept'],$formData['termsURL'],$formData['selectCaptchaType'],$formData['ccaptcha_secret'] ,$formData['ccaptcha_site'], $formData['cCaptchaType'], esc_sql($formID)));
281
282 $wpdb->query( $query ); // db call ok; no-cache ok.
283
284 return true;
285 }
286
287 /**
288 * Remove form
289 *
290 * @param int $id - target form id.
291 */
292 public static function deleteForm( $id ) {
293 global $wpdb;
294
295 $wpdb->delete(
296 $wpdb->prefix . self::TABLE_NAME,
297 array(
298 'id' => $id,
299 )
300 ); // db call ok; no-cache ok.
301 }
302
303 /** Clear forms data */
304 public static function removeAllForms() {
305 global $wpdb;
306 $wpdb->query( 'TRUNCATE TABLE ' . $wpdb->prefix . self::TABLE_NAME ); // db call ok; no-cache ok.
307 return true;
308 }
309
310 /** Create default form */
311 public static function createDefaultForm() {
312 $formData = self::getDefaultForm();
313 // phpcs:ignore
314 $html = $formData['html'];
315 $css = $formData['css'];
316 $list = maybe_serialize( array( SIB_API_Manager::get_default_list_id() ) );
317 $current_date = date( 'Y-m-d' );
318 $attributes = 'email,NAME';
319 global $wpdb;
320 $query = 'INSERT INTO ' . $wpdb->prefix . self::TABLE_NAME . ' ';
321 $deafult_form_name = esc_attr( __( 'Default Form', 'mailin' ) );
322 $query .= '(title,html,css,listID,dependTheme,successMsg,errorMsg,existMsg,invalidMsg,requiredMsg,attributes,date,isDefault) ';
323 $query .= "VALUES ('{$deafult_form_name}','{$html}','{$css}','{$list}','1','{$formData['successMsg']}','{$formData['errorMsg']}','{$formData['existMsg']}','{$formData['invalidMsg']}','{$formData['requiredMsg']}','{$attributes}','{$current_date}','1')";
324 $wpdb->query( $query ); // db call ok; no-cache ok.
325 }
326
327 /** Get default form data */
328 public static function getDefaultForm() {
329
330 $html = wp_kses(self::get_default_form_html(), SIB_Manager::SIB_ATTRIBUTE);
331 $css = wp_kses(self::get_default_css_html(), SIB_Manager::SIB_ATTRIBUTE);
332
333 $result = array(
334 'html' => $html,
335 'css' => $css,
336 'successMsg' => esc_attr( __( 'Thank you, you have successfully registered !', 'mailin' ) ),
337 'errorMsg' => esc_attr( __( 'Something wrong occured', 'mailin' ) ),
338 'existMsg' => esc_attr( __( 'You have already registered', 'mailin' ) ),
339 'invalidMsg' => esc_attr( __( 'Your email address is invalid', 'mailin' ) ),
340 'requiredMsg' => esc_attr(__('Please fill out this field', 'mailin'))
341 );
342 return $result;
343 }
344
345 /** Get Default css */
346 public static function getDefaultMessageCss() {
347 $css = file_get_contents(__DIR__ . '/' . self::DEFAULT_FORM_MESSAGE_CSS_PATH) ?: '';
348 return wp_kses($css, SIB_Manager::SIB_ATTRIBUTE);
349 }
350
351 /**
352 * Get form data of old version
353 * We suppose that the clients have got own setting values for form.
354 * If the client have default setting only then it will be return error.
355 * This function will be removed after next version
356 */
357 public static function get_old_form() {
358 // create form from old version.
359 $form_settings = get_option( 'sib_subscription_option' );
360 $html = $form_settings['sib_form_html'];
361 $avail_atts = $form_settings['available_attributes'];
362
363 $signup_settings = get_option( 'sib_signup_option' );
364 $is_confirm_email = 'yes' == $signup_settings['is_confirm_email'] ? 1 : 0;
365 $is_double_optin = 'yes' == $signup_settings['is_double_optin'] ? 1 : 0;
366 $redirect_url = $signup_settings['redirect_url'];
367 $redirect_url_click = $signup_settings['redirect_url_click'];
368 $template_id = 1 == $is_confirm_email ? $signup_settings['template_id'] : $signup_settings['doubleoptin_template_id'];
369
370 $confirmMsg = get_option( 'sib_confirm_option' );
371
372 $homeSetting = get_option( 'sib_home_option' );
373 $sib_list = maybe_serialize( array( (string) $homeSetting['list_id'] ) );
374
375 $formData = array(
376 'title' => 'Old Form',
377 'html' => $html,
378 'css' => '',
379 'dependTheme' => '1',
380 'listID' => $sib_list,
381 'templateID' => $template_id,
382 'isOpt' => $is_confirm_email,
383 'isDopt' => $is_double_optin,
384 'redirectInEmail' => $redirect_url,
385 'redirectInForm' => $redirect_url_click,
386 'successMsg' => $confirmMsg['alert_success_message'],
387 'errorMsg' => $confirmMsg['alert_error_message'],
388 'existMsg' => $confirmMsg['alert_exist_subscriber'],
389 'invalidMsg' => $confirmMsg['alert_invalid_email'],
390 'attributes' => 'email,' . implode( ',', $avail_atts ),
391 );
392
393 return $formData;
394 }
395
396 /**
397 * Add prefix to the table
398 */
399 public static function add_prefix() {
400 global $wpdb;
401 if (self::forms_table_exists()) {
402 $query = 'ALTER TABLE ' . self::TABLE_NAME . ' RENAME TO ' . $wpdb->prefix . self::TABLE_NAME . ';';
403 $wpdb->query( $query ); // db call ok; no-cache ok.
404 }
405 }
406
407 /**
408 * Change datatype of attribute column
409 */
410 public static function modify_datatype() {
411 global $wpdb;
412 if (self::forms_table_exists()) {
413 $tableStructure = $wpdb->get_results( "DESC " . $wpdb->prefix . self::TABLE_NAME );
414 foreach ($tableStructure as $key => $value)
415 {
416 if($value->Field == "attributes" && $value->Type == "varchar(255)")
417 $wpdb->query("ALTER TABLE ". $wpdb->prefix . self::TABLE_NAME." MODIFY ".$value->Field." TEXT DEFAULT NULL");
418 }
419 }
420 }
421
422 /**
423 * @return bool
424 */
425 public static function forms_table_exists()
426 {
427 global $wpdb;
428 return $wpdb->get_var( "SHOW TABLES LIKE '" . self::TABLE_NAME . "'" ) == self::TABLE_NAME;
429 }
430
431 /**
432 * @return string
433 */
434 public static function get_default_form_html()
435 {
436 return file_get_contents(__DIR__ . '/' . self::DEFAULT_FORM_HTML_PATH) ?: '';
437 }
438
439 /**
440 * @return string
441 */
442 public static function get_default_css_html()
443 {
444 return file_get_contents(__DIR__ . '/' . self::DEFAULT_FORM_CSS_PATH) ?: '';
445 }
446 }
447 }
448