@@ -24,46 +24,80 @@ class WPTriggerGithub
2424 function __construct ()
2525 {
2626 add_action ('admin_init ' , [$ this , 'generalSettingsSection ' ]);
27- add_action ('save_post ' , [$ this , 'runHook ' ], 10 , 3 );
27+ add_action ('wp_after_insert_post ' , [$ this , 'runHook ' ], 10 , 3 );
2828 add_action ('wp_dashboard_setup ' , [$ this , 'buildDashboardWidget ' ]);
2929 }
3030
3131 public function activate ()
3232 {
3333 flush_rewrite_rules ();
3434 $ this ->generalSettingsSection ();
35+ add_option ('wp_trigger_github_last_triggered_timestamp ' );
3536 }
3637
3738 public function deactivate ()
3839 {
3940 flush_rewrite_rules ();
41+ delete_option ('wp_trigger_github_last_triggered_timestamp ' );
42+ }
43+
44+ function getLastTriggeredTimestamp (){
45+ return get_option ('wp_trigger_github_last_triggered_timestamp ' );
46+ }
47+
48+ function triggerGithubRepositoryDispatch (){
49+ $ github_token = get_option ('ga_option_token ' );
50+ $ github_username = get_option ('ga_option_username ' );
51+ $ github_repo = get_option ('ga_option_repo ' );
52+
53+ if ($ github_token && $ github_username && $ github_repo ) {
54+ $ url = 'https://api.github.com/repos/ ' . $ github_username . '/ ' . $ github_repo . '/dispatches ' ;
55+ $ args = array (
56+ 'method ' => 'POST ' ,
57+ 'body ' => json_encode (array (
58+ 'event_type ' => 'wordpress ' ,
59+ )),
60+ 'headers ' => array (
61+ 'Accept ' => 'application/vnd.github.v3+json ' ,
62+ 'Content-Type ' => 'application/json ' ,
63+ 'Authorization ' => 'token ' . $ github_token
64+ ),
65+ );
66+
67+ wp_remote_post ($ url , $ args );
68+ }
4069 }
4170
4271 function runHook ($ post_id )
4372 {
73+ $ post = get_post ($ post_id );
74+
4475 if (defined ('DOING_AUTOSAVE ' ) && DOING_AUTOSAVE ) return ;
4576 if (wp_is_post_revision ($ post_id ) || wp_is_post_autosave ($ post_id )) return ;
4677
47- $ github_token = get_option ('ga_option_token ' );
48- $ github_username = get_option ('ga_option_username ' );
49- $ github_repo = get_option ('ga_option_repo ' );
50-
51- if ($ github_token && $ github_username && $ github_repo ) {
52- $ url = 'https://api.github.com/repos/ ' . $ github_username . '/ ' . $ github_repo . '/dispatches ' ;
53- $ args = array (
54- 'method ' => 'POST ' ,
55- 'body ' => json_encode (array (
56- 'event_type ' => 'wordpress '
57- )),
58- 'headers ' => array (
59- 'Accept ' => 'application/vnd.github.v3+json ' ,
60- 'Content-Type ' => 'application/json ' ,
61- 'Authorization ' => 'token ' . $ github_token
62- ),
63- );
64-
65- wp_remote_post ($ url , $ args );
78+ if ($ this ->getLastTriggeredTimestamp () == false ){
79+ update_option ('wp_trigger_github_last_triggered_timestamp ' , $ post ->post_modified );
80+ $ this ->triggerGithubRepositoryDispatch ();
81+ } else {
82+ try {
83+ $ modified_at_time = new DateTime ($ post ->post_modified );
84+ $ last_triggered_time = new DateTime ($ this ->getLastTriggeredTimestamp ());
85+ } catch (Exception $ e ){
86+ update_option ('wp_trigger_github_last_triggered_timestamp ' , $ post ->post_modified );
87+ $ this ->triggerGithubRepositoryDispatch ();
88+ }
89+
90+ // time since last trigger in seconds
91+ $ duration_since_last_trigger = ($ last_triggered_time ->diff ($ modified_at_time , true ))->s ;
92+
93+ // only trigger GitHub action if it's been more than five seconds since the last one was triggered
94+ if ($ duration_since_last_trigger > 5 ){
95+ $ this ->triggerGithubRepositoryDispatch ();
96+ update_option ('wp_trigger_github_last_triggered_timestamp ' , $ post ->post_modified );
97+ }
6698 }
99+
100+
67101 }
68102
69103 function generalSettingsSection ()
0 commit comments