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));
                 }
             }
         }
     }
 }
Example #2
0
 /**
  * Get plugin's version.
  *
  * @return string|null
  */
 public static function version()
 {
     if (!empty(self::$version)) {
         return self::$version;
     } else {
         $readme = file_get_contents(CMA_PATH . '/readme.txt');
         preg_match('/Stable tag\\: ([0-9\\.]+)/i', $readme, $match);
         if (isset($match[1])) {
             self::$version = $match[1];
             return $match[1];
         }
     }
 }
 public static function loadScripts()
 {
     wp_register_style('CMA-css', CMA_URL . '/views/resources/app.css', array(), CMA::version());
     add_action('wp_head', array(__CLASS__, 'high_priority_style'), PHP_INT_MAX);
     add_action('wp_footer', array(__CLASS__, 'high_priority_style'), PHP_INT_MAX);
     wp_enqueue_style('dashicons');
     $cmaVariables = array('CMA_URL' => get_post_type_archive_link(CMA_Thread::POST_TYPE), 'ajaxUrl' => admin_url('admin-ajax.php'), 'loaderBarUrl' => CMA_URL . '/views/resources/imgs/ajax-loader-bar.gif', 'loaderUrl' => CMA_URL . '/views/resources/imgs/ajax-loader.gif', 'navBarAutoSubmit' => CMA_Settings::getOption(CMA_Settings::OPTION_NAVBAR_AUTO_SUBMIT), 'bestAnswerRemoveOther' => CMA_Settings::getOption(CMA_Settings::OPTION_BEST_ANSWER_REMOVE_OTHER), 'bestAnswerRemoveOtherLabel' => CMA_Labels::getLocalized('best_answer_remove_other_confirm'), 'confirmThreadDelete' => CMA::__('Do you really want to delete this question?'));
     wp_enqueue_script('cma-script', CMA_RESOURCE_URL . 'script.js', array('jquery', 'cma-toast', 'jquery-ui-dialog', 'suggest'), false, true);
     wp_localize_script('cma-script', 'CMA_Variables', $cmaVariables);
     wp_enqueue_script('jquery');
     wp_enqueue_script('cma-toast', CMA_RESOURCE_URL . 'toast/js/jquery.toastmessage.js', array('jquery'), false, true);
     wp_enqueue_style('cma-toast-css', CMA_RESOURCE_URL . 'toast/resources/css/jquery.toastmessage.css', array(), false);
     wp_enqueue_script('jquery-ui-dialog');
     wp_enqueue_style("wp-jquery-ui-dialog");
     wp_enqueue_script('suggest');
 }