WPWH_TRIGGERTRIGGERED::save_data( $response,  $url,  $http_args,  $webhook )

Validation function when the trigger action is hit


Source Source

File: src/integrations/wp-webhooks/triggers/wpwh-triggertriggered.php

	public function save_data( $response, $url, $http_args, $webhook ) {


		if ( ! isset( $webhook['webhook_name'] ) || empty( $webhook['webhook_name'] ) ) {
			return;
		}
		$trigger = $webhook['webhook_name'];

		$body_data_format = 'json';
		if ( isset( $webhook['settings']['wpwhpro_trigger_response_type'] ) ) {
			$body_data_format = $webhook['settings']['wpwhpro_trigger_response_type'];
		}

		$recipes = Automator()->get->recipes_from_trigger_code( $this->trigger_code );

		$conditions = Automator()->helpers->recipe->wp_webhooks->options->match_action_condition( $trigger, $recipes, $this->trigger_meta, $this->trigger_code );

		if ( ! $conditions ) {
			return;
		}

		$user_id = get_current_user_id();
		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,
					];

					$result = Automator()->maybe_add_trigger_entry( $args, false );

					if ( $result ) {
						foreach ( $result as $r ) {
							if ( true === $r['result'] ) {
								$_args = array();
								if ( isset( $r['args'] ) && isset( $r['args']['get_trigger_id'] ) ) {
									//Saving params in trigger log meta for token parsing!
									$_args = [
										'trigger_id'     => (int) $r['args']['trigger_id'],
										'meta_key'       => $this->trigger_meta . '_request_body',
										'user_id'        => $user_id,
										'trigger_log_id' => $r['args']['get_trigger_id'],
										'run_number'     => $r['args']['run_number'],
									];

									$params = $http_args['body'];
									if ( 'json' === $body_data_format ) {
										// convert json to array
										$params = json_decode( $params, true );
									} elseif ( 'xml' === $body_data_format ) {
										// convert xml to array
										$xml_data = new \SimpleXMLElement( $params );
										$params   = Automator()->helpers->recipe->wp_webhooks->options->XML2Array( $xml_data );
									}

									Automator()->helpers->recipe->wp_webhooks->options->extract_and_save_data( $params, $_args );
								}
								Automator()->maybe_trigger_complete( $r['args'] );
							}
						}
					}
				}
			}
		}
	}