Skip to content

Commit

Permalink
Chunk up to enable unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kasparsd committed Jan 5, 2020
1 parent f2ce1b6 commit cc1245d
Showing 1 changed file with 54 additions and 26 deletions.
80 changes: 54 additions & 26 deletions src/WidgetContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,63 @@ function maybe_unset_widgets_by_context( $sidebars_widgets ) {
return $sidebars_widgets;
}


function check_widget_visibility( $widget_id ) {
// Check if this widget even has context set
/**
* Determine widget visibility according to the current global context.
*
* @param string $widget_id Widget ID.
*
* @return boolean
*/
public function check_widget_visibility( $widget_id ) {
// Check if this widget even has context set.
if ( ! isset( $this->context_options[ $widget_id ] ) ) {
return true;
}

// Get the match rule for this widget (show/hide/selected/notselected).
$match_rule = $this->context_options[ $widget_id ]['incexc']['condition'];

// Force show or hide the widget!
if ( 'show' === $match_rule ) {
return true;
} elseif ( 'hide' === $match_rule ) {
return false;
}

return $this->context_matches_condition_for_widget_id(
$widget_id,
( 'selected' === $match_rule )
);
}

/**
* If widget context rules match the selected conditional (regular or inverted).
*
* @param string $widget_id Widget ID.
* @param boolean $condition Regular or inverted logic.
*
* @return boolean
*/
public function context_matches_condition_for_widget_id( $widget_id, $condition ) {
$matches = $this->context_matches_for_widget_id( $widget_id );

if ( $condition && in_array( true, $matches, true ) ) {
return true;
} elseif ( ! $condition && ! in_array( true, $matches, true ) ) {
return true;
}

return false;
}

/**
* Get context rule matches for a widget ID.
*
* @param string $widget_id Widget ID.
*
* @return array
*/
public function context_matches_for_widget_id( $widget_id ) {
$matches = array();

foreach ( $this->get_contexts() as $context_id => $context_settings ) {
Expand All @@ -321,29 +371,7 @@ function check_widget_visibility( $widget_id ) {
);
}

// Get the match rule for this widget (show/hide/selected/notselected)
$match_rule = $this->context_options[ $widget_id ]['incexc']['condition'];

// Force show or hide the widget!
if ( 'show' === $match_rule ) {
return true;
} elseif ( 'hide' === $match_rule ) {
return false;
}

$inc = false;

if ( 'selected' === $match_rule ) {
$inc = true;
}

if ( $inc && in_array( true, $matches, true ) ) {
return true;
} elseif ( ! $inc && ! in_array( true, $matches, true ) ) {
return true;
}

return false;
return $matches;
}


Expand Down

0 comments on commit cc1245d

Please sign in to comment.