'creation date (desc)',
'lastupdate' => 'last update (desc)'
);
private $periods = array(
'all' => 'all',
'lasthour' => 'last hour',
'lastday' => 'last day',
'lastweek' => 'last week',
'lastmonth' => 'last month',
'lastyear' => 'last year'
);
public function __construct() {
$this->root_url = get_option(P3QVC_PLUGIN_SUFFIX."root_url", "https://sdn.3qsdn.com/api/v2/");
add_action( 'rest_api_init', array( $this , 'p3qvc_init' ), 2 );
}
/**
* function init
* @desc initializes the API-class
*/
public function p3qvc_init () {
$this->apitoken = get_option(P3QVC_PLUGIN_SUFFIX.'api_token', '');
if($this->apitoken) {
// prepare options for http-requests to the 3Q-API
$options = array(
'http'=>array(
'method'=>"GET",
'header'=>"Content-Type: application/json\r\n" ."X-AUTH-APIKEY: ".$this->apitoken."\r\n"
)
);
$this->requestContext = stream_context_create($options);
// load all projects related to the api-token
$this->projects = $this->getProjects();
}
if(isset($_GET['projectId']) && is_numeric($_GET['projectId'])){
$this->projectId = sanitize_text_field($_GET['projectId']);
} else {
if(!empty($this->projects['vod'])) {
$this->projectId = reset($this->projects['vod'])['Id'];
} else {
$this->projectId = null;
}
}
if(isset($_GET['projectSecret'])){
$this->projectSecret = sanitize_text_field($_GET['projectSecret']);
} else {
if(!empty($this->projects['vod'])) {
$this->projectSecret = reset($this->projects['vod'])['SecurityKey'];
} else {
$this->projectSecret = null;
}
}
if(get_option(P3QVC_PLUGIN_SUFFIX.'pager') != false) {
$this->limit = get_option(P3QVC_PLUGIN_SUFFIX.'pager');
}
if(isset($_GET['offset']) && is_numeric($_GET['offset'])) {
$this->offset = sanitize_text_field($_GET['offset']);
}
if(isset($_GET['catId']) && is_numeric($_GET['catId'])) {
$this->catId = sanitize_text_field($_GET['catId']);
}
if(isset($_GET['orderby']) && is_numeric($_GET['orderby'])) {
$this->orderBy = sanitize_text_field($_GET['orderby']);
}
if(isset($_GET['period']) && in_array($_GET['period'], $this->periods)) {
$this->period = sanitize_text_field($_GET['period']);
}
register_rest_route( '3q-video-connect/v1', 'getVideoList/json', array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this , 'getJsonVideoList'),
) );
register_rest_route( '3q-video-connect/v1', 'getVideoList/html', array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this , 'getVideoList'),
) );
register_rest_route( '3q-video-connect/v1', 'getChannelList/html', array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this , 'getChannelList'),
) );
register_rest_route( '3q-video-connect/v1', 'getUploadForm/html', array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this , 'getUploadForm'),
) );
}
/**
* function getUploadForm
* @desc provides the generated html-code for the video-upload view
* @param \WP_REST_Request $request
* @return mixed
*/
public function getUploadForm( \WP_REST_Request $request ){
$jsonArr = array();
$htmlOutput = "";
$jsonArr = array();
$htmlOutput = "";
if ($this->apitoken == null || $this->apitoken == '' || $this->apitoken === false) {
$error['error'] = "apitoken";
$error['message'] = "No API token exists or API token is invalid. Please enter an API token in the settings for the 3Q-Videos plugin.";
ob_start();
include P3QVC_VIDEOS_ROOT_PATH.'/templates/error_template.php';
$htmlOutput .= ob_get_clean();
} elseif ($this->projectId== null || $this->projectId== '') {
$error['error'] = "projectid";
$error['message'] = "No project id available. Please enter a Project Id in the settings for the 3Q-Videos plugin.";
ob_start();
include P3QVC_VIDEOS_ROOT_PATH.'/templates/error_template.php';
$htmlOutput .= ob_get_clean();
} else {
$vod_projects = $this->projects['vod'];
if (!empty($vod_projects)) {
// create the select field to set the project to upload the file
$uploadProjectSelector = "
";
$uploadProjectSelector .= "
";
ob_start();
include P3QVC_VIDEOS_ROOT_PATH . '/templates/upload_template.php';
$htmlOutput .= ob_get_clean();
} else {
$error['error'] = "projectid";
$error['message'] = "Your API token does not have a VOD project associated with it. Therefore, the upload is currently not possible.";
ob_start();
include P3QVC_VIDEOS_ROOT_PATH . '/templates/error_template.php';
$htmlOutput .= ob_get_clean();
}
}
$jsonArr['html'] = $htmlOutput;
return json_decode(json_encode($jsonArr));
}
/**
* function provides the html code to display all channels (livestream and audio)
* @param \WP_REST_Request $request
* @return string
*/
public function getChannelList( \WP_REST_Request $request ) {
$jsonArr = array();
$htmlOutput = "";
if ($this->apitoken == null || $this->apitoken == '' || $this->apitoken === false) {
$error['error'] = "apitoken";
$error['message'] = "No API token exists or API token is invalid. Please enter an API token in the settings for the 3Q-Videos plugin.";
ob_start();
include P3QVC_VIDEOS_ROOT_PATH.'/templates/error_template.php';
$htmlOutput .= ob_get_clean();
} elseif ($this->projectId== null || $this->projectId== '') {
$error['error'] = "projectid";
$error['message'] = "No project id available. Please enter a Project Id in the settings for the 3Q-Videos plugin.";
ob_start();
include P3QVC_VIDEOS_ROOT_PATH.'/templates/error_template.php';
$htmlOutput .= ob_get_clean();
} else {
$channels = $this->getChannels();
$htmlOutput = "
";
}
$jsonArr['html'] = $htmlOutput;
return json_decode(json_encode($jsonArr));
}
/**
* function provides html code for videos
* @param \WP_REST_Request $request
* @return mixed
*/
public function getVideoList( \WP_REST_Request $request ) {
$jsonArr = array();
$htmlOutput = "";
if ($this->apitoken == null || $this->apitoken == '' || $this->apitoken === false) {
$error['error'] = "apitoken";
$error['message'] = "No API token exists or API token is invalid. Please enter an API token in the settings for the 3Q-Videos plugin.";
ob_start();
include P3QVC_VIDEOS_ROOT_PATH.'/templates/error_template.php';
$htmlOutput .= ob_get_clean();
} elseif ($this->projectId== null || $this->projectId== '') {
$error['error'] = "projectid";
$error['message'] = "No project id available. Please enter a Project Id in the settings for the 3Q-Videos plugin.";
ob_start();
include P3QVC_VIDEOS_ROOT_PATH.'/templates/error_template.php';
$htmlOutput .= ob_get_clean();
} else {
$videos = $this->requestVideos();
if(is_array($videos) && empty($videos)){
$htmlOutput .= "
";
$error['error'] = "no-items";
$error['message'] = "This query returned an empty result, please try something else.";
ob_start();
include P3QVC_VIDEOS_ROOT_PATH.'/templates/error_template.php';
$htmlOutput .= ob_get_clean();
} elseif($videos == false){
$error['error'] = "request-failed";
$error['message'] = "The API token and / or the project id seem to be wrong. Please correct your settings. ";
$error['message'] .= "An HTTP error has occurred (404 Forbidden | 403 Not Found). ";
ob_start();
include P3QVC_VIDEOS_ROOT_PATH.'/templates/error_template.php';
$htmlOutput .= ob_get_clean();
} else {
$htmlOutput .= "