Skip to content
Snippets Groups Projects
Commit 4e4c0f37 authored by Christian Wach's avatar Christian Wach :soccer:
Browse files

First pass at ACFE 0.9.x compatibility

parent 660765ed
No related branches found
No related tags found
No related merge requests found
Showing with 2183 additions and 16 deletions
......@@ -77,8 +77,8 @@ class Conditional_Form_Actions_For_ACFE {
// Always load translations.
add_action( 'init', [ $this, 'translation' ] );
// Initialise when ACF Extended is initialised.
add_action( 'acfe/init', [ $this, 'initialise' ] );
// Initialise when plugins have been loaded.
add_action( 'plugins_loaded', [ $this, 'initialise' ] );
}
......
......@@ -28,6 +28,15 @@ class CFAFA_ACFE {
*/
public $plugin;
/**
* ACF Extended plugin version.
*
* @since 0.2.0
* @access public
* @var string
*/
public $acfe_version;
/**
* Constructor.
*
......@@ -58,6 +67,15 @@ class CFAFA_ACFE {
return;
}
// Bail if ACFE is not present.
if ( ! defined( 'ACFE_VERSION' ) ) {
$done = true;
return;
}
// Store ACFE version.
$this->acfe_version = ACFE_VERSION;
// Register hooks.
$this->register_hooks();
......@@ -81,7 +99,11 @@ class CFAFA_ACFE {
public function register_hooks() {
// Register ACFE Form Actions.
add_filter( 'acfe/include_form_actions', [ $this, 'register_form_actions' ], 50 );
if ( version_compare( $this->acfe_version, '0.9', '>=' ) ) {
add_action( 'acf/include_field_types', [ $this, 'register_form_actions_latest' ], 50 );
} else {
add_action( 'acfe/include_form_actions', [ $this, 'register_form_actions_legacy' ], 50 );
}
/*
// Clear Form Action Query Vars.
......@@ -106,16 +128,40 @@ class CFAFA_ACFE {
}
/**
* Register Form Actions.
* Register Form Actions for ACFE version 0.9.x.
*
* @since 0.1
* @since 0.2.0
*/
public function register_form_actions_latest() {
// Include class files.
include CFAFA_PATH . 'includes/form-actions/acfe-0.9.x/cfafa-form-action-base.php';
include CFAFA_PATH . 'includes/form-actions/acfe-0.9.x/cfafa-form-action-redirect.php';
include CFAFA_PATH . 'includes/form-actions/acfe-0.9.x/cfafa-form-action-email.php';
// Register the Form Actions.
acfe_register_form_action_type( 'CFAFA_ACFE_Form_Action_Redirect' );
acfe_register_form_action_type( 'CFAFA_ACFE_Form_Action_Email' );
// Maybe add WooCommerce Product Action.
if ( function_exists( 'WC' ) ) {
include CFAFA_PATH . 'includes/form-actions/acfe-0.9.x/cfafa-form-action-product.php';
acfe_register_form_action_type( 'CFAFA_ACFE_Form_Action_Product' );
}
}
/**
* Register Form Actions for ACFE version 0.8.x.
*
* @since 0.2.0
*/
public function register_form_actions() {
public function register_form_actions_legacy() {
// Include class files.
include CFAFA_PATH . 'includes/form-actions/cfafa-form-action-base.php';
include CFAFA_PATH . 'includes/form-actions/cfafa-form-action-redirect.php';
include CFAFA_PATH . 'includes/form-actions/cfafa-form-action-email.php';
include CFAFA_PATH . 'includes/form-actions/acfe-0.8.x/cfafa-form-action-base.php';
include CFAFA_PATH . 'includes/form-actions/acfe-0.8.x/cfafa-form-action-redirect.php';
include CFAFA_PATH . 'includes/form-actions/acfe-0.8.x/cfafa-form-action-email.php';
// Instantiate the Form Actions.
new CFAFA_Form_Action_Redirect( $this );
......@@ -123,7 +169,7 @@ class CFAFA_ACFE {
// Maybe add WooCommerce Product Action.
if ( function_exists( 'WC' ) ) {
include CFAFA_PATH . 'includes/form-actions/cfafa-form-action-product.php';
include CFAFA_PATH . 'includes/form-actions/acfe-0.8.x/cfafa-form-action-product.php';
new CFAFA_Form_Action_Product( $this );
}
......
......@@ -11,7 +11,7 @@
defined( 'ABSPATH' ) || exit;
/**
* CiviCRM Profile Sync "Conditional Email" ACFE Form Action Class.
* "Conditional Email" ACFE Form Action Class.
*
* A class that handles the "Conditional Email" ACFE Form Action.
*
......
......@@ -11,7 +11,7 @@
defined( 'ABSPATH' ) || exit;
/**
* CiviCRM Profile Sync "WooCommerce Product" ACFE Form Action Class.
* "WooCommerce Product" ACFE Form Action Class.
*
* A class that handles the "WooCommerce Product" ACFE Form Action.
*
......
<?php
/**
* "CiviCRM Redirect" ACFE Form Action Class.
* "Conditional Redirect" ACFE Form Action Class.
*
* Handles the "CiviCRM Redirect" ACFE Form Action.
* Handles the "Conditional Redirect" ACFE Form Action.
*
* @package Conditional_Form_Actions_For_ACFE
*/
......@@ -11,9 +11,9 @@
defined( 'ABSPATH' ) || exit;
/**
* CiviCRM Profile Sync "CiviCRM Redirect" ACFE Form Action Class.
* "Conditional Redirect" ACFE Form Action Class.
*
* A class that handles the "CiviCRM Redirect" ACFE Form Action.
* A class that handles the "Conditional Redirect" ACFE Form Action.
*
* @since 0.1
*/
......
<?php
/**
* ACFE Form Action Base Class.
*
* Holds methods common to ACFE Form Action classes.
*
* @package Conditional_Form_Actions_For_ACFE
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
/**
* "Base" ACFE Form Action Class.
*
* A class that is extended by ACFE Form Action classes.
*
* @since 0.2.0
*/
class CFAFA_ACFE_Form_Action_Base extends acfe_module_form_action {
/**
* Form Action Name.
*
* @since 0.2.0
* @access public
* @var string
*/
public $action_name = '';
/**
* Form Action Label.
*
* @since 0.2.0
* @access public
* @var string
*/
public $action_label = '';
/**
* Form Action Alias Placeholder.
*
* @since 0.2.0
* @access public
* @var string
*/
public $alias_placeholder = '';
/**
* Field Key Prefix.
*
* @since 0.2.0
* @access public
* @var string
*/
public $field_key = '';
/**
* Field Name Prefix.
*
* @since 0.2.0
* @access public
* @var string
*/
public $field_name = '';
/**
* Conditional Field "code".
*
* @since 0.2.0
* @access public
* @var string
*/
public $conditional_code = '';
/**
* Maybe skip the Action when the Form the Action is attached to is submitted.
*
* @since 0.2.0
*
* @param array $form The array of Form data.
* @param array $action The array of Action data.
* @return bool $prepare The net result of the set of filters.
*/
public function make_skip( $form, $action ) {
// Get some Form details.
$form_name = acf_maybe_get( $form, 'name' );
$form_id = acf_maybe_get( $form, 'ID' );
// Assume we're good to go.
$prepare = true;
/**
* Allow others to prevent Form Action.
*
* Returning false for any of these filters will skip the Action.
*
* @since 0.2.0
*
* @param bool $prepare True by default so that the Form Action goes ahead.
* @param array $form The array of Form data.
* @param array $action The array of Action data.
*/
$filter = 'acfe/form/v3/skip/' . $this->action_name;
$prepare = apply_filters( $filter, $prepare, $form, $action );
$prepare = apply_filters( $filter . '/form=' . $form_name, $prepare, $form, $action );
if ( ! empty( $action['name'] ) ) {
$prepare = apply_filters( $filter . '/action=' . $action['name'], $prepare, $form, $action );
}
// --<
return $prepare;
}
/**
* Saves the result of the Action for use by subsequent Actions.
*
* @since 0.2.0
*
* @param array $action The array of Action data.
* @param array $data The result of the Action.
*/
public function make_action_save( $action, $data ) {
// Update array of Action results.
$this->set_action_output( $data, $action );
}
/**
* Defines the action by adding a layout.
*
* @since 0.2.0
*
* @param array $layout The existing layout.
* @return array $layout The modified layout.
*/
public function register_layout( $layout ) {
// Build Action Tab.
$action_tab_fields = $this->tab_action_add();
// Build Mapping Tab.
$mapping_tab_fields = $this->tab_mapping_add();
// Build Attachments Tab.
$attachments_tab_fields = $this->tab_attachments_add();
// Combine Sub-Fields.
$layout = array_merge(
$action_tab_fields,
$mapping_tab_fields,
$attachments_tab_fields
);
/**
* Let the classes that extend this one modify the Sub-Fields.
*
* @since 0.2.0
*
* @param array $sub_fields The array of Sub-Fields.
*/
$layout = apply_filters( 'cfafa/acfe/form/v3/actions/sub_fields', $layout );
return $layout;
}
/**
* Defines the "Action" Tab.
*
* These Fields are required to configure the Form Action.
*
* The ACFE "Action name" Field has a pre-defined format, e.g. it must be
* assigned the "acfe_slug" Field Type and have "name" as its "name" and
* "field_name" as its "key". Only its "placeholder" attribute needs to be
* configured.
*
* @since 0.2.0
*
* @return array $fields The array of Fields for this section.
*/
public function tab_action_add() {
// Init Fields array.
$fields = [];
// "Action" Tab wrapper.
$fields[] = [
'key' => 'field_tab_action',
'label' => __( 'Action', 'conditional-form-actions-for-acfe' ),
'name' => '',
'type' => 'tab',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
'data-no-preference' => true,
],
'acfe_permissions' => '',
'placement' => 'top',
'endpoint' => 0,
];
// "Action name" Field.
$fields[] = [
'key' => 'field_name',
'label' => __( 'Action name', 'conditional-form-actions-for-acfe' ),
'name' => 'name',
'type' => 'acfe_slug',
'instructions' => __( '(Required) Name this action so it can be referenced.', 'conditional-form-actions-for-acfe' ),
'required' => 1,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
'data-instruction-placement' => 'field',
],
'acfe_permissions' => '',
'default_value' => '',
'placeholder' => $this->alias_placeholder,
'prepend' => '',
'append' => '',
'maxlength' => '',
];
// Add any further Fields.
$action_extras = $this->tab_action_append();
if ( ! empty( $action_extras ) ) {
$fields = array_merge( $fields, $action_extras );
}
// --<
return $fields;
}
/**
* Defines additional Fields for the "Action" Tab.
*
* @since 0.2.0
*
* @return array $fields The array of Fields for this section.
*/
public function tab_action_append() {
$fields = [];
return $fields;
}
/**
* Defines the "Mapping" Tab.
*
* @since 0.2.0
*
* @return array $fields The array of Fields for this section.
*/
public function tab_mapping_add() {
$fields = [];
return $fields;
}
/**
* Defines the "Mapping" Tab Header.
*
* @since 0.2.0
*
* @param string $label The label for this section.
* @return array $fields The array of Fields for this section.
*/
public function tab_mapping_header( $label = '' ) {
// Set a default label.
if ( empty( $label ) ) {
$label = __( 'Mapping', 'conditional-form-actions-for-acfe' );
}
// "Mapping" Tab wrapper.
$mapping_tab = [
[
'key' => $this->field_key . 'tab_load',
'label' => $label,
'name' => '',
'type' => 'tab',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
'data-no-preference' => true,
],
'acfe_permissions' => '',
'placement' => 'top',
'endpoint' => 0,
],
];
// Combine Fields.
$fields = array_merge(
$mapping_tab
);
// --<
return $fields;
}
/**
* Defines the "Attachments" Tab.
*
* @since 0.2.0
*
* @return array $fields The array of Fields for this section.
*/
public function tab_attachments_add() {
$fields = [];
return $fields;
}
/**
* Defines the "Attachments" Tab Header.
*
* @since 0.2.0
*
* @return array $fields The array of Fields for this section.
*/
public function tab_attachments_header() {
// "Attachments" Tab wrapper.
$attachments_tab = [
[
'key' => $this->field_key . 'tab_attachments',
'label' => __( 'Attachments', 'conditional-form-actions-for-acfe' ),
'name' => '',
'type' => 'tab',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
'data-no-preference' => true,
],
'acfe_permissions' => '',
'placement' => 'top',
'endpoint' => 0,
],
];
// Combine Fields.
$fields = array_merge(
$attachments_tab
);
// --<
return $fields;
}
/**
* Gets the array that defines a "Map Field" for the "Mapping" Tab.
*
* @since 0.2.0
*
* @param string $code The unique code for the Field.
* @param string $label The label for the Field.
* @param array $conditional_logic The conditional logic for the Field.
* @return array $field The array of Field data.
*/
public function mapping_field_get( $code, $label, $conditional_logic = [] ) {
// Build the Field array.
$field = [
'key' => $this->field_key . 'map_' . $code,
'label' => $label,
'name' => $this->field_name . 'map_' . $code,
'type' => 'select',
'instructions' => '',
'required' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'choices' => [],
'default_value' => [],
'allow_null' => 1,
'multiple' => 0,
'ui' => 1,
'return_format' => 'value',
'placeholder' => __( 'Default', 'conditional-form-actions-for-acfe' ),
'ajax' => 1,
'search_placeholder' => __( 'Select a field or enter a custom value or template tag.', 'conditional-form-actions-for-acfe' ),
'allow_custom' => 1,
'conditional_logic' => 0,
'ajax_action' => 'acfe/form/map_field_ajax',
];
// Default conditional logic.
$field['conditional_logic'] = [];
// Maybe replace with custom conditional logic.
if ( ! empty( $conditional_logic ) ) {
$field['conditional_logic'] = $conditional_logic;
}
// --<
return $field;
}
/**
* Prepare the data from an ACFE Form.
*
* @since 0.2.0
*
* @param array $form_data The array of data from the ACFE Form.
* @return array $filtered_data The filtered data.
*/
public function form_data_prepare( $form_data ) {
// Init filtered data.
$filtered_data = [];
// Bail if we have no Form data to save.
if ( empty( $form_data ) ) {
return $filtered_data;
}
// Populate return array from the Form data.
foreach ( $form_data as $param => $value ) {
// Skip if empty but allow (string) "0" as valid data.
if ( empty( $value ) && '0' !== $value ) {
continue;
}
// Maybe decode entities.
if ( is_string( $value ) && ! is_numeric( $value ) ) {
$value = html_entity_decode( $value );
}
// Maybe decode entities in arrays.
if ( is_array( $value ) ) {
array_walk_recursive(
$value,
function( &$item ) {
if ( is_string( $item ) && ! is_numeric( $item ) ) {
$item = html_entity_decode( $item );
}
}
);
}
// Finally add value to return array.
$filtered_data[ $param ] = $value;
}
// --<
return $filtered_data;
}
/**
* Adds the Conditional Field given data from mapped Fields.
*
* @since 0.2.0
*
* @param array $action The array of Action data.
* @return array $action The modified array of Action data.
*/
public function form_conditional_populate( $action ) {
// Build the action key.
$key = $this->field_name . 'map_' . $this->conditional_code;
// Store Conditional Field reference.
$action[ $this->conditional_code . '_ref' ] = $action[ $key ];
// Prepare to apply tags.
$action[ $this->conditional_code ] = $action[ $key ];
// Set ACFE "context". We want unformatted data.
$context = [
'context' => 'save',
'format' => false,
];
// Apply tags to the Conditional Field value.
acfe_apply_tags( $action[ $this->conditional_code ], $context );
// Save a generic conditional result.
$action['conditional'] = $action[ $this->conditional_code ];
// --<
return $action;
}
/**
* Checks the Conditional Field given data from mapped Fields.
*
* @since 0.2.0
*
* @param array $action The array of Action data.
* @return bool $continue True if the Action should continue or false to skip.
*/
public function form_conditional_check( $action ) {
// Approve by default.
$continue = true;
// Skip if the Redirect Conditional Reference Field has a value.
if ( ! empty( $action[ $this->conditional_code . '_ref' ] ) ) {
// And the Redirect Conditional Field has no value.
if ( empty( $action[ $this->conditional_code ] ) ) {
return false;
}
}
// --<
return $continue;
}
}
<?php
/**
* "Conditional Email" ACFE Form Action Class.
*
* Handles the "Conditional Email" ACFE Form Action.
*
* @package Conditional_Form_Actions_For_ACFE
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
/**
* "Conditional Email" ACFE Form Action Class.
*
* A class that handles the "Conditional Email" ACFE Form Action.
*
* @since 0.2.0
*/
class CFAFA_ACFE_Form_Action_Email extends CFAFA_ACFE_Form_Action_Base {
/**
* Plugin object.
*
* @since 0.2.0
* @access public
* @var Conditional_Form_Actions_For_ACFE
*/
public $plugin;
/**
* ACF Extended object.
*
* @since 0.2.0
* @access public
* @var CFAFA_ACFE
*/
public $acfe;
/**
* Form Action Name.
*
* @since 0.2.0
* @access public
* @var string
*/
public $name = 'email_cfafa';
/**
* Form Action Name.
*
* @since 0.2.0
* @access public
* @var string
*/
public $action_name = 'email_cfafa';
/**
* Field Key Prefix.
*
* @since 0.2.0
* @access public
* @var string
*/
public $field_key = 'field_cfafa_email_';
/**
* Field Name Prefix.
*
* @since 0.2.0
* @access public
* @var string
*/
public $field_name = 'cfafa_email_';
/**
* Array of translatable mapped Email Fields.
*
* @since 0.2.0
* @access public
* @var array
*/
public $mapped_email_fields = [];
/**
* Constructor.
*
* @since 0.2.0
*/
public function __construct() {
// Store references to objects.
$this->plugin = cfafa();
$this->acfe = $this->plugin->acfe;
// Label this Form Action.
$this->title = __( 'Conditional Email action', 'conditional-form-actions-for-acfe' );
// Alias Placeholder for this Form Action.
$this->alias_placeholder = __( 'Conditional Email', 'conditional-form-actions-for-acfe' );
// Conditional Code for this Form Action.
$this->conditional_code = 'email_conditional';
// Declare the mapped Email Fields with translatable titles.
$this->mapped_email_fields = [
'from_name' => __( 'From Name', 'conditional-form-actions-for-acfe' ),
'from_email' => __( 'From Email', 'conditional-form-actions-for-acfe' ),
'reply_to_name' => __( 'Reply To Name', 'conditional-form-actions-for-acfe' ),
'reply_to_email' => __( 'Reply To Email', 'conditional-form-actions-for-acfe' ),
'to_name' => __( 'To Name', 'conditional-form-actions-for-acfe' ),
'to_email' => __( 'To Email', 'conditional-form-actions-for-acfe' ),
'cc' => __( 'Cc', 'conditional-form-actions-for-acfe' ),
'bcc' => __( 'Bcc', 'conditional-form-actions-for-acfe' ),
'subject' => __( 'Subject', 'conditional-form-actions-for-acfe' ),
];
// Declare core Fields for this Form Action.
$this->item = [
'action' => $this->name,
'name' => '',
'html' => false,
];
// Declare mapped Fields for this Form Action.
$keys = array_keys( $this->mapped_email_fields );
foreach ( $keys as $key ) {
$this->item[ $this->field_name . 'map_' . $key ] = '';
}
// Declare static Fields for this Form Action.
$this->item[ $this->field_name . 'content' ] = '';
$this->item[ $this->field_name . 'files' ] = [];
$this->item[ $this->field_name . 'files_static' ] = [];
// Declare Conditional Field for this Form Action.
$this->item[ $this->field_name . 'map_' . $this->conditional_code ] = '';
// Init parent.
parent::__construct();
}
/**
* Prepares data for saving when the Form Action is saved.
*
* @since 0.2.0
*
* @param array $action The array of Form Action data.
* @return array $save The array of data to save.
*/
public function prepare_save_action( $action ) {
// Init with default array for this Field.
$save = $this->item;
// Always add action name.
$save['name'] = $action['name'];
// Always save Conditional Field.
$item = $this->field_name . 'map_' . $this->conditional_code;
if ( acf_maybe_get( $action, $item ) ) {
$save[ $item ] = $action[ $item ];
}
// Email data.
$keys = array_keys( $this->mapped_email_fields );
foreach ( $keys as $key ) {
$item = $this->field_name . 'map_' . $key;
if ( acf_maybe_get( $action, $item ) ) {
$save[ $item ] = $action[ $item ];
}
}
// Get Content Type from Content Group.
$group = $action[ $this->field_name . 'content_group' ];
if ( 'editor' === $group[ $this->field_name . 'content_type' ] ) {
$save[ $this->field_name . 'content' ] = $group[ $this->field_name . 'content_editor' ];
} elseif ( 'html' === $group[ $this->field_name . 'content_type' ] ) {
$save[ $this->field_name . 'content' ] = $group[ $this->field_name . 'content_html' ];
$save['html'] = true;
}
// Dynamic Files.
$item = $this->field_name . 'files';
$action[ $item ] = acf_get_array( $action[ $item ] );
foreach ( $action[ $item ] as $row ) {
$save[ $item ][] = $row;
}
// Static Files.
$item = $this->field_name . 'files_static';
$action[ $item ] = acf_get_array( $action[ $item ] );
foreach ( $action[ $item ] as $row ) {
$save[ $item ][] = $row[ $this->field_name . 'file_static' ];
}
// --<
return $save;
}
/**
* Prepares data when the Form Action is loaded.
*
* @since 0.2.0
*
* @param array $action The array of Form Action data.
* @return array $action The array of data to save.
*/
public function prepare_load_action( $action ) {
// Populate Content Editor.
$value = $action[ $this->field_name . 'content' ];
$group = $this->field_name . 'content_group';
if ( true === $action['html'] ) {
$action[ $group ][ $this->field_name . 'content_type' ] = 'html';
$action[ $group ][ $this->field_name . 'content_html' ] = $value;
} else {
$action[ $group ][ $this->field_name . 'content_type' ] = 'editor';
$action[ $group ][ $this->field_name . 'content_editor' ] = $value;
}
// Populate Dynamic Files.
$item = $this->field_name . 'files';
$files = $action[ $item ];
$action[ $item ] = [];
foreach ( $files as $row ) {
$action[ $item ][] = [
$this->field_name . 'file' => $row[ $this->field_name . 'file' ],
$this->field_name . 'file_delete' => $row[ $this->field_name . 'file_delete' ],
];
}
// Populate Static Files.
$item = $this->field_name . 'files_static';
$files = $action[ $item ];
$action[ $item ] = [];
foreach ( $files as $row ) {
$action[ $item ][] = [ $this->field_name . 'file_static' => $row ];
}
// --<
return $action;
}
/**
* Performs validation when the Form the Action is attached to is submitted.
*
* @since 0.2.0
*
* @param array $form The array of Form data.
* @param array $action The array of Action data.
*/
public function validation( $form, $action ) {
/*
// Get some Form details.
$form_name = acf_maybe_get( $form, 'name' );
$form_id = acf_maybe_get( $form, 'ID' );
//acfe_add_validation_error( $selector, $message );
*/
}
/**
* Performs the action when the Form the Action is attached to is submitted.
*
* @since 0.2.0
*
* @param array $form The array of Form data.
* @param array $action The array of Action data.
*/
public function make_action( $form, $action ) {
// Bail if a filter has overridden the action.
if ( false === $this->make_skip( $form, $action ) ) {
return;
}
// Populate Conditional Reference and value.
$action = $this->form_conditional_populate( $action );
// Populate Email data array.
$email = $this->form_email_data( $form, $action );
// Populate Attachments data array.
$attachments = $this->form_attachments_data( $form, $action );
// Check Conditional.
if ( $this->form_conditional_check( $action ) ) {
// Build the arguments.
$args = $this->form_build_args( $email, $attachments, $form, $action );
// Skip when the args have been overridden.
if ( ! empty( $args ) ) {
// Send the Email with the built arguments.
$email = $this->form_email_send( $args, $email );
// Maybe remove Dynamic Attachments.
$this->form_attachments_delete( $attachments );
}
}
// Save the results of this Action for later use.
$this->make_action_save( $action, $email );
}
/**
* Defines additional Fields for the "Action" Tab.
*
* @since 0.2.0
*
* @return array $fields The array of Fields for this section.
*/
public function tab_action_append() {
// Init Fields.
$fields = [];
// Add Conditional Field.
$label = __( 'Conditional On', 'conditional-form-actions-for-acfe' );
$conditional = $this->mapping_field_get( $this->conditional_code, $label );
$conditional['placeholder'] = __( 'Always send', 'conditional-form-actions-for-acfe' );
$conditional['wrapper']['data-instruction-placement'] = 'field';
$conditional['instructions'] = __( 'To send the Email only when a Form Field is populated (e.g. "First Name") link this to the Form Field. To send the Email only when more complex conditions are met, link this to a Hidden Field with value "1" where the conditional logic of that Field shows it when the conditions are met.', 'conditional-form-actions-for-acfe' );
$fields[] = $conditional;
// --<
return $fields;
}
/**
* Defines the "Mapping" Tab.
*
* @since 0.2.0
*
* @return array $fields The array of Fields for this section.
*/
public function tab_mapping_add() {
// Get Tab Header.
$label = __( 'Email', 'conditional-form-actions-for-acfe' );
$mapping_tab_header = $this->tab_mapping_header( $label );
// Build Email Details Accordion.
$mapping_email_accordion = $this->tab_mapping_accordion_email_add();
// Combine Sub-Fields.
$fields = array_merge(
$mapping_tab_header,
$mapping_email_accordion
);
// --<
return $fields;
}
/**
* Defines the Fields in the "Email Fields" Accordion.
*
* @since 0.2.0
*
* @return array $fields The array of Fields for this section.
*/
public function tab_mapping_accordion_email_add() {
// Init return.
$fields = [];
// Add "Mapping" Fields.
foreach ( $this->mapped_email_fields as $name => $title ) {
$fields[] = $this->mapping_field_get( $name, $title );
}
// Define Content Group.
$content_group = [
'key' => $this->field_key . 'content_group',
'label' => __( 'Content', 'conditional-form-actions-for-acfe' ),
'name' => $this->field_name . 'content_group',
'type' => 'group',
'instructions' => sprintf(
/* translators: 1: An opening <code> tag, 2: A closing <code> tag, 3: A line break tag, */
__( 'Render customized email content.%3$s%3$sRender all fields values:%3$s%1$s{fields}%2$s%3$s%3$sRender field value:%3$s%1$s{field:field_abc123}%2$s%3$s%1$s{field:my_field}%2$s', 'conditional-form-actions-for-acfe' ),
'<code>',
'</code>',
'<br />'
),
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'layout' => 'block',
'acfe_seamless_style' => true,
'acfe_group_modal' => 0,
'sub_fields' => [],
];
// Define Content Type Field.
$content_group['sub_fields'][] = [
'key' => $this->field_key . 'content_type',
'label' => '',
'name' => $this->field_name . 'content_type',
'type' => 'select',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'choices' => [
'editor' => __( 'Content Editor', 'conditional-form-actions-for-acfe' ),
'html' => __( 'Raw HTML', 'conditional-form-actions-for-acfe' ),
],
'default_value' => [ 'custom' ],
'allow_null' => 0,
'multiple' => 0,
'ui' => 0,
'return_format' => 'value',
'placeholder' => __( 'Default', 'conditional-form-actions-for-acfe' ),
'ajax' => 0,
'allow_custom' => 0,
];
// Define Content Editor Field.
$content_group['sub_fields'][] = [
'key' => $this->field_key . 'content_editor',
'label' => '',
'name' => $this->field_name . 'content_editor',
'type' => 'wysiwyg',
'instructions' => '',
'required' => 0,
'conditional_logic' => [
[
[
'field' => $this->field_key . 'content_type',
'operator' => '==',
'value' => 'editor',
],
],
],
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'tabs' => 'all',
'toolbar' => 'full',
'media_upload' => 1,
'delay' => 0,
];
// Define Content HTML Field.
$content_group['sub_fields'][] = [
'key' => $this->field_key . 'content_html',
'label' => '',
'name' => $this->field_name . 'content_html',
'type' => 'acfe_code_editor',
'instructions' => '',
'required' => 0,
'conditional_logic' => [
[
[
'field' => $this->field_key . 'content_type',
'operator' => '==',
'value' => 'html',
],
],
],
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'rows' => 18,
];
// Finally, add Content Group.
$fields[] = $content_group;
// --<
return $fields;
}
/**
* Defines the "Attachments" Tab.
*
* @since 0.2.0
*
* @return array $fields The array of Fields for this section.
*/
public function tab_attachments_add() {
// Init tab array.
$attachments_tab = [];
// Add "Dynamic Files" Repeater.
$attachments_tab[] = [
'key' => $this->field_key . 'files',
'label' => __( 'Dynamic Files', 'conditional-form-actions-for-acfe' ),
'name' => $this->field_name . 'files',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'collapsed' => '',
'min' => 0,
'max' => 0,
'layout' => 'table',
'button_label' => __( 'Add File', 'conditional-form-actions-for-acfe' ),
'sub_fields' => [
[
'key' => $this->field_key . 'file',
'label' => __( 'File', 'conditional-form-actions-for-acfe' ),
'name' => $this->field_name . 'file',
'type' => 'select',
'instructions' => '',
'required' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'choices' => [],
'default_value' => [],
'allow_null' => 1,
'multiple' => 0,
'ui' => 1,
'return_format' => 'value',
'placeholder' => '',
'ajax' => 1,
'search_placeholder' => __( 'Select a field or enter a custom value or template tag.', 'conditional-form-actions-for-acfe' ),
'allow_custom' => 1,
'conditional_logic' => 0,
'ajax_action' => 'acfe/form/map_field_ajax',
],
[
'key' => $this->field_key . 'file_delete',
'label' => __( 'Delete File', 'conditional-form-actions-for-acfe' ),
'name' => $this->field_name . 'file_delete',
'type' => 'true_false',
'instructions' => '',
'required' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'acfe_permissions' => '',
'message' => __( 'Delete once submitted', 'conditional-form-actions-for-acfe' ),
'default_value' => 0,
'ui' => 1,
'ui_on_text' => '',
'ui_off_text' => '',
],
],
];
// Add "Static Files" Repeater.
$attachments_tab[] = [
'key' => $this->field_key . 'files_static',
'label' => __( 'Static Files', 'conditional-form-actions-for-acfe' ),
'name' => $this->field_name . 'files_static',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'collapsed' => '',
'min' => 0,
'max' => 0,
'layout' => 'table',
'button_label' => __( 'Add File', 'conditional-form-actions-for-acfe' ),
'sub_fields' => [
[
'key' => $this->field_key . 'file_static',
'label' => __( 'File', 'conditional-form-actions-for-acfe' ),
'name' => $this->field_name . 'file_static',
'type' => 'file',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'acfe_permissions' => '',
'return_format' => 'id',
],
],
];
// Get Tab Header.
$attachments_tab_header = $this->tab_attachments_header();
// Combine Sub-Fields.
$fields = array_merge(
$attachments_tab_header,
$attachments_tab
);
// --<
return $fields;
}
/**
* Builds Email data array from mapped Fields.
*
* @since 0.2.0
*
* @param array $form The array of Form data.
* @param array $action The array of Action data.
* @return array $data The array of results of this Action to save for later use.
*/
public function form_email_data( $form, $action ) {
// Init data array.
$data = [];
// Set ACFE "context". We want to apply tags.
acfe_add_context( [ 'context' => 'display' ] );
// Mapped Email data.
$keys = array_keys( $this->mapped_email_fields );
foreach ( $keys as $key ) {
$index = $this->field_name . 'map_' . $key;
acfe_apply_tags( $action[ $index ] );
$data[ $key ] = $action[ $index ];
}
// Apply tags to Email content.
acfe_apply_tags( $action[ $this->field_name . 'content' ], [ 'unformat' => 'wysiwyg' ] );
// Reset the ACFE "context".
acfe_delete_context( [ 'context' ] );
// Format the Email content.
if ( true === $action['html'] ) {
// Apply Shortcodes to HTML content.
$action[ $this->field_name . 'content' ] = do_shortcode( $action[ $this->field_name . 'content' ] );
} else {
// Apply "the_content" filters to Rich Text Editor content.
$action[ $this->field_name . 'content' ] = apply_filters( 'acf_the_content', $action[ $this->field_name . 'content' ] );
}
// Now add Email content to data.
$data['content'] = $action[ $this->field_name . 'content' ];
// Also save conditional.
$data['conditional'] = $action['conditional'];
// --<
return $data;
}
/**
* Builds Attachment data array from mapped Fields.
*
* @since 0.2.0
*
* @param array $form The array of Form data.
* @param array $action The array of Action data.
* @return array $data The array of Attachment data to save for later use.
*/
public function form_attachments_data( $form, $action ) {
// Init return array.
$data = [
'attachments' => [],
'delete' => [],
];
// Process Dynamic Attachments.
foreach ( $action[ $this->field_name . 'files' ] as $row ) {
// Parse tags but get the data unformatted and raw.
$context = [
'context' => 'save',
'format' => false,
'return' => 'raw',
];
// Find the ID of the File.
$file_field_key = $row[ $this->field_name . 'file' ];
$file_id = acfe_parse_tags( $file_field_key, $context );
// Add to Attachments array.
$data['attachments'][] = get_attached_file( $file_id );
// Maybe add to delete array.
$file_delete = $row[ $this->field_name . 'file_delete' ];
if ( $file_delete ) {
$data['delete'][] = $file_id;
}
}
// Process Static Attachments.
foreach ( $action[ $this->field_name . 'files_static' ] as $file ) {
$data['attachments'][] = get_attached_file( $file );
}
// --<
return $data;
}
/**
* Builds the arguments array.
*
* @since 0.2.0
*
* @param array $email The array of Email data.
* @param array $attachments The array of Attachments data.
* @param array $form The array of Form data.
* @param array $action The array of Action data.
* @return array|bool $args The arguments array, or false on failure.
*/
public function form_build_args( $email, $attachments, $form, $action ) {
// Build "From" and "Reply To" params.
$from = $this->form_email_item_build( $email['from_name'], $email['from_email'] );
$reply_to = $this->form_email_item_build( $email['reply_to_name'], $email['reply_to_email'] );
// Maybe use "From" when "Reply To" is empty.
if ( empty( $reply_to ) ) {
$reply_to = $from;
}
// Build "To" param.
$to = $this->form_email_item_build( $email['to_name'], $email['to_email'] );
// Sanity checks.
if ( empty( $from ) || empty( $to ) ) {
return false;
}
// Build Email headers.
$headers[] = 'From: ' . $from;
if ( ! empty( $reply_to ) ) {
$headers[] = 'Reply-To: ' . $reply_to;
}
if ( ! empty( $email['cc'] ) ) {
$headers[] = 'Cc: ' . $email['cc'];
}
if ( ! empty( $email['bcc'] ) ) {
$headers[] = 'Bcc: ' . $email['bcc'];
}
$headers[] = 'Content-Type: text/html';
$headers[] = 'charset=UTF-8';
// Finally, build args array.
$args = [
'from' => $from,
'to' => $to,
'reply_to' => $reply_to,
'cc' => $email['cc'],
'bcc' => $email['bcc'],
'subject' => $email['subject'],
'content' => $email['content'],
'headers' => $headers,
'attachments' => $attachments['attachments'],
];
// Get the Form name.
$form_name = acf_maybe_get( $form, 'name' );
/**
* Allow others to filter the Email arguments.
*
* Returning false for any of these filters will skip sending the Email.
*
* @since 0.2.0
*
* @param bool $args The array of arguments.
* @param array $form The array of Form data.
* @param array $action The array of Action data.
*/
$filter = 'acfe/form/v3/submit/' . $this->action_name . '/email_args';
$args = apply_filters( $filter, $args, $form, $action );
$args = apply_filters( $filter . '/form=' . $form_name, $args, $form, $action );
if ( ! empty( $action['name'] ) ) {
$args = apply_filters( $filter . '/action=' . $action['name'], $args, $form, $action );
}
// --<
return $args;
}
/**
* Sends the Email given a set of arguments.
*
* @since 0.2.0
*
* @param array $args The array of Email arguments.
* @param array $email The array of Email data.
* @return array|bool $args The array of Email arguments, or false on failure.
*/
public function form_email_send( $args, $email ) {
// Define rules for checking Email headers.
$rules = [
[
'args_key' => 'from',
'value_old' => $this->form_email_item_build( $email['from_name'], $email['from_email'] ),
'header_key' => 'From:',
],
[
'args_key' => 'reply_to',
'value_old' => $this->form_email_item_build( $email['reply_to_name'], $email['reply_to_email'] ),
'header_key' => 'Reply-To:',
],
[
'args_key' => 'cc',
'value_old' => $email['cc'],
'header_key' => 'Cc:',
],
[
'args_key' => 'bcc',
'value_old' => $email['bcc'],
'header_key' => 'Bcc:',
],
];
// Check if the Email headers have been changed.
foreach ( $rules as $rule ) {
$check = acf_maybe_get( $args, $rule['args_key'] );
if ( ! empty( $check ) && $check !== $rule['value_old'] ) {
foreach ( $args['headers'] as &$header ) {
if ( stripos( $header, $rule['header_key'] ) !== 0 ) {
continue;
}
// Repair Email header.
$header = $rule['header_key'] . ' ' . $check;
break;
}
}
}
// Send the Email.
$result = wp_mail( $args['to'], $args['subject'], $args['content'], $args['headers'], $args['attachments'] );
// Bail on failure.
if ( false === $result ) {
return false;
}
// --<
return $args;
}
/**
* Builds an Email item according to the specification.
*
* For example "First Last <foo@bar.com>".
*
* @since 0.2.0
*
* @param string $name The name.
* @param string $email The email.
* @return string $item The built item.
*/
public function form_email_item_build( $name, $email ) {
// Init return.
$item = '';
// Parse item from params.
if ( ! empty( $name ) && ! empty( $email ) ) {
$item = sprintf( '%1$s <%2$s>', $name, $email );
} elseif ( ! empty( $email ) ) {
$item = $email;
}
// --<
return $item;
}
/**
* Deletes the Dynamic Attachments.
*
* @since 0.2.0
*
* @param array $attachments The array of Attachments data.
*/
public function form_attachments_delete( $attachments ) {
// Bail if there are none.
if ( ! empty( $attachments['delete'] ) ) {
return;
}
// Delete all the Dynamic Attachments.
foreach ( $attachments['delete'] as $file_id ) {
wp_delete_attachment( $file_id, true );
}
}
}
<?php
/**
* "WooCommerce Product" ACFE Form Action Class.
*
* Handles the "WooCommerce Product" ACFE Form Action.
*
* @package Conditional_Form_Actions_For_ACFE
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
/**
* "WooCommerce Product" ACFE Form Action Class.
*
* A class that handles the "WooCommerce Product" ACFE Form Action.
*
* @since 0.2.0
*/
class CFAFA_ACFE_Form_Action_Product extends CFAFA_ACFE_Form_Action_Base {
/**
* Plugin object.
*
* @since 0.2.0
* @access public
* @var Conditional_Form_Actions_For_ACFE
*/
public $plugin;
/**
* ACF Extended object.
*
* @since 0.2.0
* @access public
* @var CFAFA_ACFE
*/
public $acfe;
/**
* Form Action Name.
*
* @since 0.2.0
* @access public
* @var string
*/
public $name = 'woo_cfafa_product';
/**
* Form Action Name.
*
* @since 0.2.0
* @access public
* @var string
*/
public $action_name = 'woo_cfafa_product';
/**
* Field Key Prefix.
*
* @since 0.2.0
* @access public
* @var string
*/
public $field_key = 'field_cfafa_woo_product_';
/**
* Field Name Prefix.
*
* @since 0.2.0
* @access public
* @var string
*/
public $field_name = 'cfafa_woo_product_';
/**
* Constructor.
*
* @since 0.2.0
*/
public function __construct() {
// Store references to objects.
$this->plugin = cfafa();
$this->acfe = $this->plugin->acfe;
// Label this Form Action.
$this->title = __( 'WooCommerce Product action', 'conditional-form-actions-for-acfe' );
// Alias Placeholder for this Form Action.
$this->alias_placeholder = __( 'WooCommerce Product', 'conditional-form-actions-for-acfe' );
// Conditional Code for this Form Action.
$this->conditional_code = 'product_conditional';
// Declare core Fields for this Form Action.
$this->item = [
'action' => $this->name,
'name' => '',
'product_id' => false,
];
// Init parent.
parent::__construct();
}
/**
* Prepares data for saving when the Form Action is saved.
*
* @since 0.2.0
*
* @param array $action The array of Form Action data.
* @return array $save The array of data to save.
*/
public function prepare_save_action( $action ) {
// Init with default array for this Field.
$save = $this->item;
// Always add action name.
$save['name'] = $action['name'];
// Always save Conditional Field.
$item = $this->field_name . 'map_' . $this->conditional_code;
if ( acf_maybe_get( $action, $item ) ) {
$save[ $item ] = $action[ $item ];
}
// Product data.
$item = $this->field_name . 'product_id';
if ( acf_maybe_get( $action, $item ) ) {
$save['product_id'] = $action[ $item ];
}
// --<
return $save;
}
/**
* Prepares data when the Form Action is loaded.
*
* @since 0.2.0
*
* @param array $action The array of Form Action data.
* @return array $action The array of data to save.
*/
public function prepare_load_action( $action ) {
// Product data.
$action[ $this->field_name . 'product_id' ] = $action['product_id'];
// --<
return $action;
}
/**
* Performs the action when the Form the Action is attached to is submitted.
*
* @since 0.2.0
*
* @param array $form The array of Form data.
* @param string $action The customised name of the action.
*/
public function make_action( $form, $action ) {
// Bail if a filter has overridden the action.
if ( false === $this->make_skip( $form, $action ) ) {
return;
}
// Populate Conditional Reference and value.
$action = $this->form_conditional_populate( $action );
// Populate Product data array.
$product = $this->form_product_data( $form, $action );
// Check Conditional.
if ( $this->form_conditional_check( $action ) ) {
// Act with the data from the Form.
$product = $this->form_product_save( $product );
}
// Save the results of this Action for later use.
$this->make_action_save( $action, $product );
}
/**
* Defines additional Fields for the "Action" Tab.
*
* @since 0.2.0
*
* @return array $fields The array of Fields for this section.
*/
public function tab_action_append() {
// Init Fields.
$fields = [];
$fields[] = [
'key' => $this->field_key . 'product_id',
'label' => __( 'WooCommerce Product', 'conditional-form-actions-for-acfe' ),
'name' => $this->field_name . 'product_id',
'type' => 'select',
'instructions' => __( 'Use this to add a WooCommerce Product to the Cart.', 'conditional-form-actions-for-acfe' ),
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
'data-instruction-placement' => 'field',
],
'acfe_permissions' => '',
'default_value' => '',
'placeholder' => '',
'allow_null' => 1,
'multiple' => 0,
'ui' => 0,
// 'ajax' => 1,
// 'ajax_action' => 'cfafa_get_products',
'return_format' => 'value',
'choices' => $this->product_choices_get(),
];
// Add Conditional Field.
$label = __( 'Conditional On', 'conditional-form-actions-for-acfe' );
$conditional = $this->mapping_field_get( $this->conditional_code, $label );
$conditional['placeholder'] = __( 'Always add', 'conditional-form-actions-for-acfe' );
$conditional['wrapper']['data-instruction-placement'] = 'field';
$conditional['instructions'] = __( 'To add the Product to the Cart only when a Form Field is populated (e.g. "First Name") link this to the Form Field. To add the Product to the Cart only when more complex conditions are met, link this to a Hidden Field with value "1" where the conditional logic of that Field shows it when the conditions are met.', 'conditional-form-actions-for-acfe' );
$fields[] = $conditional;
// --<
return $fields;
}
/**
* Builds Product data array from mapped Fields.
*
* @since 0.2.0
*
* @param array $form The array of Form data.
* @param array $action The array of Action data.
* @return array $data The array of results of this Action to save for later use.
*/
public function form_product_data( $form, $action ) {
// Init data array.
$data = [];
// Set ACFE "context". We want unformatted data.
$context = [
'context' => 'save',
'format' => false,
];
// Get Product ID.
acfe_apply_tags( $action['product_id'], $context );
$product_id = $action['product_id'];
// Add Product ID.
$data['product_id'] = $product_id;
// Also save conditional.
$data['conditional'] = $action['conditional'];
// --<
return $data;
}
/**
* Sends the WooCommerce Product given data from mapped Fields.
*
* @since 0.2.0
*
* @param array $product_data The array of Product data.
* @return array|bool $product_data The Product data array, or false on failure.
*/
public function form_product_save( $product_data ) {
// Strip out empty Fields.
$product_data = $this->form_data_prepare( $product_data );
// Sanity check.
if ( empty( $product_data['product_id'] ) ) {
return false;
}
// Add the Product to the WooCommerce Cart.
$result = $this->product_add_to_cart( $product_data );
// Bail on failure.
if ( false === $result ) {
return false;
}
// Add Cart Item key to Product data.
$product_data['cart_item_key'] = $result;
// --<
return $product_data;
}
/**
* Gets the WooCommerce Products as an array of options for ACF.
*
* @since 0.2.0
*
* @return array $options The array of Products formatted for ACF.
*/
public function product_choices_get() {
// Init return.
$options = [];
// We need all Products.
$args = [
'limit' => -1,
];
// Query Products.
$products = wc_get_products( $args );
// Build options for ACF select.
if ( $products ) {
foreach ( $products as $product ) {
$options[ (int) $product->get_id() ] = $product->get_name();
}
}
// --<
return $options;
}
/**
* Adds the Product to the Cart.
*
* @since 0.2.0
*
* @param array $product_data The array of Product data.
* @return integer|bool $cart_item_key The key of the Cart Item, or false otherwise.
*/
public function product_add_to_cart( $product_data ) {
// Get the Product.
$product = wc_get_product( $product_data['product_id'] );
// Sanity check.
if ( empty( $product ) ) {
return false;
}
// Some defaults.
$product_id = $product->get_id();
$quantity = 1;
$variation_id = 0;
$variation = [];
$cart_item_data = [];
// Build Cart Item data?
// Add the configured Product to the Cart.
$cart_item_key = WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation, $cart_item_data );
// --<
return $cart_item_key;
}
}
<?php
/**
* "Conditional Redirect" ACFE Form Action Class.
*
* Handles the "Conditional Redirect" ACFE Form Action.
*
* @package Conditional_Form_Actions_For_ACFE
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
/**
* "Conditional Redirect" ACFE Form Action Class.
*
* A class that handles the "Conditional Redirect" ACFE Form Action.
*
* @since 0.2.0
*/
class CFAFA_ACFE_Form_Action_Redirect extends CFAFA_ACFE_Form_Action_Base {
/**
* Plugin object.
*
* @since 0.2.0
* @access public
* @var Conditional_Form_Actions_For_ACFE
*/
public $plugin;
/**
* ACF Extended object.
*
* @since 0.2.0
* @access public
* @var CFAFA_ACFE
*/
public $acfe;
/**
* Form Action Name.
*
* @since 0.2.0
* @access public
* @var string
*/
public $name = 'redirect_cfafa';
/**
* Form Action Name.
*
* @since 0.2.0
* @access public
* @var string
*/
public $action_name = 'redirect_cfafa';
/**
* Field Key Prefix.
*
* @since 0.2.0
* @access public
* @var string
*/
public $field_key = 'field_cfafa_redirect_';
/**
* Field Name Prefix.
*
* @since 0.2.0
* @access public
* @var string
*/
public $field_name = 'cfafa_redirect_';
/**
* Constructor.
*
* @since 0.2.0
*/
public function __construct() {
// Store references to objects.
$this->plugin = cfafa();
$this->acfe = $this->plugin->acfe;
// Label this Form Action.
$this->title = __( 'Conditional Redirect action', 'conditional-form-actions-for-acfe' );
// Alias Placeholder for this Form Action.
$this->alias_placeholder = __( 'Conditional Redirect', 'conditional-form-actions-for-acfe' );
// Conditional Code for this Form Action.
$this->conditional_code = 'redirect_conditional';
// Declare core Fields for this Form Action.
$this->item = [
'action' => $this->name,
'name' => '',
];
// Declare Fields for this Form Action.
$this->item[ $this->field_name . 'redirect_url' ] = '';
// Declare Conditional Field for this Form Action.
$this->item[ $this->field_name . 'map_' . $this->conditional_code ] = '';
// Init parent.
parent::__construct();
}
/**
* Performs the action when the Form the Action is attached to is submitted.
*
* @since 0.2.0
*
* @param array $form The array of Form data.
* @param array $action The array of Action data.
*/
public function make_action( $form, $action ) {
// Bail if a filter has overridden the action.
if ( false === $this->make_skip( $form, $action ) ) {
return;
}
// Populate Conditional Reference and value.
$action = $this->form_conditional_populate( $action );
// Populate Redirect data array.
$conditional_redirect = $this->form_redirect_data( $form, $action );
// Save the results of this Action for later use.
$this->make_action_save( $action, $conditional_redirect );
// Check Conditional.
if ( ! $this->form_conditional_check( $action ) ) {
return;
}
// Strip out any empty Fields.
$redirect_data = $this->form_data_prepare( $conditional_redirect );
// Sanity check.
if ( empty( $redirect_data['redirect_url'] ) ) {
return;
}
/**
* Fire the ACF Extended 0.8.x action.
*
* This is to allow any plugins that perform administrative tasks (e.g. saving
* form submissions) to act before the redirect.
*
* @see https://develop.tadpole.cc/plugins/submission-logs-for-acfe
*
* @since 0.2.0
*
* @param array $form The array of Form data.
do_action( 'acfe/form/submit_form', $form );
*/
// Do the redirect.
wp_safe_redirect( $redirect_data['redirect_url'] );
exit;
}
/**
* Defines additional Fields for the "Action" Tab.
*
* @since 0.2.0
*
* @return array $fields The array of Fields for this section.
*/
public function tab_action_append() {
// Init Fields.
$fields = [];
// Add URL Field.
$fields[] = [
'key' => $this->field_key . 'redirect_url',
'label' => __( 'Redirect URL', 'conditional-form-actions-for-acfe' ),
'name' => $this->field_name . 'redirect_url',
'type' => 'text',
'instructions' => __( 'The URL to redirect to. See "Cheatsheet" tab for all available template tags.', 'conditional-form-actions-for-acfe' ),
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
'data-instruction-placement' => 'field',
],
'acfe_permissions' => '',
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
];
// Add Conditional Field.
$label = __( 'Conditional On', 'conditional-form-actions-for-acfe' );
$conditional = $this->mapping_field_get( $this->conditional_code, $label );
$conditional['placeholder'] = __( 'Always redirect', 'conditional-form-actions-for-acfe' );
$conditional['wrapper']['data-instruction-placement'] = 'field';
$conditional['instructions'] = __( 'To redirect only when a Form Field is populated, link this to the Form Field. To redirect only when more complex conditions are met, link this to a Hidden Field with value "1" where the conditional logic of that Field shows it when the conditions are met.', 'conditional-form-actions-for-acfe' );
$fields[] = $conditional;
// --<
return $fields;
}
/**
* Builds Redirect data array from mapped Fields.
*
* @since 0.2.0
*
* @param array $form The array of Form data.
* @param array $action The array of Action data.
* @return array $data The array of results of this Action to save for later use.
*/
public function form_redirect_data( $form, $action ) {
// Init data array.
$data = [];
// Get Form name.
$form_name = acf_maybe_get( $form, 'name' );
// Set ACFE "context". We want unformatted data.
$context = [
'context' => 'save',
'format' => false,
];
// Get the Action variables.
acfe_apply_tags( $action[ $this->field_name . 'redirect_url' ], $context );
$url = $action[ $this->field_name . 'redirect_url' ];
/**
* Filter the Redirect URL.
*
* @since 0.2.0
*
* @param string $url The Redirect URL.
* @param array $form The array of Form data.
* @param array $action The array of Action data.
*/
$filter = 'acfe/form/v3/submit/' . $this->action_name . '/url';
$url = apply_filters( $filter, $url, $form, $action );
$url = apply_filters( $filter . '/form=' . $form_name, $url, $form, $action );
if ( ! empty( $action['name'] ) ) {
$url = apply_filters( $filter . '/action=' . $action['name'], $url, $form, $action );
}
// Add trimmed URL to the data array.
$data['redirect_url'] = trim( $url );
// Also save conditional.
$data['conditional'] = $action['conditional'];
// --<
return $data;
}
}
......@@ -14,6 +14,9 @@
<!-- Allow slash-delimited hooks. -->
<exclude name="WordPress.NamingConventions.ValidHookName.UseUnderscores" />
<!-- I prefer my control structures. -->
<exclude name="WordPress.WhiteSpace.ControlStructureSpacing.BlankLineAfterEnd" />
<!-- PSR4: Allow short file names. -->
<exclude name="WordPress.Files.FileName.InvalidClassFileName" />
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment