Skip to content

Commit 30af64a

Browse files
committed
Added token to login
1 parent 0c40bcf commit 30af64a

File tree

366 files changed

+69747
-363
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

366 files changed

+69747
-363
lines changed

model/common/token.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
use \Firebase\JWT\JWT;
4+
5+
class ModelCommonToken extends Model {
6+
public function getToken( $args ) {
7+
8+
$secret_key = 'bananas';
9+
$username = $args['email'];
10+
$password = $args['password'];
11+
12+
$user = wp_authenticate( $username, $password );
13+
14+
if ( is_wp_error( $user ) ) {
15+
$error_code = $user->get_error_code();
16+
17+
throw new Exception( $user->get_error_message( $error_code ) );
18+
}
19+
20+
21+
$issuedAt = time();
22+
23+
$expire = $issuedAt + ( DAY_IN_SECONDS * 7 );
24+
25+
$token = array(
26+
'iss' => get_bloginfo( 'url' ),
27+
'iat' => $issuedAt,
28+
'nbf' => $issuedAt,
29+
'exp' => $expire,
30+
'data' => array(
31+
'user' => array(
32+
'id' => $user->data->ID,
33+
),
34+
),
35+
);
36+
37+
$token = JWT::encode( $token, $secret_key );
38+
39+
$data = array(
40+
'token' => $token,
41+
'expire' => $expire,
42+
'user_id' => $user->data->ID,
43+
'user_email' => $user->data->user_email,
44+
'user_nicename' => $user->data->user_nicename,
45+
'user_display_name' => $user->data->display_name,
46+
);
47+
48+
return $data;
49+
}
50+
51+
public function validateToken( $output ) {
52+
$headers = getallheaders();
53+
$auth = isset( $headers['Authorization'] ) ? $headers['Authorization'] : false;
54+
if ( ! $auth ) {
55+
return false;
56+
}
57+
58+
list( $token ) = sscanf( $auth, 'Bearer %s' );
59+
60+
if ( ! $token ) {
61+
return false;
62+
}
63+
64+
$secret_key = 'bananas';
65+
66+
try {
67+
$token = JWT::decode( $token, $secret_key, array( 'HS256' ) );
68+
69+
if ( $token->iss != get_bloginfo( 'url' ) ) {
70+
return false;
71+
}
72+
if ( ! isset( $token->data->user->id ) ) {
73+
return false;
74+
}
75+
if ( ! $output ) {
76+
return $token;
77+
}
78+
79+
return true;
80+
} catch ( Exception $e ) {
81+
return false;
82+
}
83+
}
84+
}

model/store/compare.php

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,52 @@
11
<?php
2-
class ModelStoreCompare extends Model
3-
{
4-
public function getCompare()
5-
{
6-
$result = array();
7-
8-
if (!empty($_SESSION['compare'])) {
9-
$result = $_SESSION['compare'];
10-
}
11-
12-
return $result;
13-
}
14-
15-
public function addCompare($product_id)
16-
{
17-
if (!isset($_SESSION['compare'])) {
18-
$_SESSION['compare'] = array();
19-
}
20-
if (!in_array($product_id, $_SESSION['compare'])) {
21-
if (count($_SESSION['compare']) >= 4) {
22-
array_shift($_SESSION['compare']);
23-
}
24-
$_SESSION['compare'][] = (int)$product_id;
25-
}
26-
}
27-
28-
public function deleteCompare($product_id)
29-
{
30-
if (!empty($_SESSION['compare'])) {
31-
$key = array_search($product_id, $_SESSION['compare']);
32-
33-
if ($key !== false) {
34-
unset($_SESSION['compare'][$key]);
35-
}
36-
}
37-
}
2+
3+
class ModelStoreCompare extends Model {
4+
public function getCompare() {
5+
$result = array();
6+
7+
$compare = $_COOKIE['compare'];
8+
9+
if ( ! empty( $compare ) ) {
10+
$result = json_decode( $compare );
11+
}
12+
13+
return $result;
14+
}
15+
16+
public function addCompare( $product_id ) {
17+
$compare = $_COOKIE['compare'];
18+
19+
if ( ! empty( $compare ) ) {
20+
$compare = json_decode( $compare, true );
21+
} else {
22+
$compare = array();
23+
}
24+
if ( ! in_array( $product_id, $compare ) ) {
25+
if ( count( $compare ) >= 4 ) {
26+
array_shift( $compare );
27+
}
28+
$compare[] = $product_id;
29+
setcookie( 'compare', json_encode( $compare ), 0, "/" );
30+
$_COOKIE['compare'] = json_encode( $compare );
31+
}
32+
}
33+
34+
public function deleteCompare( $product_id ) {
35+
$compare = $_COOKIE['compare'];
36+
37+
$result = array();
38+
39+
if ( ! empty( $compare ) ) {
40+
$result = json_decode( $compare );
41+
}
42+
43+
$key = array_search( $product_id, $result );
44+
45+
if ( $key !== false ) {
46+
unset( $result[ $key ] );
47+
}
48+
49+
setcookie( 'compare', json_encode( $result ), 0, "/" );
50+
$_COOKIE['compare'] = json_encode( $result );
51+
}
3852
}

model/store/wishlist.php

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,50 @@
11
<?php
2-
class ModelStoreWishlist extends Model
3-
{
4-
public function getWishlist()
5-
{
6-
$result = array();
7-
8-
if (!empty($_SESSION['wishlist'])) {
9-
$result = $_SESSION['wishlist'];
10-
}
11-
12-
return $result;
13-
}
14-
15-
public function addWishlist($product_id)
16-
{
17-
if (!isset($_SESSION['wishlist'])) {
18-
$_SESSION['wishlist'] = array();
19-
}
20-
if (!in_array($product_id, $_SESSION['wishlist'])) {
21-
$_SESSION['wishlist'][] = (int)$product_id;
22-
}
23-
}
24-
25-
public function deleteWishlist($product_id)
26-
{
27-
if (!empty($_SESSION['wishlist'])) {
28-
$key = array_search($product_id, $_SESSION['wishlist']);
29-
30-
if ($key !== false) {
31-
unset($_SESSION['wishlist'][$key]);
32-
}
33-
}
34-
}
2+
3+
class ModelStoreWishlist extends Model {
4+
public function getWishlist() {
5+
$result = array();
6+
7+
$wishList = $_COOKIE['wishList'];
8+
9+
if ( ! empty( $wishList ) ) {
10+
$result = json_decode( $wishList );
11+
}
12+
13+
return $result;
14+
}
15+
16+
public function addWishlist( $product_id ) {
17+
18+
$wishList = $_COOKIE['wishList'];
19+
20+
if ( ! empty( $wishList ) ) {
21+
$wishList = json_decode( $wishList, true );
22+
} else {
23+
$wishList = array();
24+
}
25+
if ( ! in_array( $product_id, $wishList ) ) {
26+
$wishList[] = $product_id;
27+
setcookie( 'wishList', json_encode( $wishList ), 0 , "/" );
28+
$_COOKIE['wishList'] = json_encode( $wishList );
29+
}
30+
}
31+
32+
public function deleteWishlist( $product_id ) {
33+
$wishList = $_COOKIE['wishList'];
34+
35+
$result = array();
36+
37+
if ( ! empty( $wishList ) ) {
38+
$result = json_decode( $wishList );
39+
}
40+
41+
$key = array_search( $product_id, $result );
42+
43+
if ( $key !== false ) {
44+
unset( $result[ $key ] );
45+
}
46+
47+
setcookie( 'wishList', json_encode( $result ), 0 , "/" );
48+
$_COOKIE['wishList'] = json_encode( $result );
49+
}
3550
}

plugin.php

Lines changed: 64 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,80 @@
88
* Author URI: http://dreamvention.com
99
*/
1010

11-
/**
12-
* Создаем страницу настроек плагина
13-
*/
14-
15-
add_action('admin_menu', 'add_plugin_page');
16-
function add_plugin_page()
17-
{
18-
$codename = 'd_vuefront';
19-
$page_hook_suffix= add_options_page( __('Settings', $codename).' Vuefront', 'Vuefront', 'manage_options', 'd_vuefront', 'vuefront_options_page_output');
20-
add_action('admin_print_scripts-' . $page_hook_suffix, 'my_plugin_admin_scripts');
11+
require_once 'system/startup.php';
12+
13+
14+
add_action( 'admin_menu', 'add_plugin_page' );
15+
function add_plugin_page() {
16+
$codename = 'd_vuefront';
17+
$page_hook_suffix = add_options_page( __( 'Settings', $codename ) . ' Vuefront', 'Vuefront', 'manage_options', 'd_vuefront', 'vuefront_options_page_output' );
18+
add_action( 'admin_print_scripts-' . $page_hook_suffix, 'my_plugin_admin_scripts' );
2119
}
2220

2321
function my_plugin_admin_scripts() {
24-
wp_enqueue_style( 'vuefront-style', plugins_url('d_vuefront/view/stylesheet/admin.css') );
25-
wp_enqueue_style( 'bootstrap-style', plugins_url('d_vuefront/view/stylesheet/bootstrap.min.css') );
26-
wp_enqueue_script( 'jquery' );
27-
wp_enqueue_script( 'clipboard' );
28-
wp_enqueue_script( 'bootstrap-script', plugins_url('d_vuefront/view/javascript/bootstrap.min.js') );
22+
wp_enqueue_style( 'vuefront-style', plugins_url( 'd_vuefront/view/stylesheet/admin.css' ) );
23+
wp_enqueue_style( 'bootstrap-style', plugins_url( 'd_vuefront/view/stylesheet/bootstrap.min.css' ) );
24+
wp_enqueue_script( 'jquery' );
25+
wp_enqueue_script( 'clipboard' );
26+
wp_enqueue_script( 'bootstrap-script', plugins_url( 'd_vuefront/view/javascript/bootstrap.min.js' ) );
2927
}
3028

31-
function vuefront_options_page_output()
32-
{
33-
$codename = 'd_vuefront';
29+
function vuefront_options_page_output() {
30+
$codename = 'd_vuefront';
3431

35-
$data = array();
36-
$data['text_title'] = __('CMS Connect URL', $codename);
37-
$data['text_description'] = __('This is your CMS Connect URL link that shares your Blog data via GraphQL. When installing VueFront via the command line, you will be prompted to enter this URL. Simply copy and paste it into the command line.
32+
$data = array();
33+
$data['text_title'] = __( 'CMS Connect URL', $codename );
34+
$data['text_description'] = __( 'This is your CMS Connect URL link that shares your Blog data via GraphQL. When installing VueFront via the command line, you will be prompted to enter this URL. Simply copy and paste it into the command line.
3835
<br><br>
39-
Read more about the <a href="https://vuefront.com/cms/wordpress.html" target="_blank">CMS Connect for Wordpress</a>', $codename);
40-
$data['text_woocommerce_plugin'] = __('WooCommerce', $codename);
41-
$data['text_woocommerce_enabled'] = __('WooCommerce active', $codename);
42-
$data['text_woocommerce_description'] = sprintf(__('VueFront relies on the free <a href="%s" target="_blank">WooCommerce</a> plugin to implement store. The store feature is optional and VueFront will work fine without it. You can install it via Wordpress.', $codename), 'https://ru.wordpress.org/plugins/woocommerce/');
43-
$data['text_woocommerce_disabled'] = __('WooCommerce missing. Click to download', $codename);
44-
$data['text_copy'] = __('copy', $codename);
45-
$data['text_copied'] = __('copied!', $codename);
46-
$data['catalog'] = plugins_url('d_vuefront/index.php');
47-
$data['woocommerce'] = is_plugin_active( 'woocommerce/woocommerce.php' );
48-
$data['logo'] = plugins_url('d_vuefront/view/image/logo.png');
49-
extract($data);
50-
require_once 'view/template/setting.tpl';
36+
Read more about the <a href="https://vuefront.com/cms/wordpress.html" target="_blank">CMS Connect for Wordpress</a>', $codename );
37+
$data['text_woocommerce_plugin'] = __( 'WooCommerce', $codename );
38+
$data['text_woocommerce_enabled'] = __( 'WooCommerce active', $codename );
39+
$data['text_woocommerce_description'] = sprintf( __( 'VueFront relies on the free <a href="%s" target="_blank">WooCommerce</a> plugin to implement store. The store feature is optional and VueFront will work fine without it. You can install it via Wordpress.', $codename ), 'https://ru.wordpress.org/plugins/woocommerce/' );
40+
$data['text_woocommerce_disabled'] = __( 'WooCommerce missing. Click to download', $codename );
41+
$data['text_copy'] = __( 'copy', $codename );
42+
$data['text_copied'] = __( 'copied!', $codename );
43+
$data['catalog'] = get_rest_url( null, '/vuefront/v1/graphql' );
44+
$data['woocommerce'] = is_plugin_active( 'woocommerce/woocommerce.php' );
45+
$data['logo'] = plugins_url( 'd_vuefront/view/image/logo.png' );
46+
extract( $data );
47+
require_once 'view/template/setting.tpl';
5148
}
5249

53-
function my_plugin_action_links($links)
54-
{
55-
$links = array_merge(array(
56-
'<a href="' . esc_url(admin_url('options-general.php?page=d_vuefront')) . '">' . __('Settings') . '</a>'
57-
), $links);
58-
return $links;
50+
function my_plugin_action_links( $links ) {
51+
$links = array_merge( array(
52+
'<a href="' . esc_url( admin_url( 'options-general.php?page=d_vuefront' ) ) . '">' . __( 'Settings' ) . '</a>'
53+
), $links );
54+
55+
return $links;
5956
}
60-
add_action('plugin_action_links_' . plugin_basename(__FILE__), 'my_plugin_action_links');
57+
58+
add_action( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'my_plugin_action_links' );
6159

6260
add_action( 'plugins_loaded', 'true_load_plugin_textdomain' );
63-
61+
6462
function true_load_plugin_textdomain() {
65-
load_plugin_textdomain( 'd_vuefront', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
66-
}
63+
load_plugin_textdomain( 'd_vuefront', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
64+
}
65+
66+
function restApi( WP_REST_Request $request ) {
67+
$registry = start();
68+
69+
$registry->set( 'request', $request );
70+
71+
$output = $registry->get( 'load' )->resolver( 'startup/startup' );
72+
73+
return $output;
74+
}
75+
76+
add_action( 'determine_current_user', function ( $user ) {
77+
$registry = start();
78+
79+
return $registry->get( 'load' )->resolver( 'startup/startup/determine_current_user', $user );
80+
}, 10 );
81+
82+
add_action( 'rest_api_init', function () {
83+
register_rest_route( 'vuefront/v1', '/graphql', array(
84+
'methods' => 'POST',
85+
'callback' => 'restApi',
86+
) );
87+
} );

0 commit comments

Comments
 (0)