public static function old_update() { global $wpdb; $oldVersion = get_option(CMA::OPTION_VERSION); if (empty($oldVersion)) { return; } $currentVersion = CMA::version(); if (empty($oldVersion) or version_compare($oldVersion, $currentVersion, '<')) { update_option(CMA::OPTION_VERSION, $currentVersion); if (version_compare($oldVersion, '2.1.6', '<')) { // Update comment_type $commentsIds = $wpdb->get_col($wpdb->prepare("SELECT `{$wpdb->comments}`.`comment_ID`\n\t\t\t\t\tFROM `{$wpdb->comments}`\n\t\t\t\t\tLEFT JOIN `{$wpdb->posts}` ON `{$wpdb->comments}`.`comment_post_ID` = `{$wpdb->posts}`.`ID`\n\t\t\t\t\tWHERE `{$wpdb->posts}`.`post_type` = %s AND `{$wpdb->comments}`.`comment_type` = ''", CMA_Thread::POST_TYPE)); $data = array('comment_type' => CMA_Answer::COMMENT_TYPE); foreach ($commentsIds as $comment_ID) { $rval = $wpdb->update($wpdb->comments, $data, compact('comment_ID')); } } // Update new post meta: question+answers votes count if (version_compare($oldVersion, '2.1.7', '<')) { $posts = get_posts(array('post_type' => CMA_Thread::POST_TYPE)); foreach ($posts as $post) { $thread = CMA_Thread::getInstance($post->ID); update_post_meta($post->ID, CMA_Thread::$_meta['votes_question'], 0); $thread->updateRatingCache(); } } // Update users counters if (version_compare($oldVersion, '2.1.9', '<=')) { CMA_Thread::updateAllQA(); } if (version_compare($oldVersion, '2.1.13', '<=')) { // Create logs records for old posts $posts = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE post_type = %s", CMA_Thread::POST_TYPE)); $postsLog = new CMA_QuestionPostLog(); foreach ($posts as $post) { $count = $wpdb->get_var($wpdb->prepare('SELECT COUNT(*) FROM ' . $postsLog->getMetaTableName() . ' m JOIN ' . $postsLog->getTableName() . ' l ON l.id = m.log_id WHERE m.meta_name = %s AND m.meta_value = %s AND l.log_type = %s', CMA_QuestionPostLog::META_QUESTION_ID, (string) $post->ID, CMA_QuestionPostLog::LOG_TYPE)); if ($count == 0) { $postsLog->create(array(CMA_Log::FIELD_CREATED => $post->post_date, CMA_Log::FIELD_USER_ID => $post->post_author, CMA_Log::FIELD_IP_ADDR => NULL), array(CMA_QuestionPostLog::META_QUESTION_ID => $post->ID)); } } // Create logs records for old comments $answers = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->comments} WHERE comment_type = %s", CMA_Answer::COMMENT_TYPE)); $answersLog = new CMA_AnswerPostLog(); foreach ($answers as $answer) { $count = $wpdb->get_var($wpdb->prepare('SELECT COUNT(*) FROM ' . $answersLog->getMetaTableName() . ' m JOIN ' . $answersLog->getTableName() . ' l ON l.id = m.log_id WHERE m.meta_name = %s AND m.meta_value = %s AND l.log_type = %s', CMA_QuestionPostLog::META_QUESTION_ID, (string) $answer->comment_ID, CMA_AnswerPostLog::LOG_TYPE)); if ($count == 0) { $postsLog->create(array(CMA_Log::FIELD_CREATED => $answer->comment_date, CMA_Log::FIELD_USER_ID => $answer->user_id, CMA_Log::FIELD_IP_ADDR => NULL), array(CMA_AnswerPostLog::META_QUESTION_ID => $answer->comment_post_ID, CMA_AnswerPostLog::META_ANSWER_ID => $answer->comment_ID)); } } } if (version_compare($oldVersion, '2.2.2', '<=')) { // Votes for questions $metaKeys = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->postmeta} WHERE meta_key = %s", CMA_Thread::$_meta['usersRated'])); foreach ($metaKeys as $mk) { $value = @unserialize($mk->meta_value); if (is_array($value)) { $value = array_filter($value); foreach ($value as $userId) { add_post_meta($mk->post_id, CMA_Thread::$_meta['usersRated'], $userId, $uniq = false); } $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->postmeta} WHERE meta_id = %d", $mk->meta_id)); } } // Votes for answers $metaKeys = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->commentmeta} WHERE meta_key = %s", CMA_Answer::META_USERS_RATED)); foreach ($metaKeys as $mk) { $value = @unserialize($mk->meta_value); if (is_array($value)) { $value = array_filter($value); foreach ($value as $userId) { add_comment_meta($mk->comment_id, CMA_Answer::META_USERS_RATED, $userId, $uniq = false); } $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->commentmeta} WHERE meta_id = %d", $mk->meta_id)); } } } } }