function testBodyClassCallback()
 {
     $rule = new HM_Rewrite_Rule('^foo$');
     $called = false;
     $rule->add_body_class_callback(function () use(&$called) {
         $called = true;
     });
     $rule->matched_rule();
     apply_filters('body_class', array());
     $this->assertTrue($called);
 }
Example #2
0
/**
 * The main wrapper function for the HM_Rewrite API. Use this to add new rewrite rules
 *
 * Create a new rewrite with the arguments listed below. The only required argument is 'regex'
 *
 * @param  string 		$regex 		The rewrite regex, start / end delimter not required. Eg: '^people/([^/]+)/?'
 * @param  mixed 		$query 		The WP_Query args to be used on this "page"
 * @param  string 		$template 	The template file used to render the request. Not required, will use
 *                             		the template file for the WP_Query if not set. Relative to template_directory() or absolute.
 * @param  string 		$body_class A class to be added to body_class in the rendered template
 * @param  function 	$body_class_callback A callback that will be hooked into body_class
 * @param  function 	$request_callback A callback that will be called as soon as the request matching teh regex is hit.
 *                                     		called with the WP object
 * @param  function 	$parse_query_callback A callback taht will be hooked into 'parse_query'. Use this to modify query vars
 *                                         	  in the main WP_Query
 * @param  function 	$title_callback A callback that will be hooked into wp_title
 * @param  function 	$query_callback A callback taht will be called once the WP_Query has finished. Use to overrite any
 *                                   	annoying is_404, is_home etc that you custom query may not match to.
 * @param  function 	$access_rule An access rule for restriciton to logged-in-users only for example.
 * @param  array 		$request_methods An array of request methods, e.g. PUT, POST
 */
function hm_add_rewrite_rule($args = array())
{
    // backwards compat
    if (count(func_get_args()) > 1 && is_string($args)) {
        $args = array();
        $args['regex'] = func_get_arg(0);
        $args['query'] = func_get_arg(1);
        if (count(func_get_args()) > 2) {
            $args['template'] = func_get_arg(2);
        }
        if (count(func_get_args()) > 3) {
            $args = array_merge($args, func_get_arg(3));
        }
    }
    if (!empty($args['rewrite'])) {
        $args['regex'] = $args['rewrite'];
    }
    $regex = $args['regex'];
    $rule = new HM_Rewrite_Rule($regex, isset($args['id']) ? $args['id'] : null);
    if (!empty($args['template'])) {
        $rule->set_template($args['template']);
    }
    if (!empty($args['body_class_callback'])) {
        $rule->add_body_class_callback($args['body_class_callback']);
    }
    if (!empty($args['request_callback'])) {
        $rule->add_request_callback($args['request_callback']);
    }
    if (!empty($args['parse_query_callback'])) {
        $rule->add_parse_query_callback($args['parse_query_callback']);
    }
    if (!empty($args['title_callback'])) {
        $rule->add_title_callback($args['title_callback']);
    }
    if (!empty($args['query_callback'])) {
        $rule->add_query_callback($args['query_callback']);
    }
    if (!empty($args['access_rule'])) {
        $rule->set_access_rule($args['access_rule']);
    }
    if (!empty($args['query'])) {
        $rule->set_wp_query_args($args['query']);
    }
    if (!empty($args['permission'])) {
        $rule->set_access_rule($args['permission']);
    }
    if (!empty($args['admin_bar_callback'])) {
        $rule->add_admin_bar_callback($args['admin_bar_callback']);
    }
    if (!empty($args['disable_canonical'])) {
        $rule->disable_canonical = true;
    }
    // some convenenience properties. These are done here because they are not very nice
    if (!empty($args['body_class'])) {
        $rule->add_body_class_callback(function ($classes) use($args) {
            $classes[] = $args['body_class'];
            return $classes;
        });
    }
    if (!empty($args['parse_query_properties'])) {
        $rule->add_parse_query_callback(function (WP_Query $query) use($args) {
            $args['parse_query_properties'] = wp_parse_args($args['parse_query_properties']);
            foreach ($args['parse_query_properties'] as $property => $value) {
                $query->{$property} = $value;
            }
        });
    }
    if (!empty($args['post_query_properties'])) {
        $rule->add_query_callback(function (WP_Query $query) use($args) {
            $args['post_query_properties'] = wp_parse_args($args['post_query_properties']);
            foreach ($args['post_query_properties'] as $property => $value) {
                $query->{$property} = $value;
            }
        });
    }
    if (!empty($args['request_methods'])) {
        $rule->set_request_methods($args['request_methods']);
    }
    if (!empty($args['request_method'])) {
        $rule->set_request_methods(array($args['request_method']));
    }
    HM_Rewrite::add_rule($rule);
}