|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* BetterOptin Misc Admin Functions |
|
4
|
|
|
* |
|
5
|
|
|
* @package BetterOptin/Failsafe |
|
6
|
|
|
* @author ThemeAvenue <[email protected]> |
|
7
|
|
|
* @license GPL-2.0+ |
|
8
|
|
|
* @link http://themeavenue.net |
|
9
|
|
|
* @copyright 2015 ThemeAvenue |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// If this file is called directly, abort. |
|
13
|
|
|
if ( ! defined( 'WPINC' ) ) { |
|
14
|
|
|
die; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Create failsafe table. |
|
19
|
|
|
* |
|
20
|
|
|
* @since 2.0 |
|
21
|
|
|
* @return void |
|
22
|
|
|
*/ |
|
23
|
|
View Code Duplication |
function wpbo_failsafe_create_table() { |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
global $wpdb; |
|
26
|
|
|
|
|
27
|
|
|
$table = wpbo_failsafe_table; |
|
28
|
|
|
|
|
29
|
|
|
/* Prepare DB structure if not already existing */ |
|
30
|
|
|
if ( $wpdb->get_var( "show tables like '$table'" ) != $table ) { |
|
31
|
|
|
|
|
32
|
|
|
$sql = "CREATE TABLE $table ( |
|
33
|
|
|
ID mediumint(9) NOT NULL AUTO_INCREMENT, |
|
34
|
|
|
conversion_id mediumint(9) NOT NULL, |
|
35
|
|
|
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, |
|
36
|
|
|
first_name VARCHAR(20) COLLATE utf8_general_ci NOT NULL, |
|
37
|
|
|
last_name VARCHAR(20) COLLATE utf8_general_ci NOT NULL, |
|
38
|
|
|
email VARCHAR(20) COLLATE utf8_general_ci NOT NULL, |
|
39
|
|
|
provider VARCHAR(20) COLLATE utf8_general_ci NOT NULL, |
|
40
|
|
|
status VARCHAR(20) COLLATE utf8_general_ci NOT NULL, |
|
41
|
|
|
UNIQUE KEY ID (ID) |
|
42
|
|
|
);"; |
|
43
|
|
|
|
|
44
|
|
|
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
|
45
|
|
|
dbDelta( $sql ); |
|
46
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Insert new subscriber. |
|
53
|
|
|
* |
|
54
|
|
|
* Add a new row of data in the failsafe table. |
|
55
|
|
|
* |
|
56
|
|
|
* @since 2.0 |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $data Details of the subscriber |
|
59
|
|
|
* @param boolean $wp_error Allow the function to return a WP_Error object |
|
60
|
|
|
* |
|
61
|
|
|
* @return mixed Subscriber ID on success or WP_Error on failure |
|
62
|
|
|
*/ |
|
63
|
|
|
function wpbo_failsafe_add_subscriber( $data = array(), $wp_error = true ) { |
|
64
|
|
|
|
|
65
|
|
|
global $wpdb; |
|
66
|
|
|
|
|
67
|
|
|
$table_name = wpbo_failsafe_table; |
|
68
|
|
|
|
|
69
|
|
|
$defaults = array( |
|
70
|
|
|
'ID' => false, |
|
71
|
|
|
'conversion_id' => 0, |
|
72
|
|
|
'time' => '', |
|
73
|
|
|
'first_name' => '', |
|
74
|
|
|
'last_name' => '', |
|
75
|
|
|
'email' => '', |
|
76
|
|
|
'provider' => '', |
|
77
|
|
|
'status' => '', |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
$data = array_merge( $defaults, $data ); |
|
81
|
|
|
|
|
82
|
|
View Code Duplication |
if ( empty( $data['time'] ) || '0000-00-00 00:00:00' == $data['time'] ) { |
|
|
|
|
|
|
83
|
|
|
$data['time'] = current_time( 'mysql' ); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Validate the date |
|
88
|
|
|
*/ |
|
89
|
|
|
$valid_date = wpbo_check_date( $data['time'] ); |
|
90
|
|
|
|
|
91
|
|
View Code Duplication |
if ( ! $valid_date ) { |
|
|
|
|
|
|
92
|
|
|
if ( $wp_error ) { |
|
93
|
|
|
return new WP_Error( 'invalid_date', __( 'Whoops, the provided date is invalid.' ) ); |
|
94
|
|
|
} else { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// Make sure we have a provider |
|
100
|
|
|
if ( empty( $data['provider'] ) ) { |
|
101
|
|
|
$provider = str_replace( ' ', '', ucwords( str_replace( array( '-', '_' ), ' ', sanitize_text_field( wpbo_get_option( 'mailing_provider', '' ) ) ) ) ); |
|
102
|
|
|
$data['provider'] = $provider; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// Set the status as failed by default |
|
106
|
|
|
if ( empty( $data['status'] ) ) { |
|
107
|
|
|
$data['status'] = 'failed'; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/* Sanitize all data values */ |
|
111
|
|
|
$data = array_map( 'sanitize_text_field', $data ); |
|
112
|
|
|
|
|
113
|
|
|
$insert = $wpdb->insert( $table_name, $data, array( '%s', '%d', '%d', '%s', '%s', '%s', '%s', '%s' ) ); |
|
114
|
|
|
|
|
115
|
|
View Code Duplication |
if ( false === $insert ) { |
|
|
|
|
|
|
116
|
|
|
if ( $wp_error ) { |
|
117
|
|
|
return new WP_Error( 'insert_failed', __( 'Whoops, we could not insert the data in the database.' ) ); |
|
118
|
|
|
} else { |
|
119
|
|
|
return false; |
|
120
|
|
|
} |
|
121
|
|
|
} else { |
|
122
|
|
|
return $wpdb->insert_id; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.