on_fatal = $on_fatal; $this->on_shutdown = $on_shutdown; // Ensure we close off this transaction on shutdown to allow other shutdown processes to save changes to the DB. add_action( 'shutdown', array( $this, 'handle_shutdown' ), -100 ); } /** * Starts a MYSQL Transaction. * * @since 1.0.0 - Migrated from WooCommerce Subscriptions v3.1.0 */ public function start() { wc_transaction_query( 'start' ); $this->active_transaction = true; } /** * Commits the MYSQL Transaction. * * @since 1.0.0 - Migrated from WooCommerce Subscriptions v3.1.0 */ public function commit() { wc_transaction_query( 'commit' ); $this->active_transaction = false; } /** * Rolls back any changes made during the MYSQL Transaction. * * @since 1.0.0 - Migrated from WooCommerce Subscriptions v3.1.0 */ public function rollback() { wc_transaction_query( 'rollback' ); $this->active_transaction = false; } /** * Closes out an active transaction depending on the type of shutdown. * * Shutdowns caused by a fatal will be rolledback or committed @see $this->on_fatal. * Shutdowns caused by a natural PHP termination (no error) will be rolledback or committed. @see $this->on_shutdown. * * @since 1.0.0 - Migrated from WooCommerce Subscriptions v3.1.0 */ public function handle_shutdown() { if ( ! $this->active_transaction ) { return; } $error = error_get_last(); if ( $error && in_array( $error['type'], array( E_ERROR, E_PARSE, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR ), true ) ) { $this->{$this->on_fatal}(); } else { $this->{$this->on_shutdown}(); } } }