first commit
This commit is contained in:
25
wp-content/plugins/brizy/admin/cloud/abstract-bridge.php
Normal file
25
wp-content/plugins/brizy/admin/cloud/abstract-bridge.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class Brizy_Admin_Cloud_AbstractUploader
|
||||
*/
|
||||
abstract class Brizy_Admin_Cloud_AbstractBridge implements Brizy_Admin_Cloud_BridgeInterface {
|
||||
|
||||
/**
|
||||
* @var Brizy_Admin_Cloud_Client
|
||||
*/
|
||||
protected $client;
|
||||
|
||||
/**
|
||||
* Brizy_Admin_Cloud_AbstractUploader constructor.
|
||||
*
|
||||
* @param Brizy_Admin_Cloud_Client $client
|
||||
*/
|
||||
public function __construct( $client ) {
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
public function getCurrentCloudAccountId() {
|
||||
return $this->client->getBrizyProject()->getCloudAccountId();
|
||||
}
|
||||
}
|
||||
213
wp-content/plugins/brizy/admin/cloud/api.php
Normal file
213
wp-content/plugins/brizy/admin/cloud/api.php
Normal file
@@ -0,0 +1,213 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: alex
|
||||
* Date: 7/18/18
|
||||
* Time: 10:48 AM
|
||||
*/
|
||||
|
||||
|
||||
class Brizy_Admin_Cloud_Api extends Brizy_Admin_AbstractApi {
|
||||
|
||||
use Brizy_Admin_Cloud_SyncAware;
|
||||
|
||||
const nonce = 'brizy-api';
|
||||
|
||||
const AJAX_SIGNIN_ACTION = '-cloud-signin';
|
||||
const AJAX_SIGNUP_ACTION = '-cloud-signup';
|
||||
const AJAX_SIGNOUT_ACTION = '-cloud-signout';
|
||||
const AJAX_RESET_PASSWORD_ACTION = '-cloud-resetpassword';
|
||||
const AJAX_TRIGGER_SYNC_ACTION = '-cloud-sync';
|
||||
const AJAX_SYNC_ALLOWED = '-cloud-sync-allowed';
|
||||
|
||||
/**
|
||||
* @var Brizy_Editor_Project
|
||||
*/
|
||||
private $project;
|
||||
|
||||
/**
|
||||
* @var Brizy_Admin_Cloud_Client
|
||||
*/
|
||||
private $cloudClient;
|
||||
|
||||
/**
|
||||
* Brizy_Admin_Cloud_Api constructor.
|
||||
*
|
||||
* @param Brizy_Editor_Project $project
|
||||
*/
|
||||
public function __construct( $project ) {
|
||||
|
||||
$this->project = $project;
|
||||
$this->setClient( Brizy_Admin_Cloud_Client::instance( $project, new WP_Http() ) );
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Brizy_Editor_Project $project
|
||||
*
|
||||
* @return Brizy_Admin_Cloud_Api
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function _init( $project ) {
|
||||
static $instance;
|
||||
|
||||
if ( ! $instance ) {
|
||||
$instance = new self( $project );
|
||||
}
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
|
||||
protected function initializeApiActions() {
|
||||
$pref = 'wp_ajax_' . Brizy_Editor::prefix();
|
||||
add_action( $pref . self::AJAX_SIGNIN_ACTION, array( $this, 'actionSignIn' ) );
|
||||
add_action( $pref . self::AJAX_SIGNUP_ACTION, array( $this, 'actionSignUp' ) );
|
||||
add_action( $pref . self::AJAX_SIGNOUT_ACTION, array( $this, 'actionSignOut' ) );
|
||||
add_action( $pref . self::AJAX_RESET_PASSWORD_ACTION, array( $this, 'actionResetPassword' ) );
|
||||
add_action( $pref . self::AJAX_TRIGGER_SYNC_ACTION, array( $this, 'actionSync' ) );
|
||||
add_action( $pref . self::AJAX_SYNC_ALLOWED, array( $this, 'actionSyncAllowed' ) );
|
||||
}
|
||||
|
||||
public function actionSignIn() {
|
||||
|
||||
$this->verifyNonce( self::nonce );
|
||||
|
||||
$data = file_get_contents( "php://input" );
|
||||
|
||||
if ( ( $data = json_decode( $data ) ) === false ) {
|
||||
$this->error( 400, 'Bad request' );
|
||||
}
|
||||
|
||||
if ( $token = $this->getClient()->signIn( $data->email, $data->password ) ) {
|
||||
|
||||
// this is important when you delete block from cloud the local blocks
|
||||
// will be deleted based on this valued
|
||||
$this->project->setCloudAccountId( $token );
|
||||
$this->project->setCloudToken( $token );
|
||||
|
||||
$containers = $this->getClient()->getContainers();
|
||||
|
||||
if ( isset( $containers[0] ) ) {
|
||||
$this->project->setCloudContainer( $containers[0]->id );
|
||||
} else {
|
||||
$this->error( 400, 'SingIn failed. Invalid container.' );
|
||||
}
|
||||
|
||||
$this->project->saveStorage();
|
||||
|
||||
$this->success( [] );
|
||||
} else {
|
||||
$this->error( 400, 'SingIn failed' );
|
||||
}
|
||||
}
|
||||
|
||||
public function actionSignUp() {
|
||||
|
||||
$this->verifyNonce( self::nonce );
|
||||
|
||||
$data = file_get_contents( "php://input" );
|
||||
|
||||
if ( ( $data = json_decode( $data ) ) === false ) {
|
||||
$this->error( 400, 'Bad request' );
|
||||
}
|
||||
|
||||
if ( $token = $this->getClient()->signUp( $data->firstName, $data->lastName, $data->email, $data->password, $data->confirmPassword ) ) {
|
||||
|
||||
// this is important when you delete block from cloud the local blocks
|
||||
// will be deleted based on this valued
|
||||
$this->project->setCloudAccountId( $token );
|
||||
$this->project->setCloudToken( $token );
|
||||
|
||||
$containers = $this->getClient()->getContainers();
|
||||
|
||||
if ( isset( $containers[0] ) ) {
|
||||
$this->project->setCloudContainer( $containers[0]->id );
|
||||
} else {
|
||||
$this->error( 400, 'Unable to obtain the container.' );
|
||||
}
|
||||
|
||||
$this->project->saveStorage();
|
||||
$this->success( [] );
|
||||
} else {
|
||||
$this->error( 400, 'Sing Up failed' );
|
||||
}
|
||||
}
|
||||
|
||||
public function actionSignOut() {
|
||||
|
||||
$this->verifyNonce( self::nonce );
|
||||
|
||||
// clear version in case something gets wonrg.
|
||||
Brizy_Admin_Cloud_Client::clearVersionCache();
|
||||
|
||||
// this is important when you delete block from cloud the local blocks
|
||||
// will be deleted based on this valued
|
||||
$this->project->setCloudAccountId( null );
|
||||
$this->project->setCloudToken( null );
|
||||
$this->project->saveStorage();
|
||||
|
||||
wp_clear_scheduled_hook( Brizy_Admin_Cloud_Cron::BRIZY_CLOUD_CRON_KEY );
|
||||
|
||||
$this->success( [] );
|
||||
}
|
||||
|
||||
public function actionResetPassword() {
|
||||
|
||||
$this->verifyNonce( self::nonce );
|
||||
|
||||
$data = file_get_contents( "php://input" );
|
||||
|
||||
if ( ( $data = json_decode( $data ) ) === false ) {
|
||||
$this->error( 400, 'Bad request' );
|
||||
}
|
||||
|
||||
if ( ! $data->email ) {
|
||||
$this->error( 400, 'Invalid email' );
|
||||
}
|
||||
|
||||
if ( $token = $this->getClient()->resetPassword( $data->email ) ) {
|
||||
$this->success( [] );
|
||||
} else {
|
||||
$this->error( 400, 'Reset password failed' );
|
||||
}
|
||||
}
|
||||
|
||||
public function actionSync() {
|
||||
try {
|
||||
|
||||
if ( ! $this->project->getCloudToken() ) {
|
||||
$this->error( 400, 'Unauthorized' );
|
||||
}
|
||||
|
||||
$merged = array_merge( $this->syncLayouts(0, true ), $this->syncBlocks(0, true ) );
|
||||
|
||||
return $this->success( [ 'synchronized' => count( $merged ) ] );
|
||||
} catch ( Exception $e ) {
|
||||
Brizy_Logger::instance()->critical( 'Sync failed', [ $e ] );
|
||||
$this->error( 500, 'Sync failed' );
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public function actionSyncAllowed() {
|
||||
|
||||
try {
|
||||
$versions = $this->getClient()->getCloudEditorVersions();
|
||||
$response = [];
|
||||
$response['isSyncAllowed'] = $versions['sync'] == BRIZY_SYNC_VERSION;
|
||||
|
||||
return $this->success( $response );
|
||||
} catch ( Exception $e ) {
|
||||
Brizy_Logger::instance()->critical( 'Sync failed', [ $e ] );
|
||||
$this->error( 400, 'Sync unauthorized.' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null
|
||||
*/
|
||||
protected function getRequestNonce() {
|
||||
return $this->param( 'hash' );
|
||||
}
|
||||
}
|
||||
176
wp-content/plugins/brizy/admin/cloud/block-bridge.php
Normal file
176
wp-content/plugins/brizy/admin/cloud/block-bridge.php
Normal file
@@ -0,0 +1,176 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class Brizy_Admin_Cloud_BlockUploader
|
||||
*/
|
||||
class Brizy_Admin_Cloud_BlockBridge extends Brizy_Admin_Cloud_AbstractBridge
|
||||
{
|
||||
|
||||
use Brizy_Editor_Asset_AttachmentAware;
|
||||
|
||||
/**
|
||||
* @param Brizy_Editor_Block $block
|
||||
*
|
||||
* @return mixed|void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function export($block)
|
||||
{
|
||||
|
||||
$media = json_decode($block->getMedia());
|
||||
|
||||
if (!$media || !isset($media->fonts)) {
|
||||
throw new Exception('No fonts property in media object');
|
||||
}
|
||||
|
||||
if (!$media || !isset($media->images)) {
|
||||
throw new Exception('No images property in media object');
|
||||
}
|
||||
|
||||
$bridge = new Brizy_Admin_Cloud_MediaBridge($this->client);
|
||||
foreach ($media->images as $uid) {
|
||||
try {
|
||||
$bridge->export($uid);
|
||||
} catch (Exception $e)
|
||||
{
|
||||
Brizy_Logger::instance()->critical('Failed to export block media: ' . $e->getMessage(), [$e]);
|
||||
}
|
||||
}
|
||||
|
||||
$bridge = new Brizy_Admin_Cloud_MediaUploadsBridge($this->client);
|
||||
foreach ($media->uploads as $uid) {
|
||||
try {
|
||||
$bridge->export($uid);
|
||||
} catch (Exception $e) {
|
||||
Brizy_Logger::instance()->critical('Failed to export block uploads: ' . $e->getMessage(), [$e]);
|
||||
}
|
||||
}
|
||||
|
||||
$bridge = new Brizy_Admin_Cloud_FontBridge($this->client);
|
||||
foreach ($media->fonts as $fontUid) {
|
||||
try {
|
||||
$bridge->export($fontUid);
|
||||
} catch (Exception $e) {
|
||||
Brizy_Logger::instance()->critical('Failed to export block font: ' . $e->getMessage(), [$e]);
|
||||
}
|
||||
}
|
||||
|
||||
$bridge = new Brizy_Admin_Cloud_ScreenshotBridge($this->client);
|
||||
$bridge->export($block);
|
||||
|
||||
$cloudBlockObject = $this->client->createOrUpdateBlock($block);
|
||||
|
||||
if ($cloudBlockObject) {
|
||||
$block->setSynchronized($this->getCurrentCloudAccountId(), $cloudBlockObject->uid);
|
||||
}
|
||||
|
||||
$block->saveStorage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $blockId
|
||||
*
|
||||
* @return mixed|void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function import($blockId)
|
||||
{
|
||||
global $wpdb;
|
||||
|
||||
$blocks = $this->client->getBlocks(['uid' => $blockId]);
|
||||
|
||||
if (!isset($blocks[0])) {
|
||||
Brizy_Logger::instance()->critical('Failed to import: Unable to obtain the block from cloud ' . $blockId);
|
||||
return;
|
||||
}
|
||||
|
||||
$block = (array)$blocks[0];
|
||||
|
||||
try {
|
||||
|
||||
// create local block
|
||||
$wpdb->query('START TRANSACTION ');
|
||||
$name = md5(time());
|
||||
$post = wp_insert_post(array(
|
||||
'post_title' => $name,
|
||||
'post_name' => $name,
|
||||
'post_status' => 'publish',
|
||||
'post_type' => Brizy_Admin_Blocks_Main::CP_SAVED
|
||||
));
|
||||
|
||||
if ($post) {
|
||||
$brizyPost = Brizy_Editor_Block::get($post, $block['uid']);
|
||||
if (isset($block['media'])) {
|
||||
$brizyPost->setMedia($block['media']);
|
||||
}
|
||||
if (isset($block['meta'])) {
|
||||
$brizyPost->setMeta($block['meta']);
|
||||
}
|
||||
$brizyPost->set_editor_data($block['data']);
|
||||
$brizyPost->set_uses_editor(true);
|
||||
$brizyPost->set_needs_compile(true);
|
||||
$brizyPost->setDataVersion(1);
|
||||
$brizyPost->setSynchronized($this->getCurrentCloudAccountId(), $block['id']);
|
||||
$brizyPost->save();
|
||||
|
||||
// import fonts
|
||||
if (isset($block['media'])) {
|
||||
$blockMedia = json_decode($block['media']);
|
||||
|
||||
$fontBridge = new Brizy_Admin_Cloud_FontBridge($this->client);
|
||||
if (isset($blockMedia->fonts)) {
|
||||
foreach ($blockMedia->fonts as $cloudFontUid) {
|
||||
try {
|
||||
$fontBridge->import($cloudFontUid);
|
||||
} catch (Exception $e) {
|
||||
Brizy_Logger::instance()->critical('Failed to import block media: ' . $e->getMessage(), [$e]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$mediaBridge = new Brizy_Admin_Cloud_MediaBridge($this->client);
|
||||
$mediaBridge->setBlockId($post);
|
||||
if (isset($blockMedia->images)) {
|
||||
foreach ($blockMedia->images as $mediaUid) {
|
||||
try {
|
||||
$mediaBridge->import($mediaUid);
|
||||
} catch (Exception $e) {
|
||||
Brizy_Logger::instance()->critical('Failed to import block media: ' . $e->getMessage(), [$e]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$mediaUploadBridge = new Brizy_Admin_Cloud_MediaUploadsBridge($this->client);
|
||||
$mediaUploadBridge->setBlockId($post);
|
||||
if (isset($blockMedia->uploads)) {
|
||||
foreach ($blockMedia->uploads as $mediaUpload) {
|
||||
try {
|
||||
$mediaUploadBridge->import($mediaUpload);
|
||||
} catch (Exception $e) {
|
||||
Brizy_Logger::instance()->critical('Failed to import block uploads: ' . $e->getMessage(), [$e]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$wpdb->query('COMMIT');
|
||||
} catch (Exception $e) {
|
||||
$wpdb->query('ROLLBACK');
|
||||
Brizy_Logger::instance()->critical('Failed to import block ' . $blockId, [$e]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Brizy_Editor_Block $block
|
||||
*
|
||||
* @return mixed|void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function delete($block)
|
||||
{
|
||||
if ($block->getCloudId($this->getCurrentCloudAccountId())) {
|
||||
$this->client->deleteBlock($block->getCloudId($this->getCurrentCloudAccountId()));
|
||||
}
|
||||
}
|
||||
}
|
||||
26
wp-content/plugins/brizy/admin/cloud/bridge-interface.php
Normal file
26
wp-content/plugins/brizy/admin/cloud/bridge-interface.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Interface Brizy_Admin_Cloud_UploaderInterface
|
||||
*/
|
||||
interface Brizy_Admin_Cloud_BridgeInterface {
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function export( $entity );
|
||||
|
||||
/**
|
||||
* @param $entity
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function import( $entity );
|
||||
|
||||
/**
|
||||
* @param $entity
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function delete( $entity );
|
||||
}
|
||||
916
wp-content/plugins/brizy/admin/cloud/client.php
Normal file
916
wp-content/plugins/brizy/admin/cloud/client.php
Normal file
@@ -0,0 +1,916 @@
|
||||
<?php
|
||||
|
||||
|
||||
class Brizy_Admin_Cloud_Client extends WP_Http
|
||||
{
|
||||
|
||||
use Brizy_Editor_Asset_AttachmentAware;
|
||||
|
||||
const TRANSIENT_KEY = 'brizy_cloud_editor_versions';
|
||||
|
||||
/**
|
||||
* @var Brizy_Editor_Project
|
||||
*/
|
||||
private $brizyProject;
|
||||
|
||||
/**
|
||||
* @var WP_Http
|
||||
*/
|
||||
private $http;
|
||||
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
private $library;
|
||||
|
||||
/**
|
||||
* @var Brizy_Admin_Cloud_Client
|
||||
*/
|
||||
static private $instance;
|
||||
|
||||
/**
|
||||
* @param $project
|
||||
* @param $http
|
||||
*
|
||||
* @return Brizy_Admin_Cloud_Client
|
||||
*/
|
||||
public static function instance($project, $http)
|
||||
{
|
||||
static $instance;
|
||||
if (self::$instance) {
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
return self::$instance = new self($project, $http);
|
||||
}
|
||||
|
||||
/**
|
||||
* Brizy_Admin_Cloud_Client constructor.
|
||||
*
|
||||
* @param Brizy_Editor_Project $project
|
||||
* @param WP_Http $http
|
||||
*/
|
||||
private function __construct($project, $http)
|
||||
{
|
||||
$this->brizyProject = $project;
|
||||
$this->http = $http;
|
||||
|
||||
add_action('brizy-updated', ['Brizy_Admin_Cloud_Client', 'clearVersionCache']);
|
||||
do_action('brizy-activated', ['Brizy_Admin_Cloud_Client', 'clearVersionCache']);
|
||||
}
|
||||
|
||||
public static function clearVersionCache()
|
||||
{
|
||||
delete_transient(self::TRANSIENT_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Brizy_Editor_Project
|
||||
*/
|
||||
public function getBrizyProject()
|
||||
{
|
||||
return $this->brizyProject;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Brizy_Editor_Project $brizyProject
|
||||
*
|
||||
* @return Brizy_Admin_Cloud_Client
|
||||
*/
|
||||
public function setBrizyProject($brizyProject)
|
||||
{
|
||||
$this->brizyProject = $brizyProject;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return WP_Http
|
||||
*/
|
||||
public function getHttp()
|
||||
{
|
||||
return $this->http;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_Http $http
|
||||
*
|
||||
* @return Brizy_Admin_Cloud_Client
|
||||
*/
|
||||
public function setHttp($http)
|
||||
{
|
||||
$this->http = $http;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $uid
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getScreenshotUrl($uid)
|
||||
{
|
||||
$url = Brizy_Config::getEditorBaseUrls() . Brizy_Config::CLOUD_SCREENSHOT;
|
||||
|
||||
return sprintf($url, $uid);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $screenUid
|
||||
* @param $filePath
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function createScreenshot($screenUid, $filePath)
|
||||
{
|
||||
$data = array(
|
||||
'uid' => $screenUid,
|
||||
'attachment' => base64_encode(file_get_contents($filePath))
|
||||
);
|
||||
$response = $this->http->post(Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_SCREENSHOTS, array(
|
||||
'headers' => $this->getHeaders(),
|
||||
'body' => $data
|
||||
));
|
||||
|
||||
$code = wp_remote_retrieve_response_code($response);
|
||||
|
||||
if ($code >= 200 && $code <= 300) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function getHeaders($aditional = null)
|
||||
{
|
||||
|
||||
$values = $this->getCloudEditorVersions();
|
||||
|
||||
return array_merge(array(
|
||||
//'X-AUTH-APP-TOKEN' => Brizy_Config::CLOUD_APP_KEY,
|
||||
'X-AUTH-USER-TOKEN' => $this->brizyProject->getMetaValue('brizy-cloud-token'),
|
||||
'X-EDITOR-VERSION' => $values['editor'],
|
||||
'X-SYNC-VERSION' => BRIZY_SYNC_VERSION
|
||||
), is_array($aditional) ? $aditional : array());
|
||||
}
|
||||
|
||||
private function getHeadersWithoutAuthorization($aditional = null)
|
||||
{
|
||||
return array_merge(array(
|
||||
//'X-AUTH-APP-TOKEN' => Brizy_Config::CLOUD_APP_KEY,
|
||||
'X-SYNC-VERSION' => BRIZY_SYNC_VERSION
|
||||
), is_array($aditional) ? $aditional : array());
|
||||
}
|
||||
|
||||
public function getLibraries()
|
||||
{
|
||||
$response = $this->http->get(Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_LIBRARY, array('headers' => $this->getHeaders()));
|
||||
|
||||
$code = wp_remote_retrieve_response_code($response);
|
||||
|
||||
if ($code == 200) {
|
||||
|
||||
$body = wp_remote_retrieve_body($response);
|
||||
|
||||
$libraries = json_decode($body);
|
||||
|
||||
if (count($libraries) == 0) {
|
||||
throw new Exception('No libraries provided');
|
||||
}
|
||||
|
||||
|
||||
return $libraries;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $email
|
||||
* @param $password
|
||||
*
|
||||
* @return array|bool|WP_Error
|
||||
*/
|
||||
public function signIn($email, $password)
|
||||
{
|
||||
|
||||
$response = $this->http->post(Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_SIGNIN, array(
|
||||
'headers' => $this->getHeadersWithoutAuthorization(array(
|
||||
'Content-type' => 'application/x-www-form-urlencoded'
|
||||
)),
|
||||
'body' => array(
|
||||
'email' => $email,
|
||||
'password' => $password
|
||||
),
|
||||
'timeout' => 30
|
||||
));
|
||||
|
||||
$code = wp_remote_retrieve_response_code($response);
|
||||
|
||||
if ($code == 200) {
|
||||
|
||||
$jsonResponse = json_decode($response['body']);
|
||||
|
||||
// update cloud editor versions
|
||||
$this->getCloudEditorVersions(true);
|
||||
|
||||
return $jsonResponse->token;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $firstName
|
||||
* @param string $lastName
|
||||
* @param string $email
|
||||
* @param string $password
|
||||
* @param string $confirmPassword
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function signUp($firstName, $lastName, $email, $password, $confirmPassword)
|
||||
{
|
||||
|
||||
$response = $this->http->post(Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_SIGNUP, array(
|
||||
'headers' => $this->getHeadersWithoutAuthorization(array(
|
||||
'Content-type' => 'application/x-www-form-urlencoded'
|
||||
)),
|
||||
'body' => array(
|
||||
'first_name' => $firstName,
|
||||
'last_name' => $lastName,
|
||||
'email' => $email,
|
||||
'new_password' => $password,
|
||||
'confirm_password' => $confirmPassword,
|
||||
)
|
||||
));
|
||||
|
||||
$code = wp_remote_retrieve_response_code($response);
|
||||
|
||||
if ($code == 200) {
|
||||
|
||||
$jsonResponse = json_decode($response['body']);
|
||||
|
||||
return $jsonResponse->token;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $email
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function resetPassword($email)
|
||||
{
|
||||
|
||||
$response = $this->http->post(Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_RESET_PASSWORD, array(
|
||||
'headers' => $this->getHeaders(array(
|
||||
'Content-type' => 'application/x-www-form-urlencoded'
|
||||
)),
|
||||
'body' => array(
|
||||
'email' => $email,
|
||||
)
|
||||
));
|
||||
|
||||
$code = wp_remote_retrieve_response_code($response);
|
||||
|
||||
return $code >= 200 && $code < 300;
|
||||
}
|
||||
|
||||
public function getCloudEditorVersions($ignoreCache = false)
|
||||
{
|
||||
|
||||
$value = get_transient('brizy_cloud_editor_versions');
|
||||
|
||||
if ($value && !$ignoreCache) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$url = Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_EDITOR_VERSIONS;
|
||||
|
||||
$response = $this->http->get($url);
|
||||
|
||||
$code = wp_remote_retrieve_response_code($response);
|
||||
|
||||
if ($code == 200) {
|
||||
$value = (array)json_decode($response['body']);
|
||||
set_transient('brizy_cloud_editor_versions', $value, 3600);
|
||||
} else {
|
||||
throw new Exception(wp_remote_retrieve_response_message($response));
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function getContainers()
|
||||
{
|
||||
return $this->getCloudEntity(Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_CONTAINERS, []);
|
||||
}
|
||||
|
||||
public function getProjects($filters)
|
||||
{
|
||||
return $this->getCloudEntity(Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_PROJECTS, $filters);
|
||||
}
|
||||
|
||||
public function getProject($id)
|
||||
{
|
||||
|
||||
$url = sprintf(Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_PROJECTS . "/%d", (int)$id);
|
||||
$response = $this->http->get($url, array('headers' => $this->getHeaders()));
|
||||
|
||||
$code = wp_remote_retrieve_response_code($response);
|
||||
if ($code == 200) {
|
||||
return json_decode($response['body']);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function createProject($container, $name)
|
||||
{
|
||||
|
||||
$response = $this->http->post(Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_PROJECTS, array(
|
||||
'headers' => $this->getHeaders(),
|
||||
'body' => array(
|
||||
'name' => $name,
|
||||
'container' => $container,
|
||||
'globals' => null,
|
||||
'site_id' => null
|
||||
)
|
||||
));
|
||||
|
||||
$code = wp_remote_retrieve_response_code($response);
|
||||
if ($code == 200) {
|
||||
return json_decode($response['body']);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getBlocks($filters = array())
|
||||
{
|
||||
return $this->getCloudEntityByContainer(Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_SAVEDBLOCKS, $filters);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
*
|
||||
* @return array|mixed|object|null
|
||||
*/
|
||||
public function getBlockByUid($uid)
|
||||
{
|
||||
$blocks = $this->getBlocks(['uid' => $uid]);
|
||||
|
||||
return array_pop($blocks);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
*
|
||||
* @return array|mixed|object|null
|
||||
*/
|
||||
public function getBlock($id)
|
||||
{
|
||||
$blocks = $this->getCloudEntityByContainer(Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_SAVEDBLOCKS . "/{$id}", []);
|
||||
|
||||
return array_pop($blocks);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Brizy_Editor_Block $block
|
||||
*
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function createOrUpdateBlock($block)
|
||||
{
|
||||
|
||||
$cloudBlockData = array(
|
||||
'container' => $this->brizyProject->getCloudContainer(),
|
||||
'meta' => $block->getMeta(),
|
||||
'media' => $block->getMedia(),
|
||||
'data' => $block->get_editor_data(),
|
||||
'uid' => $block->getUid(),
|
||||
'dataVersion' => 1
|
||||
);
|
||||
|
||||
$url = Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_SAVEDBLOCKS;
|
||||
$cloudUid = $block->getCloudId($this->brizyProject->getCloudAccountId());
|
||||
$cloudBlock = null;
|
||||
if (!$cloudUid && ($cloudBlock = $this->getBlockByUid($block->getUid()))) {
|
||||
$cloudUid = $cloudBlock->uid;
|
||||
}
|
||||
|
||||
|
||||
if (!$cloudUid) {
|
||||
$response = $this->http->post($url, array(
|
||||
'headers' => $this->getHeaders(),
|
||||
'body' => $cloudBlockData
|
||||
));
|
||||
} else {
|
||||
|
||||
$cloudBlockData['dataVersion'] = $cloudBlock->dataVersion + 1;
|
||||
|
||||
$response = $this->http->request($url . "/" . $cloudBlock->id, array(
|
||||
'method' => 'PUT',
|
||||
'headers' => $this->getHeaders(),
|
||||
'body' => $cloudBlockData
|
||||
));
|
||||
}
|
||||
|
||||
$code = wp_remote_retrieve_response_code($response);
|
||||
|
||||
if ($code >= 400) {
|
||||
// update cloud editor versions
|
||||
$this->getCloudEditorVersions(true);
|
||||
Brizy_Logger::instance()->critical('Cloud api exception', [$response]);
|
||||
throw new Exception(wp_remote_retrieve_response_message($response));
|
||||
}
|
||||
|
||||
return json_decode(wp_remote_retrieve_body($response));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $blockId
|
||||
*
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function deleteBlock($blockId)
|
||||
{
|
||||
$query = http_build_query(['container' => $this->brizyProject->getCloudContainer()]);
|
||||
$url = Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_SAVEDBLOCKS . "/" . $blockId . "?" . $query;
|
||||
$response = $this->http->request($url, array('method' => 'DELETE', 'headers' => $this->getHeaders()));
|
||||
$code = wp_remote_retrieve_response_code($response);
|
||||
|
||||
if ($code >= 400) {
|
||||
throw new Exception('Invalid code return by cloud api');
|
||||
}
|
||||
|
||||
return $code == 200;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $filters
|
||||
*
|
||||
* @return array|mixed|object|null
|
||||
*/
|
||||
public function getPopups($filters = array())
|
||||
{
|
||||
return $this->getCloudEntity(Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_POPUPS, $filters);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $uid
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPopupByUid($uid)
|
||||
{
|
||||
$popups = $this->getPopups(['uid' => $uid]);
|
||||
|
||||
return array_pop($popups);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Brizy_Editor_Popup $popup
|
||||
*
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function createOrUpdatePopup($popup)
|
||||
{
|
||||
|
||||
$cloudBlockData = array(
|
||||
'container' => $this->brizyProject->getCloudContainer(),
|
||||
'meta' => $popup->getMeta(),
|
||||
'data' => $popup->get_editor_data(),
|
||||
'is_autosave' => 0,
|
||||
'uid' => $popup->getUid(),
|
||||
'dataVersion' => 1
|
||||
|
||||
);
|
||||
|
||||
$url = Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_POPUPS;
|
||||
|
||||
$cloudUid = $popup->getCloudId($this->brizyProject->getCloudAccountId());
|
||||
|
||||
|
||||
if ($cloudUid) {
|
||||
$response = $this->http->request($url, array(
|
||||
'method' => 'PUT',
|
||||
'headers' => $this->getHeaders(),
|
||||
'body' => $cloudBlockData
|
||||
));
|
||||
} else {
|
||||
$response = $this->http->post($url . "/" . $cloudUid, array(
|
||||
'headers' => $this->getHeaders(),
|
||||
'body' => $cloudBlockData
|
||||
));
|
||||
}
|
||||
|
||||
$code = wp_remote_retrieve_response_code($response);
|
||||
|
||||
if ($code >= 400) {
|
||||
$this->getCloudEditorVersions(true);
|
||||
Brizy_Logger::instance()->critical('Cloud api exception', [$response]);
|
||||
throw new Exception('Invalid code return by cloud api');
|
||||
}
|
||||
|
||||
return json_decode(wp_remote_retrieve_body($response));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $popupId
|
||||
*
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function deletePopup($popupId)
|
||||
{
|
||||
$query = http_build_query(['container' => $this->brizyProject->getCloudContainer()]);
|
||||
$url = Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_POPUPS . "/" . $popupId . "?" . $query;
|
||||
$response = $this->http->request($url, array('method' => 'DELETE', 'headers' => $this->getHeaders()));
|
||||
$code = wp_remote_retrieve_response_code($response);
|
||||
|
||||
if ($code >= 400) {
|
||||
throw new Exception('Invalid code return by cloud api');
|
||||
}
|
||||
|
||||
return $code == 200;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $filters
|
||||
*
|
||||
* @return array|mixed|object|null
|
||||
*/
|
||||
public function getLayouts($filters = array())
|
||||
{
|
||||
return $this->getCloudEntity(Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_LAYOUTS, $filters);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $uid
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getLayoutByUid($uid)
|
||||
{
|
||||
$layouts = $this->getLayouts(['uid' => $uid]);
|
||||
|
||||
return array_pop($layouts);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
*
|
||||
* @return array|mixed|object|null
|
||||
*/
|
||||
public function getLayout($id)
|
||||
{
|
||||
return $this->getCloudEntityByContainer(Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_LAYOUTS . "/" . $id, []);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Brizy_Editor_Layout $layout
|
||||
*
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function createOrUpdateLayout($layout)
|
||||
{
|
||||
|
||||
$cloudBlockData = array(
|
||||
'container' => $this->brizyProject->getCloudContainer(),
|
||||
'meta' => $layout->getMeta(),
|
||||
'media' => $layout->getMedia(),
|
||||
'data' => $layout->get_editor_data(),
|
||||
'uid' => $layout->getUid(),
|
||||
'dataVersion' => 1
|
||||
);
|
||||
|
||||
$url = Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_LAYOUTS;
|
||||
|
||||
|
||||
$cloudUid = $layout->getCloudId($this->brizyProject->getCloudAccountId());
|
||||
|
||||
$cloudLayout = null;
|
||||
if (!$cloudUid && ($cloudLayout = $this->getLayoutByUid($layout->getUid()))) {
|
||||
$cloudUid = $cloudLayout->uid;
|
||||
}
|
||||
|
||||
if (!$cloudUid) {
|
||||
$response = $this->http->post($url, array(
|
||||
'headers' => $this->getHeaders(),
|
||||
'body' => $cloudBlockData
|
||||
));
|
||||
} else {
|
||||
$cloudBlockData['dataVersion'] = $cloudLayout->dataVersion + 1;
|
||||
|
||||
$response = $this->http->request($url . "/" . $cloudLayout->id, array(
|
||||
'method' => 'PUT',
|
||||
'headers' => $this->getHeaders(),
|
||||
'body' => $cloudBlockData,
|
||||
));
|
||||
}
|
||||
|
||||
$code = wp_remote_retrieve_response_code($response);
|
||||
|
||||
if ($code >= 400) {
|
||||
$this->getCloudEditorVersions(true);
|
||||
Brizy_Logger::instance()->critical('Cloud api exception', [$response]);
|
||||
throw new Exception('Invalid code return by cloud api');
|
||||
}
|
||||
|
||||
return json_decode(wp_remote_retrieve_body($response));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $layoutId
|
||||
*
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function deleteLayout($layoutId)
|
||||
{
|
||||
$query = http_build_query(['container' => $this->brizyProject->getCloudContainer()]);
|
||||
$url = Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_LAYOUTS . "/" . $layoutId . "?" . $query;
|
||||
$response = $this->http->request($url, array('method' => 'DELETE', 'headers' => $this->getHeaders()));
|
||||
$code = wp_remote_retrieve_response_code($response);
|
||||
|
||||
if ($code >= 400) {
|
||||
throw new Exception('Invalid code return by cloud api');
|
||||
}
|
||||
|
||||
return $code == 200;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $uid
|
||||
*
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function isMediaUploaded($uid)
|
||||
{
|
||||
$cloud_entity_by_container = $this->getCloudEntity(Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_MEDIA, ['name' => $uid]);
|
||||
|
||||
return is_array($cloud_entity_by_container) && count($cloud_entity_by_container) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $uid
|
||||
*
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function isCustomFileUploaded($fileUid, $customFileName)
|
||||
{
|
||||
// download file and store it in wp
|
||||
$urlBuilder = new Brizy_Editor_UrlBuilder();
|
||||
$external_asset_url = $urlBuilder->external_custom_file($fileUid, $customFileName);
|
||||
|
||||
$response = $this->http->get($external_asset_url);
|
||||
$code = wp_remote_retrieve_response_code($response);
|
||||
|
||||
if($code>=500) {
|
||||
throw new \Exception('Unable to determine if the files was uploaded or not.');
|
||||
}
|
||||
|
||||
return $code==200;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $file
|
||||
*
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function uploadMedia($uid, $file)
|
||||
{
|
||||
|
||||
$response = $this->http->post(Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_MEDIA, array(
|
||||
'headers' => $this->getHeaders(),
|
||||
'body' => array(
|
||||
'attachment' => base64_encode(file_get_contents($file)),
|
||||
'name' => $uid,
|
||||
'filename' => basename($file)
|
||||
)
|
||||
));
|
||||
|
||||
$code = wp_remote_retrieve_response_code($response);
|
||||
|
||||
if ($code >= 400) {
|
||||
throw new Exception('Invalid code return by cloud api');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function uploadCustomFile($uid, $file)
|
||||
{
|
||||
$body = array(
|
||||
'attachment' => base64_encode(file_get_contents($file)),
|
||||
'uid' => $uid,
|
||||
'filename' => basename($file),
|
||||
'container' => $this->brizyProject->getCloudContainer(),
|
||||
);
|
||||
$response = $this->http->post(Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_CUSTOM_FILES, array(
|
||||
'headers' => $this->getHeaders(),
|
||||
'body' => $body
|
||||
));
|
||||
|
||||
$code = wp_remote_retrieve_response_code($response);
|
||||
|
||||
if ($code >= 400) {
|
||||
throw new Exception('Invalid code return by cloud api');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $font
|
||||
*
|
||||
* Ex:
|
||||
* [
|
||||
* 'id' => 'askdalskdlaksd',
|
||||
* 'family' => 'proxima-nova',
|
||||
* 'type' => 'uploaded',
|
||||
* 'weights' => [
|
||||
* '400' => [
|
||||
* 'ttf' => codecept_data_dir( 'fonts/pn-regular-webfont.ttf' ),
|
||||
* 'eot' => codecept_data_dir( 'fonts/pn-regular-webfont.eot' ),
|
||||
* 'woff' => codecept_data_dir( 'fonts/pn-regular-webfont.woff' ),
|
||||
* 'woff2' => codecept_data_dir( 'fonts/pn-regular-webfont.woff2' ),
|
||||
* ],
|
||||
* '500' => [
|
||||
* 'eot' => codecept_data_dir( 'fonts/pn-medium-webfont.eot' ),
|
||||
* 'woff' => codecept_data_dir( 'fonts/pn-medium-webfont.woff' ),
|
||||
* 'woff2' => codecept_data_dir( 'fonts/pn-medium-webfont.woff2' ),
|
||||
* ],
|
||||
* '700' => [
|
||||
* 'eot' => codecept_data_dir( 'fonts/pn-bold-webfont.eot' ),
|
||||
* 'woff' => codecept_data_dir( 'fonts/pn-bold-webfont.woff' ),
|
||||
* 'woff2' => codecept_data_dir( 'fonts/pn-bold-webfont.woff2' ),
|
||||
* ],
|
||||
* ]
|
||||
* ];
|
||||
*
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function createFont($font)
|
||||
{
|
||||
|
||||
$params = array(
|
||||
'container' => $this->brizyProject->getCloudContainer(),
|
||||
'uid' => $font['id'],
|
||||
'family' => $font['family'],
|
||||
);
|
||||
|
||||
// prepare font data
|
||||
foreach ($font['weights'] as $weigth => $files) {
|
||||
foreach ($files as $type => $file) {
|
||||
$params["files[$weigth][$type]"] = new CURLFile($file);
|
||||
}
|
||||
}
|
||||
unset($font['weights']);
|
||||
|
||||
$file_upload_request = function ($handle_or_parameters, $request = '', $url = '') use ($params) {
|
||||
$this->updateWPHTTPRequest($handle_or_parameters, $params);
|
||||
};
|
||||
// handle cURL requests
|
||||
add_action('http_api_curl', $file_upload_request, 10);
|
||||
// handle fsockopen
|
||||
add_action('requests-fsockopen.before_send', $file_upload_request, 10, 3);
|
||||
|
||||
$response = $this->http->post(Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_FONTS, array(
|
||||
'headers' => $this->getHeaders(['Content-Type' => 'multipart/form-data']),
|
||||
'body' => $params,
|
||||
'timeout' => 40
|
||||
));
|
||||
|
||||
remove_action('http_api_curl', $file_upload_request);
|
||||
remove_action('requests-fsockopen.before_send', $file_upload_request);
|
||||
|
||||
$code = wp_remote_retrieve_response_code($response);
|
||||
|
||||
if ($code >= 400) {
|
||||
throw new Exception('Invalid code return by cloud api');
|
||||
}
|
||||
|
||||
return json_decode(wp_remote_retrieve_body($response));
|
||||
}
|
||||
|
||||
public function getFont($uid)
|
||||
{
|
||||
$response = $this->getCloudEntity(Brizy_Config::CLOUD_ENDPOINT . Brizy_Config::CLOUD_FONTS . "/{$uid}");
|
||||
|
||||
if (is_array($response)) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $endpoint
|
||||
* @param $filters
|
||||
*
|
||||
* @return array|mixed|object|null
|
||||
*/
|
||||
private function getCloudEntity($endpoint, $filters = array())
|
||||
{
|
||||
|
||||
$http_build_query = http_build_query($filters);
|
||||
|
||||
if ($http_build_query) {
|
||||
$http_build_query = '?' . $http_build_query;
|
||||
}
|
||||
|
||||
$url = $endpoint . $http_build_query;
|
||||
$response = $this->http->get($url, array('headers' => $this->getHeaders()));
|
||||
|
||||
$code = wp_remote_retrieve_response_code($response);
|
||||
if ($code == 200) {
|
||||
return (array)json_decode($response['body']);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $endpoint
|
||||
* @param $filters
|
||||
*
|
||||
* @return array|mixed|object|null
|
||||
*/
|
||||
private function getCloudEntityByContainer($endpoint, $filters = array())
|
||||
{
|
||||
|
||||
$filters = array_merge($filters, ['container' => $this->brizyProject->getCloudContainer()]);
|
||||
|
||||
return $this->getCloudEntity($endpoint, $filters);
|
||||
}
|
||||
|
||||
private function updateWPHTTPRequest(&$handle_or_parameters, $form_body_arguments)
|
||||
{
|
||||
if (function_exists('curl_init') && function_exists('curl_exec')) {
|
||||
curl_setopt($handle_or_parameters, CURLOPT_POSTFIELDS, $form_body_arguments);
|
||||
} elseif (function_exists('fsockopen')) {
|
||||
$form_fields = [];
|
||||
$form_files = [];
|
||||
foreach ($form_body_arguments as $name => $value) {
|
||||
if (file_exists($value)) {
|
||||
// Not great for large files since it dumps into memory but works well for small files
|
||||
$form_files[$name] = file_get_contents($value);
|
||||
} else {
|
||||
$form_fields[$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
function build_data_files($boundary, $fields, $files)
|
||||
{
|
||||
$data = '';
|
||||
$eol = "\r\n";
|
||||
|
||||
$delimiter = '-------------' . $boundary;
|
||||
|
||||
foreach ($fields as $name => $content) {
|
||||
$data .= "--" . $delimiter . $eol
|
||||
. 'Content-Disposition: form-data; name="' . $name . "\"" . $eol . $eol
|
||||
. $content . $eol;
|
||||
}
|
||||
|
||||
foreach ($files as $name => $content) {
|
||||
$data .= "--" . $delimiter . $eol
|
||||
. 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $name . '"' . $eol
|
||||
//. 'Content-Type: image/png'.$eol
|
||||
. 'Content-Transfer-Encoding: binary' . $eol;
|
||||
|
||||
$data .= $eol;
|
||||
$data .= $content . $eol;
|
||||
}
|
||||
$data .= "--" . $delimiter . "--" . $eol;
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
$boundary = uniqid("", true);
|
||||
$handle_or_parameters = build_data_files($boundary, $form_fields, $form_files);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
61
wp-content/plugins/brizy/admin/cloud/cron.php
Normal file
61
wp-content/plugins/brizy/admin/cloud/cron.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
class Brizy_Admin_Cloud_Cron {
|
||||
|
||||
use Brizy_Admin_Cloud_SyncAware;
|
||||
|
||||
const BRIZY_CLOUD_CRON_KEY = 'brizy-cloud-synchronize';
|
||||
|
||||
|
||||
public static function _init() {
|
||||
static $instance;
|
||||
|
||||
if ( ! $instance ) {
|
||||
$instance = new self( Brizy_Admin_Cloud_Client::instance( Brizy_Editor_Project::get(), new WP_Http() ) );
|
||||
}
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Brizy_Admin_Cloud_Cron constructor.
|
||||
*/
|
||||
public function __construct( $client ) {
|
||||
|
||||
$this->setClient( $client );
|
||||
|
||||
add_action( self::BRIZY_CLOUD_CRON_KEY, array( $this, 'syncBlocksAction' ) );
|
||||
add_action( self::BRIZY_CLOUD_CRON_KEY, array( $this, 'syncLayoutsAction' ) );
|
||||
|
||||
add_filter( 'cron_schedules', array( $this, 'addBrizyCloudCronSchedules' ) );
|
||||
|
||||
|
||||
if ( ! wp_next_scheduled( self::BRIZY_CLOUD_CRON_KEY ) ) {
|
||||
$interval = is_user_logged_in() ? '5minute' : 'hourly';
|
||||
|
||||
if ( is_user_logged_in() ) {
|
||||
wp_schedule_event( time(), $interval, self::BRIZY_CLOUD_CRON_KEY );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function syncLayoutsAction() {
|
||||
Brizy_Logger::instance()->debug('Sync layouts cron called');
|
||||
return $this->syncLayouts(1);
|
||||
}
|
||||
|
||||
public function syncBlocksAction() {
|
||||
Brizy_Logger::instance()->debug('Sync blocks cron called');
|
||||
return $this->syncBlocks(1);
|
||||
}
|
||||
|
||||
public function addBrizyCloudCronSchedules( $schedules ) {
|
||||
// Adds once weekly to the existing schedules.
|
||||
$schedules['5minute'] = array(
|
||||
'interval' => 300,
|
||||
'display' => __( 'Once in 5 minutes' )
|
||||
);
|
||||
|
||||
return $schedules;
|
||||
}
|
||||
}
|
||||
103
wp-content/plugins/brizy/admin/cloud/font-bridge.php
Normal file
103
wp-content/plugins/brizy/admin/cloud/font-bridge.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class Brizy_Admin_Cloud_FontBridge
|
||||
*/
|
||||
class Brizy_Admin_Cloud_FontBridge extends Brizy_Admin_Cloud_AbstractBridge {
|
||||
|
||||
/**
|
||||
* @var Brizy_Admin_Fonts_Manager
|
||||
*/
|
||||
private $fontManager;
|
||||
|
||||
/**
|
||||
* Brizy_Admin_Cloud_FontBridge constructor.
|
||||
*
|
||||
* @param Brizy_Admin_Cloud_Client $client
|
||||
*/
|
||||
public function __construct( $client ) {
|
||||
parent::__construct( $client );
|
||||
|
||||
$this->fontManager = new Brizy_Admin_Fonts_Manager();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $fontUid
|
||||
*
|
||||
* @return mixed|void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function export( $fontUid ) {
|
||||
|
||||
$fontData = $this->fontManager->getFontForExport( $fontUid );
|
||||
|
||||
if ( ! $fontData ) {
|
||||
throw new \Exception( "Unable to find font {$fontUid}" );
|
||||
}
|
||||
|
||||
if ( ! $this->client->getFont( $fontUid ) ) {
|
||||
$this->client->createFont( $fontData );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $fontUid
|
||||
*
|
||||
* @return mixed|void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function import( $fontUid ) {
|
||||
|
||||
if ( $font = $this->fontManager->getFont( $fontUid ) ) {
|
||||
return $font;
|
||||
}
|
||||
|
||||
$font = $this->client->getFont( $fontUid );
|
||||
|
||||
$family = $font['family'];
|
||||
$weights = $font['files'];
|
||||
|
||||
$newWeights = array();
|
||||
|
||||
foreach ( $weights as $weight => $weightType ) {
|
||||
foreach ( $weightType as $type => $fileUrl ) {
|
||||
$newWeights[ $weight ][ $type ] = $this->downloadFileToTemporaryFile( $fileUrl );
|
||||
}
|
||||
}
|
||||
|
||||
return $this->fontManager->createFont( $fontUid, $family, $newWeights, 'uploaded' );
|
||||
}
|
||||
|
||||
private function downloadFileToTemporaryFile( $url ) {
|
||||
$filePath = tempnam( sys_get_temp_dir(), basename( $url ) );
|
||||
$content = Brizy_Editor_Asset_StaticFile::get_asset_content( $url );
|
||||
$result = file_put_contents( $filePath, $content );
|
||||
|
||||
if ( $result === false ) {
|
||||
Brizy_Logger::instance()->critical( 'Filed to write font content',
|
||||
[
|
||||
'url' => $url,
|
||||
'filePath' => $filePath
|
||||
] );
|
||||
throw new Exception( 'Filed to write font content' );
|
||||
}
|
||||
|
||||
return array(
|
||||
'name' => basename( $url ),
|
||||
'type' => Brizy_Public_AssetProxy::get_mime( $filePath ),
|
||||
'tmp_name' => $filePath,
|
||||
'error' => 0,
|
||||
'size' => filesize( $filePath )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $fontUid
|
||||
*
|
||||
* @return mixed|void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function delete( $fontUid ) {
|
||||
throw new Exception( 'Not implemented' );
|
||||
}
|
||||
}
|
||||
178
wp-content/plugins/brizy/admin/cloud/layout-bridge.php
Normal file
178
wp-content/plugins/brizy/admin/cloud/layout-bridge.php
Normal file
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class Brizy_Admin_Cloud_BlockUploader
|
||||
*/
|
||||
class Brizy_Admin_Cloud_LayoutBridge extends Brizy_Admin_Cloud_AbstractBridge
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Brizy_Editor_Block $layout
|
||||
*
|
||||
* @return mixed|void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function export($layout)
|
||||
{
|
||||
|
||||
$media = json_decode($layout->getMedia());
|
||||
|
||||
if (!$media || !isset($media->fonts)) {
|
||||
throw new Exception('No fonts property in media object');
|
||||
}
|
||||
|
||||
if (!$media || !isset($media->images)) {
|
||||
throw new Exception('No images property in media object');
|
||||
}
|
||||
|
||||
$bridge = new Brizy_Admin_Cloud_MediaBridge($this->client);
|
||||
foreach ($media->images as $uid) {
|
||||
try {
|
||||
$bridge->export($uid);
|
||||
} catch (Exception $e) {
|
||||
Brizy_Logger::instance()->critical('Failed to export layout media: ' . $e->getMessage(), [$e]);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$bridge = new Brizy_Admin_Cloud_MediaUploadsBridge($this->client);
|
||||
foreach ($media->uploads as $uid) {
|
||||
try {
|
||||
$bridge->export($uid);
|
||||
} catch (Exception $e) {
|
||||
Brizy_Logger::instance()->critical('Failed to export layout uploads: ' . $e->getMessage(), [$e]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$bridge = new Brizy_Admin_Cloud_FontBridge($this->client);
|
||||
foreach ($media->fonts as $fontUid) {
|
||||
try {
|
||||
$bridge->export($fontUid);
|
||||
} catch (Exception $e) {
|
||||
Brizy_Logger::instance()->critical('Failed to export layout font: ' . $e->getMessage(), [$e]);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$bridge = new Brizy_Admin_Cloud_ScreenshotBridge($this->client);
|
||||
$bridge->export($layout);
|
||||
|
||||
$layoutObject = $this->client->createOrUpdateLayout($layout);
|
||||
|
||||
if ($layoutObject) {
|
||||
$layout->setSynchronized($this->getCurrentCloudAccountId(), $layoutObject->uid);
|
||||
}
|
||||
$layout->saveStorage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $layoutId
|
||||
*
|
||||
* @return mixed|void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function import($layoutId)
|
||||
{
|
||||
global $wpdb;
|
||||
|
||||
$layouts = $this->client->getLayouts(['uid' => $layoutId]);
|
||||
|
||||
if (!isset($layouts[0])) {
|
||||
Brizy_Logger::instance()->critical('Failed to import: Unable to obtain the layout from cloud ' . $layoutId);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$wpdb->query('START TRANSACTION ');
|
||||
|
||||
$layout = (array)$layouts[0];
|
||||
|
||||
$name = md5(time());
|
||||
$post = wp_insert_post(array(
|
||||
'post_title' => $name,
|
||||
'post_name' => $name,
|
||||
'post_status' => 'publish',
|
||||
'post_type' => Brizy_Admin_Layouts_Main::CP_LAYOUT
|
||||
));
|
||||
|
||||
if ($post) {
|
||||
$brizyPost = Brizy_Editor_Layout::get($post, $layout['uid']);
|
||||
|
||||
if (isset($layout['media'])) {
|
||||
$brizyPost->setMedia($layout['media']);
|
||||
}
|
||||
if (isset($layout['meta'])) {
|
||||
$brizyPost->setMeta($layout['meta']);
|
||||
}
|
||||
$brizyPost->set_editor_data($layout['data']);
|
||||
$brizyPost->set_uses_editor(true);
|
||||
$brizyPost->set_needs_compile(true);
|
||||
$brizyPost->saveStorage();
|
||||
$brizyPost->setDataVersion(1);
|
||||
$brizyPost->setSynchronized($this->getCurrentCloudAccountId(), $layout['uid']);
|
||||
$brizyPost->save();
|
||||
|
||||
|
||||
// import fonts
|
||||
if (isset($layout['media'])) {
|
||||
$blockMedia = json_decode($layout['media']);
|
||||
|
||||
$fontBridge = new Brizy_Admin_Cloud_FontBridge($this->client);
|
||||
if (isset($blockMedia->fonts)) {
|
||||
foreach ($blockMedia->fonts as $cloudFontUid) {
|
||||
try {
|
||||
$fontBridge->import($cloudFontUid);
|
||||
} catch (Exception $e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$mediaBridge = new Brizy_Admin_Cloud_MediaBridge($this->client);
|
||||
$mediaBridge->setBlockId($post);
|
||||
if (isset($blockMedia->images)) {
|
||||
foreach ($blockMedia->images as $mediaUid) {
|
||||
try {
|
||||
$mediaBridge->import($mediaUid);
|
||||
} catch (Exception $e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$mediaUploadBridge = new Brizy_Admin_Cloud_MediaUploadsBridge($this->client);
|
||||
$mediaUploadBridge->setBlockId($post);
|
||||
if (isset($blockMedia->uploads)) {
|
||||
foreach ($blockMedia->uploads as $mediaUid) {
|
||||
try {
|
||||
$mediaUploadBridge->import($mediaUid);
|
||||
} catch (Exception $e) {
|
||||
Brizy_Logger::instance()->critical('Failed to import layout uploads: ' . $e->getMessage(), [$e]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$wpdb->query('COMMIT');
|
||||
} catch (Exception $e) {
|
||||
$wpdb->query('ROLLBACK');
|
||||
Brizy_Logger::instance()->critical('Failed to import layout ' . $layoutId, [$e]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Brizy_Editor_Block $layout
|
||||
*
|
||||
* @return mixed|void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function delete($layout)
|
||||
{
|
||||
|
||||
if ($layout->getCloudId($this->getCurrentCloudAccountId())) {
|
||||
$this->client->deleteLayout($layout->getCloudId($this->getCurrentCloudAccountId()));
|
||||
}
|
||||
}
|
||||
}
|
||||
98
wp-content/plugins/brizy/admin/cloud/media-bridge.php
Normal file
98
wp-content/plugins/brizy/admin/cloud/media-bridge.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class Brizy_Admin_Cloud_BlockUploader
|
||||
*/
|
||||
class Brizy_Admin_Cloud_MediaBridge extends Brizy_Admin_Cloud_AbstractBridge {
|
||||
|
||||
use Brizy_Editor_Asset_AttachmentAware;
|
||||
|
||||
/**
|
||||
* This is the block id for which we are importing the media
|
||||
* If this is not set the import will fail.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $blockId;
|
||||
|
||||
/**
|
||||
* @param $mediaUid
|
||||
*
|
||||
* @return mixed|void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function export( $mediaUid ) {
|
||||
|
||||
$mediaId = (int) $this->getAttachmentByMediaName( $mediaUid );
|
||||
|
||||
if ( ! $mediaId ) {
|
||||
throw new Exception( "Unable to find media {$mediaUid}" );
|
||||
}
|
||||
|
||||
if ( $this->client->isMediaUploaded( $mediaUid ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$filePath = get_attached_file( $mediaId );
|
||||
$this->client->uploadMedia( $mediaUid, $filePath );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $mediaUid
|
||||
*
|
||||
* @return mixed|void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function import( $mediaUid ) {
|
||||
|
||||
if ( ! $this->blockId ) {
|
||||
throw new Exception( 'The block id is not set.' );
|
||||
}
|
||||
|
||||
// enable svg upload
|
||||
$svnUpload = new Brizy_Admin_Svg_Main();
|
||||
$jsonUpload = new Brizy_Admin_Json_Main();
|
||||
$svnUploadEnabled = Brizy_Editor_Storage_Common::instance()->get( 'svg-upload', false );
|
||||
$jsonUploadEnabled = Brizy_Editor_Storage_Common::instance()->get( 'json-upload', false );
|
||||
|
||||
if ( ! $svnUploadEnabled ) {
|
||||
$svnUpload->enableSvgUpload();
|
||||
}
|
||||
if ( ! $jsonUploadEnabled ) {
|
||||
$jsonUpload->enableJsonUpload();
|
||||
}
|
||||
|
||||
$media_cacher = new Brizy_Editor_CropCacheMedia( $this->client->getBrizyProject() );
|
||||
$media_cacher->download_original_image( $mediaUid, false );
|
||||
|
||||
// disabled it if was disabled before
|
||||
if ( ! $svnUploadEnabled ) {
|
||||
$svnUpload->disableSvgUpload();
|
||||
}
|
||||
if ( ! $jsonUploadEnabled ) {
|
||||
$jsonUpload->disableJsonUpload();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $layoutId
|
||||
*
|
||||
* @return mixed|void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function delete( $layoutId ) {
|
||||
throw new Exception( 'Not implemented' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $blockId
|
||||
*
|
||||
* @return Brizy_Admin_Cloud_MediaBridge
|
||||
*/
|
||||
public function setBlockId( $blockId ) {
|
||||
$this->blockId = (int) $blockId;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
139
wp-content/plugins/brizy/admin/cloud/media-uploads-bridge.php
Normal file
139
wp-content/plugins/brizy/admin/cloud/media-uploads-bridge.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class Brizy_Admin_Cloud_MediaUploadsBridges
|
||||
*/
|
||||
class Brizy_Admin_Cloud_MediaUploadsBridge extends Brizy_Admin_Cloud_AbstractBridge
|
||||
{
|
||||
|
||||
use Brizy_Editor_Asset_AttachmentAware;
|
||||
use Brizy_Editor_Asset_StaticFileTrait;
|
||||
|
||||
/**
|
||||
* This is the block id for which we are importing the media
|
||||
* If this is not set the import will fail.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $blockId;
|
||||
|
||||
/**
|
||||
* @param $mediaUid
|
||||
*
|
||||
* @return mixed|void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function export($mediaUpload)
|
||||
{
|
||||
|
||||
list($uploadUid,$uploadName) = explode('|||', $mediaUpload);
|
||||
$mediaId = (int)$this->getAttachmentByMediaName($uploadUid);
|
||||
|
||||
if (!$mediaId) {
|
||||
throw new Exception("Unable to find media {$uploadUid}");
|
||||
}
|
||||
|
||||
if ($this->client->isCustomFileUploaded($uploadUid,$uploadName)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// upload file
|
||||
$filePath = get_attached_file($mediaId);
|
||||
$this->client->uploadCustomFile($uploadUid, $filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $mediaUid
|
||||
*
|
||||
* @return mixed|void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function import($mediaUpload)
|
||||
{
|
||||
|
||||
if (!$this->blockId) {
|
||||
throw new Exception('The block id is not set.');
|
||||
}
|
||||
|
||||
list($fileUid, $customFileName) = explode('|||', $mediaUpload);
|
||||
|
||||
// enable svg upload
|
||||
// enable svg upload
|
||||
$svnUpload = new Brizy_Admin_Svg_Main();
|
||||
$jsonUpload = new Brizy_Admin_Json_Main();
|
||||
$svnUploadEnabled = Brizy_Editor_Storage_Common::instance()->get( 'svg-upload', false );
|
||||
$jsonUploadEnabled = Brizy_Editor_Storage_Common::instance()->get( 'json-upload', false );
|
||||
|
||||
if ( ! $svnUploadEnabled ) {
|
||||
$svnUpload->enableSvgUpload();
|
||||
}
|
||||
if ( ! $jsonUploadEnabled ) {
|
||||
$jsonUpload->enableJsonUpload();
|
||||
}
|
||||
|
||||
// download file and store it in wp
|
||||
$urlBuilder = new Brizy_Editor_UrlBuilder();
|
||||
$external_asset_url = $urlBuilder->external_custom_file($fileUid, $customFileName);
|
||||
|
||||
|
||||
$original_asset_path = $urlBuilder->brizy_upload_path("/custom_files/" . $customFileName);
|
||||
$original_asset_path_relative = $urlBuilder->brizy_upload_relative_path("/custom_files/" . $customFileName);
|
||||
|
||||
if (!file_exists($original_asset_path)) {
|
||||
// I assume that the media was already attached.
|
||||
|
||||
if (!$this->store_file($external_asset_url, $original_asset_path)) {
|
||||
// unable to save the attachment
|
||||
Brizy_Logger::instance()->error('Unable to store original media file', array(
|
||||
'source' => (string)$external_asset_url,
|
||||
'destination' => $original_asset_path
|
||||
));
|
||||
throw new Exception('Unable to cache custom file upload');
|
||||
}
|
||||
}
|
||||
|
||||
$attachmentId = $this->create_attachment($customFileName, $original_asset_path, $original_asset_path_relative, null, $fileUid);
|
||||
|
||||
if ($attachmentId === 0 || is_wp_error($attachmentId)) {
|
||||
Brizy_Logger::instance()->error('Unable to create custom file attachment', array(
|
||||
'customFile' => (string)$external_asset_url,
|
||||
'original_asset_path' => (string)$original_asset_path,
|
||||
'original_asset_path_relative' => (string)$original_asset_path_relative,
|
||||
|
||||
));
|
||||
throw new Exception('Unable to attach media');
|
||||
}
|
||||
|
||||
// disabled it if was disabled before
|
||||
if ( ! $svnUploadEnabled ) {
|
||||
$svnUpload->disableSvgUpload();
|
||||
}
|
||||
if ( ! $jsonUploadEnabled ) {
|
||||
$jsonUpload->disableJsonUpload();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $blockId
|
||||
*
|
||||
* @return mixed|void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function delete($blockId)
|
||||
{
|
||||
throw new Exception('Not implemented');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $blockId
|
||||
*
|
||||
* @return Brizy_Admin_Cloud_MediaBridge
|
||||
*/
|
||||
public function setBlockId($blockId)
|
||||
{
|
||||
$this->blockId = (int)$blockId;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
163
wp-content/plugins/brizy/admin/cloud/popup-bridge.php
Normal file
163
wp-content/plugins/brizy/admin/cloud/popup-bridge.php
Normal file
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class Brizy_Admin_Cloud_BlockUploader
|
||||
*/
|
||||
class Brizy_Admin_Cloud_PopupBridge extends Brizy_Admin_Cloud_AbstractBridge
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Brizy_Editor_Block $popup
|
||||
*
|
||||
* @return mixed|void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function export($popup)
|
||||
{
|
||||
|
||||
// check if the assets are uploaded in cloud
|
||||
// upload them if needed
|
||||
// create the block in cloud
|
||||
|
||||
$media = json_decode($popup->getMedia());
|
||||
|
||||
if (!$media || !isset($media->fonts)) {
|
||||
throw new Exception('No fonts property in media object');
|
||||
}
|
||||
|
||||
if (!$media || !isset($media->images)) {
|
||||
throw new Exception('No images property in media object');
|
||||
}
|
||||
|
||||
$bridge = new Brizy_Admin_Cloud_MediaBridge($this->client);
|
||||
foreach ($media->images as $uid) {
|
||||
try {
|
||||
$bridge->export($uid);
|
||||
} catch (Exception $e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$bridge = new Brizy_Admin_Cloud_MediaUploadsBridge($this->client);
|
||||
foreach ($media->uploads as $uid) {
|
||||
try {
|
||||
$bridge->export($uid);
|
||||
} catch (Exception $e) {
|
||||
Brizy_Logger::instance()->critical('Failed to export popup uploads: ' . $e->getMessage(), [$e]);
|
||||
}
|
||||
}
|
||||
|
||||
$bridge = new Brizy_Admin_Cloud_FontBridge($this->client);
|
||||
foreach ($media->fonts as $fontUid) {
|
||||
try {
|
||||
$bridge->export($fontUid);
|
||||
} catch (Exception $e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$popupObject = $this->client->createOrUpdatePopup($popup);
|
||||
|
||||
if ($popupObject) {
|
||||
$popup->setSynchronized($this->getCurrentCloudAccountId(), $popupObject->uid);
|
||||
}
|
||||
$popup->saveStorage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $popupId
|
||||
*
|
||||
* @return mixed|void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function import($popupId)
|
||||
{
|
||||
global $wpdb;
|
||||
|
||||
$popups = $this->client->getPopups(['uid' => $popupId]);
|
||||
|
||||
if (!isset($popups[0])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$popup = $popups[0];
|
||||
try {
|
||||
$wpdb->query('START TRANSACTION ');
|
||||
$name = md5(time());
|
||||
$post = wp_insert_post(array(
|
||||
'post_title' => $name,
|
||||
'post_name' => $name,
|
||||
'post_status' => 'publish',
|
||||
'post_type' => Brizy_Admin_Popups_Main::CP_POPUP
|
||||
));
|
||||
|
||||
if ($post) {
|
||||
$brizyPost = Brizy_Editor_Popup::get($post, $popup['uid']);
|
||||
$brizyPost->setMeta($popup['meta']);
|
||||
$brizyPost->setCloudId($popup['id']);
|
||||
$brizyPost->set_editor_data($popup['data']);
|
||||
$brizyPost->set_uses_editor(true);
|
||||
$brizyPost->set_needs_compile(true);
|
||||
$brizyPost->setDataVersion(1);
|
||||
$brizyPost->save();
|
||||
|
||||
|
||||
// import fonts
|
||||
if (isset($layout['media'])) {
|
||||
$blockMedia = json_decode($popup['media']);
|
||||
|
||||
$fontBridge = new Brizy_Admin_Cloud_FontBridge($this->client);
|
||||
if (isset($blockMedia->fonts)) {
|
||||
foreach ($blockMedia->fonts as $cloudFontUid) {
|
||||
try {
|
||||
$fontBridge->import($cloudFontUid);
|
||||
} catch (Exception $e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$mediaBridge = new Brizy_Admin_Cloud_MediaBridge($this->client);
|
||||
$mediaBridge->setBlockId($post);
|
||||
if (isset($blockMedia->images)) {
|
||||
foreach ($blockMedia->images as $mediaUid) {
|
||||
try {
|
||||
$mediaBridge->import($mediaUid);
|
||||
} catch (Exception $e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$mediaUploadBridge = new Brizy_Admin_Cloud_MediaUploadsBridge($this->client);
|
||||
$mediaUploadBridge->setBlockId($post);
|
||||
if (isset($blockMedia->uploads)) {
|
||||
foreach ($blockMedia->uploads as $mediaUid) {
|
||||
try {
|
||||
$mediaUploadBridge->import($mediaUid);
|
||||
} catch (Exception $e) {
|
||||
Brizy_Logger::instance()->critical('Failed to import layout uploads: ' . $e->getMessage(), [$e]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$wpdb->query('COMMIT');
|
||||
} catch (Exception $e) {
|
||||
$wpdb->query('ROLLBACK');
|
||||
Brizy_Logger::instance()->critical('Importing layout ' . $popupId . ' failed', [$e]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Brizy_Editor_Block $layout
|
||||
*
|
||||
* @return mixed|void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function delete($layout)
|
||||
{
|
||||
$this->client->deletePopup($layout->getCloudId());
|
||||
}
|
||||
}
|
||||
59
wp-content/plugins/brizy/admin/cloud/proxy.php
Normal file
59
wp-content/plugins/brizy/admin/cloud/proxy.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
|
||||
class Brizy_Admin_Cloud_Proxy {
|
||||
|
||||
const ACTION = 'brizy_cloud_proxy';
|
||||
|
||||
/**
|
||||
* Brizy_Admin_Cloud_Proxy constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
add_action( 'wp_ajax_' . self::ACTION, array( $this, 'handleRequest' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function handleRequest() {
|
||||
|
||||
$vars = $_GET;
|
||||
|
||||
$project = Brizy_Editor_Project::get();
|
||||
|
||||
// redirect the request
|
||||
$token = $project->getMetaValue( 'brizy-cloud-token' );
|
||||
$cloudProjectId = $project->getMetaValue( 'brizy-cloud-project' );
|
||||
|
||||
if ( ! isset( $vars['endpoint'] ) ) {
|
||||
wp_send_json_error( null, 400 );
|
||||
exit;
|
||||
}
|
||||
|
||||
if ( ! $token || ! $cloudProjectId || ! isset( $vars['endpoint'] ) ) {
|
||||
wp_send_json_error( null, 401 );
|
||||
exit;
|
||||
}
|
||||
|
||||
$http = new WP_Http();
|
||||
$url = Brizy_Config::CLOUD_ENDPOINT . "/api/" . $vars['endpoint'];
|
||||
$response = $http->request( $url, array(
|
||||
'headers' => array(
|
||||
'X-AUTH-APP-TOKEN' => Brizy_Config::CLOUD_APP_KEY,
|
||||
'X-AUTH-USER-TOKEN' => $token
|
||||
),
|
||||
'body' => $_REQUEST['request'],
|
||||
'method' => $_SERVER['REQUEST_METHOD'],
|
||||
) );
|
||||
|
||||
$status = wp_remote_retrieve_response_code( $response );
|
||||
|
||||
if ( $response instanceof WP_Error ) {
|
||||
wp_send_json_error( wp_remote_retrieve_response_message( $response ), $status );
|
||||
exit;
|
||||
}
|
||||
|
||||
wp_send_json_success( wp_remote_retrieve_body( $response ), $status );
|
||||
exit;
|
||||
}
|
||||
}
|
||||
47
wp-content/plugins/brizy/admin/cloud/screenshot-bridge.php
Normal file
47
wp-content/plugins/brizy/admin/cloud/screenshot-bridge.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class Brizy_Admin_Cloud_BlockUploader
|
||||
*/
|
||||
class Brizy_Admin_Cloud_ScreenshotBridge extends Brizy_Admin_Cloud_AbstractBridge {
|
||||
|
||||
use Brizy_Editor_Asset_AttachmentAware;
|
||||
|
||||
/**
|
||||
* @param Brizy_Editor_Block $block
|
||||
*
|
||||
* @return mixed|void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function export( $block ) {
|
||||
$meta = json_decode( $block->getMeta() );
|
||||
$screenUid = $meta->_thumbnailSrc;
|
||||
|
||||
$manager = new Brizy_Editor_Screenshot_Manager( new Brizy_Editor_UrlBuilder( $this->client->getBrizyProject() ) );
|
||||
$screenPath = $manager->getScreenshot( $screenUid );
|
||||
|
||||
if ( $screenPath ) {
|
||||
$this->client->createScreenshot( $screenUid, $screenPath );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Brizy_Editor_Block $block
|
||||
*
|
||||
* @return mixed|void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function import( $block ) {
|
||||
throw new Exception('Import screenshots not implemented yet');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Brizy_Editor_Block $layout
|
||||
*
|
||||
* @return mixed|void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function delete( $layout ) {
|
||||
$this->client->deleteBlock( $layout->getCloudId() );
|
||||
}
|
||||
}
|
||||
144
wp-content/plugins/brizy/admin/cloud/sync-aware.php
Normal file
144
wp-content/plugins/brizy/admin/cloud/sync-aware.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
|
||||
trait Brizy_Admin_Cloud_SyncAware {
|
||||
|
||||
/**
|
||||
* @var Brizy_Admin_Cloud_Client
|
||||
*/
|
||||
protected $client;
|
||||
|
||||
/**
|
||||
* @return Brizy_Admin_Cloud_Client
|
||||
*/
|
||||
public function getClient() {
|
||||
return $this->client;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Brizy_Admin_Cloud_Client $client
|
||||
*
|
||||
* @return Brizy_Admin_Cloud_SyncAware
|
||||
*/
|
||||
public function setClient( $client ) {
|
||||
$this->client = $client;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function syncLayouts( $limit = 0, $throwException = false ) {
|
||||
$layoutIds = $this->getLayoutsForSync( $limit );
|
||||
$synchronized = [];
|
||||
foreach ( $layoutIds as $lId ) {
|
||||
try {
|
||||
if ( $this->syncLayout( $lId->ID ) ) {
|
||||
$synchronized[] = $lId->ID;
|
||||
}
|
||||
} catch ( Exception $e ) {
|
||||
Brizy_Logger::instance()->critical( 'Failed to sync layout',
|
||||
[
|
||||
'blockId' => $lId->ID,
|
||||
$e
|
||||
] );
|
||||
|
||||
if ( $throwException ) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $synchronized;
|
||||
}
|
||||
|
||||
protected function syncBlocks( $limit = 0, $throwException = false ) {
|
||||
$postIds = $this->getBlocksForSync( $limit );
|
||||
$synchronized = [];
|
||||
foreach ( $postIds as $block ) {
|
||||
try {
|
||||
if ( $this->syncBlock( $block->ID ) ) {
|
||||
$synchronized[] = $block->ID;
|
||||
}
|
||||
} catch ( Exception $e ) {
|
||||
Brizy_Logger::instance()->critical( 'Failed to sync block',
|
||||
[
|
||||
'blockId' => $block->ID
|
||||
] );
|
||||
|
||||
if ( $throwException ) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $synchronized;
|
||||
}
|
||||
|
||||
protected function syncBlock( $blockId ) {
|
||||
|
||||
$brizyBlock = Brizy_Editor_Block::get( $blockId );
|
||||
|
||||
$cloud_account_id = $this->getClient()->getBrizyProject()->getCloudAccountId();
|
||||
if ( $brizyBlock &&
|
||||
$brizyBlock->isSynchronizable( $cloud_account_id ) &&
|
||||
!$brizyBlock->isSynchronized( $cloud_account_id ) ) {
|
||||
$updater = new Brizy_Admin_Cloud_BlockBridge( $this->client );
|
||||
$updater->export( $brizyBlock );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected function syncLayout( $layoutId ) {
|
||||
|
||||
$brizyLayout = Brizy_Editor_Layout::get( $layoutId );
|
||||
$cloud_account_id = $this->getClient()->getBrizyProject()->getCloudAccountId();
|
||||
if ( $brizyLayout &&
|
||||
$brizyLayout->isSynchronizable( $cloud_account_id ) &&
|
||||
!$brizyLayout->isSynchronized( $cloud_account_id ) ) {
|
||||
$updater = new Brizy_Admin_Cloud_LayoutBridge( $this->client );
|
||||
$updater->export( $brizyLayout );
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
protected function getLayoutsForSync( $limit = 0 ) {
|
||||
global $wpdb;
|
||||
|
||||
$savedBlockType = Brizy_Admin_Layouts_Main::CP_LAYOUT;
|
||||
|
||||
$limitQuery = "";
|
||||
if ( $limit !== 0 ) {
|
||||
$limitQuery = " LIMIT " . ( (int) $limit );
|
||||
}
|
||||
|
||||
$postIds = $wpdb->get_results(
|
||||
"SELECT ID FROM {$wpdb->posts} p
|
||||
WHERE p.post_type='{$savedBlockType}'
|
||||
{$limitQuery}" );
|
||||
|
||||
return $postIds;
|
||||
}
|
||||
|
||||
|
||||
protected function getBlocksForSync( $limit = 0 ) {
|
||||
global $wpdb;
|
||||
|
||||
$savedBlockType = Brizy_Admin_Blocks_Main::CP_SAVED;
|
||||
|
||||
$limitQuery = "";
|
||||
if ( $limit !== 0 ) {
|
||||
$limitQuery = " LIMIT " . ( (int) $limit );
|
||||
}
|
||||
|
||||
$postIds = $wpdb->get_results(
|
||||
"SELECT ID FROM {$wpdb->posts} p
|
||||
WHERE p.post_type='{$savedBlockType}'
|
||||
{$limitQuery}
|
||||
"
|
||||
);
|
||||
|
||||
return $postIds;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user