public function clear_cache() { $sql = "TRUNCATE `{$this->wpdb->prefix}icl_string_pages`"; $this->wpdb->query($sql); $sql = "TRUNCATE `{$this->wpdb->prefix}icl_string_urls`"; $this->wpdb->query($sql); }
/** * Adapated from interconnect/it's search/replace script. * Modified to use WordPress wpdb functions instead of PHP's native mysql/pdo functions. * * @link https://interconnectit.com/products/search-and-replace-for-wordpress-databases/ * * @access public * @param string $table The table to run the replacement on. * @param string $search The string to replace. * @param string $replace The string to replace with. * @return array Collection of information gathered during the run. */ public function revisr_srdb($table, $search = '', $replace = '') { // Get a list of columns in this table. $columns = array(); $fields = $this->wpdb->get_results('DESCRIBE ' . $table); foreach ($fields as $column) { $columns[$column->Field] = $column->Key == 'PRI' ? true : false; } $this->wpdb->flush(); // Count the number of rows we have in the table if large we'll split into blocks, This is a mod from Simon Wheatley $this->wpdb->get_results('SELECT COUNT(*) FROM ' . $table); $row_count = $this->wpdb->num_rows; if ($row_count == 0) { continue; } $page_size = 50000; $pages = ceil($row_count / $page_size); for ($page = 0; $page < $pages; $page++) { $current_row = 0; $start = $page * $page_size; $end = $start + $page_size; // Grab the content of the table. $data = $this->wpdb->get_results("SELECT * FROM {$table} LIMIT {$start}, {$end}", ARRAY_A); // Loop through the data. foreach ($data as $row) { $current_row++; $update_sql = array(); $where_sql = array(); $upd = false; foreach ($columns as $column => $primary_key) { $edited_data = $data_to_fix = $row[$column]; // Run a search replace on the data that'll respect the serialisation. $edited_data = $this->recursive_unserialize_replace($search, $replace, $data_to_fix); // Something was changed if ($edited_data != $data_to_fix) { $update_sql[] = $column . ' = "' . $this->mysql_escape_mimic($edited_data) . '"'; $upd = true; } if ($primary_key) { $where_sql[] = $column . ' = "' . $this->mysql_escape_mimic($data_to_fix) . '"'; } } if ($upd && !empty($where_sql)) { $sql = 'UPDATE ' . $table . ' SET ' . implode(', ', $update_sql) . ' WHERE ' . implode(' AND ', array_filter($where_sql)); $result = $this->wpdb->query($sql); if (!$result) { $error_msg = sprintf(__('Error updating the table: %s.', 'revisr'), $table); } } elseif ($upd) { $error_msg = sprintf(__('The table "%s" has no primary key. Manual change needed on row %s.', 'revisr'), $table, $current_row); } } } $this->wpdb->flush(); if (isset($error_msg)) { Revisr_Admin::log($error_msg, 'error'); return false; } }
/** * @param $ids Array or string of log ID's to delete * * @return false|int */ public function delete_by_id($ids) { if (is_array($ids)) { // create comma-separated string $ids = implode(',', $ids); } // escape string for usage in IN clause $ids = esc_sql($ids); $sql = sprintf("DELETE FROM `%s` WHERE ID IN (%s)", $this->table_name, $ids); return $this->db->query($sql); }
function reset_pro_translation_configuration() { $translation_service_name = $this->TranslationProxy->get_current_service_name(); $this->sitepress->set_setting('content_translation_languages_setup', false); $this->sitepress->set_setting('content_translation_setup_complete', false); $this->sitepress->set_setting('content_translation_setup_wizard_step', false); $this->sitepress->set_setting('translator_choice', false); $this->sitepress->set_setting('icl_lang_status', false); $this->sitepress->set_setting('icl_balance', false); $this->sitepress->set_setting('icl_support_ticket_id', false); $this->sitepress->set_setting('icl_current_session', false); $this->sitepress->set_setting('last_get_translator_status_call', false); $this->sitepress->set_setting('last_icl_reminder_fetch', false); $this->sitepress->set_setting('icl_account_email', false); $this->sitepress->set_setting('translators_management_info', false); $this->sitepress->set_setting('site_id', false); $this->sitepress->set_setting('access_key', false); $this->sitepress->set_setting('ts_site_id', false); $this->sitepress->set_setting('ts_access_key', false); if (class_exists('TranslationProxy_Basket')) { //Cleaning the basket TranslationProxy_Basket::delete_all_items_from_basket(); } $sql_for_remote_rids = $this->wpdb->prepare("FROM {$this->wpdb->prefix}icl_translation_status\n\t\t\t\t\t\t\t\t \t\t\t\tWHERE translation_service != 'local'\n\t\t\t\t\t\t\t\t \t\t\t\t\tAND translation_service != 0\n\t\t\t\t\t\t\t\t\t\t\t\t\tAND status IN ( %d, %d )", ICL_TM_WAITING_FOR_TRANSLATOR, ICL_TM_IN_PROGRESS); //Delete all translation service jobs with status "waiting for translator" or "in progress" $this->wpdb->query("DELETE FROM {$this->wpdb->prefix}icl_translate_job WHERE rid IN (SELECT rid {$sql_for_remote_rids})"); //Delete all translation statuses with status "waiting for translator" or "in progress" $this->wpdb->query("DELETE {$sql_for_remote_rids}"); //Cleaning up Translation Proxy settings $this->sitepress->set_setting('icl_html_status', false); $this->sitepress->set_setting('language_pairs', false); if (!$this->TranslationProxy->has_preferred_translation_service()) { $this->sitepress->set_setting('translation_service', false); $this->sitepress->set_setting('icl_translation_projects', false); } $this->sitepress->save_settings(); $this->wpdb->query("TRUNCATE TABLE {$this->wpdb->prefix}icl_core_status"); $this->wpdb->query("TRUNCATE TABLE {$this->wpdb->prefix}icl_content_status"); $this->wpdb->query("TRUNCATE TABLE {$this->wpdb->prefix}icl_string_status"); $this->wpdb->query("TRUNCATE TABLE {$this->wpdb->prefix}icl_node"); $this->wpdb->query("TRUNCATE TABLE {$this->wpdb->prefix}icl_reminders"); if ($this->TranslationProxy->has_preferred_translation_service() && $translation_service_name) { $confirm_message = 'The translation process with %1$s was reset.'; } elseif ($translation_service_name) { $confirm_message = 'Your site was successfully disconnected from %1$s. Go to the translators tab to connect a new %1$s account or use a different translation service.'; } else { $confirm_message = 'PRO translation has been reset.'; } $response = sprintf(__($confirm_message, 'wpml-translation-management'), $translation_service_name); return $response; }
/** * Adapated from interconnect/it's search/replace script. * * Modified to use WordPress wpdb functions instead of PHP's native mysql/pdo functions, * and to be compatible with batch processing via AJAX. * * @link https://interconnectit.com/products/search-and-replace-for-wordpress-databases/ * * @access public * @param string $table The table to run the replacement on. * @param int $page The page/block to begin the query on. * @param array $args An associative array containing arguements for this run. * @return array */ public function srdb($table, $page, $args) { // Load up the default settings for this chunk. $table = esc_sql($table); $current_page = absint($page); $pages = $this->get_pages_in_table($table); $done = false; $args['search_for'] = str_replace('#BSR_BACKSLASH#', '\\', $args['search_for']); $args['replace_with'] = str_replace('#BSR_BACKSLASH#', '\\', $args['replace_with']); $table_report = array('change' => 0, 'updates' => 0, 'start' => microtime(true), 'end' => microtime(true), 'errors' => array(), 'skipped' => false); // Get a list of columns in this table. list($primary_key, $columns) = $this->get_columns($table); // Bail out early if there isn't a primary key. if (null === $primary_key) { $table_report['skipped'] = true; return array(true, $table_report); } $current_row = 0; $start = $page * $this->page_size; $end = $this->page_size; // Grab the content of the table. $data = $this->wpdb->get_results("SELECT * FROM {$table} LIMIT {$start}, {$end}", ARRAY_A); // Loop through the data. foreach ($data as $row) { $current_row++; $update_sql = array(); $where_sql = array(); $upd = false; foreach ($columns as $column) { $data_to_fix = $row[$column]; if ($column == $primary_key) { $where_sql[] = $column . ' = "' . $this->mysql_escape_mimic($data_to_fix) . '"'; continue; } // Skip GUIDs by default. if ('on' !== $args['replace_guids'] && 'guid' == $column) { continue; } if ($this->wpdb->options === $table) { // Skip any BSR options as they may contain the search field. if (isset($should_skip) && true === $should_skip) { $should_skip = false; continue; } // If the Site URL needs to be updated, let's do that last. if (isset($update_later) && true === $update_later) { $update_later = false; $edited_data = $this->recursive_unserialize_replace($args['search_for'], $args['replace_with'], $data_to_fix, false, $args['case_insensitive']); if ($edited_data != $data_to_fix) { $table_report['change']++; $table_report['updates']++; update_option('bsr_update_site_url', $edited_data); continue; } } if ('_transient_bsr_results' === $data_to_fix || 'bsr_profiles' === $data_to_fix || 'bsr_update_site_url' === $data_to_fix) { $should_skip = true; } if ('siteurl' === $data_to_fix && $args['dry_run'] !== 'on') { $update_later = true; } } // Run a search replace on the data that'll respect the serialisation. $edited_data = $this->recursive_unserialize_replace($args['search_for'], $args['replace_with'], $data_to_fix, false, $args['case_insensitive']); // Something was changed if ($edited_data != $data_to_fix) { $update_sql[] = $column . ' = "' . $this->mysql_escape_mimic($edited_data) . '"'; $upd = true; $table_report['change']++; } } // Determine what to do with updates. if ($args['dry_run'] === 'on') { // Don't do anything if a dry run } elseif ($upd && !empty($where_sql)) { // If there are changes to make, run the query. $sql = 'UPDATE ' . $table . ' SET ' . implode(', ', $update_sql) . ' WHERE ' . implode(' AND ', array_filter($where_sql)); $result = $this->wpdb->query($sql); if (!$result) { $table_report['errors'][] = sprintf(__('Error updating row: %d.', 'better-search-replace'), $current_row); } else { $table_report['updates']++; } } } // end row loop if ($current_page >= $pages - 1) { $done = true; } // Flush the results and return the report. $table_report['end'] = microtime(true); $this->wpdb->flush(); return array('table_complete' => $done, 'table_report' => $table_report); }
/** * Adapated from interconnect/it's search/replace script. * Modified to use WordPress wpdb functions instead of PHP's native mysql/pdo functions. * * @link https://interconnectit.com/products/search-and-replace-for-wordpress-databases/ * * @access public * @param string $table The table to run the replacement on. * @param string $search The string to replace. * @param string $replace The string to replace with. * @param boolean $replace_guids Whether to skip the GUID column * @param boolean $dry_run Whether to run as a dry run * @param boolean $case_insensitive If we should ignore case. * @return array */ public function srdb($table, $search = '', $replace = '', $replace_guids, $dry_run, $case_insensitive) { $table = esc_sql($table); $table_report = array('change' => 0, 'updates' => 0, 'start' => microtime(true), 'end' => microtime(true), 'errors' => array()); // Get a list of columns in this table. $columns = array(); $fields = $this->wpdb->get_results('DESCRIBE ' . $table); foreach ($fields as $column) { $columns[$column->Field] = $column->Key == 'PRI' ? true : false; } $this->wpdb->flush(); // Count the number of rows we have in the table if large we'll split into blocks, This is a mod from Simon Wheatley $row_count = $this->wpdb->get_var("SELECT COUNT(*) FROM {$table}"); if ($row_count == 0) { return $table_report; } // Default to a lower page size for shared hosting/large DB's. $page_size = get_option('bsr_page_size') ? get_option('bsr_page_size') : 20000; $pages = ceil($row_count / $page_size); for ($page = 0; $page < $pages; $page++) { $current_row = 0; $start = $page * $page_size; $end = $page_size; // Grab the content of the table. $data = $this->wpdb->get_results("SELECT * FROM {$table} LIMIT {$start}, {$end}", ARRAY_A); // Loop through the data. foreach ($data as $row) { $current_row++; $update_sql = array(); $where_sql = array(); $upd = false; foreach ($columns as $column => $primary_key) { $data_to_fix = $row[$column]; // Skip GUIDs by default. if (true !== $replace_guids && 'guid' == $column) { continue; } // Run a search replace on the data that'll respect the serialisation. $edited_data = $this->recursive_unserialize_replace($search, $replace, $data_to_fix, false, $case_insensitive); // Something was changed if ($edited_data != $data_to_fix) { $update_sql[] = $column . ' = "' . $this->mysql_escape_mimic($edited_data) . '"'; $upd = true; $this->report['change']++; $table_report['change']++; } if ($primary_key) { $where_sql[] = $column . ' = "' . $this->mysql_escape_mimic($data_to_fix) . '"'; } } // Determine what to do with updates. if ($dry_run === true) { // Don't do anything if a dry run } elseif ($upd && !empty($where_sql)) { // If there are changes to make, run the query. $sql = 'UPDATE ' . $table . ' SET ' . implode(', ', $update_sql) . ' WHERE ' . implode(' AND ', array_filter($where_sql)); $result = $this->wpdb->query($sql); if (!$result) { $this->report['errors']++; $table_report['errors'][] = sprintf(__('Error updating row: %d.', 'better-search-replace'), $current_row); } else { $this->report['updates']++; $table_report['updates']++; } } elseif ($upd) { $this->report['errors']++; $table_report['errors'][] = sprintf(__('Row %d has no primary key, manual change needed.', 'better-search-replace'), $current_row); } } } // Flush the results and return the report. $table_report['end'] = microtime(true); $this->wpdb->flush(); return $table_report; }