Skip to content

Commit 7e58b9f

Browse files
authored
Merge pull request #680 from 10up/ehnancement/669
Remove the `AccessControl` class.
2 parents d553b45 + 86225a9 commit 7e58b9f

File tree

4 files changed

+11
-317
lines changed

4 files changed

+11
-317
lines changed

includes/Classifai/Blocks/recommended-content-block/register.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Classifai\Blocks\RecommendedContentBlock;
99

10+
use Classifai\Features\RecommendedContent;
1011
use Classifai\Providers\Azure\Personalizer;
1112
use function Classifai\get_asset_info;
1213

@@ -18,7 +19,6 @@ function register() {
1819
return __NAMESPACE__ . "\\$function_name";
1920
};
2021

21-
$personalizer = new Personalizer( false );
2222
wp_register_script(
2323
'recommended-content-block-editor-script',
2424
CLASSIFAI_PLUGIN_URL . 'dist/recommended-content-block.js',
@@ -31,7 +31,7 @@ function register() {
3131
'recommended-content-block-editor-script',
3232
sprintf(
3333
'var hasRecommendedContentAccess = %d;',
34-
$personalizer->is_feature_enabled( 'recommended_content' )
34+
( new RecommendedContent() )->is_feature_enabled()
3535
),
3636
'before'
3737
);

includes/Classifai/Providers/AccessControl.php

-230
This file was deleted.

includes/Classifai/Providers/Azure/Personalizer.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -140,22 +140,22 @@ public function get_default_provider_settings(): array {
140140
public function sanitize_settings( array $new_settings ): array {
141141
$settings = $this->feature_instance->get_settings();
142142

143-
$new_settings['endpoint_url'] = esc_url_raw( $new_settings['endpoint_url'] ?? $settings['endpoint_url'] );
144-
$new_settings['api_key'] = sanitize_text_field( $new_settings['api_key'] ?? $settings['api_key'] );
143+
$new_settings['endpoint_url'] = esc_url_raw( $new_settings[ static::ID ]['endpoint_url'] ?? $settings[ static::ID ]['endpoint_url'] );
144+
$new_settings['api_key'] = sanitize_text_field( $new_settings[ static::ID ]['api_key'] ?? $settings[ static::ID ]['api_key'] );
145145

146-
if ( ! empty( $new_settings['endpoint_url'] ) && ! empty( $new_settings['api_key'] ) ) {
147-
$auth_check = $this->authenticate_credentials( $new_settings['endpoint_url'], $new_settings['api_key'] );
146+
if ( ! empty( $new_settings[ static::ID ]['endpoint_url'] ) && ! empty( $new_settings[ static::ID ]['api_key'] ) ) {
147+
$auth_check = $this->authenticate_credentials( $new_settings[ static::ID ]['endpoint_url'], $new_settings[ static::ID ]['api_key'] );
148148

149149
if ( is_wp_error( $auth_check ) ) {
150150
$settings_errors['classifai-registration-credentials-error'] = $auth_check->get_error_message();
151-
$new_settings['authenticated'] = false;
151+
$new_settings[ static::ID ]['authenticated'] = false;
152152
} else {
153-
$new_settings['authenticated'] = true;
153+
$new_settings[ static::ID ]['authenticated'] = true;
154154
}
155155
} else {
156-
$new_settings['authenticated'] = false;
157-
$new_settings['endpoint_url'] = '';
158-
$new_settings['api_key'] = '';
156+
$new_settings[ static::ID ]['authenticated'] = false;
157+
$new_settings[ static::ID ]['endpoint_url'] = '';
158+
$new_settings[ static::ID ]['api_key'] = '';
159159

160160
$settings_errors['classifai-registration-credentials-empty'] = __( 'Please enter your credentials', 'classifai' );
161161
}

includes/Classifai/Providers/Provider.php

-76
Original file line numberDiff line numberDiff line change
@@ -214,80 +214,4 @@ public function add_api_key_field( array $args = [] ) {
214214
]
215215
);
216216
}
217-
218-
/**
219-
* Determine if the current user has access of the feature
220-
*
221-
* @param string $feature Feature to check.
222-
* @return bool
223-
*/
224-
protected function has_access( string $feature ): bool {
225-
$access_control = new AccessControl( $this, $feature );
226-
return $access_control->has_access();
227-
}
228-
229-
/**
230-
* Determine if the feature is enabled and current user can access the feature
231-
*
232-
* @param string $feature Feature to check.
233-
* @return bool
234-
*/
235-
public function is_feature_enabled( string $feature ): bool {
236-
$is_feature_enabled = false;
237-
$settings = $this->get_settings();
238-
239-
// Check if provider is configured, user has access to the feature and the feature is turned on.
240-
if (
241-
$this->is_configured() &&
242-
$this->has_access( $feature ) &&
243-
$this->is_enabled( $feature )
244-
) {
245-
$is_feature_enabled = true;
246-
}
247-
248-
/**
249-
* Filter to override permission to a specific classifai feature.
250-
*
251-
* @since 2.4.0
252-
* @hook classifai_{$this->option_name}_enable_{$feature}
253-
*
254-
* @param {bool} $is_feature_enabled Is the feature enabled?
255-
* @param {array} $settings Current feature settings.
256-
*
257-
* @return {bool} Returns true if the user has access and the feature is enabled, false otherwise.
258-
*/
259-
return apply_filters( "classifai_{$this->option_name}_enable_{$feature}", $is_feature_enabled, $settings );
260-
}
261-
262-
/**
263-
* Determine if the feature is turned on.
264-
*
265-
* Note: This function does not check if the user has access to the feature.
266-
*
267-
* - Use `is_feature_enabled()` to check if the user has access to the feature and feature is turned on.
268-
* - Use `has_access()` to check if the user has access to the feature.
269-
*
270-
* @param string $feature Feature to check.
271-
* @return bool
272-
*/
273-
public function is_enabled( string $feature ): bool {
274-
$settings = $this->get_settings();
275-
$enable_key = 'enable_' . $feature;
276-
277-
// Check if feature is turned on.
278-
$is_enabled = ( isset( $settings[ $enable_key ] ) && 1 === (int) $settings[ $enable_key ] );
279-
280-
/**
281-
* Filter to override a specific classifai feature enabled.
282-
*
283-
* @since 2.5.0
284-
* @hook classifai_is_{$feature}_enabled
285-
*
286-
* @param {bool} $is_enabled Is the feature enabled?
287-
* @param {array} $settings Current feature settings.
288-
*
289-
* @return {bool} Returns true if the feature is enabled, false otherwise.
290-
*/
291-
return apply_filters( "classifai_is_{$feature}_enabled", $is_enabled, $settings );
292-
}
293217
}

0 commit comments

Comments
 (0)