WPP_POLLSUBMIT::poll_success()


Source Source

File: src/integrations/wp-polls/triggers/wpp-pollsubmit.php

	public function poll_success() {

		if ( isset( $_REQUEST['action'] ) && sanitize_key( $_REQUEST['action'] ) === 'polls' ) {

			// Get Poll ID
			$poll_id = ( isset( $_REQUEST['poll_id'] ) ? (int) sanitize_key( $_REQUEST['poll_id'] ) : 0 );

			// Ensure Poll ID Is Valid
			if ( $poll_id === 0 ) {
				return;
			}

			// Verify Referer
			if ( ! check_ajax_referer( 'poll_' . $poll_id . '-nonce', 'poll_' . $poll_id . '_nonce', false ) ) {
				return;
			}

			$view = sanitize_key( $_REQUEST['view'] );
			if ( 'process' === $view ) {
				$poll_aid_array = array_unique( array_map( 'intval', array_map( 'sanitize_key', explode( ',', $_POST["poll_$poll_id"] ) ) ) );


				$recipes          = Automator()->get->recipes_from_trigger_code( $this->trigger_code );
				$required_poll_id = Automator()->get->meta_from_recipes( $recipes, $this->trigger_meta );

				foreach ( $recipes as $recipe_id => $recipe ) {
					foreach ( $recipe['triggers'] as $trigger ) {
						$trigger_id = $trigger['ID'];//return early for all products
						if (
							isset( $required_poll_id[ $recipe_id ] )
							&& isset( $required_poll_id[ $recipe_id ][ $trigger_id ] )
						) {
							if (
								0 === (int) (string) $required_poll_id[ $recipe_id ][ $trigger_id ] ||
								(string) $poll_id === (string) $required_poll_id[ $recipe_id ][ $trigger_id ]
							) {

								$pass_args = [
									'code'           => $this->trigger_code,
									'meta'           => $this->trigger_meta,
									'ignore_post_id' => true,
									'user_id'        => get_current_user_id(),
								];

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

								if ( isset( $args ) ) {
									foreach ( $args as $result ) {
										if ( true === $result['result'] ) {

											$trigger_meta = [
												'user_id'        => get_current_user_id(),
												'trigger_id'     => $result['args']['trigger_id'],
												'trigger_log_id' => $result['args']['get_trigger_id'],
												'run_number'     => $result['args']['run_number'],
											];

											$trigger_meta['meta_key']   = $this->trigger_meta;
											$trigger_meta['meta_value'] = $poll_id;
											Automator()->insert_trigger_meta( $trigger_meta );

											$trigger_meta['meta_key']   = 'WPPOLLANSWER';
											$trigger_meta['meta_value'] = maybe_serialize( $poll_aid_array );
											Automator()->insert_trigger_meta( $trigger_meta );

											Automator()->maybe_trigger_complete( $result['args'] );
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}