Automattic\WooCommerce\Internal\Admin

Analytics::output_refund_fix_tool_js │ public │ WC 10.8.0

Output the inline script that injects a "Check" button into the full refund fix tool row on the WooCommerce > Status > Tools page.

Метод класса: Analytics{}

Хуков нет.

Возвращает

void. Ничего (null).

Использование

$Analytics = new Analytics();
$Analytics->output_refund_fix_tool_js(): void;

Список изменений

С версии 10.8.0 Введена.

Код Analytics::output_refund_fix_tool_js() WC 11.2.2

<?php
public function output_refund_fix_tool_js(): void {
	// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verified by WooCommerce tools framework.
	if ( ! isset( $_GET['page'], $_GET['tab'] ) || 'wc-status' !== $_GET['page'] || 'tools' !== $_GET['tab'] ) {
		return;
	}
	// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verified by WooCommerce tools framework.
	if ( isset( $_GET['wc_refund_fix_action'] ) && 'disable' === sanitize_key( $_GET['wc_refund_fix_action'] ) ) {
		return;
	}

	$tool_class         = self::FULL_REFUND_FIX_DATA_TOOL_ID;
	$nonce              = wp_create_nonce( 'woocommerce_refund_fix_check' );
	$ajax_url           = admin_url( 'admin-ajax.php' );
	$label_check        = __( 'Check', 'woocommerce' );
	$label_working      = __( 'Checking…', 'woocommerce' );
	$msg_needs_fix      = __( 'Your store has orders that need fixing.', 'woocommerce' );
	$msg_no_fix         = __( 'No affected orders found.', 'woocommerce' );
	$label_disable_tool = __( 'Disable tool', 'woocommerce' );
	$msg_in_progress    = __( 'A fix is already in progress, please check back later.', 'woocommerce' );
	$msg_error          = __( 'Check failed, please try again.', 'woocommerce' );
	?>
	<script type="text/javascript">
	( function() {
		const toolRow = document.querySelector( 'tr.<?php echo esc_js( $tool_class ); ?>' );
		if ( ! toolRow ) {
			return;
		}
		const actionCell = toolRow.querySelector( 'td.run-tool' );
		if ( ! actionCell ) {
			return;
		}

		const statusSpan = document.createElement( 'span' );
		statusSpan.style.cssText = 'display:block;margin-top:6px;';
		statusSpan.setAttribute( 'aria-live', 'polite' );
		statusSpan.setAttribute( 'role', 'status' );

		const checkBtn = document.createElement( 'button' );
		checkBtn.type = 'button';
		checkBtn.className = 'button button-secondary';
		checkBtn.style.marginRight = '8px';
		checkBtn.textContent = <?php echo wp_json_encode( $label_check ); ?>;

		const fixBtn = actionCell.querySelector( 'input[type=submit]' );
		const originalFixLabel = fixBtn ? fixBtn.value : '';
		const toolForm = document.getElementById( 'form_<?php echo esc_js( $tool_class ); ?>' );

		checkBtn.addEventListener( 'click', function() {
			checkBtn.disabled = true;
			checkBtn.textContent = <?php echo wp_json_encode( $label_working ); ?>;
			statusSpan.textContent = '';
			statusSpan.style.color = '';

			const data = new FormData();
			data.append( 'action', 'woocommerce_check_refund_fix_needed' );
			data.append( 'nonce', <?php echo wp_json_encode( $nonce ); ?> );

			fetch( <?php echo wp_json_encode( $ajax_url ); ?>, { method: 'POST', body: data } )
				.then( function( r ) { return r.json(); } )
				.then( function( json ) {
					checkBtn.disabled = false;
					checkBtn.textContent = <?php echo wp_json_encode( $label_check ); ?>;
					if ( json.success ) {
						if ( json.data.fix_in_progress ) {
							statusSpan.textContent = <?php echo wp_json_encode( $msg_in_progress ); ?>;
							statusSpan.style.color = '#1d2327';
						} else if ( json.data.needs_fix ) {
							statusSpan.textContent = <?php echo wp_json_encode( $msg_needs_fix ); ?>;
							statusSpan.style.color = '#d63638';
							if ( fixBtn ) {
								fixBtn.value = originalFixLabel;
								fixBtn.disabled = false;
							}
							const existingFlag = toolForm ? toolForm.querySelector( 'input[name="wc_refund_fix_action"]' ) : null;
							if ( existingFlag ) {
								existingFlag.parentNode.removeChild( existingFlag );
							}
						} else {
							statusSpan.textContent = <?php echo wp_json_encode( $msg_no_fix ); ?>;
							statusSpan.style.color = '#1d2327';
							if ( fixBtn ) {
								fixBtn.value = <?php echo wp_json_encode( $label_disable_tool ); ?>;
								fixBtn.disabled = false;
							}
							if ( toolForm && ! toolForm.querySelector( 'input[name="wc_refund_fix_action"]' ) ) {
								const flagInput = document.createElement( 'input' );
								flagInput.type = 'hidden';
								flagInput.name = 'wc_refund_fix_action';
								flagInput.value = 'disable';
								toolForm.appendChild( flagInput );
							}
						}
					} else {
						statusSpan.textContent = ( json.data && json.data.message ) ? json.data.message : <?php echo wp_json_encode( $msg_error ); ?>;
						statusSpan.style.color = '#d63638';
					}
				} )
				.catch( function() {
					checkBtn.disabled = false;
					checkBtn.textContent = <?php echo wp_json_encode( $label_check ); ?>;
					statusSpan.textContent = <?php echo wp_json_encode( $msg_error ); ?>;
					statusSpan.style.color = '#d63638';
				} );
		} );

		if ( fixBtn ) {
			actionCell.insertBefore( checkBtn, fixBtn );
		} else {
			actionCell.appendChild( checkBtn );
		}
		actionCell.appendChild( statusSpan );
	} )();
	</script>
	<?php
}