-
Notifications
You must be signed in to change notification settings - Fork 286
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
Create infrastructure for detecting & installing WP Consent API. #8305
Merged
aaemnnosttv
merged 6 commits into
develop
from
enhancement/8276-como-wp-consent-api-infra
Feb 22, 2024
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
88aa99d
Add GET consent-api-info endpoint and datastore counterpoint.
techanvil db3fb69
Add tests for `getConsentAPIInfo()` selector.
techanvil d3593e0
Add tests for GET consent-api-info endpoint.
techanvil 0f55394
Remove use of deprecated `assertArraySubset()`.
techanvil 73420b2
Cast values to string for PHP 7.4.
techanvil 8277b0f
Fix tests for multisite.
techanvil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,6 +132,52 @@ protected function get_rest_routes() { | |
), | ||
) | ||
), | ||
new REST_Route( | ||
'core/site/data/consent-api-info', | ||
array( | ||
array( | ||
'methods' => WP_REST_Server::READABLE, | ||
'callback' => function () { | ||
$is_active = function_exists( 'wp_set_consent' ); | ||
$installed = $is_active; | ||
$slug = 'wp-consent-api'; | ||
$plugin = "$slug/$slug.php"; | ||
|
||
$response = array( | ||
'hasConsentAPI' => $is_active, | ||
); | ||
|
||
if ( ! $is_active ) { | ||
if ( ! function_exists( 'get_plugins' ) ) { | ||
require_once ABSPATH . 'wp-admin/includes/plugin.php'; | ||
} | ||
foreach ( array_keys( get_plugins() ) as $installed_plugin ) { | ||
if ( $installed_plugin === $plugin ) { | ||
$installed = true; | ||
break; | ||
} | ||
} | ||
|
||
// Alternate wp_nonce_url without esc_html breaking query parameters. | ||
$nonce_url = function ( $action_url, $action ) { | ||
return add_query_arg( '_wpnonce', wp_create_nonce( $action ), $action_url ); | ||
}; | ||
$activate_url = $nonce_url( self_admin_url( 'plugins.php?action=activate&plugin=' . $plugin ), 'activate-plugin_' . $plugin ); | ||
$install_url = $nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=' . $slug ), 'install-plugin_' . $slug ); | ||
|
||
$response['wpConsentPlugin'] = array( | ||
'installed' => $installed, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that there's no need to include the |
||
'activateURL' => current_user_can( 'activate_plugin', $plugin ) ? esc_url_raw( $activate_url ) : false, | ||
'installURL' => current_user_can( 'install_plugins' ) ? esc_url_raw( $install_url ) : false, | ||
); | ||
} | ||
|
||
return new WP_REST_Response( $response ); | ||
}, | ||
'permission_callback' => $can_manage_options, | ||
), | ||
) | ||
), | ||
); | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 could actually be different if it were installed via Composer. See https://packagist.org/packages/rlankhorst/wp-consent-level-api
Definitely an edge case for most of our users I think, but worth keeping in mind that the
$plugin
can't always be assumed to be$slug/$slug
. Not just because of Composer, but the directory can be renamed, or sometimes even the plugin itself can change the name of its entry point php 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.
Thanks @aaemnnosttv, that's a good point. I've created #8307 to address this later. Seems like a post-MVP issue to me.