Skip to content

Commit fe5c912

Browse files
committed
Change how plugin perms are accessed
1 parent 3fbd11b commit fe5c912

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

projects/packages/forms/src/blocks/contact-form/class-contact-form-block.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,8 @@ public static function load_editor_scripts() {
795795
'assetsUrl' => Jetpack_Forms::assets_url(),
796796
'preferredView' => $preferred_view,
797797
'isMailPoetEnabled' => Jetpack_Forms::is_mailpoet_enabled(),
798+
'canInstallPlugins' => current_user_can( 'install_plugins' ),
799+
'canActivatePlugins' => current_user_can( 'activate_plugins' ),
798800
),
799801
);
800802

projects/packages/forms/src/blocks/contact-form/components/jetpack-integrations-modal/integration-card/plugin-action-button.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
* External dependencies
33
*/
44
import { Button, Spinner, Tooltip } from '@wordpress/components';
5-
import { store as coreStore } from '@wordpress/core-data';
6-
import { useSelect } from '@wordpress/data';
75
import { __ } from '@wordpress/i18n';
86
/**
97
* Internal dependencies
108
*/
9+
import { config as dashboardConfig } from '../../../../../dashboard';
1110
import { usePluginInstallation } from '../hooks/use-plugin-installation';
11+
import type { JPFormsBlocksDefaults } from '../../../../../types';
1212

1313
type PluginActionButtonProps = {
1414
slug: string;
@@ -32,10 +32,14 @@ const PluginActionButton = ( {
3232
trackEventName
3333
);
3434

35+
// Permissions checks. We need to check both the editor-provided jpFormsBlocks
36+
// and the dashboard config since this component loads in both places.
37+
// In a future PR, we should consolidate this to a single source of truth.
38+
const jpDefaults: JPFormsBlocksDefaults | undefined = window.jpFormsBlocks?.defaults;
3539
const canUserInstallPlugins =
36-
useSelect( select => select( coreStore ).canUser( 'create', 'plugins' ), [] ) ?? false;
40+
Boolean( jpDefaults?.canInstallPlugins ) || Boolean( dashboardConfig( 'canInstallPlugins' ) );
3741
const canUserActivatePlugins =
38-
useSelect( select => select( coreStore ).canUser( 'update', 'plugins' ), [] ) ?? false;
42+
Boolean( jpDefaults?.canActivatePlugins ) || Boolean( dashboardConfig( 'canActivatePlugins' ) );
3943

4044
const canPerformAction = isInstalled ? canUserActivatePlugins : canUserInstallPlugins;
4145
const isDisabled = isInstalling || ! canPerformAction;

projects/packages/forms/src/dashboard/class-dashboard.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ public function render_dashboard( $extra_config = array() ) {
230230
'renderMigrationPage' => $this->switch->is_jetpack_forms_announcing_new_menu(),
231231
'dashboardURL' => add_query_arg( 'jetpack_forms_migration_announcement_seen', 'yes', $this->switch->get_forms_admin_url() ),
232232
'isMailpoetEnabled' => Jetpack_Forms::is_mailpoet_enabled(),
233+
'canInstallPlugins' => current_user_can( 'install_plugins' ),
234+
'canActivatePlugins' => current_user_can( 'activate_plugins' ),
233235
);
234236

235237
if ( ! empty( $extra_config ) ) {

projects/packages/forms/src/types/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ export interface JPFormsBlocksDefaults {
104104
formsResponsesSpamUrl?: string;
105105
/** Whether MailPoet integration is enabled. */
106106
isMailPoetEnabled?: boolean;
107+
/** Whether the current user can install plugins. */
108+
canInstallPlugins?: boolean;
109+
/** Whether the current user can activate plugins. */
110+
canActivatePlugins?: boolean;
107111
}
108112

109113
/**

0 commit comments

Comments
 (0)