| @@ -379,9 +379,9 @@ | ||
| 379 | 379 | function fs_newest_sdk_plugin_first() { |
| 380 | 380 | global $fs_active_plugins; |
| 381 | 381 | |
| 382 | 382 | /** |
| 383 | - * @todo Multi-site network activated plugin are always loaded prior to site plugins so if there's a a plugin activated in the network mode that has an older version of the SDK of another plugin which is site activated that has new SDK version, the fs-essential-functions.php will be loaded from the older SDK. Same thing about MU plugins (loaded even before network activated plugins). | |
| 383 | + * @todo Multi-site network activated plugin are always loaded prior to site plugins so if there's a plugin activated in the network mode that has an older version of the SDK of another plugin which is site activated that has new SDK version, the fs-essential-functions.php will be loaded from the older SDK. Same thing about MU plugins (loaded even before network activated plugins). | |
| 384 | 384 | * |
| 385 | 385 | * @link https://github.com/Freemius/wordpress-sdk/issues/26 |
| 386 | 386 | */ |
| 387 | 387 | |
| @@ -386,40 +386,57 @@ | ||
| 386 | 386 | */ |
| 387 | 387 | |
| 388 | 388 | $newest_sdk_plugin_path = $fs_active_plugins->newest->plugin_path; |
| 389 | 389 | |
| 390 | - $active_plugins = get_option( 'active_plugins', array() ); | |
| 391 | - $newest_sdk_plugin_key = array_search( $newest_sdk_plugin_path, $active_plugins ); | |
| 392 | - if ( 0 === $newest_sdk_plugin_key ) { | |
| 393 | - // if it's 0 it's the first plugin already, no need to continue | |
| 394 | - return false; | |
| 395 | - } else if ( is_numeric( $newest_sdk_plugin_key ) ) { | |
| 396 | - // Remove plugin from its current position. | |
| 397 | - array_splice( $active_plugins, $newest_sdk_plugin_key, 1 ); | |
| 390 | + $active_plugins = get_option( 'active_plugins', array() ); | |
| 391 | + $updated_active_plugins = array( $newest_sdk_plugin_path ); | |
| 398 | 392 | |
| 399 | - // Set it to be included first. | |
| 400 | - array_unshift( $active_plugins, $newest_sdk_plugin_path ); | |
| 393 | + $plugin_found = false; | |
| 394 | + $is_first_path = true; | |
| 401 | 395 | |
| 402 | - update_option( 'active_plugins', $active_plugins ); | |
| 396 | + foreach ( $active_plugins as $key => $plugin_path ) { | |
| 397 | + if ( $plugin_path === $newest_sdk_plugin_path ) { | |
| 398 | + if ( $is_first_path ) { | |
| 399 | + // if it's the first plugin already, no need to continue | |
| 400 | + return false; | |
| 401 | + } | |
| 403 | 402 | |
| 403 | + $plugin_found = true; | |
| 404 | + | |
| 405 | + // Skip the plugin (it is already added as the 1st item of $updated_active_plugins). | |
| 406 | + continue; | |
| 407 | + } | |
| 408 | + | |
| 409 | + $updated_active_plugins[] = $plugin_path; | |
| 410 | + | |
| 411 | + if ( $is_first_path ) { | |
| 412 | + $is_first_path = false; | |
| 413 | + } | |
| 414 | + } | |
| 415 | + | |
| 416 | + if ( $plugin_found ) { | |
| 417 | + update_option( 'active_plugins', $updated_active_plugins ); | |
| 418 | + | |
| 404 | 419 | return true; |
| 405 | - } else if ( is_multisite() && false === $newest_sdk_plugin_key ) { | |
| 420 | + } | |
| 421 | + | |
| 422 | + if ( is_multisite() ) { | |
| 406 | 423 | // Plugin is network active. |
| 407 | 424 | $network_active_plugins = get_site_option( 'active_sitewide_plugins', array() ); |
| 408 | 425 | |
| 409 | - if (isset($network_active_plugins[$newest_sdk_plugin_path])) { | |
| 410 | - reset($network_active_plugins); | |
| 411 | - if ( $newest_sdk_plugin_path === key($network_active_plugins) ) { | |
| 426 | + if ( isset( $network_active_plugins[ $newest_sdk_plugin_path ] ) ) { | |
| 427 | + reset( $network_active_plugins ); | |
| 428 | + if ( $newest_sdk_plugin_path === key( $network_active_plugins ) ) { | |
| 412 | 429 | // Plugin is already activated first on the network level. |
| 413 | 430 | return false; |
| 414 | - } else if ( is_numeric( $newest_sdk_plugin_key ) ) { | |
| 415 | - $time = $network_active_plugins[$newest_sdk_plugin_path]; | |
| 431 | + } else { | |
| 432 | + $time = $network_active_plugins[ $newest_sdk_plugin_path ]; | |
| 416 | 433 | |
| 417 | 434 | // Remove plugin from its current position. |
| 418 | - unset($network_active_plugins[$newest_sdk_plugin_path]); | |
| 435 | + unset( $network_active_plugins[ $newest_sdk_plugin_path ] ); | |
| 419 | 436 | |
| 420 | 437 | // Set it to be included first. |
| 421 | - $network_active_plugins = array($newest_sdk_plugin_path => $time) + $network_active_plugins; | |
| 438 | + $network_active_plugins = array( $newest_sdk_plugin_path => $time ) + $network_active_plugins; | |
| 422 | 439 | |
| 423 | 440 | update_site_option( 'active_sitewide_plugins', $network_active_plugins ); |
| 424 | 441 | |
| 425 | 442 | return true; |