WordPress.org

Make WordPress Core

Opened 7 months ago

Closed 7 months ago

Last modified 5 months ago

#46836 closed defect (bug) (fixed)

Site Health: Allow any callable for a direct site health test.

Reported by: kraftbj Owned by: SergeyBiryukov
Milestone: 5.2 Priority: normal
Severity: normal Version:
Component: Site Health Keywords: has-patch site-health
Focuses: Cc:
PR Number:

Description

When trying to extend the site health suite of tests, we should allow any callable to return test results for direct tests (async tests still need to be a string for the ajax action).

Being able to do this would be helpful:

add_filter( 'site_status_tests', 'debugger_site_status_tests' );
function debugger_site_status_tests( $tests ) {
	$tests['direct']['callable'] = array(
		'label' => 'Example Test',
		'test' => function(){
			return array(
				'label'       => __( 'Example worked.),
				'status'      => 'good',
				'badge'       => array(
					'label' => __( 'Testing'),
					'color' => 'green',
				),
				'description' => sprintf(
					'<p>%s</p>',
					__( "For direct tests, any callable works." )
				),
				'actions'     => '',
				'test'        => 'callable_test',
			);
		}
	);

	return $tests;
}

See #46573

Attachments (1)

46836.diff (2.0 KB) - added by kraftbj 7 months ago.

Download all attachments as: .zip

Change History (9)

@kraftbj
7 months ago

#1 @kraftbj
7 months ago

Real-life example: https://github.com/Automattic/jetpack/pull/11976

In this case, we already have an existing testing suite that we want to add results of to the Site Health check. Each individual test is a separate protected method of a class and our process for adding new tests is simply add a new test__ prefixed-function in a file.

The current implementation makes it difficult since we would need a globally-scoped function for each test (unless I'm missing something a bit creative).

If we allow any callable, we could either pass an class/method array in the individual test was public or, within the filtering function, dynamically run each test.

e.g.

function jetpack_add_tests( $core_tests ) {
	$suite = new Jetpack_Cxn_Tests();
	$tests = $suite->list_tests(); // array
	foreach ( $tests as $test ) {
		$core_tests[] = array(
			'label' => $test['label'],
			...
			'test' => function() use ($test, $suite ) {
				$result = $suite->run_test( $test['name'] );
				return array(... ); // dynamic mapping of our result format to Core's
			},
		);
	}
	return $core_tests;
}
Last edited 7 months ago by kraftbj (previous) (diff)

This ticket was mentioned in Slack in #core-php by kraft. View the logs.


7 months ago

#3 @SergeyBiryukov
7 months ago

  • Keywords site-health added

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


7 months ago

#5 @kraftbj
7 months ago

Seeing the Slack mention of punting this to 5.3. I would be really nice to get this in with the initial release of the feature so plugin developers can utilize callables with their first integration into Site Health instead of needing to special version control.

With this patch, if a developer wants to use a callable, they can do so only with the filter. If this is punted to 5.3, they would have to do extra checks since the presence of the filter is not enough to determine what they can use in the tests array item.

I'm fine owning the ticket, but still would need a committer willing to commit. :)

#6 @SergeyBiryukov
7 months ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 45234:

Site Health: Allow any callable added via site_status_tests filter to return test results for direct tests.

Async tests still need to be a string for the AJAX action.

Props kraftbj.
Fixes #46836.

This ticket was mentioned in Slack in #core by chanthaboune. View the logs.


7 months ago

#8 @spacedmonkey
5 months ago

  • Component changed from Administration to Site Health
Note: See TracTickets for help on using tickets.