| @@ -1,0 +1,140 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Comment Post | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\WordPress\Triggers\Comment_Post | |
| 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_WordPress_Comment_Post extends AutomatorWP_Integration_Trigger { | |
| 13 | + | |
| 14 | + /** | |
| 15 | + * Initialize the trigger | |
| 16 | + * | |
| 17 | + * @since 1.0.0 | |
| 18 | + */ | |
| 19 | + public function __construct( $integration ) { | |
| 20 | + | |
| 21 | + $this->integration = $integration; | |
| 22 | + $this->trigger = $integration . '_comment_post'; | |
| 23 | + | |
| 24 | + parent::__construct(); | |
| 25 | + | |
| 26 | + } | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * Register the trigger | |
| 30 | + * | |
| 31 | + * @since 1.0.0 | |
| 32 | + */ | |
| 33 | + public function register() { | |
| 34 | + | |
| 35 | + automatorwp_register_trigger( $this->trigger, array( | |
| 36 | + 'integration' => $this->integration, | |
| 37 | + 'label' => __( 'User comments on a post', 'automatorwp' ), | |
| 38 | + 'select_option' => __( 'User comments on <strong>a post</strong>', 'automatorwp' ), | |
| 39 | + /* translators: %1$s: Post title. %2$s: Number of times. */ | |
| 40 | + 'edit_label' => sprintf( __( 'User comments on %1$s %2$s time(s)', 'automatorwp' ), '{post}', '{times}' ), | |
| 41 | + /* translators: %1$s: Post title. */ | |
| 42 | + 'log_label' => sprintf( __( 'User comments on %1$s', 'automatorwp' ), '{post}' ), | |
| 43 | + 'action' => array( | |
| 44 | + 'comment_approved_', | |
| 45 | + 'comment_approved_comment', | |
| 46 | + 'wp_insert_comment', | |
| 47 | + ), | |
| 48 | + 'function' => array( $this, 'listener' ), | |
| 49 | + 'priority' => 10, | |
| 50 | + 'accepted_args' => 2, | |
| 51 | + 'options' => array( | |
| 52 | + 'post' => automatorwp_utilities_post_option(), | |
| 53 | + 'times' => automatorwp_utilities_times_option(), | |
| 54 | + ), | |
| 55 | + 'tags' => array_merge( | |
| 56 | + automatorwp_utilities_comment_tags(), | |
| 57 | + automatorwp_utilities_post_tags(), | |
| 58 | + automatorwp_utilities_times_tag() | |
| 59 | + ) | |
| 60 | + ) ); | |
| 61 | + | |
| 62 | + } | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * Trigger listener | |
| 66 | + * | |
| 67 | + * @since 1.0.0 | |
| 68 | + * | |
| 69 | + * @param int $comment_ID The comment ID. | |
| 70 | + * @param array|WP_Comment $comment The Comment. | |
| 71 | + */ | |
| 72 | + public function listener( $comment_ID, $comment ) { | |
| 73 | + | |
| 74 | + // Ensure comment as array (wp_insert_comment uses object, comment_{status}_comment uses array) | |
| 75 | + if ( is_object( $comment ) ) { | |
| 76 | + $comment = get_object_vars( $comment ); | |
| 77 | + } | |
| 78 | + | |
| 79 | + // Check if comment is approved | |
| 80 | + if ( (int) $comment['comment_approved'] !== 1 ) { | |
| 81 | + return; | |
| 82 | + } | |
| 83 | + | |
| 84 | + $post = get_post( absint( $comment[ 'comment_post_ID' ] ) ); | |
| 85 | + | |
| 86 | + // Bail if not post instanced | |
| 87 | + if( ! $post instanceof WP_Post ) { | |
| 88 | + return; | |
| 89 | + } | |
| 90 | + | |
| 91 | + // Bail if post type is not a post | |
| 92 | + if( $post->post_type !== 'post' ) { | |
| 93 | + return; | |
| 94 | + } | |
| 95 | + | |
| 96 | + $user_id = (int) $comment['user_id']; | |
| 97 | + | |
| 98 | + automatorwp_trigger_event( array( | |
| 99 | + 'trigger' => $this->trigger, | |
| 100 | + 'user_id' => $user_id, | |
| 101 | + 'comment_id' => $comment_ID, | |
| 102 | + 'post_id' => $post->ID, | |
| 103 | + ) ); | |
| 104 | + | |
| 105 | + } | |
| 106 | + | |
| 107 | + /** | |
| 108 | + * User deserves check | |
| 109 | + * | |
| 110 | + * @since 1.0.0 | |
| 111 | + * | |
| 112 | + * @param bool $deserves_trigger True if user deserves trigger, false otherwise | |
| 113 | + * @param stdClass $trigger The trigger object | |
| 114 | + * @param int $user_id The user ID | |
| 115 | + * @param array $event Event information | |
| 116 | + * @param array $trigger_options The trigger's stored options | |
| 117 | + * @param stdClass $automation The trigger's automation object | |
| 118 | + * | |
| 119 | + * @return bool True if user deserves trigger, false otherwise | |
| 120 | + */ | |
| 121 | + public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { | |
| 122 | + | |
| 123 | + // Don't deserve if post is not received | |
| 124 | + if( ! isset( $event['post_id'] ) ) { | |
| 125 | + return false; | |
| 126 | + } | |
| 127 | + | |
| 128 | + // Don't deserve if post doesn't match with the trigger option | |
| 129 | + if( ! automatorwp_posts_matches( $event['post_id'], $trigger_options['post'] ) ) { | |
| 130 | + return false; | |
| 131 | + } | |
| 132 | + | |
| 133 | + return $deserves_trigger; | |
| 134 | + | |
| 135 | + } | |
| 136 | + | |
| 137 | +} | |
| 138 | + | |
| 139 | +new AutomatorWP_WordPress_Comment_Post( 'wordpress' ); | |
| 140 | +new AutomatorWP_WordPress_Comment_Post( 'comments' ); | |