@@ -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