PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 5.8.6
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v5.8.6
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 / shortlinkspro / includes / triggers / click-link-from-category.php

click-link-from-category.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 5.8.6, at integrations/shortlinkspro/includes/triggers/click-link-from-category.php

170 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Click a link from a category
4 *
5 * @package AutomatorWP\Integrations\ShortLinksPro\Triggers\Click_Link_From_Category
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_ShortLinksPro_Click_Link_From_Category extends AutomatorWP_Integration_Trigger {
13
14 public $integration = 'shortlinkspro';
15 public $trigger = 'shortlinkspro_click_link_from_category';
16
17 /**
18 * Register the trigger
19 *
20 * @since 1.0.0
21 */
22 public function register() {
23 automatorwp_register_trigger( $this->trigger, array(
24 'integration' => $this->integration,
25 'label' => __( 'User clicks a link from a category', 'automatorwp' ),
26 'select_option' => __( 'User clicks <strong>a link</strong> from a category', 'automatorwp' ),
27 /* translators: %1$s: Link title. %2$s: Number of times. */
28 'edit_label' => sprintf( __( 'User clicks a link from %1$s %2$s time(s)', 'automatorwp' ), '{post}', '{times}' ),
29 /* translators: %1$s: Link title. */
30 'log_label' => sprintf( __( 'User clicks %1$s', 'automatorwp' ), '{post}' ),
31 'action' => 'shortlinkspro_before_redirect',
32 'function' => array( $this, 'listener' ),
33 'priority' => 10,
34 'accepted_args' => 3,
35 'options' => array(
36 'post' => automatorwp_utilities_ajax_selector_option( array(
37 'field' => 'post',
38 'name' => __( 'Category:', 'automatorwp' ),
39 'option_none_value' => 'any',
40 'option_none_label' => __( 'any category', 'automatorwp' ),
41 'action_cb' => 'automatorwp_shortlinkspro_get_categories',
42 'options_cb' => 'automatorwp_shortlinkspro_options_cb_category',
43 'default' => 'any'
44 ) ),
45 'times' => automatorwp_utilities_times_option(),
46 ),
47 'tags' => array_merge(
48 automatorwp_shortlinkspro_get_link_tags(),
49 automatorwp_utilities_times_tag()
50 )
51
52 ) );
53 }
54
55 /**
56 * Trigger listener
57 *
58 * @since 1.0.0
59 *
60 * @param stdClass $link Link object
61 * @param string $parameters The query parameters
62 * @param string $url URL to redirect
63 */
64 public function listener( $link, $parameters, $url ) {
65
66 $user_id = get_current_user_id();
67
68 // Login is required
69 if ( $user_id === 0 ) {
70 return;
71 }
72
73 // Shorthand
74 $link_id = $link->id;
75 $link_slug = $link->slug;
76
77 $categories = automatorwp_shortlinkspro_get_categories_from_link( $link_id );
78
79 // Bail if not categories
80 if ( ! $categories )
81 return;
82
83 foreach ( $categories as $category ) {
84
85 // Trigger link categories event
86 automatorwp_trigger_event( array(
87 'trigger' => $this->trigger,
88 'user_id' => $user_id,
89 'link_id' => $link_id,
90 'link_slug' => $link_slug,
91 'category_id' => $category->id,
92 ) );
93
94 }
95
96 }
97
98 /**
99 * User deserves check
100 *
101 * @since 1.0.0
102 *
103 * @param bool $deserves_trigger True if user deserves trigger, false otherwise
104 * @param stdClass $trigger The trigger object
105 * @param int $user_id The user ID
106 * @param array $event Event information
107 * @param array $trigger_options The trigger's stored options
108 * @param stdClass $automation The trigger's automation object
109 *
110 * @return bool True if user deserves trigger, false otherwise
111 */
112 public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) {
113
114 // Don't deserve if post is not received
115 if( ! isset( $event['category_id'] ) ) {
116 return false;
117 }
118
119 // Bail if post doesn't match with the trigger option
120 if( $trigger_options['post'] !== 'any' && absint( $event['category_id'] ) !== absint( $trigger_options['post'] ) ) {
121 return false;
122 }
123
124 return $deserves_trigger;
125
126 }
127
128 /**
129 * Register the required hooks
130 *
131 * @since 1.0.0
132 */
133 public function hooks() {
134
135 // Log meta data
136 add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 );
137
138 parent::hooks();
139 }
140
141 /**
142 * Trigger custom log meta
143 *
144 * @since 1.0.0
145 *
146 * @param array $log_meta Log meta data
147 * @param stdClass $trigger The trigger object
148 * @param int $user_id The user ID
149 * @param array $event Event information
150 * @param array $trigger_options The trigger's stored options
151 * @param stdClass $automation The trigger's automation object
152 *
153 * @return array
154 */
155 function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) {
156
157 // Bail if action type don't match this action
158 if( $trigger->type !== $this->trigger ) {
159 return $log_meta;
160 }
161
162 $log_meta['link_slug'] = ( isset( $event['link_slug'] ) ? $event['link_slug'] : '' );
163
164 return $log_meta;
165
166 }
167
168 }
169
170 new AutomatorWP_ShortLinksPro_Click_Link_From_Category();