| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package AutomatorWP\Brizy\Functions |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
// Exit if accessed directly |
| 9 |
if( !defined( 'ABSPATH' ) ) exit; |
| 10 |
|
| 11 |
/** |
| 12 |
* Options callback for select2 fields assigned to forms |
| 13 |
* |
| 14 |
* @since 1.0.0 |
| 15 |
* |
| 16 |
* @param stdClass $field |
| 17 |
* |
| 18 |
* @return array |
| 19 |
*/ |
| 20 |
function automatorwp_brizy_options_cb_form( $field ) { |
| 21 |
|
| 22 |
// Setup vars |
| 23 |
$value = $field->escaped_value; |
| 24 |
$none_value = 'any'; |
| 25 |
$none_label = __( 'any form', 'automatorwp-brizy' ); |
| 26 |
$options = automatorwp_options_cb_none_option( $field, $none_value, $none_label ); |
| 27 |
|
| 28 |
if( ! empty( $value ) ) { |
| 29 |
if( ! is_array( $value ) ) { |
| 30 |
$value = array( $value ); |
| 31 |
} |
| 32 |
|
| 33 |
foreach( $value as $form_id ) { |
| 34 |
|
| 35 |
// Skip option none |
| 36 |
if( $form_id === $none_value ) { |
| 37 |
continue; |
| 38 |
} |
| 39 |
|
| 40 |
$options[$form_id] = automatorwp_brizy_get_form_title( $form_id ); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
return $options; |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get all forms |
| 50 |
* |
| 51 |
* @since 1.0.0 |
| 52 |
* |
| 53 |
* @return string|null |
| 54 |
*/ |
| 55 |
function automatorwp_brizy_get_forms( ) { |
| 56 |
|
| 57 |
global $wpdb; |
| 58 |
|
| 59 |
$all_forms = array(); |
| 60 |
|
| 61 |
$forms = $wpdb->get_results( |
| 62 |
$wpdb->prepare( |
| 63 |
"SELECT ID, post_title, post_content |
| 64 |
FROM {$wpdb->posts} |
| 65 |
WHERE post_content LIKE %s |
| 66 |
AND post_status = 'publish' |
| 67 |
AND post_type IN ('post', 'page')", |
| 68 |
'%' . $wpdb->esc_like('data-brz-form-id') . '%' |
| 69 |
) |
| 70 |
); |
| 71 |
|
| 72 |
foreach ( $forms as $post ) { |
| 73 |
|
| 74 |
$form_title = $post->post_title; |
| 75 |
$form_content = $post->post_content; |
| 76 |
|
| 77 |
preg_match_all( '/data-brz-form-id="([^"]+)"/', $form_content, $matches ); |
| 78 |
$form_ids = array_unique($matches[1]); |
| 79 |
|
| 80 |
foreach ( $form_ids as $id ) { |
| 81 |
$all_forms[] = array( |
| 82 |
'id' => $id, |
| 83 |
'name' => $form_title . ' - ' . $id, |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
return $all_forms; |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Get the form title |
| 96 |
* |
| 97 |
* @since 1.0.0 |
| 98 |
* |
| 99 |
* @param int $form_id |
| 100 |
* |
| 101 |
* @return string|null |
| 102 |
*/ |
| 103 |
function automatorwp_brizy_get_form_title( $form_id ) { |
| 104 |
|
| 105 |
global $wpdb; |
| 106 |
|
| 107 |
// Empty title if no ID provided |
| 108 |
if( empty( $form_id ) ) { |
| 109 |
return ''; |
| 110 |
} |
| 111 |
|
| 112 |
$post_title = $wpdb->get_var( |
| 113 |
$wpdb->prepare( |
| 114 |
"SELECT post_title |
| 115 |
FROM {$wpdb->posts} |
| 116 |
WHERE post_content LIKE %s |
| 117 |
AND post_status = 'publish' |
| 118 |
AND post_type IN ('post', 'page') |
| 119 |
LIMIT 1", |
| 120 |
'%' . $wpdb->esc_like($form_id) . '%' |
| 121 |
) |
| 122 |
); |
| 123 |
|
| 124 |
return $post_title . ' - ' . $form_id; |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Get form fields values |
| 130 |
* |
| 131 |
* @since 1.0.0 |
| 132 |
* |
| 133 |
* @param array $fields |
| 134 |
* |
| 135 |
* @return array |
| 136 |
*/ |
| 137 |
function automatorwp_brizy_get_form_fields_values( $fields ) { |
| 138 |
|
| 139 |
$form_fields = array(); |
| 140 |
|
| 141 |
// Loop all fields |
| 142 |
foreach ( $fields as $field_name => $field_value ) { |
| 143 |
|
| 144 |
$field_name = $field_value->label; |
| 145 |
$value = ( isset( $field_value->value ) ? $field_value->value : '' ); |
| 146 |
|
| 147 |
$form_fields[$field_name] = $value; |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
// Check for AutomatorWP 1.4.4 |
| 153 |
if( function_exists( 'automatorwp_utilities_pull_array_values' ) ) { |
| 154 |
$form_fields = automatorwp_utilities_pull_array_values( $form_fields ); |
| 155 |
} |
| 156 |
|
| 157 |
return $form_fields; |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Custom tags replacements |
| 163 |
* |
| 164 |
* @since 1.0.0 |
| 165 |
* |
| 166 |
* @param string $parsed_content Content parsed |
| 167 |
* @param array $replacements Automation replacements |
| 168 |
* @param int $automation_id The automation ID |
| 169 |
* @param int $user_id The user ID |
| 170 |
* @param string $content The content to parse |
| 171 |
* |
| 172 |
* @return string |
| 173 |
*/ |
| 174 |
function automatorwp_brizy_parse_automation_tags( $parsed_content, $replacements, $automation_id, $user_id, $content ) { |
| 175 |
|
| 176 |
$new_replacements = array(); |
| 177 |
|
| 178 |
// Get automation triggers to pass their tags |
| 179 |
$triggers = automatorwp_get_automation_triggers( $automation_id ); |
| 180 |
|
| 181 |
foreach( $triggers as $trigger ) { |
| 182 |
|
| 183 |
$trigger_args = automatorwp_get_trigger( $trigger->type ); |
| 184 |
|
| 185 |
// Skip if trigger is not from this integration |
| 186 |
if( $trigger_args['integration'] !== 'brizy' ) { |
| 187 |
continue; |
| 188 |
} |
| 189 |
|
| 190 |
// Get the last trigger log (where data for tags replacement will be get |
| 191 |
$log = automatorwp_get_user_last_completion( $trigger->id, $user_id, 'trigger' ); |
| 192 |
|
| 193 |
if( ! $log ) { |
| 194 |
continue; |
| 195 |
} |
| 196 |
|
| 197 |
ct_setup_table( 'automatorwp_logs' ); |
| 198 |
$form_fields = ct_get_object_meta( $log->id, 'form_fields', true ); |
| 199 |
ct_reset_setup_table(); |
| 200 |
|
| 201 |
// Skip if not form fields |
| 202 |
if( ! is_array( $form_fields ) ) { |
| 203 |
continue; |
| 204 |
} |
| 205 |
|
| 206 |
// Look for form field tags |
| 207 |
preg_match_all( "/\{t:" . $trigger->id . ":form_field:\s*(.*?)\s*\}/", $parsed_content, $matches ); |
| 208 |
|
| 209 |
if( is_array( $matches ) && isset( $matches[1] ) ) { |
| 210 |
|
| 211 |
foreach( $matches[1] as $field_name ) { |
| 212 |
// Replace {t:ID:form_field:NAME} by the field value |
| 213 |
if( isset( $form_fields[$field_name] ) ) { |
| 214 |
$new_replacements['{t:' . $trigger->id . ':form_field:' . $field_name . '}'] = $form_fields[$field_name]; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
// Look for form field tags |
| 221 |
preg_match_all( "/\{" . $trigger->id . ":form_field:\s*(.*?)\s*\}/", $parsed_content, $matches ); |
| 222 |
|
| 223 |
if( is_array( $matches ) && isset( $matches[1] ) ) { |
| 224 |
|
| 225 |
foreach( $matches[1] as $field_name ) { |
| 226 |
// Replace {ID:form_field:NAME} by the field value |
| 227 |
if( isset( $form_fields[$field_name] ) ) { |
| 228 |
$new_replacements['{' . $trigger->id . ':form_field:' . $field_name . '}'] = $form_fields[$field_name]; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
} |
| 233 |
|
| 234 |
} |
| 235 |
|
| 236 |
if( count( $new_replacements ) ) { |
| 237 |
|
| 238 |
$tags = array_keys( $new_replacements ); |
| 239 |
|
| 240 |
// Replace all tags by their replacements |
| 241 |
$parsed_content = str_replace( $tags, $new_replacements, $parsed_content ); |
| 242 |
|
| 243 |
} |
| 244 |
|
| 245 |
return $parsed_content; |
| 246 |
|
| 247 |
} |
| 248 |
add_filter( 'automatorwp_parse_automation_tags', 'automatorwp_brizy_parse_automation_tags', 10, 5 ); |
| 249 |
|