-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b476a82
commit 343ba07
Showing
5 changed files
with
1,594 additions
and
1,148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
|
||
class Brizy_Admin_Symbols_Api extends Brizy_Admin_AbstractApi | ||
{ | ||
const nonce = Brizy_Editor_API::nonce; | ||
const CREATE_ACTION = '_add_symbol'; | ||
const UPDATE_ACTION = '_update_symbol'; | ||
const DELETE_ACTION = '_delete_symbol'; | ||
const LIST_ACTION = '_list_symbols'; | ||
|
||
|
||
/** | ||
* @var Brizy_Admin_Symbols_Manager | ||
*/ | ||
private $manager; | ||
|
||
|
||
/** | ||
* Brizy_Admin_Rules_Api constructor. | ||
* | ||
* @param Brizy_Admin_Symbols_Manager $manager | ||
*/ | ||
public function __construct($manager) | ||
{ | ||
$this->manager = $manager; | ||
|
||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* @return Brizy_Admin_Rules_Api | ||
*/ | ||
public static function _init() | ||
{ | ||
static $instance; | ||
|
||
if ( ! $instance) { | ||
$instance = new self(new Brizy_Admin_Symbols_Manager()); | ||
} | ||
|
||
return $instance; | ||
} | ||
|
||
protected function getRequestNonce() | ||
{ | ||
return $this->param('hash'); | ||
} | ||
|
||
protected function initializeApiActions() | ||
{ | ||
$pref = 'wp_ajax_'.Brizy_Editor::prefix(); | ||
|
||
add_action($pref.self::CREATE_ACTION, array($this, 'actionCreateOrUpdate')); | ||
add_action($pref.self::UPDATE_ACTION, array($this, 'actionCreateOrUpdate')); | ||
add_action($pref.self::DELETE_ACTION, array($this, 'actionDelete')); | ||
add_action($pref.self::LIST_ACTION, array($this, 'actionGetList')); | ||
} | ||
|
||
/** | ||
* @return null|void | ||
*/ | ||
public function actionGetList() | ||
{ | ||
$this->verifyNonce(self::nonce); | ||
|
||
try { | ||
$symbols = $this->manager->getList(); | ||
|
||
$this->success($symbols); | ||
} catch (Exception $e) { | ||
Brizy_Logger::instance()->error($e->getMessage(), [$e]); | ||
$this->error(400, $e->getMessage()); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function actionCreateOrUpdate() | ||
{ | ||
|
||
$this->verifyNonce(self::nonce); | ||
|
||
$data = file_get_contents("php://input"); | ||
|
||
try { | ||
$asymbol = $this->manager->createFromJson($data); | ||
$symbol = null; | ||
if ($asymbol->getUid()) { | ||
$symbol = $this->manager->get($asymbol->getUid()); | ||
$symbol->patchFrom($asymbol); | ||
} else { | ||
$symbol = $asymbol; | ||
} | ||
$symbol->incrementVersion(); | ||
$this->manager->validateSymbol($symbol); | ||
$this->manager->saveSymbol($symbol); | ||
} catch (Exception $e) { | ||
$this->error(400, "Error".$e->getMessage()); | ||
} | ||
|
||
wp_send_json_success($symbol, 200); | ||
} | ||
|
||
public function actionDelete() | ||
{ | ||
|
||
$this->verifyNonce(self::nonce); | ||
|
||
$uid = $this->param('uid'); | ||
|
||
if ( ! $uid) { | ||
$this->error(400, "Error: Please provide the symbol uid"); | ||
} | ||
|
||
try { | ||
$symbol = $this->manager->get($uid); | ||
$this->manager->deleteSymbol($symbol); | ||
} catch (Exception $e) { | ||
$this->error(400, 'Unable to delete symbol'); | ||
} | ||
|
||
$this->success(null); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
class Brizy_Admin_Symbols_Manager | ||
{ | ||
|
||
/** | ||
* @param $jsonString | ||
* @param string $postType | ||
* | ||
* @return Brizy_Admin_Symbols_Symbol | ||
* @throws Exception | ||
*/ | ||
public function createFromJson($jsonString) | ||
{ | ||
$jsonObj = json_decode($jsonString); | ||
|
||
return Brizy_Admin_Symbols_Symbol::createFromJsonObject($jsonObj); | ||
} | ||
|
||
/** | ||
* @param $jsonString | ||
* @param string $postType | ||
* | ||
* @return array | ||
* @throws Exception | ||
*/ | ||
public function createSymbolsFromJson($jsonString, $postType = Brizy_Admin_Templates::CP_TEMPLATE) | ||
{ | ||
$rulesJson = json_decode($jsonString); | ||
$rules = array(); | ||
|
||
if (is_array($rulesJson)) { | ||
foreach ($rulesJson as $ruleJson) { | ||
$rules[] = Brizy_Admin_Symbols_Symbol::createFromJsonObject($ruleJson); | ||
} | ||
} | ||
|
||
return $rules; | ||
} | ||
|
||
/** | ||
* @return Brizy_Admin_Symbols_Symbol[] | ||
*/ | ||
public function getList() | ||
{ | ||
|
||
return []; | ||
} | ||
|
||
/** | ||
* @return Brizy_Admin_Symbols_Symbol | ||
*/ | ||
public function get($uid) | ||
{ | ||
|
||
return new Brizy_Admin_Symbols_Symbol; | ||
} | ||
|
||
/** | ||
* @param $symbol | ||
* | ||
* @return void | ||
*/ | ||
public function addSymbol($symbol) | ||
{ | ||
|
||
} | ||
|
||
/** | ||
* @param $symbol | ||
* | ||
* @return void | ||
*/ | ||
public function deleteSymbol($symbol) | ||
{ | ||
|
||
} | ||
|
||
/** | ||
* @return Brizy_Admin_Symbols_Symbol | ||
*/ | ||
public function saveSymbol($symbol) | ||
{ | ||
|
||
} | ||
|
||
/** | ||
* @param Brizy_Admin_Symbols_Symbol $symbol | ||
* | ||
* @return void | ||
*/ | ||
public function validateSymbol($symbol) | ||
{ | ||
if (is_null($symbol->getUid()) || empty($symbol->getUid())) { | ||
throw new Exception('Please provide the symbol uid'); | ||
} | ||
|
||
if (is_null($symbol->getVersion()) || empty($symbol->getVersion())) { | ||
throw new Exception('Please provide the symbol version'); | ||
} | ||
|
||
$currentSymbol = $this->get($symbol->getUid()); | ||
|
||
if ($currentSymbol && ($currentSymbol->getVersion() + 1 != $symbol->getVersion())) { | ||
throw new Exception('Invalid symbol version. Please refresh and try again.'); | ||
} | ||
|
||
if ( is_null($symbol->getLabel()) || empty($symbol->getLabel())) { | ||
throw new Exception('Please provide the symbol label'); | ||
} | ||
|
||
if ( is_null($symbol->getData()) || empty($symbol->getData())) { | ||
throw new Exception('Please provide the symbol data'); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.