| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
class Brizy_Admin_Blocks_Manager extends Brizy_Admin_Entity_AbstractManager { |
| 5 |
|
| 6 |
/** |
| 7 |
* @var |
| 8 |
*/ |
| 9 |
private $blockType; |
| 10 |
|
| 11 |
/** |
| 12 |
* Brizy_Admin_Blocks_Manager constructor. |
| 13 |
* |
| 14 |
* @param $type |
| 15 |
* |
| 16 |
* @throws Exception |
| 17 |
*/ |
| 18 |
public function __construct( $type ) { |
| 19 |
if ( ! in_array( $type, [ Brizy_Admin_Blocks_Main::CP_GLOBAL, Brizy_Admin_Blocks_Main::CP_SAVED ] ) ) { |
| 20 |
throw new Exception(); |
| 21 |
} |
| 22 |
|
| 23 |
$this->blockType = $type; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @param $args |
| 28 |
* |
| 29 |
* @return Brizy_Editor_Block[] |
| 30 |
* @throws Exception |
| 31 |
*/ |
| 32 |
public function getEntities( $args ) { |
| 33 |
return $this->getEntitiesByType( $this->blockType, $args ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @param $uid |
| 38 |
* |
| 39 |
* @return Brizy_Editor_Block|array |
| 40 |
* @throws Exception |
| 41 |
*/ |
| 42 |
public function getEntity( $uid ) { |
| 43 |
return $this->getEntityUidAndType( $uid, $this->blockType ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @param $uid |
| 48 |
* @param string $status |
| 49 |
* |
| 50 |
* @return mixed|null+ |
| 51 |
*/ |
| 52 |
public function createEntity( $uid, $status = 'publish' ) { |
| 53 |
return $this->createEntityByType( $uid, $this->blockType, $status ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @param $post |
| 58 |
* @param null $uid |
| 59 |
* |
| 60 |
* @return Brizy_Editor_Block|Brizy_Editor_Post|mixed$uid |
| 61 |
* @throws Exception |
| 62 |
*/ |
| 63 |
protected function convertWpPostToEntity( $post, $uid = null ) { |
| 64 |
return Brizy_Editor_Block::get( $post, $uid ); |
| 65 |
} |
| 66 |
|
| 67 |
//===================================================================== |
| 68 |
//===================================================================== |
| 69 |
//===================================================================== |
| 70 |
//===================================================================== |
| 71 |
//===================================================================== |
| 72 |
//===================================================================== |
| 73 |
|
| 74 |
|
| 75 |
/** |
| 76 |
* @param $type |
| 77 |
* @param $arags |
| 78 |
* @param array $fields |
| 79 |
* |
| 80 |
* @return array |
| 81 |
* @throws Brizy_Editor_Exceptions_NotFound |
| 82 |
*/ |
| 83 |
public function getAllBlocks( $type, $arags, $fields = array() ) { |
| 84 |
|
| 85 |
$blocks = $this->getLocalBlocks( $type, $arags, $fields ); |
| 86 |
|
| 87 |
try { |
| 88 |
$versions = $this->cloud->getCloudEditorVersions(); |
| 89 |
|
| 90 |
if ( $this->cloud && $type == Brizy_Admin_Blocks_Main::CP_SAVED && $versions['sync'] == BRIZY_SYNC_VERSION ) { |
| 91 |
|
| 92 |
$cloudBlocks = $this->cloud->getBlocks( array( 'fields' => $fields ) ); |
| 93 |
|
| 94 |
foreach ( (array) $cloudBlocks as $cblock ) { |
| 95 |
$existingBlock = false; |
| 96 |
foreach ( $blocks as $block ) { |
| 97 |
if ( $cblock->uid == $block['uid'] ) { |
| 98 |
$existingBlock = true; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
if ( ! $existingBlock ) { |
| 103 |
|
| 104 |
$localBlock = $this->getLocalBlock( Brizy_Admin_Blocks_Main::CP_SAVED, $cblock->uid ); |
| 105 |
|
| 106 |
if ( in_array( 'synchronized', $fields ) ) { |
| 107 |
if ( $localBlock ) { |
| 108 |
$cblock->synchronized = $localBlock->isSynchronized( $this->cloud->getBrizyProject()->getCloudAccountId() ); |
| 109 |
} else { |
| 110 |
$cblock->synchronized = false; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
if ( in_array( 'isCloudEntity', $fields ) ) { |
| 115 |
$cblock->isCloudEntity = true; |
| 116 |
} |
| 117 |
|
| 118 |
if ( in_array( 'synchronizable', $fields ) ) { |
| 119 |
$cblock->synchronizable = true; |
| 120 |
} |
| 121 |
|
| 122 |
$blocks[] = (array) $cblock; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
} |
| 127 |
} catch ( Exception $e ) { |
| 128 |
// do nothing... |
| 129 |
} |
| 130 |
|
| 131 |
return $blocks; |
| 132 |
} |
| 133 |
|
| 134 |
public function getBlockByUid( $type, $uid ) { |
| 135 |
$block = $this->getLocalBlock( $type, $uid ); |
| 136 |
$versions = $this->cloud->getCloudEditorVersions(); |
| 137 |
|
| 138 |
if ( ! $block && $this->cloud && $type == Brizy_Admin_Blocks_Main::CP_SAVED && $versions['sync'] == BRIZY_SYNC_VERSION ) { |
| 139 |
$bridge = new Brizy_Admin_Cloud_BlockBridge( $this->cloud ); |
| 140 |
$bridge->import( $uid ); |
| 141 |
|
| 142 |
$block = $this->getLocalBlock( $type, $uid ); |
| 143 |
} |
| 144 |
|
| 145 |
return $block; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* @param $type |
| 150 |
* @param array $arags |
| 151 |
* @param array $fields |
| 152 |
* |
| 153 |
* @return array |
| 154 |
* @throws Brizy_Editor_Exceptions_NotFound |
| 155 |
*/ |
| 156 |
public function getLocalBlocks( $type, $arags = array(), $fields = array() ) { |
| 157 |
$filterArgs = array( |
| 158 |
'post_type' => $type, |
| 159 |
'posts_per_page' => - 1, |
| 160 |
'post_status' => 'any', |
| 161 |
'orderby' => 'ID', |
| 162 |
'order' => 'ASC', |
| 163 |
); |
| 164 |
$filterArgs = array_merge( $filterArgs, $arags ); |
| 165 |
|
| 166 |
$wpBlocks = get_posts( $filterArgs ); |
| 167 |
$blocks = array(); |
| 168 |
|
| 169 |
foreach ( $wpBlocks as $wpPost ) { |
| 170 |
$blocks[] = Brizy_Editor_Block::get( $wpPost )->createResponse( $fields ); |
| 171 |
} |
| 172 |
|
| 173 |
return $blocks; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* @param $type |
| 178 |
* @param $uid |
| 179 |
* |
| 180 |
* @return array|null |
| 181 |
* @throws Brizy_Editor_Exceptions_NotFound |
| 182 |
*/ |
| 183 |
public function getLocalBlock( $type, $uid ) { |
| 184 |
$blocks = get_posts( array( |
| 185 |
'post_type' => $type, |
| 186 |
'post_status' => 'publish', |
| 187 |
'meta_key' => 'brizy_post_uid', |
| 188 |
'meta_value' => $uid, |
| 189 |
'numberposts' => - 1, |
| 190 |
'orderby' => 'ID', |
| 191 |
'order' => 'DESC', |
| 192 |
) ); |
| 193 |
|
| 194 |
if ( isset( $blocks[0] ) ) { |
| 195 |
$block = \Brizy_Editor_Block::get( $blocks[0] )->createResponse(); |
| 196 |
} else { |
| 197 |
$block = null; |
| 198 |
} |
| 199 |
|
| 200 |
return $block; |
| 201 |
} |
| 202 |
} |
| 203 |
|