Last active
June 27, 2022 09:58
-
-
Save robertuniqid/bf1a06fa27e7a227c12da983ea20d226 to your computer and use it in GitHub Desktop.
WordPress Remote Users Sync - use Action Scheduler during WooCommerce checkout
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 | |
// Replace my_prefix with something else, includes AS Group | |
/** | |
* @param string $endpoint | |
* @param \Wprus_Api_Abstract $object | |
*/ | |
function _my_prefix_wp_notify_remote_after_init_notification_hooks( $endpoint, $object ) { | |
if( !function_exists( 'as_enqueue_async_action' ) ) | |
return; | |
if( $object instanceof \Wprus_Api_Create ) { | |
if( isset( $_GET[ 'wc-ajax' ] ) && $_GET[ 'wc-ajax' ] === 'checkout' ) { | |
remove_action( 'user_register', [ $object, 'notify_remote' ], PHP_INT_MAX, 1 ); | |
add_action( 'user_register', function( $user_id ) { | |
as_enqueue_async_action( 'my_prefix_wp_notify_remote_user_register', [ $user_id ], 'my_prefix' ); | |
}); | |
} | |
add_action( 'my_prefix_wp_notify_remote_user_register', function( $user_id ) use ( $object ) { | |
$object->notify_remote( $user_id ); | |
}); | |
} | |
if( $object instanceof \Wprus_Api_Update ) { | |
if( isset( $_GET[ 'wc-ajax' ] ) && $_GET[ 'wc-ajax' ] === 'checkout' ) { | |
remove_action( 'profile_update', [ $object, 'notify_remote' ], PHP_INT_MAX, 2 ); | |
remove_action( 'add_user_role', [ $object, 'notify_remote' ], PHP_INT_MAX, 2 ); | |
remove_action( 'remove_user_role', [ $object, 'notify_remote' ], PHP_INT_MAX, 2 ); | |
remove_action( 'set_user_role', [ $object, 'notify_remote' ], PHP_INT_MAX, 2 ); | |
add_action( 'profile_update', function( $user_id, $old_userdata ) { | |
as_enqueue_async_action( 'my_prefix_wp_notify_remote_user_update', [ $user_id, $old_userdata ], 'my_prefix' ); | |
}, PHP_INT_MAX, 2 ); | |
add_action( 'add_user_role', function( $user_id, $old_userdata ) { | |
as_enqueue_async_action( 'my_prefix_wp_notify_remote_user_update', [ $user_id, $old_userdata ], 'my_prefix' ); | |
}, PHP_INT_MAX, 2 ); | |
add_action( 'remove_user_role', function( $user_id, $old_userdata ) { | |
as_enqueue_async_action( 'my_prefix_wp_notify_remote_user_update', [ $user_id, $old_userdata ], 'my_prefix' ); | |
}, PHP_INT_MAX, 2 ); | |
add_action( 'set_user_role', function( $user_id, $old_userdata ) { | |
as_enqueue_async_action( 'my_prefix_wp_notify_remote_user_update', [ $user_id, $old_userdata ], 'my_prefix' ); | |
}, PHP_INT_MAX, 2 ); | |
} | |
add_action( 'my_prefix_wp_notify_remote_user_update', function( $user_id, $old_userdata ) use ( $object ) { | |
$response = $object->notify_remote( $user_id, $old_userdata ); | |
}, 100, 2 ); | |
} | |
} | |
add_action( 'wprus_after_init_notification_hooks', '_my_prefix_wp_notify_remote_after_init_notification_hooks', 100, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment