Plugin Directory

Changeset 2787827


Ignore:
Timestamp:
09/20/2022 08:11:42 PM (4 years ago)
Author:
glukmann
Message:

Fix triggering many times

Location:
wp-trigger-github
Files:
4 edited
1 copied

Legend:

Unmodified
Added
Removed
  • wp-trigger-github/tags/1.3.0/readme.txt

    r2609892 r2787827  
    55License URI: https://www.gnu.org/licenses/gpl-2.0.html
    66Stable tag: trunk
    7 Tested up to: 5.8.1
     7Tested up to: 5.9.4
    88
    99== Description ==
  • wp-trigger-github/tags/1.3.0/wp-trigger-github.php

    r2609892 r2787827  
    88Plugin URI: https://github.com/gglukmann/wp-trigger-github
    99Description: Save or update action triggers Github repository_dispatch action
    10 Version: 1.2.4
     10Version: 1.3.0
    1111Author: Gert Glükmann
    1212Author URI: https://github.com/gglukmann
     
    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  }
     
    3333    flush_rewrite_rules();
    3434    $this->generalSettingsSection();
     35    add_option('wp_trigger_github_last_triggered_timestamp');
    3536  }
    3637
     
    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
  • wp-trigger-github/trunk/readme.txt

    r2609892 r2787827  
    55License URI: https://www.gnu.org/licenses/gpl-2.0.html
    66Stable tag: trunk
    7 Tested up to: 5.8.1
     7Tested up to: 5.9.4
    88
    99== Description ==
  • wp-trigger-github/trunk/wp-trigger-github.php

    r2609892 r2787827  
    88Plugin URI: https://github.com/gglukmann/wp-trigger-github
    99Description: Save or update action triggers Github repository_dispatch action
    10 Version: 1.2.4
     10Version: 1.3.0
    1111Author: Gert Glükmann
    1212Author URI: https://github.com/gglukmann
     
    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  }
     
    3333    flush_rewrite_rules();
    3434    $this->generalSettingsSection();
     35    add_option('wp_trigger_github_last_triggered_timestamp');
    3536  }
    3637
     
    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
Note: See TracChangeset for help on using the changeset viewer.