@@ -63,10 +63,39 @@ class TGM_Plugin_Activation {
6363 *
6464 * @since 1.0.0
6565 *
66+ * @since 2.5.0 the array has the plugin slug as an associative key
67+ *
6668 * @var array
6769 */
6870 public $ plugins = array ();
6971
72+ /**
73+ * Holds arrays of plugin names to use to sort the plugins array.
74+ *
75+ * @since 2.5.0
76+ *
77+ * @var array
78+ */
79+ protected $ sort_order = array ();
80+
81+ /**
82+ * Whether any plugins have the 'force_activation' setting set to true
83+ *
84+ * @since 2.5.0
85+ *
86+ * @var bool
87+ */
88+ protected $ has_forced_activation = false ;
89+
90+ /**
91+ * Whether any plugins have the 'force_deactivation' setting set to true
92+ *
93+ * @since 2.5.0
94+ *
95+ * @var bool
96+ */
97+ protected $ has_forced_deactivation = false ;
98+
7099 /**
71100 * Name of the unique ID to hash notices.
72101 *
@@ -256,13 +285,8 @@ public function init() {
256285 return ;
257286 }
258287
259- $ sorted = array ();
260-
261- foreach ( $ this ->plugins as $ plugin ) {
262- $ sorted [] = $ plugin ['name ' ];
263- }
264-
265- array_multisort ( $ sorted , SORT_ASC , $ this ->plugins );
288+ // Sort the plugins
289+ array_multisort ( $ this ->sort_order , SORT_ASC , $ this ->plugins );
266290
267291 add_action ( 'admin_menu ' , array ( $ this , 'admin_menu ' ) );
268292 add_action ( 'admin_head ' , array ( $ this , 'dismiss ' ) );
@@ -277,19 +301,13 @@ public function init() {
277301 }
278302
279303 // Setup the force activation hook.
280- foreach ( $ this ->plugins as $ plugin ) {
281- if ( isset ( $ plugin ['force_activation ' ] ) && true === $ plugin ['force_activation ' ] ) {
282- add_action ( 'admin_init ' , array ( $ this , 'force_activation ' ) );
283- break ;
284- }
304+ if ( true === $ this ->has_forced_activation ) {
305+ add_action ( 'admin_init ' , array ( $ this , 'force_activation ' ) );
285306 }
286307
287308 // Setup the force deactivation hook.
288- foreach ( $ this ->plugins as $ plugin ) {
289- if ( isset ( $ plugin ['force_deactivation ' ] ) && true === $ plugin ['force_deactivation ' ] ) {
290- add_action ( 'switch_theme ' , array ( $ this , 'force_deactivation ' ) );
291- break ;
292- }
309+ if ( true === $ this ->has_forced_deactivation ) {
310+ add_action ( 'switch_theme ' , array ( $ this , 'force_deactivation ' ) );
293311 }
294312
295313 }
@@ -375,8 +393,6 @@ public function admin_menu() {
375393 return ;
376394 }
377395
378- $ this ->populate_file_path ();
379-
380396 foreach ( $ this ->plugins as $ plugin ) {
381397 if ( ! is_plugin_active ( $ plugin ['file_path ' ] ) ) {
382398
@@ -718,8 +734,7 @@ public function notices() {
718734 return ;
719735 }
720736
721- $ installed_plugins = get_plugins (); // Retrieve a list of all the plugins
722- $ this ->populate_file_path ();
737+ $ installed_plugins = get_plugins (); // Retrieve a list of all the plugins
723738
724739 $ message = array (); // Store the messages in an array to be outputted after plugins have looped through.
725740 $ install_link = false ; // Set to false, change to true in loop if conditions exist, used for action link 'install'.
@@ -930,14 +945,37 @@ public function register( $plugin ) {
930945 return ;
931946 }
932947
933- foreach ( $ this ->plugins as $ registered_plugin ) {
934- if ( $ plugin ['slug ' ] === $ registered_plugin ['slug ' ] ) {
935- return ;
936- }
948+ if ( ! is_string ( $ plugin ['slug ' ] ) || empty ( $ plugin ['slug ' ] ) || isset ( $ this ->plugins [ $ plugin ['slug ' ] ] ) ) {
949+ return ;
937950 }
938951
939- $ this ->plugins [] = $ plugin ;
952+ $ defaults = array (
953+ 'name ' => '' ,
954+ 'slug ' => '' ,
955+ 'source ' => 'repo ' ,
956+ 'required ' => false ,
957+ 'version ' => '' ,
958+ 'force_activation ' => false ,
959+ 'force_deactivation ' => false ,
960+ 'external_url ' => '' ,
961+ 'is_callable ' => '' ,
962+ );
963+
964+ $ plugin = wp_parse_args ( $ plugin , $ defaults );
965+ $ plugin ['file_path ' ] = $ this ->_get_plugin_basename_from_slug ( $ plugin ['slug ' ] );
940966
967+ $ this ->plugins [ $ plugin ['slug ' ] ] = $ plugin ;
968+ $ this ->sort_order [ $ plugin ['slug ' ] ] = $ plugin ['name ' ];
969+
970+ // Should we add the force activation hook ?
971+ if ( true === $ plugin ['force_activation ' ] ) {
972+ $ this ->has_forced_activation = true ;
973+ }
974+
975+ // Should we add the force deactivation hook ?
976+ if ( true === $ plugin ['force_deactivation ' ] ) {
977+ $ this ->has_forced_deactivation = true ;
978+ }
941979 }
942980
943981 /**
@@ -1011,12 +1049,20 @@ public function flush_plugins_cache() {
10111049 * Set file_path key for each installed plugin.
10121050 *
10131051 * @since 2.1.0
1052+ *
1053+ * @param string $plugin_slug Optional. If set, only (re-)populates the file path for that specific plugin.
1054+ * Parameter added in v2.5.0.
10141055 */
1015- public function populate_file_path () {
1056+ public function populate_file_path ( $ plugin_slug = null ) {
1057+
1058+ if ( is_string ( $ plugin_slug ) && ! empty ( $ plugin_slug ) ) {
1059+ $ this ->plugins [ $ plugin_slug ]['file_path ' ] = $ this ->_get_plugin_basename_from_slug ( $ plugin_slug );
10161060
1017- // Add file_path key for all plugins.
1018- foreach ( $ this ->plugins as $ plugin => $ values ) {
1019- $ this ->plugins [ $ plugin ]['file_path ' ] = $ this ->_get_plugin_basename_from_slug ( $ values ['slug ' ] );
1061+ } else {
1062+ // Add file_path key for all plugins.
1063+ foreach ( $ this ->plugins as $ slug => $ values ) {
1064+ $ this ->plugins [ $ slug ]['file_path ' ] = $ this ->_get_plugin_basename_from_slug ( $ slug );
1065+ }
10201066 }
10211067
10221068 }
@@ -1032,6 +1078,10 @@ public function populate_file_path() {
10321078 */
10331079 protected function _get_plugin_basename_from_slug ( $ slug ) {
10341080
1081+ if ( ! function_exists ( 'get_plugins ' ) ) {
1082+ require_once ABSPATH . 'wp-admin/includes/plugin.php ' ;
1083+ }
1084+
10351085 $ keys = array_keys ( get_plugins () );
10361086
10371087 foreach ( $ keys as $ key ) {
@@ -1111,9 +1161,6 @@ public function update_dismiss() {
11111161 */
11121162 public function force_activation () {
11131163
1114- // Set file_path parameter for any installed plugins.
1115- $ this ->populate_file_path ();
1116-
11171164 $ installed_plugins = get_plugins ();
11181165
11191166 foreach ( $ this ->plugins as $ plugin ) {
@@ -1143,9 +1190,6 @@ public function force_activation() {
11431190 */
11441191 public function force_deactivation () {
11451192
1146- // Set file_path parameter for any installed plugins.
1147- $ this ->populate_file_path ();
1148-
11491193 foreach ( $ this ->plugins as $ plugin ) {
11501194 // Only proceed forward if the parameter is set to true and plugin is active.
11511195 if ( isset ( $ plugin ['force_deactivation ' ] ) && $ plugin ['force_deactivation ' ] && is_plugin_active ( $ plugin ['file_path ' ] ) ) {
0 commit comments