Created
April 2, 2024 07:17
-
-
Save stefanpejcic/41fc547d60870e0dfa91ccdb61ca5cbd to your computer and use it in GitHub Desktop.
Remote Post Check for WooCommerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Remote Post Check for WooCommerce | |
Description: Checks the remote post and displays the result in the WordPress admin page with troubleshooting info. | |
Version: 1.0 | |
Author: Stefan Pejcic | |
Author URL: https://pejcic.rs | |
*/ | |
function check_remote_post() { | |
$enable_remote_post = true; | |
if ( $enable_remote_post ) { | |
$post_response_code = get_transient( 'woocommerce_test_remote_post' ); | |
if ( false === $post_response_code || is_wp_error( $post_response_code ) ) { | |
$response = wp_safe_remote_post( | |
'https://www.paypal.com/cgi-bin/webscr', | |
array( | |
'timeout' => 10, | |
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ), | |
'httpversion' => '1.1', | |
'body' => array( | |
'cmd' => '_notify-validate', | |
), | |
) | |
); | |
if ( ! is_wp_error( $response ) ) { | |
$post_response_code = $response['response']['code']; | |
} | |
set_transient( 'woocommerce_test_remote_post', $post_response_code, HOUR_IN_SECONDS ); | |
} | |
$post_response_successful = ! is_wp_error( $post_response_code ) && $post_response_code >= 200 && $post_response_code < 300; | |
// Display the result in WordPress admin page with troubleshooting info | |
add_action( 'admin_notices', function () use ( $post_response_successful, $post_response_code, $response ) { | |
$message = $post_response_successful ? 'Remote post successful (HTTP ' . $post_response_code . ')' : 'Remote post failed'; | |
if ( ! $post_response_successful ) { | |
$error_message = is_wp_error( $response ) ? 'Error: ' . $response->get_error_message() : 'HTTP ' . $post_response_code; | |
$message .= '<br><strong>Troubleshooting Info:</strong> ' . esc_html( $error_message ); | |
} | |
echo '<div class="notice notice-info is-dismissible"><p>' . wp_kses( $message, 'post' ) . '</p></div>'; | |
} ); | |
} | |
} | |
// Run the function on admin_init hook | |
add_action( 'admin_init', 'check_remote_post' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment