admin
2 years ago
core
2 years ago
class-activator.php
3 years ago
class-assets.php
2 years ago
class-builder-page.php
3 years ago
class-deactivator.php
3 years ago
class-debug-info.php
2 years ago
class-feed-ajax.php
2 years ago
class-feed-block.php
2 years ago
class-feed-deserializer.php
2 years ago
class-feed-old.php
4 years ago
class-feed-page.php
3 years ago
class-feed-serializer.php
3 years ago
class-feed-shortcode.php
3 years ago
class-feed-widget.php
4 years ago
class-plugin-overview-ajax.php
3 years ago
class-plugin-overview.php
3 years ago
class-plugin-settings.php
2 years ago
class-plugin-support.php
3 years ago
class-plugin.php
2 years ago
class-post-types.php
3 years ago
class-reviews-cron.php
2 years ago
class-settings-save.php
3 years ago
class-view.php
2 years ago
index.php
4 years ago
page-setting-fig.php
3 years ago
page-setting-support.php
4 years ago
class-activator.php
310 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WP_Rplg_Google_Reviews\Includes; |
| 4 | |
| 5 | use WP_Rplg_Google_Reviews\Includes\Core\Database; |
| 6 | |
| 7 | class Activator { |
| 8 | |
| 9 | private $database; |
| 10 | |
| 11 | public function __construct(Database $database) { |
| 12 | $this->database = $database; |
| 13 | } |
| 14 | |
| 15 | public function options() { |
| 16 | return array( |
| 17 | 'grw_version', |
| 18 | 'grw_active', |
| 19 | 'grw_google_api_key', |
| 20 | 'grw_language', |
| 21 | 'grw_activation_time', |
| 22 | 'grw_auth_code', |
| 23 | 'grw_debug_mode', |
| 24 | 'grw_feed_ids', |
| 25 | 'grw_do_activation', |
| 26 | 'grw_revupd_cron', |
| 27 | 'grw_revupd_cron_timeout', |
| 28 | 'grw_revupd_cron_log', |
| 29 | 'grw_debug_refresh', |
| 30 | 'grw_rev_notice_hide', |
| 31 | 'rplg_rev_notice_show', |
| 32 | 'grw_rate_us', |
| 33 | ); |
| 34 | } |
| 35 | |
| 36 | public function register() { |
| 37 | add_action('init', array($this, 'check_version')); |
| 38 | } |
| 39 | |
| 40 | public function check_version() { |
| 41 | if (version_compare(get_option('grw_version'), GRW_VERSION, '<')) { |
| 42 | $this->activate(); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Activates the plugin on a multisite |
| 48 | */ |
| 49 | public function activate() { |
| 50 | $network_wide = get_option('grw_is_multisite'); |
| 51 | if ($network_wide) { |
| 52 | $this->activate_multisite(); |
| 53 | } else { |
| 54 | $this->activate_single_site(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | private function activate_multisite() { |
| 59 | global $wpdb; |
| 60 | |
| 61 | $site_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
| 62 | |
| 63 | foreach($site_ids as $site_id) { |
| 64 | switch_to_blog($site_id); |
| 65 | $this->activate_single_site(); |
| 66 | restore_current_blog(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | private function activate_single_site() { |
| 71 | $current_version = GRW_VERSION; |
| 72 | $last_active_version = get_option('grw_version'); |
| 73 | |
| 74 | if (empty($last_active_version)) { |
| 75 | $this->first_install(); |
| 76 | update_option('grw_version', $current_version); |
| 77 | update_option('grw_auth_code', $this->random_str(127)); |
| 78 | update_option('grw_revupd_cron', '1'); |
| 79 | } elseif ($last_active_version !== $current_version) { |
| 80 | $this->exist_install($current_version, $last_active_version); |
| 81 | update_option('grw_version', $current_version); |
| 82 | update_option('grw_revupd_cron', '1'); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private function first_install() { |
| 87 | $this->database->create(); |
| 88 | |
| 89 | add_option('grw_active', '1'); |
| 90 | add_option('grw_google_api_key', ''); |
| 91 | } |
| 92 | |
| 93 | private function exist_install($current_version, $last_active_version) { |
| 94 | $this->update_db($last_active_version); |
| 95 | } |
| 96 | |
| 97 | public function update_db($last_active_version) { |
| 98 | global $wpdb; |
| 99 | |
| 100 | switch($last_active_version) { |
| 101 | |
| 102 | case version_compare($last_active_version, '1.8.2', '<'): |
| 103 | $wpdb->query("ALTER TABLE " . $wpdb->prefix . Database::BUSINESS_TABLE . " ADD review_count INTEGER"); |
| 104 | $place_ids = $wpdb->get_col("SELECT place_id FROM " . $wpdb->prefix . Database::BUSINESS_TABLE . " WHERE rating > 0 LIMIT 5"); |
| 105 | foreach($place_ids as $place_id) { |
| 106 | //TODO: grw_refresh_reviews(array($place_id)); |
| 107 | } |
| 108 | break; |
| 109 | |
| 110 | case version_compare($last_active_version, '1.8.7', '<'): |
| 111 | $row = $wpdb->get_results( |
| 112 | "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS " . |
| 113 | "WHERE table_name = '" . $wpdb->prefix . Database::REVIEW_TABLE . "' AND column_name = 'hide'" |
| 114 | ); |
| 115 | if(empty($row)){ |
| 116 | $wpdb->query("ALTER TABLE " . $wpdb->prefix . Database::REVIEW_TABLE . " ADD hide VARCHAR(1) DEFAULT '' NOT NULL"); |
| 117 | } |
| 118 | break; |
| 119 | |
| 120 | case version_compare($last_active_version, '2.0.1', '<'): |
| 121 | $grw_auth_code = get_option('grw_auth_code'); |
| 122 | if (!$grw_auth_code || strlen($grw_auth_code) == 0) { |
| 123 | update_option('grw_auth_code', $this->random_str(127)); |
| 124 | } |
| 125 | break; |
| 126 | |
| 127 | case version_compare($last_active_version, '2.1.5', '<'): |
| 128 | if (!function_exists('drop_index') || !function_exists('dbDelta')) { |
| 129 | require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 130 | } |
| 131 | if (!function_exists('maybe_drop_column')) { |
| 132 | // Define 'maybe_drop_column' function without including install-helper.php due to error: |
| 133 | // Fatal error: Cannot redeclare maybe_create_table() |
| 134 | // (previously declared in /wp-admin/install-helper.php:52) in /wp-admin/includes/upgrade.php on line 1616 |
| 135 | function maybe_drop_column($table_name, $column_name, $drop_ddl) { |
| 136 | global $wpdb; |
| 137 | foreach ($wpdb->get_col( "DESC $table_name", 0) as $column) { |
| 138 | if ($column === $column_name) { |
| 139 | $wpdb->query($drop_ddl); |
| 140 | foreach ($wpdb->get_col("DESC $table_name", 0) as $column) { |
| 141 | if ($column === $column_name) { |
| 142 | return false; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | return true; |
| 148 | } |
| 149 | } |
| 150 | if (drop_index($wpdb->prefix . Database::REVIEW_TABLE, 'grp_google_review_hash')) { |
| 151 | maybe_drop_column( |
| 152 | $wpdb->prefix . Database::REVIEW_TABLE, |
| 153 | "hash", |
| 154 | "ALTER TABLE " . $wpdb->prefix . Database::REVIEW_TABLE . " DROP COLUMN hash" |
| 155 | ); |
| 156 | } |
| 157 | $sql = "CREATE TABLE IF NOT EXISTS " . $wpdb->prefix . "grp_google_stats (". |
| 158 | "id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,". |
| 159 | "google_place_id BIGINT(20) UNSIGNED NOT NULL,". |
| 160 | "time INTEGER NOT NULL,". |
| 161 | "rating DOUBLE PRECISION,". |
| 162 | "review_count INTEGER,". |
| 163 | "PRIMARY KEY (`id`),". |
| 164 | "INDEX grp_google_place_id (`google_place_id`)". |
| 165 | ") " . $wpdb->get_charset_collate() . ";"; |
| 166 | dbDelta($sql); |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Creates the plugin database on a multisite |
| 173 | */ |
| 174 | public function create_db() { |
| 175 | $network_wide = get_option('grw_is_multisite'); |
| 176 | if ($network_wide) { |
| 177 | $this->create_db_multisite(); |
| 178 | } else { |
| 179 | $this->create_db_single_site(); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | private function create_db_multisite() { |
| 184 | global $wpdb; |
| 185 | |
| 186 | $site_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
| 187 | |
| 188 | foreach($site_ids as $site_id) { |
| 189 | switch_to_blog($site_id); |
| 190 | $this->create_db_single_site(); |
| 191 | restore_current_blog(); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | private function create_db_single_site() { |
| 196 | $this->database->create(); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Drops the plugin database on a multisite |
| 201 | */ |
| 202 | public function drop_db($multisite = false) { |
| 203 | $network_wide = get_option('grw_is_multisite'); |
| 204 | if ($multisite && $network_wide) { |
| 205 | $this->drop_db_multisite(); |
| 206 | } else { |
| 207 | $this->drop_db_single_site(); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | private function drop_db_multisite() { |
| 212 | global $wpdb; |
| 213 | |
| 214 | $site_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
| 215 | |
| 216 | foreach($site_ids as $site_id) { |
| 217 | switch_to_blog($site_id); |
| 218 | $this->drop_db_single_site(); |
| 219 | restore_current_blog(); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | private function drop_db_single_site() { |
| 224 | $this->database->drop(); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Delete all options of the plugin on a multisite |
| 229 | */ |
| 230 | public function delete_all_options($multisite = false) { |
| 231 | $network_wide = get_option('grw_is_multisite'); |
| 232 | if ($multisite && $network_wide) { |
| 233 | $this->delete_all_options_multisite(); |
| 234 | } else { |
| 235 | $this->delete_all_options_single_site(); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | private function delete_all_options_multisite() { |
| 240 | global $wpdb; |
| 241 | |
| 242 | $site_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
| 243 | |
| 244 | foreach($site_ids as $site_id) { |
| 245 | switch_to_blog($site_id); |
| 246 | $this->delete_all_options_single_site(); |
| 247 | restore_current_blog(); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | private function delete_all_options_single_site() { |
| 252 | foreach ($this->options() as $opt) { |
| 253 | delete_option($opt); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Delete all feeds of the plugin on a multisite |
| 259 | */ |
| 260 | public function delete_all_feeds($multisite = false) { |
| 261 | $network_wide = get_option('grw_is_multisite'); |
| 262 | if ($multisite && $network_wide) { |
| 263 | $this->delete_all_feeds_multisite(); |
| 264 | } else { |
| 265 | $this->delete_all_feeds_single_site(); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | private function delete_all_feeds_multisite() { |
| 270 | global $wpdb; |
| 271 | |
| 272 | $site_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
| 273 | |
| 274 | foreach($site_ids as $site_id) { |
| 275 | switch_to_blog($site_id); |
| 276 | $this->delete_all_feeds_single_site(); |
| 277 | restore_current_blog(); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | private function delete_all_feeds_single_site() { |
| 282 | $args = array( |
| 283 | 'post_type' => Post_Types::FEED_POST_TYPE, |
| 284 | 'post_status' => array('any', 'trash'), |
| 285 | 'posts_per_page' => -1, |
| 286 | 'fields' => 'ids', |
| 287 | ); |
| 288 | |
| 289 | $query = new \WP_Query($args); |
| 290 | $grw_posts = $query->posts; |
| 291 | |
| 292 | if (!empty($grw_posts)) { |
| 293 | foreach ($grw_posts as $grw_post) { |
| 294 | wp_delete_post($grw_post, true); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | private function random_str($len) { |
| 300 | $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 301 | $charlen = strlen($chars); |
| 302 | $randstr = ''; |
| 303 | for ($i = 0; $i < $len; $i++) { |
| 304 | $randstr .= $chars[rand(0, $charlen - 1)]; |
| 305 | } |
| 306 | return $randstr; |
| 307 | } |
| 308 | |
| 309 | } |
| 310 |