New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve flexibility of context actions #374
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6f8980d
Improve flexibility of context actions
mboynes 56f7a1d
Improve docs, fix bug where an action always fired
mboynes d44e245
Correct misinformation in docs
mboynes 070c5c2
Merge branch 'master' into enhance-context-actions
mboynes b51f4f9
Allow reloading of fm_get_context()
mboynes e4dc652
Test new context actions
mboynes d7a822a
Test context actions
mboynes cbcf00a
phpdoc
mboynes 59bb4cb
Hopefully fix tests in 4.3
mboynes e0afb3f
Change variable name to be more clear
mboynes 8ad9f49
Fix calculate context unit tests
mboynes File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -243,12 +243,15 @@ function _fieldmanager_registry( $var, $val = null ) { | |
* | ||
* @see fm_calculate_context() for detail about the returned array values. | ||
* | ||
* @param bool $force Optional. If true, FM will recalculate the current context. | ||
* This is necessary for testing and perhaps other | ||
* programmatic purposes. | ||
* @return array Contextual information for the current request. | ||
*/ | ||
function fm_get_context() { | ||
function fm_get_context( $force = false ) { | ||
static $calculated_context; | ||
|
||
if ( $calculated_context ) { | ||
if ( ! $force && $calculated_context ) { | ||
return $calculated_context; | ||
} else { | ||
$calculated_context = fm_calculate_context(); | ||
|
@@ -408,30 +411,38 @@ function fm_match_context( $context, $type = null ) { | |
*/ | ||
function fm_trigger_context_action() { | ||
$calculated_context = fm_get_context(); | ||
if ( empty( $calculated_context ) ) { | ||
if ( empty( $calculated_context[0] ) ) { | ||
return; | ||
} | ||
|
||
$context = $calculated_context[0]; | ||
if ( $type = $calculated_context[1] ) { | ||
list( $context, $type ) = $calculated_context; | ||
|
||
if ( $type ) { | ||
/** | ||
* Fires when a specific Fieldmanager context and type load. | ||
* | ||
* The dynamic portions of the hook name, $context and $type, refer to | ||
* the values returned by fm_calculate_context(). For example, the Edit | ||
* screen for the Page post type would fire "fm_post_page". | ||
*/ | ||
do_action( "fm_{$context}_{$type}" ); | ||
} else { | ||
/** | ||
* Fires when a specific Fieldmanager context, but not type, loads. | ||
* | ||
* The dynamic portion of the hook name, $context, refers to the first | ||
* value returned by fm_calculate_context(). For example, the Edit User | ||
* screen would fire "fm_user". | ||
* @param string $type The context subtype, e.g. the post type, taxonomy | ||
* name, submenu option name. | ||
*/ | ||
do_action( "fm_{$context}" ); | ||
do_action( "fm_{$context}_{$type}", $type ); | ||
} | ||
|
||
/** | ||
* Fires when any Fieldmanager context loads. | ||
* | ||
* The dynamic portion of the hook name, $context, refers to the first | ||
* value returned by fm_calculate_context(). For example, the Edit User | ||
* screen would fire "fm_user". | ||
* | ||
* @param string|null $type The context subtype, e.g. the post type, | ||
* taxonomy name, submenu option name. null if this | ||
* context does not have a subtype. | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would note that the new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, thanks! |
||
do_action( "fm_{$context}", $type ); | ||
} | ||
add_action( 'init', 'fm_trigger_context_action', 99 ); | ||
|
||
|
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 this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me,
$force
reads as if you're forcing FM to return something when it otherwise wouldn't, which isn't quite what's happening, if I'm following correctly. Maybe FM could do with a dedicatedclear
orrecalculate
workflow, or a way to get a new calculated context independently of the static one? (I realize this comment is coming a little late in the 1.0 cycle, though.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good feedback. I think in the short term, I'll just rename this to
$recalculate
.