@@ -25,19 +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- // Only add this action if we're on the useful plugins page or post.php
31- global $ pagenow ;
32-
33- $ page = isset ( $ _GET ['page ' ] ) ? sanitize_text_field ( $ _GET ['page ' ] ) : '' ;
34- $ is_useful_plugins_page = ( $ page === 'stackable-useful-plugins ' );
35- $ is_post_editor = ( $ pagenow === 'post.php ' );
36-
37- if ( $ is_useful_plugins_page || $ is_post_editor ) {
38- add_action ( 'admin_menu ' , array ( $ this , 'get_useful_plugins_info ' ) );
39- }
40-
4128 // use WordPress ajax installer
4229 // see Docs: https://developer.wordpress.org/reference/functions/wp_ajax_install_plugin/
4330 add_action ('wp_ajax_stackable_useful_plugins_activate ' , array ( $ this , 'do_plugin_activate ' ) );
@@ -47,6 +34,13 @@ function __construct() {
4734 add_action ('wp_ajax_stackable_check_cimo_status ' , array ( $ this , 'check_cimo_status ' ) );
4835
4936 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 );
5044 add_filter ( 'stackable_localize_script ' , array ( $ this , 'localize_hide_cimo_notice ' ) );
5145 }
5246 }
@@ -77,7 +71,11 @@ public static function is_plugin_activated( $plugin_slug ) {
7771 }
7872
7973
80- 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+
8179 $ current_user_cap = current_user_can ( 'install_plugins ' ) ? 2 : (
8280 current_user_can ( 'activate_plugins ' ) ? 1 : 0
8381 );
@@ -94,7 +92,7 @@ public function get_useful_plugins_info() {
9492 $ data_to_localize = array ();
9593
9694 $ has_premium = false ;
97- foreach ( self :: $ PLUGINS as $ key => $ plugin ) {
95+ foreach ( $ plugin_config as $ key => $ plugin ) {
9896 $ status = 'not_installed ' ;
9997 $ full_slug_to_use = $ plugin ['full_slug ' ];
10098
@@ -132,13 +130,12 @@ public function get_useful_plugins_info() {
132130 );
133131 }
134132
135- // Make Cimo available in the block editor
136- $ this ->add_cimo_args_to_localize_editor ( $ data_to_localize , $ current_user_cap , $ has_premium );
137- // Make all plugin data and the ajax url available in the admin settings
138- $ 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 ;
139136 }
140137
141- 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 ) {
142139 $ slug = 'cimo-image-optimizer ' ;
143140 if ( ! isset ( $ data_to_localize [ $ slug ] ) ) {
144141 return ;
@@ -173,23 +170,18 @@ public function add_cimo_args_to_localize_editor( $data_to_localize, $current_us
173170
174171 $ cimo_data [ 'action ' ] = html_entity_decode ( $ action_link );
175172
176- add_filter ( 'stackable_localize_script ' , function ( $ args ) use ( $ cimo_data ) {
177- return $ this ->add_localize_script ( $ args , 'cimo ' , $ cimo_data );
178- }, 1 );
179-
173+ return $ this ->add_localize_script ( $ args , 'cimo ' , $ cimo_data );
180174 }
181175
182- public function add_args_to_localize_admin ( $ data_to_localize ) {
176+ public function add_args_to_localize_admin ( $ args , $ data_to_localize ) {
183177 $ argsToAdd = array (
184178 'usefulPlugins ' => $ data_to_localize ,
185179 'installerNonce ' => wp_create_nonce ( "updates " ),
186180 'activateNonce ' => wp_create_nonce ( "stk_activate_useful_plugin " ),
187181 'ajaxUrl ' => admin_url ('admin-ajax.php ' )
188182 );
189183
190- add_filter ( 'stackable_localize_settings_script ' , function ( $ args ) use ( $ argsToAdd ) {
191- return $ this ->add_localize_script ( $ args , '' , $ argsToAdd );
192- } );
184+ return $ this ->add_localize_script ( $ args , '' , $ argsToAdd );
193185 }
194186
195187 public function add_localize_script ( $ args , $ arg_key , $ data ) {
0 commit comments