Skip to content

Commit edc4c3b

Browse files
minor defensive coding improvements
1 parent 4c05c14 commit edc4c3b

3 files changed

Lines changed: 11 additions & 6 deletions

File tree

src/admin/class-admin.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,9 @@ public function flush_rules($post_id)
734734
foreach ($boxes as $box) {
735735
// get box meta data
736736
$box_meta = get_post_meta($box->ID, 'boxzilla_options', true);
737+
if (! $box_meta || ! is_array($box_meta) || empty($box_meta['rules'])) {
738+
continue;
739+
}
737740

738741
// add box rules to all rules
739742
$rules[ $box->ID ] = (array) $box_meta['rules'];

src/admin/class-menu.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22

33
namespace Boxzilla\Admin;
44

5-
use Boxzilla\Plugin;
6-
use Boxzilla\Box;
7-
use Boxzilla\Boxzilla;
8-
95
class Menu
106
{
11-
public function init()
7+
public function init(): void
128
{
139
add_action('admin_head-nav-menus.php', [ $this, 'add_nav_menu_meta_boxes' ]);
1410

@@ -48,6 +44,9 @@ private function get_boxes()
4844
public function nav_menu_links()
4945
{
5046
$posts = $this->get_boxes();
47+
if (count($posts) === 0) {
48+
return;
49+
}
5150

5251
?>
5352
<div id="posttype-boxzilla-boxes" class="posttypediv">

src/class-box.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@ class Box
4141
*/
4242
public function __construct($post)
4343
{
44-
4544
// fetch post if it hasn't been fetched yet
4645
if (! $post instanceof WP_Post) {
4746
$post = get_post($post);
47+
48+
if ($post === null) {
49+
throw new \InvalidArgumentException('No post found with ID ' . $post);
50+
}
4851
}
4952

5053
// store ref to post

0 commit comments

Comments
 (0)