Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow creation of comment form via ajax #149

Open
luflow opened this issue Apr 4, 2017 · 12 comments · Fixed by #355 · May be fixed by #564
Open

Allow creation of comment form via ajax #149

luflow opened this issue Apr 4, 2017 · 12 comments · Fixed by #355 · May be fixed by #564
Assignees
Labels
enhancement v3 This issue is for the new version (v3) of the plugin

Comments

@luflow
Copy link

luflow commented Apr 4, 2017

Hi everyone!

I just want to understand your decision to block all AJAX calls in the plugin init() function
https://github.com/pluginkollektiv/antispam-bee/blob/master/antispam_bee.php#L76

In our case we just want to create the comment form via AJAX because we are working with a page cache and the comment form will be loaded in a popup after clicking the "Add a comment" Button

Before I work out a pull request to work around this issue I just want to understand the decision to block all AJAX calls.

Thanks :)

@websupporter
Copy link
Contributor

Hi @krautnerds,
I do not really recall or know the reason behind it and what I see from the first sight to not block this could potentially give us compatibility with ajax driven comment plugins.

Would removing this block simply solve your issue?

@swissspidy
Copy link
Contributor

At first glance it looks like these checks are just there to prevent adding too many unnecessary hooks further down.

@Zodiac1978
Copy link
Member

Updated reference:
https://github.com/pluginkollektiv/antispam-bee/blob/master/antispam_bee.php#L106-L108

Any chance to achieve this without breaking AJAX Comments plugins @websupporter ?

@matthiaswagnerwy
Copy link

+1 on this. we store our comments in an ajax-callback with wp_handle_comment_submission. antispam bee does not work here since those checks and therefore the comment body is recognized as empty.

@Zodiac1978
Copy link
Member

Zodiac1978 commented Jul 9, 2020

@websupporter What about adding a filter (or option?) here to allow AJAX calls if necessary?

if ( (defined('DOING_AJAX') && DOING_AJAX) or (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) ) { return; }

I can remove the check and test with some plugins to see if we could add this easily. Or do you think this is solved with v3 already?

Which plugins should I check?
(If the plugin is removing the complete comment form it will not work at all, so this list needs more checking and is not meant to be correct or complete):
https://wordpress.org/plugins/wp-ajaxify-comments/
https://wordpress.org/plugins/wpdiscuz/
https://wordpress.org/plugins/epoch/
https://wordpress.org/plugins/anycomment/
https://wordpress.org/plugins/deeper-comments/

And this is a tutorial for adding this feature in a theme. Maybe we should check this theme path too:
https://rudrastyh.com/wordpress/ajax-comments.html

@luflow Did you tried to remove the check? Does this work out? Would a filter be a solution for you? Is this a plugin or a theme solution and is it public or custom?

@matthiaswagnerwy Who is "we" in your case? Public plugin we should test? Or something custom made? Have you removed the check to test if this would work for you? Would a filter help you to fix this easier?

@matthiaswagnerwy
Copy link

@Zodiac1978 thanks for getting back to this. we made a custom solution based on the tutorial from rudrastyh.com. i tried to remove that mentioned return-statement in antispam-bee temporarily, but it was not enough.

i found a few lines which i think should also be considered, but stopped working on that and instead added a simple honeypot for myself since we need to go live soon.
https://github.com/pluginkollektiv/antispam-bee/blob/master/antispam_bee.php#L137
https://github.com/pluginkollektiv/antispam-bee/blob/master/antispam_bee.php#L1057-1059

a filter would be fine to control this behaviour, if you don't want to add more complexity to your settings interface :) of course, if there would be a setting this should also be based on a filter ;)

thanks for your great work!

@Zodiac1978
Copy link
Member

i found a few lines which i think should also be considered

Thanks for findings! I can't guarantee anything, but I will try to follow your path. Hopefully we can fix this.

@Zodiac1978
Copy link
Member

Zodiac1978 commented Oct 1, 2020

we made a custom solution based on the tutorial from rudrastyh.com.

I tried to get this working with ASB and this tutorial and I have a solution.

At first we need to comment out these lines:

        if ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ) {
            return;
        }

My suggestion would be to add a filter here to just disable the return in case of a ajax request.

In case of WP in a subfolder the ajax url should be created with a different solution:
'ajaxurl' => admin_url( 'admin-ajax.php' ),

And now the main part:

    // Get post ID.
    $post_id = (int) Antispam_Bee::get_key( $_POST, 'comment_post_ID' );

    // Create secret with post ID  (only possible with active ASB!).
    $secret  = Antispam_Bee::get_secret_id_for_post( $post_id );

    // Read honeypot and comment field (only possible with active ASB!).
    $hidden_field = Antispam_Bee::get_key( $_POST, 'comment' );
    $plugin_field = Antispam_Bee::get_key( $_POST, $secret );

    // Compare and maybe fix fields.
        if ( empty( $hidden_field ) && ! empty( $plugin_field ) ) {
            $_POST['comment'] = $plugin_field;
            unset( $_POST[ $secret ] );
        } else {
            // If nothing is fixed, no comment is possible, so nothing to do here.
        }

This has to be added before the line:
$comment = wp_handle_comment_submission( wp_unslash( $_POST ) );

So if someone wants to do this, we just have to add a filter to disable the AJAX check.

@Zodiac1978 Zodiac1978 linked a pull request Oct 1, 2020 that will close this issue
@Zodiac1978 Zodiac1978 self-assigned this Oct 1, 2020
@Zodiac1978 Zodiac1978 reopened this Dec 27, 2023
@Zodiac1978
Copy link
Member

Although the above code now allows commenting, Antispam Bee still does not work properly, because we exit our functions because of a request path check:

https://github.com/pluginkollektiv/antispam-bee/blob/master/antispam_bee.php#L1134-L1136

and

https://github.com/pluginkollektiv/antispam-bee/blob/master/antispam_bee.php#L1179-L1199

The check only allows wp-comment-post.php as request path. But in an AJAX call the request path is /wp-admin/admin-ajax.php and therefore we exit precheck_incoming_request and handle_incoming_request early without firing our filters.

@Zodiac1978 Zodiac1978 added the v3 This issue is for the new version (v3) of the plugin label Dec 30, 2023
@Zodiac1978
Copy link
Member

Themes with wp_handle_comment_submission (AJAX) Feature:
https://wpdirectory.net/search/01HJKQW4Q7NCTKNB3W54CPARMZ

@Zodiac1978
Copy link
Member

Zodiac1978 commented Jan 21, 2024

For Ajax call is_admin is true, so we don't get in the else branch but stay in the admin branch:

https://github.com/pluginkollektiv/antispam-bee/blob/master/antispam_bee.php#L168

There needs to be added one more check!

@Zodiac1978
Copy link
Member

Zodiac1978 commented Feb 3, 2024

This could be solved with adding this check:
} elseif ( is_admin() && ! defined( 'DOING_AJAX' ) ) {

If this is an AJAX request, we shouldn't go to the admin branch of the if, but also go in the else branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement v3 This issue is for the new version (v3) of the plugin
Projects
None yet
5 participants