/
uoa-sendwebhook.php
125 lines (98 loc) · 2.6 KB
/
uoa-sendwebhook.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace Uncanny_Automator;
/**
* Class UOA_SENDWEBHOOK
*
* @package Uncanny_Automator
*/
class UOA_SENDWEBHOOK {
use Recipe\Actions;
use Recipe\Webhooks;
/**
* Set up Automator action constructor.
*/
public function __construct() {
$this->setup_action();
// Migrate existing WP -> Webhook action.
$this->maybe_migrate_wp_webhooks();
}
/**
* Setting up Webhook trigger
*
* @return void
*/
protected function setup_action() {
$this->set_integration( 'WEBHOOKS' );
$this->set_action_code( 'WPSENDWEBHOOK' );
$this->set_action_meta( 'WPWEBHOOK' );
$this->set_author( 'Uncanny Automator' );
$this->set_support_link(
Automator()->get_author_support_link(
$this->get_action_code(),
'knowledge-base/send-data-to-a-webhook/?utm_source=uncanny_automator&utm_medium=automator-send_data_to_webhook&utm_content=help_button'
)
);
$this->set_requires_user( false );
/* translators: Action - Uncanny Automator */
$this->set_sentence(
sprintf(
/* translators: Trigger sentence */
esc_attr__( 'Send data to {{a webhook:%1$s}}', 'uncanny-automator' ),
$this->get_action_meta()
)
);
/* translators: Action - Uncanny Automator */
$this->set_readable_sentence(
esc_attr__( 'Send data to {{a webhook}}', 'uncanny-automator' )
);
$this->set_options_group(
Automator()->send_webhook->fields->options_group(
$this->get_action_meta()
)
);
$this->set_buttons(
Automator()->send_webhook->fields->buttons(
$this->get_action_meta(),
$this->get_support_link()
)
);
$this->set_background_processing( true );
// add filter to inject tokens
add_filter(
'automator_action_' . $this->get_action_code() . '_tokens_renderable',
array( $this, 'inject_webhooks_response_tokens' ),
99,
3
);
$this->register_action();
}
/**
* Migrate all existing wp -> webhook actions.
*
* @return void
*/
public function maybe_migrate_wp_webhooks() {
$option_key = 'automator_wpwebhooks_action_moved';
if ( 'yes' === automator_get_option( $option_key ) ) {
return;
}
global $wpdb;
$current_actions = $wpdb->get_results(
$wpdb->prepare(
"SELECT post_id FROM $wpdb->postmeta WHERE meta_value = %s AND meta_key = %s",
'WPSENDWEBHOOK',
'code'
)
);
if ( empty( $current_actions ) ) {
update_option( $option_key, 'yes', true );
return;
}
foreach ( $current_actions as $action ) {
$action_id = $action->post_id;
update_post_meta( $action_id, 'integration', 'WEBHOOKS' );
update_post_meta( $action_id, 'integration_name', 'Webhooks' );
}
update_option( $option_key, 'yes', true );
}
}