PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Nov 18, 2017 4:30 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 115 posts ]  Go to page 1, 2, 3, 4, 5 ... 8  Next
Author Message
PostPosted: Tue Sep 05, 2017 3:52 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4356
Location: United Kingdom
I've found this code within Wordpress. I want to be able to change the query so it only finds comments where the comments_content field is not empty.

This isn't a normal PHP query to me, so not sure how to do it. Whether I need to add a function and puts something in there, so that $comments_content is only empty when it is used. I don't know. Any help would be great.

This query is in wp-includes/link-template.php.

Syntax: [ Download ] [ Hide ]
function paginate_comments_links( $args = array() ) {
        global $wp_rewrite;

        if ( ! is_singular() )
                return;

        $page = get_query_var('cpage');
        if ( !$page )
                $page = 1;
        $max_page = get_comment_pages_count();
        $defaults = array(
                'base' => add_query_arg( 'cpage', '%#%' ),
                'format' => '',
                'total' => $max_page,
                'current' => $page,
                'echo' => true,
                'add_fragment' => '#comments'
        );
        if ( $wp_rewrite->using_permalinks() )
                $defaults['base'] = <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>(<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>(get_permalink()) . $wp_rewrite->comments_pagination_base . '-%#%', 'commentpaged');

        $args = wp_parse_args( $args, $defaults );
        $page_links = paginate_links( $args );

        if ( $args['echo'] )
                echo $page_links;
        else
                return $page_links;
}

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Wed Sep 06, 2017 6:05 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6391
Location: Montreal, Canada
A few things. First, don't modify the WordPress core. Next time you update WP, all your changes will be lost. Write a plugin or make changes at the theme level. If you're using a third-party theme, create a sub-theme and make the changes there so you can update your theme later.

To the crux of the matter. That function you posted doesn't retrieve comments, it simply creates the pagination links for comments already retrieved. Comments themselves are retrieved by wp_list_comments. I haven't worked with WordPress in a couple of years, so I could be wrong, but I'm not seeing any obvious way to exclude empty comments. I'm kind of surprised that isn't default behaviour. Does it display empty comments even when you aren't logged in?

In any event, wp_list_comments does accept an array of comment objects as its second parameter, so you could retrieve the defaults, use array_filter or similar to filter out the empty comments, and then pass the result back to wp_list_comments. Barring that, you may need to create your own methods to retrieve and paginate over your comments. Take a look at this tutorial for ideas.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Sep 06, 2017 6:20 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4356
Location: United Kingdom
I don't know about these queries, but yes I can see what you mean, as there is no actual query on this file.
On the page you sent, "Other Simple Comment Restrictions", this is I think where it is trying to guide to do it.
Syntax: [ Download ] [ Hide ]
Using these parameters we could list a custom comment type which contains the term "kazoo".

Syntax: [ Download ] [ Hide ]
$args = array(
    'type' => 'review',
    'search' => 'kazoo'
);
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );

So how could you use this, but not to say where reviews is on "kazoo", but where comment_content is not empty? And how to do get that into the query?

That's what I am trying to learn. Oddly, it is only for this one website, because of the import we did. But it's important, as we get so many reviews, we are at the moment, only showing 30 (disabling the pagination via CSS for now).

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Wed Sep 06, 2017 6:58 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6391
Location: Montreal, Canada
If these are simply due to an import, would it not be easier to simply mark them as unapproved, or to delete them altogether?

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Sep 06, 2017 7:05 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4356
Location: United Kingdom
No. I wish we could. We imported over 5,000 star reviews from a non-WP site. To do that, and keep the reviews schema, they were imported into Reviews but as they were just ratings, they had no 'comments'.

At the top of the page we have all the star reviews as an average. And in the Reviews tab, we show only reviews where it has something in comment_content, but the pagination still shows ALL the page numbers for every review for that product/post, no matter what is in that field.... hence trying to fix pagination.

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Wed Sep 13, 2017 4:38 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4356
Location: United Kingdom
https://codex.wordpress.org/Function_Reference/paginate_comments_links
https://developer.wordpress.org/reference/functions/paginate_comments_links/

Found these two pages. The query itself must be somewhere, in order to extract the info. I'm told that these things are edited via Hooks and Actions, but that this query doesn't have those.

I suppose it's because usually no one needs to edit it. But we need to.

It would even be in the basic Wordpress setup for reviews on Posts. Just need to figure out how to write a little query where comment_content is not empty, and put that into the query. But with WP queries I am stumped.
If it was my custom coded website, it would be easy.

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Wed Sep 13, 2017 4:53 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6391
Location: Montreal, Canada
simonmlewis wrote:
https://codex.wordpress.org/Function_Reference/paginate_comments_links
https://developer.wordpress.org/reference/functions/paginate_comments_links/

Found these two pages. The query itself must be somewhere, in order to extract the info. I'm told that these things are edited via Hooks and Actions, but that this query doesn't have those.

I suppose it's because usually no one needs to edit it. But we need to.

It would even be in the basic Wordpress setup for reviews on Posts. Just need to figure out how to write a little query where comment_content is not empty, and put that into the query. But with WP queries I am stumped.
If it was my custom coded website, it would be easy.

Have you tried what I suggested above?

Quote:
In any event, wp_list_comments does accept an array of comment objects as its second parameter, so you could retrieve the defaults, use array_filter or similar to filter out the empty comments, and then pass the result back to wp_list_comments. Barring that, you may need to create your own methods to retrieve and paginate over your comments. Take a look at this tutorial for ideas.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Sep 13, 2017 5:06 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4356
Location: United Kingdom
Quote:
so you could retrieve the defaults

Syntax: [ Download ] [ Hide ]
<?php $args = array(
        'walker'            => null,
        'max_depth'         => '',
        'style'             => 'ul',
        'callback'          => null,
        'end-callback'      => null,
        'type'              => 'all',
        'reply_text'        => 'Reply',
        'page'              => '',
        'per_page'          => '',
        'avatar_size'       => 32,
        'reverse_top_level' => null,
        'reverse_children'  => '',
        'format'            => 'html5', // or 'xhtml' if no 'HTML5' theme support
        'short_ping'        => false,   // @since 3.6
        'echo'              => true     // boolean, default is true
); ?>


Quote:
use array_filter or similar to filter out the empty comments

How?

Quote:
then pass the result back to wp_list_comments

Again how. sorry I am totally blind with this, but I do want to learn. Iv'e seen people doing functions and passing stuff back but never understood it.

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Wed Sep 13, 2017 5:36 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6391
Location: Montreal, Canada
Again, not having worked with WP in a couple of years, and having to leave for work myself in a few minutes, here's where I'd start:

wp_list_comments allows you to hook into a filter: wp_list_comments_args. If I recall WP correctly, you'll need to add_filter to tell WP what to run when that filter is applied. Take a look at this tutorial to understand how filters work. This is where you'll likely want to use array_filter to remove all the comments with empty contents.

For an example of how array_filter works, consider this:
Syntax: [ Download ] [ Hide ]
$array = [
    ['id' => 'A', 'condition' => true],
    ['id' => 'B', 'condition' => false],
    ['id' => 'C', 'condition' => false],
    ['id' => 'D', 'condition' => true],
];

$filtered = array_filter($array, function($item) {
    return $item['condition'] === true;
});

print_r($filtered);

which prints the following:
Syntax: [ Download ] [ Hide ]
Array
(
    [0] => Array
        (
            [id] => A
            [condition] => 1
        )

    [3] => Array
        (
            [id] => D
            [condition] => 1
        )

)

Make sense?

Quote:
Again how. sorry I am totally blind with this, but I do want to learn. Iv'e seen people doing functions and passing stuff back but never understood it.

https://laracasts.com/series/php-for-beginners
Take the time to go through this free video series. Yes, it's basic and there will be things you're already well aware of, but I expect it will also help fill in the gaps on the things you're less familiar with. From there, I'm happy to answer any questions you might have.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Sep 13, 2017 5:53 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4356
Location: United Kingdom
Ok I can see how arrays work, and the different types. Where you can take an array, create a function with an added part to it, then merge them together.
What I don't yet see if how I can say in the array of things it is looking for in the default arg, where comment_content is empty.

In the default arg, a portion of it is this:
Syntax: [ Download ] [ Hide ]
'page'              => '',

Is the => a way of saying "equals"?
Such as "page = 'fred'"?

So could you do something a bit like this:
Syntax: [ Download ] [ Hide ]
<?php wp_list_comments( array( 'comment_content' != '' ) ); ?>

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Wed Sep 13, 2017 6:20 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6391
Location: Montreal, Canada
simonmlewis wrote:
In the default arg, a portion of it is this:
Syntax: [ Download ] [ Hide ]
'page'              => '',

Is the => a way of saying "equals"?
Such as "page = 'fred'"?

Yes, that's exactly right. These are equivalent:
Syntax: [ Download ] [ Hide ]
$shirt = [
    'size' => 'M',
    'colour' => 'Red',
];

Syntax: [ Download ] [ Hide ]
$shirt = [];
$shirt['size'] = 'M';
$shirt['colour'] = 'Red';


simonmlewis wrote:
What I don't yet see if how I can say in the array of things it is looking for in the default arg, where comment_content is empty.

You can't. Keep going through the function and you'll see it calls apply_filters. It provides the name of the filter it will call, which allows you to hook into it via add_filter. You pass that the name of the filter as the first argument, and the name of the function you want run when that filter is called as the second argument. This second argument will be the function you define, and this is where you can filter out comments with no content.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Sep 13, 2017 6:31 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4356
Location: United Kingdom
Thanks for confirmation.
I still don't see where you state where something is *NOT* something.
Where color is NOT red.
Where fieldname is NOT empty.
Where comment_content != ''.

Even though you cannot do that, surely you have to somehow tell it in the arg, where a field is not empty. That's the start of this puzzle for me. I See lots where field => 'red'. but not where field != 'red'.

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Wed Sep 13, 2017 6:52 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4356
Location: United Kingdom
Taking a step backward a little.
First I need to get into the function for list comments.

Syntax: [ Download ] [ Hide ]
wp_list_comments( $args, $comments );

This is the array in it's Arguments.
Syntax: [ Download ] [ Hide ]
$args = array(
        'walker'            => null,
        'max_depth'         => '',
        'style'             => 'ul',
        'callback'          => null,
        'end-callback'      => null,
        'type'              => 'all',
        'reply_text'        => 'Reply',
        'page'              => '',
        'per_page'          => '',
        'avatar_size'       => 32,
        'reverse_top_level' => null,
        'reverse_children'  => '',
        'format'            => 'html5', // or 'xhtml' if no 'HTML5' theme support
        'short_ping'        => false,   // @since 3.6
        'echo'              => true     // boolean, default is true

So I need to add_array to this.

Syntax: [ Download ] [ Hide ]
if(has_filter('wp_list_comments')) {
                $args = apply_filters('wp_list_comments', $args);
        }


Then this?
Syntax: [ Download ] [ Hide ]
function wp_list_comments_remove_blanks($args) {
        // the $args parameter is an array of arguments for  wp_list_comments() function
 
$additional_arg = array(
                'comment_content'  != '',
        );
 
        // combine the two arrays
        $arg = array_merge($additional_arg, $arg);
        return $arg;
}
add_filter('wp_list_comments', 'wp_list_comments_remove_blanks');


Is this getting toward it ?

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Wed Sep 13, 2017 6:53 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6391
Location: Montreal, Canada
Go back and look at my array_filter example. That's where you'd do it, inside the filter function you defined.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Sep 13, 2017 7:04 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6391
Location: Montreal, Canada
I'm pretty sure you want to hook into wp_list_comments_args. On my phone on the subway, so hard for me to dig into the code, but that's where I'd start. I'll look further tonight if you haven't resolved it.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 115 posts ]  Go to page 1, 2, 3, 4, 5 ... 8  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group