-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwp-trigger-github.php
201 lines (170 loc) · 6.11 KB
/
wp-trigger-github.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* @package WPTriggerGithub
*/
/*
Plugin Name: WP Trigger Github
Plugin URI: https://github.com/gglukmann/wp-trigger-github
Description: Save or update action triggers Github repository_dispatch action
Version: 1.3.0
Author: Gert Glükmann
Author URI: https://github.com/gglukmann
License: GNU General Public License v2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text-Domain: wp-trigger-github
*/
if (!defined('ABSPATH')) {
die;
}
class WPTriggerGithub
{
function __construct()
{
add_action('admin_init', [$this, 'generalSettingsSection']);
add_action('wp_after_insert_post', [$this, 'runHook'], 10, 3);
add_action('wp_dashboard_setup', [$this, 'buildDashboardWidget']);
}
public function activate()
{
flush_rewrite_rules();
$this->generalSettingsSection();
add_option('wp_trigger_github_last_triggered_timestamp');
}
public function deactivate()
{
flush_rewrite_rules();
delete_option('wp_trigger_github_last_triggered_timestamp');
}
function getLastTriggeredTimestamp(){
return get_option('wp_trigger_github_last_triggered_timestamp');
}
function triggerGithubRepositoryDispatch(){
$github_token = get_option('ga_option_token');
$github_username = get_option('ga_option_username');
$github_repo = get_option('ga_option_repo');
if ($github_token && $github_username && $github_repo) {
$url = 'https://api.github.com/repos/' . $github_username . '/' . $github_repo . '/dispatches';
$args = array(
'method' => 'POST',
'body' => json_encode(array(
'event_type' => 'wordpress',
)),
'headers' => array(
'Accept' => 'application/vnd.github.v3+json',
'Content-Type' => 'application/json',
'Authorization' => 'token ' . $github_token
),
);
wp_remote_post($url, $args);
}
}
function runHook($post_id)
{
$post = get_post($post_id);
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (wp_is_post_revision($post_id) || wp_is_post_autosave($post_id)) return;
if ($this->getLastTriggeredTimestamp() == false){
update_option('wp_trigger_github_last_triggered_timestamp', $post->post_modified);
$this->triggerGithubRepositoryDispatch();
} else {
try{
$modified_at_time = new DateTime($post->post_modified);
$last_triggered_time = new DateTime($this->getLastTriggeredTimestamp());
} catch(Exception $e){
update_option('wp_trigger_github_last_triggered_timestamp', $post->post_modified);
$this->triggerGithubRepositoryDispatch();
}
// time since last trigger in seconds
$duration_since_last_trigger = ($last_triggered_time->diff($modified_at_time, true))->s;
// only trigger GitHub action if it's been more than five seconds since the last one was triggered
if ($duration_since_last_trigger > 5){
$this->triggerGithubRepositoryDispatch();
update_option('wp_trigger_github_last_triggered_timestamp', $post->post_modified);
}
}
}
function generalSettingsSection()
{
add_settings_section(
'ga_general_settings_section',
'WP Trigger Github Settings',
[$this, 'mySectionOptionsCallback'],
'general'
);
add_settings_field(
'ga_option_username',
'Repository Owner Name',
[$this, 'myTextboxCallback'],
'general',
'ga_general_settings_section',
['ga_option_username']
);
add_settings_field(
'ga_option_repo',
'Repository Name',
[$this, 'myTextboxCallback'],
'general',
'ga_general_settings_section',
['ga_option_repo']
);
add_settings_field(
'ga_option_token',
'Personal Access Token',
[$this, 'myPasswordCallback'],
'general',
'ga_general_settings_section',
['ga_option_token']
);
add_settings_field(
'ga_option_workflow',
'Actions Workflow Name',
[$this, 'myTextboxCallback'],
'general',
'ga_general_settings_section',
['ga_option_workflow']
);
register_setting('general', 'ga_option_token', 'esc_attr');
register_setting('general', 'ga_option_username', 'esc_attr');
register_setting('general', 'ga_option_repo', 'esc_attr');
register_setting('general', 'ga_option_workflow', 'esc_attr');
}
function mySectionOptionsCallback()
{
echo '<p>Add repository owner name, repository name and generated personal access token to trigger Actions workflow.<br />If you want to see status badge on dashboard, add workflow name.</p>';
}
function myTextboxCallback($args)
{
$option = get_option($args[0]);
echo '<input type="text" id="' . $args[0] . '" name="' . $args[0] . '" value="' . $option . '" />';
}
function myPasswordCallback($args)
{
$option = get_option($args[0]);
echo '<input type="password" id="' . $args[0] . '" name="' . $args[0] . '" value="' . $option . '" />';
}
/**
* Create Dashboard Widget for Github Actions deploy status
*/
function buildDashboardWidget()
{
global $wp_meta_boxes;
wp_add_dashboard_widget('github_actions_dashboard_status', 'Deploy Status', [$this, 'buildDashboardStatus']);
}
function buildDashboardStatus()
{
$github_username = get_option('ga_option_username');
$github_repo = get_option('ga_option_repo');
$github_workflow = rawurlencode(get_option('ga_option_workflow'));
$markup = '<a href="https://github.com/' . $github_username . '/' . $github_repo . '/actions" target="_blank" rel="noopener noreferrer">';
$markup .= '<img src="https://github.com/' . $github_username . '/' . $github_repo . '/actions/workflows/' . $github_workflow . '/badge.svg" alt="Github Actions Status" />';
$markup .= '</a>';
echo $markup;
}
}
if (class_exists('WPTriggerGithub')) {
$WPTriggerGithub = new WPTriggerGithub();
}
// activation
register_activation_hook(__FILE__, array($WPTriggerGithub, 'activate'));
// deactivate
register_deactivation_hook(__FILE__, array($WPTriggerGithub, 'deactivate'));