poll-submitted.php
120 lines
| 1 | <?php |
| 2 | /** |
| 3 | * PollSubmitted. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category PollSubmitted |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\WpPolls\Triggers; |
| 15 | |
| 16 | use SureTriggers\Controllers\AutomationController; |
| 17 | use SureTriggers\Integrations\WpPolls\WpPolls; |
| 18 | use SureTriggers\Traits\SingletonLoader; |
| 19 | |
| 20 | if ( ! class_exists( 'PollSubmitted' ) ) : |
| 21 | |
| 22 | /** |
| 23 | * PollSubmitted |
| 24 | * |
| 25 | * @category PollSubmitted |
| 26 | * @package SureTriggers |
| 27 | * @author BSF <username@example.com> |
| 28 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 29 | * @link https://www.brainstormforce.com/ |
| 30 | * @since 1.0.0 |
| 31 | * |
| 32 | * @psalm-suppress UndefinedTrait |
| 33 | */ |
| 34 | class PollSubmitted { |
| 35 | |
| 36 | /** |
| 37 | * Integration type. |
| 38 | * |
| 39 | * @var string |
| 40 | */ |
| 41 | public $integration = 'WpPolls'; |
| 42 | |
| 43 | /** |
| 44 | * Trigger name. |
| 45 | * |
| 46 | * @var string |
| 47 | */ |
| 48 | public $trigger = 'wp_polls_poll_submitted'; |
| 49 | |
| 50 | use SingletonLoader; |
| 51 | |
| 52 | /** |
| 53 | * Constructor |
| 54 | * |
| 55 | * @since 1.0.0 |
| 56 | */ |
| 57 | public function __construct() { |
| 58 | add_filter( 'sure_trigger_register_trigger', [ $this, 'register' ] ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Register action. |
| 63 | * |
| 64 | * @param array $triggers trigger data. |
| 65 | * @return array |
| 66 | */ |
| 67 | public function register( $triggers ) { |
| 68 | |
| 69 | $triggers[ $this->integration ][ $this->trigger ] = [ |
| 70 | 'label' => __( 'Poll Submitted', 'suretriggers' ), |
| 71 | 'action' => 'wp_polls_poll_submitted', |
| 72 | 'common_action' => 'wp_polls_vote_poll_success', |
| 73 | 'function' => [ $this, 'trigger_listener' ], |
| 74 | 'priority' => 10, |
| 75 | 'accepted_args' => 0, |
| 76 | ]; |
| 77 | |
| 78 | return $triggers; |
| 79 | |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Trigger listener |
| 84 | * |
| 85 | * @since 1.0.0 |
| 86 | * @return void |
| 87 | */ |
| 88 | public function trigger_listener() { |
| 89 | if ( ! isset( $_POST['poll_id'] ) || ! isset( $_POST[ 'poll_' . $_POST['poll_id'] ] ) ) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | $poll_id = (int) sanitize_key( $_POST['poll_id'] ); |
| 94 | |
| 95 | if ( ! check_ajax_referer( 'poll_' . $poll_id . '-nonce', 'poll_' . $poll_id . '_nonce', false ) ) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | $selected_answers_ids = sanitize_text_field( $_POST[ 'poll_' . $_POST['poll_id'] ] ); |
| 100 | |
| 101 | $context = WpPolls::get_poll_context( $selected_answers_ids, $poll_id ); |
| 102 | |
| 103 | AutomationController::sure_trigger_handle_trigger( |
| 104 | [ |
| 105 | 'trigger' => $this->trigger, |
| 106 | 'context' => $context, |
| 107 | ] |
| 108 | ); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Ignore false positive |
| 114 | * |
| 115 | * @psalm-suppress UndefinedMethod |
| 116 | */ |
| 117 | PollSubmitted::get_instance(); |
| 118 | |
| 119 | endif; |
| 120 |