← All changes
|
integrations/geodirectory/includes/triggers/comment-posted.php
+115
-0
5.7.8
→
6.0.4
View file →
| @@ -1,0 +1,115 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Comment_Posted | |
| 5 | + * | |
| 6 | + * @package AutomatorWP\Integrations\GeoDirectory\Triggers\Comment_Posted | |
| 7 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> | |
| 8 | + * @since 1.0.0 | |
| 9 | + */ | |
| 10 | +// Exit if accessed directly | |
| 11 | +if (!defined('ABSPATH')) exit; | |
| 12 | + | |
| 13 | +class AutomatorWP_GeoDirectory_Comment_Posted extends AutomatorWP_Integration_Trigger | |
| 14 | +{ | |
| 15 | + | |
| 16 | + public $integration = 'geodirectory'; | |
| 17 | + public $trigger = 'geodirectory_comment_posted'; | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * Register the trigger | |
| 21 | + * | |
| 22 | + * @since 1.0.0 | |
| 23 | + */ | |
| 24 | + public function register() | |
| 25 | + { | |
| 26 | + automatorwp_register_trigger($this->trigger, array( | |
| 27 | + 'integration' => $this->integration, | |
| 28 | + 'label' => __('User reviews a place', 'automatorwp'), | |
| 29 | + 'select_option' => __('User <strong>reviews</strong> a place', 'automatorwp'), | |
| 30 | + /* translators: %1$s: Place name. %2$s: Number of times. */ | |
| 31 | + 'edit_label' => sprintf(__('User reviews %1$s %2$s time(s)', 'automatorwp'), '{post}', '{times}'), | |
| 32 | + /* translators: %1$s: Place name. */ | |
| 33 | + 'log_label' => sprintf(__('User reviews %1$s', 'automatorwp'), '{post}'), | |
| 34 | + 'action' => 'geodir_after_save_comment', | |
| 35 | + 'function' => array($this, 'listener'), | |
| 36 | + 'priority' => 10, | |
| 37 | + 'accepted_args' => 1, | |
| 38 | + 'options' => array( | |
| 39 | + 'post' => automatorwp_utilities_post_option( array( | |
| 40 | + 'name' => __('Place:', 'automatorwp'), | |
| 41 | + 'option_none_label' => __('any place', 'automatorwp'), | |
| 42 | + 'post_type' => 'gd_place' | |
| 43 | + ) ), | |
| 44 | + 'times' => automatorwp_utilities_times_option(), | |
| 45 | + ), | |
| 46 | + 'tags' => array_merge( | |
| 47 | + automatorwp_utilities_post_tags(), | |
| 48 | + automatorwp_utilities_times_tag() | |
| 49 | + ) | |
| 50 | + )); | |
| 51 | + } | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * Trigger listener | |
| 55 | + * | |
| 56 | + * @since 1.0.0 | |
| 57 | + * | |
| 58 | + * @param array $review | |
| 59 | + */ | |
| 60 | + public function listener( $review ) | |
| 61 | + { | |
| 62 | + | |
| 63 | + // Bail if place id is not provided | |
| 64 | + if ( empty( $review ) ) { | |
| 65 | + return; | |
| 66 | + } | |
| 67 | + | |
| 68 | + //bail if user is not logged | |
| 69 | + $user_id = get_current_user_id(); | |
| 70 | + | |
| 71 | + if ( $user_id === 0 ) { | |
| 72 | + return; | |
| 73 | + } | |
| 74 | + | |
| 75 | + $post_id = $review['comment_post_ID']; | |
| 76 | + | |
| 77 | + automatorwp_trigger_event(array( | |
| 78 | + 'trigger' => $this->trigger, | |
| 79 | + 'user_id' => $user_id, | |
| 80 | + 'post_id' => $post_id, | |
| 81 | + )); | |
| 82 | + | |
| 83 | + } | |
| 84 | + | |
| 85 | + /** | |
| 86 | + * User deserves check | |
| 87 | + * | |
| 88 | + * @since 1.0.0 | |
| 89 | + * | |
| 90 | + * @param bool $deserves_trigger True if user deserves trigger, false otherwise | |
| 91 | + * @param stdClass $trigger The trigger object | |
| 92 | + * @param int $user_id The user ID | |
| 93 | + * @param array $event Event information | |
| 94 | + * @param array $trigger_options The trigger's stored options | |
| 95 | + * @param stdClass $automation The trigger's automation object | |
| 96 | + * | |
| 97 | + * @return bool True if user deserves trigger, false otherwise | |
| 98 | + */ | |
| 99 | + public function user_deserves_trigger($deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation) | |
| 100 | + { | |
| 101 | + // Don't deserve if post is not received | |
| 102 | + if ( !isset( $event['post_id'] ) ) { | |
| 103 | + return false; | |
| 104 | + } | |
| 105 | + | |
| 106 | + // Don't deserve if post doesn't match with the trigger option | |
| 107 | + if( ! automatorwp_posts_matches( $event['post_id'], $trigger_options['post'] ) ) { | |
| 108 | + return false; | |
| 109 | + } | |
| 110 | + | |
| 111 | + return $deserves_trigger; | |
| 112 | + } | |
| 113 | +} | |
| 114 | + | |
| 115 | +new AutomatorWP_GeoDirectory_Comment_Posted(); | |