Last active
August 29, 2016 11:12
-
-
Save loburets/8a1cdc43fb68e49c22167231874cc7ce to your computer and use it in GitHub Desktop.
wordpress and laravel integration
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
var ajaxSended = false; | |
//Authorize in laravel app by ajax | |
$('.ui-dialog').on('click', '.ui-button[type="submit"]', function(event) { | |
if (ajaxSended) { | |
ajaxSended = false; | |
return true; | |
} | |
event.preventDefault(); | |
// Block interface | |
$('#ual_div_<?php $ual->form_id(); ?>').block({ message: null }); | |
var $button = $(this); | |
var login = $("#ual_form_<?php $ual->form_id(); ?>").find('[name="ual_username"]').val(); | |
var password = $("#ual_form_<?php $ual->form_id(); ?>").find('[name="ual_password"]').val(); | |
$.ajax({ | |
type: 'POST', | |
url: 'http://app.wfresidency.loc/wpLogin', | |
//allow some crossdomain xhr headers like as 'Set-Cookie' which need for authorize | |
xhrFields: { | |
withCredentials: true, | |
}, | |
data: { | |
'login': login, | |
'password': password, | |
}, | |
success: function(){ | |
$button.trigger('click'); | |
}, | |
error: function(){ | |
$button.trigger('click'); | |
} | |
}); | |
ajaxSended = true; | |
}); | |
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 | |
add_action( 'user_register', 'create_user_in_laravel_app' ); | |
function create_user_in_laravel_app($user_id) { | |
$user = get_user_by('id', $user_id); | |
if (!empty($user->get_role_caps()['administrator'])) { | |
return; | |
} | |
require_once get_home_path() . LARAVEL_APP_PATH . '/app/Helpers/WpFunctions.php'; | |
LrCreateUser($user); | |
} | |
add_action( 'delete_user', 'delete_user_in_laravel_app' ); | |
function delete_user_in_laravel_app($user_id) { | |
require_once get_home_path() . LARAVEL_APP_PATH . '/app/Helpers/WpFunctions.php'; | |
LrDeleteUser($user_id); | |
} | |
add_action( 'profile_update', 'update_user_in_laravel_app'); | |
function update_user_in_laravel_app($user_id) { | |
$user = get_user_by('id', $user_id); | |
if (!empty($user->get_role_caps()['administrator'])) { | |
return; | |
} | |
require_once get_home_path() . LARAVEL_APP_PATH . '/app/Helpers/WpFunctions.php'; | |
LrUpdateUser($user); | |
} |
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\Http\Middleware; | |
use Closure; | |
class CheckExternalCall { | |
/** | |
* Handle an incoming request and add headers for external call | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @param string|null $guard | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next) { | |
if ($request->server('HTTP_ORIGIN') == env('WORDPRESS_URL')) { | |
header('Access-Control-Allow-Origin: ' . env('WORDPRESS_URL')); | |
header('Access-Control-Allow-Methods: GET, POST, OPTIONS'); | |
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Request-With, Set-Cookie'); | |
header('Access-Control-Allow-Credentials: true'); | |
} | |
return $next($request); | |
} | |
} | |
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 | |
//laravel | |
namespace App\Helpers; | |
use App\Repositories\UserRepository; | |
use Auth; | |
use Response; | |
class AuthHelper { | |
/** | |
* @var \App\Repositories\UserRepository | |
*/ | |
private $userRepository; | |
public function __construct( | |
UserRepository $userRepository | |
) { | |
$this->userRepository = $userRepository; | |
} | |
/** | |
* Authenticate user from the wordpress site | |
* | |
* @param string $login | |
* @param string $password | |
* @return \Illuminate\Http\Response | |
*/ | |
public function checkInWpAndAuthorize($login, $password) { | |
require(base_path() . env('WORDPRESS_PATH') . 'wp-load.php'); | |
$check = \wp_authenticate_username_password(NULL, $login, $password); | |
if (\is_wp_error($check)) { | |
return Response::json(['error' => 'Wrong credentials'], 403); | |
} | |
if (Auth::check()) { | |
Auth::logout(); | |
} | |
$user = $this->userRepository | |
->findWhere(['wp_id' => $check->ID]) | |
->first(); | |
if (empty($user)) { | |
$user = $this->userRepository->create( | |
[], | |
[ | |
'name' => $login, | |
'email' => $check->user_email, | |
'wp_id' => $check->ID, | |
] | |
); | |
} | |
if ($user->hasRole('admin')) { | |
return Response::json(['error' => 'Wrong credentials'], 403); | |
} | |
Auth::login($user, true); | |
return Response::json(['message' => 'User logged']); | |
} | |
} |
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 | |
/** | |
* File with functions called in the wordpress site by including this file | |
* | |
* Name all functions by prefix "Lr" for clear understanding in the wordpress code | |
*/ | |
//init laravel | |
require __DIR__.'/../../bootstrap/autoload.php'; | |
$app = require_once __DIR__.'/../../bootstrap/app.php'; | |
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class); | |
$response = $kernel->handle( | |
$request = Illuminate\Http\Request::capture() | |
); | |
//functions | |
/** | |
* Make new user in laravel app by the WP user | |
* | |
* @param $user | |
*/ | |
function LrCreateUser($user) { | |
if (empty($user)) { | |
return; | |
} | |
$userRepository = \App::make('App\Repositories\UserRepository'); | |
$userRepository->create( | |
[], | |
[ | |
'name' => $user->get('user_login'), | |
'email' => $user->get('user_email'), | |
'wp_id' => $user->get('ID'), | |
] | |
); | |
} | |
/** | |
* Delete user by ID in WP | |
* | |
* @param $userId | |
*/ | |
function LrDeleteUser($userId) { | |
if (empty($userId)) { | |
return; | |
} | |
$userRepository = \App::make('App\Repositories\UserRepository'); | |
$user = $userRepository->findWhere(['wp_id' => $userId])->first(); | |
if (empty($user)) { | |
return; | |
} | |
if ($user->hasRole('admin')) { | |
return; | |
} | |
$user->delete(); | |
} | |
/** | |
* Update user in laravel app by the WP user | |
* | |
* @param $wpUser | |
*/ | |
function LrUpdateUser($wpUser) { | |
if (empty($wpUser)) { | |
return; | |
} | |
$userRepository = \App::make('App\Repositories\UserRepository'); | |
$lrUser = $userRepository->findWhere(['wp_id' => $wpUser->get('ID')])->first(); | |
if (empty($lrUser)) { | |
return; | |
} | |
$lrUser->name = $wpUser->get('user_login'); | |
$lrUser->email = $wpUser->get('user_email'); | |
$lrUser->save(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment