Skip to content

Commit

Permalink
new: Symbols api
Browse files Browse the repository at this point in the history
  • Loading branch information
alecszaharia committed Dec 21, 2022
1 parent b476a82 commit 343ba07
Show file tree
Hide file tree
Showing 5 changed files with 1,594 additions and 1,148 deletions.
2 changes: 1 addition & 1 deletion admin/abstract-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function verifyNonce( $action ) {

$version = $this->param( 'version' );
if ( $version !== BRIZY_EDITOR_VERSION ) {
Brizy_Logger::instance()->critical( 'Request with invalid version',
Brizy_Logger::instance()->critical( 'Request with invalid editor version',
[
'editorVersion' => BRIZY_EDITOR_VERSION,
'providedVersion' => $version
Expand Down
126 changes: 126 additions & 0 deletions admin/symbols/api.php
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);
}

}
118 changes: 118 additions & 0 deletions admin/symbols/manager.php
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');
}

}

}
Loading

0 comments on commit 343ba07

Please sign in to comment.