77
88namespace WCPay \WooPay ;
99
10+ use WC_Payments_API_Client ;
1011use WCPay \Logger ;
1112
1213/**
1516 */
1617class WooPay_Scheduler {
1718
19+ const INVALID_EXTENSIONS_FOUND_OPTION_NAME = 'woopay_invalid_extension_found ' ;
20+ const INCOMPATIBLE_EXTENSIONS_LIST_OPTION_NAME = 'woopay_incompatible_extensions ' ;
21+
22+ /**
23+ * WC_Payments_API_Client instance.
24+ *
25+ * @var WC_Payments_API_Client
26+ */
27+ private $ payments_api_client ;
28+
29+ /**
30+ * Constructor.
31+ *
32+ * @param WC_Payments_API_Client $payments_api_client The Payments API Client.
33+ * @return void
34+ */
35+ public function __construct ( WC_Payments_API_Client $ payments_api_client ) {
36+ $ this ->payments_api_client = $ payments_api_client ;
37+ }
38+
1839 /**
1940 * Init the hooks.
2041 */
2142 public function init () {
2243 add_action ( 'init ' , [ $ this , 'schedule ' ] );
23- add_action ( 'validate_incompatible_extensions ' , [ $ this , 'disable_woopay_if_incompatible_extension_active ' ] );
44+ add_action ( 'validate_incompatible_extensions ' , [ $ this , 'update_incompatible_extensions_list_and_maybe_show_warning ' ] );
45+ add_action ( 'activated_plugin ' , [ $ this , 'show_warning_when_incompatible_extension_is_enabled ' ] );
46+ add_action ( 'deactivated_plugin ' , [ $ this , 'hide_warning_when_incompatible_extension_is_disabled ' ] );
2447
2548 register_deactivation_hook ( WCPAY_PLUGIN_FILE , [ $ this , 'remove_scheduler ' ] );
2649 }
@@ -42,29 +65,78 @@ public function schedule() {
4265 }
4366
4467 /**
45- * Disables WooPay if an incompatible extension is active
68+ * Updates the list of incompatible extensions.
4669 */
47- public function disable_woopay_if_incompatible_extension_active () {
70+ public function update_incompatible_extensions_list_and_maybe_show_warning () {
4871 try {
4972 $ incompatible_extensions = $ this ->get_incompatible_extensions ();
73+ $ active_plugins = get_option ( 'active_plugins ' , [] );
5074
51- $ active_plugins = get_option ( ' active_plugins ' );
52- delete_option ( ' woopay_disabled_invalid_extensions ' );
75+ update_option ( self :: INCOMPATIBLE_EXTENSIONS_LIST_OPTION_NAME , $ incompatible_extensions );
76+ delete_option ( self :: INVALID_EXTENSIONS_FOUND_OPTION_NAME );
5377
5478 if ( ! empty ( $ active_plugins ) && is_array ( $ active_plugins ) ) {
55- foreach ( $ active_plugins as $ plugin ) {
56- $ plugin = $ this ->format_extension_name ( $ plugin );
57-
58- if ( in_array ( $ plugin , $ incompatible_extensions , true ) ) {
59- update_option ( 'woopay_disabled_invalid_extensions ' , true );
60- }
79+ if ( $ this ->contains_incompatible_extension ( $ active_plugins , $ incompatible_extensions ) ) {
80+ update_option ( self ::INVALID_EXTENSIONS_FOUND_OPTION_NAME , true );
6181 }
6282 }
6383 } catch ( \Exception $ e ) {
6484 Logger::error ( 'Failed to decode WooPay incompatible extensions list. ' . $ e );
6585 }
6686 }
6787
88+ /**
89+ * Adds a warning to the WC Payments settings page.
90+ *
91+ * @param string $plugin The plugin being enabled.
92+ */
93+ public function show_warning_when_incompatible_extension_is_enabled ( $ plugin ) {
94+ $ incompatible_extensions = get_option ( self ::INCOMPATIBLE_EXTENSIONS_LIST_OPTION_NAME , [] );
95+ $ plugin = $ this ->format_extension_name ( $ plugin );
96+
97+ if ( $ this ->contains_incompatible_extension ( [ $ plugin ], $ incompatible_extensions ) ) {
98+ update_option ( self ::INVALID_EXTENSIONS_FOUND_OPTION_NAME , true );
99+ }
100+ }
101+
102+ /**
103+ * Removes the warning when the last incompatible extension is removed.
104+ *
105+ * @param string $plugin_being_deactivated The plugin name.
106+ */
107+ public function hide_warning_when_incompatible_extension_is_disabled ( $ plugin_being_deactivated ) {
108+ $ incompatible_extensions = get_option ( self ::INCOMPATIBLE_EXTENSIONS_LIST_OPTION_NAME , [] );
109+ $ active_plugins = get_option ( 'active_plugins ' , [] );
110+
111+ // Needs to remove the plugin being deactivated because WordPress only updates the list after this hook runs.
112+ $ active_plugins = array_diff ( $ active_plugins , [ $ plugin_being_deactivated ] );
113+
114+ // Only deactivates the warning if there are no other incompatible extensions.
115+ if ( ! $ this ->contains_incompatible_extension ( $ active_plugins , $ incompatible_extensions ) ) {
116+ delete_option ( self ::INVALID_EXTENSIONS_FOUND_OPTION_NAME );
117+ }
118+ }
119+
120+ /**
121+ * Checks if there is any incompatible extension in the list.
122+ *
123+ * @param mixed $active_plugins list of active plugins.
124+ * @param mixed $incompatible_extensions list of incompatible extensions.
125+ *
126+ * @return bool
127+ */
128+ public function contains_incompatible_extension ( $ active_plugins , $ incompatible_extensions ) {
129+ foreach ( $ active_plugins as $ plugin ) {
130+ $ plugin = $ this ->format_extension_name ( $ plugin );
131+
132+ if ( in_array ( $ plugin , $ incompatible_extensions , true ) ) {
133+ return true ;
134+ }
135+ }
136+
137+ return false ;
138+ }
139+
68140 /**
69141 * Removes the folder and file extension from the plugin name.
70142 *
@@ -83,34 +155,8 @@ private function format_extension_name( $plugin ) {
83155 * @return array
84156 */
85157 public function get_incompatible_extensions () {
86- $ args = [
87- 'url ' => WooPay_Utilities::get_woopay_rest_url ( 'extensions/incompatible ' ),
88- 'method ' => 'GET ' ,
89- 'timeout ' => 30 ,
90- 'headers ' => [
91- 'Content-Type ' => 'application/json ' ,
92- ],
93- ];
94-
95- /**
96- * Suppress psalm error from Jetpack Connection namespacing WP_Error.
97- *
98- * @psalm-suppress UndefinedDocblockClass
99- */
100- $ response = \Automattic \Jetpack \Connection \Client::remote_request ( $ args );
101- $ response_body = wp_remote_retrieve_body ( $ response );
102-
103- // phpcs:ignore
104- /**
105- * @psalm-suppress UndefinedDocblockClass
106- */
107- if ( is_wp_error ( $ response ) || ! is_array ( $ response ) || ( ! empty ( $ response ['code ' ] ) && ( $ response ['code ' ] >= 300 || $ response ['code ' ] < 200 ) ) ) {
108- Logger::error ( 'HTTP_REQUEST_ERROR ' . var_export ( $ response , true ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
109- return ;
110- }
111-
112- $ json = json_decode ( $ response_body , true );
158+ $ incompatible_extensions = $ this ->payments_api_client ->get_woopay_incompatible_extensions ();
113159
114- return isset ( $ json ['incompatible_extensions ' ] ) ? $ json ['incompatible_extensions ' ] : [];
160+ return isset ( $ incompatible_extensions ['incompatible_extensions ' ] ) ? $ incompatible_extensions ['incompatible_extensions ' ] : [];
115161 }
116162}
0 commit comments