Skip to content

Commit

Permalink
Merge pull request #13 from mspecs/no-jira/wp-redirect-token
Browse files Browse the repository at this point in the history
feat (api): add redirection hook with token
  • Loading branch information
axeldetlofsson committed May 22, 2023
2 parents 60378f3 + 1aea9ef commit a8a348e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion inc/class.mspecs-api-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,4 @@ public function add_buyer_to_viewing_slot($deal_id, $viewing_id, $slot_id, $buye
public function generate_webhook_secret(){
return $this->request('PUT', 'provider/webhookSecret');
}
}
}
29 changes: 29 additions & 0 deletions inc/class.mspecs-rest-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

class Mspecs_Rest_Api {
public static function init(){
add_action('rest_api_init', function(){
register_rest_route('mspecs', '/deal/', array(
'methods' => 'GET',
'callback' => array('Mspecs_Rest_Api', 'handle_redirect'),
));
});
}

public static function handle_redirect(){
$token = $_GET['token'];
$token_parts = explode('.', $token);
$payload = $token_parts[1];
$payload = base64_decode($payload);

$payload = json_decode($payload, true);

if(!$payload){
echo 'Error decoding payload: '.json_last_error_msg();
return;
}

do_action( 'redirect_to_deal', $payload);
}
}

2 changes: 1 addition & 1 deletion inc/class.mspecs-webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,4 @@ public static function install(){
$api_client->update_service($service_id, $data);
}
}
}
}
3 changes: 2 additions & 1 deletion inc/class.mspecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public static function init() {
Mspecs_Sync_Manager::init();
Mspecs_Webhook::init();
Mspecs_Error_Handler::init();
Mspecs_Rest_Api::init();

self::register_post_types();
}
Expand Down Expand Up @@ -161,4 +162,4 @@ public static function get_api_client(){

return $api_client;
}
}
}
3 changes: 2 additions & 1 deletion mspecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
require_once( MSPECS_PLUGIN_DIR . 'inc/class.mspecs-sync-manager.php' );
require_once( MSPECS_PLUGIN_DIR . 'inc/class.mspecs-webhook.php' );
require_once( MSPECS_PLUGIN_DIR . 'inc/class.mspecs-error-handler.php' );
require_once( MSPECS_PLUGIN_DIR . 'inc/class.mspecs-rest-api.php' );
require_once( MSPECS_PLUGIN_DIR . 'public-api.php' );

add_action( 'init', array( 'Mspecs', 'init' ) );
add_action( 'plugins_loaded', array( 'Mspecs', 'plugins_loaded' ) );

function mspecs_test(){
mspecs_log('Test');
}
}

0 comments on commit a8a348e

Please sign in to comment.