In this article, we are going to integrate Yii2 and WordPress applications. It can be useful in case when the WordPress app is a client and the Yii2 app is an API provider. Yes, you can say it can be done with WordPress natively without using of Yii2, but Yii2 Framework has a lot of built-in instruments that helps to create an API ASAP. ОК. Let's start.
We will have a WordPress app and the Yii2 basic app installed to the sub-directory called api.
There are 5 steps we need to implement.
Step 1 - Configuring Yii2 entry script
Add the following lines before Yii2 initialisation:
Step 2 - Writing the User model
We have overwritten the findIdentity method and provided the wpUser property so when the user is logged in WordPress he also will be authorized in the Yii app. Also, we have added some other properties such as login, niceName ant others that are present in a WordPress user instance.
Step 3 - Writing the bootstrapping class
We will have a WordPress app and the Yii2 basic app installed to the sub-directory called api.
There are 5 steps we need to implement.
Step 1 - Configuring Yii2 entry script
Add the following lines before Yii2 initialisation:
//api/web/index.php define('WP_USE_THEMES', false); require __DIR__ . '/../../wp-blog-header.php';
Step 2 - Writing the User model
namespace app\models; class User extends \yii\base\Object implements \yii\web\IdentityInterface { /** * @var \WP_User $wpUser WordPress User record */ public static $wpUser; public $id; public $login; public $niceName; public $displayName; public $email; public $dateRegistered; public $roles; /** * @inheritdoc */ public static function findIdentity($id) { $wpUser = self::$wpUser; if ($wpUser) { $user = new self; $wpUserData = $wpUser->data; $user->id = $wpUser->ID; $user->login = $wpUserData->user_login; $user->niceName = $wpUserData->user_nicename; $user->displayName = $wpUserData->display_name; $user->email = $wpUserData->user_email; $user->dateRegistered = $wpUserData->user_registered; $user->roles = $wpUser->roles; return $user; } } /** * Set WP user * @param \WP_User $wpUser */ public static function setWpUser(\WP_User $wpUser) { self::$wpUser = $wpUser; } /** * @return int|null */ public static function getWpUserId() { if (!empty(self::$wpUser)) { return self::$wpUser->ID; } return null; } /** * @inheritdoc */ public static function findIdentityByAccessToken($token, $type = null) { return null; } /** * @inheritdoc */ public function getId() { return $this->id; } /** * @inheritdoc */ public function getAuthKey() { return null; } /** * @inheritdoc */ public function validateAuthKey($authKey) { return false; } }
We have overwritten the findIdentity method and provided the wpUser property so when the user is logged in WordPress he also will be authorized in the Yii app. Also, we have added some other properties such as login, niceName ant others that are present in a WordPress user instance.
Step 3 - Writing the bootstrapping class
namespace app\components; use yii; use app\models\User; use yii\base\BootstrapInterface; use yii\web\ForbiddenHttpException; class IdentityBootstrap implements BootstrapInterface { /** * Get WordPress user and login it in Yii application * @uses is_user_logged_in() to check WP user is logged in * @uses wp_get_current_user() to get WP user record * @uses wp_parse_auth_cookie() to check WP cookie expire date * @throws ForbiddenHttpException */ public function bootstrap($app) { if (!is_user_logged_in()) { throw new ForbiddenHttpException('Authentication required.'); } User::setWpUser(wp_get_current_user()); $user = User::findIdentity(User::getWpUserId()); $authCookie = wp_parse_auth_cookie('', 'logged_in'); $authDuration = 0; if (!empty($authCookie['expiration'])) { $authDuration = (int)$authCookie['expiration'] - time(); } Yii::$app->user->login($user, $authDuration); } }
Here we have configured application bootstrap class which uses WordPress functions to check the user is logged in and get the WP user instance. After this, we can authenticate the user in Yii app.
Step 4 - Registering of bootstrapping class in the app config
api/config/web.php 'bootstrap' => [ 'log', 'app\components\IdentityBootstrap' //authenticate WP user ],
Step 5 - Finishing
To start using the API we need to connect the javascript with the AJAX calls in the WP theme. I will put it in /api/web/js/app.js.
// get user orders $(document).ready(function() { $.ajax({ url : "/api/orders", type: "POST", success : function(res) {//do smth here..} }); });
That's all. Mission complete.
No comments:
Post a Comment