Skip to content

Commit 425cf0f

Browse files
committed
Add extension checks and admin notice
1 parent 1d2e403 commit 425cf0f

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

plugins/optimization-detective/helper.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ function od_render_generator_meta_tag(): void {
204204
* provide optimization functionality using the Optimization Detective plugin.
205205
*
206206
* @since n.e.x.t
207+
* @access private
207208
*
208209
* @param string[]|mixed $links List of plugin action links HTML.
209210
* @return string[]|mixed Modified list of plugin action links HTML.
@@ -225,6 +226,96 @@ function od_render_extensions_action_link( $links ) {
225226
);
226227
}
227228

229+
/**
230+
* Checks for installed extensions of Optimization Detective.
231+
*
232+
* @since n.e.x.t
233+
* @access private
234+
*
235+
* @return string[] List of installed extension plugin slugs.
236+
*/
237+
function od_check_installed_extensions(): array {
238+
$installed_plugins = get_plugins();
239+
$extensions = array(
240+
'image-prioritizer/load.php',
241+
'embed-optimizer/load.php',
242+
);
243+
$installed_extensions = array_intersect( $extensions, array_keys( $installed_plugins ) );
244+
245+
return $installed_extensions;
246+
}
247+
248+
/**
249+
* Renders an admin notice prompting the user to install extensions for Optimization Detective.
250+
*
251+
* @since n.e.x.t
252+
* @access private
253+
*/
254+
function od_maybe_render_installed_extensions_admin_notice(): void {
255+
$message = sprintf(
256+
'<p><strong>%s</strong></p>',
257+
esc_html__( 'Optimization Detective does not provide any functionality on its own.', 'optimization-detective' )
258+
);
259+
260+
$message .= '<p>' . esc_html__( 'This plugin is a framework that requires extension plugins to provide optimization features. To benefit from Optimization Detective, please install one or more of the following extensions:', 'optimization-detective' ) . '</p>';
261+
262+
$extensions = array(
263+
'image-prioritizer' => array(
264+
'name' => __( 'Image Prioritizer', 'optimization-detective' ),
265+
'description' => __( 'Prioritizes the loading of images and videos based on how visible they are to actual visitors; adds fetchpriority and applies lazy-loading.', 'optimization-detective' ),
266+
'url' => admin_url( 'plugin-install.php?tab=plugin-information&plugin=image-prioritizer&TB_iframe=true&width=772' ),
267+
),
268+
'embed-optimizer' => array(
269+
'name' => __( 'Embed Optimizer', 'optimization-detective' ),
270+
'description' => __( 'Optimizes the performance of embeds through lazy-loading, preconnecting, and reserving space to reduce layout shifts.', 'optimization-detective' ),
271+
'url' => admin_url( 'plugin-install.php?tab=plugin-information&plugin=embed-optimizer&TB_iframe=true&width=772' ),
272+
),
273+
);
274+
275+
$message .= '<table class="widefat" style="margin-bottom: 11px;"><tbody>';
276+
foreach ( $extensions as $extension ) {
277+
$message .= sprintf(
278+
'<tr>
279+
<td><strong><a href="%s" class="thickbox open-plugin-details-modal">%s</a></strong></td>
280+
<td>%s</td>
281+
</tr>',
282+
esc_url( $extension['url'] ),
283+
esc_html( $extension['name'] ),
284+
esc_html( $extension['description'] )
285+
);
286+
}
287+
$message .= '</tbody></table>';
288+
289+
$notice = wp_get_admin_notice(
290+
$message,
291+
array(
292+
'type' => 'warning',
293+
'paragraph_wrap' => false,
294+
)
295+
);
296+
297+
add_thickbox();
298+
echo wp_kses( $notice, wp_kses_allowed_html( 'post' ) );
299+
}
300+
/**
301+
* Checks for installed extensions and displays an admin notice if none are found.
302+
*
303+
* @since n.e.x.t
304+
* @access private
305+
*/
306+
function od_maybe_check_installed_extensions(): void {
307+
$installed_extensions = get_transient( 'od_installed_extensions' );
308+
if ( ! is_array( $installed_extensions ) ) {
309+
$installed_extensions = od_check_installed_extensions();
310+
set_transient( 'od_installed_extensions', $installed_extensions, WEEK_IN_SECONDS );
311+
}
312+
if ( count( $installed_extensions ) > 0 ) {
313+
return;
314+
}
315+
316+
add_action( 'admin_notices', 'od_maybe_render_installed_extensions_admin_notice' );
317+
}
318+
228319
/**
229320
* Gets the path to a script or stylesheet.
230321
*

plugins/optimization-detective/hooks.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
add_action( 'wp_head', 'od_render_generator_meta_tag' );
2424
add_filter( 'site_status_tests', 'od_add_rest_api_availability_test' );
2525
add_action( 'admin_init', 'od_maybe_run_rest_api_health_check' );
26+
add_action( 'admin_init', 'od_maybe_check_installed_extensions' );
2627
add_action( 'after_plugin_row_meta', 'od_render_rest_api_health_check_admin_notice_in_plugin_row', 30 );
2728
add_filter( 'plugin_action_links_optimization-detective/load.php', 'od_render_extensions_action_link' );
2829
add_action( 'rest_api_init', 'od_register_rest_url_metric_store_endpoint' );

0 commit comments

Comments
 (0)