itthinx / decent-comments

Decent Comments shows what people say. Provides widgets, shortcodes and API to display comments including author avatars, links, comment excerpts.
Other
1 stars 3 forks source link

taxonomy queries in multi-lingual installations are modified to refer to current languag #3

Open Jon007 opened 7 years ago

Jon007 commented 7 years ago

This may be regarded as a WPML/Polyang issue, and depending on installation requirements this may or may not be a desirable behaviour, however from plugin viewpoint the behaviour is inconsistent depending on the query.

Simple queries - show all comments or show all comments by post type return results for all languages.

Taxonomy queries - are modified to show only comments in the currently selected language. In my view this is inconsistent and undesirable: and in the marketplace tools like tripadvisor give you the change to see comments in all languages to have a better view of feedback (and you can always translate it), whereas if feedback is separated for each language there is less feedback available for everyone.

To confirm I set up 5 taxonomy queries for the same category, querying using English values, Spanish values or both: all 5 queries resulted in the same SQL being generated, which used 1588 if the current language is English and 1599 if the current language is Spanish (English 1588=inkstone, Spanish 1599=piedra-tinta), and

Comments by product category id 1588 inkstone: [decent_comments number="25" taxonomy="product_cat" term_ids="1588"]

Comments by product category id 1599 inkstone Spanish: [decent_comments number="25" taxonomy="product_cat" term_ids="1599"]

Comments by product category term inkstone: [decent_comments number="25" taxonomy="product_cat" terms="inkstone"]

Comments by product category term piedra-tinta: [decent_comments number="25" taxonomy="product_cat" terms="piedra-tinta"]

Comments by product category ids 1588 and 1599 inkstone English and Spanish: [decent_comments number="25" taxonomy="product_cat" term_ids="1588,1599"]

English view SQL result: SELECT * FROM wp_comments LEFT JOIN wp_posts ON comment_post_ID = wp_posts.ID WHERE comment_approved = '1' AND comment_type NOT IN ("pingback", "trackback") AND wp_posts.post_type NOT IN ('shop_order','shop_order_refund') AND wp_posts.post_type <> 'shop_webhook' AND comment_post_ID IN ( SELECT DISTINCT ID FROM wp_posts LEFT JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id LEFT JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id WHERE wp_term_taxonomy.term_id IN ( 1588 ) ) ORDER BY comment_date_gmt DESC LIMIT 25

Spanish view SQL result: SELECT * FROM wp_comments LEFT JOIN wp_posts ON comment_post_ID = wp_posts.ID WHERE comment_approved = '1' AND comment_type NOT IN ("pingback", "trackback") AND wp_posts.post_type NOT IN ('shop_order','shop_order_refund') AND wp_posts.post_type <> 'shop_webhook' AND comment_post_ID IN ( SELECT DISTINCT ID FROM wp_posts LEFT JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id LEFT JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id WHERE wp_term_taxonomy.term_id IN ( 1599 ) ) ORDER BY comment_date_gmt DESC LIMIT 25

Jon007 commented 7 years ago

Correction, this behaviour is not inconsistent:

Jon007 commented 7 years ago

Here is a fix that gets the requested taxonomy comments, without the request being changed by any plugin filters.

note that current code, class-decent-comment.php l.287++ takes an array of taxonomy ids and instantiates term objects... in order to get the ids again, and in doing so applies filters:

            $terms = get_terms( $taxonomy, array( 'include' => $term_ids ) );
            if ( is_array( $terms ) ) {
                $term_ids = array();
                foreach ( $terms as $term ) {
                    $term_ids[] = $term->term_id;
                }
                $term_ids = implode( ",", $term_ids );
                if ( strlen($term_ids) == 0 ) {
                    $term_ids = "NULL";
                }
                $where .=
                    " AND comment_post_ID IN (
                        SELECT DISTINCT ID FROM $wpdb->posts
                        LEFT JOIN $wpdb->term_relationships ON $wpdb->posts.ID = $wpdb->term_relationships.object_id
                        LEFT JOIN $wpdb->term_taxonomy ON $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id
                        WHERE $wpdb->term_taxonomy.term_id IN ( $term_ids )
                        ) ";
            }

This get_terms() part can be removed since we already have term ids so the resulting code is:

            if ( is_array( $term_ids ) ) {
                $term_ids = implode( ",", $term_ids );
                if ( strlen($term_ids) == 0 ) {
                    $term_ids = "NULL";
                }
                $where .=
                    " AND comment_post_ID IN (
                        SELECT DISTINCT ID FROM $wpdb->posts
                        LEFT JOIN $wpdb->term_relationships ON $wpdb->posts.ID = $wpdb->term_relationships.object_id
                        LEFT JOIN $wpdb->term_taxonomy ON $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id
                        WHERE $wpdb->term_taxonomy.term_id IN ( $term_ids )
                        ) ";
            }

also a small change to l.254 to avoid adding empty item to terms array which may result in invalid query later:

            //Avoid adding empty item to array
            if ( is_string( $term_ids ) && strlen($term_ids)>0 ) {
                $term_ids = explode( ",", $term_ids );
            }
            else{
                $term_ids = array();
            }

The effect of this change is that the comments will display for the selected term. If the term is language specific then the comments will only show in that language, but it will be the requested term, and so if wanted it is also possible to request comments for multiple terms in different languages.

Tested for syntaxes:

[decent_comments taxonomy="product_cat" term_ids="1234"]
[decent_comments taxonomy="product_cat" term_ids="1234,5678"]
[decent_comments taxonomy="product_cat" terms="my-english-term"]
[decent_comments taxonomy="product_cat" terms="my-english-term,mi-termino-esp"]
Jon007 commented 7 years ago

Within the language context, this means that if you ask for comment on a category in Spanish language, that's what you get, regardless of current interface language. At the same time if you are writing a post in English and use an english taxonomy query you are only going to get English comments, but you now have the choice to add terms from additional languages without the language plugin removing them.

Jon007 commented 7 years ago

Ok, one note which may be a desirable feature: if querying by terms rather than by id, then will find comments for any post matching the term, independently of the taxonomy.

So in the example: [decent_comments taxonomy="product_cat" terms="my-english-term"] the taxonomy part is not relevant, the ids returned from the terms query are not filtered by taxonomy (great!)

This will match:

So it provides a more complete answer to the query comments about ".." (albeit taxonomy based rather than full text search etc)