Automattic\WooCommerce\Internal\Features

FeaturesController::enqueue_script_to_fix_plugin_list_htmlpublicWC 1.0

Fix for the HTML of the plugins list when there are feature-plugin incompatibility warnings.

WordPress renders the plugin information rows in the plugins page in <tr> elements as follows:

  • If the plugin needs update, the <tr> will have an "update" class. This will prevent the lower border line to be drawn. Later an additional <tr> with an "update available" warning will be rendered, it will have a "plugin-update-tr" class which will draw the missing lower border line.
  • Otherwise, the <tr> will be already drawn with the lower border line.

This is a problem for our rendering of the "plugin is incompatible with X features" warning:

  • If the plugin info <tr> has "update", our <tr> will render nicely right after it; but then our own "plugin-update-tr" class will draw an additional line before the "needs update" warning.
  • If not, the plugin info <tr> will render its lower border line right before our compatibility info <tr>.

This small script fixes this by adding the "update" class to the plugin info <tr> if it doesn't have it (so no extra line before our <tr>), or removing 'plugin-update-tr' from our <tr> otherwise (and then some extra manual tweaking of margins is needed).

Method of the class: FeaturesController{}

No Hooks.

Returns

null. Nothing (null).

Usage

$FeaturesController = new FeaturesController();
$FeaturesController->enqueue_script_to_fix_plugin_list_html( $current_screen ): void;
$current_screen(string) (required)
The current screen object.

FeaturesController::enqueue_script_to_fix_plugin_list_html() code WC 10.8.1

public function enqueue_script_to_fix_plugin_list_html( $current_screen ): void {
	if ( 'plugins' !== $current_screen->id ) {
		return;
	}

	$handle = 'wc-features-fix-plugin-list-html';
	wp_register_script( $handle, '', array(), WC_VERSION, array( 'in_footer' => true ) );
	wp_enqueue_script( $handle );
	wp_add_inline_script(
		$handle,
		"
            const warningRows = document.querySelectorAll('tr[data-plugin-row-type=\"feature-incomp-warn\"]');
            for(const warningRow of warningRows) {
                const pluginName = warningRow.getAttribute('data-plugin');
                const pluginInfoRow = document.querySelector('tr.active[data-plugin=\"' + pluginName + '\"]:not(.plugin-update-tr), tr.inactive[data-plugin=\"' + pluginName + '\"]:not(.plugin-update-tr)');
                if(!pluginInfoRow) {
                    continue;
                }
                if(pluginInfoRow.classList.contains('update')) {
                    warningRow.classList.remove('plugin-update-tr');
                    warningRow.querySelector('.notice').style.margin = '5px 10px 15px 30px';
                }
                else {
                    pluginInfoRow.classList.add('update');
                }
            }
            "
	);
}