Skip to content

Commit 2b69182

Browse files
committed
Refactor to cater for falsey get_plugin() return value.
Rollback license field changes. Check for falsey rather than is_array Drop Resource type declaration License field refactor Additional false handling Modified unit test
1 parent fe4aa43 commit 2b69182

File tree

5 files changed

+67
-15
lines changed

5 files changed

+67
-15
lines changed

src/Uplink/Admin/License_Field.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public function register_settings() {
3030
$collection = Config::get_container()->get( Collection::class );
3131
$plugin = $collection->current();
3232

33+
if ( ! $plugin ) {
34+
return;
35+
}
36+
3337
add_settings_section(
3438
sprintf( '%s_%s', self::LICENSE_FIELD_ID, sanitize_title( $plugin->get_slug() ) ),
3539
'',
@@ -78,8 +82,14 @@ public function get_field_html( Plugin $plugin ) : string {
7882
* @inheritDoc
7983
*/
8084
public function render( bool $show_title = true, bool $show_button = true ) {
85+
$plugin = $this->get_plugin();
86+
87+
if ( ! $plugin ) {
88+
return;
89+
}
90+
8191
echo $this->get_content( [
82-
'plugin' => $this->get_plugin(),
92+
'plugin' => $plugin,
8393
'show_title' => $show_title,
8494
'show_button' => $show_button,
8595
] );
@@ -91,8 +101,14 @@ public function render( bool $show_title = true, bool $show_button = true ) {
91101
* @return void
92102
*/
93103
public function enqueue_assets() {
104+
$plugin = $this->get_plugin();
105+
106+
if ( ! $plugin ) {
107+
return;
108+
}
109+
94110
$handle = sprintf( 'stellarwp-uplink-license-admin-%s', Config::get_hook_prefix() );
95-
$path = preg_replace( '/.*\/vendor/', plugin_dir_url( $this->get_plugin()->get_path() ) . 'vendor', dirname( __DIR__, 2 ) );
111+
$path = preg_replace( '/.*\/vendor/', plugin_dir_url( $plugin->get_path() ) . 'vendor', dirname( __DIR__, 2 ) );
96112
$js_src = apply_filters( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/admin_js_source', $path . '/assets/js/key-admin.js' );
97113
wp_register_script( $handle, $js_src, [ 'jquery' ], '1.0.0', true );
98114
wp_enqueue_script( $handle );

src/Uplink/Admin/Plugins_Page.php

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@ public function display_plugin_messages( string $page ) {
3131
return;
3232
}
3333

34-
$messages = [];
35-
$plugin_file = $this->get_plugin()->get_path();
34+
$messages = [];
35+
$plugin = $this->get_plugin();
36+
37+
if ( ! $plugin ) {
38+
return;
39+
}
40+
41+
$plugin_file = $plugin->get_path();
3642
$plugin_updates = get_plugin_updates();
3743
$resource = $plugin_updates[ $plugin_file ] ?? null;
3844

@@ -72,7 +78,7 @@ public function display_plugin_messages( string $page ) {
7278
} else {
7379
$update_message = sprintf(
7480
esc_html__( 'There is a new version of %1$s available. %2$s', '%TEXTDOMAIN%' ),
75-
$this->get_plugin()->get_name(),
81+
$plugin->get_name(),
7682
$update_now_link
7783
);
7884
}
@@ -103,7 +109,7 @@ public function display_plugin_messages( string $page ) {
103109
);
104110

105111
$this->plugin_notice = [
106-
'slug' => $this->get_plugin()->get_slug(),
112+
'slug' => $plugin->get_slug(),
107113
'message_row_html' => $message_row_html,
108114
];
109115
}
@@ -142,7 +148,13 @@ public function store_admin_notices( string $page ) {
142148
* @return void
143149
*/
144150
public function output_notices_script() {
145-
$slug = $this->get_plugin()->get_slug();
151+
$plugin = $this->get_plugin();
152+
153+
if ( ! $plugin ) {
154+
return;
155+
}
156+
157+
$slug = $plugin->get_slug();
146158
$notice = $this->get_plugin_notice();
147159

148160
if ( empty( $notice ) ) {
@@ -204,7 +216,13 @@ public function get_plugin_message( Plugin $resource ) {
204216
* @return void
205217
*/
206218
public function remove_default_inline_update_msg() {
207-
remove_action( "after_plugin_row_{$this->get_plugin()->get_path()}", 'wp_plugin_update_row' );
219+
$plugin = $this->get_plugin();
220+
221+
if ( ! $plugin ) {
222+
return;
223+
}
224+
225+
remove_action( "after_plugin_row_{$plugin->get_path()}", 'wp_plugin_update_row' );
208226
}
209227

210228
/**
@@ -214,7 +232,13 @@ public function remove_default_inline_update_msg() {
214232
*/
215233
public function check_for_updates( $transient ) {
216234
try {
217-
return $this->get_plugin()->check_for_updates( $transient );
235+
$plugin = $this->get_plugin();
236+
237+
if ( ! $plugin ) {
238+
return $transient;
239+
}
240+
241+
return $plugin->check_for_updates( $transient );
218242
} catch ( \Throwable $exception ) {
219243
return $transient;
220244
}
@@ -242,13 +266,19 @@ protected function get_plugin() {
242266
* @return mixed
243267
*/
244268
public function inject_info( $result, string $action = null, $args = null ) {
245-
$relevant = ( 'plugin_information' === $action ) && is_object( $args ) && isset( $args->slug ) && ( $args->slug === $this->get_plugin()->get_slug() );
269+
$plugin = $this->get_plugin();
270+
271+
if ( ! $plugin ) {
272+
return $result;
273+
}
274+
275+
$relevant = ( 'plugin_information' === $action ) && is_object( $args ) && isset( $args->slug ) && ( $args->slug === $plugin->get_slug() );
246276

247277
if ( ! $relevant ) {
248278
return $result;
249279
}
250280

251-
$plugin_info = $this->get_plugin()->validate_license();
281+
$plugin_info = $plugin->validate_license();
252282

253283
if ( $plugin_info ) {
254284
return $plugin_info->to_wp_format();

src/Uplink/Resources/Collection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ public function add( Resource $resource ) {
3030
}
3131

3232
/**
33-
* @return Resource
33+
* @return Resource|bool
3434
*/
3535
#[\ReturnTypeWillChange]
36-
public function current(): Resource {
37-
return current( $this->resources );
36+
public function current() {
37+
return ( ! $this->resources ) ? current( $this->resources ) : false;
3838
}
3939

4040
/**

src/Uplink/Resources/Filters/Path_FilterIterator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public function __construct( \Iterator $iterator, array $paths ) {
3232
public function accept(): bool {
3333
$resource = $this->getInnerIterator()->current();
3434

35+
if ( ! $resource ) {
36+
return false;
37+
}
38+
3539
return in_array( $resource->get_path(), $this->paths, true );
3640
}
3741

tests/wpunit/Resources/CollectionTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ public function it_should_register_resources() {
105105
*/
106106
public function it_should_loop_over_resources() {
107107
foreach ( $this->collection as $resource ) {
108-
$this->assertInstanceOf( Uplink_Resources\Resource::class, $resource );
108+
if ( $resource ) {
109+
$this->assertInstanceOf( Uplink_Resources\Resource::class, $resource );
110+
}
109111
}
110112
}
111113
}

0 commit comments

Comments
 (0)