-
Notifications
You must be signed in to change notification settings - Fork 297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue / 10169 Plugin Detection Logic #10209
base: develop
Are you sure you want to change the base?
Conversation
… helper util for WooCommerce plugin detection.
Build files for b3f3ae2 are ready:
|
Note to reviewerPlease see this comment against the issue as to why the implementation is slightly different to the IB, thank you. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @10upsimon looks good overall, I left you a few comments
@@ -760,6 +761,54 @@ private function get_inline_base_data() { | |||
'isMultisite' => is_multisite(), | |||
); | |||
|
|||
if ( Feature_Flags::enabled( 'adsPax' ) ) { | |||
$inline_data['plugins'] = array( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure we need this additional nesting, it only complicates already nested structure 🤔 . It does related to plugins, although this goes into the core site namespace, and plugins are technically within the site scope
@@ -18,6 +18,7 @@ | |||
use Google\Site_Kit\Core\Util\URL; | |||
use WP_Dependencies; | |||
use WP_Post_Type; | |||
use Google\Site_Kit\Core\Util\Plugin_Status; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's group this import together with other imports from Site_Kit
namespace
// Get the status of the WooCommerce plugin. | ||
$woocommerce_plugin_status = Plugin_Status::get_plugin_status( 'woocommerce/woocommerce.php', 'https://woocommerce.com/' ); | ||
|
||
// Get the status of the Google for WooCommerce plugin. | ||
$google_for_woocommerce_plugin_status = Plugin_Status::get_plugin_status( 'google-listings-and-ads/google-listings-and-ads.php', 'https://wordpress.org/plugins/google-listings-and-ads' ); | ||
|
||
switch ( $woocommerce_plugin_status ) { | ||
case Plugin_Status::PLUGIN_STATUS_ACTIVE: | ||
$inline_data['plugins']['woocommerce']['installed'] = true; | ||
$inline_data['plugins']['woocommerce']['active'] = true; | ||
break; | ||
case Plugin_Status::PLUGIN_STATUS_INSTALLED: | ||
$inline_data['plugins']['woocommerce']['installed'] = true; | ||
$inline_data['plugins']['woocommerce']['active'] = false; | ||
break; | ||
} | ||
|
||
switch ( $google_for_woocommerce_plugin_status ) { | ||
case Plugin_Status::PLUGIN_STATUS_ACTIVE: | ||
$inline_data['plugins']['google-listings-and-ads']['installed'] = true; | ||
$inline_data['plugins']['google-listings-and-ads']['active'] = true; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we can extract woo and GfW logic of computing the inline data, and return of the inline data itself into a separate module, to keep both of the methods more compact
} else { | ||
foreach ( $plugins as $plugin_file => $installed_plugin ) { | ||
if ( $installed_plugin['PluginURI'] === $plugin_uri ) { | ||
$plugin = $plugin_file; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure it is safe to remove this re-assignment of $plugin
? We do define it at the top of the function based on the known path, which should be enough, but there might be instance it was not enough that we had to match it by URI and re-assign it, might be worthwhile confirming this @nfmohit who did this initial implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -713,7 +705,7 @@ protected function inline_module_data( $modules_data ) { | |||
$inline_data['existingClientID'] = $existing_client_id; | |||
} | |||
|
|||
$is_woocommerce_active = $this->woocommerce->is_active(); | |||
$is_woocommerce_active = Plugin_Status::PLUGIN_STATUS_ACTIVE === Plugin_Status::get_plugin_status( 'woocommerce/woocommerce.php', 'https://woocommerce.com/' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use this check in more then one place, it will be more efficient to wrap it within a separate method, like is_woocommerce_active
that will handle this, and we can also store the result within the class property to avoid re-fetching the plugins and re-doing the same loop. We could call this method during the constructor or register
method for example
protected function enable_feature( $feature ) { | ||
$enable_callback = function ( $enabled, $feature_name ) use ( $feature ) { | ||
if ( $feature_name === $feature ) { | ||
return true; | ||
} | ||
return $enabled; | ||
}; | ||
|
||
add_filter( 'googlesitekit_is_feature_enabled', $enable_callback, 10, 2 ); | ||
|
||
return function () use ( $enable_callback ) { | ||
remove_filter( 'googlesitekit_is_feature_enabled', $enable_callback, 10 ); | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is already defined in the main TestCase
class
site-kit-wp/tests/phpunit/includes/TestCase.php
Lines 89 to 102 in 91fb322
protected function enable_feature( $feature ) { | |
$enable_callback = function ( $enabled, $feature_name ) use ( $feature ) { | |
if ( $feature_name === $feature ) { | |
return true; | |
} | |
return $enabled; | |
}; | |
add_filter( 'googlesitekit_is_feature_enabled', $enable_callback, 10, 2 ); | |
return function () use ( $enable_callback ) { | |
remove_filter( 'googlesitekit_is_feature_enabled', $enable_callback, 10 ); | |
}; | |
} |
'installed' => false, | ||
'active' => false, | ||
), | ||
'google-listings-and-ads' => array( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, one more thing, I know we planned to use this slug in the IB, but let's rename it to googleForWooCommerce
, to be more clear what plugin is this about since that's how it is called now, and to adhere to general approach of not having kebab-case properties in the inline data, or data stores in general (with modules being exception since we have to adhere to the module slug everywhere)
Summary
Addresses issue:
Relevant technical choices
PR Author Checklist
Do not alter or remove anything below. The following sections will be managed by moderators only.
Code Reviewer Checklist
Merge Reviewer Checklist