Skip to content

Commit 62da705

Browse files
committed
fix WPSEO integration
1 parent 4b2d0da commit 62da705

File tree

7 files changed

+90
-48
lines changed

7 files changed

+90
-48
lines changed

assets/img/wcpos-icon.svg

Lines changed: 16 additions & 10 deletions
Loading

includes/API/Auth.php

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,24 @@ public function __construct() {
4343
*/
4444
public function register_routes(): void {
4545
// Generate JWT token
46-
// register_rest_route(
47-
// $this->namespace,
48-
// '/' . $this->rest_base . '/authorize',
49-
// array(
50-
// 'methods' => WP_REST_Server::READABLE,
51-
// 'callback' => array( $this, 'generate_token' ),
52-
// 'permission_callback' => function ( WP_REST_Request $request ) {
53-
// $authorization = $request->get_header( 'authorization' );
54-
//
55-
// return ! is_null( $authorization );
56-
// },
57-
// )
58-
// );
46+
register_rest_route(
47+
$this->namespace,
48+
'/' . $this->rest_base . '/authorize',
49+
array(
50+
'methods' => WP_REST_Server::READABLE,
51+
'callback' => array( $this, 'generate_token' ),
52+
'permission_callback' => function ( WP_REST_Request $request ) {
53+
// special case for user=demo param
54+
if ( $request->get_param( 'user' ) === 'demo' ) {
55+
return true;
56+
}
57+
58+
$authorization = $request->get_header( 'authorization' );
59+
60+
return ! is_null( $authorization );
61+
},
62+
)
63+
);
5964

6065
// Validate JWT token
6166
register_rest_route(
@@ -125,20 +130,20 @@ public function generate_token( WP_REST_Request $request ) {
125130
/** Try to authenticate the user with the passed credentials*/
126131
$user = wp_authenticate( $username, $password );
127132

128-
// If the authentication fails return a error
133+
// If the authentication fails return an error
129134
if ( is_wp_error( $user ) ) {
130135
$error_code = $user->get_error_code();
131136

132-
return new WP_Error(
137+
$data = new WP_Error(
133138
'woocommerce_pos_' . $error_code,
134139
$user->get_error_message( $error_code ),
135140
array(
136141
'status' => 403,
137142
)
138143
);
139-
}
140-
141-
$data = $this->auth_service->generate_token( $user );
144+
} else {
145+
$data = $this->auth_service->generate_token( $user );
146+
}
142147

143148
/**
144149
* Let the user modify the data before sending it back

includes/Init.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@ public function __construct() {
3434

3535
// Headers for API discoverability
3636
add_filter( 'rest_pre_serve_request', array( $this, 'rest_pre_serve_request' ), 5, 4 );
37-
add_action( 'send_headers', array( $this, 'send_headers' ), 10, 1 );
37+
add_action( 'send_headers', array( $this, 'send_headers' ), 99, 1 );
3838

3939
// Hack - remove when possible
4040
add_filter( 'woocommerce_rest_shop_order_schema', array( $this, 'shop_order_schema' ), 10, 1 );
41-
add_filter( 'option_wpseo', array( $this, 'remove_wpseo_rest_api_links' ), 10, 1 );
4241
}
4342

4443
/**
@@ -170,19 +169,12 @@ private function integrations(): void {
170169
// if ( class_exists( 'WC-Bookings' ) ) {
171170
// new Integrations\Bookings();
172171
// }
173-
}
174172

175-
/**
176-
* Yoast SEO adds SEO to the WC REST API by default, this adds to the download weight and can cause problems
177-
* It is programmatically turned off here for POS requests
178-
* This gets loaded and cached before the rest_api init hook, so we can't use the filter
179-
*/
180-
public function remove_wpseo_rest_api_links( $wpseo_options ) {
181-
if ( woocommerce_pos_request() ) {
182-
$wpseo_options['remove_rest_api_links'] = true;
183-
$wpseo_options['enable_headless_rest_endpoints'] = false;
184-
return $wpseo_options;
185-
}
186-
return $wpseo_options;
173+
// Yoast SEO - https://wordpress.org/plugins/wordpress-seo/
174+
if ( class_exists( 'WPSEO_Options' ) ) {
175+
new Integrations\WPSEO();
176+
}
187177
}
178+
179+
188180
}

includes/Integrations/WPSEO.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace WCPOS\WooCommercePOS\Integrations;
4+
5+
/**
6+
* Yoast SEO Integration
7+
*/
8+
class WPSEO {
9+
public function __construct() {
10+
add_filter( 'option_wpseo', array( $this, 'remove_wpseo_rest_api_links' ), 10, 1 );
11+
}
12+
13+
/**
14+
* Yoast SEO adds SEO to the WC REST API by default, this adds to the download weight and can cause problems
15+
* It is programmatically turned off here for POS requests
16+
* This gets loaded and cached before the rest_api init hook, so we can't use the filter
17+
*
18+
* QUESTION: How long is the WPSEO_Options cache persisted?
19+
*/
20+
public function remove_wpseo_rest_api_links( $wpseo_options ) {
21+
$wpseo_options['remove_rest_api_links'] = false; // needed for WC API discovery
22+
23+
if ( woocommerce_pos_request() ) {
24+
$wpseo_options['remove_rest_api_links'] = true;
25+
$wpseo_options['enable_headless_rest_endpoints'] = false;
26+
return $wpseo_options;
27+
}
28+
29+
return $wpseo_options;
30+
}
31+
}

includes/Services/Auth.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use WCPOS\WooCommercePOS\API\Stores;
1010
use WP_Error;
1111
use WP_User;
12+
use const DAY_IN_SECONDS;
1213

1314
class Auth {
1415
/**

includes/Templates/Frontend.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public function __construct() {
2828
}
2929

3030

31+
/**
32+
* @return void
33+
*/
3134
public function get_template(): void {
3235
// force ssl
3336
if ( ! is_ssl() && woocommerce_pos_get_settings( 'general', 'force_ssl' ) ) {

includes/Templates/Login.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace WCPOS\WooCommercePOS\Templates;
44

55
use WCPOS\WooCommercePOS\Services\Auth;
6+
use WP_User;
67

78
class Login {
89
/**
@@ -25,7 +26,9 @@ private function send_headers() {
2526
header( 'Content-Security-Policy: frame-ancestors http://localhost:* https://localhost:*' );
2627
}
2728

28-
29+
/**
30+
* @return void
31+
*/
2932
public function get_template(): void {
3033
$login_attempt = isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce_pos_login' );
3134

@@ -35,7 +38,7 @@ public function get_template(): void {
3538
$creds = array();
3639
$creds['user_login'] = $_POST['log'];
3740
$creds['user_password'] = $_POST['pwd'];
38-
// $creds['remember'] = isset( $_POST['rememberme'] );
41+
// $creds['remember'] = isset( $_POST['rememberme'] );
3942

4043
$user = wp_signon( $creds, false );
4144

@@ -44,7 +47,7 @@ public function get_template(): void {
4447
$error_string .= '<p class="error">' . $error[0] . '</p>';
4548
}
4649
} else {
47-
$this->login_success();
50+
$this->login_success( $user );
4851
exit;
4952
}
5053
}
@@ -62,10 +65,11 @@ public function get_template(): void {
6265
/**
6366
* Login success
6467
*
68+
* @param WP_User $user
69+
*
6570
* @return void
6671
*/
67-
private function login_success() {
68-
$user = wp_get_current_user();
72+
private function login_success( WP_User $user ) {
6973
$user_data = $this->auth_service->get_user_data( $user );
7074
$credentials = wp_json_encode( $user_data );
7175

0 commit comments

Comments
 (0)