admin
10 months ago
core
10 months ago
class-activator.php
10 months ago
class-assets.php
11 months ago
class-builder-page.php
1 year ago
class-deactivator.php
3 years ago
class-debug-info.php
2 years ago
class-feed-ajax.php
1 year ago
class-feed-block.php
1 year ago
class-feed-deserializer.php
11 months ago
class-feed-old.php
4 years ago
class-feed-page.php
11 months ago
class-feed-serializer.php
2 years ago
class-feed-shortcode.php
2 years ago
class-feed-widget.php
4 years ago
class-plugin-overview-ajax.php
1 year ago
class-plugin-overview.php
11 months ago
class-plugin-settings.php
10 months ago
class-plugin-support.php
1 year ago
class-plugin.php
11 months ago
class-post-types.php
3 years ago
class-reviews-cron.php
1 year ago
class-settings-save.php
1 year ago
class-view.php
10 months ago
index.php
4 years ago
page-setting-advance.php
1 year ago
page-setting-fig.php
1 year ago
page-setting-support.php
4 years ago
class-activator.php
373 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_async_css', |
| 20 | 'grw_freq_revs_upd', |
| 21 | 'grw_google_api_key', |
| 22 | 'grw_gpa_old', |
| 23 | 'grw_language', |
| 24 | 'grw_activation_time', |
| 25 | 'grw_auth_code', |
| 26 | 'grw_debug_mode', |
| 27 | 'grw_feed_ids', |
| 28 | 'grw_do_activation', |
| 29 | 'grw_demand_assets', |
| 30 | 'grw_revupd_cron', |
| 31 | 'grw_revupd_cron_timeout', |
| 32 | 'grw_revupd_cron_log', |
| 33 | 'grw_save_log', |
| 34 | 'grw_debug_refresh', |
| 35 | 'grw_notice_msg', |
| 36 | 'grw_notice_type', |
| 37 | 'grw_rev_notice_hide', |
| 38 | 'grw_last_error', |
| 39 | 'rplg_rev_notice_show', |
| 40 | 'grw_rate_us', |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | public function register() { |
| 45 | add_action('init', array($this, 'init')); |
| 46 | add_filter('https_ssl_verify', '__return_false'); |
| 47 | add_filter('block_local_requests', '__return_false'); |
| 48 | } |
| 49 | |
| 50 | public function init() { |
| 51 | $this->check_version(); |
| 52 | } |
| 53 | |
| 54 | public function check_version() { |
| 55 | if (version_compare(get_option('grw_version'), GRW_VERSION, '<')) { |
| 56 | $this->activate(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Activates the plugin on a multisite |
| 62 | */ |
| 63 | public function activate() { |
| 64 | $network_wide = get_option('grw_is_multisite'); |
| 65 | if ($network_wide) { |
| 66 | $this->activate_multisite(); |
| 67 | } else { |
| 68 | $this->activate_single_site(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | private function activate_multisite() { |
| 73 | global $wpdb; |
| 74 | |
| 75 | $site_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
| 76 | |
| 77 | foreach($site_ids as $site_id) { |
| 78 | switch_to_blog($site_id); |
| 79 | $this->activate_single_site(); |
| 80 | restore_current_blog(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | private function activate_single_site() { |
| 85 | $current_version = GRW_VERSION; |
| 86 | $last_active_version = get_option('grw_version'); |
| 87 | |
| 88 | if (empty($last_active_version)) { |
| 89 | $this->first_install(); |
| 90 | update_option('grw_version', $current_version); |
| 91 | update_option('grw_auth_code', $this->random_str(127)); |
| 92 | update_option('grw_revupd_cron', '1'); |
| 93 | } elseif ($last_active_version !== $current_version) { |
| 94 | $this->exist_install($current_version, $last_active_version); |
| 95 | update_option('grw_version', $current_version); |
| 96 | update_option('grw_revupd_cron', '1'); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | private function first_install() { |
| 101 | $this->database->create(); |
| 102 | |
| 103 | add_option('grw_active', '1'); |
| 104 | add_option('grw_google_api_key', ''); |
| 105 | } |
| 106 | |
| 107 | private function exist_install($current_version, $last_active_version) { |
| 108 | $this->update_db($last_active_version); |
| 109 | } |
| 110 | |
| 111 | public function update_db($last_active_version) { |
| 112 | global $wpdb; |
| 113 | |
| 114 | switch($last_active_version) { |
| 115 | |
| 116 | case version_compare($last_active_version, '1.8.2', '<'): |
| 117 | $wpdb->query("ALTER TABLE " . $wpdb->prefix . Database::BUSINESS_TABLE . " ADD review_count INTEGER"); |
| 118 | |
| 119 | case version_compare($last_active_version, '1.8.7', '<'): |
| 120 | $row = $wpdb->get_results( |
| 121 | "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS " . |
| 122 | "WHERE table_name = '" . $wpdb->prefix . Database::REVIEW_TABLE . "' AND column_name = 'hide'" |
| 123 | ); |
| 124 | if(empty($row)){ |
| 125 | $wpdb->query("ALTER TABLE " . $wpdb->prefix . Database::REVIEW_TABLE . " ADD hide VARCHAR(1) DEFAULT '' NOT NULL"); |
| 126 | } |
| 127 | |
| 128 | case version_compare($last_active_version, '2.0.1', '<'): |
| 129 | $grw_auth_code = get_option('grw_auth_code'); |
| 130 | if (!$grw_auth_code || strlen($grw_auth_code) == 0) { |
| 131 | update_option('grw_auth_code', $this->random_str(127)); |
| 132 | } |
| 133 | |
| 134 | case version_compare($last_active_version, '2.1.5', '<'): |
| 135 | if (!function_exists('drop_index') || !function_exists('dbDelta')) { |
| 136 | require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 137 | } |
| 138 | if (!function_exists('maybe_drop_column')) { |
| 139 | // Define 'maybe_drop_column' function without including install-helper.php due to error: |
| 140 | // Fatal error: Cannot redeclare maybe_create_table() |
| 141 | // (previously declared in /wp-admin/install-helper.php:52) in /wp-admin/includes/upgrade.php on line 1616 |
| 142 | function maybe_drop_column($table_name, $column_name, $drop_ddl) { |
| 143 | global $wpdb; |
| 144 | foreach ($wpdb->get_col( "DESC $table_name", 0) as $column) { |
| 145 | if ($column === $column_name) { |
| 146 | $wpdb->query($drop_ddl); |
| 147 | foreach ($wpdb->get_col("DESC $table_name", 0) as $column) { |
| 148 | if ($column === $column_name) { |
| 149 | return false; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | return true; |
| 155 | } |
| 156 | } |
| 157 | if (drop_index($wpdb->prefix . Database::REVIEW_TABLE, 'grp_google_review_hash')) { |
| 158 | maybe_drop_column( |
| 159 | $wpdb->prefix . Database::REVIEW_TABLE, |
| 160 | "hash", |
| 161 | "ALTER TABLE " . $wpdb->prefix . Database::REVIEW_TABLE . " DROP COLUMN hash" |
| 162 | ); |
| 163 | } |
| 164 | $sql = "CREATE TABLE IF NOT EXISTS " . $wpdb->prefix . "grp_google_stats (". |
| 165 | "id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,". |
| 166 | "google_place_id BIGINT(20) UNSIGNED NOT NULL,". |
| 167 | "time INTEGER NOT NULL,". |
| 168 | "rating DOUBLE PRECISION,". |
| 169 | "review_count INTEGER,". |
| 170 | "PRIMARY KEY (`id`),". |
| 171 | "INDEX grp_google_place_id (`google_place_id`)". |
| 172 | ") " . $wpdb->get_charset_collate() . ";"; |
| 173 | dbDelta($sql); |
| 174 | |
| 175 | //case version_compare($last_active_version, '4.2', '<'): |
| 176 | //$this->delete_duplicates(); |
| 177 | //$wpdb->query("ALTER TABLE `" . $wpdb->prefix . Database::REVIEW_TABLE . "` ADD UNIQUE `grp_author_url_lang` (`author_url`, `language`)"); |
| 178 | |
| 179 | case version_compare($last_active_version, '4.8.1', '<'): |
| 180 | $wpdb->query("ALTER TABLE `" . $wpdb->prefix . Database::REVIEW_TABLE . "` MODIFY COLUMN `author_url` VARCHAR(127)"); |
| 181 | |
| 182 | case version_compare($last_active_version, '5.8', '<'): |
| 183 | $wpdb->query("ALTER TABLE " . $wpdb->prefix . Database::REVIEW_TABLE . " ADD images TEXT"); |
| 184 | $wpdb->query("ALTER TABLE " . $wpdb->prefix . Database::REVIEW_TABLE . " ADD reply TEXT"); |
| 185 | $wpdb->query("ALTER TABLE " . $wpdb->prefix . Database::REVIEW_TABLE . " ADD reply_time INTEGER"); |
| 186 | |
| 187 | case version_compare($last_active_version, '6.2', '<'): |
| 188 | $wpdb->query("ALTER TABLE " . $wpdb->prefix . Database::BUSINESS_TABLE . " ADD map_url VARCHAR(512)"); |
| 189 | |
| 190 | case version_compare($last_active_version, '6.3', '<'): |
| 191 | $wpdb->query("ALTER TABLE " . $wpdb->prefix . Database::REVIEW_TABLE . " ADD url VARCHAR(255)"); |
| 192 | $wpdb->query("ALTER TABLE " . $wpdb->prefix . Database::REVIEW_TABLE . " ADD provider VARCHAR(32)"); |
| 193 | } |
| 194 | |
| 195 | if (!empty($wpdb->last_error)) { |
| 196 | update_option('grw_last_error', time() . ': ' . $wpdb->last_error); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Creates the plugin database on a multisite |
| 202 | */ |
| 203 | public function create_db() { |
| 204 | $network_wide = get_option('grw_is_multisite'); |
| 205 | if ($network_wide) { |
| 206 | $this->create_db_multisite(); |
| 207 | } else { |
| 208 | $this->create_db_single_site(); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | private function create_db_multisite() { |
| 213 | global $wpdb; |
| 214 | |
| 215 | $site_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
| 216 | |
| 217 | foreach($site_ids as $site_id) { |
| 218 | switch_to_blog($site_id); |
| 219 | $this->create_db_single_site(); |
| 220 | restore_current_blog(); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | private function create_db_single_site() { |
| 225 | $this->database->create(); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Drops the plugin database on a multisite |
| 230 | */ |
| 231 | public function drop_db($multisite = false) { |
| 232 | $network_wide = get_option('grw_is_multisite'); |
| 233 | if ($multisite && $network_wide) { |
| 234 | $this->drop_db_multisite(); |
| 235 | } else { |
| 236 | $this->drop_db_single_site(); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | private function drop_db_multisite() { |
| 241 | global $wpdb; |
| 242 | |
| 243 | $site_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
| 244 | |
| 245 | foreach($site_ids as $site_id) { |
| 246 | switch_to_blog($site_id); |
| 247 | $this->drop_db_single_site(); |
| 248 | restore_current_blog(); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | private function drop_db_single_site() { |
| 253 | $this->database->drop(); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Delete all options of the plugin on a multisite |
| 258 | */ |
| 259 | public function delete_all_options($multisite = false) { |
| 260 | $network_wide = get_option('grw_is_multisite'); |
| 261 | if ($multisite && $network_wide) { |
| 262 | $this->delete_all_options_multisite(); |
| 263 | } else { |
| 264 | $this->delete_all_options_single_site(); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | private function delete_all_options_multisite() { |
| 269 | global $wpdb; |
| 270 | |
| 271 | $site_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
| 272 | |
| 273 | foreach($site_ids as $site_id) { |
| 274 | switch_to_blog($site_id); |
| 275 | $this->delete_all_options_single_site(); |
| 276 | restore_current_blog(); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | private function delete_all_options_single_site() { |
| 281 | foreach ($this->options() as $opt) { |
| 282 | delete_option($opt); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Delete all feeds of the plugin on a multisite |
| 288 | */ |
| 289 | public function delete_all_feeds($multisite = false) { |
| 290 | $network_wide = get_option('grw_is_multisite'); |
| 291 | if ($multisite && $network_wide) { |
| 292 | $this->delete_all_feeds_multisite(); |
| 293 | } else { |
| 294 | $this->delete_all_feeds_single_site(); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | private function delete_all_feeds_multisite() { |
| 299 | global $wpdb; |
| 300 | |
| 301 | $site_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
| 302 | |
| 303 | foreach($site_ids as $site_id) { |
| 304 | switch_to_blog($site_id); |
| 305 | $this->delete_all_feeds_single_site(); |
| 306 | restore_current_blog(); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | private function delete_all_feeds_single_site() { |
| 311 | $args = array( |
| 312 | 'post_type' => Post_Types::FEED_POST_TYPE, |
| 313 | 'post_status' => array('any', 'trash'), |
| 314 | 'posts_per_page' => -1, |
| 315 | 'fields' => 'ids', |
| 316 | ); |
| 317 | |
| 318 | $query = new \WP_Query($args); |
| 319 | $grw_posts = $query->posts; |
| 320 | |
| 321 | if (!empty($grw_posts)) { |
| 322 | foreach ($grw_posts as $grw_post) { |
| 323 | wp_delete_post($grw_post, true); |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | public function delete_duplicates() { |
| 329 | global $wpdb; |
| 330 | |
| 331 | $last_error = array(); |
| 332 | |
| 333 | // Delete duplicte reviews |
| 334 | $wpdb->query($wpdb->prepare( |
| 335 | "DELETE `" . $wpdb->prefix . Database::REVIEW_TABLE . "` " . |
| 336 | "FROM `" . $wpdb->prefix . Database::REVIEW_TABLE . "` INNER JOIN (" . |
| 337 | "SELECT MIN(id) AS last_id, author_url, language FROM `" . $wpdb->prefix . Database::REVIEW_TABLE . "` " . |
| 338 | "WHERE author_url IN (" . |
| 339 | "SELECT author_url FROM `" . $wpdb->prefix . Database::REVIEW_TABLE . "` " . |
| 340 | "GROUP BY author_url, language HAVING COUNT(*) > 1" . |
| 341 | ") GROUP BY author_url, language" . |
| 342 | ") DUPLIC ON DUPLIC.author_url = `" . $wpdb->prefix . Database::REVIEW_TABLE . "`.author_url " . |
| 343 | "AND DUPLIC.language = `" . $wpdb->prefix . Database::REVIEW_TABLE . "`.language " . |
| 344 | "WHERE `" . $wpdb->prefix . Database::REVIEW_TABLE . "`.ID > DUPLIC.last_id;")); |
| 345 | |
| 346 | if (!empty($wpdb->last_error)) { |
| 347 | array_push($last_error, $wpdb->last_error); |
| 348 | } |
| 349 | |
| 350 | // Add unique index for author_url and lang |
| 351 | $wpdb->query("ALTER TABLE `" . $wpdb->prefix . Database::REVIEW_TABLE . "` ADD UNIQUE `grp_author_url_lang` (`author_url`, `language`)"); |
| 352 | |
| 353 | if (!empty($wpdb->last_error)) { |
| 354 | array_push($last_error, $wpdb->last_error); |
| 355 | } |
| 356 | |
| 357 | if (count($last_error) > 0) { |
| 358 | update_option('grw_last_error', time() . ': ' . implode("; ", $last_error)); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | private function random_str($len) { |
| 363 | $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 364 | $charlen = strlen($chars); |
| 365 | $randstr = ''; |
| 366 | for ($i = 0; $i < $len; $i++) { |
| 367 | $randstr .= $chars[rand(0, $charlen - 1)]; |
| 368 | } |
| 369 | return $randstr; |
| 370 | } |
| 371 | |
| 372 | } |
| 373 |