-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
308 lines (212 loc) · 8.46 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
/**
* Plugin Name: StartBlock
* Description: A set of PeakZebra blocks that helps you incrementally start a relationship with new subscribers or customers.
* Version: 1.0
* Author: PeakZebra
*/
if( !defined ( 'ABSPATH' )) exit;
// Setup
define('PZ_PLUGIN_DIR', plugin_dir_path(__FILE__));
// Includes
include( PZ_PLUGIN_DIR . 'includes/register-blocks.php');
// Hooks
add_action('init', 'pz_register');
add_action('init', 'pz_setcookie');
// add_action('wp_enqueue_scripts', 'pz_setstyle');
// If already set, read cookie
// set global variable for personID
if(isset($_COOKIE['pz_num'])) {
$pz_personID = $_COOKIE['pz_num'];
}
function pz_setstyle() {
wp_enqueue_style( 'pz_style', PZ_PLUGIN_DIR . 'build/blocks/subscribe/index.css');
}
class PZQuestions {
function __construct() {
global $wpdb;
$this->charset = $wpdb->get_charset_collate();
$this->question_tablename = $wpdb->prefix . "pz_question";
$this->startblock_tablename = $wpdb->prefix . "pz_startblock";
$this->answers_tablename = $wpdb->prefix . "pz_answers";
add_action('activate_startblock/index.php', array($this, 'onActivate'));
// add_action('admin_post_do-segment', array($this, 'do_segment'));
// add_action('admin_post_nopriv_do-segment', array($this, 'do_segment'));
add_action('admin_post_do-startblock', array($this, 'do_startblock'));
add_action('admin_post_nopriv_do-startblock', array($this, 'do_startblock'));
add_action('admin_post_do-question-block', array($this, 'do_question_block'));
add_action('admin_post_nopriv_do-question-block', array($this, 'do_question_block'));
add_shortcode( 'pz_startdata', array($this, 'do_pz_shortcode') );
}
function onActivate() {
global $wpdb;
require_once( ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta("CREATE TABLE $this->question_tablename (
responseID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
personID bigint(20) unsigned NOT NULL DEFAULT 1,
qslug varchar(30) NOT NULL DEFAULT '',
qchoice varchar(60) NOT NULL DEFAULT '',
created varchar(12) NOT NULL DEFAULT '',
PRIMARY KEY (responseID)
) $this->charset;");
dbDelta("CREATE TABLE $this->startblock_tablename (
personID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
fname varchar(60) NOT NULL DEFAULT '',
lname varchar(60) NOT NULL DEFAULT '',
email varchar(100) NOT NULL DEFAULT '',
created varchar(12) NOT NULL DEFAULT '',
PRIMARY KEY (personID)
) $this->charset;");
}
function do_pz_shortcode ($atts) {
global $wpdb;
global $pz_personID;
// if this is not a subscriber, then the ID won't be set, in which case this
// function shouldn't have been called, but just to be sure...
if( !isset($pz_personID) ) {return ("pz_none");}
// handle possibility that shortcode parameter isn't set correctly or at all
if( !isset($atts['slug'])) {
return( ">>>Shortcode must include a 'slug=value' parameter<<<" );
}
// if looking for name or email, read record from startblock table
if( $atts['slug'] == 'fname' ) {
$entries = $wpdb->get_results("SELECT * FROM $this->startblock_tablename WHERE personID = $pz_personID");
return $entries[0]->fname;
}
if( $atts['slug'] == 'lname' ) {
$entries = $wpdb->get_results("SELECT * FROM $this->startblock_tablename WHERE personID = $pz_personID");
return $entries[0]->lname;
}
if( $atts['slug'] == 'email' ) {
$entries = $wpdb->get_results("SELECT * FROM $this->startblock_tablename WHERE personID = $pz_personID");
return $entries[0]->email;
}
// else, read any answer settings we have for the current user
$entries = $wpdb->get_results("SELECT * FROM $this->question_tablename WHERE personID = $pz_personID");
$found = false;
ob_start();
foreach($entries as $entry) {
if( $atts['slug'] == $entry->qslug) {
echo $entry->qchoice;
$found = true;
}
}
if( !$found ) echo ">>> Slug parameter not found <<<";
return ob_get_clean();
}
function do_question_block () {
global $wpdb;
$created = date("m/j/Y");
$personID = 1;
// Check if cookie is already set
if(isset($_COOKIE['pz_num'])) {
$personID = $_COOKIE['pz_num'];
}
$item = [];
$item['personID'] = $personID; // already sanitized above
$item['qslug'] = sanitize_text_field($_POST['qslug']);
$item['qchoice'] = sanitize_text_field($_POST['qchoice']);
$item['created']= $created;
if( $wpdb->insert($this->question_tablename, $item ) <= 0 ) {
var_dump( $wpdb );
exit;
}
wp_redirect('/');
exit;
}
function do_startblock () {
global $wpdb;
$created = date("m/j/Y");
$item = [];
$item['personID'] = null;
$item['fname'] = sanitize_text_field($_POST['fname']);
$item['lname'] = sanitize_text_field($_POST['lname']);
$item['email'] = sanitize_text_field($_POST['email']);
$item['created']= $created;
if( $wpdb->insert($this->startblock_tablename, $item ) <= 0 ) {
var_dump( $wpdb );
exit;
}
$pz_id = $wpdb->insert_id; // this is the id number of the record we just inserted
// setcookie('pz_num', $pz_id, time()+31556926);
wp_redirect('/?pznum=' . $pz_id);
exit;
}
}
function pz_setcookie () {
if(isset($_GET['pznum'])) {
setcookie('pz_num', sanitize_text_field($_GET['pznum']), time()+31556926);
}
}
function basement() {
if(isset($_COOKIE['pz_num'])) {
$personID = $_COOKIE['pz_num'];
// read in the record associated with this id
$item = $wpdb->get_results("SELECT * FROM $this->startblock_tablename WHERE personID = $personID");
}
}
add_action("init", "download_csv");
function download_csv() {
// XXX add admin only
if (isset($_POST['download_csv'])) {
global $wpdb;
$sql = "SELECT * FROM {$wpdb->prefix}pz_startblock";
$rows = $wpdb->get_results($sql, 'ARRAY_A');
if ($rows) {
$csv_fields = array();
$csv_fields[] = "First";
$csv_fields[] = 'Last';
$csv_fields[] = 'Email';
$output_filename = 'file_name' .'.csv';
$output_handle = @fopen('php://output', 'w');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Description: File Transfer');
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename=' .
$output_filename);
header('Expires: 0');
header('Pragma: public');
$first = true;
// Parse results to csv format
foreach ($rows as $row) {
// Add table headers
if ($first) {
$titles = array();
foreach ($row as $key => $val) {
$titles[] = $key;
}
fputcsv($output_handle, $titles);
$first = false;
}
$leadArray = (array) $row; // Cast the Object to an array
// Add row to file
fputcsv($output_handle, $leadArray);
}
//echo '<a href="'.$output_handle.'">test</a>';
// Close output file stream
fclose($output_handle);
die();
}
}
}
add_action("init", "drop_start_table");
function drop_start_table() {
// XXX add admin only
if (isset($_POST['unload'])) {
global $wpdb;
$table_name = $wpdb->prefix . "pz_startblock";
$sql = "TRUNCATE TABLE $table_name";
$wpdb->query($sql);
}
}
$pzQuestions = new PZQuestions();
function pz_get_cookie_script() {
wp_enqueue_script(
"pz_get_cookie",
plugins_url("/src/blocks/subscribe/pz_get_cookie.js", __FILE__),
array(),
"1.0.0",
false
);
}
add_action("wp_enqueue_scripts", "pz_get_cookie_script");