Last active
August 4, 2016 19:21
-
-
Save mikeselander/a511df68389f2b000931c7a756d4a627 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Add the Livefyre lf_init_script action to our actions to be modified. | |
* | |
* @param array $actions Actions to be modified. | |
* @return array $actions Actions with our action added. | |
*/ | |
function modify_livefyre_script_order( $actions ) { | |
// Move the Livefyre comments output to load after our scripts. | |
$actions[] = array( | |
'hook' => 'wp_footer', | |
'method' => 'lf_init_script', | |
'old_priority' => 10, | |
'new_priority' => 25, | |
); | |
return $actions; | |
} | |
add_filter( 'modify_action_priority', 'modify_livefyre_script_order' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Modify the priority of a particular hooked action. | |
* | |
* This is useful for cases when dealing with an externally-hooked action (plugin) | |
* that hooks a mathod in an unmodifiable way. For example, loading a method in a class | |
* outside of the global scope as a child of another non-static class in a constructor. | |
* | |
* For example, LFAPPS_Comments_Display->lf_init_script(). | |
* | |
* @global array $wp_filter All filters and actions registered. | |
*/ | |
function modify_action_priority() { | |
global $wp_filter; | |
/** | |
* Filter to modify any number of actions to another priority. | |
* | |
* Feed in an array to modify an action from one priority to another. | |
* | |
* @param array Arguments for modification [ 'hook', 'method', 'old_priority', 'new_priority' ] | |
*/ | |
$actions = apply_filters( 'modify_action_priority', array() ); | |
// No actions to modify or $wp_filter is empty, return early. | |
if ( empty( $wp_filter ) || empty( $actions ) ) { | |
return; | |
} | |
foreach ( $actions as $action ) { | |
// We don't have the correct priority, bump to next action. | |
if ( ! isset( $wp_filter[ $action['hook'] ][ $action['old_priority'] ] ) ) { | |
continue; | |
} | |
// Fetch our actions based on hook and priority. | |
$priority_hook_actions = $wp_filter[ $action['hook'] ][ $action['old_priority'] ]; | |
// Loop through each action hooked into this priority. | |
foreach ( $priority_hook_actions as $key => $value ) { | |
// If our action matches the hashed key, move it to our next priority. | |
if ( strpos( $key, $action['method'] ) > -1 ) { | |
// Unset the action from the old priority. | |
unset( $wp_filter[ $action['hook'] ][ $action['old_priority'] ][ $key ] ); | |
// Add the action to the new priority. | |
$wp_filter[ $action['hook'] ][ $action['new_priority'] ][ $key ] = $value; | |
// No more need to keep looking, break out. | |
break; | |
} | |
} | |
} | |
} | |
add_action( 'wp_loaded', 'modify_action_priority' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Class The_Sun_Test_Football_Teams_Query_Functions | |
* | |
* Test helper functions for retrieving data related to Football Teams. | |
*/ | |
class The_Sun_Modify_Action_Priority extends WP_UnitTestCase { | |
/** | |
* Holds our settings for overriding the action. | |
* | |
* @var array | |
*/ | |
public static $override_details = array( | |
'hook' => 'get_header', | |
'method' => 'function_to_modify', | |
'old_priority' => 10, | |
'new_priority' => 5, | |
); | |
/** | |
* Set up the proper hooks and filters. | |
*/ | |
public static function wpSetUpBeforeClass() { | |
add_action( self::$override_details['hook'], array( 'The_Sun_Modify_Action_Priority', self::$override_details['method'] ), 10 ); | |
add_filter( 'modify_action_priority', function( $actions ){ | |
$actions[] = self::$override_details; | |
return $actions; | |
}, 15, 1 ); | |
} | |
/** | |
* Action to override - doesn't need to do anything. | |
*/ | |
public static function function_to_modify(){} | |
/** | |
* Verify that the action priority has appropriately changed. | |
*/ | |
public function test_modify_action_priority() { | |
global $wp_filter; | |
// Assert that our action is loaded as part of the test suite. | |
$this->assertSame( 1, did_action( 'wp_loaded' ) ); | |
// Assert that our function is hooked into wp_loaded (returns a priority if running correctly). | |
$this->assertNotFalse( has_action( 'wp_loaded', 'modify_action_priority' ) ); | |
// Run our function manually since it's hooked in too early to be run naturally. | |
modify_action_priority(); | |
// Assert that our new key exists before pulling it for checking our action location. | |
$this->assertArrayHasKey( self::$override_details['new_priority'], $wp_filter[ self::$override_details['hook'] ] ); | |
$correct_priority = $wp_filter[ self::$override_details['hook'] ][ self::$override_details['new_priority'] ]; | |
$incorrect_priority = $wp_filter[ self::$override_details['hook'] ][ self::$override_details['old_priority'] ]; | |
// Get the spl_object_hash - needed to find the right action key. | |
$hash = substr( key( $correct_priority ), 0, 32 ); | |
// Assert that the action hook has been added to the correct priority. | |
$this->assertArrayHasKey( $hash . self::$override_details['method'], $correct_priority ); | |
// Assert that the action has been removed from the old priority. | |
$this->assertArrayNotHasKey( $hash . self::$override_details['method'], $incorrect_priority ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment