2424 * Plugin Name: W++ Core
2525 * Plugin URI: https://github.com/tofandel/wplusplus-core/
2626 * Description: A Wordpress Plugin acting as core for other of my plugins and including the Ultimate Redux Framework Bundle and OOP APIs to use it
27- * Version: 1.8.1
27+ * Version: 1.9
2828 * Author: Adrien Foulon <tofandel@tukan.hu>
2929 * Author URI: https://tukan.fr/a-propos/#adrien-foulon
3030 * Text Domain: wppc
@@ -42,11 +42,111 @@ class WPlusPlusCore extends WP_Plugin implements WP_Plugin_Interface {
4242 protected $ no_redux = true ;
4343
4444 public function actionsAndFilters () {
45- add_action ('site_transient_update_plugins ' , [$ this , 'WPPBundledUpdate ' ]);
45+
46+ //If we have another plugin using the core (which is the point of this plugin)
47+ //Then we can hide the core and make it update with the latest version when this other plugin is updating/
48+ add_action ( 'upgrader_process_complete ' , [ $ this , 'WPPBundledUpgrade ' ], 10 , 2 );
49+ //add_action( 'site_transient_update_plugins', [ $this, 'WPPBundledUpdate' ] );
50+ add_action ( 'pre_current_active_plugins ' , [ $ this , 'maybe_hide_plugin ' ] );
51+ add_filter ( 'all_plugins ' , [ $ this , 'multisite_maybe_hide_plugin ' ] );
52+ }
53+
54+
55+ public function multisite_maybe_hide_plugin ( $ plugins ) {
56+ if ( count ( self ::getSingletons () ) > 1 ) {
57+ unset( $ plugins [ $ this ->getPluginFile () ] );
58+ }
59+
60+ return $ plugins ;
61+ }
62+
63+ public function maybe_hide_plugin () {
64+ global $ wp_list_table ;
65+ if ( count ( self ::getSingletons () ) > 1 && isset ( $ wp_list_table ->items [ $ this ->getPluginFile () ] ) ) {
66+ unset( $ wp_list_table ->items [ $ this ->getPluginFile () ] );
67+ }
4668 }
4769
48- public function WPPBundledUpdate () {
70+ /**
71+ * @param $plugins
72+ *
73+ * @return bool
74+ * @throws \ReflectionException
75+ */
76+ private function searchPlugins ( $ plugins ) {
77+ $ non_core = self ::getSingletons ();
78+ $ class = new \ReflectionClass ( static ::class );
79+ $ class ->getName ();
80+ unset( $ non_core [ $ class ->getName () ] );
81+
82+ foreach ( $ non_core as $ plugin ) {
83+ /**
84+ * @var WP_Plugin $plugin
85+ */
86+ $ file = $ plugin ->getPluginFile ();
87+ if ( in_array ( $ file , $ plugins ) ) {
88+ return true ;
89+ }
90+ }
91+
92+ return false ;
93+ }
94+
95+ /**
96+ * @param \WP_Upgrader $upgrader
97+ *
98+ * @param array $info
99+ *
100+ * @return bool|array
101+ * @throws \ReflectionException
102+ */
103+ public function WPPBundledUpgrade ( $ upgrader , $ info = array () ) {
104+ if ( $ info ['action ' ] == 'update ' && $ info ['type ' ] == 'plugin ' && $ this ->searchPlugins ( $ info ['plugins ' ] ) ) {
105+ $ plugin = $ this ->getPluginFile ();
106+ $ current = get_site_transient ( 'update_plugins ' );
107+ if ( ! isset ( $ current ->response [ $ plugin ] ) ) {
108+ return false ;
109+ }
110+ // Get the URL to the zip file
111+ $ r = $ current ->response [ $ plugin ];
112+
113+ add_filter ( 'upgrader_pre_install ' , array ( $ upgrader , 'deactivate_plugin_before_upgrade ' ), 10 , 2 );
114+ add_filter ( 'upgrader_clear_destination ' , array ( $ upgrader , 'delete_old_plugin ' ), 10 , 4 );
115+ //'source_selection' => array($upgrader, 'source_selection'), //there's a trac ticket to move up the directory for zip's which are made a bit differently, useful for non-.org plugins.
116+ add_action ( 'upgrader_process_complete ' , 'wp_clean_plugins_cache ' , 9 , 0 );
117+ $ upgrader ->run ( array (
118+ 'package ' => $ r ->package ,
119+ 'destination ' => WP_PLUGIN_DIR ,
120+ 'clear_destination ' => true ,
121+ 'abort_if_destination_exists ' => false ,
122+ 'clear_working ' => true ,
123+ 'is_multi ' => true ,
124+ 'hook_extra ' => array (
125+ 'plugin ' => $ plugin ,
126+ 'type ' => 'plugin ' ,
127+ 'action ' => 'update ' ,
128+ ),
129+ ) );
130+ // Cleanup our hooks, in case something else does a upgrade on this connection.
131+ remove_action ( 'upgrader_process_complete ' , 'wp_clean_plugins_cache ' , 9 );
132+ remove_filter ( 'upgrader_pre_install ' , array ( $ upgrader , 'deactivate_plugin_before_upgrade ' ) );
133+ remove_filter ( 'upgrader_clear_destination ' , array ( $ upgrader , 'delete_old_plugin ' ) );
134+
135+ if ( ! $ upgrader ->result || is_wp_error ( $ upgrader ->result ) ) {
136+ return $ upgrader ->result ;
137+ }
138+
139+ // Force refresh of plugin update information
140+ wp_clean_plugins_cache ( true );
141+
142+ //The plugin will get deactivated, we set a transient that the mu-plugin will use
143+ //to try to reactivate the plugin on the next wordpress loading and abort if a fatal error occurs
144+ set_transient ( 'wpp_reactivate_core ' , $ this ->getPluginFile (), 60 * 20 );
145+
146+ return true ;
147+ }
49148
149+ return false ;
50150 }
51151
52152 public function definitions () {
0 commit comments