AppsService.php
2 weeks ago
CollaborationCommentService.php
2 weeks ago
CollaborationService.php
2 weeks ago
CollectionItemService.php
2 weeks ago
CollectionService.php
2 weeks ago
EditorService.php
2 weeks ago
FontService.php
2 weeks ago
MediaService.php
2 weeks ago
PageService.php
2 weeks ago
PageSettingsService.php
2 weeks ago
UtilityPageService.php
2 weeks ago
PageService.php
362 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kirki\App\Services; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | use Exception; |
| 8 | use Kirki\App\Constants\CollaborationConnectionType; |
| 9 | use Kirki\App\Constants\CollaborationParent; |
| 10 | use Kirki\App\Constants\PageMetaKeys; |
| 11 | use Kirki\App\Constants\PostTypes; |
| 12 | use Kirki\App\DTO\Collaboration\CreateCollaborationDTO; |
| 13 | use Kirki\App\DTO\Page\EditorPagePayloadDTO; |
| 14 | use Kirki\App\DTO\Page\EditPageDTO; |
| 15 | use Kirki\App\DTO\Page\EditPopupDTO; |
| 16 | use Kirki\App\DTO\Page\PagePayloadDTO; |
| 17 | use Kirki\App\DTO\Page\TogglePageSymbolDTO; |
| 18 | use Kirki\App\Models\Page as PageModel; |
| 19 | use Kirki\App\Models\PostMeta; |
| 20 | use Kirki\App\Supports\Canvas; |
| 21 | use Kirki\App\Supports\Facades\GlobalData; |
| 22 | use Kirki\App\Supports\Facades\Page; |
| 23 | use Kirki\App\Supports\Template; |
| 24 | use Kirki\Framework\Http\Response; |
| 25 | |
| 26 | use function Kirki\App\soft_flush_rewrite_rules; |
| 27 | use function Kirki\Framework\collection; |
| 28 | |
| 29 | class PageService |
| 30 | { |
| 31 | /** @var CollaborationService */ |
| 32 | protected $collaboration_service; |
| 33 | |
| 34 | public function __construct(CollaborationService $collaboration_service) |
| 35 | { |
| 36 | $this->collaboration_service = $collaboration_service; |
| 37 | } |
| 38 | |
| 39 | public static function create() |
| 40 | { |
| 41 | return new static(new CollaborationService()); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Create a new page. |
| 46 | * |
| 47 | * @param PagePayloadDTO $payload |
| 48 | * |
| 49 | * @return PageModel |
| 50 | */ |
| 51 | public function save(PagePayloadDTO $payload) |
| 52 | { |
| 53 | $page_data = [ |
| 54 | 'post_title' => $payload->post_title, |
| 55 | 'post_name' => $payload->post_title, |
| 56 | // Check if type = kirki_page then change it to wp page type. cause we only set template if page type is kirki_page. |
| 57 | 'post_type' => $payload->post_type === PostTypes::PAGE ? PostTypes::WP_PAGE : $payload->post_type, |
| 58 | ]; |
| 59 | |
| 60 | $page = PageModel::create_post($page_data); |
| 61 | |
| 62 | if (!empty($payload->blocks)) { |
| 63 | // This is for popup creation. cause popup has predefined blocks. |
| 64 | Page::save_blocks($page->ID, $payload->blocks); |
| 65 | } |
| 66 | |
| 67 | if (!empty($payload->conditions)) { |
| 68 | // This is for template creation. cause popup has predefined conditions. |
| 69 | PostMeta::update_meta_value($page->ID, PageMetaKeys::TEMPLATE_CONDITIONS, $payload->conditions); |
| 70 | } |
| 71 | |
| 72 | // @todo: Need to remove this code. after checking collection_type used or not |
| 73 | if (!empty($payload->collection_type)) { |
| 74 | PostMeta::update_meta_value($page->ID, PageMetaKeys::TEMPLATE_COLLECTION_TYPE, $payload->collection_type); |
| 75 | } |
| 76 | |
| 77 | if (!empty($payload->utility_page_type)) { |
| 78 | PostMeta::update_meta_value($page->ID, PageMetaKeys::UTILITY_PAGE_TYPE, $payload->utility_page_type); |
| 79 | } |
| 80 | |
| 81 | Page::save_editor_mode($page->ID); |
| 82 | |
| 83 | switch ($payload->post_type) { |
| 84 | case PostTypes::WP_PAGE: |
| 85 | case PostTypes::PAGE: |
| 86 | PostMeta::update_meta_value($page->ID, PageMetaKeys::PAGE_TEMPLATE, Canvas::get_full_canvas_template_path()); |
| 87 | break; |
| 88 | case PostTypes::UTILITY: |
| 89 | if (!empty($payload->utility_page_type)) { |
| 90 | Template::assign_utility_page_template($page->ID, $payload->utility_page_type); |
| 91 | soft_flush_rewrite_rules(); |
| 92 | } |
| 93 | break; |
| 94 | default: |
| 95 | break; |
| 96 | } |
| 97 | |
| 98 | if ( |
| 99 | !empty($payload->custom_template) |
| 100 | && isset($payload->custom_template['url']) |
| 101 | && !empty(($payload->custom_template['url'])) |
| 102 | ) { |
| 103 | Template::assign_custom_page_template($page->ID, $payload->custom_template['url']); |
| 104 | } |
| 105 | |
| 106 | return $page; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Update page data |
| 111 | * |
| 112 | * @param PageModel $page |
| 113 | * @param EditPageDTO $payload |
| 114 | * |
| 115 | * @return PageModel |
| 116 | */ |
| 117 | public function update(PageModel $page, EditPageDTO $payload) |
| 118 | { |
| 119 | $post_payload = collection($payload->to_array())->filter(fn ($value) => !is_null($value))->to_array(); |
| 120 | |
| 121 | if (!empty($post_payload)) { |
| 122 | $page = PageModel::update_post($page->ID, $post_payload); |
| 123 | |
| 124 | if ($page->post_type === PostTypes::UTILITY) { |
| 125 | soft_flush_rewrite_rules(); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return $page; |
| 130 | } |
| 131 | |
| 132 | public function update_popup_data(PageModel $popup, EditPopupDTO $payload) |
| 133 | { |
| 134 | if (!is_null($payload->blocks)) { |
| 135 | Page::save_blocks($popup->ID, $payload->blocks); |
| 136 | } |
| 137 | |
| 138 | if (!is_null($payload->styleBlocks)) { |
| 139 | Page::save_random_global_style_blocks($popup->ID, $payload->styleBlocks); |
| 140 | } |
| 141 | |
| 142 | if (!is_null($payload->usedFonts)) { |
| 143 | Page::save_used_font_list($popup->ID, $payload->usedFonts); |
| 144 | } |
| 145 | |
| 146 | return $popup; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Save editor page data |
| 151 | * |
| 152 | * @return int|false |
| 153 | */ |
| 154 | public function save_page_data(EditorPagePayloadDTO $payload) |
| 155 | { |
| 156 | if (is_null($payload->page) || empty($payload->data)) { |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | $staging_version = false; |
| 161 | $page_data = $payload->data; |
| 162 | |
| 163 | if($payload->is_staging) { |
| 164 | list( |
| 165 | 'staging_version' => $staging_version, |
| 166 | 'data' => $page_data |
| 167 | ) = Page::save_staging_data($payload); |
| 168 | } |
| 169 | |
| 170 | if (isset($page_data['styles'])) { |
| 171 | $global_style_blocks_for_collaboration = []; |
| 172 | |
| 173 | foreach ($page_data['styles'] as $key => $style_block) { |
| 174 | if ( |
| 175 | (isset($style_block['isDefault']) && $style_block['isDefault'] === true) |
| 176 | || (isset($style_block['isGlobal']) && $style_block['isGlobal'] === true) |
| 177 | ) { |
| 178 | if (isset($style_block['fromStage'])){ |
| 179 | unset($page_data['styles'][$key]['fromStage']); |
| 180 | $global_style_blocks_for_collaboration[$key] = $page_data['styles'][$key]; |
| 181 | |
| 182 | continue; |
| 183 | } |
| 184 | |
| 185 | unset($page_data['styles'][$key]); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | if ( |
| 190 | count($global_style_blocks_for_collaboration) > 0 && |
| 191 | $payload->session_id && $payload->is_staging === false |
| 192 | ) { |
| 193 | // Cause template import also called this method without session_id |
| 194 | $create_collaboration_dto = CreateCollaborationDTO::from_array([ |
| 195 | 'session_id' => $payload->session_id, |
| 196 | 'parent' => CollaborationParent::GLOBAL, |
| 197 | 'parent_id' => 0, |
| 198 | 'data' => [ |
| 199 | 'type' => CollaborationConnectionType::UPDATE_GLOBAL_STYLE, |
| 200 | 'payload' => ['styleBlock' => $global_style_blocks_for_collaboration], |
| 201 | ], |
| 202 | ]); |
| 203 | |
| 204 | $this->collaboration_service->save_action($create_collaboration_dto); |
| 205 | } |
| 206 | |
| 207 | Page::update_page_styleblocks($payload->page->ID, $page_data['styles']); |
| 208 | unset($page_data['styles']); |
| 209 | } |
| 210 | |
| 211 | if (isset($page_data['usedStyles'])) { |
| 212 | Page::save_used_style_block_ids($payload->page->ID, $page_data['usedStyles']); |
| 213 | unset($page_data['usedStyles']); |
| 214 | } |
| 215 | |
| 216 | if (isset($page_data['usedStyleIdsRandom'])) { |
| 217 | Page::save_random_used_style_block_ids($payload->page->ID, $page_data['usedStyleIdsRandom']); |
| 218 | unset($page_data['usedStyleIdsRandom']); |
| 219 | } |
| 220 | |
| 221 | if (isset($page_data['usedFonts'])) { |
| 222 | Page::save_used_font_list($payload->page->ID, $page_data['usedFonts']); |
| 223 | unset( $page_data['usedFonts'] ); |
| 224 | } |
| 225 | |
| 226 | if (isset($page_data['customFonts'])) { |
| 227 | //save others data if isset. this is for template import |
| 228 | $custom_fonts = GlobalData::get_global_custom_fonts(); |
| 229 | |
| 230 | foreach ($page_data['customFonts'] as $key => $custom_font) { |
| 231 | $custom_fonts[$key] = $custom_font; |
| 232 | } |
| 233 | |
| 234 | GlobalData::update_global_custom_fonts($custom_fonts); |
| 235 | unset($page_data['customFonts']); |
| 236 | } |
| 237 | |
| 238 | if (isset($page_data['viewportList'])) { |
| 239 | // Save others data if isset. this is for template import |
| 240 | $controller_data = GlobalData::get_global_ui_controller(); |
| 241 | |
| 242 | if (!$controller_data) { |
| 243 | $controller_data = [ |
| 244 | 'viewport' => [ |
| 245 | 'active' => 'md', |
| 246 | 'defaults'=> ["md", "tablet", "mobileLandscape", "mobile"], |
| 247 | 'list' => $page_data['viewportList'], |
| 248 | 'mdWidth' => 1200, |
| 249 | "scale" => 1, |
| 250 | "width" => 2484, |
| 251 | "zoom" => 1 |
| 252 | ] |
| 253 | ]; |
| 254 | } elseif (isset($controller_data['viewport'], $controller_data['viewport']['list'])) { |
| 255 | $controller_data['viewport']['list'] = $page_data['viewportList']; |
| 256 | } |
| 257 | |
| 258 | GlobalData::update_global_ui_controller($controller_data); |
| 259 | unset( $page_data['viewportList'] ); |
| 260 | } |
| 261 | |
| 262 | if (isset($page_data['blocks'])) { |
| 263 | Page::save_blocks($payload->page->ID, ['blocks' => $page_data['blocks']]); |
| 264 | } |
| 265 | |
| 266 | Page::save_editor_mode($payload->page->ID); |
| 267 | |
| 268 | return $staging_version; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Toggle disabled page symbols |
| 273 | * |
| 274 | * @param TogglePageSymbolDTO $payload |
| 275 | * |
| 276 | * @return bool |
| 277 | */ |
| 278 | public function toggle_disabled_page_symbols(TogglePageSymbolDTO $payload) |
| 279 | { |
| 280 | $prev_status = PostMeta::get_meta_value($payload->post_id, PageMetaKeys::DISABLED_PAGE_SYMBOLS, []); |
| 281 | |
| 282 | if (!is_array($prev_status)) { |
| 283 | $prev_status = []; |
| 284 | } |
| 285 | |
| 286 | $current_status = array_merge($prev_status, [$payload->symbol_type => $payload->disable]); |
| 287 | |
| 288 | PostMeta::update_meta_value($payload->post_id, PageMetaKeys::DISABLED_PAGE_SYMBOLS, $current_status); |
| 289 | |
| 290 | return true; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Duplicate page |
| 295 | * |
| 296 | * @param PageModel $current_page |
| 297 | * |
| 298 | * @return PageModel New page |
| 299 | */ |
| 300 | public function duplicate_page(PageModel $current_page) |
| 301 | { |
| 302 | $new_page = PageModel::create_post([ |
| 303 | 'post_title' => $current_page->post_title . ' (copy)', |
| 304 | 'post_content' => $current_page->post_content, |
| 305 | 'post_name' => $current_page->post_name, |
| 306 | 'post_type' => $current_page->post_type, |
| 307 | 'post_status' => $current_page->post_status, |
| 308 | ]); |
| 309 | |
| 310 | $current_page_block = PostMeta::get_meta_value($current_page->ID, PageMetaKeys::BLOCKS); |
| 311 | |
| 312 | if (!empty($current_page_block)) { |
| 313 | Page::save_blocks($new_page->ID, $current_page_block); |
| 314 | Page::save_editor_mode($new_page->ID); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Also duplicate _wp_page_template meta if exists |
| 319 | */ |
| 320 | $current_page_template = PostMeta::get_meta_value($current_page->ID, PageMetaKeys::PAGE_TEMPLATE); |
| 321 | |
| 322 | if (!empty($current_page_template)) { |
| 323 | $current_page_template = Canvas::normalize_full_canvas_template_path($current_page_template); |
| 324 | PostMeta::update_meta_value($new_page->ID, PageMetaKeys::PAGE_TEMPLATE, $current_page_template); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Also duplicate this page style blocks if exists |
| 329 | */ |
| 330 | $current_random_post_styles = PostMeta::get_meta_value($current_page->ID, PageMetaKeys::STYLE_BLOCK_RANDOM, []); |
| 331 | |
| 332 | if (!empty($current_random_post_styles)) { |
| 333 | Page::save_random_global_style_blocks($new_page->ID, $current_random_post_styles); |
| 334 | } |
| 335 | |
| 336 | $current_used_fonts = PostMeta::get_meta_value($current_page->ID, PageMetaKeys::USED_FONT_LIST, []); |
| 337 | |
| 338 | if (!empty($current_used_fonts)) { |
| 339 | Page::save_used_font_list($new_page->ID, $current_used_fonts); |
| 340 | } |
| 341 | |
| 342 | if ($current_page->post_type === PostTypes::UTILITY) { |
| 343 | soft_flush_rewrite_rules(); |
| 344 | } |
| 345 | |
| 346 | return $new_page; |
| 347 | } |
| 348 | |
| 349 | public function delete_page(PageModel $page) { |
| 350 | $is_deleted = PageModel::delete_post($page->ID); |
| 351 | |
| 352 | if (!$is_deleted) { |
| 353 | throw new Exception(esc_html__('Page not deleted.', 'kirki'), Response::FORBIDDEN); |
| 354 | } |
| 355 | |
| 356 | if ($page->post_type === PostTypes::UTILITY) { |
| 357 | soft_flush_rewrite_rules(); |
| 358 | } |
| 359 | |
| 360 | return $is_deleted; |
| 361 | } |
| 362 | } |