PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.0
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.0
6.0.3 6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 5.6.1 All 34 releases
automatorwp / integrations / qsm / includes / triggers / anonymous-submit-quiz.php

anonymous-submit-quiz.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 6.0.0, at integrations/qsm/includes/triggers/anonymous-submit-quiz.php

174 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Submit Form
4 *
5 * @package AutomatorWP\Integrations\QSM\Triggers\Submit_Form
6 * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]>
7 * @since 1.0.0
8 */
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 class AutomatorWP_QSM_Anonymous_Submit_Quiz extends AutomatorWP_Integration_Trigger {
13
14 public $integration = 'qsm';
15 public $trigger = 'qsm_anonymous_quiz_submitted';
16
17 /**
18 * Register the trigger
19 *
20 * @since 1.0.0
21 */
22 public function register() {
23
24 automatorwp_register_trigger( $this->trigger, array(
25 'integration' => $this->integration,
26 'anonymous' => true,
27 'label' => __( 'Guest submits a quiz', 'automatorwp' ),
28 'select_option' => __( 'Guest submits <strong>a quiz</strong>', 'automatorwp' ),
29 /* translators: %1$s: Post title. %2$s: Number of times. */
30 'edit_label' => sprintf( __( 'Guest submits %1$s %2$s time(s)', 'automatorwp' ), '{post}', '{times}' ),
31 /* translators: %1$s: Post title. */
32 'log_label' => sprintf( __( 'Guest submits %1$s', 'automatorwp' ), '{post}' ),
33 'action' => 'qsm_quiz_submitted',
34 'function' => array( $this, 'listener' ),
35 'priority' => 10,
36 'accepted_args' => 4,
37 'options' => array(
38 'post' => automatorwp_utilities_post_option( array(
39 'name' => __('Quiz:', 'automatorwp'),
40 'option_none_label' => __('any quiz', 'automatorwp'),
41 'post_type' => 'qsm_quiz'
42 ) ),
43 'times' => automatorwp_utilities_times_option(),
44 ),
45 'tags' => array_merge(
46 automatorwp_qsm_get_quiz_tags(),
47 automatorwp_utilities_times_tag()
48 )
49 ) );
50
51 }
52
53 /**
54 * Trigger listener
55 *
56 * @since 1.0.0
57 *
58 * @param $results_array
59 * @param $results_id
60 * @param $qmn_quiz_options
61 * @param $qmn_array_for_variables
62 */
63 public function listener( $results_array, $results_id, $qmn_quiz_options, $qmn_array_for_variables ) {
64
65
66 $user_id = get_current_user_id();
67 $quiz_id = $qmn_quiz_options->quiz_id;
68
69 // Bail if user is logged in
70 if ( $user_id !== 0 ) {
71 return;
72 }
73
74 // Bail if not all details provided
75 if ( empty( $quiz_id ) ) {
76 return;
77 }
78
79 // Get quiz post ID
80 $post_id = automatorwp_qsm_get_quiz_post_id( $quiz_id );
81
82 // Bail if no post id
83 if ( empty( $post_id ) ) {
84 return;
85 }
86
87 // Quiz name for tags
88 $quiz_name = $qmn_quiz_options->quiz_name;
89 $points = $qmn_array_for_variables['total_correct'];
90
91 // Trigger submit form event
92 automatorwp_trigger_event( array(
93 'trigger' => $this->trigger,
94 'post_id' => $post_id[0],
95 'quiz_id' => $quiz_id,
96 'quiz_name' => $quiz_name,
97 'points' => $points,
98 ) );
99
100 }
101
102 /**
103 * Anonymous deserves check
104 *
105 * @since 1.0.0
106 *
107 * @param bool $deserves_trigger True if anonymous deserves trigger, false otherwise
108 * @param stdClass $trigger The trigger object
109 * @param array $event Event information
110 * @param array $trigger_options The trigger's stored options
111 * @param stdClass $automation The trigger's automation object
112 *
113 * @return bool True if anonymous deserves trigger, false otherwise
114 */
115 public function anonymous_deserves_trigger( $deserves_trigger, $trigger, $event, $trigger_options, $automation ) {
116
117 // Don't deserve if post is not received
118 if ( !isset( $event['post_id'] ) ) {
119 return false;
120 }
121
122 // Don't deserve if post doesn't match with the trigger option
123 if( ! automatorwp_posts_matches( $event['post_id'], $trigger_options['post'] ) ) {
124 return false;
125 }
126
127 return $deserves_trigger;
128
129 }
130
131 /**
132 * Register the required hooks
133 *
134 * @since 1.0.0
135 */
136 public function hooks() {
137
138 // Log meta data
139 add_filter( 'automatorwp_anonymous_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 );
140
141 parent::hooks();
142 }
143
144 /**
145 * Trigger custom log meta
146 *
147 * @since 1.0.0
148 *
149 * @param array $log_meta Log meta data
150 * @param stdClass $trigger The trigger object
151 * @param array $event Event information
152 * @param array $trigger_options The trigger's stored options
153 * @param stdClass $automation The trigger's automation object
154 *
155 * @return array
156 */
157 function log_meta( $log_meta, $trigger, $event, $trigger_options, $automation ) {
158
159 // Bail if action type don't match this action
160 if( $trigger->type !== $this->trigger ) {
161 return $log_meta;
162 }
163
164 $log_meta['quiz_id'] = ( isset( $event['quiz_id'] ) ? $event['quiz_id'] : '' );
165 $log_meta['quiz_name'] = ( isset( $event['quiz_name'] ) ? $event['quiz_name'] : '' );
166 $log_meta['points'] = ( isset( $event['points'] ) ? $event['points'] : '' );
167
168 return $log_meta;
169
170 }
171
172 }
173
174 new AutomatorWP_QSM_Anonymous_Submit_Quiz();