PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.79
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.79
5.5.83 5.5.82 5.5.81 5.5.80 5.5.79 5.5.77 5.5.76 5.5.75 5.5.73 5.5.72 5.5.22 5.5.23 5.5.29 5.5.3 5.5.31 5.5.32 5.5.34 5.5.35 5.5.36 5.5.37 5.5.4 5.5.40 5.5.41 5.5.42 5.5.43 All 159 releases
wp-data-access / includes / class-wp-data-access-switch.php

class-wp-data-access-switch.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.79, at includes/class-wp-data-access-switch.php

223 lines 9.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Suppress "error - 0 - No summary was found for this file" on phpdoc generation
5 *
6 * @package plugin\includes
7 */
8 use WPDataAccess\Plugin_Table_Models\WPDA_Design_Table_Model;
9 use WPDataAccess\Plugin_Table_Models\WPDA_Logging_Model;
10 use WPDataAccess\Plugin_Table_Models\WPDA_Media_Model;
11 use WPDataAccess\Plugin_Table_Models\WPDA_Publisher_Model;
12 use WPDataAccess\Plugin_Table_Models\WPDA_Table_Settings_Model;
13 use WPDataAccess\Plugin_Table_Models\WPDA_User_Menus_Model;
14 use WPDataAccess\Plugin_Table_Models\WPDP_Page_Model;
15 use WPDataAccess\Plugin_Table_Models\WPDP_Project_Model;
16 use WPDataAccess\Plugin_Table_Models\WPDP_Project_Design_Table_Model;
17 use WPDataAccess\Premium\WPDAPRO_Dashboard\WPDAPRO_Dashboard;
18 use WPDataAccess\Utilities\WPDA_Repository;
19 use WPDataAccess\WPDA;
20 /**
21 * Class WP_Data_Access_Switch
22 *
23 * Switch to:
24 * + activate plugin {@see WP_Data_Access_Switch::activate()}
25 * + deactive plugin {@see WP_Data_Access_Switch::deactivate()}
26 *
27 * @author Peter Schulz
28 * @since 1.0.0
29 *
30 * @see WP_Data_Access_Switch::activate()
31 * @see WP_Data_Access_Switch::deactivate()
32 */
33 // phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound
34 class WP_Data_Access_Switch {
35 /**
36 * Activate plugin WP Data Access
37 *
38 * The user must have the appropriate privileges to perform this operation.
39 *
40 * For single site installation {@see WP_Data_Access_Switch::activate_blog()} will be called. For multi site
41 * installations {@see WP_Data_Access_Switch::activate_blog()} must be called for every blog.
42 *
43 * IMPORTANT!!!
44 *
45 * For blogs installed on multi site installations after activation of the plugin, activation of the plugin for
46 * that blog will not be performed if the plugin is network activated. In that case the admin user of the blog
47 * will receive a message when viewing a plugin page with an option to follow these steps manually.
48 *
49 * @since 1.0.0
50 *
51 * @see WP_Data_Access_Switch::activate_blog()
52 */
53 public static function activate() {
54 if ( current_user_can( 'activate_plugins' ) ) {
55 // Activate plugin.
56 if ( is_multisite() ) {
57 global $wpdb;
58 // Multisite installation.
59 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
60 $blogids = $wpdb->get_col( "select blog_id from {$wpdb->blogs}" );
61 // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
62 foreach ( $blogids as $blog_id ) {
63 // Activate blog.
64 switch_to_blog( $blog_id );
65 self::activate_blog();
66 restore_current_blog();
67 }
68 } else {
69 // Single site installation.
70 self::activate_blog();
71 }
72 }
73 }
74
75 /**
76 * Activate blog
77 *
78 * The user must have the appropriate privileges to perform this operation.
79 *
80 * On activation this method checks whether there has previously been a version of the plugin installed. For this
81 * purpose the wp_options table read directly (usually done via class WPDA). If a value is found, this method
82 * checks if the version number in wp_options is the same as the plugin version. If these are equal, no action is
83 * needed. If they are not equal, this method will check if there is an upgrade or downgrade for the delta
84 * between these releases.
85 *
86 * This action is performed on the 'active WordPress blog'. On single site there is only one blog. On multisite
87 * installations it must be executed for every blog.
88 *
89 * On a fresh installation the following actions are performed:
90 * + save plugin version number in wp_options {@see WPDA::set_option()}
91 * + (re)create plugin repository {@see WPDA_Repository::recreate()}
92 *
93 * @since 1.0.0
94 *
95 * @see WPDA::set_option()
96 * @see WPDA_Repository::create()
97 */
98 protected static function activate_blog() {
99 if ( current_user_can( 'activate_plugins' ) ) {
100 if ( get_option( WPDataAccess\WPDA::OPTION_WPDA_VERSION[0] ) !== WPDataAccess\WPDA::OPTION_WPDA_VERSION[1] ) {
101 self::recreate_repository();
102 } elseif ( !WPDA_User_Menus_Model::table_exists() || !WPDA_Design_Table_Model::table_exists() || !WPDP_Project_Design_Table_Model::table_exists() || !WPDA_Publisher_Model::table_exists() || !WPDA_Logging_Model::table_exists() || !WPDA_Media_Model::table_exists() || !WPDP_Project_Model::table_exists() || !WPDP_Page_Model::table_exists() || !WPDA_Table_Settings_Model::table_exists() ) {
103 self::recreate_repository();
104 }
105 if ( wpda_freemius()->is_free_plan() ) {
106 update_option( 'wpda_fulltext_support', 'off' );
107 }
108 self::flush_rewrite_rules();
109 self::set_legacy_tools();
110 }
111 }
112
113 /**
114 * (re)create plugin repository
115 *
116 * If no repository is found, a new one is created
117 * If a repository is found, the table structures are update and the data transferred
118 */
119 protected static function recreate_repository() {
120 $wpda_repository = new WPDA_Repository();
121 $wpda_repository->recreate();
122 // Save (new) plugin version.
123 WPDA::set_option( WPDA::OPTION_WPDA_VERSION );
124 // Create content folder to store CSV and cache files.
125 WPDA::wpda_create_content_folder();
126 // Generate new sonce seed on every update.
127 WPDA::set_option( WPDA::OPTION_PLUGIN_SONCE_SEED, bin2hex( openssl_random_pseudo_bytes( wp_rand( 40, 60 ) ) ) );
128 WPDA::set_option( WPDA::OPTION_WPDA_UPGRADED, true );
129 }
130
131 /**
132 * Deactivate plugin WP Data Access
133 *
134 * On deactivation we leave the repository and options as they are in case the user wants to reactivate the
135 * plugin later again. Tables and options are deleted when the plugin is uninstalled. To keep tables and options
136 * on uninstall change plugin settings (see uninstall settings).
137 *
138 * @since 1.0.0
139 */
140 public static function deactivate() {
141 if ( current_user_can( 'activate_plugins' ) ) {
142 // Deactivate plugin.
143 if ( is_multisite() ) {
144 global $wpdb;
145 // Multisite installation.
146 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
147 $blogids = $wpdb->get_col( "select blog_id from {$wpdb->blogs}" );
148 // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
149 foreach ( $blogids as $blog_id ) {
150 // Deactivate blog.
151 switch_to_blog( $blog_id );
152 self::flush_rewrite_rules();
153 restore_current_blog();
154 }
155 } else {
156 // Single site installation.
157 self::flush_rewrite_rules();
158 }
159 }
160 }
161
162 private static function flush_rewrite_rules() {
163 // Unscheduled all WP Data Access events
164 self::unschedule_event( 'wpda_data_backup' );
165 self::unschedule_event( \WPDataAccess\Query_Builder\WPDA_Query_Builder_Scheduler::SCHEDULER_HOOK_NAME );
166 self::unschedule_event( \WPDataAccess\Utilities\WPDA_Export_Scheduler::SCHEDULER_HOOK_NAME );
167 }
168
169 private static function unschedule_event( $hook ) {
170 $crons = _get_cron_array();
171 foreach ( $crons as $timestamp => $cron ) {
172 if ( isset( $cron[$hook] ) ) {
173 foreach ( $cron[$hook] as $event ) {
174 if ( isset( $event['args'] ) ) {
175 wp_unschedule_event( $timestamp, $hook, $event['args'] );
176 }
177 }
178 }
179 }
180 }
181
182 private static function set_legacy_tools() {
183 // Get current legacy tool settings
184 $stored_legacy_tools = get_option( 'wpda_plugin_legacy_tools' );
185 if ( false === $stored_legacy_tools ) {
186 // Use default for fresh installation
187 // Update if legacy tool is actively used
188 $option_legacy_tools = \WPDataAccess\Utilities\WPDA_Legacy_Tool_Visibility::get();
189 $option_updated = false;
190 if ( 0 < $option_legacy_tools['tables'][1] ) {
191 $option_legacy_tools['tables'] = array(true, $option_legacy_tools['tables'][1]);
192 $option_updated = true;
193 }
194 if ( 0 < $option_legacy_tools['forms'][1] ) {
195 $option_legacy_tools['forms'] = array(true, $option_legacy_tools['forms'][1]);
196 $option_updated = true;
197 }
198 if ( 0 < $option_legacy_tools['templates'][1] ) {
199 $option_legacy_tools['templates'] = array(true, $option_legacy_tools['templates'][1]);
200 $option_updated = true;
201 }
202 if ( 0 < $option_legacy_tools['designer'][1] ) {
203 $option_legacy_tools['designer'] = array(true, $option_legacy_tools['designer'][1]);
204 $option_updated = true;
205 }
206 if ( 0 < $option_legacy_tools['dashboards'][1] ) {
207 $option_legacy_tools['dashboards'] = array(true, $option_legacy_tools['dashboards'][1]);
208 $option_updated = true;
209 }
210 if ( 0 < $option_legacy_tools['charts'][1] ) {
211 $option_legacy_tools['charts'] = array(true, $option_legacy_tools['charts'][1]);
212 $option_updated = true;
213 }
214 if ( $option_updated ) {
215 // Save legacy tool settings.
216 WPDA::set_option( WPDA::OPTION_PLUGIN_LEGACY_TOOLS, $option_legacy_tools );
217 }
218 }
219 }
220
221 }
222
223 // phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound