Plugin Directory

Changeset 3437367


Ignore:
Timestamp:
01/12/2026 05:36:27 AM (3 months ago)
Author:
senlin
Message:

Tested up to: 6.9

Location:
multilingual-comments-wpml
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • multilingual-comments-wpml/tags/1.2.1/multilingual-comments-wpml.php

    r3090993 r3437367  
    1010 *
    1111 * Requires at least:   4.9
    12  * Tested up to:        6.5
     12 * Tested up to:        6.9
    1313
    1414 * License: GPL-3.0+
     
    3939    {
    4040        register_activation_hook(__FILE__, [$this, 'activate']);
    41         add_filter('comments_array', [$this, 'merge_comments'], 100, 2);
     41        // Remove the later-stage comments_array filter
     42        // add_filter('comments_array', [$this, 'merge_comments'], 100, 2);
     43
     44        // Add earlier-stage filter for comment queries
     45        add_filter('comments_clauses', [$this, 'modify_comments_query'], 5, 2);
    4246        add_filter('get_comments_number', [$this, 'merge_comment_count'], 100, 2);
    4347    }
     
    6771    }
    6872
    69     private function sort_merged_comments($a, $b)
     73    public function modify_comments_query($clauses, $query)
    7074    {
    71         return $a->comment_ID - $b->comment_ID;
     75        global $wpdb, $sitepress;
     76
     77        // Ensure we are only modifying queries for a specific post (avoid affecting global queries)
     78        if (empty($query->query_vars['post_id'])) {
     79            return $clauses;
     80        }
     81
     82        $post_ID = $query->query_vars['post_id'];
     83        $post = get_post($post_ID);
     84
     85        // If the post does not exist, return the unmodified query
     86        if (!$post) {
     87            return $clauses;
     88        }
     89
     90        // Temporarily remove WPML's built-in comment filtering to prevent conflicts
     91        remove_filter('comments_clauses', array($sitepress, 'comments_clauses'));
     92
     93        // Get a list of all active languages
     94        $languages = apply_filters('wpml_active_languages', null, 'skip_missing=1');
     95        $post_ids = [$post_ID]; // Start with the current post ID
     96
     97        // Loop through available languages and get translated post IDs
     98        foreach ($languages as $code => $l) {
     99            if (!$l['active']) { // Skip the current active language
     100                $translated_id = apply_filters('wpml_object_id', $post_ID, $post->post_type, false, $l['language_code']);
     101                if ($translated_id) {
     102                    $post_ids[] = $translated_id; // Add translated post ID to the array
     103                }
     104            }
     105        }
     106
     107        // Ensure all post IDs are unique and properly formatted for SQL queries
     108        $post_ids = array_map('intval', array_unique($post_ids));
     109
     110        // Modify the WHERE clause to include all translations in the comment query
     111        $clauses['where'] = preg_replace(
     112            "/comment_post_ID = \d+/", // Look for the default `comment_post_ID = X` condition
     113            "comment_post_ID IN (" . implode(',', $post_ids) . ")", // Replace it with multiple post IDs
     114            $clauses['where']
     115        );
     116
     117        /**
     118         * Fix Pagination Issue:
     119         * - WordPress paginates comments **before** applying the `comments_clauses` filter.
     120         * - Since we are now fetching more comments, we need to adjust the LIMIT clause manually.
     121         * - `number` represents the number of comments per page.
     122         * - `offset` ensures pagination remains consistent.
     123         */
     124        if (!empty($query->query_vars['number'])) {
     125            $clauses['limits'] = "LIMIT " . (int) $query->query_vars['number'] . " OFFSET " . (int) $query->query_vars['offset'];
     126        }
     127
     128        // Re-add WPML's comment filtering after modifying the query
     129        add_filter('comments_clauses', array($sitepress, 'comments_clauses'), 10, 2);
     130
     131        return $clauses;
    72132    }
    73133
    74     public function merge_comments($comments, $post_ID)
    75     {
    76         global $sitepress;
    77 
    78         remove_filter('comments_clauses', array($sitepress, 'comments_clauses'));
    79 
    80         $languages = apply_filters('wpml_active_languages', null, 'skip_missing=1');
    81 
    82         $post = get_post($post_ID);
    83         $type = $post->post_type;
    84 
    85         foreach ($languages as $code => $l) {
    86             if (!$l['active']) {
    87                 $otherID = apply_filters('wpml_object_id', $post_ID, $type, false, $l['language_code']);
    88                 // add condition to prevent $otherID returning `null`
    89                 if ($otherID) {
    90                     $othercomments = get_comments(array('post_id' => $otherID, 'status' => 'approve', 'order' => 'ASC'));
    91                     $comments = array_merge($comments, $othercomments);
    92                 }
    93             }
    94         }
    95 
    96         if ($languages) {
    97             usort($comments, [$this, 'sort_merged_comments']);
    98         }
    99 
    100         add_filter('comments_clauses', array($sitepress, 'comments_clauses'), 10, 2);
    101 
    102         return $comments;
    103     }
    104134
    105135    public function merge_comment_count($count, $post_ID)
  • multilingual-comments-wpml/tags/1.2.1/readme.txt

    r3282635 r3437367  
    44Donate link: https://so-wp.com/donations
    55Requires at least: 4.9
    6 Tested up to: 6.8
     6Tested up to: 6.9
    77Requires PHP: 7.4
    88Stable tag: 1.2.1
  • multilingual-comments-wpml/trunk/multilingual-comments-wpml.php

    r3090993 r3437367  
    1010 *
    1111 * Requires at least:   4.9
    12  * Tested up to:        6.5
     12 * Tested up to:        6.9
    1313
    1414 * License: GPL-3.0+
     
    3939    {
    4040        register_activation_hook(__FILE__, [$this, 'activate']);
    41         add_filter('comments_array', [$this, 'merge_comments'], 100, 2);
     41        // Remove the later-stage comments_array filter
     42        // add_filter('comments_array', [$this, 'merge_comments'], 100, 2);
     43
     44        // Add earlier-stage filter for comment queries
     45        add_filter('comments_clauses', [$this, 'modify_comments_query'], 5, 2);
    4246        add_filter('get_comments_number', [$this, 'merge_comment_count'], 100, 2);
    4347    }
     
    6771    }
    6872
    69     private function sort_merged_comments($a, $b)
     73    public function modify_comments_query($clauses, $query)
    7074    {
    71         return $a->comment_ID - $b->comment_ID;
     75        global $wpdb, $sitepress;
     76
     77        // Ensure we are only modifying queries for a specific post (avoid affecting global queries)
     78        if (empty($query->query_vars['post_id'])) {
     79            return $clauses;
     80        }
     81
     82        $post_ID = $query->query_vars['post_id'];
     83        $post = get_post($post_ID);
     84
     85        // If the post does not exist, return the unmodified query
     86        if (!$post) {
     87            return $clauses;
     88        }
     89
     90        // Temporarily remove WPML's built-in comment filtering to prevent conflicts
     91        remove_filter('comments_clauses', array($sitepress, 'comments_clauses'));
     92
     93        // Get a list of all active languages
     94        $languages = apply_filters('wpml_active_languages', null, 'skip_missing=1');
     95        $post_ids = [$post_ID]; // Start with the current post ID
     96
     97        // Loop through available languages and get translated post IDs
     98        foreach ($languages as $code => $l) {
     99            if (!$l['active']) { // Skip the current active language
     100                $translated_id = apply_filters('wpml_object_id', $post_ID, $post->post_type, false, $l['language_code']);
     101                if ($translated_id) {
     102                    $post_ids[] = $translated_id; // Add translated post ID to the array
     103                }
     104            }
     105        }
     106
     107        // Ensure all post IDs are unique and properly formatted for SQL queries
     108        $post_ids = array_map('intval', array_unique($post_ids));
     109
     110        // Modify the WHERE clause to include all translations in the comment query
     111        $clauses['where'] = preg_replace(
     112            "/comment_post_ID = \d+/", // Look for the default `comment_post_ID = X` condition
     113            "comment_post_ID IN (" . implode(',', $post_ids) . ")", // Replace it with multiple post IDs
     114            $clauses['where']
     115        );
     116
     117        /**
     118         * Fix Pagination Issue:
     119         * - WordPress paginates comments **before** applying the `comments_clauses` filter.
     120         * - Since we are now fetching more comments, we need to adjust the LIMIT clause manually.
     121         * - `number` represents the number of comments per page.
     122         * - `offset` ensures pagination remains consistent.
     123         */
     124        if (!empty($query->query_vars['number'])) {
     125            $clauses['limits'] = "LIMIT " . (int) $query->query_vars['number'] . " OFFSET " . (int) $query->query_vars['offset'];
     126        }
     127
     128        // Re-add WPML's comment filtering after modifying the query
     129        add_filter('comments_clauses', array($sitepress, 'comments_clauses'), 10, 2);
     130
     131        return $clauses;
    72132    }
    73133
    74     public function merge_comments($comments, $post_ID)
    75     {
    76         global $sitepress;
    77 
    78         remove_filter('comments_clauses', array($sitepress, 'comments_clauses'));
    79 
    80         $languages = apply_filters('wpml_active_languages', null, 'skip_missing=1');
    81 
    82         $post = get_post($post_ID);
    83         $type = $post->post_type;
    84 
    85         foreach ($languages as $code => $l) {
    86             if (!$l['active']) {
    87                 $otherID = apply_filters('wpml_object_id', $post_ID, $type, false, $l['language_code']);
    88                 // add condition to prevent $otherID returning `null`
    89                 if ($otherID) {
    90                     $othercomments = get_comments(array('post_id' => $otherID, 'status' => 'approve', 'order' => 'ASC'));
    91                     $comments = array_merge($comments, $othercomments);
    92                 }
    93             }
    94         }
    95 
    96         if ($languages) {
    97             usort($comments, [$this, 'sort_merged_comments']);
    98         }
    99 
    100         add_filter('comments_clauses', array($sitepress, 'comments_clauses'), 10, 2);
    101 
    102         return $comments;
    103     }
    104134
    105135    public function merge_comment_count($count, $post_ID)
  • multilingual-comments-wpml/trunk/readme.txt

    r3090993 r3437367  
    44Donate link: https://so-wp.com/donations
    55Requires at least: 4.9
    6 Tested up to: 6.5
     6Tested up to: 6.9
    77Requires PHP: 7.4
    88Stable tag: 1.2.1
Note: See TracChangeset for help on using the changeset viewer.