/**
* Plugin Name: Audioline-rce
* Author: HorysConsulting
* Author URI: https://horysconsulting.com
*/
class AudiolineRce {
protected $api_key ='a507b9c5-2bdb-4d22-9b73-cb1931535f8b';
protected $external_plugin_url = 'https://app.showcast.live';
protected $external_token_validity = 60*60*8;
function init(){
add_action( 'rest_api_init', function () {
register_rest_route( 'audioline_rce', 'external_login', array(
'methods' => 'POST',
'callback' => array($this,'external_login'),
'permission_callback' => array( $this, 'check_access' )
));
register_rest_route( 'audioline_rce', 'external_auth', array(
'methods' => 'POST',
'callback' => array($this,'external_auth'),
'permission_callback' => array( $this, 'check_access' )
));
register_rest_route( 'audioline_rce', 'external_users', array(
'methods' => 'POST',
'callback' => array($this,'external_users'),
'permission_callback' => array( $this, 'check_access' )
));
});
//add_action('wp_login', 'register_external_plugin_token');
add_shortcode( "audioline_rce", array($this,"shortcode_handler_function"));
}
function check_access(WP_REST_Request $request){
$key = $request->get_header('api_key');
if($key == $this->api_key){
return true;
}
return false;
}
function external_login( $data ) {
$user=wp_authenticate($data['username'], $data['password'] );
return $user;
}
function external_auth($data){
//falta authenticar
$users=get_users(array('meta_key' => 'external_plugin_token', 'meta_value' => $data["external_plugin_token"]));
return $users[0];
}
function external_users($data){
//falta authenticar
$users=get_users();//array('role'=>$data["role"])
return $users;
}
function register_external_plugin_token() {
$seed=wp_rand(10000000,99999999);
$token=hash('sha256', "ext_plugin_token".$seed);
$token_expiration_date=get_user_meta( get_current_user_id(),'external_plugin_token_validity', true );
$old_token=get_user_meta( get_current_user_id(),'external_plugin_token', true );
if(is_int((int)$token_expiration_date)){
if((time() - (int)$token_expiration_date) > $this->external_token_validity){
update_user_meta( get_current_user_id(), "external_plugin_token", $token);
update_user_meta( get_current_user_id(), "external_plugin_token_validity", time());
}
}elseif($token_expiration_date=="" or $old_token==""){
update_user_meta( get_current_user_id(), "external_plugin_token", $token );
update_user_meta( get_current_user_id(), "external_plugin_token_validity", time());
}
}
function shortcode_handler_function( $atts, $content, $tag ){
//$url = 'http://audioline.horysconsulting.com';
$url = $this->external_plugin_url;
$this->register_external_plugin_token();
$auth_token=get_user_meta( get_current_user_id(),'external_plugin_token', true );
$lang = isset($atts['lang']) ? $atts['lang'] : "en";
return '