| 564 | | // Filters: these are the core of WP's plugin architecture |
|---|
| 565 | | |
|---|
| 566 | | function merge_filters($tag) { |
|---|
| 567 | | global $wp_filter; |
|---|
| 568 | | if ( isset($wp_filter['all']) ) { |
|---|
| 569 | | foreach ($wp_filter['all'] as $priority => $functions) { |
|---|
| 570 | | if ( isset($wp_filter[$tag][$priority]) ) |
|---|
| 571 | | $wp_filter[$tag][$priority] = array_merge($wp_filter['all'][$priority], $wp_filter[$tag][$priority]); |
|---|
| 572 | | else |
|---|
| 573 | | $wp_filter[$tag][$priority] = array_merge($wp_filter['all'][$priority], array()); |
|---|
| 574 | | $wp_filter[$tag][$priority] = array_unique($wp_filter[$tag][$priority]); |
|---|
| 575 | | } |
|---|
| 576 | | } |
|---|
| 577 | | |
|---|
| 578 | | if ( isset($wp_filter[$tag]) ) |
|---|
| 579 | | ksort( $wp_filter[$tag] ); |
|---|
| 580 | | } |
|---|
| 581 | | |
|---|
| 582 | | function apply_filters($tag, $string) { |
|---|
| 583 | | global $wp_filter; |
|---|
| 584 | | |
|---|
| 585 | | $args = array_slice(func_get_args(), 2); |
|---|
| 586 | | |
|---|
| 587 | | merge_filters($tag); |
|---|
| 588 | | |
|---|
| 589 | | if ( !isset($wp_filter[$tag]) ) { |
|---|
| 590 | | return $string; |
|---|
| 591 | | } |
|---|
| 592 | | foreach ($wp_filter[$tag] as $priority => $functions) { |
|---|
| 593 | | if ( !is_null($functions) ) { |
|---|
| 594 | | foreach($functions as $function) { |
|---|
| 595 | | |
|---|
| 596 | | $all_args = array_merge(array($string), $args); |
|---|
| 597 | | $function_name = $function['function']; |
|---|
| 598 | | $accepted_args = $function['accepted_args']; |
|---|
| 599 | | |
|---|
| 600 | | if ( $accepted_args == 1 ) |
|---|
| 601 | | $the_args = array($string); |
|---|
| 602 | | elseif ( $accepted_args > 1 ) |
|---|
| 603 | | $the_args = array_slice($all_args, 0, $accepted_args); |
|---|
| 604 | | elseif ( $accepted_args == 0 ) |
|---|
| 605 | | $the_args = NULL; |
|---|
| 606 | | else |
|---|
| 607 | | $the_args = $all_args; |
|---|
| 608 | | |
|---|
| 609 | | $string = call_user_func_array($function_name, $the_args); |
|---|
| 610 | | } |
|---|
| 611 | | } |
|---|
| 612 | | } |
|---|
| 613 | | return $string; |
|---|
| 614 | | } |
|---|
| 615 | | |
|---|
| 616 | | function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) { |
|---|
| 617 | | global $wp_filter; |
|---|
| 618 | | |
|---|
| 619 | | // check that we don't already have the same filter at the same priority |
|---|
| 620 | | if ( isset($wp_filter[$tag]["$priority"]) ) { |
|---|
| 621 | | foreach($wp_filter[$tag]["$priority"] as $filter) { |
|---|
| 622 | | // uncomment if we want to match function AND accepted_args |
|---|
| 623 | | // if ( $filter == array($function, $accepted_args) ) { |
|---|
| 624 | | if ( $filter['function'] == $function_to_add ) { |
|---|
| 625 | | return true; |
|---|
| 626 | | } |
|---|
| 627 | | } |
|---|
| 628 | | } |
|---|
| 629 | | |
|---|
| 630 | | // So the format is wp_filter['tag']['array of priorities']['array of ['array (functions, accepted_args)]'] |
|---|
| 631 | | $wp_filter[$tag]["$priority"][] = array('function'=>$function_to_add, 'accepted_args'=>$accepted_args); |
|---|
| 632 | | return true; |
|---|
| 633 | | } |
|---|
| 634 | | |
|---|
| 635 | | function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) { |
|---|
| 636 | | global $wp_filter; |
|---|
| 637 | | |
|---|
| 638 | | // rebuild the list of filters |
|---|
| 639 | | if ( isset($wp_filter[$tag]["$priority"]) ) { |
|---|
| 640 | | $new_function_list = array(); |
|---|
| 641 | | foreach($wp_filter[$tag]["$priority"] as $filter) { |
|---|
| 642 | | if ( $filter['function'] != $function_to_remove ) { |
|---|
| 643 | | $new_function_list[] = $filter; |
|---|
| 644 | | } |
|---|
| 645 | | } |
|---|
| 646 | | $wp_filter[$tag]["$priority"] = $new_function_list; |
|---|
| 647 | | } |
|---|
| 648 | | return true; |
|---|
| 649 | | } |
|---|
| 650 | | |
|---|
| 651 | | // The *_action functions are just aliases for the *_filter functions, they take special strings instead of generic content |
|---|
| 652 | | |
|---|
| 653 | | function do_action($tag, $arg = '') { |
|---|
| 654 | | global $wp_filter; |
|---|
| 655 | | $extra_args = array_slice(func_get_args(), 2); |
|---|
| 656 | | if ( is_array($arg) ) |
|---|
| 657 | | $args = array_merge($arg, $extra_args); |
|---|
| 658 | | else |
|---|
| 659 | | $args = array_merge(array($arg), $extra_args); |
|---|
| 660 | | |
|---|
| 661 | | merge_filters($tag); |
|---|
| 662 | | |
|---|
| 663 | | if ( !isset($wp_filter[$tag]) ) { |
|---|
| 664 | | return; |
|---|
| 665 | | } |
|---|
| 666 | | foreach ($wp_filter[$tag] as $priority => $functions) { |
|---|
| 667 | | if ( !is_null($functions) ) { |
|---|
| 668 | | foreach($functions as $function) { |
|---|
| 669 | | |
|---|
| 670 | | $function_name = $function['function']; |
|---|
| 671 | | $accepted_args = $function['accepted_args']; |
|---|
| 672 | | |
|---|
| 673 | | if ( $accepted_args == 1 ) { |
|---|
| 674 | | if ( is_array($arg) ) |
|---|
| 675 | | $the_args = $arg; |
|---|
| 676 | | else |
|---|
| 677 | | $the_args = array($arg); |
|---|
| 678 | | } elseif ( $accepted_args > 1 ) { |
|---|
| 679 | | $the_args = array_slice($args, 0, $accepted_args); |
|---|
| 680 | | } elseif ( $accepted_args == 0 ) { |
|---|
| 681 | | $the_args = NULL; |
|---|
| 682 | | } else { |
|---|
| 683 | | $the_args = $args; |
|---|
| 684 | | } |
|---|
| 685 | | |
|---|
| 686 | | $string = call_user_func_array($function_name, $the_args); |
|---|
| 687 | | } |
|---|
| 688 | | } |
|---|
| 689 | | } |
|---|
| 690 | | } |
|---|
| 691 | | |
|---|
| 692 | | function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) { |
|---|
| 693 | | add_filter($tag, $function_to_add, $priority, $accepted_args); |
|---|
| 694 | | } |
|---|
| 695 | | |
|---|
| 696 | | function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) { |
|---|
| 697 | | remove_filter($tag, $function_to_remove, $priority, $accepted_args); |
|---|
| 698 | | } |
|---|
| 699 | | |
|---|