Skip to content

Commit c204cea

Browse files
Modules: Ignore non-string module tags (#53848)
1 parent dfcfdd4 commit c204cea

3 files changed

Lines changed: 44 additions & 4 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: bugfix
3+
4+
Modules: Fix a fatal error on the Modules screen when the tag filter in the address is not plain text.

‎projects/plugins/jetpack/class.jetpack-modules-list-table.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ public function views() {
346346
$views = $this->get_views();
347347

348348
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is a view, not a model or controller.
349-
$module_tag = empty( $_GET['module_tag'] ) ? 'all' : sanitize_title( wp_unslash( $_GET['module_tag'] ) );
349+
$module_tag = empty( $_GET['module_tag'] ) || ! is_string( $_GET['module_tag'] ) ? 'all' : sanitize_title( wp_unslash( $_GET['module_tag'] ) );
350350

351351
echo "<ul class='subsubsub'>\n";
352352
foreach ( $views as $class => $view ) {
@@ -380,7 +380,7 @@ public function filter_displayed_table_items( $modules ) {
380380
public static function is_module_displayed( $module ) {
381381
// Handle module tag based filtering.
382382
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is a view, not a model or controller.
383-
if ( ! empty( $_REQUEST['module_tag'] ) ) {
383+
if ( ! empty( $_REQUEST['module_tag'] ) && is_string( $_REQUEST['module_tag'] ) ) {
384384
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is a view, not a model or controller.
385385
$module_tag = sanitize_text_field( wp_unslash( $_REQUEST['module_tag'] ) );
386386
if ( ! in_array( $module_tag, array_map( 'jetpack_get_module_i18n_tag', $module['module_tags'] ), true ) ) {

‎projects/plugins/jetpack/tests/php/Get_Modules_Test.php‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Test module related methods in Jetpack and Jetpack_Admin class.
3+
* Test module discovery, administration, and list-table filtering.
44
*
55
* @package jetpack
66
*/
@@ -9,12 +9,16 @@
99
use PHPUnit\Framework\Attributes\CoversClass;
1010
use PHPUnit\Framework\Attributes\DataProvider;
1111

12+
require_once JETPACK__PLUGIN_DIR . 'class.jetpack-modules-list-table.php';
13+
1214
/**
13-
* Test module related methods in Jetpack and Jetpack_Admin class.
15+
* Test module discovery, administration, and list-table filtering.
1416
*
1517
* @covers \Jetpack_Admin
18+
* @covers \Jetpack_Modules_List_Table
1619
*/
1720
#[CoversClass( Jetpack_Admin::class )]
21+
#[CoversClass( Jetpack_Modules_List_Table::class )]
1822
class Get_Modules_Test extends WP_UnitTestCase {
1923
use \Automattic\Jetpack\PHPUnit\WP_UnitTestCase_Fix;
2024

@@ -255,4 +259,36 @@ public function test_get_module_valid_module() {
255259
public function test_get_module_module_no_info() {
256260
$this->assertFalse( Jetpack::get_module( 'module-extras' ) );
257261
}
262+
263+
/**
264+
* Ignore an array tag in the view selector.
265+
*/
266+
public function test_array_module_tag_selects_all_view() {
267+
$original_get = $_GET;
268+
try {
269+
$_GET['module_tag'] = array( 'x' );
270+
$table = $this->getMockBuilder( Jetpack_Modules_List_Table::class )
271+
->disableOriginalConstructor()
272+
->onlyMethods( array( 'get_views' ) )
273+
->getMock();
274+
$table->expects( $this->once() )->method( 'get_views' )->willReturn( array( 'all' => 'All' ) );
275+
$this->expectOutputRegex( "/\A<ul class='subsubsub'>\s*<li class='all current'>All<\/li>\s*<\/ul>\z/" );
276+
$table->views();
277+
} finally {
278+
$_GET = $original_get;
279+
}
280+
}
281+
282+
/**
283+
* Ignore an array tag in the module filter.
284+
*/
285+
public function test_array_module_tag_does_not_filter_modules() {
286+
$original_request = $_REQUEST;
287+
try {
288+
$_REQUEST['module_tag'] = array( 'x' );
289+
$this->assertTrue( Jetpack_Modules_List_Table::is_module_displayed( array( 'module_tags' => array( 'Social' ) ) ) );
290+
} finally {
291+
$_REQUEST = $original_request;
292+
}
293+
}
258294
}

0 commit comments

Comments
 (0)