11<?php
22/**
3+ * NOTE: As this is non-extension based plugin, there is no data network-populate / network-drop data links if the plugin is network-enabled
34 * @package ExpandableFAQ
45 * @author KestutisIT
56 * @copyright KestutisIT
1415use ExpandableFAQ \Models \Language \LanguageInterface ;
1516use ExpandableFAQ \Models \Cache \StaticSession ;
1617use ExpandableFAQ \Models \Status \NetworkStatus ;
17- use ExpandableFAQ \Models \Status \ SingleStatus ;
18- use ExpandableFAQ \Models \Update \Database60Z ;
18+ use ExpandableFAQ \Models \Update \ NetworkPatchesObserver ;
19+ use ExpandableFAQ \Models \Update \NetworkUpdatesObserver ;
1920use ExpandableFAQ \Models \Validation \StaticValidator ;
2021use ExpandableFAQ \Views \PageView ;
2122
@@ -33,149 +34,18 @@ public function __construct(ConfigurationInterface &$paramConf, LanguageInterfac
3334 $ this ->lang = $ paramLang ;
3435 }
3536
36- /**
37- * Activate (enable+install or enable only) plugin for across the whole network
38- * @note - 'get_sites' function requires WordPress 4.6 or newer!
39- * @throws \Exception
40- */
41- public function processPopulateData ()
42- {
43- // Create mandatory instances
44- $ objNetworkStatus = new NetworkStatus ($ this ->conf , $ this ->lang );
45-
46- // We only allow to populate the data if the newest plugin database struct exists
47- if ($ objNetworkStatus ->checkPluginDB_StructExists ($ this ->conf ->getPluginSemver ()))
48- {
49- // Save original locale
50- $ orgLang = $ this ->lang ;
51-
52- $ sites = get_sites ();
53- foreach ($ sites AS $ site )
54- {
55- $ blogId = $ site ->blog_id ;
56- switch_to_blog ($ blogId );
57-
58- $ lang = new Language (
59- $ this ->conf ->getTextDomain (), $ this ->conf ->getGlobalLangPath (), $ this ->conf ->getLocalLangPath (), $ this ->conf ->getBlogLocale ($ blogId ), FALSE
60- );
61- $ objInstaller = new InstallController ($ this ->conf , $ lang , $ blogId );
62-
63- // Populate the data (without table creation)
64- // INFO: This plugin do not use custom roles
65- $ objInstaller ->setCustomCapabilities ();
66- // INFO: This plugin do not use REST API
67- // INFO: This plugin do not use custom post types
68- $ objInstaller ->setContent ();
69- $ objInstaller ->replaceResettableContent ();
70- $ objInstaller ->registerAllForTranslation ();
71- }
72- // Switch back to current blog id. Restore current blog won't work here, as it would just restore to previous blog of the long loop
73- switch_to_blog ($ this ->conf ->getBlogId ());
74- // Restore original locale
75- $ this ->lang = $ orgLang ;
76- }
77- }
78-
79- /**
80- * Note: for data drop, we do not drop the roles, to protect from issues to happen on other extensions
81- */
82- public function processDropData ()
83- {
84- $ sites = get_sites ();
85- foreach ($ sites AS $ site )
86- {
87- $ blogId = $ site ->blog_id ;
88- switch_to_blog ($ blogId );
89-
90- // Delete any old table content if exists
91- foreach (Install::getTableClasses () AS $ tableClass )
92- {
93- if (class_exists ($ tableClass ))
94- {
95- $ objTable = new $ tableClass ($ this ->conf , $ this ->lang , $ blogId );
96- if (method_exists ($ objTable , 'deleteContent ' ) && method_exists ($ objTable , 'getDebugMessages ' ) && method_exists ($ objTable , 'getErrorMessages ' ))
97- {
98- $ objTable ->deleteContent ();
99- StaticSession::cacheHTMLArray ('admin_debug_message ' , $ objTable ->getDebugMessages ());
100- // We don't process okay messages here
101- StaticSession::cacheValueArray ('admin_error_message ' , $ objTable ->getErrorMessages ());
102- }
103- }
104- }
105- // Delete any old WordPress posts if exists
106- // INFO: NOTHING for plugin - it does not use any custom post types
107- // NOTE: To void a errors on WordPress page deletion error, we skip exception raising for them
108- }
109- }
110-
111- /**
112- * For updating across multisite the network-enabled plugin from 5.0.0 to V6.0.Z
113- * @note - Works only with WordPress 4.6+
114- * @return bool
115- * @throws \Exception
116- */
117- private function process60Z_Patches ()
118- {
119- // Create mandatory instances
120- $ allSitesSemverUpdated = TRUE ;
121-
122- // NOTE: Network site is one of the sites. So it will update network site id as well.
123- $ sites = get_sites ();
124- foreach ($ sites AS $ site )
125- {
126- $ blogId = $ site ->blog_id ;
127- switch_to_blog ($ blogId );
128-
129- $ lang = new Language (
130- $ this ->conf ->getTextDomain (), $ this ->conf ->getGlobalLangPath (), $ this ->conf ->getLocalLangPath (), $ this ->conf ->getBlogLocale ($ blogId ), FALSE
131- );
132-
133- // Update the database data
134- $ objSingleDB_Patch = new Database60Z ($ this ->conf , $ lang , $ blogId );
135- $ objSingleStatus = new SingleStatus ($ this ->conf , $ lang , $ blogId );
136- $ pluginSemverInDB = $ objSingleStatus ->getPluginSemverInDatabase ();
137-
138- // Process ONLY if the current blog has populated extension data, network struct is already updated
139- // and current site database was not yet updated
140- if (
141- $ objSingleStatus ->checkPluginDataExists ('6.0.0 ' )
142- && version_compare ($ pluginSemverInDB , '6.0.0 ' , '>= ' ) && version_compare ($ pluginSemverInDB , '6.1.0 ' , '< ' )
143- ) {
144- $ dataPatched = $ objSingleDB_Patch ->patchData ();
145- if ($ dataPatched === FALSE )
146- {
147- $ allSitesSemverUpdated = FALSE ;
148- } else
149- {
150- // Update the current site database version to 6.0.0
151- $ semverUpdated = $ objSingleDB_Patch ->updateDatabaseSemver ();
152- if ($ semverUpdated == FALSE )
153- {
154- $ allSitesSemverUpdated = FALSE ;
155- }
156- }
157- }
158-
159- StaticSession::cacheHTMLArray ('admin_debug_message ' , $ objSingleDB_Patch ->getDebugMessages ());
160- StaticSession::cacheValueArray ('admin_okay_message ' , $ objSingleDB_Patch ->getOkayMessages ());
161- StaticSession::cacheValueArray ('admin_error_message ' , $ objSingleDB_Patch ->getErrorMessages ());
162- }
163- // Switch back to current network blog id. Restore current blog won't work here, as it would just restore to previous blog of the long loop
164- switch_to_blog ($ this ->conf ->getBlogId ());
165-
166- return $ allSitesSemverUpdated ;
167- }
168-
16937 /**
17038 * @throws \Exception
17139 */
17240 private function processUpdate ()
17341 {
17442 // Create mandatory instances
17543 $ objStatus = new NetworkStatus ($ this ->conf , $ this ->lang );
44+ $ objUpdatesObserver = new NetworkUpdatesObserver ($ this ->conf , $ this ->lang );
45+ $ objPatchesObserver = new NetworkPatchesObserver ($ this ->conf , $ this ->lang );
17646
17747 // Allow only one update at-a-time per site refresh. We need that to save resources of server to not to get to timeout phase
178- $ allUpdatableSitesUpdated = FALSE ;
48+ $ allUpdatableSitesSemverUpdated = FALSE ;
17949 $ minPluginSemverInDatabase = $ objStatus ->getMinPluginSemverInDatabase ();
18050 $ maxPluginSemverInDatabase = $ objStatus ->getMaxPluginSemverInDatabase ();
18151 $ latestSemver = $ this ->conf ->getPluginSemver ();
@@ -189,20 +59,30 @@ private function processUpdate()
18959 if (version_compare ($ minPluginSemverInDatabase , $ latestSemver , '== ' ))
19060 {
19161 // It's a last version
192- $ allUpdatableSitesUpdated = TRUE ;
62+ $ allUpdatableSitesSemverUpdated = TRUE ;
19363 }
19464
195- // Run patches
65+ // Process 6.0.Z patches
19666 if (version_compare ($ minPluginSemverInDatabase , '6.0.0 ' , '>= ' ) && version_compare ($ maxPluginSemverInDatabase , '6.1.0 ' , '< ' ))
19767 {
198- $ allUpdatableSitesUpdated = $ this -> process60Z_Patches ( );
68+ $ allUpdatableSitesSemverUpdated = $ objPatchesObserver -> doPatch ( 6 , 0 );
19969 }
70+
71+ // Cache update messages
72+ StaticSession::cacheHTMLArray ('admin_debug_message ' , $ objUpdatesObserver ->getSavedDebugMessages ());
73+ StaticSession::cacheValueArray ('admin_okay_message ' , $ objUpdatesObserver ->getSavedOkayMessages ());
74+ StaticSession::cacheValueArray ('admin_error_message ' , $ objUpdatesObserver ->getSavedErrorMessages ());
75+
76+ // Cache patch messages
77+ StaticSession::cacheHTMLArray ('admin_debug_message ' , $ objPatchesObserver ->getSavedDebugMessages ());
78+ StaticSession::cacheValueArray ('admin_okay_message ' , $ objPatchesObserver ->getSavedOkayMessages ());
79+ StaticSession::cacheValueArray ('admin_error_message ' , $ objPatchesObserver ->getSavedErrorMessages ());
20080 }
20181
20282 // Check if plugin is up-to-date
20383 $ pluginUpToDate = $ objStatus ->isAllBlogsWithPluginDataUpToDate ();
20484
205- if ($ allUpdatableSitesUpdated === FALSE || $ pluginUpToDate === FALSE )
85+ if ($ allUpdatableSitesSemverUpdated === FALSE || $ pluginUpToDate === FALSE )
20686 {
20787 // Failed or if there is more updates to go
20888 wp_safe_redirect ('admin.php?page= ' .$ this ->conf ->getPluginURL_Prefix ().'network-status&tab=status ' );
@@ -226,8 +106,6 @@ public function printContent()
226106 $ printOkayMessage = StaticSession::getValueOnce ('admin_okay_message ' );
227107
228108 // Both - _POST and _GET supported
229- if (isset ($ _GET ['populate_data ' ]) || isset ($ _POST ['populate_data ' ])) { $ this ->processPopulateData (); }
230- if (isset ($ _GET ['drop_data ' ]) || isset ($ _POST ['drop_data ' ])) { $ this ->processDropData (); }
231109 if (isset ($ _GET ['update ' ]) || isset ($ _POST ['update ' ])) { $ this ->processUpdate (); }
232110
233111 // Create mandatory instances
0 commit comments