Skip to content

Commit

Permalink
fix WPSEO integration
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Jul 18, 2023
1 parent 4b2d0da commit 62da705
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 48 deletions.
26 changes: 16 additions & 10 deletions assets/img/wcpos-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 23 additions & 18 deletions includes/API/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,24 @@ public function __construct() {
*/
public function register_routes(): void {
// Generate JWT token
// register_rest_route(
// $this->namespace,
// '/' . $this->rest_base . '/authorize',
// array(
// 'methods' => WP_REST_Server::READABLE,
// 'callback' => array( $this, 'generate_token' ),
// 'permission_callback' => function ( WP_REST_Request $request ) {
// $authorization = $request->get_header( 'authorization' );
//
// return ! is_null( $authorization );
// },
// )
// );
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/authorize',
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'generate_token' ),
'permission_callback' => function ( WP_REST_Request $request ) {
// special case for user=demo param
if ( $request->get_param( 'user' ) === 'demo' ) {
return true;
}

$authorization = $request->get_header( 'authorization' );

return ! is_null( $authorization );
},
)
);

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

// If the authentication fails return a error
// If the authentication fails return an error
if ( is_wp_error( $user ) ) {
$error_code = $user->get_error_code();

return new WP_Error(
$data = new WP_Error(
'woocommerce_pos_' . $error_code,
$user->get_error_message( $error_code ),
array(
'status' => 403,
)
);
}

$data = $this->auth_service->generate_token( $user );
} else {
$data = $this->auth_service->generate_token( $user );
}

/**
* Let the user modify the data before sending it back
Expand Down
22 changes: 7 additions & 15 deletions includes/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ public function __construct() {

// Headers for API discoverability
add_filter( 'rest_pre_serve_request', array( $this, 'rest_pre_serve_request' ), 5, 4 );
add_action( 'send_headers', array( $this, 'send_headers' ), 10, 1 );
add_action( 'send_headers', array( $this, 'send_headers' ), 99, 1 );

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

/**
Expand Down Expand Up @@ -170,19 +169,12 @@ private function integrations(): void {
// if ( class_exists( 'WC-Bookings' ) ) {
// new Integrations\Bookings();
// }
}

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


}
31 changes: 31 additions & 0 deletions includes/Integrations/WPSEO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace WCPOS\WooCommercePOS\Integrations;

/**
* Yoast SEO Integration
*/
class WPSEO {
public function __construct() {
add_filter( 'option_wpseo', array( $this, 'remove_wpseo_rest_api_links' ), 10, 1 );
}

/**
* Yoast SEO adds SEO to the WC REST API by default, this adds to the download weight and can cause problems
* It is programmatically turned off here for POS requests
* This gets loaded and cached before the rest_api init hook, so we can't use the filter
*
* QUESTION: How long is the WPSEO_Options cache persisted?
*/
public function remove_wpseo_rest_api_links( $wpseo_options ) {
$wpseo_options['remove_rest_api_links'] = false; // needed for WC API discovery

if ( woocommerce_pos_request() ) {
$wpseo_options['remove_rest_api_links'] = true;
$wpseo_options['enable_headless_rest_endpoints'] = false;
return $wpseo_options;
}

return $wpseo_options;
}
}
1 change: 1 addition & 0 deletions includes/Services/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use WCPOS\WooCommercePOS\API\Stores;
use WP_Error;
use WP_User;
use const DAY_IN_SECONDS;

class Auth {
/**
Expand Down
3 changes: 3 additions & 0 deletions includes/Templates/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public function __construct() {
}


/**
* @return void
*/
public function get_template(): void {
// force ssl
if ( ! is_ssl() && woocommerce_pos_get_settings( 'general', 'force_ssl' ) ) {
Expand Down
14 changes: 9 additions & 5 deletions includes/Templates/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace WCPOS\WooCommercePOS\Templates;

use WCPOS\WooCommercePOS\Services\Auth;
use WP_User;

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


/**
* @return void
*/
public function get_template(): void {
$login_attempt = isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce_pos_login' );

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

$user = wp_signon( $creds, false );

Expand All @@ -44,7 +47,7 @@ public function get_template(): void {
$error_string .= '<p class="error">' . $error[0] . '</p>';
}
} else {
$this->login_success();
$this->login_success( $user );
exit;
}
}
Expand All @@ -62,10 +65,11 @@ public function get_template(): void {
/**
* Login success
*
* @param WP_User $user
*
* @return void
*/
private function login_success() {
$user = wp_get_current_user();
private function login_success( WP_User $user ) {
$user_data = $this->auth_service->get_user_data( $user );
$credentials = wp_json_encode( $user_data );

Expand Down

0 comments on commit 62da705

Please sign in to comment.