Skip to content

Instantly share code, notes, and snippets.

@dodync
Last active February 6, 2023 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dodync/00bf7a13e4b82312494df6fd7d0714f6 to your computer and use it in GitHub Desktop.
Save dodync/00bf7a13e4b82312494df6fd7d0714f6 to your computer and use it in GitHub Desktop.
sync user to laravel, WordpressHelper.php untuk laravel
<?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
}
}
}
<?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