ELEM_SUBMITFORM

Class ELEM_SUBMITFORM

Contents

  • Methods

  • Source Source

    File: src/integrations/elementor/triggers/elem-submitform.php

    class ELEM_SUBMITFORM {
    
    	/**
    	 * Integration code
    	 * @var string
    	 */
    	public static $integration = 'ELEM';
    
    	private $trigger_code;
    	private $trigger_meta;
    
    	/**
    	 * Set up Automator trigger constructor.
    	 */
    	public function __construct() {
    		$this->trigger_code = 'ELEMSUBMITFORM';
    		$this->trigger_meta = 'ELEMFORM';
    		$this->define_trigger();
    	}
    
    	/**
    	 * Define and register the trigger by pushing it into the Automator object
    	 */
    	public function define_trigger() {
    
    
    
    		$trigger = array(
    			'author'              => Automator()->get_author_name(),
    			'support_link'        => Automator()->get_author_support_link( $this->trigger_code, 'integration/elementor/' ),
    			'integration'         => self::$integration,
    			'code'                => $this->trigger_code,
    			/* translators: Logged-in trigger - Forminator */
    			'sentence'            => sprintf( esc_attr__( 'A user submits {{a form:%1$s}}', 'uncanny-automator' ), $this->trigger_meta ),
    			/* translators: Logged-in trigger - Forminator */
    			'select_option_name'  => esc_attr__( 'A user submits {{a form}}', 'uncanny-automator' ),
    			'action'              => 'elementor_pro/forms/new_record',
    			'priority'            => 100,
    			'accepted_args'       => 2,
    			'validation_function' => array( $this, 'elem_submit_form' ),
    			'options'             => [
    				Automator()->helpers->recipe->elementor->options->all_elementor_forms( null, $this->trigger_meta ),
    			],
    		);
    
    		Automator()->register->trigger( $trigger );
    
    		return;
    	}
    
    	/**
    	 * Validation function when the trigger action is hit
    	 *
    	 * @param object $record
    	 * @param object $object
    	 */
    	public function elem_submit_form( $record, $object ) {
    
    		if ( ! $object->is_success ) {
    			return;
    		}
    
    		$form_id = $record->get_form_settings( 'id' );
    		if ( empty( $form_id ) ) {
    			return;
    		}
    
    
    		$user_id    = wp_get_current_user()->ID;
    		$recipes    = Automator()->get->recipes_from_trigger_code( $this->trigger_code );
    		$conditions = $this->match_condition( $form_id, $recipes, $this->trigger_meta, $this->trigger_code );
    
    		if ( ! $conditions ) {
    			return;
    		}
    
    		if ( ! empty( $conditions ) ) {
    			foreach ( $conditions['recipe_ids'] as $recipe_id ) {
    				if ( ! Automator()->is_recipe_completed( $recipe_id, $user_id ) ) {
    					$args = [
    						'code'            => $this->trigger_code,
    						'meta'            => $this->trigger_meta,
    						'recipe_to_match' => $recipe_id,
    						'ignore_post_id'  => true,
    						'user_id'         => $user_id,
    					];
    
    					$args = Automator()->maybe_add_trigger_entry( $args, false );
    					do_action( 'automator_save_elementor_form_entry', $record, $recipes, $args );
    					if ( $args ) {
    						foreach ( $args as $result ) {
    							if ( true === $result['result'] ) {
    								Automator()->maybe_trigger_complete( $result['args'] );
    							}
    						}
    					}
    				}
    			}
    		}
    	}
    
    	/**
    	 * Matching Form id because its not an integer.
    	 *
    	 * @param array $form_id .
    	 * @param array $recipes .
    	 * @param string $trigger_meta .
    	 * @param string $trigger_code .
    	 *
    	 * @return array|bool
    	 */
    	public function match_condition( $form_id, $recipes = null, $trigger_meta = null, $trigger_code = null ) {
    
    		if ( null === $recipes ) {
    			return false;
    		}
    
    		$recipe_ids     = array();
    		$entry_to_match = $form_id;
    
    		foreach ( $recipes as $recipe ) {
    			foreach ( $recipe['triggers'] as $trigger ) {
    				if ( key_exists( $trigger_meta, $trigger['meta'] ) && (string) $trigger['meta'][ $trigger_meta ] === (string) $entry_to_match ) {
    					$recipe_ids[ $recipe['ID'] ] = $recipe['ID'];
    					break;
    				}
    			}
    		}
    
    		if ( ! empty( $recipe_ids ) ) {
    			return [ 'recipe_ids' => $recipe_ids, 'result' => true ];
    		}
    
    		return false;
    	}
    }
    

    Methods Methods