-
Notifications
You must be signed in to change notification settings - Fork 3
/
divi-helpers.php
319 lines (272 loc) · 8.06 KB
/
divi-helpers.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
309
310
311
312
313
314
315
316
317
318
319
<?php
namespace Uncanny_Automator;
use Uncanny_Automator_Pro\Divi_Pro_Helpers;
/**
* Divi integration helper file
*/
class Divi_Helpers {
/**
* Store Divi options
*
* @var Divi_Helpers
*/
public $options;
public $load_options = true;
/**
* Store Divi Pro Helper instance
*
* @var Divi_Pro_Helpers
*/
public $pro;
/**
* Divi_Helpers constructor.
*/
public function __construct() {
}
/**
* Set Divi options
*
* @param Divi_Helpers $options
*/
public function setOptions( Divi_Helpers $options ) { //phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
$this->options = $options;
}
/**
* Set Divi Pro Helper instance
*
* @param Divi_Pro_Helpers $pro
*/
public function setPro( Divi_Pro_Helpers $pro ) { //phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
$this->pro = $pro;
}
/**
* Fetch all Divi forms
*
* @param string $label
* @param string $option_code
* @param array $args
*
* @return mixed
*/
public function all_divi_forms( $label = null, $option_code = 'DIVIMFORMS', $args = array() ) {
$label = null === $label ? esc_attr__( 'Form', 'uncanny-automator' ) : $label;
$args = wp_parse_args(
$args,
array(
'uo_include_any' => false,
'uo_any_label' => esc_attr__( 'Any form', 'uncanny-automator' ),
)
);
$token = key_exists( 'token', $args ) ? $args['token'] : false;
$is_ajax = key_exists( 'is_ajax', $args ) ? $args['is_ajax'] : false;
$target_field = key_exists( 'target_field', $args ) ? $args['target_field'] : '';
$end_point = key_exists( 'endpoint', $args ) ? $args['endpoint'] : '';
$options = array();
$option = array(
'option_code' => $option_code,
'label' => $label,
'input_type' => 'select',
'required' => true,
'supports_tokens' => $token,
'is_ajax' => $is_ajax,
'fill_values_in' => $target_field,
'endpoint' => $end_point,
'options' => $options,
);
if ( ! Automator()->helpers->recipe->load_helpers ) {
return apply_filters( 'uap_option_all_divi_forms', $option );
}
// if ( $args['uo_include_any'] ) {
// $options['-1'] = $args['uo_any_label'];
// }
$data = self::extract_forms();
if ( $data ) {
foreach ( $data as $form_id => $d ) {
$options[ $form_id ] = $d['title'];
}
}
$option['options'] = $options;
return apply_filters( 'uap_option_all_divi_forms', $option );
}
/**
* Pseudo function copied from Divi
*
* @param $array
* @param $address
* @param string $default
*
* @return mixed|string
*/
public static function array_get( $array, $address, $default = '' ) {
$keys = is_array( $address ) ? $address : explode( '.', $address );
$value = $array;
foreach ( $keys as $key ) {
if ( ! empty( $key ) && isset( $key[0] ) && '[' === $key[0] ) {
$index = substr( $key, 1, - 1 );
if ( is_numeric( $index ) ) {
$key = (int) $index;
}
}
if ( ! isset( $value[ $key ] ) ) {
return $default;
}
$value = $value[ $key ];
}
return $value;
}
/**
* Extract form info from the Divi shortcode
*
* @return array
*/
public static function extract_forms() {
global $wpdb;
$form_posts = $wpdb->get_results( $wpdb->prepare( "SELECT `ID`, `post_content`, `post_title` FROM $wpdb->posts WHERE post_status NOT IN('trash', 'inherit', 'auto-draft') AND post_type IS NOT NULL AND post_type NOT LIKE %s AND post_content LIKE %s", 'revision', '%%et_pb_contact_form%%' ) );
$data = array();
if ( empty( $form_posts ) ) {
return $data;
}
foreach ( $form_posts as $form_post ) {
// Get forms
$pattern_regex = '/\[et_pb_contact_form(.*?)](.+?)\[\/et_pb_contact_form]/';
preg_match_all( $pattern_regex, $form_post->post_content, $forms, PREG_SET_ORDER );
if ( empty( $forms ) ) {
continue;
}
$jjj = 0;
foreach ( $forms as $form ) {
$pattern_form = get_shortcode_regex( array( 'et_pb_contact_form' ) );
preg_match_all( "/$pattern_form/", $form[0], $forms_extracted, PREG_SET_ORDER );
if ( empty( $forms_extracted ) ) {
continue;
}
foreach ( $forms_extracted as $form_extracted ) {
$form_attrs = shortcode_parse_atts( $form_extracted[3] );
$form_id = isset( $form_attrs['_unique_id'] ) ? $form_attrs['_unique_id'] : '';
if ( empty( $form_id ) ) {
continue;
}
$form_id = sprintf( '%d-%s', $form_post->ID, $form_id );
$form_title = isset( $form_attrs['title'] ) ? $form_attrs['title'] : __( 'No form title', 'uncanny-automator' );
$form_title = sprintf( '%s - %s', $form_post->post_title, $form_title );
$fields = self::extract_fields( $form[0] );
$data[ $form_id ]['title'] = $form_title;
$data[ $form_id ]['fields'] = $fields;
}
$jjj ++;
}
}
return $data;
}
/**
* Extracting fields from the form shortcode
*
* @param $content_shortcode
*
* @return array
*/
public static function extract_fields( $content_shortcode ) {
$fields = array();
$pattern = get_shortcode_regex( array( 'et_pb_contact_field' ) );
preg_match_all( "/$pattern/", $content_shortcode, $contact_fields, PREG_SET_ORDER );
if ( empty( $contact_fields ) ) {
return $fields;
}
foreach ( $contact_fields as $contact_field ) {
$contact_field_attrs = shortcode_parse_atts( $contact_field[3] );
$field_id = strtolower( self::array_get( $contact_field_attrs, 'field_id' ) );
$fields[] = array(
'field_title' => self::array_get( $contact_field_attrs, 'field_title', __( 'No title', 'uncanny-automator' ) ),
'field_type' => self::array_get( $contact_field_attrs, 'field_type', 'text' ),
'field_id' => $field_id,
'required_mark' => self::array_get( $contact_field_attrs, 'required_mark', 'on' ),
);
}
return $fields;
}
/**
* Select form by ID
*
* @param $form_id
*
* @return array|mixed
*/
public static function get_form_by_id( $form_id ) {
$forms = self::extract_forms();
if ( empty( $forms ) ) {
return array();
}
foreach ( $forms as $_form_id => $d ) {
if ( (string) $_form_id === (string) $form_id ) {
return $d['fields'];
}
}
return array();
}
/**
* Match conditions in Divi triggers
*
* @param $form_id
* @param null $recipes
* @param null $trigger_meta
*
* @return array|false
*/
public static function match_condition( $form_id, $recipes = null, $trigger_meta = null ) {
if ( empty( $recipes ) ) {
return false;
}
$recipe_ids = array();
$entry_to_match = $form_id;
foreach ( $recipes as $recipe ) {
foreach ( $recipe['triggers'] as $trigger ) {
if ( ! array_key_exists( $trigger_meta, $trigger['meta'] ) ) {
continue;
}
if ( ( (string) $trigger['meta'][ $trigger_meta ] === (string) $entry_to_match ) || ( intval( '-1' ) === intval( $trigger['meta'][ $trigger_meta ] ) ) ) {
$recipe_ids[ $recipe['ID'] ] = $recipe['ID'];
}
}
}
if ( ! empty( $recipe_ids ) ) {
return array(
'recipe_ids' => $recipe_ids,
'result' => true,
);
}
return false;
}
/**
* Saving tokens
*
* @param $result
* @param $fields_values
* @param $form_id
* @param $trigger_meta
* @param $user_id
*/
public static function save_tokens( $result, $fields_values, $form_id, $trigger_meta, $user_id ) {
if ( empty( $result ) ) {
return;
}
if ( false === $result['result'] ) {
return;
}
$all_fields = array();
foreach ( $fields_values as $k => $v ) {
$field_id = "$form_id|$k";
$all_fields[ $field_id ] = $v['value'];
}
$trigger_id = $result['args']['trigger_id'];
$trigger_log_id = absint( $result['args']['trigger_log_id'] );
$run_number = absint( $result['args']['run_number'] );
$meta_key = sprintf( '%d:%s', $trigger_id, $trigger_meta );
$trigger_meta = array(
'user_id' => $user_id,
'trigger_id' => $trigger_id,
'trigger_log_id' => $trigger_log_id,
'run_number' => $run_number,
);
Automator()->db->token->save( $meta_key, maybe_serialize( $all_fields ), $trigger_meta );
}
}