Make WordPress Core

Opened 8 years ago

Closed 8 years ago

#35600 closed enhancement (fixed)

Hooks for post sticky status changes

Reported by: ojrask's profile ojrask Owned by: ojrask's profile ojrask
Milestone: 4.6 Priority: normal
Severity: normal Version: 2.7
Component: Posts, Post Types Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

Currently sticky post status changes are hookable using the update_option hook variants which are fired when stick_post and unstick_post functions are called.

Getting the sticked post in those hooks is a little bit annoying, as you have to parse old and new values for an updated option.

I'd suggest adding action hooks to stick_post and unstick_post to fire some action called such as

<?php

do_action('post_sticked', $post->ID);
do_action('post_unsticked', $post->ID);

I could not find any straightforward hook that allows hooking as described above.

Let me know if there is a feature like the one above which I could not find, and whether the suggested enhancement would be good or bad, or in need of more discussion.

One thing I would like to know whether this would have broader benefits or whether this would be considered a small goal for a very small portion of developers.

Marked the version as latest stable, but I presume the feature is not available in any version which has included sticky posts.

Attachments (5)

35600.diff (2.2 KB) - added by ojrask 8 years ago.
Patch for #35600, includes logic and tests.
35600-1.diff (2.5 KB) - added by ojrask 8 years ago.
Adjusted patch, adds inline hook PHP documentation.
35600-2.diff (2.7 KB) - added by ojrask 8 years ago.
Rename hooks, make tests work in PHP 5.2
35600-3.diff (2.7 KB) - added by ojrask 8 years ago.
Fix wording in tests
35600.4.diff (2.1 KB) - added by ocean90 8 years ago.

Download all attachments as: .zip

Change History (24)

@ojrask
8 years ago

Patch for #35600, includes logic and tests.

#1 @ojrask
8 years ago

  • Keywords has-patch has-unit-tests added

#2 @SergeyBiryukov
8 years ago

  • Keywords needs-docs added

New hooks need to be documented as per the documentation standards.

Last edited 8 years ago by SergeyBiryukov (previous) (diff)

#3 @ojrask
8 years ago

Oh boy, completely forgot about that one. Let me adjust the patch and send a new one.

@ojrask
8 years ago

Adjusted patch, adds inline hook PHP documentation.

#4 @ojrask
8 years ago

  • Keywords needs-docs removed

I used @since 4.5.0 as saw that one elsewhere too. Should be adjusted in case this is implemented to some earlier version.

Last edited 8 years ago by ojrask (previous) (diff)

#5 @ojrask
8 years ago

  • Version changed from 4.4.1 to 2.7

Changed the version to 2.7 as hooks in their current state were introduced in 2.1 and sticky posts arrived in version 2.7.

#6 @SergeyBiryukov
8 years ago

  • Milestone changed from Awaiting Review to 4.6

#7 @ocean90
8 years ago

@ojrask The tests must be compatible with PHP 5.2. For testing hooks I suggest to use MockAction from /tests/phpunit/includes/utils.php.

#8 @ojrask
8 years ago

I see, let me adjust the patch accordingly.

This ticket was mentioned in Slack in #core by chriscct7. View the logs.


8 years ago

#10 @screamingdev
8 years ago

How is this different from using the action "update_option_sticky_posts" ? Just asking.

#11 @voldemortensen
8 years ago

@ojrask In addition to updating the unit tests to be PHP 5.2 compatible, the hook names should be post_stuck and post_unstuck as stuck is the past (past participle) of stick. The dead line for enhancements for 4.6 is about a month away (June 29) if you're still interested in pursuing this patch.

#12 @ojrask
8 years ago

@voldemortensen I'll see that the patch is updated accordingly. Stuck and unstuck do make sense.

@screamingdev Using the update_option_$key hook is cumbersome and requires the developer to do some option value juggling (to parse the difference before and after), instead of a hook providing the exact post ID which was stuck or unstuck. Please correct me if this is not the case. :)

@ojrask
8 years ago

Rename hooks, make tests work in PHP 5.2

#13 @ojrask
8 years ago

Added a revised patch (35600-2.diff), which renames stick -> stuck and unstick -> unstuck, also adjusts tests so they should work fine in PHP 5.2. Additionally changed @since from 4.5.0 to 4.6.0.

Let me know if something is still in need of adjustments. :)

Last edited 8 years ago by ojrask (previous) (diff)

#14 @chriscct7
8 years ago

  • Owner set to ojrask
  • Status changed from new to assigned

#15 @netweb
8 years ago

  • Keywords needs-refresh added

@ojrask The tests should also use the same Orwellian Newspeak unstuck rather than unstickied, unstick, unsticking etc :)

@ojrask
8 years ago

Fix wording in tests

#16 @ojrask
8 years ago

@netweb patch 35600-3.diff should fix that issue.

#17 @netweb
8 years ago

  • Keywords needs-refresh removed

35600-3.diff looks good @ojrask

This ticket was mentioned in Slack in #core by voldemortensen. View the logs.


8 years ago

@ocean90
8 years ago

#19 @swissspidy
8 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 37857:

Posts: Add hooks for post sticky status changes.

This adds a new post_stuck action that fires when a post is made sticky and a post_unstuck action that fires when the sticky flag is removed again.

Props ojrask.
Fixes #35600.

Note: See TracTickets for help on using tickets.