Skip Navigation

[Resolved] Split: Request for a CRED action hook triggered once a post expiration is completed

This support ticket is created 6 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 1 reply, has 2 voices.

Last updated by Beda 6 years, 3 months ago.

Assisted by: Beda.

Author
Posts
#1077532

In the prior thread I wanted to be able to "chain" post expirations, so that, for example

1. a post is submitted via a CRED Form and is set to "expire" in two hours, when its status is changed to "private"
2. I would then like it to "expire" again after 2 days, when it would be changed to "trash"

Nigel tried to get it working using the cred_post_expiration_custom_actions filter to add custom actions for when the first expiry happens to reset the _cred_post_expiration_time and _cred_post_expiration_action meta fields.

This custom code nearly works:

/**
 * Multi-part expiration
 */
function tssupp_custom_expiration( $custom_actions, $post_id, $form_data ){

	$new_expiry_time = time() + 2 * 24 * 60 * 60;

	// Set updated "post expiry" time
	$custom_actions[] = array(
		'meta_key'		=> '_cred_post_expiration_time',
		'meta_value'	=>	$new_expiry_time
	);

	// Set updated "post expiry" action
	$custom_actions[] = array(
		'meta_key'		=> '_cred_post_expiration_action',
		'meta_value'	=>	array(
			'post_status'		=> 'trash',
			'custom_actions'	=> array()
		)
	);


	return $custom_actions;
}
add_filter( 'cred_post_expiration_custom_actions', 'tssupp_custom_expiration', 10, 3 );

Checking with phpMyAdmin the _cred_post_expiration_action field is updated like so (unserialized):

Array
(
    [post_status] => private
    [custom_actions] => Array
        (
            [0] => Array
                (
                    [meta_key] => _cred_post_expiration_time
                    [meta_value] => 1533747702
                )

            [1] => Array
                (
                    [meta_key] => _cred_post_expiration_action
                    [meta_value] => Array
                        (
                            [post_status] => trash
                            [custom_actions] => Array
                                (
                                )
                        )
                )
        )
)

But after the first expiration the _cred_post_expiration_time is set to zero—it must happen after the custom actions are applied.

So the request is to add a new action hook for when the post expiration has completed (which would allow setting up a new one, as well as other possibilities).

(Likewise, the CRED API could benefit from a new action hook for when a post submission really has completed: cred_submit_complete happens before the notifications are sent.)

#1078314

I understand, but somehow this is the opposite of what Forms does.

It offers you expiration settings so to do something if the post expires (put the post in a private state, for example)
It does not offer the opposite, to expire a post when a status changes... it is kind of pointless doing it, as the only you can do with a Post Expiration what you cannot do with a Date field is... automatically bind the CRON to fire the actions you set on the date reached.

So, the idea here is to let expire a post and then change the post status, nothing else (from the point of view for what it was created).

Once the post expires, Forms takes care and fires the actions to put the post in that state as set in the form

Now, the hook you used (cred_post_expiration_custom_actions) documented as below linked, hooks in cred_save_data later, so that is when the post is saved (also offered as API hook on itself):
https://toolset.com/documentation/user-guides/automatic-post-expiration/#what-happens-when-posts-expire
In the code this looks fine, it is hooked last, so it will overwrite whatever came before.
If you update expiration fields on that moment, they should save fine.

But, the code used is not clear to me.
The API allows to update meta fields.

function my_custom_actions($custom_actions, $post_id, $form_data) {
    $custom_actions[] = array( 'meta_key' => 'advert_active', 'meta_value' =>'false' );
    return $custom_actions;
}

However the API used there cannot change the post status with this, it only addresses meta_keys

If the purpose is, to:
- expire a post when something happens to a post (opposite of what Forms does now) then this is a new feature I can request.
However, I miss the point of doing this
- or, re-set a new expiration time, after the post is expired, and changes status, that I can request as well to be added
However, I see not many people yet requesting this, we let the Developers evaluate this.

Please accept my apologies if I misunderstood.