Last active
September 27, 2024 01:46
-
-
Save petertwise/acfa9ae940df5c2521c5adefb9d6cdb5 to your computer and use it in GitHub Desktop.
Log WooCommerce status changes with stack trace, even when WP_DEBUG is off.
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: SquareCandy Status Change Logger for WooCommmerce | |
* Description: Log WooCommerce status changes with stack trace, even when WP_DEBUG is off. | |
* Author: squarecandy | |
* License: GNU General Public License v3 or later | |
* License URI: http://www.gnu.org/licenses/gpl-3.0.html | |
*/ | |
add_action( 'woocommerce_order_status_changed', 'squarecandy_status_change_logger', 999, 4 ); | |
function squarecandy_status_change_logger( $order_id, $old_status, $new_status, $order ) { | |
// just log orders that move to cancelled, but also all REST API requests | |
// if ( 'cancelled' !== $new_status && ! defined( 'REST_REQUEST' ) ) { | |
// return; | |
// } | |
$log = '===============' . PHP_EOL; | |
$log .= 'Order #' . $order_id . ' status changed from ' . $old_status . ' to ' . $new_status . PHP_EOL; | |
$log .= '----------' . PHP_EOL; | |
// $log .= print_r( $order, true ) . PHP_EOL; | |
// $log .= '----------' . PHP_EOL; | |
$log .= 'Debug Backtrace' . PHP_EOL; | |
$log .= '----------' . PHP_EOL; | |
foreach ( debug_backtrace() as $i => $item ) { | |
if ( 0 === $i ) { | |
$log .= $item['args'][1] . PHP_EOL; | |
} else { | |
$log .= $item['file'] . ' -- line ' . $item['line'] . PHP_EOL; | |
} | |
} | |
if ( defined( 'REST_REQUEST' ) ) : | |
$log .= '---------------' . PHP_EOL; | |
$log .= 'THIS IS A REST API REQUEST' . PHP_EOL; | |
$log .= '-------HEADERS--------' . PHP_EOL; | |
$log .= print_r( getallheaders(), true ); | |
$log .= '-------POST--------' . PHP_EOL; | |
$log .= print_r( $_POST, true ); | |
$log .= '-------GET--------' . PHP_EOL; | |
$log .= print_r( $_GET, true ); | |
$log .= '--------ORDER-------' . PHP_EOL; | |
$log .= print_r( $order, true ) . PHP_EOL; | |
endif; | |
$log .= '===============' . PHP_EOL; | |
$file = wp_get_upload_dir()['basedir'] . '/order_status_change.log'; | |
file_put_contents($file, $log, FILE_APPEND | LOCK_EX); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment