| 1 |
<?php |
| 2 |
/** |
| 3 |
* Automation UI |
| 4 |
* |
| 5 |
* @package AutomatorWP\Automation_UI |
| 6 |
* @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Render automation type dialog |
| 14 |
* |
| 15 |
* @since 1.3.0 |
| 16 |
*/ |
| 17 |
function automatorwp_render_automation_type_dialog() { |
| 18 |
|
| 19 |
if( ! isset( $_GET['page'] ) ) { |
| 20 |
return; |
| 21 |
} |
| 22 |
|
| 23 |
$allowed_pages = array( |
| 24 |
'automatorwp_automations', |
| 25 |
'edit_automatorwp_automations', |
| 26 |
); |
| 27 |
|
| 28 |
// Only render on allowed pages |
| 29 |
if( ! in_array( $_GET['page'], $allowed_pages ) ) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
$types = automatorwp_get_automation_types(); ?> |
| 34 |
|
| 35 |
<div class="automatorwp-automation-type-dialog-wrapper" style="display: none;"> |
| 36 |
|
| 37 |
<div class="automatorwp-automation-type-dialog"> |
| 38 |
<h2 class="automatorwp-automation-type-dialog-title"><?php esc_html_e( 'Automation type', 'automatorwp' ); ?></h2> |
| 39 |
<div class="automatorwp-automation-types"> |
| 40 |
<?php foreach( $types as $type => $args ) : |
| 41 |
if( in_array( $type, automatorwp_get_automation_loop_types() ) ) continue; ?> |
| 42 |
<div class="automatorwp-automation-type automatorwp-automation-type-<?php echo $type; ?>" data-type="<?php echo $type; ?>"> |
| 43 |
<img src="<?php echo $args['image']; ?>" alt="<?php echo $args['label']; ?>"> |
| 44 |
<div class="automatorwp-automation-type-description"> |
| 45 |
<strong><?php echo $args['label']; ?></strong> |
| 46 |
<span><?php echo $args['desc']; ?></span> |
| 47 |
</div> |
| 48 |
</div> |
| 49 |
<?php endforeach; ?> |
| 50 |
</div> |
| 51 |
<h3 class="automatorwp-automation-type-dialog-automation-loop-title"> |
| 52 |
<?php esc_html_e( 'Loop Automations', 'automatorwp' ); ?> |
| 53 |
<span class="cmb-tooltip"> |
| 54 |
<i class="dashicons dashicons-cmb-tooltip"></i> |
| 55 |
<span class="cmb-tooltip-desc cmb-tooltip-top"><?php _e( 'Designed to run actions on a <b>group of elements</b>. Can be run <b>manually</b>, on a <b>specific date</b> or on a <b>recurring</b> basis.', 'automatorwp' ); ?></span> |
| 56 |
</span> |
| 57 |
</h3> |
| 58 |
<div class="automatorwp-automation-types automatorwp-automation-loop-types"> |
| 59 |
<?php foreach( $types as $type => $args ) : |
| 60 |
if( ! in_array( $type, automatorwp_get_automation_loop_types() ) ) continue; ?> |
| 61 |
<div class="automatorwp-automation-type automatorwp-automation-type-<?php echo $type; ?>" data-type="<?php echo $type; ?>"> |
| 62 |
<img src="<?php echo $args['image']; ?>" alt="<?php echo $args['label']; ?>"> |
| 63 |
<div class="automatorwp-automation-type-description"> |
| 64 |
<strong><?php echo $args['label']; ?></strong> |
| 65 |
<span><?php echo $args['desc']; ?></span> |
| 66 |
</div> |
| 67 |
</div> |
| 68 |
<?php endforeach; ?> |
| 69 |
</div> |
| 70 |
<div class="automatorwp-notice-warning"> |
| 71 |
<?php echo __( '<strong>Note:</strong> Automation type cannot be changed later. ', 'automatorwp' ); ?> |
| 72 |
</div> |
| 73 |
<div class="automatorwp-dialog-bottom automatorwp-automation-type-dialog-bottom"> |
| 74 |
<div class="automatorwp-dialog-buttons automatorwp-automation-type-dialog-buttons"> |
| 75 |
<button type="button" class="button button-primary automatorwp-automation-type-dialog-confirm"><?php _e( 'Confirm', 'automatorwp' ); ?></button> |
| 76 |
<button type="button" data-dialog="automatorwp-automation-type-dialog" class="button automatorwp-button-danger automatorwp-dialog-cancel automatorwp-automation-type-dialog-cancel"><?php _e( 'Cancel', 'automatorwp' ); ?></button> |
| 77 |
</div> |
| 78 |
</div> |
| 79 |
</div> |
| 80 |
|
| 81 |
</div> |
| 82 |
|
| 83 |
<?php |
| 84 |
|
| 85 |
} |
| 86 |
add_action( 'admin_footer', 'automatorwp_render_automation_type_dialog' ); |
| 87 |
|
| 88 |
/** |
| 89 |
* Render automation url export dialog |
| 90 |
* |
| 91 |
* @since 1.4.8 |
| 92 |
*/ |
| 93 |
function automatorwp_render_automation_url_export_dialog() { |
| 94 |
|
| 95 |
if( ! isset( $_GET['page'] ) ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
$allowed_pages = array( |
| 100 |
'automatorwp_automations', |
| 101 |
'edit_automatorwp_automations', |
| 102 |
); |
| 103 |
|
| 104 |
// Only render on allowed pages |
| 105 |
if( ! in_array( $_GET['page'], $allowed_pages ) ) { |
| 106 |
return; |
| 107 |
} ?> |
| 108 |
|
| 109 |
<div class="automatorwp-automation-url-export-dialog-wrapper" style="display: none;"> |
| 110 |
|
| 111 |
<div class="automatorwp-automation-url-export-dialog"> |
| 112 |
<h2><?php _e( 'Export automation', 'automatorwp' ); ?></h2> |
| 113 |
<div class="automatorwp-automation-url-export-dialog-input"> |
| 114 |
<label><?php _e( 'Enter you website URL:', 'automatorwp' ); ?></label> |
| 115 |
<input type="text" placeholder="https://website.com"> |
| 116 |
<div class="automatorwp-notice-error" style="display: none;"><?php _e( 'Please, enter a valid URL.', 'automatorwp' ); ?></div> |
| 117 |
</div> |
| 118 |
<div class="automatorwp-automation-url-export-dialog-url"> |
| 119 |
<label><?php _e( 'Exportable URL:', 'automatorwp' ); ?></label> |
| 120 |
<input type="text"> |
| 121 |
<div class="automatorwp-automation-url-export-dialog-url-copy"> |
| 122 |
<div class="automatorwp-automation-url-export-dialog-url-copy-icon"><span class="dashicons dashicons-admin-page"></span></div> |
| 123 |
<div class="automatorwp-automation-url-export-dialog-url-copy-text"> |
| 124 |
<span class="automatorwp-automation-url-export-dialog-url-copy-text-copy"><?php _e( 'Copy URL', 'automatorwp' ); ?></span> |
| 125 |
<span class="automatorwp-automation-url-export-dialog-url-copy-text-copied" style="display: none;"><?php _e( 'Copied!', 'automatorwp' ); ?></span> |
| 126 |
</div> |
| 127 |
</div> |
| 128 |
<span class="automatorwp-automation-url-export-dialog-desc"><?php _e( 'Copy this URL and place it on your browser to automatically import this automation on your website.', 'automatorwp' ); ?></span> |
| 129 |
</div> |
| 130 |
<div class="automatorwp-dialog-bottom automatorwp-automation-url-export-dialog-bottom"> |
| 131 |
<div class="automatorwp-dialog-buttons automatorwp-automation-url-export-dialog-buttons"> |
| 132 |
<button type="button" class="button button-primary automatorwp-automation-url-export-dialog-confirm"> |
| 133 |
<span class="automatorwp-automation-url-export-dialog-confirm-default-text"><?php _e( 'Generate URL to export', 'automatorwp' ); ?></span> |
| 134 |
<span class="automatorwp-automation-url-export-dialog-confirm-generating-text" style="display: none;"><?php _e( 'Generating URL', 'automatorwp' ); ?>...</span> |
| 135 |
<span class="automatorwp-automation-url-export-dialog-confirm-success-text" style="display: none;"><?php _e( 'Done', 'automatorwp' ); ?></span> |
| 136 |
</button> |
| 137 |
<button type="button" data-dialog="automatorwp-automation-url-export-dialog" class="button automatorwp-button-danger automatorwp-dialog-cancel automatorwp-automation-url-export-dialog-cancel"><?php _e( 'Cancel', 'automatorwp' ); ?></button> |
| 138 |
</div> |
| 139 |
</div> |
| 140 |
</div> |
| 141 |
|
| 142 |
</div> |
| 143 |
|
| 144 |
<?php |
| 145 |
|
| 146 |
} |
| 147 |
add_action( 'admin_footer', 'automatorwp_render_automation_url_export_dialog' ); |
| 148 |
|
| 149 |
/** |
| 150 |
* Render automation type dialog |
| 151 |
* |
| 152 |
* @since 1.3.0 |
| 153 |
* |
| 154 |
* @param string $classes |
| 155 |
* |
| 156 |
* @return string |
| 157 |
*/ |
| 158 |
function automatorwp_automation_ui_admin_body_class( $classes ) { |
| 159 |
|
| 160 |
global $ct_table; |
| 161 |
|
| 162 |
if( ! isset( $_GET['page'] ) ) { |
| 163 |
return $classes; |
| 164 |
} |
| 165 |
|
| 166 |
// Only add class on allowed pages |
| 167 |
if( $_GET['page'] !== 'edit_automatorwp_automations' ) { |
| 168 |
return $classes; |
| 169 |
} |
| 170 |
|
| 171 |
if( $ct_table->name !== 'automatorwp_automations' ) { |
| 172 |
return $classes; |
| 173 |
} |
| 174 |
|
| 175 |
$primary_key = $ct_table->db->primary_key; |
| 176 |
|
| 177 |
if( ! isset( $_GET[$primary_key] ) ) { |
| 178 |
return $classes; |
| 179 |
} |
| 180 |
|
| 181 |
$automation_id = (int) $_GET[$primary_key]; |
| 182 |
$automation = $ct_table->db->get( $automation_id ); |
| 183 |
|
| 184 |
$classes .= ' edit-' . $automation->type . '-automation '; |
| 185 |
|
| 186 |
$classes = apply_filters( 'automatorwp_automation_ui_admin_body_class', $classes, $automation ); |
| 187 |
|
| 188 |
return $classes; |
| 189 |
|
| 190 |
} |
| 191 |
add_filter( 'admin_body_class', 'automatorwp_automation_ui_admin_body_class' ); |
| 192 |
|
| 193 |
/** |
| 194 |
* Automation UI meta boxes |
| 195 |
* |
| 196 |
* @since 1.0.0 |
| 197 |
*/ |
| 198 |
function automatorwp_automation_ui_add_meta_boxes() { |
| 199 |
|
| 200 |
add_meta_box( 'automatorwp_triggers', __( 'Triggers', 'automatorwp' ), 'automatorwp_automation_ui_triggers_meta_box', 'automatorwp_automations', 'normal', 'default' ); |
| 201 |
add_meta_box( 'automatorwp_actions', __( 'Actions', 'automatorwp' ), 'automatorwp_automation_ui_actions_meta_box', 'automatorwp_automations', 'normal', 'default' ); |
| 202 |
|
| 203 |
} |
| 204 |
add_action( 'add_meta_boxes', 'automatorwp_automation_ui_add_meta_boxes' ); |
| 205 |
|
| 206 |
/** |
| 207 |
* Renders the triggers meta box |
| 208 |
* |
| 209 |
* @since 1.0.0 |
| 210 |
* |
| 211 |
* @param stdClass $automation The automation object |
| 212 |
* @param string $type Type to render form |
| 213 |
*/ |
| 214 |
function automatorwp_automation_ui_triggers_meta_box( $automation, $type ) { |
| 215 |
|
| 216 |
global $automatorwp_triggers_current_position; |
| 217 |
|
| 218 |
$automatorwp_triggers_current_position = 0; |
| 219 |
|
| 220 |
$triggers = automatorwp_get_automation_triggers( $automation->id ); |
| 221 |
|
| 222 |
?> |
| 223 |
<?php // Section title and subtitle ?> |
| 224 |
<div class="automatorwp-title"><?php _e( 'Triggers', 'automatorwp' ); ?></div> |
| 225 |
<div class="automatorwp-subtitle"><?php _e( 'When this happens...', 'automatorwp' ); ?></div> |
| 226 |
|
| 227 |
<?php // Sequential ?> |
| 228 |
<div class="automatorwp-sequential-field cmb2-switch"> |
| 229 |
<label for="sequential"><?php _e( 'Sequential', 'automatorwp' ); ?></label> |
| 230 |
<div class="cmb-td"> |
| 231 |
<input type="checkbox" id="sequential" name="sequential" class="automatorwp-auto-save" value="1" <?php checked( $automation->sequential, 1 ); ?> /> |
| 232 |
<label for="sequential"><span class="cmb2-metabox-description"><?php _e( 'Check this option to force users to complete triggers in order.', 'automatorwp' ); ?></span></label> |
| 233 |
</div> |
| 234 |
</div> |
| 235 |
|
| 236 |
<?php // Triggers ?> |
| 237 |
<div class="automatorwp-automation-items automatorwp-triggers"> |
| 238 |
|
| 239 |
<?php $triggers = automatorwp_check_automation_required_triggers( $automation, $triggers ); ?> |
| 240 |
|
| 241 |
<?php foreach( $triggers as $trigger ) : ?> |
| 242 |
|
| 243 |
<?php automatorwp_automation_item_edit_html( $trigger, 'trigger', $automation ); ?> |
| 244 |
|
| 245 |
<?php endforeach; ?> |
| 246 |
|
| 247 |
</div> |
| 248 |
|
| 249 |
<?php automatorwp_automation_ui_add_item_form( $automation, 'trigger' ); ?> |
| 250 |
|
| 251 |
<?php // Anonymous automation notice ?> |
| 252 |
<div class="automatorwp-automation-ui-anonymous-notice"><span class="dashicons dashicons-info"></span> <?php _e( 'Anonymous automations only support one trigger per automation.', 'automatorwp' ); ?></div> |
| 253 |
<?php // Add Trigger ?> |
| 254 |
<button type="button" class="button automatorwp-button-success automatorwp-add-trigger"><span class="dashicons dashicons-plus"></span><?php _e( 'Add Trigger', 'automatorwp' ); ?></button> |
| 255 |
<?php // Add Filter ?> |
| 256 |
<button type="button" class="button button-primary automatorwp-add-filter"><span class="dashicons dashicons-filter"></span><?php _e( 'Add Filter', 'automatorwp' ); ?></button> |
| 257 |
<?php // Box spinner ?> |
| 258 |
<div class="automatorwp-spinner automatorwp-box-spinner" style="display: none;"> |
| 259 |
<span class="spinner is-active"></span> |
| 260 |
<span class="spinner-label"><?php _e( 'Saving...', 'automatorwp' ); ?></span> |
| 261 |
</div> |
| 262 |
<?php |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Renders the actions meta box |
| 267 |
* |
| 268 |
* @since 1.0.0 |
| 269 |
* |
| 270 |
* @param stdClass $automation The automation object |
| 271 |
* @param string $type Type to render form |
| 272 |
*/ |
| 273 |
function automatorwp_automation_ui_actions_meta_box( $automation, $type ) { |
| 274 |
|
| 275 |
$actions = automatorwp_get_automation_actions( $automation->id ); |
| 276 |
|
| 277 |
?> |
| 278 |
<?php // Section title and subtitle ?> |
| 279 |
<div class="automatorwp-title"><?php _e( 'Actions', 'automatorwp' ); ?></div> |
| 280 |
<div class="automatorwp-subtitle"><?php _e( 'Do this...', 'automatorwp' ); ?></div> |
| 281 |
|
| 282 |
<?php // Actions ?> |
| 283 |
<div class="automatorwp-automation-items automatorwp-actions"> |
| 284 |
|
| 285 |
<?php $actions = automatorwp_check_automation_required_actions( $automation, $actions ); ?> |
| 286 |
|
| 287 |
<?php foreach( $actions as $action ) : ?> |
| 288 |
|
| 289 |
<?php automatorwp_automation_item_edit_html( $action, 'action', $automation ); ?> |
| 290 |
|
| 291 |
<?php endforeach; ?> |
| 292 |
|
| 293 |
</div> |
| 294 |
|
| 295 |
<?php automatorwp_automation_ui_add_item_form( $automation, 'action' ); ?> |
| 296 |
|
| 297 |
<?php // Add Action ?> |
| 298 |
<button type="button" class="button automatorwp-button-success automatorwp-add-action"><span class="dashicons dashicons-plus"></span><?php _e( 'Add Action', 'automatorwp' ); ?></button> |
| 299 |
<?php // Add Filter ?> |
| 300 |
<button type="button" class="button button-primary automatorwp-add-filter"><span class="dashicons dashicons-filter"></span><?php _e( 'Add Filter', 'automatorwp' ); ?></button> |
| 301 |
<?php // Box spinner ?> |
| 302 |
<div class="automatorwp-spinner automatorwp-box-spinner" style="display: none;"> |
| 303 |
<span class="spinner is-active"></span> |
| 304 |
<span class="spinner-label"><?php _e( 'Saving...', 'automatorwp' ); ?></span> |
| 305 |
</div> |
| 306 |
<?php |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Automation UI add item form |
| 311 |
* |
| 312 |
* @since 1.0.0 |
| 313 |
* |
| 314 |
* @param stdClass $automation The automation object |
| 315 |
* @param string $item_type The item type (trigger|action) |
| 316 |
*/ |
| 317 |
function automatorwp_automation_ui_add_item_form( $automation, $item_type ) { |
| 318 |
|
| 319 |
$choices_filters = array( |
| 320 |
'show_in_selector' => true, |
| 321 |
); |
| 322 |
|
| 323 |
if( $item_type === 'trigger' ) { |
| 324 |
$choices_filters['anonymous'] = (bool) ( $automation->type === 'anonymous' ); |
| 325 |
} |
| 326 |
|
| 327 |
// Sort integrations |
| 328 |
$integrations = AutomatorWP()->integrations; |
| 329 |
ksort( $integrations ); |
| 330 |
|
| 331 |
?> |
| 332 |
|
| 333 |
<div class="automatorwp-add-item-form" style="display: none;"> |
| 334 |
|
| 335 |
<div class="automatorwp-automation-item-details"> |
| 336 |
<div class="automatorwp-integration-icon"></div> |
| 337 |
</div> |
| 338 |
|
| 339 |
<div class="automatorwp-automation-item-content"> |
| 340 |
|
| 341 |
<?php |
| 342 |
/** |
| 343 |
* Available action at top of the add item content |
| 344 |
* |
| 345 |
* @since 1.57 |
| 346 |
* |
| 347 |
* @param stdClass $automation The automation object |
| 348 |
* @param string $item_type The item type |
| 349 |
*/ |
| 350 |
do_action( 'automatorwp_automation_ui_add_item_content_top', $automation, $item_type ); ?> |
| 351 |
|
| 352 |
<div class="automatorwp-automation-item-actions"> |
| 353 |
<div class="automatorwp-automation-item-action automatorwp-automation-item-action-close" title="<?php echo esc_attr( __( 'Close', 'automatorwp') ); ?>"><span class="dashicons dashicons-no-alt"></span></div> |
| 354 |
</div> |
| 355 |
|
| 356 |
<div class="automatorwp-select-integration automatorwp-form"> |
| 357 |
|
| 358 |
<div class="automatorwp-select-integration-label"><?php _e( 'Select an integration', 'automatorwp' ); ?></div> |
| 359 |
|
| 360 |
<select class="automatorwp-all-integrations-choices"> |
| 361 |
<option value=""><?php echo esc_html( $item_type === 'trigger' ? __( 'Search for triggers', 'automatorwp' ) : __( 'Search for actions', 'automatorwp' ) ); ?></option> |
| 362 |
<?php foreach( $integrations as $integration => $args ) : ?> |
| 363 |
|
| 364 |
<?php // Skip filters |
| 365 |
if( $integration === 'filter' ) continue; ?> |
| 366 |
|
| 367 |
<?php switch ( $item_type ) { |
| 368 |
case 'trigger': |
| 369 |
$choices = automatorwp_get_integration_triggers( $integration, $choices_filters ); |
| 370 |
break; |
| 371 |
case 'action': |
| 372 |
$choices = automatorwp_get_integration_actions( $integration, $choices_filters ); |
| 373 |
break; |
| 374 |
default: |
| 375 |
$choices = array(); |
| 376 |
break; |
| 377 |
} |
| 378 |
|
| 379 |
// Skip integrations without triggers or actions |
| 380 |
if( ! empty( $choices ) ) : ?> |
| 381 |
<optgroup label="<?php echo esc_attr( $args['label'] ); ?>" data-icon="<?php echo esc_attr( $args['icon'] ); ?>"> |
| 382 |
|
| 383 |
<?php foreach( $choices as $trigger => $args ) : |
| 384 |
|
| 385 |
if( empty( $args['select_option'] ) && empty( $args['label'] ) ) { |
| 386 |
continue; |
| 387 |
} ?> |
| 388 |
<option value="<?php echo esc_attr( $trigger ); ?>" data-text="<?php echo esc_attr( $args['select_option'] ); ?>"><?php echo $args['label']; ?></option> |
| 389 |
<?php endforeach; ?> |
| 390 |
|
| 391 |
</optgroup> |
| 392 |
|
| 393 |
<?php endif; ?> |
| 394 |
|
| 395 |
<?php endforeach; ?> |
| 396 |
</select> |
| 397 |
|
| 398 |
<div class="automatorwp-integrations"> |
| 399 |
|
| 400 |
<?php foreach( $integrations as $integration => $args ) : ?> |
| 401 |
|
| 402 |
<?php // Skip filters |
| 403 |
if( $integration === 'filter' ) continue; ?> |
| 404 |
|
| 405 |
<?php switch ( $item_type ) { |
| 406 |
case 'trigger': |
| 407 |
$choices = automatorwp_get_integration_triggers( $integration, $choices_filters ); |
| 408 |
break; |
| 409 |
case 'action': |
| 410 |
$choices = automatorwp_get_integration_actions( $integration, $choices_filters ); |
| 411 |
break; |
| 412 |
default: |
| 413 |
$choices = array(); |
| 414 |
break; |
| 415 |
} |
| 416 |
|
| 417 |
// Skip integrations without triggers or actions |
| 418 |
if( ! empty( $choices ) ) : ?> |
| 419 |
|
| 420 |
<div class="automatorwp-integration" |
| 421 |
data-integration="<?php echo esc_attr( $integration ); ?>" |
| 422 |
data-label="<?php echo esc_attr( $args['label'] ); ?>" |
| 423 |
data-icon="<?php echo esc_attr( $args['icon'] ); ?>"> |
| 424 |
<div class="automatorwp-integration-icon"> |
| 425 |
<img src="<?php echo esc_attr( $args['icon'] ); ?>" alt="<?php echo esc_attr( $args['label'] ); ?>"> |
| 426 |
</div> |
| 427 |
<div class="automatorwp-integration-label"><?php echo $args['label']; ?></div> |
| 428 |
</div> |
| 429 |
|
| 430 |
<?php endif; ?> |
| 431 |
|
| 432 |
<?php |
| 433 |
/** |
| 434 |
* Available action to extend integration markup |
| 435 |
* |
| 436 |
* @since 1.0.0 |
| 437 |
* |
| 438 |
* @param string $integration The integration name |
| 439 |
* @param array $integration_args Integration arguments |
| 440 |
* @param stdClass $automation The automation object |
| 441 |
* @param string $item_type The item type |
| 442 |
*/ |
| 443 |
do_action( 'automatorwp_automation_ui_after_integration_choice', $integration, $args, $automation, $item_type ); ?> |
| 444 |
|
| 445 |
<?php endforeach; ?> |
| 446 |
|
| 447 |
</div> |
| 448 |
|
| 449 |
</div> |
| 450 |
|
| 451 |
<div class="automatorwp-integration-choices-container" style="display: none;"> |
| 452 |
|
| 453 |
<?php if ( $item_type === 'trigger' ) : ?> |
| 454 |
|
| 455 |
<div class="automatorwp-select-trigger-label"><?php _e( 'Select a trigger', 'automatorwp' ); ?></div> |
| 456 |
|
| 457 |
<?php foreach( $integrations as $integration => $args ) : ?> |
| 458 |
|
| 459 |
<?php // Skip filters |
| 460 |
if( $integration === 'filter' ) continue; ?> |
| 461 |
|
| 462 |
<select class="automatorwp-integration-choices" |
| 463 |
data-integration="<?php echo esc_attr( $integration ); ?>" |
| 464 |
data-placeholder="<?php echo esc_attr( __( 'Search for triggers', 'automatorwp' ) ); ?>" |
| 465 |
style="display: none;"> |
| 466 |
|
| 467 |
<option value=""></option> |
| 468 |
<?php foreach( automatorwp_get_integration_triggers( $integration, $choices_filters ) as $trigger => $args ) : |
| 469 |
|
| 470 |
if( empty( $args['select_option'] ) && empty( $args['label'] ) ) { |
| 471 |
continue; |
| 472 |
} ?> |
| 473 |
<option value="<?php echo esc_attr( $trigger ); ?>" data-text="<?php echo esc_attr( $args['select_option'] ); ?>"><?php echo $args['label']; ?></option> |
| 474 |
<?php endforeach; ?> |
| 475 |
|
| 476 |
<?php |
| 477 |
/** |
| 478 |
* Available action to extend integration triggers choices |
| 479 |
* |
| 480 |
* @since 1.2.4 |
| 481 |
* |
| 482 |
* @param string $integration The integration name |
| 483 |
* @param array $integration_args Integration arguments |
| 484 |
* @param stdClass $automation The automation object |
| 485 |
* @param string $item_type The item type |
| 486 |
*/ |
| 487 |
do_action( 'automatorwp_automation_ui_after_integration_triggers_choices', $integration, $args, $automation, $item_type ); ?> |
| 488 |
|
| 489 |
</select> |
| 490 |
|
| 491 |
<?php endforeach; ?> |
| 492 |
|
| 493 |
<?php elseif ( $item_type === 'action' ) : ?> |
| 494 |
|
| 495 |
<div class="automatorwp-select-action-label"><?php _e( 'Select an action', 'automatorwp' ); ?></div> |
| 496 |
|
| 497 |
<?php foreach( $integrations as $integration => $args ) : ?> |
| 498 |
|
| 499 |
<select class="automatorwp-integration-choices" |
| 500 |
data-integration="<?php echo esc_attr( $integration ); ?>" |
| 501 |
data-placeholder="<?php echo esc_attr( __( 'Search for actions', 'automatorwp' ) ); ?>" |
| 502 |
style="display: none;"> |
| 503 |
|
| 504 |
<option value=""></option> |
| 505 |
<?php foreach( automatorwp_get_integration_actions( $integration, $choices_filters ) as $action => $args ) : |
| 506 |
|
| 507 |
if( empty( $args['select_option'] ) && empty( $args['label'] ) ) { |
| 508 |
continue; |
| 509 |
} ?> |
| 510 |
<option value="<?php echo esc_attr( $action ); ?>" data-text="<?php echo esc_attr( $args['select_option'] ); ?>"><?php echo $args['label']; ?></option> |
| 511 |
<?php endforeach; ?> |
| 512 |
|
| 513 |
<?php |
| 514 |
/** |
| 515 |
* Available action to extend integration actions choices |
| 516 |
* |
| 517 |
* @since 1.2.4 |
| 518 |
* |
| 519 |
* @param string $integration The integration name |
| 520 |
* @param array $integration_args Integration arguments |
| 521 |
* @param stdClass $automation The automation object |
| 522 |
* @param string $item_type The item type |
| 523 |
*/ |
| 524 |
do_action( 'automatorwp_automation_ui_after_integration_actions_choices', $integration, $args, $automation, $item_type ); ?> |
| 525 |
|
| 526 |
</select> |
| 527 |
|
| 528 |
<?php endforeach; ?> |
| 529 |
|
| 530 |
<?php endif; ?> |
| 531 |
|
| 532 |
<button type="button" class="button automatorwp-button-danger automatorwp-cancel-choice-select"><?php _e( 'Cancel', 'automatorwp' ); ?></button> |
| 533 |
|
| 534 |
</div> |
| 535 |
|
| 536 |
<?php |
| 537 |
/** |
| 538 |
* Available action at bottom of the add item content |
| 539 |
* |
| 540 |
* @since 1.57 |
| 541 |
* |
| 542 |
* @param stdClass $automation The automation object |
| 543 |
* @param string $item_type The item type (trigger|action) |
| 544 |
*/ |
| 545 |
do_action( 'automatorwp_automation_ui_add_item_content_bottom', $automation, $item_type ); ?> |
| 546 |
|
| 547 |
</div> |
| 548 |
|
| 549 |
</div> |
| 550 |
|
| 551 |
<?php |
| 552 |
|
| 553 |
} |
| 554 |
|
| 555 |
/** |
| 556 |
* Get the object type args |
| 557 |
* |
| 558 |
* @since 1.0.0 |
| 559 |
* |
| 560 |
* @param stdClass $object The trigger/action object |
| 561 |
* @param string $item_type The item type (trigger|action) |
| 562 |
* |
| 563 |
* @return array |
| 564 |
*/ |
| 565 |
function automatorwp_automation_item_type_args( $object, $item_type ) { |
| 566 |
|
| 567 |
$type_args = array(); |
| 568 |
|
| 569 |
if( $item_type === 'trigger' ) { |
| 570 |
$type_args = automatorwp_get_trigger( $object->type ); |
| 571 |
} else if( $item_type === 'action' ) { |
| 572 |
$type_args = automatorwp_get_action( $object->type ); |
| 573 |
} |
| 574 |
|
| 575 |
/** |
| 576 |
* Get the object type args |
| 577 |
* |
| 578 |
* @since 1.0.0 |
| 579 |
* |
| 580 |
* @param array $type_args The trigger/action args |
| 581 |
* @param stdClass $object The trigger/action object |
| 582 |
* @param string $item_type The item type (trigger|action) |
| 583 |
* |
| 584 |
* @return array |
| 585 |
*/ |
| 586 |
return apply_filters( 'automatorwp_automation_item_type_args', $type_args, $object, $item_type ); |
| 587 |
|
| 588 |
} |
| 589 |
|
| 590 |
/** |
| 591 |
* Renders the trigger/action edit HTML |
| 592 |
* |
| 593 |
* @since 1.0.0 |
| 594 |
* |
| 595 |
* @param stdClass $object The trigger/action object |
| 596 |
* @param string $item_type The item type (trigger|action) |
| 597 |
* @param stdClass $automation The automation object |
| 598 |
*/ |
| 599 |
function automatorwp_automation_item_edit_html( $object, $item_type, $automation ) { |
| 600 |
|
| 601 |
global $automatorwp_triggers_current_position; |
| 602 |
|
| 603 |
// Check item type |
| 604 |
if( ! in_array( $item_type, array( 'trigger', 'action' ) ) ) { |
| 605 |
return; |
| 606 |
} |
| 607 |
|
| 608 |
// Check type args |
| 609 |
$type_args = automatorwp_automation_item_type_args( $object, $item_type ); |
| 610 |
|
| 611 |
if( ! $type_args ) { |
| 612 |
automatorwp_automation_missing_integration_item_edit_html( $object, $item_type, $automation ); |
| 613 |
return; |
| 614 |
} |
| 615 |
|
| 616 |
// Check integration |
| 617 |
$integration = automatorwp_get_integration( $type_args['integration'] ); |
| 618 |
|
| 619 |
if( ! $integration ) { |
| 620 |
automatorwp_automation_missing_integration_item_edit_html( $object, $item_type, $automation ); |
| 621 |
return; |
| 622 |
} |
| 623 |
|
| 624 |
if( $item_type === 'action' && $object->type === 'automatorwp_anonymous_user' ) { |
| 625 |
$integration['icon'] = AUTOMATORWP_URL . 'assets/img/automatorwp-anonymous.svg'; |
| 626 |
} |
| 627 |
|
| 628 |
// Setup the item classes |
| 629 |
$classes = array( |
| 630 |
'automatorwp-automation-item', |
| 631 |
'automatorwp-' . $item_type, |
| 632 |
'automatorwp-' . $item_type . '-' . str_replace( '_', '-', $object->type ), |
| 633 |
); |
| 634 |
|
| 635 |
if( $object->type === 'filter' ) { |
| 636 |
$classes[] = 'automatorwp-automation-item-filter'; |
| 637 |
} |
| 638 |
|
| 639 |
// Setup the item actions |
| 640 |
$actions = array( |
| 641 |
'move-up' => array( |
| 642 |
'label' => __( 'Move up', 'automatorwp'), |
| 643 |
'icon' => 'arrow-up-alt2' |
| 644 |
), |
| 645 |
'move-down' => array( |
| 646 |
'label' => __( 'Move down', 'automatorwp'), |
| 647 |
'icon' => 'arrow-down-alt2' |
| 648 |
), |
| 649 |
'delete' => array( |
| 650 |
'label' => __( 'Delete', 'automatorwp'), |
| 651 |
'icon' => 'trash' |
| 652 |
), |
| 653 |
); |
| 654 |
|
| 655 |
// Anonymous |
| 656 |
if( $automation->type === 'anonymous' ) { |
| 657 |
if( $item_type === 'trigger' ) { |
| 658 |
$classes[] = 'automatorwp-no-grab'; |
| 659 |
unset( $actions['move-up'] ); |
| 660 |
unset( $actions['move-down'] ); |
| 661 |
} else if( $object->type === 'automatorwp_anonymous_user' ) { |
| 662 |
$classes[] = 'automatorwp-no-grab'; |
| 663 |
unset( $actions['move-up'] ); |
| 664 |
unset( $actions['move-down'] ); |
| 665 |
unset( $actions['delete'] ); |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
/** |
| 670 |
* Filter to modify the integration args of the trigger/action edit HTML |
| 671 |
* |
| 672 |
* @since 1.0.0 |
| 673 |
* |
| 674 |
* @param array $integration The integration args |
| 675 |
* @param stdClass $object The trigger/action object |
| 676 |
* @param string $item_type The item type (trigger|action) |
| 677 |
* @param stdClass $automation The automation object |
| 678 |
* |
| 679 |
* @return array |
| 680 |
*/ |
| 681 |
$integration = apply_filters( 'automatorwp_automation_item_edit_html_integration', $integration, $object, $item_type, $automation ); |
| 682 |
|
| 683 |
/** |
| 684 |
* Filter to modify the CSS classes of the trigger/action edit HTML |
| 685 |
* |
| 686 |
* @since 1.3.0 |
| 687 |
* |
| 688 |
* @param array $classes The trigger/action CSS classes |
| 689 |
* @param stdClass $object The trigger/action object |
| 690 |
* @param string $item_type The item type (trigger|action) |
| 691 |
* @param stdClass $automation The automation object |
| 692 |
* |
| 693 |
* @return array |
| 694 |
*/ |
| 695 |
$classes = apply_filters( 'automatorwp_automations_ui_item_edit_html_classes', $classes, $object, $item_type, $automation ); |
| 696 |
|
| 697 |
/** |
| 698 |
* Filter to modify the actions of the trigger/action edit HTML |
| 699 |
* |
| 700 |
* @since 1.3.0 |
| 701 |
* |
| 702 |
* @param array $actions The trigger/action actions |
| 703 |
* @param stdClass $object The trigger/action object |
| 704 |
* @param string $item_type The item type (trigger|action) |
| 705 |
* @param stdClass $automation The automation object |
| 706 |
* |
| 707 |
* @return array |
| 708 |
*/ |
| 709 |
$actions = apply_filters( 'automatorwp_automations_ui_item_edit_html_actions', $actions, $object, $item_type, $automation ); |
| 710 |
|
| 711 |
?> |
| 712 |
<div id="automatorwp-item-<?php echo esc_attr( $object->id ); ?>" class="<?php echo implode( ' ', $classes ); ?>"> |
| 713 |
|
| 714 |
<div class="automatorwp-automation-item-details"> |
| 715 |
<div class="automatorwp-integration-icon"> |
| 716 |
<img src="<?php echo esc_attr( $integration['icon'] ); ?>" title="<?php echo esc_attr( $integration['label'] ); ?>" alt="<?php echo esc_attr( $integration['label'] ); ?>"> |
| 717 |
</div> |
| 718 |
</div> |
| 719 |
|
| 720 |
<div class="automatorwp-automation-item-content"> |
| 721 |
|
| 722 |
<?php if( ! empty( $actions ) ) : ?> |
| 723 |
<div class="automatorwp-automation-item-actions"> |
| 724 |
<?php foreach( $actions as $action => $action_args ) : ?> |
| 725 |
<div class="automatorwp-automation-item-action automatorwp-automation-item-action-<?php echo $action; ?>" title="<?php echo esc_attr( $action_args['label'] ); ?>"><span class="dashicons dashicons-<?php echo $action_args['icon']; ?>"></span></div> |
| 726 |
<?php endforeach; ?> |
| 727 |
|
| 728 |
</div> |
| 729 |
<?php endif; ?> |
| 730 |
|
| 731 |
<div class="automatorwp-integration-label"><?php echo $integration['label']; ?></div> |
| 732 |
|
| 733 |
<?php |
| 734 |
|
| 735 |
$position = ( is_int( $automatorwp_triggers_current_position ) ? $automatorwp_triggers_current_position + 1 : $object->position + 1 ); |
| 736 |
|
| 737 |
if( $object->type !== 'filter' && is_int( $automatorwp_triggers_current_position ) ) { |
| 738 |
$automatorwp_triggers_current_position++; |
| 739 |
} |
| 740 |
|
| 741 |
?> |
| 742 |
|
| 743 |
<div class="automatorwp-automation-item-position" style="<?php echo ( $automation->sequential ? '' : 'display: none;' ); ?>"><?php echo $position; ?>.</div> |
| 744 |
<div class="automatorwp-automation-item-label"><?php echo automatorwp_parse_automation_item_edit_label( $object, $item_type ); ?></div> |
| 745 |
|
| 746 |
<?php |
| 747 |
/** |
| 748 |
* After item label |
| 749 |
* |
| 750 |
* @since 1.0.0 |
| 751 |
* |
| 752 |
* @param stdClass $object The trigger/action object |
| 753 |
* @param string $item_type The object type (trigger|action) |
| 754 |
*/ |
| 755 |
do_action( 'automatorwp_automation_ui_after_item_label', $object, $item_type ); ?> |
| 756 |
|
| 757 |
<?php // Render the options form ?> |
| 758 |
<?php foreach( $type_args['options'] as $option => $args ) : ?> |
| 759 |
|
| 760 |
<div class="automatorwp-option-form-container" data-option="<?php echo esc_attr( $option ); ?>" data-from="<?php echo esc_attr( ( isset( $args['from'] ) ? $args['from'] : '' ) ); ?>" style="display: none;"> |
| 761 |
|
| 762 |
<?php |
| 763 |
/** |
| 764 |
* After option from |
| 765 |
* |
| 766 |
* @since 1.0.0 |
| 767 |
* |
| 768 |
* @param stdClass $object The trigger/action object |
| 769 |
* @param string $item_type The object type (trigger|action) |
| 770 |
* @param string $option The option key |
| 771 |
* @param array $args The option arguments |
| 772 |
*/ |
| 773 |
do_action( 'automatorwp_automation_ui_after_option_form', $object, $item_type, $option, $args ); ?> |
| 774 |
|
| 775 |
<?php |
| 776 |
// Get the option form |
| 777 |
$cmb2 = automatorwp_get_automation_item_option_form( $object, $item_type, $option, $automation ); |
| 778 |
|
| 779 |
if( $cmb2 ) { |
| 780 |
|
| 781 |
ct_setup_table( "automatorwp_{$item_type}s" ); |
| 782 |
|
| 783 |
// Render the form |
| 784 |
CMB2_Hookup::enqueue_cmb_css(); |
| 785 |
CMB2_Hookup::enqueue_cmb_js(); |
| 786 |
$cmb2->show_form(); |
| 787 |
|
| 788 |
ct_reset_setup_table(); |
| 789 |
} |
| 790 |
?> |
| 791 |
|
| 792 |
<?php |
| 793 |
/** |
| 794 |
* Before option from |
| 795 |
* |
| 796 |
* @since 1.0.0 |
| 797 |
* |
| 798 |
* @param stdClass $object The trigger/action object |
| 799 |
* @param string $item_type The object type (trigger|action) |
| 800 |
* @param string $option The option key |
| 801 |
* @param array $args The option arguments |
| 802 |
*/ |
| 803 |
do_action( 'automatorwp_automation_ui_before_option_form', $object, $item_type, $option, $args ); ?> |
| 804 |
|
| 805 |
<button type="button" class="button button-primary automatorwp-save-option-form"><?php _e( 'Save', 'automatorwp' ); ?></button> |
| 806 |
<button type="button" class="button automatorwp-button-danger automatorwp-cancel-option-form"><?php _e( 'Cancel', 'automatorwp' ); ?></button> |
| 807 |
|
| 808 |
<div class="automatorwp-spinner" style="display: none;"> |
| 809 |
<span class="spinner is-active"></span> |
| 810 |
<span class="spinner-label"><?php _e( 'Saving...', 'automatorwp' ); ?></span> |
| 811 |
</div> |
| 812 |
|
| 813 |
</div> |
| 814 |
|
| 815 |
<?php endforeach; ?> |
| 816 |
|
| 817 |
</div> |
| 818 |
|
| 819 |
<?php // Hidden fields ?> |
| 820 |
<input type="hidden" class="id" value="<?php echo esc_attr( $object->id ); ?>"/> |
| 821 |
<input type="hidden" class="type" value="<?php echo esc_attr( $object->type ); ?>"/> |
| 822 |
<input type="hidden" class="status" value="<?php echo esc_attr( $object->status ); ?>"/> |
| 823 |
<input type="hidden" class="position" value="<?php echo esc_attr( $object->position ); ?>"/> |
| 824 |
|
| 825 |
</div> |
| 826 |
<?php |
| 827 |
|
| 828 |
} |
| 829 |
|
| 830 |
/** |
| 831 |
* Creates if the required triggers exists |
| 832 |
* |
| 833 |
* @since 2.2.2 |
| 834 |
* |
| 835 |
* @param stdClass $automation The automation object |
| 836 |
* @param array $triggers The automation triggers |
| 837 |
* |
| 838 |
* @return array |
| 839 |
*/ |
| 840 |
function automatorwp_check_automation_required_triggers( $automation, $triggers ) { |
| 841 |
|
| 842 |
return apply_filters( 'automatorwp_check_automation_required_triggers', $triggers, $automation ); |
| 843 |
|
| 844 |
} |
| 845 |
|
| 846 |
/** |
| 847 |
* Creates if the required actions exists |
| 848 |
* |
| 849 |
* @since 1.3.0 |
| 850 |
* |
| 851 |
* @param stdClass $automation The automation object |
| 852 |
* @param array $actions The automation actions |
| 853 |
* |
| 854 |
* @return array |
| 855 |
*/ |
| 856 |
function automatorwp_check_automation_required_actions( $automation, $actions ) { |
| 857 |
|
| 858 |
if( $automation->type === 'anonymous' ) { |
| 859 |
|
| 860 |
$create = false; |
| 861 |
|
| 862 |
// Check if the first action is the action required for anonymous automations |
| 863 |
if( ! isset( $actions[0] ) ) { |
| 864 |
$create = true; |
| 865 |
} |
| 866 |
|
| 867 |
if( isset( $actions[0] ) && $actions[0]->type !== 'automatorwp_anonymous_user' ) { |
| 868 |
$create = true; |
| 869 |
} |
| 870 |
|
| 871 |
if( $create ) { |
| 872 |
ct_setup_table( 'automatorwp_actions' ); |
| 873 |
|
| 874 |
$action_data = array( |
| 875 |
'automation_id' => $automation->id, |
| 876 |
'title' => sprintf( __( 'Actions will be run on %1$s', 'automatorwp' ), '{user}' ), |
| 877 |
'type' => 'automatorwp_anonymous_user', |
| 878 |
'status' => 'active', |
| 879 |
'position' => '0', |
| 880 |
'date' => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ), |
| 881 |
); |
| 882 |
|
| 883 |
// Insert the new action |
| 884 |
$action_id = ct_insert_object( $action_data ); |
| 885 |
|
| 886 |
if( $action_id ) { |
| 887 |
$action_data['id'] = $action_id; |
| 888 |
|
| 889 |
$action_data = (object) $action_data; |
| 890 |
|
| 891 |
// Prepend the new action at start of the actions list |
| 892 |
array_unshift( $actions, $action_data ); |
| 893 |
} |
| 894 |
|
| 895 |
ct_reset_setup_table(); |
| 896 |
} |
| 897 |
|
| 898 |
} |
| 899 |
|
| 900 |
return apply_filters( 'automatorwp_check_automation_required_actions', $actions, $automation ); |
| 901 |
|
| 902 |
} |
| 903 |
|
| 904 |
/** |
| 905 |
* Renders the trigger/action missing integration edit HTML |
| 906 |
* |
| 907 |
* @since 1.1.2 |
| 908 |
* |
| 909 |
* @param stdClass $object The trigger/action object |
| 910 |
* @param string $item_type The item type (trigger|action) |
| 911 |
* @param stdClass $automation The automation object |
| 912 |
*/ |
| 913 |
function automatorwp_automation_missing_integration_item_edit_html( $object, $item_type, $automation ) { |
| 914 |
|
| 915 |
if( $item_type === 'trigger' ) { |
| 916 |
$warning_message = __( 'Trigger disabled because plugin associated couldn\'t be found. Please, re-install the plugin associated or remove this trigger.', 'automatorwp' ); |
| 917 |
} else { |
| 918 |
$warning_message = __( 'Action disabled because plugin associated couldn\'t be found. Please, re-install the plugin associated or remove this action.', 'automatorwp' ); |
| 919 |
|
| 920 |
} |
| 921 |
|
| 922 |
?> |
| 923 |
<div id="automatorwp-item-<?php echo esc_attr( $object->id ); ?>" class="automatorwp-automation-item automatorwp-automation-missing-integration-item automatorwp-<?php echo esc_attr( $item_type ); ?>"> |
| 924 |
|
| 925 |
<div class="automatorwp-automation-item-details"> |
| 926 |
<div class="automatorwp-integration-icon"> |
| 927 |
<img src="<?php echo esc_attr( AUTOMATORWP_URL . 'assets/img/integration-missing.svg' ); ?>" title="<?php echo esc_attr( __( 'Missing plugin', 'automatorwp' ) ); ?>"> |
| 928 |
</div> |
| 929 |
</div> |
| 930 |
|
| 931 |
<div class="automatorwp-automation-item-content"> |
| 932 |
|
| 933 |
<div class="automatorwp-automation-item-actions"> |
| 934 |
<div class="automatorwp-automation-item-action automatorwp-automation-item-action-delete" title="<?php echo esc_attr( __( 'Delete', 'automatorwp') ); ?>"><span class="dashicons dashicons-trash"></span></div> |
| 935 |
</div> |
| 936 |
|
| 937 |
<div class="automatorwp-integration-label"><?php echo __( 'Missing plugin', 'automatorwp' ); ?></div> |
| 938 |
|
| 939 |
<div class="automatorwp-automation-item-position" style="<?php echo ( $automation->sequential ? '' : 'display: none;' ); ?>"><?php echo $object->position + 1; ?>.</div> |
| 940 |
<div class="automatorwp-automation-item-label"><?php echo $object->title; ?></div> |
| 941 |
|
| 942 |
<div class="automatorwp-notice-warning"><?php echo $warning_message; ?></div> |
| 943 |
|
| 944 |
<?php |
| 945 |
/** |
| 946 |
* After missing integration item label |
| 947 |
* |
| 948 |
* @since 1.1.2 |
| 949 |
* |
| 950 |
* @param stdClass $object The trigger/action object |
| 951 |
* @param string $item_type The object type (trigger|action) |
| 952 |
*/ |
| 953 |
do_action( 'automatorwp_automation_ui_after_missing_integration_item_label', $object, $item_type ); ?> |
| 954 |
|
| 955 |
</div> |
| 956 |
|
| 957 |
<?php // Hidden fields ?> |
| 958 |
<input type="hidden" class="id" value="<?php echo esc_attr( $object->id ); ?>"/> |
| 959 |
<input type="hidden" class="type" value="<?php echo esc_attr( $object->type ); ?>"/> |
| 960 |
<input type="hidden" class="status" value="<?php echo esc_attr( $object->status ); ?>"/> |
| 961 |
<input type="hidden" class="position" value="<?php echo esc_attr( $object->position ); ?>"/> |
| 962 |
|
| 963 |
</div> |
| 964 |
<?php |
| 965 |
|
| 966 |
} |
| 967 |
|
| 968 |
/** |
| 969 |
* Parses the trigger/action edit label |
| 970 |
* |
| 971 |
* @since 1.0.0 |
| 972 |
* |
| 973 |
* @param stdClass $object The trigger object |
| 974 |
* @param string $item_type The item type (trigger|action) |
| 975 |
* @param string $context The context this function is executed |
| 976 |
* |
| 977 |
* @return string |
| 978 |
*/ |
| 979 |
function automatorwp_parse_automation_item_edit_label( $object, $item_type, $context = 'edit' ) { |
| 980 |
|
| 981 |
$type_args = automatorwp_automation_item_type_args( $object, $item_type ); |
| 982 |
|
| 983 |
if( ! $type_args ) { |
| 984 |
return ''; |
| 985 |
} |
| 986 |
|
| 987 |
/** |
| 988 |
* Filter to dynamically change the edit label |
| 989 |
* |
| 990 |
* @since 1.0.0 |
| 991 |
* |
| 992 |
* @param string $label The edit label |
| 993 |
* @param stdClass $object The trigger/action object |
| 994 |
* @param string $item_type The item type (trigger|action) |
| 995 |
* @param string $context The context this function is executed |
| 996 |
* @param array $type_args The type parameters |
| 997 |
* |
| 998 |
* @return string |
| 999 |
*/ |
| 1000 |
$label = apply_filters( 'automatorwp_parse_automation_item_edit_label', $type_args['edit_label'], $object, $item_type, $context, $type_args ); |
| 1001 |
|
| 1002 |
return automatorwp_parse_automation_item_label( $object, $item_type, $label, $context ); |
| 1003 |
|
| 1004 |
} |
| 1005 |
|
| 1006 |
/** |
| 1007 |
* Parses the trigger/action log label |
| 1008 |
* |
| 1009 |
* @since 1.0.0 |
| 1010 |
* |
| 1011 |
* @param stdClass $object The trigger/action object |
| 1012 |
* @param string $item_type The item type (trigger|action) |
| 1013 |
* @param string $context The context this function is executed |
| 1014 |
* |
| 1015 |
* @return string |
| 1016 |
*/ |
| 1017 |
function automatorwp_parse_automation_item_log_label( $object, $item_type, $context = 'edit' ) { |
| 1018 |
|
| 1019 |
$type_args = automatorwp_automation_item_type_args( $object, $item_type ); |
| 1020 |
|
| 1021 |
if( ! $type_args ) { |
| 1022 |
return ''; |
| 1023 |
} |
| 1024 |
|
| 1025 |
/** |
| 1026 |
* Filter to dynamically change the log label |
| 1027 |
* |
| 1028 |
* @since 1.0.0 |
| 1029 |
* |
| 1030 |
* @param string $label The edit label |
| 1031 |
* @param stdClass $object The trigger/action object |
| 1032 |
* @param string $item_type The item type (trigger|action) |
| 1033 |
* @param string $context The context this function is executed |
| 1034 |
* @param array $type_args The type parameters |
| 1035 |
* |
| 1036 |
* @return string |
| 1037 |
*/ |
| 1038 |
$label = apply_filters( 'automatorwp_parse_automation_item_log_label', $type_args['log_label'], $object, $item_type, $context, $type_args ); |
| 1039 |
|
| 1040 |
return automatorwp_parse_automation_item_label( $object, $item_type, $label, $context ); |
| 1041 |
|
| 1042 |
} |
| 1043 |
|
| 1044 |
/** |
| 1045 |
* Parses the trigger/action label given |
| 1046 |
* |
| 1047 |
* @since 1.0.0 |
| 1048 |
* |
| 1049 |
* @param stdClass $object The trigger object |
| 1050 |
* @param string $item_type The item type (trigger|action) |
| 1051 |
* @param string $label The label to parse |
| 1052 |
* @param string $context The context this function is executed |
| 1053 |
* |
| 1054 |
* @return string |
| 1055 |
*/ |
| 1056 |
function automatorwp_parse_automation_item_label( $object, $item_type, $label, $context = 'edit' ) { |
| 1057 |
|
| 1058 |
$type_args = automatorwp_automation_item_type_args( $object, $item_type ); |
| 1059 |
|
| 1060 |
if( ! $type_args ) { |
| 1061 |
return ''; |
| 1062 |
} |
| 1063 |
|
| 1064 |
$replacements = array(); |
| 1065 |
|
| 1066 |
foreach( $type_args['options'] as $option => $args ) { |
| 1067 |
$replacements['{' . $option . '}'] = automatorwp_get_automation_item_option_replacement( $object, $item_type, $option, $context ); |
| 1068 |
} |
| 1069 |
|
| 1070 |
/** |
| 1071 |
* Trigger/action label replacements |
| 1072 |
* |
| 1073 |
* @since 1.0.0 |
| 1074 |
* |
| 1075 |
* @param array $replacements The replacements to apply |
| 1076 |
* @param stdClass $object The trigger object |
| 1077 |
* @param string $item_type The item type (trigger|action) |
| 1078 |
* @param string $label The label to parse |
| 1079 |
* @param string $context The context this function is executed |
| 1080 |
* |
| 1081 |
* @return array |
| 1082 |
*/ |
| 1083 |
$replacements = apply_filters( 'automatorwp_parse_automation_item_label_replacements', $replacements, $object, $item_type, $label, $context ); |
| 1084 |
|
| 1085 |
$tags = array_keys( $replacements ); |
| 1086 |
|
| 1087 |
$label_parsed = str_replace( $tags, $replacements, $label ); |
| 1088 |
|
| 1089 |
/** |
| 1090 |
* Trigger/action label parsed |
| 1091 |
* |
| 1092 |
* @since 1.0.0 |
| 1093 |
* |
| 1094 |
* @param string $label_parsed The label parsed |
| 1095 |
* @param stdClass $object The trigger object |
| 1096 |
* @param string $item_type The item type (trigger|action) |
| 1097 |
* @param string $label The originallabel to parse |
| 1098 |
* @param string $context The context this function is executed |
| 1099 |
* @param array $tags The tags applied |
| 1100 |
* @param array $replacements The replacements applied |
| 1101 |
* |
| 1102 |
* @return string |
| 1103 |
*/ |
| 1104 |
return apply_filters( 'automatorwp_parse_automation_item_label', $label_parsed, $object, $item_type, $label, $context, $tags, $replacements ); |
| 1105 |
|
| 1106 |
} |
| 1107 |
|
| 1108 |
/** |
| 1109 |
* Get the option replacement |
| 1110 |
* |
| 1111 |
* @since 1.0.0 |
| 1112 |
* |
| 1113 |
* @param stdClass $object The trigger/action object |
| 1114 |
* @param string $item_type The item type (trigger|action) |
| 1115 |
* @param string $option The option name |
| 1116 |
* @param string $context The context this function is executed |
| 1117 |
* |
| 1118 |
* @return string |
| 1119 |
*/ |
| 1120 |
function automatorwp_get_automation_item_option_replacement( $object, $item_type, $option, $context = 'edit' ) { |
| 1121 |
|
| 1122 |
global $automatorwp_event; |
| 1123 |
|
| 1124 |
// Check item type |
| 1125 |
if( ! in_array( $item_type, array( 'trigger', 'action' ) ) ) { |
| 1126 |
return false; |
| 1127 |
} |
| 1128 |
|
| 1129 |
// Check type args |
| 1130 |
$type_args = automatorwp_automation_item_type_args( $object, $item_type ); |
| 1131 |
|
| 1132 |
if( ! $type_args ) { |
| 1133 |
return ''; |
| 1134 |
} |
| 1135 |
|
| 1136 |
// Bail if this type hasn't any option |
| 1137 |
if( ! isset( $type_args['options'][$option] ) ) { |
| 1138 |
return ''; |
| 1139 |
} |
| 1140 |
|
| 1141 |
$option_args = $type_args['options'][$option]; |
| 1142 |
|
| 1143 |
$field_id = ( isset( $option_args['from'] ) ? $option_args['from'] : '' ); |
| 1144 |
$value = ''; |
| 1145 |
|
| 1146 |
// If not isset the from field, try to get a default value |
| 1147 |
if( ! isset( $option_args['fields'][$field_id] ) ) { |
| 1148 |
|
| 1149 |
if( isset( $option_args['default'] ) && ! empty( $option_args['default'] ) ) { |
| 1150 |
$value = $option_args['default']; |
| 1151 |
} |
| 1152 |
|
| 1153 |
} else { |
| 1154 |
|
| 1155 |
ct_setup_table( "automatorwp_{$item_type}s" ); |
| 1156 |
|
| 1157 |
$field = $option_args['fields'][$field_id]; |
| 1158 |
$value = ct_get_object_meta( $object->id, $field_id, true ); |
| 1159 |
|
| 1160 |
if( ( $value === '' || is_null( $value ) ) && isset( $field['default'] ) ) { |
| 1161 |
$value = $field['default']; |
| 1162 |
} |
| 1163 |
|
| 1164 |
// Ensure the field value and escaped_value keys |
| 1165 |
$field['object_id'] = $object->id; |
| 1166 |
$field['value'] = $value; |
| 1167 |
$field['escaped_value'] = $value; |
| 1168 |
|
| 1169 |
// Select field |
| 1170 |
if( in_array( $field['type'], array( 'select', 'automatorwp_select', 'automatorwp_select_filter' ) ) ) { |
| 1171 |
|
| 1172 |
// Get the field options |
| 1173 |
$field_options = automatorwp_get_field_options( $field ); |
| 1174 |
|
| 1175 |
// Try to get the displayed value |
| 1176 |
$found = automatorwp_get_array_key_value( $value, $field_options ); |
| 1177 |
|
| 1178 |
if( $found ) { |
| 1179 |
$value = $found; |
| 1180 |
} |
| 1181 |
} |
| 1182 |
|
| 1183 |
// Array values |
| 1184 |
if( is_array( $value ) ) { |
| 1185 |
|
| 1186 |
// Get the field options |
| 1187 |
$field_options = automatorwp_get_field_options( $field ); |
| 1188 |
|
| 1189 |
// Replace values by the options label |
| 1190 |
if( count( $field_options ) ) { |
| 1191 |
foreach( $value as $i => $v ) { |
| 1192 |
// Try to get the displayed value |
| 1193 |
$found = automatorwp_get_array_key_value( $v, $field_options ); |
| 1194 |
|
| 1195 |
if( isset( $found ) ) { |
| 1196 |
$value[$i] = $found; |
| 1197 |
} |
| 1198 |
} |
| 1199 |
} |
| 1200 |
|
| 1201 |
/* translators: The ampersand character who represents the word "and" */ |
| 1202 |
$and = __( '&', 'automatorwp' ); |
| 1203 |
$value = automatorwp_implode_array( ', ', " {$and} ", $value ); |
| 1204 |
} |
| 1205 |
|
| 1206 |
// Fallback to default option if exists |
| 1207 |
if( ( $value === '' || is_null( $value ) ) && isset( $option_args['default'] ) && ! empty( $option_args['default'] ) ) { |
| 1208 |
$value = $option_args['default']; |
| 1209 |
} |
| 1210 |
|
| 1211 |
ct_reset_setup_table(); |
| 1212 |
|
| 1213 |
} |
| 1214 |
|
| 1215 |
/** |
| 1216 |
* Filters the option value for replacement on labels |
| 1217 |
* |
| 1218 |
* @since 1.0.0 |
| 1219 |
* |
| 1220 |
* @param string $value The option value |
| 1221 |
* @param stdClass $object The trigger/action object |
| 1222 |
* @param string $item_type The item type (trigger|action) |
| 1223 |
* @param string $option The option name |
| 1224 |
* @param string $context The context this function is executed |
| 1225 |
* |
| 1226 |
* @return string |
| 1227 |
*/ |
| 1228 |
$value = apply_filters( 'automatorwp_get_automation_item_option_replacement', $value, $object, $item_type, $option, $context ); |
| 1229 |
|
| 1230 |
if( $context === 'edit' ) { |
| 1231 |
|
| 1232 |
$option_class = 'button button-primary'; |
| 1233 |
|
| 1234 |
if( $object->type === 'filter' ) { |
| 1235 |
$option_class = 'button'; |
| 1236 |
} |
| 1237 |
|
| 1238 |
/** |
| 1239 |
* Filters the option button class |
| 1240 |
* |
| 1241 |
* @since 1.2.4 |
| 1242 |
* |
| 1243 |
* @param string $option_class The option class, by default "button button-primary" |
| 1244 |
* @param stdClass $object The trigger/action object |
| 1245 |
* @param string $item_type The item type (trigger|action) |
| 1246 |
* @param string $option The option name |
| 1247 |
* @param string $context The context this function is executed |
| 1248 |
* |
| 1249 |
* @return string |
| 1250 |
*/ |
| 1251 |
$option_class = apply_filters( 'automatorwp_get_automation_item_option_button_class', $option_class, $object, $item_type, $option, $context ); |
| 1252 |
|
| 1253 |
$value = '<span class="' . esc_attr__( $option_class ) . ' automatorwp-option" data-option="' . $option . '">' . $value . '</span>'; |
| 1254 |
} else if( $context === 'view' ) { |
| 1255 |
|
| 1256 |
$automation = automatorwp_get_trigger_automation( $object->id ); |
| 1257 |
|
| 1258 |
if( is_object( $automation ) ) { |
| 1259 |
|
| 1260 |
$user_id = ( isset( $automatorwp_event['user_id'] ) ? $automatorwp_event['user_id'] : 0 ); |
| 1261 |
|
| 1262 |
$value = automatorwp_parse_automation_tags( $automation->id, $user_id, $value ); |
| 1263 |
} |
| 1264 |
|
| 1265 |
|
| 1266 |
} |
| 1267 |
|
| 1268 |
return $value; |
| 1269 |
|
| 1270 |
} |
| 1271 |
|
| 1272 |
/** |
| 1273 |
* Gets a CMB2 object from a trigger/action option |
| 1274 |
* |
| 1275 |
* @since 1.0.0 |
| 1276 |
* |
| 1277 |
* @param stdClass $object The trigger/action object |
| 1278 |
* @param string $item_type The item type (trigger|action) |
| 1279 |
* @param string $option Option form to render |
| 1280 |
* @param stdClass $automation The automation object |
| 1281 |
* |
| 1282 |
* @return CMB2|false |
| 1283 |
*/ |
| 1284 |
function automatorwp_get_automation_item_option_form( $object, $item_type, $option, $automation ) { |
| 1285 |
|
| 1286 |
// Check item type |
| 1287 |
if( ! in_array( $item_type, array( 'trigger', 'action' ) ) ) { |
| 1288 |
return false; |
| 1289 |
} |
| 1290 |
|
| 1291 |
// Check type args |
| 1292 |
$type_args = automatorwp_automation_item_type_args( $object, $item_type ); |
| 1293 |
|
| 1294 |
if( ! $type_args ) { |
| 1295 |
return false; |
| 1296 |
} |
| 1297 |
|
| 1298 |
// Bail if this type hasn't any option |
| 1299 |
if( ! isset( $type_args['options'][$option] ) ) { |
| 1300 |
return false; |
| 1301 |
} |
| 1302 |
|
| 1303 |
$args = $type_args['options'][$option]; |
| 1304 |
|
| 1305 |
/** |
| 1306 |
* Filter to override the trigger/action option fields |
| 1307 |
* |
| 1308 |
* @since 1.0.0 |
| 1309 |
* |
| 1310 |
* @param array $fields The option fields |
| 1311 |
* @param stdClass $object The trigger/action object |
| 1312 |
* @param string $item_type The item type (trigger|action) |
| 1313 |
* @param string $option Option form to render |
| 1314 |
* @param stdClass $automation The automation object |
| 1315 |
* |
| 1316 |
* @return array |
| 1317 |
*/ |
| 1318 |
$fields = apply_filters( 'automatorwp_get_automation_item_option_form_fields', $args['fields'], $object, $item_type, $option, $automation ); |
| 1319 |
|
| 1320 |
ct_setup_table( "automatorwp_{$item_type}s" ); |
| 1321 |
|
| 1322 |
// Setup the CMB2 form |
| 1323 |
$cmb2 = new CMB2( array( |
| 1324 |
'id' => $option .'_form', |
| 1325 |
'object_types' => array( 'automatorwp_triggers', 'automatorwp_actions' ), |
| 1326 |
'classes' => 'automatorwp-form automatorwp-option-form', |
| 1327 |
'hookup' => false, |
| 1328 |
), $object->id ); |
| 1329 |
|
| 1330 |
// Setup the options fields |
| 1331 |
foreach ( $fields as $field_id => $field ) { |
| 1332 |
|
| 1333 |
$field['id'] = $field_id; |
| 1334 |
|
| 1335 |
if( $field['type'] === 'group' ) { |
| 1336 |
// Group fields |
| 1337 |
|
| 1338 |
// Setup field arguments on each group field |
| 1339 |
foreach ( $field['fields'] as $field_group_id => $field_group ) { |
| 1340 |
|
| 1341 |
$field_group['id'] = $field_group_id; |
| 1342 |
|
| 1343 |
$field['fields'][$field_group_id] = automatorwp_automation_item_option_field_args( $object, $item_type, $option, $automation, $field_id . '[' .$field_group_id . ']', $field_group ); |
| 1344 |
|
| 1345 |
} |
| 1346 |
|
| 1347 |
} else { |
| 1348 |
// Single fields |
| 1349 |
|
| 1350 |
$field = automatorwp_automation_item_option_field_args( $object, $item_type, $option, $automation, $field_id, $field ); |
| 1351 |
|
| 1352 |
} |
| 1353 |
|
| 1354 |
// Add the field to the form |
| 1355 |
$cmb2->add_field( $field ); |
| 1356 |
|
| 1357 |
} |
| 1358 |
|
| 1359 |
ct_reset_setup_table(); |
| 1360 |
|
| 1361 |
return $cmb2; |
| 1362 |
|
| 1363 |
} |
| 1364 |
|
| 1365 |
/** |
| 1366 |
* Gets a CMB2 object from a trigger option |
| 1367 |
* |
| 1368 |
* @since 1.0.0 |
| 1369 |
* |
| 1370 |
* @param stdClass $object The trigger/action object |
| 1371 |
* @param string $item_type The item type (trigger|action) |
| 1372 |
* @param string $option Option form to render |
| 1373 |
* @param stdClass $automation The automation object |
| 1374 |
* @param string $field_id The field ID |
| 1375 |
* @param array $field The field parameters |
| 1376 |
* |
| 1377 |
* @return array |
| 1378 |
*/ |
| 1379 |
function automatorwp_automation_item_option_field_args( $object, $item_type, $option, $automation, $field_id, $field ) { |
| 1380 |
|
| 1381 |
$repeatable = ( isset( $field['repeatable'] ) && $field['repeatable'] === true ); |
| 1382 |
|
| 1383 |
// Prevent to render field names to avoid conflicts on the main form |
| 1384 |
$field['attributes']['name'] = ''; |
| 1385 |
|
| 1386 |
// Update id attribute to avoid id collisions |
| 1387 |
$field['attributes']['id'] = $field_id . '-' . $object->id; |
| 1388 |
$field['attributes']['data-option'] = $field_id . ( $repeatable ? '[]' : '' ); |
| 1389 |
|
| 1390 |
// Setup the fields tags selector |
| 1391 |
|
| 1392 |
// Set a specific sanitization callback for fields that support HTML and URLs |
| 1393 |
if( in_array( $field['type'], array( 'textarea', 'wysiwyg', 'oembed', 'file' ) ) ) { |
| 1394 |
$field['sanitization_cb'] = 'automatorwp_textarea_sanitization_cb'; |
| 1395 |
} |
| 1396 |
|
| 1397 |
// Check if field type is compatible with tags selector |
| 1398 |
if( in_array( $field['type'], array( 'text', 'textarea', 'wysiwyg', 'file' ) ) ) { |
| 1399 |
$field['after_field'] = automatorwp_get_tags_selector_html( $automation, $object, $item_type ); |
| 1400 |
} |
| 1401 |
|
| 1402 |
// If field is required, update its label with the required mark |
| 1403 |
if( isset( $field['required'] ) && $field['required'] && isset( $field['name'] ) ) { |
| 1404 |
$field['name'] .= '<span class="automatorwp-field-required">*</span>'; |
| 1405 |
} |
| 1406 |
|
| 1407 |
/** |
| 1408 |
* Filter available to process custom field parameters |
| 1409 |
* |
| 1410 |
* @since 1.0.0 |
| 1411 |
* |
| 1412 |
* @param array $field The field parameters |
| 1413 |
* @param stdClass $object The trigger/action object |
| 1414 |
* @param string $item_type The item type (trigger|action) |
| 1415 |
* @param string $option Option form to render |
| 1416 |
* @param stdClass $automation The automation object |
| 1417 |
* @param string $field_id The field ID |
| 1418 |
* |
| 1419 |
* @return array |
| 1420 |
*/ |
| 1421 |
return apply_filters( 'automatorwp_automation_item_option_field_args', $field, $object, $item_type, $option, $automation, $field_id ); |
| 1422 |
|
| 1423 |
} |