Brain-WP/Cortex

Accessing $matches within before or after callbacks

chrisvanpatten opened this issue · 8 comments

Hi there -

Really loving this library. Definitely the smoothest routing utility for WordPress that I've found!

I'm trying to figure out how to access the captured $matches within the before/after callbacks. Right now, as far as I can tell, you can only access it in the $queryBuilder callable.

It's very possible I'm missing something obvious… if not, consider this a feature request :)

Thanks!

Hi @chrisvanpatten

Thanks for your interest in Cortex.

I don't know if you noticed that version 0.* (that at the moment lives on "master" branch) is pretty different that version 1.*. (that at the moment lives in the "refactoring-fastroute" branch).

To help you I need to know which version are you using.

Thanks

Using 1.*, and loving it!

It doesn't seem to merge in the $matches array… here's what I've got:

dev-directories_ecommercefuel_com_myroute_word_

I haven't touched the merge_query_string setting at all - left it at the default.

Oh, yes, you right. When using a query route, the route vars is a callback that get called passing matches... and yes, in that case they can't be get inside "before" / "after" callbacks.

I consider this a feature request I will do as soon as I can.

Awesome, thanks! I found a temporary workaround for my use-case, but having it baked in like this would be super nice.

Appreciate the quick response! Looking forward to using this more.

So I have pushed a new branch "issue-12" that should fix this.

Note that I need to fix unit tests, now they are failing for this branch because of the changes, however it should work.

With changes introduced in this branch "before" and "after" callbacks receives 4 arguments:

  • $vars
  • $wp
  • $template
  • $matches

Where:

  • $vars depends on the route. E.g. For query routes it is the array returned by query route callback. Note that this array passes throught the filter cortex.matched-vars that may alter it. In other cases, this array may be identical to $matches (4th argument).
  • $wp is the current instance of $wp object
  • $template is the template that has been set in the route, if any.
  • $matches is an array created merging:
    • the $matches array from the route regex
    • any query vars in the URL (unless merge_query_string route setting is false)

Thanks! This is working brilliantly.

@chrisvanpatten Thanks! I hope I will find the time today to write some tests, merge and release new version.

Thanks to you for your interest and your issue.

@chrisvanpatten this have been merged and included in v1.0.0-alpha.7.

Thanks for your contribute.