Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(editor-client): symbols crud #1904

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 58 additions & 51 deletions admin/symbols/api.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php


class Brizy_Admin_Symbols_Api extends Brizy_Admin_AbstractApi {
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';
Expand All @@ -20,7 +21,8 @@ class Brizy_Admin_Symbols_Api extends Brizy_Admin_AbstractApi {
*
* @param Brizy_Admin_Symbols_Manager $manager
*/
public function __construct( $manager ) {
public function __construct($manager)
{
$this->manager = $manager;

parent::__construct();
Expand All @@ -29,31 +31,35 @@ public function __construct( $manager ) {
/**
* @return Brizy_Admin_Rules_Api
*/
public static function _init() {
public static function _init()
{
static $instance;

if ( ! $instance ) {
$instance = new self( new Brizy_Admin_Symbols_Manager() );
if (!$instance) {
$instance = new self(new Brizy_Admin_Symbols_Manager());
}

return $instance;
}

protected function getRequestNonce() {
return $this->param( 'hash' );
protected function getRequestNonce()
{
return $this->param('hash');
}

protected function initializeApiActions() {
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' ) );
add_filter( 'brizy_editor_config', array( $this, 'editorConfig' ), 10, 2 );
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'));
add_filter('brizy_editor_config', array($this, 'editorConfig'), 10, 2);
}

public function editorConfig( $config, $context = null ) {
public function editorConfig($config, $context = null)
{
$config['symbols'] = $this->manager->getList();

return $config;
Expand All @@ -62,64 +68,65 @@ public function editorConfig( $config, $context = null ) {
/**
* @return null|void
*/
public function actionGetList() {
$this->verifyNonce( self::nonce );
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() );
$this->success($symbols);
} catch (Exception $e) {
Brizy_Logger::instance()->error($e->getMessage(), [$e]);
$this->error(400, $e->getMessage());
}

return null;
}

public function actionCreateOrUpdate() {
public function actionCreateOrUpdate()
{

$this->verifyNonce( self::nonce );
$this->verifyNonce(self::nonce);

$data = file_get_contents( "php://input" );
$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;

$asymbols = $this->manager->createFromJson($data);
foreach ($asymbols as $asymbol) {
$symbol = $this->manager->get($asymbol->getUid());
if ($symbol) {
$symbol->patchFrom($asymbol);
$symbol->incrementVersion();
} else {
$symbol = $asymbol;
}

$this->manager->validateSymbol($symbol);
$this->manager->saveSymbol($symbol);
}
$symbol->incrementVersion();
$this->manager->validateSymbol( $symbol );
$this->manager->saveSymbol( $symbol );
} catch ( Exception $e ) {
$this->error( 400, "Error" . $e->getMessage() );
} catch (Exception $e) {
$this->error(400, "Error" . $e->getMessage());
}

wp_send_json_success( $symbol, 200 );
wp_send_json_success($asymbols, 200);
}

public function actionDelete() {

$this->verifyNonce( self::nonce );

$uid = $this->param( 'uid' );

if ( ! $uid ) {
$this->error( 400, "Error: Please provide the symbol uid" );
}
public function actionDelete()
{

$this->verifyNonce(self::nonce);
$data = file_get_contents("php://input");
try {
$symbol = $this->manager->get( $uid );
$this->manager->deleteSymbol( $symbol );
} catch ( Exception $e ) {
$this->error( 400, 'Unable to delete symbol' );
$asymbols = $this->manager->createFromJson($data);
foreach ($asymbols as $asymbol) {
$this->manager->deleteSymbol($asymbol);
}
} catch (Exception $e) {
$this->error(400, "Error" . $e->getMessage());
}

$this->success( null );
$this->success(null);
}

}
96 changes: 55 additions & 41 deletions admin/symbols/manager.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

class Brizy_Admin_Symbols_Manager {
class Brizy_Admin_Symbols_Manager
{
const BRIZY_SYMBOLS_KEY = 'brizy-symbols';

/**
Expand All @@ -10,22 +11,32 @@ class Brizy_Admin_Symbols_Manager {
* @return Brizy_Admin_Symbols_Symbol
* @throws Exception
*/
public function createFromJson( $jsonString ) {
$jsonObj = json_decode( $jsonString );
public function createFromJson($jsonString)
{
$jsonObj = json_decode($jsonString);
$result = [];
if (is_array($jsonObj)) {
foreach ($jsonObj as $obj) {
$result[] = Brizy_Admin_Symbols_Symbol::createFromJsonObject($obj);
}
} elseif (!is_null($jsonObj)) {
$result[] = Brizy_Admin_Symbols_Symbol::createFromJsonObject($jsonObj);
}

return Brizy_Admin_Symbols_Symbol::createFromJsonObject( $jsonObj );
return $result;
}

/**
* @return Brizy_Admin_Symbols_Symbol[]
*/
public function getList() {
$symbolsEncoded = get_option( self::BRIZY_SYMBOLS_KEY, base64_encode( "[]" ) );
$jsonSymbols = json_decode( base64_decode( $symbolsEncoded ) );
public function getList()
{
$symbolsEncoded = get_option(self::BRIZY_SYMBOLS_KEY, base64_encode("[]"));
$jsonSymbols = json_decode(base64_decode($symbolsEncoded));

$symbols = [];
foreach ( $jsonSymbols as $symbol ) {
$symbols[] = Brizy_Admin_Symbols_Symbol::createFromJsonObject( $symbol );
foreach ($jsonSymbols as $symbol) {
$symbols[] = Brizy_Admin_Symbols_Symbol::createFromJsonObject($symbol);
}

return $symbols;
Expand All @@ -34,12 +45,13 @@ public function getList() {
/**
* @return Brizy_Admin_Symbols_Symbol
*/
public function get( $uid ) {
public function get($uid)
{

$symbols = $this->getList();

foreach ( $symbols as $symbol ) {
if ( $symbol->getUid() == $uid ) {
foreach ($symbols as $symbol) {
if ($symbol->getUid() == $uid) {
return $symbol;
}
}
Expand All @@ -51,77 +63,79 @@ public function get( $uid ) {
/**
* @param Brizy_Admin_Symbols_Symbol $aSymbol
*/
public function deleteSymbol( $aSymbol ) {
if ( ! $aSymbol ) {
public function deleteSymbol($aSymbol)
{
if (!$aSymbol) {
throw new Exception("Unable to delete NULL symbol");
}

$symbols = $this->getList();

foreach ( $symbols as $i => $symbol ) {
if ( $symbol->getUid() == $aSymbol->getUid() ) {
unset( $symbols[ $i ] );
foreach ($symbols as $i => $symbol) {
if ($symbol->getUid() == $aSymbol->getUid()) {
unset($symbols[$i]);
}
}

$this->saveAllSymbols( $symbols );
$this->saveAllSymbols($symbols);
}

/**
* @param Brizy_Admin_Symbols_Symbol $aSymbol
*
* @return Brizy_Admin_Symbols_Symbol
*/
public function saveSymbol( $aSymbol ) {
if ( ! $aSymbol ) {
public function saveSymbol($aSymbol)
{
if (!$aSymbol) {
throw new Exception("Unable to save NULL symbol");
}
$symbols = $this->getList();

foreach ( $symbols as $i => $symbol ) {
if ( $symbol->getUid() == $aSymbol->getUid() ) {
$symbols[ $i ] = $aSymbol;
$this->saveAllSymbols( $symbols );
foreach ($symbols as $i => $symbol) {
if ($symbol->getUid() == $aSymbol->getUid()) {
$symbols[$i] = $aSymbol;
$this->saveAllSymbols($symbols);

return;
}
}
$symbols[] = $aSymbol;
$this->saveAllSymbols( $symbols );
$this->saveAllSymbols($symbols);
}

private function saveAllSymbols( $symbols ) {
update_option( self::BRIZY_SYMBOLS_KEY, base64_encode( json_encode( $symbols ) ) );
private function saveAllSymbols($symbols)
{
update_option(self::BRIZY_SYMBOLS_KEY, base64_encode(json_encode($symbols)));
}

/**
* @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' );
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' );
if (is_null($symbol->getVersion()) || empty($symbol->getVersion())) {
throw new Exception('Please provide the symbol version');
}

$currentSymbol = $this->get( $symbol->getUid() );
$currentSymbol = $this->get($symbol->getUid());

if ( $currentSymbol && ( $currentSymbol->getVersion() + 1 != $symbol->getVersion() ) ) {
throw new Exception( 'Invalid symbol version. Please refresh and try again.' );
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->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' );
if (is_null($symbol->getData()) || empty($symbol->getData())) {
throw new Exception('Please provide the symbol data');
}

}

}
Loading