first commit

This commit is contained in:
Roman Pyrih
2023-07-24 08:30:51 +02:00
commit c2e100a763
7128 changed files with 1622619 additions and 0 deletions

View File

@@ -0,0 +1,753 @@
<?php
/**
* Created by PhpStorm.
* User: alex
* Date: 7/18/18
* Time: 10:48 AM
*/
class Brizy_Admin_Blocks_Api extends Brizy_Admin_AbstractApi {
const nonce = 'brizy-api';
const GET_SAVED_BLOCK_ACTION = '-get-saved-block';
const GET_GLOBAL_BLOCKS_ACTION = '-get-global-blocks';
const GET_SAVED_BLOCKS_ACTION = '-get-saved-blocks';
const CREATE_GLOBAL_BLOCK_ACTION = '-create-global-block';
const CREATE_SAVED_BLOCK_ACTION = '-create-saved-block';
const UPDATE_GLOBAL_BLOCK_ACTION = '-update-global-block';
const UPDATE_GLOBAL_BLOCKS_ACTION = '-update-global-blocks';
const UPDATE_SAVED_BLOCK_ACTION = '-saved-global-block';
const DELETE_GLOBAL_BLOCK_ACTION = '-delete-global-block';
const DELETE_SAVED_BLOCK_ACTION = '-delete-saved-block';
const UPDATE_POSITIONS_ACTION = '-update-block-positions';
const DOWNLOAD_BLOCKS = '-download-blocks';
const UPLOAD_BLOCKS = '-upload-blocks';
/**
* @var Brizy_Admin_Rules_Manager
*/
private $ruleManager;
/**
* @return Brizy_Admin_Blocks_Api
*/
public static function _init() {
static $instance;
if ( ! $instance ) {
$instance = new self( new Brizy_Admin_Rules_Manager() );
}
return $instance;
}
/**
* Brizy_Admin_Blocks_Api constructor.
*
* @param Brizy_Admin_Rules_Manager $ruleManager
*/
public function __construct( $ruleManager ) {
$this->ruleManager = $ruleManager;
parent::__construct();
}
protected function getRequestNonce() {
return $this->param( 'hash' );
}
protected function initializeApiActions() {
$pref = 'wp_ajax_' . Brizy_Editor::prefix();
add_action( $pref . self::DOWNLOAD_BLOCKS, array( $this, 'actionDownloadBlocks' ) );
add_action( $pref . self::UPLOAD_BLOCKS, array( $this, 'actionUploadBlocks' ) );
add_action( $pref . self::GET_GLOBAL_BLOCKS_ACTION, array( $this, 'actionGetGlobalBlocks' ) );
add_action( $pref . self::CREATE_GLOBAL_BLOCK_ACTION, array( $this, 'actionCreateGlobalBlock' ) );
add_action( $pref . self::UPDATE_GLOBAL_BLOCK_ACTION, array( $this, 'actionUpdateGlobalBlock' ) );
add_action( $pref . self::UPDATE_GLOBAL_BLOCKS_ACTION, array( $this, 'actionUpdateGlobalBlocks' ) );
add_action( $pref . self::DELETE_GLOBAL_BLOCK_ACTION, array( $this, 'actionDeleteGlobalBlock' ) );
add_action( $pref . self::GET_SAVED_BLOCKS_ACTION, array( $this, 'actionGetSavedBlocks' ) );
add_action( $pref . self::GET_SAVED_BLOCK_ACTION, array( $this, 'actionGetSavedBlockByUid' ) );
add_action( $pref . self::UPDATE_SAVED_BLOCK_ACTION, array( $this, 'actionUpdateSavedBlock' ) );
add_action( $pref . self::CREATE_SAVED_BLOCK_ACTION, array( $this, 'actionCreateSavedBlock' ) );
add_action( $pref . self::DELETE_SAVED_BLOCK_ACTION, array( $this, 'actionDeleteSavedBlock' ) );
add_action( $pref . self::UPDATE_POSITIONS_ACTION, array( $this, 'actionUpdateBlockPositions' ) );
}
public function actionDownloadBlocks() {
$this->verifyNonce( self::nonce );
if ( ! $this->param( 'uid' ) ) {
$this->error( 400, 'Invalid block uid param' );
}
try {
$bockManager = new Brizy_Admin_Blocks_Manager( Brizy_Admin_Blocks_Main::CP_SAVED );
$uids = [];
// this is not a very eficien solution if you have a big array of uids
$explode = explode( ',', $this->param( 'uid' ) );
$items = array_map( function ( $auid ) use ( $uids, $bockManager ) {
list( $uid, $isPro ) = explode( ':', $auid );
$uids[] = $uid;
$item = new Brizy_Editor_Zip_ArchiveItem( $uid, $isPro );
if ( $post = $bockManager->getEntity( $uid ) ) {
$item->setPost( $post );
return $item;
}
return null;
}, $explode );
$items = array_filter( $items );
if ( count( $items ) == 0 ) {
$this->error( 404, __( 'There are no block to be archived' ) );
}
$fontManager = new Brizy_Admin_Fonts_Manager();
$zip = new Brizy_Editor_Zip_Archiver( Brizy_Editor_Project::get(), $fontManager, BRIZY_EDITOR_VERSION );
switch ( $this->param( 'type' ) ) {
case 'popup':
$zipPath = "Popup-" . date( DATE_ATOM ) . ".zip";
break;
default:
$zipPath = "Block-" . date( DATE_ATOM ) . ".zip";
break;
}
$zipPath = $zip->createZip( $items, $zipPath );
header( "Pragma: public" );
header( "Expires: 0" );
header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
header( "Cache-Control: private", false );
header( "Content-Type: application/octet-stream" );
header( "Content-Disposition: attachment; filename=\"" . basename( $zipPath ) . "\";" );
header( "Content-Transfer-Encoding: binary" );
echo file_get_contents( $zipPath );
exit;
} catch ( Exception $exception ) {
$this->error( 400, __( $exception->getMessage(), 'brizy' ) );
}
}
public function actionUploadBlocks() {
try {
$this->verifyNonce( self::nonce );
if ( ! isset( $_FILES['files'] ) ) {
$this->error( 400, __( 'Invalid block file' ) );
}
$fields = $this->param( 'fields' ) ? $this->param( 'fields' ) : [];
if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
$file = [
'name' => $_FILES['files']['name'][0],
'type' => $_FILES['files']['type'][0],
'tmp_name' => $_FILES['files']['tmp_name'][0],
'error' => $_FILES['files']['error'][0],
'size' => $_FILES['files']['size'][0],
];
$uploadedFile = wp_handle_upload( $file, [ 'test_form' => false ] );
if ( isset( $uploadedFile['file'] ) ) {
$zip = new Brizy_Editor_Zip_Archiver( Brizy_Editor_Project::get(),
new Brizy_Admin_Fonts_Manager(),
BRIZY_EDITOR_VERSION );
list( $instances, $errors ) = $zip->createFromZip( $uploadedFile['file'] );
unset( $uploadedFile['file'] );
$bockManager = new Brizy_Admin_Blocks_Manager( Brizy_Admin_Blocks_Main::CP_SAVED );
$this->success( [
'success' => $bockManager->createResponseForEntities( $instances, $fields ),
'errors' => $errors
] );
} else {
if ( isset( $uploadedFile['error'] ) ) {
$this->error( 400, $uploadedFile['error'] );
} else {
$this->error( 400, __( "Invalid zip file provided" ) );
}
}
} catch ( Exception $exception ) {
$this->error( 400, $exception->getMessage() );
}
}
public function actionGetGlobalBlocks() {
$this->verifyNonce( self::nonce );
try {
$fields = $this->param( 'fields' ) ? $this->param( 'fields' ) : [];
$bockManager = new Brizy_Admin_Blocks_Manager( Brizy_Admin_Blocks_Main::CP_GLOBAL );
$blocks = $bockManager->getEntities( ['post_status'=>'any'] );
$this->success( $bockManager->createResponseForEntities( $blocks, $fields ) );
} catch ( Exception $exception ) {
$this->error( 400, $exception->getMessage() );
}
}
public function actionCreateGlobalBlock() {
$this->verifyNonce( self::nonce );
if ( ! $this->param( 'uid' ) ) {
$this->error( 400, 'Invalid uid' );
}
if ( ! $this->param( 'data' ) ) {
$this->error( 400, 'Invalid data' );
}
if ( ! $this->param( 'meta' ) ) {
$this->error( 400, 'Invalid meta data' );
}
try {
$editorData = stripslashes( $this->param( 'data' ) );
$position = stripslashes( $this->param( 'position' ) );
$status = stripslashes( $this->param( 'status' ) );
$rulesData = stripslashes( $this->param( 'rules' ) );
if ( ! in_array( $status, [ 'publish', 'draft' ] ) ) {
$this->error( 400, "Invalid status" );
}
$bockManager = new Brizy_Admin_Blocks_Manager( Brizy_Admin_Blocks_Main::CP_GLOBAL );
$block = $bockManager->createEntity( $this->param( 'uid' ), $status );
$block->setMeta( stripslashes( $this->param( 'meta' ) ) );
$block->set_editor_data( $editorData );
$block->set_needs_compile( true );
if ( $position ) {
$block->setPosition( Brizy_Editor_BlockPosition::createFromSerializedData( get_object_vars( json_decode( $position ) ) ) );
}
// rules
if ( $rulesData ) {
$rules = $this->ruleManager->createRulesFromJson( $rulesData, Brizy_Admin_Blocks_Main::CP_GLOBAL );
$this->ruleManager->addRules( $block->getWpPostId(), $rules );
}
$block->save();
do_action( 'brizy_global_block_created', $block );
do_action( 'brizy_global_data_updated' );
$this->success( $block->createResponse() );
} catch ( Exception $exception ) {
$this->error( 400, $exception->getMessage() );
}
}
public function actionUpdateGlobalBlock() {
$this->verifyNonce( self::nonce );
try {
if ( ! $this->param( 'uid' ) ) {
$this->error( '400', 'Invalid uid' );
}
if ( ! $this->param( 'data' ) ) {
$this->error( '400', 'Invalid data' );
}
if ( ! $this->param( 'meta' ) ) {
$this->error( 400, 'Invalid meta data' );
}
$status = stripslashes( $this->param( 'status' ) );
if ( ! in_array( $status, [ 'publish', 'draft' ] ) ) {
$this->error( 400, "Invalid post type" );
}
$bockManager = new Brizy_Admin_Blocks_Manager( Brizy_Admin_Blocks_Main::CP_GLOBAL );
$block = $bockManager->getEntity( $this->param( 'uid' ) );
if ( ! $block ) {
$this->error( 400, "Global block not found" );
}
/**
* @var Brizy_Editor_Block $block ;
*/
$block->setMeta( stripslashes( $this->param( 'meta' ) ) );
$block->set_editor_data( stripslashes( $this->param( 'data' ) ) );
if ( (int) $this->param( 'is_autosave' ) ) {
$block->save( 1 );
} else {
// issue: #14271
//$block->setDataVersion( $this->param( 'dataVersion' ) );
$block->getWpPost()->post_status = $status;
// position
$position = stripslashes( $this->param( 'position' ) );
if ( $position ) {
$block->setPosition( Brizy_Editor_BlockPosition::createFromSerializedData( get_object_vars( json_decode( $position ) ) ) );
}
// rules
$rulesData = stripslashes( $this->param( 'rules' ) );
if ( $rulesData ) {
$rules = $this->ruleManager->createRulesFromJson( $rulesData, Brizy_Admin_Blocks_Main::CP_GLOBAL );
$this->ruleManager->setRules( $block->getWpPostId(), $rules );
}
$block->save( 0 );
do_action( 'brizy_global_block_updated', $block );
do_action( 'brizy_global_data_updated' );
}
Brizy_Editor_Block::cleanClassCache();
$this->success( Brizy_Editor_Block::get( $block->getWpPostId() )->createResponse() );
} catch ( Exception $exception ) {
$this->error( 400, $exception->getMessage() );
}
}
public function actionUpdateGlobalBlocks() {
//$this->verifyNonce( self::nonce );
try {
if ( ! $this->param( 'uid' ) ) {
$this->success( [] );
}
foreach ( (array) $this->param( 'uid' ) as $i => $uid ) {
if ( ! $this->param( 'uid' )[ $i ] ) {
$this->error( '400', 'Invalid uid' );
}
// if ( ! $this->param( 'data' )[ $i ] ) {
// $this->error( '400', 'Invalid data' );
// }
if ( ! $this->param( 'meta' )[ $i ] ) {
$this->error( 400, 'Invalid meta data' );
}
$status = stripslashes( $this->param( 'status' )[ $i ] );
if ( ! in_array( $status, [ 'publish', 'draft' ] ) ) {
$this->error( 400, "Invalid post type" );
}
}
$blocks = [];
foreach ( (array) $this->param( 'uid' ) as $i => $uid ) {
$status = stripslashes( $this->param( 'status' )[ $i ] );
$bockManager = new Brizy_Admin_Blocks_Manager( Brizy_Admin_Blocks_Main::CP_GLOBAL );
$block = $bockManager->getEntity( $this->param( 'uid' )[ $i ] );
if ( ! $block ) {
$this->error( 400, "Global block not found" );
}
/**
* @var Brizy_Editor_Block $block ;
*/
$block->setMeta( stripslashes( $this->param( 'meta' )[ $i ] ) );
if ( isset( $this->param( 'data' )[ $i ] ) && ! empty( $this->param( 'data' )[ $i ] ) ) {
$block->set_editor_data( stripslashes( $this->param( 'data' )[ $i ] ) );
}
if ( isset( $this->param( 'is_autosave' )[ $i ] ) && (int) $this->param( 'is_autosave' )[ $i ] === 1 ) {
$block->save( 1 );
} else {
// issue: #14271
//$block->setDataVersion( $this->param( 'dataVersion' )[ $i ] );
$block->getWpPost()->post_status = $status;
// position
$position = stripslashes( $this->param( 'position' )[ $i ] );
if ( $position && ( $positionObject = json_decode( $position ) ) ) {
$block->setPosition( Brizy_Editor_BlockPosition::createFromSerializedData( get_object_vars( $positionObject ) ) );
}
// rules
$rulesData = stripslashes( $this->param( 'rules' )[ $i ] );
if ( $rulesData ) {
$rules = $this->ruleManager->createRulesFromJson( $rulesData,
Brizy_Admin_Blocks_Main::CP_GLOBAL );
$this->ruleManager->setRules( $block->getWpPostId(), $rules );
}
$block->save( 0 );
do_action( 'brizy_global_block_updated', $block );
$blocks[] = $block;
}
}
do_action( 'brizy_global_data_updated' );
Brizy_Editor_Block::cleanClassCache();
$response = [];
foreach ( $blocks as $block ) {
$response[] = Brizy_Editor_Block::get( $block->getWpPostId() )->createResponse();
}
$this->success( $response );
} catch ( Exception $exception ) {
$this->error( 400, $exception->getMessage() );
}
}
public function actionDeleteGlobalBlock() {
$this->verifyNonce( self::nonce );
if ( ! $this->param( 'uid' ) ) {
$this->error( '400', 'Invalid uid' );
}
$block = $this->getBlock( $this->param( 'uid' ), Brizy_Admin_Blocks_Main::CP_GLOBAL );
if ( $block ) {
do_action( 'brizy_global_block_deleted', $block );
do_action( 'brizy_global_data_deleted' );
$this->deleteBlock( $block, Brizy_Admin_Blocks_Main::CP_GLOBAL );
$this->success( null );
}
$this->error( '404', 'Block not found' );
}
public function actionGetSavedBlocks() {
$this->verifyNonce( self::nonce );
try {
$fields = $this->param( 'fields' ) ? $this->param( 'fields' ) : [];
$bockManager = new Brizy_Admin_Blocks_Manager( Brizy_Admin_Blocks_Main::CP_SAVED );
$blocks = $bockManager->getEntities( [] );
$blocks = apply_filters( 'brizy_get_saved_blocks',
$bockManager->createResponseForEntities( $blocks, $fields ),
$fields,
$bockManager );
$this->success( $blocks );
} catch ( Exception $exception ) {
$this->error( 400, $exception->getMessage() );
}
}
public function actionGetSavedBlockByUid() {
$this->verifyNonce( self::nonce );
if ( ! $this->param( 'uid' ) ) {
$this->error( 400, 'Invalid uid' );
}
$fields = $this->param( 'fields' ) ? $this->param( 'fields' ) : [];
try {
$bockManager = new Brizy_Admin_Blocks_Manager( Brizy_Admin_Blocks_Main::CP_SAVED );
$block = $bockManager->getEntity( $this->param( 'uid' ) );
$block = apply_filters( 'brizy_get_saved_block', $block, $this->param( 'uid' ), $bockManager );
if ( ! $block ) {
$this->error( 404, 'Block not found' );
}
$this->success( $block->createResponse( $fields ) );
} catch ( Exception $exception ) {
$this->error( 400, $exception->getMessage() );
}
}
public function actionCreateSavedBlock() {
$this->verifyNonce( self::nonce );
if ( ! $this->param( 'uid' ) ) {
$this->error( 400, 'Invalid uid' );
}
if ( ! $this->param( 'data' ) ) {
$this->error( 400, 'Invalid data' );
}
if ( ! $this->param( 'meta' ) ) {
$this->error( 400, 'Invalid meta data' );
}
if ( ! $this->param( 'media' ) ) {
$this->error( 400, 'Invalid media data provided' );
}
try {
$bockManager = new Brizy_Admin_Blocks_Manager( Brizy_Admin_Blocks_Main::CP_SAVED );
$block = $bockManager->createEntity( $this->param( 'uid' ) );
$block->setMedia( stripslashes( $this->param( 'media' ) ) );
$block->setMeta( stripslashes( $this->param( 'meta' ) ) );
$block->set_editor_data( stripslashes( $this->param( 'data' ) ) );
$block->set_needs_compile( true );
//$block->setCloudUpdateRequired( true );
$block->save();
do_action( 'brizy_saved_block_created', $block );
do_action( 'brizy_global_data_updated' );
$this->success( $block->createResponse() );
} catch ( Exception $exception ) {
$this->error( 400, $exception->getMessage() );
}
}
public function actionUpdateSavedBlock() {
$this->verifyNonce( self::nonce );
try {
if ( ! $this->param( 'uid' ) ) {
$this->error( '400', 'Invalid uid' );
}
if ( ! $this->param( 'data' ) ) {
$this->error( '400', 'Invalid data' );
}
if ( $this->param( 'dataVersion' ) === null ) {
$this->error( '400', 'Invalid data version' );
}
if ( ! $this->param( 'meta' ) ) {
$this->error( 400, 'Invalid meta data' );
}
if ( ! $this->param( 'media' ) ) {
$this->error( 400, 'Invalid media data provided' );
}
$bockManager = new Brizy_Admin_Blocks_Manager( Brizy_Admin_Blocks_Main::CP_SAVED );
$block = $bockManager->getEntity( $this->param( 'uid' ) );
if ( ! $block instanceof Brizy_Editor_Block ) {
$this->error( '404', 'Block not found' );
}
$block->set_editor_data( stripslashes( $this->param( 'data' ) ) );
$block->setDataVersion( $this->param( 'dataVersion' ) );
$block->setMedia( stripslashes( $this->param( 'media' ) ) );
$block->setMeta( stripslashes( $this->param( 'meta' ) ) );
if ( (int) $this->param( 'is_autosave' ) ) {
$block->save( 1 );
} else {
$block->save();
do_action( 'brizy_saved_block_updated', $block );
do_action( 'brizy_global_data_updated' );
}
Brizy_Editor_Block::cleanClassCache();
$this->success( Brizy_Editor_Block::get( $block->getWpPostId() )->createResponse() );
} catch ( Exception $exception ) {
$this->error( 400, $exception->getMessage() );
}
}
public function actionDeleteSavedBlock() {
$this->verifyNonce( self::nonce );
if ( ! $this->param( 'uid' ) ) {
$this->error( '400', 'Invalid uid' );
}
try {
$bockManager = new Brizy_Admin_Blocks_Manager( Brizy_Admin_Blocks_Main::CP_SAVED );
$block = $bockManager->getEntity( $this->param( 'uid' ) );
do_action( 'brizy_saved_block_delete', $this->param( 'uid' ) );
if ( $block ) {
do_action( 'brizy_global_data_deleted' );
$bockManager->deleteEntity( $block );
} else {
$this->error( '404', 'Block not found' );
}
} catch ( Exception $e ) {
$this->error( '500', 'Unable to delete block' );
}
$this->success( null );
}
public function actionUpdateBlockPositions() {
global $wpdb;
$this->verifyNonce( self::nonce );
$data = file_get_contents( "php://input" );
$dataObject = json_decode( $data );
if ( ! $dataObject ) {
$this->error( 400, 'Invalid position data provided' );
}
$wpdb->query( 'START TRANSACTION' );
try {
foreach ( get_object_vars( $dataObject ) as $uid => $position ) {
if ( ! ( isset( $position->top ) && isset( $position->bottom ) && isset( $position->align ) ) ) {
throw new Exception();
}
$positionObj = new Brizy_Editor_BlockPosition( $position->top, $position->bottom, $position->align );
$bockManager = new Brizy_Admin_Blocks_Manager( Brizy_Admin_Blocks_Main::CP_GLOBAL );
$block = $bockManager->getEntity( $uid );
if ( ! $block ) {
throw new Exception();
}
$block->setPosition( $positionObj );
if ( $this->param( 'is_autosave' ) == 1 ) {
$block->save( 1 );
} else {
$block->saveStorage();
}
do_action( 'brizy_global_block_updated', $block );
}
do_action( 'brizy_global_data_updated' );
$wpdb->query( 'COMMIT' );
} catch ( Exception $e ) {
$wpdb->query( 'ROLLBACK' );
$this->error( '400', 'Unable to save block positions' );
}
$this->success( json_encode( $dataObject ) );
}
/**
* @param $uid
* @param $postType
*
* @return string|null
*/
private function getBlockIdByUidAndBlockType( $uid, $postType ) {
global $wpdb;
$prepare = $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} p
JOIN {$wpdb->postmeta} pm ON
pm.post_id=p.ID and
meta_key='brizy_post_uid' and
meta_value='%s'
WHERE p.post_type IN ('%s')
ORDER BY p.ID DESC
LIMIT 1",
array( $uid, $postType ) );
return $wpdb->get_var( $prepare );
}
/**
* @param $uid
* @param $postType
*
* @return string|null
*/
private function getBlockIdByUid( $uid ) {
global $wpdb;
$prepare = $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} p
JOIN {$wpdb->postmeta} pm ON pm.post_id=p.ID and meta_key='brizy_post_uid' and meta_value='%s'
WHERE p.post_type <> 'attachment'
ORDER BY p.ID DESC
LIMIT 1",
array( $uid, ) );
return $wpdb->get_var( $prepare );
}
/**
* @param $id
* @param $postType
*
* @return Brizy_Editor_Block|null
* @throws Brizy_Editor_Exceptions_NotFound
*/
private function getBlock( $id, $postType ) {
$postId = $this->getBlockIdByUidAndBlockType( $id, $postType );
if ( $postId ) {
return Brizy_Editor_Block::get( $postId );
}
return null;
}
/**
* @param $uid
* @param $status
* @param $type
*
* @return Brizy_Editor_Block
* @throws Brizy_Editor_Exceptions_NotFound
*/
private function createBlock( $uid, $status, $type ) {
$name = md5( time() );
$post = wp_insert_post( array(
'post_title' => $name,
'post_name' => $name,
'post_status' => $status,
'post_type' => $type,
) );
if ( $post ) {
$brizyPost = Brizy_Editor_Block::get( $post, $uid );
$brizyPost->set_uses_editor( true );
$brizyPost->set_needs_compile( true );
$brizyPost->setDataVersion( 1 );
return $brizyPost;
}
throw new Exception( 'Unable to create block' );
}
/**
* @param $postUid
* @param $postType
*
* @return false|WP_Post|null
*/
private function deleteBlock( $block, $postType ) {
if ( $postType === Brizy_Admin_Blocks_Main::CP_SAVED ) {
}
return wp_delete_post( $block->getWpPostId() );
}
}

View File

@@ -0,0 +1,150 @@
<?php
/**
* Created by PhpStorm.
* User: alex
* Date: 1/11/19
* Time: 10:59 AM
*/
class Brizy_Admin_Blocks_Main {
const CP_GLOBAL = 'brizy-global-block';
const CP_SAVED = 'brizy-saved-block';
/**
* @return Brizy_Admin_Blocks_Main
*/
public static function _init() {
static $instance;
if ( ! $instance ) {
$instance = new self();
}
return $instance;
}
/**
* BrizyPro_Admin_Popups constructor.
*/
public function __construct() {
if ( Brizy_Editor_User::is_user_allowed() ) {
add_action( 'wp_loaded', array( $this, 'initializeActions' ) );
}
add_filter( 'brizy_global_data', array( $this, 'populateGlobalData' ) );
}
/**
* Populated the global data for compiler
*
* @param $globalData
*
* @return mixed
* @throws Brizy_Editor_Exceptions_NotFound
*/
public function populateGlobalData( $globalData ) {
if ( ! is_object( $globalData ) ) {
$globalData = (object) array( 'globalBlocks' => array(), 'savedBlocks' => array() );
}
$blocks = get_posts( array(
'post_type' => Brizy_Admin_Blocks_Main::CP_GLOBAL,
'posts_per_page' => - 1,
'post_status' => 'publish',
'orderby' => 'ID',
'order' => 'ASC',
) );
foreach ( $blocks as $block ) {
$brizy_editor_block = Brizy_Editor_Block::get( $block );
$uid = $brizy_editor_block->getUid();
$block_data = $brizy_editor_block->convertToOptionValue();
$globalData->globalBlocks[ $uid ] = array(
'data' => json_decode( $brizy_editor_block->get_editor_data() ),
'position' => $block_data['position'],
'rules' => $block_data['rules']
);
}
$blocks = get_posts( array(
'post_type' => Brizy_Admin_Blocks_Main::CP_SAVED,
'posts_per_page' => - 1,
'post_status' => 'publish',
'orderby' => 'ID',
'order' => 'ASC',
) );
foreach ( $blocks as $block ) {
$brizy_editor_block = Brizy_Editor_Block::get( $block );
$block_data = $brizy_editor_block->convertToOptionValue();
$globalData->savedBlocks[] = array(
'data' => json_decode( $brizy_editor_block->get_editor_data() ),
'rules' => $block_data['rules']
);
}
return $globalData;
}
public function initializeActions() {
Brizy_Admin_Blocks_Api::_init();
}
static public function registerCustomPosts() {
$labels = array(
'name' => _x( 'Global blocks', 'post type general name' ),
);
register_post_type( self::CP_GLOBAL,
array(
'labels' => $labels,
'public' => false,
'has_archive' => false,
'description' => __bt( 'brizy', 'Brizy' ) . ' ' . __( 'global block.', 'brizy' ),
'publicly_queryable' => false,
'show_ui' => false,
'show_in_menu' => false,
'query_var' => false,
'capability_type' => 'page',
'hierarchical' => false,
'show_in_rest' => false,
'exclude_from_search' => true,
'supports' => array( 'title', 'revisions', 'page-attributes' )
)
);
$labels = array(
'name' => _x( 'Saved blocks', 'brizy' ),
);
register_post_type( self::CP_SAVED,
array(
'labels' => $labels,
'public' => false,
'has_archive' => false,
'description' => __bt( 'brizy', 'Brizy' ) . ' ' . __( 'global block.' ),
'publicly_queryable' => false,
'show_ui' => false,
'show_in_menu' => false,
'query_var' => false,
'capability_type' => 'page',
'hierarchical' => false,
'show_in_rest' => false,
'exclude_from_search' => true,
'supports' => array( 'title', 'revisions', 'page-attributes' )
)
);
add_filter( 'brizy_supported_post_types', function ( $posts ) {
$posts[] = self::CP_SAVED;
$posts[] = self::CP_GLOBAL;
return $posts;
} );
}
}

View File

@@ -0,0 +1,202 @@
<?php
class Brizy_Admin_Blocks_Manager extends Brizy_Admin_Entity_AbstractManager {
/**
* @var
*/
private $blockType;
/**
* Brizy_Admin_Blocks_Manager constructor.
*
* @param $type
*
* @throws Exception
*/
public function __construct( $type ) {
if ( ! in_array( $type, [ Brizy_Admin_Blocks_Main::CP_GLOBAL, Brizy_Admin_Blocks_Main::CP_SAVED ] ) ) {
throw new Exception();
}
$this->blockType = $type;
}
/**
* @param $args
*
* @return Brizy_Editor_Block[]
* @throws Exception
*/
public function getEntities( $args ) {
return $this->getEntitiesByType( $this->blockType, $args );
}
/**
* @param $uid
*
* @return Brizy_Editor_Block
* @throws Exception
*/
public function getEntity( $uid ) {
return $this->getEntityUidAndType( $uid, $this->blockType );
}
/**
* @param $uid
* @param string $status
*
* @return mixed|null+
*/
public function createEntity( $uid, $status = 'publish' ) {
return $this->createEntityByType( $uid, $this->blockType, $status );
}
/**
* @param $post
* @param null $uid
*
* @return Brizy_Editor_Block|Brizy_Editor_Post|mixed$uid
* @throws Exception
*/
protected function convertWpPostToEntity( $post, $uid = null ) {
return Brizy_Editor_Block::get( $post, $uid );
}
//=====================================================================
//=====================================================================
//=====================================================================
//=====================================================================
//=====================================================================
//=====================================================================
/**
* @param $type
* @param $arags
* @param array $fields
*
* @return array
* @throws Brizy_Editor_Exceptions_NotFound
*/
public function getAllBlocks( $type, $arags, $fields = array() ) {
$blocks = $this->getLocalBlocks( $type, $arags, $fields );
try {
$versions = $this->cloud->getCloudEditorVersions();
if ( $this->cloud && $type == Brizy_Admin_Blocks_Main::CP_SAVED && $versions['sync'] == BRIZY_SYNC_VERSION ) {
$cloudBlocks = $this->cloud->getBlocks( array( 'fields' => $fields ) );
foreach ( (array) $cloudBlocks as $cblock ) {
$existingBlock = false;
foreach ( $blocks as $block ) {
if ( $cblock->uid == $block['uid'] ) {
$existingBlock = true;
}
}
if ( ! $existingBlock ) {
$localBlock = $this->getLocalBlock( Brizy_Admin_Blocks_Main::CP_SAVED, $cblock->uid );
if ( in_array( 'synchronized', $fields ) ) {
if ( $localBlock ) {
$cblock->synchronized = $localBlock->isSynchronized( $this->cloud->getBrizyProject()->getCloudAccountId() );
} else {
$cblock->synchronized = false;
}
}
if ( in_array( 'isCloudEntity', $fields ) ) {
$cblock->isCloudEntity = true;
}
if ( in_array( 'synchronizable', $fields ) ) {
$cblock->synchronizable = true;
}
$blocks[] = (array) $cblock;
}
}
}
} catch ( Exception $e ) {
// do nothing...
}
return $blocks;
}
public function getBlockByUid( $type, $uid ) {
$block = $this->getLocalBlock( $type, $uid );
$versions = $this->cloud->getCloudEditorVersions();
if ( ! $block && $this->cloud && $type == Brizy_Admin_Blocks_Main::CP_SAVED && $versions['sync'] == BRIZY_SYNC_VERSION ) {
$bridge = new Brizy_Admin_Cloud_BlockBridge( $this->cloud );
$bridge->import( $uid );
$block = $this->getLocalBlock( $type, $uid );
}
return $block;
}
/**
* @param $type
* @param array $arags
* @param array $fields
*
* @return array
* @throws Brizy_Editor_Exceptions_NotFound
*/
public function getLocalBlocks( $type, $arags = array(), $fields = array() ) {
$filterArgs = array(
'post_type' => $type,
'posts_per_page' => - 1,
'post_status' => 'any',
'orderby' => 'ID',
'order' => 'ASC',
);
$filterArgs = array_merge( $filterArgs, $arags );
$wpBlocks = get_posts( $filterArgs );
$blocks = array();
foreach ( $wpBlocks as $wpPost ) {
$blocks[] = Brizy_Editor_Block::get( $wpPost )->createResponse( $fields );
}
return $blocks;
}
/**
* @param $type
* @param $uid
*
* @return array|null
* @throws Brizy_Editor_Exceptions_NotFound
*/
public function getLocalBlock( $type, $uid ) {
$blocks = get_posts( array(
'post_type' => $type,
'post_status' => 'publish',
'meta_key' => 'brizy_post_uid',
'meta_value' => $uid,
'numberposts' => - 1,
'orderby' => 'ID',
'order' => 'DESC',
) );
if ( isset( $blocks[0] ) ) {
$block = \Brizy_Editor_Block::get( $blocks[0] )->createResponse();
} else {
$block = null;
}
return $block;
}
}