Last active
February 6, 2023 02:51
-
-
Save dodync/00bf7a13e4b82312494df6fd7d0714f6 to your computer and use it in GitHub Desktop.
sync user to laravel, WordpressHelper.php untuk laravel
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: WP User Data Importer | |
Description: Sends user data to a remote service on user registration and password update | |
Version: 1.0 | |
Author: Your Name | |
Author URI: Your Website | |
*/ | |
function send_user_data_on_user_registration($user_id) { | |
$endpoint = 'https://www.mydomain.com/api/auth/wpImport'; | |
$user = get_userdata($user_id); | |
$post_data = array( | |
'username' => $user->user_login, | |
'email' => $user->user_email, | |
); | |
$response = wp_remote_post($endpoint, array( | |
'method' => 'POST', | |
'timeout' => 45, | |
'redirection' => 5, | |
'httpversion' => '1.0', | |
'blocking' => true, | |
'headers' => array( | |
'Content-Type' => 'application/json', | |
), | |
'body' => wp_json_encode($post_data), | |
)); | |
if ( is_wp_error( $response ) ) { | |
// Handle error | |
} else { | |
// Handle success | |
} | |
} | |
add_action('user_register', 'send_user_data_on_user_registration'); | |
function send_user_data_on_password_update($user_id) { | |
$endpoint = 'https://www.mydomain.com/api/auth/wpImport'; | |
$user = get_userdata($user_id); | |
$post_data = array( | |
'username' => $user->user_login, | |
); | |
$response = wp_remote_post($endpoint, array( | |
'method' => 'POST', | |
'timeout' => 45, | |
'redirection' => 5, | |
'httpversion' => '1.0', | |
'blocking' => true, | |
'headers' => array( | |
'Content-Type' => 'application/json', | |
), | |
'body' => wp_json_encode($post_data), | |
)); | |
if ( is_wp_error( $response ) ) { | |
// Handle error | |
} else { | |
// Handle success | |
} | |
} | |
add_action('password_reset', 'send_user_data_on_password_update'); | |
function send_all_user_data() { | |
$endpoint = 'https://www.mydomain.com/api/auth/wpImport'; | |
$users = get_users(); | |
foreach ($users as $user) { | |
$post_data = array( | |
'username' => $user->user_login, | |
'email' => $user->user_email, | |
); | |
$response = wp_remote_post($endpoint, array( | |
'method' => 'POST', | |
'timeout' => 45, | |
'redirection' => 5, | |
'httpversion' => '1.0', | |
'blocking' => true, | |
'headers' => array( | |
'Content-Type' => 'application/json', | |
), | |
'body' => wp_json_encode($post_data), | |
)); | |
if ( is_wp_error( $response ) ) { | |
// Handle error | |
} else { | |
// Handle success | |
} | |
} | |
} |
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 | |
namespace App\Helpers; | |
use GuzzleHttp\Client; | |
class WordpressHelper | |
{ | |
private $client; | |
public function __construct() | |
{ | |
$this->client = new Client([ | |
'base_uri' => 'https://example.com/wp-json/wp/v2/', | |
]); | |
} | |
public function createUser($username, $email, $password) | |
{ | |
$response = $this->client->post('users', [ | |
'headers' => [ | |
'Authorization' => 'Basic ' . base64_encode("admin:password"), | |
], | |
'json' => [ | |
'username' => $username, | |
'email' => $email, | |
'password' => $password, | |
], | |
]); | |
return json_decode($response->getBody()->getContents()); | |
} | |
public function changePassword($userId, $newPassword) | |
{ | |
$response = $this->client->post("users/{$userId}", [ | |
'headers' => [ | |
'Authorization' => 'Basic ' . base64_encode("admin:password"), | |
], | |
'json' => [ | |
'password' => $newPassword, | |
], | |
]); | |
return json_decode($response->getBody()->getContents()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment