Skip to content

Commit

Permalink
Avoid SortWidgets.php by validating widget containers. Fixes #90 (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
bwmarkle authored Jun 10, 2021
1 parent aede264 commit 1e13128
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ composer require boldgrid/library

## Changelog ##

### 2.13.4 ###

Release date: June 10th, 2021

* Bug Fix: Avoid PHP Error in Dashboard/SortWidgets.php by validating widget containers.

### 2.13.3 ###

Release date: October 13th, 2020
Expand Down
41 changes: 40 additions & 1 deletion src/Library/Dashboard/SortWidgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ public function sortCustomOrder() {
/**
* Sort the widgets as containted in the global $wp_meta_boxes.
*
* We're not actually "sorting". This method will look in our configs and find the BoldGrid widgets
* that we want to appear at the top of the dashboard, and put them there at the top. It does this
* by removing the widget from wherever it is and then adding it to the beginning of the array of
* widgets.
*
* @since 2.9.0
*/
public function sortGlobal() {
Expand All @@ -157,10 +162,44 @@ public function sortGlobal() {
continue;
}

/*
* Avoid the following error: Fatal error: Uncaught Error: Unsupported operand types.
*
* This is a very edge case, and we cannot reproduce it, but we need to avoid the fatal.
* It is caused when we are trying move our metabox to the beginning using:
* $boxes = $our_metabox + $boxes.
*
* Validate $boxes in the above scenario.
*/
if ( ! is_array( $wp_meta_boxes['dashboard'][ $configs['container'] ][ $configs['priority'] ] ) ) {
$wp_meta_boxes['dashboard'][ $configs['container'] ][ $configs['priority'] ] = array();
}

// First, remove the widget from where it is now.
unset( $wp_meta_boxes['dashboard'][$widget['container']][$widget['priority']][$id] );

// Then, add it to the correct location.
/*
* Then, add it to the beginning of the array.
*
* We can't use array_unshift because we don't want the array keys to change.
*
* In the example below, we are setting "boldgrid-notifications" as the first metabox in
* $wp_meta_boxes['dashboard']['normal']['core']. This is how we are making it show atop
* other metaboxes.
*
* Before: Array(
* [dashboard_site_health] => Array,
* [dashboard_right_now] => Array,
* [dashboard_activity] => Array,
* )
*
* After: Array(
* [boldgrid-notifications] => Array,
* [dashboard_site_health] => Array,
* [dashboard_right_now] => Array,
* [dashboard_activity] => Array,
* )
*/
$wp_meta_boxes['dashboard'][ $configs['container'] ][ $configs['priority'] ] =
array( $id => $widget['widget'] ) +
$wp_meta_boxes['dashboard'][ $configs['container'] ][ $configs['priority'] ];
Expand Down
2 changes: 1 addition & 1 deletion src/library.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

return array(
// libraryVersion is used to put the version number on js/css files.
'libraryVersion' => '2.13.3',
'libraryVersion' => '2.13.4',
'api' => 'https://api.boldgrid.com',
'option' => 'license',
'key' => get_site_option( 'boldgrid_api_key', null ),
Expand Down

0 comments on commit 1e13128

Please sign in to comment.