Last active
September 21, 2021 10:37
-
-
Save BrianHenryIE/7b93bd2e3ef93fe23a5dce734f14f462 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/includes/class-wcslack-settings-legacy.php b/includes/class-wcslack-settings-legacy.php | |
index 6f7be3a..c4102aa 100644 | |
--- a/includes/class-wcslack-settings-legacy.php | |
+++ b/includes/class-wcslack-settings-legacy.php | |
@@ -618,7 +618,6 @@ if ( ! class_exists( 'WC_Slack_Settings' ) ) { | |
if ( isset( $_GET['wcslack_reload_channels'] ) && ( $_GET['wcslack_reload_channels'] == 1 ) ) { | |
delete_transient( 'wcslack_all_channels' ); | |
- delete_transient( 'wcslack_all_groups' ); | |
wp_safe_redirect( get_admin_url() . 'admin.php?page=wc-settings&tab=integration§ion=wcslack' ); | |
@@ -706,19 +705,13 @@ if ( ! class_exists( 'WC_Slack_Settings' ) ) { | |
if ( $api_key ) { | |
$all_channels = get_transient( 'wcslack_all_channels' ) ?: array(); | |
- $all_groups = get_transient( 'wcslack_all_groups' ) ?: array(); | |
if ( empty( $all_channels ) ) { | |
$all_channels = (array) $wcslack_api->all_channels( $api_key ); | |
set_transient( 'wcslack_all_channels', $all_channels, 86400 ); | |
} | |
- if ( empty( $all_groups ) ) { | |
- $all_groups = (array) $wcslack_api->all_groups( $api_key ); | |
- set_transient( 'wcslack_all_groups', $all_groups, 86400 ); | |
- } | |
- | |
- return array_merge( $all_channels, $all_groups ); | |
+ return $all_channels; | |
} else { | |
diff --git a/includes/class-wcslack-settings.php b/includes/class-wcslack-settings.php | |
index 4e52e05..6f7d7cc 100644 | |
--- a/includes/class-wcslack-settings.php | |
+++ b/includes/class-wcslack-settings.php | |
@@ -112,10 +112,10 @@ class WC_Slack_Settings extends WC_Integration { | |
// Actions. | |
add_action( 'woocommerce_update_options_integration_' . $this->id, array( $this, 'process_admin_options' ) ); | |
- add_action( 'init', array( $this, 'test_init' ), 10 ); | |
- add_action( 'init', array( $this, 'scripts' ) ); | |
+ add_action( 'admin_init', array( $this, 'test_init' ), 10 ); | |
+ add_action( 'admin_init', array( $this, 'scripts' ) ); | |
- add_action( 'init', array( $this, 'available_channels' ) ); | |
+ add_action( 'woocommerce_slack_update_channels', array( $this, 'cron_update_channels' ) ); | |
// Filters. | |
add_filter( 'woocommerce_settings_api_sanitized_fields_' . $this->id, array( $this, 'sanitize_settings' ) ); | |
@@ -124,6 +124,10 @@ class WC_Slack_Settings extends WC_Integration { | |
$this->process_slack_redirect(); | |
} | |
+ if( false === wp_get_scheduled_event('woocommerce_slack_update_channels') ) { | |
+ wp_schedule_event(time(), 'twicedaily', 'woocommerce_slack_update_channels'); | |
+ } | |
+ | |
} | |
/** | |
@@ -914,7 +918,6 @@ class WC_Slack_Settings extends WC_Integration { | |
if ( isset( $_GET['wcslack_reload_channels'] ) && ( $_GET['wcslack_reload_channels'] == 1 ) ) { | |
delete_transient( 'wcslack_all_channels' ); | |
- delete_transient( 'wcslack_all_groups' ); | |
wp_safe_redirect( admin_url() . 'admin.php?page=wc-settings&tab=integration§ion=wcslack' ); | |
@@ -979,35 +982,34 @@ class WC_Slack_Settings extends WC_Integration { | |
*/ | |
public function available_channels() { | |
- $wcslack_api = new WC_Slack_API(); | |
- $wrapper = $this->wrapper(); | |
- $api_key = $wrapper['api_key']; | |
- $connection = $this->test_client_connection(); | |
+ $all_channels = get_transient( 'wcslack_all_channels' ) ?: array(); | |
- if ( $api_key && $connection ) { | |
- $all_channels = get_transient( 'wcslack_all_channels' ) ?: array(); | |
- $all_groups = get_transient( 'wcslack_all_groups' ) ?: array(); | |
+ if( empty( $all_channels ) ) { | |
+ wp_clear_scheduled_hook('woocommerce_slack_update_channels' ); | |
+ wp_schedule_single_event( time(), 'woocommerce_slack_update_channels' ); | |
+ } | |
- if ( empty( $all_channels ) ) { | |
- $all_channels = (array) $wcslack_api->all_channels( $api_key ); | |
- set_transient( 'wcslack_all_channels', $all_channels, 86400 ); | |
- } | |
+ return $all_channels; | |
- if ( empty( $all_groups ) ) { | |
- $all_groups = (array) $wcslack_api->all_groups( $api_key ); | |
- set_transient( 'wcslack_all_groups', $all_groups, 86400 ); | |
- } | |
+ } | |
- return array_merge( (array) $all_channels, (array) $all_groups ); | |
+ /** | |
+ * Slack was return 429 (rate limiting) while the channel transient was empty, causing this to run | |
+ * multiple times on every pageload. | |
+ */ | |
+ public function cron_update_channels() { | |
- } else { | |
+ $wcslack_api = new WC_Slack_API(); | |
- return array( 'Reload Page to See Channels' ); | |
+ $wrapper = $this->wrapper(); | |
+ $api_key = $wrapper['api_key']; | |
+ $connection = $this->test_client_connection(); | |
- } | |
+ $all_channels = (array) $wcslack_api->all_channels( $api_key ); | |
+ set_transient( 'wcslack_all_channels', $all_channels, DAY_IN_SECONDS ); | |
- } | |
+ } | |
/** | |
* Santize our settings | |
diff --git a/includes/class-wcslack-slack.php b/includes/class-wcslack-slack.php | |
index d7cc62f..0486aaa 100644 | |
--- a/includes/class-wcslack-slack.php | |
+++ b/includes/class-wcslack-slack.php | |
@@ -52,8 +52,14 @@ if ( ! class_exists( 'WC_Slack_API' ) ) { | |
public function all_channels( $api_key ) { | |
+ $channels_array = array( 'select' => 'Select Channel...' ); | |
+ | |
$url = 'https://slack.com/api/conversations.list?types=public_channel,private_channel'; | |
+ if( ! empty( get_transient( 'wcslack_429' ) ) ) { | |
+ return $channels_array; | |
+ } | |
+ | |
$resp = wp_remote_get( $url, array( | |
'user-agent' => get_bloginfo( 'name' ) . ' / 1.0', | |
'headers'=> array( | |
@@ -62,7 +68,13 @@ if ( ! class_exists( 'WC_Slack_API' ) ) { | |
), | |
) ); | |
- $channels_array = array( 'select' => 'Select Channel...' ); | |
+ | |
+ if( 429 == wp_remote_retrieve_response_code( $resp ) ) { | |
+ | |
+ set_transient( 'wcslack_429', $resp, MINUTE_IN_SECONDS ); | |
+ | |
+ return $channels_array; | |
+ } | |
$body = wp_remote_retrieve_body( $resp ); | |
$decoded = json_decode( $body, true ); | |
@@ -83,52 +95,6 @@ if ( ! class_exists( 'WC_Slack_API' ) ) { | |
} | |
- /** | |
- * List all groups | |
- * | |
- * @package WooCommerce Slack | |
- * @author Bryce <bryce@bryce.se> | |
- * @since 1.1.0 | |
- */ | |
- | |
- public function all_groups( $api_key ) { | |
- | |
- $url = 'https://slack.com/api/groups.list'; | |
- | |
- $resp = wp_remote_get( $url, array( | |
- 'user-agent' => get_bloginfo( 'name' ) . ' / 1.0', | |
- 'headers'=> array( | |
- 'Content-Type' => 'application/json', | |
- 'Authorization' => 'Bearer ' . $api_key, | |
- ), | |
- ) ); | |
- | |
- $body = wp_remote_retrieve_body( $resp ); | |
- $decoded = json_decode( $body, true ); | |
- | |
- $groups_array = array(); | |
- | |
- if ( empty( $decoded['groups'] ) ) { | |
- return $groups_array; | |
- } | |
- | |
- $groups = $decoded['groups']; | |
- | |
- if ( ! empty( $groups ) ) { | |
- | |
- foreach ( $groups as $group ) { | |
- | |
- $groups_array[$group['id']] = $group['name']; | |
- | |
- } | |
- | |
- } | |
- | |
- return $groups_array; | |
- | |
- } | |
- | |
- | |
/** | |
* Send Message / Notification | |
* | |
diff --git a/languages/woocommerce-slack.pot b/languages/woocommerce-slack.pot | |
index bc703a6..ae48327 100644 | |
--- a/languages/woocommerce-slack.pot | |
+++ b/languages/woocommerce-slack.pot | |
@@ -654,7 +654,7 @@ msgstr "" | |
#. Description of the plugin/theme | |
msgid "" | |
-"Easily send notifications to your different Slack channels and groups " | |
+"Easily send notifications to your different Slack channels " | |
"whenever a WooCommerce event happens!" | |
msgstr "" | |
diff --git a/woocommerce-slack.php b/woocommerce-slack.php | |
index 8742833..af92841 100644 | |
--- a/woocommerce-slack.php | |
+++ b/woocommerce-slack.php | |
@@ -2,7 +2,7 @@ | |
/** | |
* Plugin Name: WooCommerce Slack | |
* Plugin URI: https://woocommerce.com/products/woocommerce-slack/ | |
- * Description: Easily send notifications to your different Slack channels and groups whenever a WooCommerce event happens! | |
+ * Description: Easily send notifications to your different Slack channels whenever a WooCommerce event happens! | |
* Version: 1.2.8 | |
* Author: WooCommerce | |
* Author URI: https://woocommerce.com/ |
Thanks Brian - This is a brilliant fix! @woocommerce you should really merge this into your plugin as it's killing sites' performances
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The real problem:
The Slack API's groups.list is deprecated "It will stop functioning in February 2021" and seems to be returning an empty array, which means nothing is stored in the transient, so the plugin thinks the cache has expired and retries countless times, culminating in the 429 responses.