Skip to content

Commit d75d91c

Browse files
committed
Allow setting a message to be displayed if the module can't be loaded
1 parent 0ffc23c commit d75d91c

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

admin/load.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,16 @@ function perflab_render_modules_page_field( $module_slug, $module_data, $module_
156156
<input type="checkbox" id="<?php echo esc_attr( "{$base_id}_enabled" ); ?>" aria-describedby="<?php echo esc_attr( "{$base_id}_description" ); ?>" disabled>
157157
<input type="hidden" name="<?php echo esc_attr( "{$base_name}[enabled]" ); ?>" value="<?php echo $enabled ? '1' : '0'; ?>">
158158
<?php
159+
$message = perflab_can_load_module( $module_slug, true );
160+
if ( $message ) {
161+
echo esc_html( $message );
162+
} else {
159163
printf(
160164
/* translators: %s: module name */
161165
__( '%s is already part of your WordPress version and therefore cannot be loaded as part of the plugin.', 'performance-lab' ),
162166
esc_html( $module_data['name'] )
163167
);
168+
}
164169
?>
165170
<?php } ?>
166171
</label>

load.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,14 @@ function perflab_render_generator() {
216216
*
217217
* @since 1.3.0
218218
*
219-
* @param string $module Slug of the module.
220-
* @return bool Whether the module can be loaded or not.
219+
* @param string $module Slug of the module.
220+
* @param bool $get_message Optional. Whether to return the message instead of a boolean. Default false.
221+
*
222+
* @return bool|string Whether the module can be loaded or not.
223+
* If the module can't be loaded, it can return either `false`,
224+
* or a string with the message to be shown to users when $get_message is `true`.
221225
*/
222-
function perflab_can_load_module( $module ) {
226+
function perflab_can_load_module( $module, $get_message = false ) {
223227
$module_load_file = PERFLAB_PLUGIN_DIR_PATH . 'modules/' . $module . '/can-load.php';
224228

225229
// If the `can-load.php` file does not exist, assume the module can be loaded.
@@ -236,7 +240,13 @@ function perflab_can_load_module( $module ) {
236240
}
237241

238242
// Call the closure to determine whether the module can be loaded.
239-
return (bool) $module();
243+
$result = $module();
244+
245+
if ( false !== $result && true !== $result ) {
246+
return $get_message ? $result : false;
247+
}
248+
249+
return (bool) $result;
240250
}
241251

242252
/**

modules/database/sqlite/can-load.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@
1212
* @since n.e.x.t
1313
*/
1414
return function() {
15-
$is_writable_wp_content_dir = wp_is_writable( WP_CONTENT_DIR );
16-
return class_exists( 'SQLite3' ) && $is_writable_wp_content_dir;
15+
if ( ! wp_is_writable( WP_CONTENT_DIR ) ) {
16+
return sprintf(
17+
/* translators: %s: WP_CONTENT_DIR */
18+
__( 'The SQLite module cannot be activated because the %s directory is not writable.', 'performance-lab' ),
19+
WP_CONTENT_DIR
20+
);
21+
}
22+
23+
if ( ! extension_loaded( 'sqlite3' ) || ! class_exists( 'SQLite3' ) ) {
24+
return __( 'The SQLite module cannot be activated because the SQLite extension is not loaded.', 'performance-lab' );
25+
}
26+
27+
return true;
1728
};

0 commit comments

Comments
 (0)