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