| 1 |
<?php |
| 2 |
|
| 3 |
namespace wpforo\classes; |
| 4 |
|
| 5 |
if( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class PluginsOrdering { |
| 10 |
/** |
| 11 |
* wpForo plugin basename. |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
private $wpforo; |
| 16 |
|
| 17 |
/** |
| 18 |
* Query Monitor plugin basename. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
private $query_monitor = 'query-monitor/query-monitor.php'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Constructor. |
| 26 |
*/ |
| 27 |
public function __construct( string $wpforo = 'wpforo/wpforo.php' ) { |
| 28 |
$this->wpforo = $wpforo; |
| 29 |
|
| 30 |
// Ensure correct ordering when plugins are updated. |
| 31 |
add_filter( |
| 32 |
'pre_update_option_active_plugins', |
| 33 |
[ $this, 'filter_active_plugins' ], |
| 34 |
9999 |
| 35 |
); |
| 36 |
|
| 37 |
// Ensure correct ordering for network-activated plugins. |
| 38 |
add_filter( |
| 39 |
'pre_update_site_option_active_sitewide_plugins', |
| 40 |
[ $this, 'filter_active_sitewide_plugins' ], |
| 41 |
9999 |
| 42 |
); |
| 43 |
|
| 44 |
// Reorder plugins after updates. |
| 45 |
add_action( |
| 46 |
'upgrader_process_complete', |
| 47 |
[ $this, 'reorder_after_update' ], |
| 48 |
9999, |
| 49 |
2 |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
public function filter_active_plugins( $plugins ) { |
| 54 |
if( empty( $plugins ) || ! is_array( $plugins ) ) { |
| 55 |
return $plugins; |
| 56 |
} |
| 57 |
|
| 58 |
return $this->reorder_plugins( $plugins ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Reorder plugins ensuring Query Monitor is first and wpForo second. |
| 63 |
*/ |
| 64 |
private function reorder_plugins( $plugins ): array { |
| 65 |
$priority = []; |
| 66 |
|
| 67 |
if( in_array( $this->query_monitor, $plugins, true ) ) { |
| 68 |
$priority[] = $this->query_monitor; |
| 69 |
} |
| 70 |
|
| 71 |
if( in_array( $this->wpforo, $plugins, true ) ) { |
| 72 |
$priority[] = $this->wpforo; |
| 73 |
} |
| 74 |
|
| 75 |
$remaining = array_diff( $plugins, $priority ); |
| 76 |
|
| 77 |
return array_values( array_merge( $priority, $remaining ) ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Reorder plugins after wpForo is updated. |
| 82 |
*/ |
| 83 |
public function reorder_after_update( $upgrader, $options ) { |
| 84 |
if( empty( $options['type'] ) || 'plugin' !== $options['type'] ) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
if( empty( $options['action'] ) || 'update' !== $options['action'] ) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
if( empty( $options['plugins'] ) || ! is_array( $options['plugins'] ) ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
if( ! in_array( $this->wpforo, $options['plugins'], true ) ) { |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
// Reorder single-site plugins. |
| 101 |
$active_plugins = get_option( 'active_plugins', [] ); |
| 102 |
update_option( |
| 103 |
'active_plugins', |
| 104 |
$this->reorder_plugins( $active_plugins ) |
| 105 |
); |
| 106 |
|
| 107 |
// Reorder multisite plugins. |
| 108 |
if( is_multisite() ) { |
| 109 |
$network_plugins = get_site_option( |
| 110 |
'active_sitewide_plugins', |
| 111 |
[] |
| 112 |
); |
| 113 |
|
| 114 |
update_site_option( |
| 115 |
'active_sitewide_plugins', |
| 116 |
$this->filter_active_sitewide_plugins( $network_plugins ) |
| 117 |
); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Reorder network active plugins for multisite installations. |
| 123 |
*/ |
| 124 |
public function filter_active_sitewide_plugins( $plugins ) { |
| 125 |
if( empty( $plugins ) || ! is_array( $plugins ) ) { |
| 126 |
return $plugins; |
| 127 |
} |
| 128 |
|
| 129 |
$priority = []; |
| 130 |
|
| 131 |
if( isset( $plugins[ $this->query_monitor ] ) ) { |
| 132 |
$priority[ $this->query_monitor ] = $plugins[ $this->query_monitor ]; |
| 133 |
unset( $plugins[ $this->query_monitor ] ); |
| 134 |
} |
| 135 |
|
| 136 |
if( isset( $plugins[ $this->wpforo ] ) ) { |
| 137 |
$priority[ $this->wpforo ] = $plugins[ $this->wpforo ]; |
| 138 |
unset( $plugins[ $this->wpforo ] ); |
| 139 |
} |
| 140 |
|
| 141 |
return $priority + $plugins; |
| 142 |
} |
| 143 |
} |
| 144 |
|