Skip to content

Commit

Permalink
Merge branch 'activate/hook_priority'
Browse files Browse the repository at this point in the history
  • Loading branch information
Archetyped committed Oct 31, 2020
2 parents 1eaeff4 + 5f96f9b commit 08f0a07
Showing 1 changed file with 53 additions and 8 deletions.
61 changes: 53 additions & 8 deletions controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,15 @@ protected function _hooks() {
*/
public function _hooks_init() {
if ( $this->is_enabled() ) {
$priority = $this->util->priority( 'low' );
$cb_hooks_add_last = $this->m( 'hooks_add_last' );

// Init lightbox
add_action( 'wp_footer', $this->m( 'client_footer' ) );
$this->util->add_action( 'footer_script', $this->m( 'client_init' ), 1 );
$this->util->add_filter( 'footer_script', $this->m( 'client_script_media' ), 2 );
// Link activation
add_filter( 'the_content', $this->m( 'activate_links' ), $priority );
add_filter( 'get_post_galleries', $this->m( 'activate_galleries' ), $priority );
add_filter( 'the_content', $cb_hooks_add_last );
add_filter( 'get_post_galleries', $cb_hooks_add_last );
$this->util->add_filter( 'post_process_links', $this->m( 'activate_groups' ), 11 );
$this->util->add_filter( 'validate_uri_regex', $this->m( 'validate_uri_regex_default' ), 1 );
// Content exclusion
Expand All @@ -248,22 +248,67 @@ public function _hooks_init() {
// Widgets
if ( $this->options->get_bool( 'enabled_widget' ) ) {
add_action( 'dynamic_sidebar_before', $this->m( 'widget_process_nested' ) );
add_action( 'dynamic_sidebar', $this->m( 'widget_process_start' ), PHP_INT_MAX );
add_filter( 'dynamic_sidebar_params', $this->m( 'widget_process_inter' ), PHP_INT_MAX );
add_action( 'dynamic_sidebar_after', $this->m( 'widget_process_finish' ), PHP_INT_MAX - 1 );
add_action( 'dynamic_sidebar_after', $this->m( 'widget_process_nested_finish' ), PHP_INT_MAX );
add_action( 'dynamic_sidebar', $cb_hooks_add_last );
add_filter( 'dynamic_sidebar_params', $this->m( 'widget_process_inter' ), 1 );
add_action( 'dynamic_sidebar_after', $cb_hooks_add_last );
add_action( 'dynamic_sidebar_after', $cb_hooks_add_last );
} else {
add_action( 'dynamic_sidebar_before', $this->m( 'widget_block_start' ) );
add_action( 'dynamic_sidebar_after', $this->m( 'widget_block_finish' ) );
}

// Menus
if ( $this->options->get_bool( 'enabled_menu' ) ) {
add_filter( 'wp_nav_menu', $this->m( 'menu_process' ), $priority, 2 );
add_filter( 'wp_nav_menu', $cb_hooks_add_last );
}
}
}

/**
* Adds hook(s) to end of filter/action stack.
*
* @param string $data Optional. Data being filtered.
* @return string Filtered content.
*/
public function hooks_add_last( $data = null ) {
global $wp_filter;

$tag = current_filter();

// Stop processing on invalid hook.
if ( empty( $tag ) || ! is_string( $tag ) ) {
return $data;
}

// Get lowest priority for filter.
$max_priority = max( array_keys( $wp_filter[ $tag ]->callbacks ) ) + 123;

switch ( $tag ) {
case 'the_content':
add_filter( $tag, $this->m( 'activate_links' ), $max_priority );
break;
case 'get_post_galleries':
add_filter( $tag, $this->m( 'activate_galleries' ), $max_priority );
break;
case 'dynamic_sidebar':
add_action( $tag, $this->m( 'widget_process_start' ), $max_priority );
break;
case 'dynamic_sidebar_after':
add_action( $tag, $this->m( 'widget_process_finish' ), $max_priority );
add_action( $tag, $this->m( 'widget_process_nested_finish' ), $max_priority );
break;
case 'wp_nav_menu':
add_filter( $tag, $this->m( 'menu_process' ), $max_priority, 2 );
break;
}

// Remove init hook.
remove_filter( $tag, $this->m( __FUNCTION__ ) );

// Return content (for filters).
return $data;
}

/**
* Add post ID to link group ID
* @uses `SLB::get_group_id` filter
Expand Down

0 comments on commit 08f0a07

Please sign in to comment.