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

new: Symbols api #1841

Open
wants to merge 16 commits into
base: master
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
1 change: 1 addition & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pipeline {

stage('Prepare the build') {
steps {
sh "./jenkins/build-client.sh"
sh "./jenkins/version-update.sh ${params.buildVersion} ${params.minProVersion} ${params.editorVersion} ${params.syncVersion}"
sh "./jenkins/update-tested-version.sh README.md readme.txt"
}
Expand Down
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
2 changes: 1 addition & 1 deletion admin/blocks/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function actionDownloadBlocks() {
$items = array_filter( $items );

if ( count( $items ) == 0 ) {
$this->error( 404, __( 'There are no block to be archived' ) );
$this->error( 404, __( 'There are no blocks to be archived', 'brizy' ) );
}

$fontManager = new Brizy_Admin_Fonts_Manager();
Expand Down
2 changes: 1 addition & 1 deletion admin/form-entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function addOnOffOption() {
$class = 'disableFormLogs';
$val = 0;
} else {
$label = __( 'Enable ', 'brizy' );
$label = __( 'Enable', 'brizy' );
$class = 'enableFormLogs';
$val = 1;
}
Expand Down
2 changes: 1 addition & 1 deletion admin/membership/membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function admin_bar_menu( &$wp_admin_bar ) {
'role' => 'default'
],
[
'name' => esc_html__( 'Not Logged', 'brizy' ),
'name' => esc_html__( 'Not Logged In', 'brizy' ),
'role' => 'not_logged'
],
[
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