-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtdev_inscriere.php
More file actions
352 lines (303 loc) · 15.8 KB
/
btdev_inscriere.php
File metadata and controls
352 lines (303 loc) · 15.8 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
/**
* BTDEV - Form Inscriere Stripe
*
* @package btdev_insciere
* @author Timotei
* @copyright 2023 Bondas
* @license GPL-2.0-or-late
*
* Plugin Name: Form Inscriere + Stripe
* Plugin URI: https://bondas.ro/
* Description: Form inscriere integrat cu Stripe
* Version: 1.0.0
* Requires at least: 6.0
* Requires PHP: 7.1
* Author: Bondas Timotei
* Author URI: https://bondas.ro/
* License: GPLv2 or later
* Text Domain: btdev_inscriere_text
* Domain Path: /languages
*/
namespace BTDEV_INSCRIERI;
use BTDEV_INSCRIERI\Exceptions\Submission as BTDEV_INSCRIERI_EXCEPTIONSSUBMISSION;
use BTDEV_INSCRIERI\Exceptions\Payment as BTDEV_INSCRIERI_EXCEPTIONSPAYMENT;
use BTDEV_INSCRIERI\Traits\Utils as BTDEV_INSCRIERI_UTILS;
use BTDEV_INSCRIERI\Traits\HtmlMessages as BTDEV_INSCRIERI_MESSAGES;
use BTDEV_INSCRIERI\Api\Tables as BTDEV_INSCRIERI_API_TABLES;
use BTDEV_INSCRIERI\Api\Entries as BTDEV_INSCRIERI_API_ENTRIES;
use BTDEV_INSCRIERI\Api\Submissions as BTDEV_INSCRIERI_API_SUBMISSIONS;
use BTDEV_INSCRIERI\Classes\Shortcodes as BTDEV_INSCRIERI_SHORTCODES;
use BTDEV_INSCRIERI\Classes\Submission as BTDEV_INSCRIERI_SUBMISSION;
use BTDEV_INSCRIERI\Classes\Tables as BTDEV_INSCRIERI_TABLES;
use BTDEV_INSCRIERI\Classes\ThirdParty\Captcha as BTDEV_INSCRIERI_THIRDPARTY_CAPTCHA;
use BTDEV_INSCRIERI\Posttypes\Form as BTDEV_INSCRIERI_POSTTYPE_FORM;
use BTDEV_INSCRIERI\Integrations\Gutenberg\Gutenberg as BTDEV_INSCRIERI_INTEGRATIONS_GUTENBERG;
use BTDEV_INSCRIERI\Integrations\Elementor\Elementor as BTDEV_INSCRIERI_INTEGRATIONS_ELEMENTOR;
if (!defined('ABSPATH')) {
exit;
}
if (session_status() !== PHP_SESSION_ACTIVE) {
session_start();
}
require_once 'vendor/autoload.php';
class Main
{
use BTDEV_INSCRIERI_UTILS;
use BTDEV_INSCRIERI_MESSAGES;
public function __construct()
{
$this->load_module_hooks();
// TODO: Install script
}
public function load_module_hooks()
{
$this->add_scripts_css();
add_action('init', array($this, 'add_post_types'));
add_action('init', array($this, 'add_shortcodes'));
add_action('init', array($this, 'add_get_actions'));
add_action('wp_footer', array($this, 'add_footer_notification'));
add_action('init', array(new BTDEV_INSCRIERI_API_TABLES(), 'add_ajax_handles'));
add_action('init', array(new BTDEV_INSCRIERI_API_ENTRIES(), 'add_ajax_handles'));
add_action('init', array(new BTDEV_INSCRIERI_API_SUBMISSIONS(), 'add_ajax_handles'));
add_action('init', array($this, 'load_integrations'));
}
public function add_scripts_css()
{
add_action('wp_enqueue_scripts', array($this, 'init_assets'));
add_action('admin_enqueue_scripts', array($this, 'admin_init_assets'));
add_action('after_setup_theme', array($this, 'after_setup_theme'));
}
public function after_setup_theme()
{
add_theme_support('editor-styles');
}
public function common_assets($is_admin = false)
{
$handle = 'btdev_inscriere_main' . ($is_admin ? '_admin' : '') . '_script';
$data = [
'ajax_url' => admin_url('admin-ajax.php'),
'translation' => [
'participant' => __('Participant', 'btdev_inscriere_text'),
'you_must_have_at_least_one_entry' => __('You must have at least one participant.', 'btdev_inscriere_text'),
'are_you_sure_you_want_to_delete' => __('Are you sure you want to delete?', 'btdev_inscriere_text'),
'please_confirm_the_action' => __('Please confirm the action.', 'btdev_inscriere_text'),
]
];
if (is_admin()) {
$data['forms'] = $this->utils_get_forms('Choose a form');
$table = new BTDEV_INSCRIERI_TABLES();
$data['tables'] = $table->get_table_types();
}
wp_localize_script($handle, $this->utils_get_plugin_js_var(), $data);
}
public function init_assets()
{
wp_enqueue_style('btdev_inscriere_datatable', '//cdn.datatables.net/2.0.1/css/dataTables.dataTables.min.css', array(), '2.0.1', 'all');
wp_enqueue_style('btdev_inscriere_main', $this->utils_get_absolute_url() . 'assets/style.css', array(), $this->utils_get_assets_version(), 'all');
wp_enqueue_script('btdev_inscriere_recaptcha', 'https://www.google.com/recaptcha/api.js', array(), '1.0', array('in_footer' => true, 'strategy' => 'async'));
wp_enqueue_script('btdev_inscriere_datatable', '//cdn.datatables.net/2.0.1/js/dataTables.min.js', array('jquery'), '2.0.1', true);
wp_enqueue_script('btdev_inscriere_datatable_responsive', 'https://cdn.datatables.net/responsive/2.5.0/js/dataTables.responsive.min.js', array('jquery'), '2.5.0', true);
wp_enqueue_script('btdev_inscriere_datatable_fixed', 'https://cdn.datatables.net/fixedheader/3.4.0/js/dataTables.fixedHeader.min.js', array('jquery'), '3.4.0', true);
wp_enqueue_script('btdev_inscriere_main_script', $this->utils_get_absolute_url() . 'assets/script.js', array('jquery'), $this->utils_get_assets_version(), true);
$this->common_assets();
if (!wp_style_is('dashicons')) {
wp_enqueue_style('dashicons');
}
}
public function admin_init_assets()
{
wp_enqueue_script('btdev_inscriere_main_admin_script', $this->utils_get_absolute_url() . 'assets/admin/script.js', array('jquery'), $this->utils_get_assets_version(), true);
// React form forms
$inc = require 'assets/admin/form_html/build/index.asset.php';
wp_enqueue_script(
'btdev_inscriere_admin_forms_form',
$this->utils_get_absolute_url() . 'assets/admin/form_html/build/index.js',
($this->utils_is_any_debug() ? $inc['dependencies'] : array()),
($this->utils_is_any_debug() ? $inc['version'] : $this->utils_get_assets_version()),
true
);
wp_enqueue_style('btdev_inscriere_admin_forms_form', $this->utils_get_absolute_url() . 'assets/admin/form_html/build/index.css', array(), $this->utils_get_assets_version(), 'all');
$this->common_assets(true);
}
public function add_footer_notification()
{
echo '<div id="btdev_popup_notifications"></div>';
}
public function add_post_types()
{
$this->add_post_type_form();
}
public function add_post_type_form()
{
new BTDEV_INSCRIERI_POSTTYPE_FORM();
}
public function add_shortcodes()
{
$shortcodes = new BTDEV_INSCRIERI_SHORTCODES();
// General
add_shortcode('bbdev_inscrieri_list_entries', array($shortcodes, 'list_entries'));
add_shortcode('bbdev_inscrieri_list_payments', array($shortcodes, 'list_payments'));
// For forms
add_shortcode('bbdev_inscrieri_form', array($shortcodes, 'form'));
// TODO: must do edit form/code
add_shortcode('bbdev_inscrieri_form_edit', array($shortcodes, 'form'));
// For email
add_shortcode('bbdev_inscrieri_entry_summary', array($shortcodes, 'submission_summary'));
}
public function add_get_actions()
{
if (isset($_POST['form_type']) && $_POST['form_type'] === 'btdev_inscrieri') {
$redirect_to = isset($_POST['current_url']) ? $_POST['current_url'] : '/';
$return_data = $_POST;
try {
if (isset($_GET[$this->utils_get_url_parameter('create')])) {
if (isset($return_data['form_name']) && $return_data['form_name'] !== '') {
// Form Class
$form_name = $return_data['form_name'];
$classname = 'BTDEV_INSCRIERI\\Forms\\Data' . ucfirst($form_name);
$form_class = new $classname();
// Create Submission class and data
$submission = new BTDEV_INSCRIERI_SUBMISSION($return_data, $form_class);
// throw new BTDEV_INSCRIERI_EXCEPTIONSSUBMISSION(__('Asa vreau', 'btdev_inscriere_text'), $redirect_to);
// CAPTCHA
if (isset($form_class->full_data['captcha']) && isset($form_class->full_data['captcha']['enabled']) && $form_class->full_data['captcha']['enabled'] === true) {
$captcha_class = new BTDEV_INSCRIERI_THIRDPARTY_CAPTCHA($return_data);
$captcha_result = $captcha_class->checkCaptcha();
if ($captcha_result['result'] !== true) {
$redirect_to = $captcha_result['redirect_to'];
throw new BTDEV_INSCRIERI_EXCEPTIONSSUBMISSION($captcha_result['message'], $redirect_to);
}
}
// Terms agree test
if (isset($form_class->full_data['tandc']) && isset($form_class->full_data['tandc']['enabled']) && $form_class->full_data['tandc']['enabled'] === true) {
if (!isset($return_data['agree_tac']) || $return_data['agree_tac'] !== 'true') {
throw new BTDEV_INSCRIERI_EXCEPTIONSSUBMISSION('You must accept terms and conditions.', $redirect_to);
}
}
// Have payment?
$redirect_to_gateway = false;
if (isset($form_class->full_data['payment']) && isset($form_class->full_data['payment']['enabled']) && $form_class->full_data['payment']['enabled'] === true) {
$classname = 'BTDEV_INSCRIERI\\Classes\\ThirdParty\\' . ucfirst($return_data['payment_name']);
if (class_exists($classname)) {
$payment_class = new $classname();
$payment_class->load_submission($submission);
$redirect_to_gateway = $payment_class->get_redirect_to_gateway();
} else {
throw new BTDEV_INSCRIERI_EXCEPTIONSSUBMISSION('Payment method not found.', $redirect_to);
}
$payment = $payment_class->prepare_payment();
foreach ($payment as $key => $field) {
$submission->set_submission_data($key, $field);
}
} else {
$submission->set_submission_data('payment_status', 'success');
}
// Save to DB
$db_data = $submission->add_to_db();
// Redirect to gateway OR site success
if ($redirect_to_gateway === true && $submission->data['payment_session_link'] !== '') {
$this->do_redirect($submission->data['payment_session_link']);
} else {
$url = $form_class->get_form_page_url('success');
$this->do_redirect($url);
}
} else {
throw new BTDEV_INSCRIERI_EXCEPTIONSSUBMISSION('No form name sent.', $redirect_to);
}
}
} catch (BTDEV_INSCRIERI_EXCEPTIONSSUBMISSION $e) {
$_SESSION['btdev_saved_data'] = $return_data;
$this->messages_add(__($e->message, 'btdev_inscriere_text'));
$this->do_redirect($e->redirect_to);
} catch (\Exception $e) {
$_SESSION['btdev_saved_data'] = $return_data;
$this->messages_add(
sprintf(
__('Unexpected exception: %s.', 'btdev_inscriere_text'),
$e->getMessage()
)
);
$this->do_redirect($redirect_to);
}
}
// Return from Stripe on cancel + embed back button
if (isset($_GET[$this->utils_get_url_parameter('payment_return')]) && isset($_GET['session_id'])) {
global $wpdb;
$session_id = $_GET['session_id'];
$table = $this->utils_get_db_tables('submission');
$submission_db = $wpdb->get_row("SELECT * FROM " . $table . " WHERE payment_session_id = '" . $session_id . "'", ARRAY_A);
try {
if ($submission_db !== null) {
$classnameForm = 'BTDEV_INSCRIERI\\Forms\\Data' . ucfirst($submission_db['form_name']);
$form_class = new $classnameForm();
$submission = new BTDEV_INSCRIERI_SUBMISSION($submission_db, $form_class);
$classname = 'BTDEV_INSCRIERI\\Classes\\ThirdParty\\' . ucfirst($submission->data['payment_name']);
if (class_exists($classname)) {
$payment_class = new $classname();
$payment_class->load_submission($submission);
$payment_class->failure_payment();
} else {
throw new BTDEV_INSCRIERI_EXCEPTIONSPAYMENT('No payment class found.');
$this->do_redirect('/');
}
} else {
throw new BTDEV_INSCRIERI_EXCEPTIONSPAYMENT('No submission found.');
$this->do_redirect('/');
}
} catch (BTDEV_INSCRIERI_EXCEPTIONSPAYMENT $e) {
$this->messages_add(__($e->message, 'btdev_inscriere_text'));
} catch (\Exception $e) {
$this->messages_add(
sprintf(
__('Unexpected exception: %s.', 'btdev_inscriere_text'),
$e->getMessage()
)
);
}
}
// Return from Stripe on success
if (isset($_GET[$this->utils_get_url_parameter('payment_ok')]) && isset($_GET['session_id'])) {
global $wpdb;
$session_id = $_GET['session_id'];
$table = $this->utils_get_db_tables('submission');
$submission_db = $wpdb->get_row("SELECT * FROM " . $table . " WHERE payment_session_id = '" . $session_id . "'", ARRAY_A);
try {
if ($submission_db !== null) {
$classnameForm = 'BTDEV_INSCRIERI\\Forms\\Data' . ucfirst($submission_db['form_name']);
$form_class = new $classnameForm();
$submission = new BTDEV_INSCRIERI_SUBMISSION($submission_db, $form_class);
$classname = 'BTDEV_INSCRIERI\\Classes\\ThirdParty\\' . ucfirst($submission->data['payment_name']);
if (class_exists($classname)) {
$payment_class = new $classname();
$payment_class->load_submission($submission);
$payment_class->success_payment();
} else {
throw new BTDEV_INSCRIERI_EXCEPTIONSPAYMENT('No payment class found.');
$this->do_redirect('/');
}
} else {
throw new BTDEV_INSCRIERI_EXCEPTIONSPAYMENT('No submission found.');
$this->do_redirect('/');
}
} catch (BTDEV_INSCRIERI_EXCEPTIONSPAYMENT $e) {
$this->messages_add(__($e->message, 'btdev_inscriere_text'));
} catch (\Exception $e) {
$this->messages_add(
sprintf(
__('Unexpected exception: %s.', 'btdev_inscriere_text'),
$e->getMessage()
)
);
}
}
}
public function load_integrations()
{
new BTDEV_INSCRIERI_INTEGRATIONS_GUTENBERG();
if (!defined('ELEMENTOR_TESTS')) {
new BTDEV_INSCRIERI_INTEGRATIONS_ELEMENTOR();
}
}
}
$bt_dev_main = new Main();