PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.34
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.34
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.34, at includes/class-wp-data-access-switch.php

214 lines 8.6 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 class WP_Data_Access_Switch {
34 /**
35 * Activate plugin WP Data Access
36 *
37 * The user must have the appropriate privileges to perform this operation.
38 *
39 * For single site installation {@see WP_Data_Access_Switch::activate_blog()} will be called. For multi site
40 * installations {@see WP_Data_Access_Switch::activate_blog()} must be called for every blog.
41 *
42 * IMPORTANT!!!
43 *
44 * For blogs installed on multi site installations after activation of the plugin, activation of the plugin for
45 * that blog will not be performed if the plugin is network activated. In that case the admin user of the blog
46 * will receive a message when viewing a plugin page with an option to follow these steps manually.
47 *
48 * @since 1.0.0
49 *
50 * @see WP_Data_Access_Switch::activate_blog()
51 */
52 public static function activate() {
53 if ( current_user_can( 'activate_plugins' ) ) {
54 // Activate plugin.
55 if ( is_multisite() ) {
56 global $wpdb;
57 // Multisite installation.
58 $blogids = $wpdb->get_col( "select blog_id from {$wpdb->blogs}" );
59 // db call ok; no-cache ok.
60 foreach ( $blogids as $blog_id ) {
61 // Activate blog.
62 switch_to_blog( $blog_id );
63 self::activate_blog();
64 restore_current_blog();
65 }
66 } else {
67 // Single site installation.
68 self::activate_blog();
69 }
70 }
71 }
72
73 /**
74 * Activate blog
75 *
76 * The user must have the appropriate privileges to perform this operation.
77 *
78 * On activation this method checks whether there has previously been a version of the plugin installed. For this
79 * purpose the wp_options table read directly (usually done via class WPDA). If a value is found, this method
80 * checks if the version number in wp_options is the same as the plugin version. If these are equal, no action is
81 * needed. If they are not equal, this method will check if there is an upgrade or downgrade for the delta
82 * between these releases.
83 *
84 * This action is performed on the 'active WordPress blog'. On single site there is only one blog. On multisite
85 * installations it must be executed for every blog.
86 *
87 * On a fresh installation the following actions are performed:
88 * + save plugin version number in wp_options {@see WPDA::set_option()}
89 * + (re)create plugin repository {@see WPDA_Repository::recreate()}
90 *
91 * @since 1.0.0
92 *
93 * @see WPDA::set_option()
94 * @see WPDA_Repository::create()
95 */
96 protected static function activate_blog() {
97 if ( current_user_can( 'activate_plugins' ) ) {
98 if ( get_option( WPDataAccess\WPDA::OPTION_WPDA_VERSION[0] ) !== WPDataAccess\WPDA::OPTION_WPDA_VERSION[1] ) {
99 self::recreate_repository();
100 } 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() ) {
101 self::recreate_repository();
102 }
103 if ( wpda_freemius()->is_free_plan() ) {
104 update_option( 'wpda_fulltext_support', 'off' );
105 }
106 self::flush_rewrite_rules();
107 self::set_legacy_tools();
108 }
109 }
110
111 /**
112 * (re)create plugin repository
113 *
114 * If no repository is found, a new one is created
115 * If a repository is found, the table structures are update and the data transferred
116 */
117 protected static function recreate_repository() {
118 $wpda_repository = new WPDA_Repository();
119 $wpda_repository->recreate();
120 // Save (new) plugin version.
121 WPDA::set_option( WPDA::OPTION_WPDA_VERSION );
122 // Create content folder to store CSV and cache files.
123 WPDA::wpda_create_content_folder();
124 // Generate new sonce seed on every update.
125 WPDA::set_option( WPDA::OPTION_PLUGIN_SONCE_SEED, bin2hex( openssl_random_pseudo_bytes( wp_rand( 40, 60 ) ) ) );
126 WPDA::set_option( WPDA::OPTION_WPDA_UPGRADED, true );
127 }
128
129 /**
130 * Deactivate plugin WP Data Access
131 *
132 * On deactivation we leave the repository and options as they are in case the user wants to reactivate the
133 * plugin later again. Tables and options are deleted when the plugin is uninstalled. To keep tables and options
134 * on uninstall change plugin settings (see uninstall settings).
135 *
136 * @since 1.0.0
137 */
138 public static function deactivate() {
139 if ( current_user_can( 'activate_plugins' ) ) {
140 // Deactivate plugin.
141 if ( is_multisite() ) {
142 global $wpdb;
143 // Multisite installation.
144 $blogids = $wpdb->get_col( "select blog_id from {$wpdb->blogs}" );
145 // db call ok; no-cache ok.
146 foreach ( $blogids as $blog_id ) {
147 // Deactivate blog.
148 switch_to_blog( $blog_id );
149 self::flush_rewrite_rules();
150 restore_current_blog();
151 }
152 } else {
153 // Single site installation.
154 self::flush_rewrite_rules();
155 }
156 }
157 }
158
159 private static function flush_rewrite_rules() {
160 }
161
162 private static function set_legacy_tools() {
163 // Get current legacy tool settings
164 $stored_legacy_tools = get_option( 'wpda_plugin_legacy_tools' );
165 $option_legacy_tools = \WPDataAccess\Utilities\WPDA_Legacy_Tool_Visibility::get();
166 // Update legacy tool settings
167 if ( 0 === $option_legacy_tools['tables'] ) {
168 $option_legacy_tools['tables'][0] = false;
169 } else {
170 if ( false === $stored_legacy_tools ) {
171 $option_legacy_tools['tables'][0] = true;
172 }
173 }
174 if ( 0 === $option_legacy_tools['forms'] ) {
175 $option_legacy_tools['forms'][0] = false;
176 } else {
177 if ( false === $stored_legacy_tools ) {
178 $option_legacy_tools['forms'][0] = true;
179 }
180 }
181 if ( 0 === $option_legacy_tools['templates'] ) {
182 $option_legacy_tools['templates'][0] = false;
183 } else {
184 if ( false === $stored_legacy_tools ) {
185 $option_legacy_tools['templates'][0] = true;
186 }
187 }
188 if ( 0 === $option_legacy_tools['designer'] ) {
189 $option_legacy_tools['designer'][0] = false;
190 } else {
191 if ( false === $stored_legacy_tools ) {
192 $option_legacy_tools['designer'][0] = true;
193 }
194 }
195 if ( 0 === $option_legacy_tools['dashboards'] ) {
196 $option_legacy_tools['dashboards'][0] = false;
197 } else {
198 if ( false === $stored_legacy_tools ) {
199 $option_legacy_tools['dashboards'][0] = true;
200 }
201 }
202 if ( 0 === $option_legacy_tools['charts'] ) {
203 $option_legacy_tools['charts'][0] = false;
204 } else {
205 if ( false === $stored_legacy_tools ) {
206 $option_legacy_tools['charts'][0] = true;
207 }
208 }
209 // Save legacy tool settings.
210 WPDA::set_option( WPDA::OPTION_PLUGIN_LEGACY_TOOLS, $option_legacy_tools );
211 }
212
213 }
214