← All changes
|
integrations/wpdiscuz/includes/triggers/vote-comment.php
+144
-0
5.7.4
→
6.0.3
View file →
| @@ -1,0 +1,144 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Vote Comment | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\wpDiscuz\Triggers\Vote_Comment | |
| 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_wpDiscuz_Vote_Comment extends AutomatorWP_Integration_Trigger { | |
| 13 | + | |
| 14 | + public $integration = 'wpdiscuz'; | |
| 15 | + public $trigger = 'wpdiscuz_vote_comment'; | |
| 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 votes up/down a comment', 'automatorwp' ), | |
| 27 | + 'select_option' => __( 'User <strong>votes up/down</strong> a comment', 'automatorwp' ), | |
| 28 | + /* translators: %1$s: Vote Up/Down. %2$s: Number of times. */ | |
| 29 | + 'edit_label' => sprintf( __( 'User %1$s a comment %2$s time(s)', 'automatorwp' ), '{vote}', '{times}' ), | |
| 30 | + /* translators: %1$s: Post title. */ | |
| 31 | + 'log_label' => sprintf( __( 'User %1$s a comment', 'automatorwp' ), '{vote}' ), | |
| 32 | + 'action' => 'automatorwp_wpdiscuz_add_vote', | |
| 33 | + 'function' => array( $this, 'listener' ), | |
| 34 | + 'priority' => 10, | |
| 35 | + 'accepted_args' => 2, | |
| 36 | + 'options' => array( | |
| 37 | + 'vote' => array( | |
| 38 | + 'from' => 'vote', | |
| 39 | + 'fields' => array( | |
| 40 | + 'vote' => array( | |
| 41 | + 'name' => __( 'Vote:', 'automatorwp' ), | |
| 42 | + 'type' => 'select', | |
| 43 | + 'options' => array( | |
| 44 | + 'vote_up' => __( 'votes up', 'automatorwp' ), | |
| 45 | + 'vote_down' => __( 'votes down', 'automatorwp' ), | |
| 46 | + ), | |
| 47 | + 'default' => 'vote_up' | |
| 48 | + ) | |
| 49 | + ) | |
| 50 | + ), | |
| 51 | + 'times' => automatorwp_utilities_times_option(), | |
| 52 | + ), | |
| 53 | + 'tags' => array_merge( | |
| 54 | + automatorwp_utilities_comment_tags(), | |
| 55 | + automatorwp_utilities_post_tags(), | |
| 56 | + automatorwp_utilities_times_tag() | |
| 57 | + ) | |
| 58 | + ) ); | |
| 59 | + | |
| 60 | + } | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * Trigger listener | |
| 64 | + * | |
| 65 | + * @since 1.0.0 | |
| 66 | + * | |
| 67 | + * @param int $vote 1 on vote up, -1 on vote down | |
| 68 | + * @param WP_Comment $comment Comment object | |
| 69 | + */ | |
| 70 | + public function listener( $vote, $comment ) { | |
| 71 | + | |
| 72 | + // Bail if this trigger is not in use | |
| 73 | + if( ! automatorwp_is_trigger_in_use( $this->trigger ) ) { | |
| 74 | + return; | |
| 75 | + } | |
| 76 | + | |
| 77 | + $comment_id = $comment->comment_ID; | |
| 78 | + $user_id = get_current_user_id(); | |
| 79 | + $comment_author_id = automatorwp_wpdiscuz_get_commment_user_id( $comment ); | |
| 80 | + $post_id = absint( $comment->comment_post_ID ); | |
| 81 | + | |
| 82 | + // Bail if voter is not logged in | |
| 83 | + if( $user_id === 0 ) { | |
| 84 | + return; | |
| 85 | + } | |
| 86 | + | |
| 87 | + // Bail if user has voted himself | |
| 88 | + if( $user_id === $comment_author_id ) { | |
| 89 | + return; | |
| 90 | + } | |
| 91 | + | |
| 92 | + // Trigger the vote | |
| 93 | + automatorwp_trigger_event( array( | |
| 94 | + 'trigger' => $this->trigger, | |
| 95 | + 'user_id' => $user_id, | |
| 96 | + 'comment_id' => $comment_id, | |
| 97 | + 'post_id' => $post_id, | |
| 98 | + 'vote' => $vote, | |
| 99 | + ) ); | |
| 100 | + | |
| 101 | + } | |
| 102 | + | |
| 103 | + /** | |
| 104 | + * User deserves check | |
| 105 | + * | |
| 106 | + * @since 1.0.0 | |
| 107 | + * | |
| 108 | + * @param bool $deserves_trigger True if user deserves trigger, false otherwise | |
| 109 | + * @param stdClass $trigger The trigger object | |
| 110 | + * @param int $user_id The user ID | |
| 111 | + * @param array $event Event information | |
| 112 | + * @param array $trigger_options The trigger's stored options | |
| 113 | + * @param stdClass $automation The trigger's automation object | |
| 114 | + * | |
| 115 | + * @return bool True if user deserves trigger, false otherwise | |
| 116 | + */ | |
| 117 | + public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { | |
| 118 | + | |
| 119 | + // Don't deserve if post is not received | |
| 120 | + if( ! isset( $event['vote'] ) ) { | |
| 121 | + return false; | |
| 122 | + } | |
| 123 | + | |
| 124 | + // Don't deserve if vote doesn't match with the trigger option | |
| 125 | + switch ( $trigger_options['vote'] ) { | |
| 126 | + case 'vote_up': | |
| 127 | + if( $event['vote'] < 0 ) { | |
| 128 | + return false; | |
| 129 | + } | |
| 130 | + break; | |
| 131 | + case 'vote_down': | |
| 132 | + if( $event['vote'] > 0 ) { | |
| 133 | + return false; | |
| 134 | + } | |
| 135 | + break; | |
| 136 | + } | |
| 137 | + | |
| 138 | + return $deserves_trigger; | |
| 139 | + | |
| 140 | + } | |
| 141 | + | |
| 142 | +} | |
| 143 | + | |
| 144 | +new AutomatorWP_wpDiscuz_Vote_Comment(); | |