Last active
February 19, 2019 05:24
-
-
Save james-allan/a15ebe1bd4835cd9172f6ff36a167f85 to your computer and use it in GitHub Desktop.
A helper plugin to log when Renewal Order Posts go missing during the renewal process.
This file contains hidden or 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: WCS Missing Renewal Order Posts Logger | |
Plugin URI: | |
Description: A helper plugin to log when Renewal Order Posts go missing during the renewal process. | |
Author: James Allan | |
Author URI: | |
Version: 1.0 | |
*/ | |
class WCS_Missing_Renewal_Order_Posts_Logger { | |
private static $log = null; | |
private static $logger_handle = 'wcs-missing-renewal-orders'; | |
/** | |
* Setup hooks & filters, when the class is initialised. | |
*/ | |
public static function init() { | |
add_action( 'woocommerce_scheduled_subscription_payment', array( __CLASS__, 'attach_callbacks' ), 0 ); | |
} | |
/** | |
* Attach all the callbacks to listen for deleted posts on. | |
* | |
* Hooks are attached in the order in which they occur. | |
*/ | |
public static function attach_callbacks() { | |
add_action( 'wp_insert_post', array( __CLASS__, 'check_post_insert' ), 1, 2 ); | |
add_action( 'woocommerce_order_object_updated_props', array( __CLASS__, 'check_order' ), 1 ); | |
add_action( 'woocommerce_order_object_updated_props', array( __CLASS__, 'check_order' ), 1 ); | |
add_action( 'woocommerce_new_order', array( __CLASS__, 'check_order_id' ) ); | |
add_action( 'woocommerce_update_order', array( __CLASS__, 'check_order_id' ) ); | |
add_action( 'wcs_renewal_order_meta_query', array( __CLASS__, 'check_order_second_param' ), 10, 2 ); | |
add_action( 'woocommerce_before_order_object_save', array( __CLASS__, 'check_order' ) ); | |
add_action( 'wcs_new_order_created', array( __CLASS__, 'check_order' ) ); | |
add_action( 'wcs_renewal_order_created', array( __CLASS__, 'check_order' ), 1 ); | |
add_action( 'wcs_renewal_order_created', array( __CLASS__, 'check_order' ), 10000 ); | |
add_action( 'woocommerce_order_number', array( __CLASS__, 'check_order_id' ) ); | |
} | |
public static function check_post_insert( $post_id, $post ) { | |
if ( is_a( $post, 'WP_Post' ) && 'shop_order' === $post->post_type ) { | |
self::check_order_id( $post_id ); | |
} | |
} | |
public static function check_order_second_param( $value , $order ) { | |
self::check_order( $order ); | |
return $value; | |
} | |
public static function check_order( $order ) { | |
if ( is_a( $order, 'WC_Abstract_Order' ) ) { | |
self::check_order_id( $order->get_id() ); | |
} else { | |
self::log( 'Received an invalid order object on ' . self::get_current_filter() ); | |
} | |
return $order; | |
} | |
public static function check_order_id( $value ) { | |
$order_id = absint( $value ); | |
if ( ! $order_id ) { | |
return $value; | |
} | |
$raw_post = self::get_database_post( $order_id ); | |
$post = get_post( $order_id ); | |
if ( $post && 'shop_order' !== $post->post_type ) { | |
return $value; | |
} | |
$failure = false; | |
$messages = array(); | |
$current_filter = self::get_current_filter(); | |
if ( ! $raw_post ) { | |
$failure = true; | |
$messages[] = " couldn't find the post in the database"; | |
} else { | |
$messages[] = " found the post in the database"; | |
} | |
if ( ! $post ) { | |
$failure = true; | |
$messages[] = " couldn't get a post object"; | |
} else { | |
$messages[] = " got a post object"; | |
} | |
self::log( "For order #$order_id on $current_filter" . implode( $messages, ' &' ) ); | |
if ( $failure ) { | |
self::log( "Backtrace:\n" . self::get_backtrace() ); | |
} | |
return $value; | |
} | |
private static function get_database_post( $post_id ) { | |
global $wpdb; | |
return $wpdb->get_row( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d", $post_id ), ARRAY_A ); | |
} | |
private static function log( $message ) { | |
if ( is_null( self::$log ) ) { | |
self::$log = wc_get_logger(); | |
} | |
self::$log->add( self::$logger_handle, $message ); | |
} | |
private static function get_current_filter() { | |
global $wp_filter; | |
$current_filter = current_filter(); | |
if ( isset( $wp_filter[ $current_filter ] ) ) { | |
$current_filter .= " (priority {$wp_filter[ $current_filter ]->current_priority()})"; | |
} | |
return $current_filter; | |
} | |
private static function get_backtrace( $args = false ) { | |
ob_start(); | |
debug_print_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); | |
$trace = ob_get_contents(); | |
ob_end_clean(); | |
// Remove first item from backtrace as it's this function which is redundant. | |
$trace = preg_replace ( '/^#0\s+' . __FUNCTION__ . "[^\n]*\n/", '', $trace, 1 ); | |
$trace = preg_replace ( '/^#0\s+' . __METHOD__ . "[^\n]*\n/", '', $trace, 1 ); | |
return $trace; | |
} | |
} | |
WCS_Missing_Renewal_Order_Posts_Logger::init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment