options = $options;
$options = $options['pqc_astroprint_settings'];
$this->files = $files;
$this->license_key = @get_option( 'pqc-license', null )['code'];
$this->url = PQC_API_PHANES_URI;
$this->headers = array(
"PHANES3DP-ASTROPRINT: 1",
'PHANES3DP-HOST: ' . home_url(),
'PHANES3DP-LICENSE-KEY: ' . $this->license_key,
'PHANES3DP-TOKEN: '. $options['request_token'],
);
$project_id = $options['folder_id'];
$project_name = $options['folder_name'];
$this->project_id = empty($project_id) ? null : $project_id;
$this->project_name = empty($project_name) ? null : $project_name;
$this->free_use = null;
}
public function request()
{
$send = $this->send();
if ($send === false) return;
$this->project_name = empty($this->project_name) ?
uniqid('PH3DP_') : $this->project_name;
$create = $this->createProject();
if ($create === false) return;
$this->files = $send;
$move = $this->move();
$options = $this->options;
$args = wp_parse_args( [
'folder_id' => $this->project_id,
'folder_name' => $this->project_name,
'request_token' => $this->request_token,
], $options['pqc_astroprint_settings'] );
if (!pqc_is_array_equal( $args, $options['pqc_astroprint_settings'] )) {
$options['pqc_astroprint_settings'] = $args;
update_option( PQC_SETTING_OPTIONS, $options );
}
$exists = '';
if (!empty($this->already_exists)) $exists = join(', ', $this->already_exists);
if (count($this->already_exists) == count($this->files)) {
$msg = "
The file(s) to be uploaded already exist in your file manager.
They will be moved to $this->project_name folder if necessary.
($exists)
";
} else {
$msg = "
Files has been synchronized successfully. Folder name: $this->project_name";
if (!empty($exists)) {
$msg .= "
Note* Some file(s) already exists in your file manager:
($exists)
";
}
}
$_SESSION['astroprint_msg'] = $msg;
if (isset($this->free_use) && !empty($this->free_use)) {
$_SESSION['astroprint_msg'] .= $this->free_use;
}
return;
}
private function send()
{
$this->intent = 'designs';
$this->body = [
'type' => 'create',
'id' => '',
'action' => '',
'data' => $this->files,
];
$response = $this->makePostCall();
if (
isset( $response['error'] ) &&
$response['error'] === true &&
isset( $response['authorization_url'] )
) {
$msg = $response['error_description'] .
'
' . __( 'Authorize Phanes3dp now', 'pqc' ) . '
';
if (isset($response['free_use_url'])) {
$text = __( 'Authorize free app now', 'pqc' );
$msg .= sprintf(
__(' Or %s', 'pqc'),
$response['free_use_url'], $text
);
}
} elseif (! $response ||
(isset( $response['error'] ) && $response['error'] === true)
) {
if (isset($response['error_description'])) {
$msg = $response['error_description'];
} elseif (isset($response['response'])) {
$msg = $response['response'];
} else {
$msg = 'Something went wrong, please try again';
}
} else {
if (isset($response['token'])) {
$token = sanitize_text_field( $response['token'] );
$this->request_token = $token;
unset($response['token']);
}
if (isset($response['free_use_url'])) {
$text = __( 'Authorize free app now', 'pqc' );
$msg .= sprintf(
__('
%s or ignore to continue with premium', 'pqc'),
$response['free_use_url'], $text
);
$this->free_use = $msg;
}
if (isset($response['data']) || ! empty($response)) {
$data = isset($response['data']) ?
$response['data'] : $response;
unset($_SESSION['astroprint_msg']);
return $response;
} else {
$msg = 'Something went wrong, please try again';
}
}
$_SESSION['astroprint_msg'] = $msg;
return false;
}
private function move()
{
$move = [];
$exists = [];
foreach($this->files as $name => $info) {
if (!is_array($info) && !is_object($info)) continue;
if (is_object($info)) $info = json_decode(json_encode($info), true);
if (!isset($info['design_id'])) continue;
$project_id = $info['project_id'];
if ($project_id != $this->project_id) {
$move[] = $info['design_id'];
}
// Check if file already exists
if ( (bool) $info['already_exists'] === true ) $exists[] = $name;
}
if (!empty($move)) {
foreach($move as $design_id) {
$ok = $this->moveFile($design_id);
}
}
$this->already_exists = $exists;
}
private function moveFile($id)
{
$this->intent = 'designs';
$this->body = [
'type' => 'update',
'id' => $id,
'action' => '',
'data' => ['project_id' => $this->project_id],
];
$response = $this->makePostCall();
if (
isset( $response['error'] ) &&
$response['error'] === true &&
isset( $response['authorization_url'] )
) {
$msg = $response['error_description'] .
'
' . __( 'Authorize Phanes3dp now', 'pqc' ) . '
';
} elseif (! $response ||
(isset( $response['error'] ) && $response['error'] === true)
) {
if (isset($response['error_description'])) {
$msg = $response['error_description'];
} elseif (isset($response['response'])) {
$msg = $response['response'];
} else {
$msg = 'Something went wrong, please try again';
}
} else {
if (isset($response['token'])) {
$token = sanitize_text_field( $response['token'] );
$this->request_token = $token;
unset($response['token']);
}
if ($response != 'OK') {
unset($_SESSION['astroprint_msg']);
return true;
} else {
$msg = $response;
}
}
$_SESSION['astroprint_msg'] = $msg;
return false;
}
private function getProject()
{
$this->intent = 'projects';
$this->body = [
'type' => 'read',
'id' => '',
'action' => '',
'data' => [
'query' => $this->project_name
],
];
$response = $this->makePostCall();
if (
isset( $response['error'] ) &&
$response['error'] === true &&
isset( $response['authorization_url'] )
) {
$msg = $response['error_description'] .
'
' . __( 'Authorize Phanes3dp now', 'pqc' ) . '
';
} elseif (! $response ||
(isset( $response['error'] ) && $response['error'] === true)
) {
if (isset($response['error_description'])) {
$msg = $response['error_description'];
} elseif (isset($response['response'])) {
$msg = $response['response'];
} else {
$msg = 'Something went wrong, please try again';
}
} else {
if (isset($response['token'])) {
$token = sanitize_text_field( $response['token'] );
$this->request_token = $token;
unset($response['token']);
}
if (isset($response['data']) || ! empty($response)) {
$data = isset($response['data']) ?
$response['data'] : $response;
unset($_SESSION['astroprint_msg']);
return $data;
} else {
$msg = 'Something went wrong, please try again';
}
}
$_SESSION['astroprint_msg'] = $msg;
return false;
}
private function createProject()
{
$response = $this->getProject();
if ($response !== false && !empty($response)) {
$this->project_id = $response[0]['id'];
return true;
}
$this->intent = 'projects';
$this->body = [
'type' => 'create',
'id' => '',
'action' => '',
'data' => [
'name' => $this->project_name
],
];
$response = $this->makePostCall();
if (
isset( $response['error'] ) &&
$response['error'] === true &&
isset( $response['authorization_url'] )
) {
$msg = $response['error_description'] .
'
' . __( 'Authorize Phanes3dp now', 'pqc' ) . '
';
} elseif (! $response ||
(isset( $response['error'] ) && $response['error'] === true)
) {
if (isset($response['error_description'])) {
$msg = $response['error_description'];
} elseif (isset($response['response'])) {
$msg = $response['response'];
} else {
$msg = 'Something went wrong, please try again';
}
} else {
if (isset($response['token'])) {
$token = sanitize_text_field( $response['token'] );
$this->request_token = $token;
unset($response['token']);
}
if (isset($response['data']) || ! empty($response)) {
$data = isset($response['data']) ?
$response['data'] : $response;
$this->project_id = $response['id'];
unset($_SESSION['astroprint_msg']);
return true;
} else {
$msg = 'Something went wrong, please try again';
}
}
$_SESSION['astroprint_msg'] = $msg;
return false;
}
private function makePostCall()
{
$curl = curl_init();
$headers = $this->headers;
$headers[] = "PHANES3DP-INTENT: $this->intent";
curl_setopt_array( $curl, array(
CURLOPT_URL => $this->url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POST => true,
CURLOPT_MAXREDIRS => 5,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => array(
'data' => json_encode( $this->body )
),
) );
$response = curl_exec( $curl );
if ( empty( $response ) ) {
// some kind of an error happened
$error = curl_error( $curl );
error_log( $error );
curl_close( $curl ); // close cURL handler
return array( 'error' => true, 'response' => $error );
} else {
$info = curl_getinfo( $curl );
curl_close( $curl ); // close cURL handler
if ( $info['http_code'] != 200 && $info['http_code'] != 201 ) {
error_log( $response );
return array( 'error' => true, 'response' => $response );
}
}
// Convert the result from JSON format to a PHP array
$json_response = json_decode( $response, true );
return $json_response;
}
}