ActionScheduler_wpPostStore::set_unique_post_slug()
Create a (probably unique) post name for scheduled actions in a more performant manner than wp_unique_post_slug().
When an action's post status is transitioned to something other than 'draft', 'pending' or 'auto-draft, like 'publish' or 'failed' or 'trash', WordPress will find a unique slug (stored in post_name column) using the wp_unique_post_slug() function. This is done to ensure URL uniqueness. The approach taken by wp_unique_post_slug() is to iterate over existing post_name values that match, and append a number 1 greater than the largest. This makes sense when manually creating a post from the Edit Post screen. It becomes a bottleneck when automatically processing thousands of actions, with a database containing thousands of related post_name values.
WordPress 5.1 introduces the pre_wp_unique_post_slug for plugins to address this issue.
We can short-circuit WordPress's wp_unique_post_slug() approach using the pre_wp_unique_post_slug This method is available to be used as a callback on that filter. It provides a more scalable approach to generating a post_name/slug that is probably unique. Because Action Scheduler never actually uses the post_name field, or an action's slug, being probably unique is good enough.
For more backstory on this issue, see:
- https://github.com/woocommerce/action-scheduler/issues/44 and
- https://core.trac.wordpress.org/ticket/21112
Method of the class: ActionScheduler_wpPostStore{}
No Hooks.
Return
String
.
Usage
$ActionScheduler_wpPostStore = new ActionScheduler_wpPostStore(); $ActionScheduler_wpPostStore->set_unique_post_slug( $override_slug, $slug, $post_ID, $post_status, $post_type );
- $override_slug(string) (required)
- Short-circuit return value.
- $slug(string) (required)
- The desired slug (post_name).
- $post_ID(int) (required)
- Post ID.
- $post_status(string) (required)
- The post status.
- $post_type(string) (required)
- Post type.
ActionScheduler_wpPostStore::set_unique_post_slug() ActionScheduler wpPostStore::set unique post slug code WC 9.2.3
public function set_unique_post_slug( $override_slug, $slug, $post_ID, $post_status, $post_type ) { if ( self::POST_TYPE === $post_type ) { $override_slug = uniqid( self::POST_TYPE . '-', true ) . '-' . wp_generate_password( 32, false ); } return $override_slug; }