PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.1
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.1
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 / wordpress / triggers / anonymous-comment-post-taxonomy.php

anonymous-comment-post-taxonomy.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 6.0.1, at integrations/wordpress/triggers/anonymous-comment-post-taxonomy.php

159 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Anonymous Comment Post Taxonomy
4 *
5 * @package AutomatorWP\Integrations\WordPress\Triggers\Anonymous_Comment_Post_Taxonomy
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_Anonymous_Comment_Post_Taxonomy 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 . '_anonymous_comment_post_taxonomy';
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 'anonymous' => true,
38 'label' => __( 'Guest comments on a post of a taxonomy', 'automatorwp' ),
39 'select_option' => __( 'Guest comments on a post of <strong>a taxonomy</strong>', 'automatorwp' ),
40 /* translators: %1$s: Term title. %2$s: Number of times. */
41 'edit_label' => sprintf( __( 'Guest comments on a post of %1$s %2$s time(s)', 'automatorwp' ), '{term}', '{times}' ),
42 /* translators: %1$s: Term title. */
43 'log_label' => sprintf( __( 'Guest comments on a post of %1$s', 'automatorwp' ), '{term}' ),
44 'action' => array(
45 'comment_approved_',
46 'comment_approved_comment',
47 'wp_insert_comment',
48 ),
49 'function' => array( $this, 'listener' ),
50 'priority' => 10,
51 'accepted_args' => 2,
52 'options' => array(
53 'term' => automatorwp_utilities_taxonomy_option(),
54 'times' => automatorwp_utilities_times_option(),
55 ),
56 'tags' => array_merge(
57 automatorwp_utilities_comment_tags(),
58 automatorwp_utilities_post_tags(),
59 automatorwp_utilities_times_tag()
60 )
61 ) );
62
63 }
64
65 /**
66 * Trigger listener
67 *
68 * @since 1.0.0
69 *
70 * @param int $comment_ID The comment ID.
71 * @param array|WP_Comment $comment The Comment.
72 */
73 public function listener( $comment_ID, $comment ) {
74
75 // Ensure comment as array (wp_insert_comment uses object, comment_{status}_comment uses array)
76 if ( is_object( $comment ) ) {
77 $comment = get_object_vars( $comment );
78 }
79
80 // Check if comment is approved
81 if ( (int) $comment['comment_approved'] !== 1 ) {
82 return;
83 }
84
85 $post = get_post( absint( $comment[ 'comment_post_ID' ] ) );
86
87 // Bail if not post instanced
88 if( ! $post instanceof WP_Post ) {
89 return;
90 }
91
92 $taxonomies = get_object_taxonomies( $post->post_type );
93
94 foreach( $taxonomies as $taxonomy ) {
95
96 $terms_ids = automatorwp_get_term_ids( $post->ID, $taxonomy );
97
98 // Bail if post isn't assigned to any category
99 if( empty( $terms_ids ) ) {
100 continue;
101 }
102
103 $user_id = (int) $comment['user_id'];
104
105 // Bail if comment assigned to a user
106 if( $user_id !== 0 ) {
107 return;
108 }
109
110 automatorwp_trigger_event( array(
111 'trigger' => $this->trigger,
112 'post_id' => $post->ID,
113 'comment_id' => $comment_ID,
114 'taxonomy' => $taxonomy,
115 'terms_ids' => $terms_ids,
116 ) );
117
118 }
119
120 }
121
122 /**
123 * Anonymous deserves check
124 *
125 * @since 1.0.0
126 *
127 * @param bool $deserves_trigger True if anonymous deserves trigger, false otherwise
128 * @param stdClass $trigger The trigger object
129 * @param array $event Event information
130 * @param array $trigger_options The trigger's stored options
131 * @param stdClass $automation The trigger's automation object
132 *
133 * @return bool True if anonymous deserves trigger, false otherwise
134 */
135 public function anonymous_deserves_trigger( $deserves_trigger, $trigger, $event, $trigger_options, $automation ) {
136
137 // Don't deserve if post is not received
138 if( ! isset( $event['post_id'] ) && ! isset( $event['taxonomy'] ) && ! isset( $event['terms_ids'] ) ) {
139 return false;
140 }
141
142 // Don't deserve if taxonomy doesn't match with the trigger option
143 if( $trigger_options['taxonomy'] !== 'any' && $trigger_options['taxonomy'] !== $event['taxonomy'] ) {
144 return false;
145 }
146
147 // Don't deserve if term doesn't match with the trigger option
148 if( ! automatorwp_terms_matches( $event['terms_ids'], $trigger_options['term'] ) ) {
149 return false;
150 }
151
152 return $deserves_trigger;
153
154 }
155
156 }
157
158 new AutomatorWP_WordPress_Anonymous_Comment_Post_Taxonomy( 'wordpress' );
159 new AutomatorWP_WordPress_Anonymous_Comment_Post_Taxonomy( 'comments' );