| 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_Submit_Quiz extends AutomatorWP_Integration_Trigger { |
| 13 |
|
| 14 |
public $integration = 'qsm'; |
| 15 |
public $trigger = 'qsm_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 |
'label' => __( 'User submits a quiz', 'automatorwp' ), |
| 27 |
'select_option' => __( 'User submits <strong>a quiz</strong>', 'automatorwp' ), |
| 28 |
/* translators: %1$s: Post title. %2$s: Number of times. */ |
| 29 |
'edit_label' => sprintf( __( 'User submits %1$s %2$s time(s)', 'automatorwp' ), '{post}', '{times}' ), |
| 30 |
/* translators: %1$s: Post title. */ |
| 31 |
'log_label' => sprintf( __( 'User submits %1$s', 'automatorwp' ), '{post}' ), |
| 32 |
'action' => 'qsm_quiz_submitted', |
| 33 |
'function' => array( $this, 'listener' ), |
| 34 |
'priority' => 10, |
| 35 |
'accepted_args' => 4, |
| 36 |
'options' => array( |
| 37 |
'post' => automatorwp_utilities_post_option( array( |
| 38 |
'name' => __('Quiz:', 'automatorwp'), |
| 39 |
'option_none_label' => __('any quiz', 'automatorwp'), |
| 40 |
'post_type' => 'qsm_quiz' |
| 41 |
) ), |
| 42 |
'times' => automatorwp_utilities_times_option(), |
| 43 |
), |
| 44 |
'tags' => array_merge( |
| 45 |
automatorwp_qsm_get_quiz_tags(), |
| 46 |
automatorwp_utilities_times_tag() |
| 47 |
) |
| 48 |
) ); |
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Trigger listener |
| 54 |
* |
| 55 |
* @since 1.0.0 |
| 56 |
* |
| 57 |
* @param $results_array |
| 58 |
* @param $results_id |
| 59 |
* @param $qmn_quiz_options |
| 60 |
* @param $qmn_array_for_variables |
| 61 |
*/ |
| 62 |
public function listener( $results_array, $results_id, $qmn_quiz_options, $qmn_array_for_variables ) { |
| 63 |
|
| 64 |
$user_id = get_current_user_id(); |
| 65 |
$quiz_id = $qmn_quiz_options->quiz_id; |
| 66 |
|
| 67 |
// Bail if not all details provided |
| 68 |
if ( empty( $quiz_id ) ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
// Get quiz post ID |
| 73 |
$post_id = automatorwp_qsm_get_quiz_post_id( $quiz_id ); |
| 74 |
|
| 75 |
// Bail if no post id |
| 76 |
if ( empty( $post_id ) ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
// Quiz name for tags |
| 81 |
$quiz_name = $qmn_quiz_options->quiz_name; |
| 82 |
$points = $qmn_array_for_variables['total_correct']; |
| 83 |
|
| 84 |
// Trigger submit form event |
| 85 |
automatorwp_trigger_event( array( |
| 86 |
'trigger' => $this->trigger, |
| 87 |
'user_id' => $user_id, |
| 88 |
'quiz_id' => $quiz_id, |
| 89 |
'post_id' => $post_id[0], |
| 90 |
'quiz_name' => $quiz_name, |
| 91 |
'points' => $points, |
| 92 |
) ); |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* User deserves check |
| 98 |
* |
| 99 |
* @since 1.0.0 |
| 100 |
* |
| 101 |
* @param bool $deserves_trigger True if user deserves trigger, false otherwise |
| 102 |
* @param stdClass $trigger The trigger object |
| 103 |
* @param int $user_id The user ID |
| 104 |
* @param array $event Event information |
| 105 |
* @param array $trigger_options The trigger's stored options |
| 106 |
* @param stdClass $automation The trigger's automation object |
| 107 |
* |
| 108 |
* @return bool True if user deserves trigger, false otherwise |
| 109 |
*/ |
| 110 |
public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 111 |
|
| 112 |
// Don't deserve if post is not received |
| 113 |
if ( !isset( $event['post_id'] ) ) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
// Don't deserve if post doesn't match with the trigger option |
| 118 |
if( ! automatorwp_posts_matches( $event['post_id'], $trigger_options['post'] ) ) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
return $deserves_trigger; |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Register the required hooks |
| 128 |
* |
| 129 |
* @since 1.0.0 |
| 130 |
*/ |
| 131 |
public function hooks() { |
| 132 |
|
| 133 |
// Log meta data |
| 134 |
add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 ); |
| 135 |
|
| 136 |
parent::hooks(); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Trigger custom log meta |
| 141 |
* |
| 142 |
* @since 1.0.0 |
| 143 |
* |
| 144 |
* @param array $log_meta Log meta data |
| 145 |
* @param stdClass $trigger The trigger object |
| 146 |
* @param int $user_id The user ID |
| 147 |
* @param array $event Event information |
| 148 |
* @param array $trigger_options The trigger's stored options |
| 149 |
* @param stdClass $automation The trigger's automation object |
| 150 |
* |
| 151 |
* @return array |
| 152 |
*/ |
| 153 |
function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 154 |
|
| 155 |
// Bail if action type don't match this action |
| 156 |
if( $trigger->type !== $this->trigger ) { |
| 157 |
return $log_meta; |
| 158 |
} |
| 159 |
|
| 160 |
$log_meta['quiz_id'] = ( isset( $event['quiz_id'] ) ? $event['quiz_id'] : '' ); |
| 161 |
$log_meta['quiz_name'] = ( isset( $event['quiz_name'] ) ? $event['quiz_name'] : '' ); |
| 162 |
$log_meta['points'] = ( isset( $event['points'] ) ? $event['points'] : '' ); |
| 163 |
|
| 164 |
return $log_meta; |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
new AutomatorWP_QSM_Submit_Quiz(); |