rspraymond / wp-discord

https://wordpress.org/plugins/wp-discord/
9 stars 9 forks source link

Use of transition_post_status is problematic with some functions #13

Closed CharlyLeetham closed 6 years ago

CharlyLeetham commented 6 years ago

I use Gravity Forms to allow people to edit certain posts from the frontend. That plugin will set an already published post to 'draft' and then republish it.

As wp-discord uses transition_post_status to determine if a post should be sent to discord is problematic.

To get around this, I've modified the post_published_event function in class-wp-discord-admin.php (Around line 209 at the time of writing) and replaced:

   ` // Only post to discord when a post switches from unpublished to published.
    if ($old_status == 'publish' || $new_status != 'publish') {
        return true;
    }`

With:

    `// Only post to discord when a post switches from unpublished to published.
    $alreadyposted = get_post_meta($post->ID, 'wpdiscord_alreadyposted', true);

    if ( $alreadyposted  ) {
        return true;
    } elseif ($old_status == 'publish' || $new_status != 'publish') {
        return true;
    }`

After $webhook->post_content($content); Add: update_post_meta($post->ID, 'wpdiscord_alreadyposted', 'true');


These changes allow for custom meta to be set when the discord post is made and then for the post to be skipped if the function in use toggles the published setting.

I'm sure there's a better way of doing this, but that's what I came up with.

rspraymond commented 6 years ago

@CharlyLeetham Did you want to open a pull request for this? Maybe call the boolean wpdiscord_posted

rspraymond commented 6 years ago

Also I think it would be fine to just add the condition to the existing if check.

<?php
// Only post to discord when a post switches from unpublished to published.
    if ($old_status == 'publish' || $new_status != 'publish' || $alreadyposted) {
        return true;
    }`
CharlyLeetham commented 6 years ago

Hi @rspraymond Sorry for the delay, busy few weeks. You're correct regarding the logic, I tested that to be sure. I think I've created the pull request correctly - first time doing this and all.

Please let me know if it's correct.

rspraymond commented 6 years ago

@CharlyLeetham Pull request merged. Thanks for your contribution!