Skip to content

Commit c2f990d

Browse files
authored
fix: use own useful plugin icons, add hook only if in editor or useful plugins page (#3664)
* use own plugin icons, add hook only if in editor or useful plugins page * use add_filter directly * return args
1 parent 8c52e7c commit c2f990d

File tree

4 files changed

+30
-50
lines changed

4 files changed

+30
-50
lines changed

src/welcome/images/cimo-icon.png

4.61 KB
Loading
935 Bytes
Loading

src/welcome/useful-plugins.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import CimoIcon from './images/cimo-icon.png'
2+
import InteractionsIcon from './images/interactions-icon.png'
3+
14
import {
2-
i18n, usefulPlugins, ajaxUrl, installerNonce, activateNonce,
5+
i18n, usefulPlugins, ajaxUrl, installerNonce, activateNonce, srcUrl,
36
} from 'stackable'
47

58
import { Button, Spinner } from '@wordpress/components'
@@ -10,10 +13,12 @@ import { __ } from '@wordpress/i18n'
1013
// List of plugins to display in the Useful Plugins section
1114
const PLUGINS = [ {
1215
id: 'interactions',
16+
icon: InteractionsIcon,
1317
title: __( 'Interactions', i18n ),
1418
description: __( 'Easily add animations and interactive experiences to your web pages using the block editor. Craft interactions from subtle hover effects to attention-grabbing story-telling animations.', i18n ),
1519
}, {
1620
id: 'cimo-image-optimizer',
21+
icon: CimoIcon,
1722
title: __( 'Cimo - Image Optimizer', i18n ),
1823
description: __( 'A game-changer for image optimization. Cimo optimizes and converts your images instantly as you upload — even before the files are added to your Media Library.', i18n ),
1924
} ]
@@ -101,20 +106,10 @@ const PluginCard = ( { plugin } ) => {
101106
} )
102107
}
103108

104-
// Validate URL before using
105-
const isValidUrl = url => {
106-
try {
107-
const parsed = new URL( url )
108-
return [ 'http:', 'https:' ].includes( parsed.protocol )
109-
} catch {
110-
return false
111-
}
112-
}
113-
114109
return <div key={ plugin.id } className="s-card">
115110
<div className="s-plugin-title">
116-
{ pluginData.icon && isValidUrl( pluginData.icon ) && (
117-
<img className="s-plugin-icon" src={ pluginData.icon } alt={ __( 'Plugin icon', i18n ) } />
111+
{ plugin.icon && (
112+
<img className="s-plugin-icon" src={ `${ srcUrl }/${ plugin.icon }` } alt={ __( 'Plugin icon', i18n ) } />
118113
) }
119114
<h3 className="s-card-title">{ plugin.title }</h3>
120115
</div>

src/welcome/useful-plugins.php

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ class Stackable_Useful_Plugins {
2525
);
2626

2727
function __construct() {
28-
// Register action on 'admin_menu' to ensure filters for the editor and admin settings
29-
// are added early, before those scripts are enqueued and filters are applied.
30-
add_action( 'admin_menu', array( $this, 'get_useful_plugins_info' ) );
31-
3228
// use WordPress ajax installer
3329
// see Docs: https://developer.wordpress.org/reference/functions/wp_ajax_install_plugin/
3430
add_action('wp_ajax_stackable_useful_plugins_activate', array( $this, 'do_plugin_activate' ) );
@@ -38,6 +34,13 @@ function __construct() {
3834
add_action('wp_ajax_stackable_check_cimo_status', array( $this, 'check_cimo_status' ) );
3935

4036
if ( is_admin() ) {
37+
add_filter( 'stackable_localize_settings_script', function ( $args ) {
38+
return $this->get_useful_plugins_info( $args, array( $this, 'add_args_to_localize_admin' ) );
39+
} );
40+
add_filter( 'stackable_localize_script', function ( $args ) {
41+
return $this->get_useful_plugins_info( $args, array( $this, 'add_cimo_args_to_localize_editor' ),
42+
[ 'cimo-image-optimizer' => self::$PLUGINS[ 'cimo-image-optimizer' ] ] );
43+
}, 1 );
4144
add_filter( 'stackable_localize_script', array( $this, 'localize_hide_cimo_notice' ) );
4245
}
4346
}
@@ -68,18 +71,19 @@ public static function is_plugin_activated( $plugin_slug ) {
6871
}
6972

7073

71-
public function get_useful_plugins_info() {
74+
public function get_useful_plugins_info( $args, $callback, $plugin_config = null ) {
75+
if ( $plugin_config === null ) {
76+
$plugin_config = self::$PLUGINS;
77+
}
78+
7279
$current_user_cap = current_user_can( 'install_plugins' ) ? 2 : (
7380
current_user_can( 'activate_plugins') ? 1 : 0
7481
);
7582

7683
if ( ! $current_user_cap ) {
77-
return;
84+
return $args;
7885
}
7986

80-
if ( ! function_exists( 'plugins_api' ) ) {
81-
include_once( ABSPATH . 'wp-admin/includes/plugin-install.php' );
82-
}
8387
if ( ! function_exists( 'get_plugins' ) || ! function_exists( 'is_plugin_active' ) ) {
8488
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
8589
}
@@ -88,7 +92,7 @@ public function get_useful_plugins_info() {
8892
$data_to_localize = array();
8993

9094
$has_premium = false;
91-
foreach ( self::$PLUGINS as $key => $plugin ) {
95+
foreach ( $plugin_config as $key => $plugin ) {
9296
$status = 'not_installed';
9397
$full_slug_to_use = $plugin['full_slug'];
9498

@@ -120,35 +124,21 @@ public function get_useful_plugins_info() {
120124
}
121125
}
122126

123-
$plugin_info = plugins_api( 'plugin_information', [
124-
'slug' => $plugin['slug'],
125-
'fields' => [ 'icons' => true, 'sections' => false ],
126-
] );
127-
128-
$icon_url = '';
129-
if ( ! is_wp_error( $plugin_info ) && isset( $plugin_info->icons )
130-
&& is_array( $plugin_info->icons ) && ! empty( $plugin_info->icons )
131-
) {
132-
$icon_url = array_values( $plugin_info->icons )[0];
133-
}
134-
135127
$data_to_localize[ $key ] = array(
136128
'status' => $status,
137-
'icon' => $icon_url,
138129
'fullSlug' => $full_slug_to_use,
139130
);
140131
}
141132

142-
// Make Cimo available in the block editor
143-
$this->add_cimo_args_to_localize_editor( $data_to_localize, $current_user_cap, $has_premium );
144-
// Make all plugin data and the ajax url available in the admin settings
145-
$this->add_args_to_localize_admin( $data_to_localize );
133+
$args = call_user_func( $callback, $args, $data_to_localize, $current_user_cap, $has_premium );
134+
135+
return $args;
146136
}
147137

148-
public function add_cimo_args_to_localize_editor( $data_to_localize, $current_user_cap, $has_premium ) {
138+
public function add_cimo_args_to_localize_editor( $args, $data_to_localize, $current_user_cap, $has_premium ) {
149139
$slug = 'cimo-image-optimizer';
150140
if ( ! isset( $data_to_localize[ $slug ] ) ) {
151-
return;
141+
return $args;
152142
}
153143
$full_slug = $data_to_localize[ $slug ][ 'fullSlug' ];
154144

@@ -180,23 +170,18 @@ public function add_cimo_args_to_localize_editor( $data_to_localize, $current_us
180170

181171
$cimo_data[ 'action' ] = html_entity_decode( $action_link );
182172

183-
add_filter( 'stackable_localize_script', function ( $args ) use( $cimo_data ) {
184-
return $this->add_localize_script( $args, 'cimo', $cimo_data );
185-
}, 1 );
186-
173+
return $this->add_localize_script( $args, 'cimo', $cimo_data );
187174
}
188175

189-
public function add_args_to_localize_admin( $data_to_localize ) {
176+
public function add_args_to_localize_admin( $args, $data_to_localize ) {
190177
$argsToAdd = array(
191178
'usefulPlugins' => $data_to_localize,
192179
'installerNonce' => wp_create_nonce( "updates" ),
193180
'activateNonce' => wp_create_nonce( "stk_activate_useful_plugin" ),
194181
'ajaxUrl' => admin_url('admin-ajax.php')
195182
);
196183

197-
add_filter( 'stackable_localize_settings_script', function ( $args ) use( $argsToAdd ) {
198-
return $this->add_localize_script( $args, '', $argsToAdd );
199-
} );
184+
return $this->add_localize_script( $args, '', $argsToAdd );
200185
}
201186

202187
public function add_localize_script( $args, $arg_key, $data ) {

0 commit comments

Comments
 (0)