API
2 days ago
Admin
3 weeks ago
Ajax
3 days ago
ExportImport
3 days ago
FormValidator
2 months ago
Frontend
3 days ago
Manager
1 week ago
API.php
3 days ago
Admin.php
2 months ago
Ajax.php
3 days ago
Apps.php
3 weeks ago
ContentManager.php
2 months ago
DbQueryUtils.php
2 months ago
ElementVisibilityConditions.php
2 months ago
Frontend.php
2 months ago
HelperFunctions.php
3 days ago
KirkiBase.php
3 weeks ago
PostsQueryUtils.php
2 months ago
Staging.php
3 days ago
View.php
1 month ago
Staging.php
499 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This class act like controller for Editor, Iframe, Frontend |
| 5 | * |
| 6 | * @package kirki |
| 7 | */ |
| 8 | |
| 9 | namespace Kirki; |
| 10 | |
| 11 | use Kirki\Ajax\Collaboration\Collaboration; |
| 12 | use Kirki\HelperFunctions; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; // Exit if accessed directly. |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Frontend handler class |
| 20 | */ |
| 21 | class Staging { |
| 22 | |
| 23 | /** |
| 24 | * @deprecated |
| 25 | * @see Kirki\App\Managers\PageManager::get_most_recent_stage_version() |
| 26 | */ |
| 27 | public static function get_most_recent_stage_version( $post_id, $stage_must = true, $restoring = false, $old_version = false ) { |
| 28 | $staged_versions = self::get_all_staged_versions( $post_id, true ); |
| 29 | $recent_stage_version = false; |
| 30 | $version_number = 0; |
| 31 | $being_restored = false; |
| 32 | |
| 33 | foreach ( $staged_versions as $version ) { |
| 34 | if ( is_array( $version ) && intval( $version['version'] ) > $version_number ) { |
| 35 | $version_number = $version['version']; |
| 36 | $recent_stage_version = $version; |
| 37 | } |
| 38 | |
| 39 | if ( $restoring && intval( $old_version ) === intval( $version['version'] ) ) { |
| 40 | $being_restored = $version; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | if ( $version_number === 0 || ( $stage_must && isset( $recent_stage_version['publish'] ) && $recent_stage_version['publish'] ) || $restoring ) { |
| 45 | $version_number = self::add_stage_version( $post_id, $version_number + 1, $staged_versions, $being_restored ); |
| 46 | } |
| 47 | |
| 48 | return $version_number; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @deprecated |
| 53 | * @see Kirki\App\Managers\PageManager::get_all_staged_versions() |
| 54 | */ |
| 55 | public static function get_all_staged_versions( $post_id, $internal = false, $withname = false ) { |
| 56 | // delete_post_meta($post_id, KIRKI_META_NAME_FOR_STAGED_VERSIONS); |
| 57 | $staged_versions = get_post_meta( $post_id, KIRKI_META_NAME_FOR_STAGED_VERSIONS, true ); |
| 58 | if ( ! is_array( $staged_versions ) || count( $staged_versions ) < 1 ) { |
| 59 | $staged_versions = self::create_first_stage_version( $post_id ); |
| 60 | } |
| 61 | if ( $withname ) { |
| 62 | $staged_versions = self::add_editor_names_into_stage_versions( $staged_versions ); |
| 63 | } |
| 64 | if ( $internal ) { |
| 65 | return $staged_versions; |
| 66 | } |
| 67 | wp_send_json( $staged_versions ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @deprecated |
| 72 | * @see Kirki\App\Managers\PageManager::publish_stage_version() |
| 73 | */ |
| 74 | public static function publish_stage_version( $internal = false, $post_id = false ) { |
| 75 | if ( ! $internal ) { |
| 76 | $post_id = HelperFunctions::sanitize_text( isset( $_POST['post_id'] ) ? $_POST['post_id'] : null ); |
| 77 | } |
| 78 | |
| 79 | if ( ! $post_id ) { |
| 80 | wp_send_json( false, 400 ); |
| 81 | } |
| 82 | |
| 83 | $version_id = self::get_most_recent_stage_version( $post_id, false ); |
| 84 | $staged_versions = self::get_all_staged_versions( $post_id, true, true ); |
| 85 | $staged_versions = array_map( |
| 86 | function ( $item ) use ( $version_id ) { |
| 87 | return ( is_array( $item ) && isset( $item['version'] ) && intval( $item['version'] ) === intval( $version_id ) ) |
| 88 | ? array_merge( $item, array( 'publish' => true ) ) |
| 89 | : array_merge( $item, array( 'publish' => false ) ); |
| 90 | }, |
| 91 | $staged_versions |
| 92 | ); |
| 93 | update_post_meta( $post_id, KIRKI_META_NAME_FOR_STAGED_VERSIONS, $staged_versions ); |
| 94 | if ( $internal ) { |
| 95 | return $staged_versions; |
| 96 | } |
| 97 | wp_send_json( $staged_versions ); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | /** |
| 102 | * @deprecated |
| 103 | * @see Kirki\App\Managers\PageManager::get_published_stage_version() |
| 104 | */ |
| 105 | public static function get_published_stage_version( $post_id ) { |
| 106 | $staged_versions = self::get_all_staged_versions( $post_id, true, true ); |
| 107 | |
| 108 | // Find the first staged version that has 'publish' set to true |
| 109 | $published_version = null; |
| 110 | foreach ( $staged_versions as $item ) { |
| 111 | if ( ! empty( $item['publish'] ) ) { |
| 112 | $published_version = $item; |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return isset( $published_version['version'] ) ? $published_version['version'] : false; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @deprecated |
| 122 | * @see Kirki\App\Managers\PageManager::get_published_staged_version_info() |
| 123 | */ |
| 124 | public static function get_published_stage_version_info( $post_id ) { |
| 125 | $staged_versions = self::get_all_staged_versions( $post_id, true, true ); |
| 126 | |
| 127 | // Find the first staged version that has 'publish' set to true |
| 128 | $published_version = null; |
| 129 | foreach ( $staged_versions as $item ) { |
| 130 | if ( ! empty( $item['publish'] ) ) { |
| 131 | $published_version = $item; |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return isset( $published_version['version'] ) ? $published_version : false; |
| 137 | } |
| 138 | |
| 139 | public static function get_page_staging_data( $post_id, $version_id ) { |
| 140 | $kirki_data = array(); |
| 141 | $kirki_data['blocks'] = null; |
| 142 | |
| 143 | $meta_name = self::get_staged_meta_name( 'kirki', $post_id, $version_id ); |
| 144 | $staging_post_meta = get_post_meta( $post_id, $meta_name, true ); |
| 145 | if ( $staging_post_meta ) { |
| 146 | $kirki_data = $staging_post_meta; |
| 147 | } |
| 148 | |
| 149 | $styles = HelperFunctions::get_page_styleblocks( $post_id, $version_id ); |
| 150 | $kirki_data['styles'] = isset( $styles ) ? $styles : ''; |
| 151 | return $kirki_data; |
| 152 | } |
| 153 | |
| 154 | // This function receives the page data (blocks, styleblocks...) and saves in stage version whichever required in stage |
| 155 | // returns the data which needs to be saved in Publish version |
| 156 | /** |
| 157 | * @deprecated |
| 158 | * @see Kirki\App\Services\PageService::save_page_data() |
| 159 | */ |
| 160 | public static function save_page_staging_data_to_db( $post_id, $page_data ) { |
| 161 | $staging_version = self::get_most_recent_stage_version( $post_id ); |
| 162 | self::update_last_edited_datetime_of_stage_version( $post_id ); |
| 163 | if ( isset( $page_data['styles'] ) ) { |
| 164 | $new_random_styles = $page_data['styles']; |
| 165 | $new_global_styles = array(); // global Styleblocks which won't be saved in publish but stage |
| 166 | $add_to_publish_global = array(); // global Styleblocks which won't be saved in stage but publish |
| 167 | |
| 168 | foreach ( $new_random_styles as $key => $style ) { |
| 169 | if ( ( isset( $style['isDefault'] ) && $style['isDefault'] === true ) || ( isset( $style['isGlobal'] ) && $style['isGlobal'] === true ) ) { |
| 170 | if ( isset( $style['fromStage'] ) && $style['fromStage'] ) { |
| 171 | $new_global_styles[ $key ] = $style; |
| 172 | } else { |
| 173 | // Adding fromStage true because publish version saves only styleblocks having fromStage |
| 174 | $style['fromStage'] = true; |
| 175 | $add_to_publish_global[ $key ] = $style; |
| 176 | } |
| 177 | unset( $new_random_styles[ $key ] ); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | $meta_key = self::get_staged_meta_name( KIRKI_GLOBAL_STYLE_BLOCK_META_KEY . '_random', $post_id, $staging_version ); |
| 182 | update_post_meta( $post_id, $meta_key, $new_random_styles ); |
| 183 | |
| 184 | // $data = array( |
| 185 | // 'type' => 'COLLABORATION_UPDATE_GLOBAL_STYLE', |
| 186 | // 'payload' => array( 'styleBlock' => $new_random_styles ), |
| 187 | // ); |
| 188 | // Collaboration::save_action_to_db( 'post', $post_id, $data, 1 ); |
| 189 | |
| 190 | $global_meta_key = self::get_staged_meta_name( KIRKI_GLOBAL_STYLE_BLOCK_META_KEY, $post_id, $staging_version ); |
| 191 | update_post_meta( $post_id, $global_meta_key, $new_global_styles ); |
| 192 | |
| 193 | // $data = array( |
| 194 | // 'type' => 'COLLABORATION_UPDATE_GLOBAL_STYLE', |
| 195 | // 'payload' => array( 'styleBlock' => $new_global_styles ), |
| 196 | // ); |
| 197 | // Collaboration::save_action_to_db( 'global', 0, $data, 1 ); |
| 198 | |
| 199 | if ( count( $add_to_publish_global ) ) { |
| 200 | $page_data['styles'] = $add_to_publish_global; |
| 201 | } else { |
| 202 | unset( $page_data['styles'] ); // Unset if no global style blocks for publish version |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | if ( isset( $page_data['usedStyles'] ) ) { |
| 207 | $meta_key = KIRKI_META_NAME_FOR_USED_STYLE_BLOCK_IDS; |
| 208 | $meta_key = self::get_staged_meta_name( $meta_key, $post_id, $staging_version ); |
| 209 | update_post_meta( $post_id, $meta_key, $page_data['usedStyles'] ); |
| 210 | unset( $page_data['usedStyles'] ); |
| 211 | } |
| 212 | |
| 213 | if ( isset( $page_data['usedStyleIdsRandom'] ) ) { |
| 214 | $meta_key = KIRKI_META_NAME_FOR_USED_STYLE_BLOCK_IDS . '_random'; |
| 215 | $meta_key = self::get_staged_meta_name( $meta_key, $post_id, $staging_version ); |
| 216 | update_post_meta( $post_id, $meta_key, $page_data['usedStyleIdsRandom'] ); |
| 217 | unset( $page_data['usedStyleIdsRandom'] ); |
| 218 | } |
| 219 | |
| 220 | if ( isset( $page_data['usedFonts'] ) ) { |
| 221 | $meta_key = KIRKI_META_NAME_FOR_USED_FONT_LIST; |
| 222 | $meta_key = self::get_staged_meta_name( $meta_key, $post_id, $staging_version ); |
| 223 | update_post_meta( $post_id, $meta_key, $page_data['usedFonts'] ); |
| 224 | unset( $page_data['usedFonts'] ); |
| 225 | } |
| 226 | |
| 227 | if ( isset( $page_data['blocks'] ) ) { |
| 228 | $meta_key = 'kirki'; |
| 229 | $meta_key = self::get_staged_meta_name( $meta_key, $post_id, $staging_version ); |
| 230 | update_post_meta( $post_id, $meta_key, array( 'blocks' => $page_data['blocks'] ) ); |
| 231 | // $data = array( |
| 232 | // 'type' => 'COLLABORATION_PAGE_DATA', |
| 233 | // 'payload' => array('data' => $page_data['blocks']), |
| 234 | // ); |
| 235 | unset( $page_data['blocks'] ); |
| 236 | // Collaboration::save_action_to_db('post', $post_id, $data, 1); |
| 237 | } |
| 238 | |
| 239 | return array( |
| 240 | 'page_data' => $page_data, |
| 241 | 'version' => $staging_version, |
| 242 | ); |
| 243 | } |
| 244 | |
| 245 | public static function restore_stage_version() { |
| 246 | |
| 247 | $post_id = HelperFunctions::sanitize_text( isset( $_POST['post_id'] ) ? $_POST['post_id'] : null ); |
| 248 | $old_version_id = HelperFunctions::sanitize_text( isset( $_POST['stage_version'] ) ? $_POST['stage_version'] : null ); |
| 249 | |
| 250 | $new_version = self::get_most_recent_stage_version( $post_id, true, true, $old_version_id ); |
| 251 | |
| 252 | $old_meta_key = self::get_staged_meta_name( KIRKI_GLOBAL_STYLE_BLOCK_META_KEY . '_random', $post_id, $old_version_id ); |
| 253 | $old_data = get_post_meta( $post_id, $old_meta_key, true ); |
| 254 | $new_meta_key = self::get_staged_meta_name( KIRKI_GLOBAL_STYLE_BLOCK_META_KEY . '_random', $post_id, $new_version ); |
| 255 | update_post_meta( $post_id, $new_meta_key, $old_data ); |
| 256 | |
| 257 | $old_meta_key = self::get_staged_meta_name( KIRKI_GLOBAL_STYLE_BLOCK_META_KEY, $post_id, $old_version_id ); |
| 258 | $old_data = get_post_meta( $post_id, $old_meta_key, true ); |
| 259 | $new_meta_key = self::get_staged_meta_name( KIRKI_GLOBAL_STYLE_BLOCK_META_KEY, $post_id, $new_version ); |
| 260 | update_post_meta( $post_id, $new_meta_key, $old_data ); |
| 261 | |
| 262 | $old_meta_key = self::get_staged_meta_name( KIRKI_META_NAME_FOR_USED_STYLE_BLOCK_IDS, $post_id, $old_version_id ); |
| 263 | $old_data = get_post_meta( $post_id, $old_meta_key, true ); |
| 264 | $new_meta_key = self::get_staged_meta_name( KIRKI_META_NAME_FOR_USED_STYLE_BLOCK_IDS, $post_id, $new_version ); |
| 265 | update_post_meta( $post_id, $new_meta_key, $old_data ); |
| 266 | |
| 267 | $old_meta_key = self::get_staged_meta_name( KIRKI_META_NAME_FOR_USED_STYLE_BLOCK_IDS . '_random', $post_id, $old_version_id ); |
| 268 | $old_data = get_post_meta( $post_id, $old_meta_key, true ); |
| 269 | $new_meta_key = self::get_staged_meta_name( KIRKI_META_NAME_FOR_USED_STYLE_BLOCK_IDS . '_random', $post_id, $new_version ); |
| 270 | update_post_meta( $post_id, $new_meta_key, $old_data ); |
| 271 | |
| 272 | $old_meta_key = self::get_staged_meta_name( KIRKI_META_NAME_FOR_USED_FONT_LIST, $post_id, $old_version_id ); |
| 273 | $old_data = get_post_meta( $post_id, $old_meta_key, true ); |
| 274 | $new_meta_key = self::get_staged_meta_name( KIRKI_META_NAME_FOR_USED_FONT_LIST, $post_id, $new_version ); |
| 275 | update_post_meta( $post_id, $new_meta_key, $old_data ); |
| 276 | |
| 277 | $old_meta_key = self::get_staged_meta_name( 'kirki', $post_id, $old_version_id ); |
| 278 | $old_data = get_post_meta( $post_id, $old_meta_key, true ); |
| 279 | $new_meta_key = self::get_staged_meta_name( 'kirki', $post_id, $new_version ); |
| 280 | update_post_meta( $post_id, $new_meta_key, $old_data ); |
| 281 | |
| 282 | wp_send_json( array_merge( array( 'versions' => self::get_all_staged_versions( $post_id, true, true ) ), array( 'new_version' => $new_version ) ) ); |
| 283 | } |
| 284 | |
| 285 | public static function rename_stage_version() { |
| 286 | $post_id = HelperFunctions::sanitize_text( isset( $_POST['post_id'] ) ? $_POST['post_id'] : null ); |
| 287 | $version_id = HelperFunctions::sanitize_text( isset( $_POST['version_id'] ) ? $_POST['version_id'] : null ); |
| 288 | $name = HelperFunctions::sanitize_text( isset( $_POST['name'] ) ? $_POST['name'] : null ); |
| 289 | |
| 290 | if ( ! $post_id || ! $version_id || ! $name ) { |
| 291 | wp_send_json( false, 400 ); |
| 292 | } |
| 293 | |
| 294 | $staged_versions = self::get_all_staged_versions( $post_id, true, true ); |
| 295 | $staged_versions = array_map( |
| 296 | function ( $item ) use ( $version_id, $name ) { |
| 297 | return ( is_array( $item ) && isset( $item['version'] ) && intval( $item['version'] ) === intval( $version_id ) ) |
| 298 | ? array_merge( $item, array( 'name' => $name ) ) |
| 299 | : $item; |
| 300 | }, |
| 301 | $staged_versions |
| 302 | ); |
| 303 | |
| 304 | update_post_meta( $post_id, KIRKI_META_NAME_FOR_STAGED_VERSIONS, $staged_versions ); |
| 305 | wp_send_json( $staged_versions ); |
| 306 | } |
| 307 | |
| 308 | public static function delete_stage_version() { |
| 309 | $post_id = HelperFunctions::sanitize_text( isset( $_POST['post_id'] ) ? $_POST['post_id'] : null ); |
| 310 | $version_id = HelperFunctions::sanitize_text( isset( $_POST['version_id'] ) ? $_POST['version_id'] : null ); |
| 311 | |
| 312 | if ( ! $post_id || ! $version_id ) { |
| 313 | wp_send_json( false, 400 ); |
| 314 | } |
| 315 | // Delete stage meta data |
| 316 | $meta_key = self::get_staged_meta_name( KIRKI_GLOBAL_STYLE_BLOCK_META_KEY . '_random', $post_id, $version_id, true ); |
| 317 | delete_post_meta( $post_id, $meta_key ); |
| 318 | |
| 319 | $meta_key = self::get_staged_meta_name( KIRKI_GLOBAL_STYLE_BLOCK_META_KEY, $post_id, $version_id, true ); |
| 320 | delete_post_meta( $post_id, $meta_key ); |
| 321 | |
| 322 | $meta_key = self::get_staged_meta_name( KIRKI_META_NAME_FOR_USED_STYLE_BLOCK_IDS, $post_id, $version_id, true ); |
| 323 | delete_post_meta( $post_id, $meta_key ); |
| 324 | |
| 325 | $meta_key = self::get_staged_meta_name( KIRKI_META_NAME_FOR_USED_STYLE_BLOCK_IDS . '_random', $post_id, $version_id, true ); |
| 326 | delete_post_meta( $post_id, $meta_key ); |
| 327 | |
| 328 | $meta_key = self::get_staged_meta_name( KIRKI_META_NAME_FOR_USED_FONT_LIST, $post_id, $version_id, true ); |
| 329 | delete_post_meta( $post_id, $meta_key ); |
| 330 | |
| 331 | $meta_key = self::get_staged_meta_name( 'kirki', $post_id, $version_id, true ); |
| 332 | delete_post_meta( $post_id, $meta_key ); |
| 333 | |
| 334 | // delete the version from stage version list |
| 335 | $staged_versions = self::get_all_staged_versions( $post_id, true, true ); |
| 336 | $staged_versions = array_values( |
| 337 | array_filter( |
| 338 | $staged_versions, |
| 339 | function ( $item ) use ( $version_id ) { |
| 340 | return ! ( is_array( $item ) && ! $item['publish'] && intval( $item['version'] ) === intval( $version_id ) ); |
| 341 | } |
| 342 | ) |
| 343 | ); |
| 344 | update_post_meta( $post_id, KIRKI_META_NAME_FOR_STAGED_VERSIONS, $staged_versions ); |
| 345 | |
| 346 | wp_send_json( $staged_versions ); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * @deprecated |
| 351 | * @see Kirki\App\Managers\PageManager::get_staged_meta_name() |
| 352 | */ |
| 353 | public static function get_staged_meta_name( $meta_name, $post_id, $stage_version = false, $stage_only = false ) { |
| 354 | $version = $stage_version; |
| 355 | if ( ! $version ) { |
| 356 | $version = self::get_most_recent_stage_version( $post_id, false ); |
| 357 | } elseif ( ! $stage_only ) { |
| 358 | $published_version = self::get_published_stage_version( $post_id ); |
| 359 | if ( $published_version && $version == $published_version ) { |
| 360 | return $meta_name; |
| 361 | } |
| 362 | } |
| 363 | return 'staged_' . $version . '_' . $meta_name; |
| 364 | } |
| 365 | |
| 366 | public static function get_all_stage_related_meta_names( $post_id ) { |
| 367 | $versions = get_post_meta( $post_id, KIRKI_META_NAME_FOR_STAGED_VERSIONS, true ); |
| 368 | if ( ! $versions ) { |
| 369 | return array(); |
| 370 | } |
| 371 | $meta_names = array( KIRKI_META_NAME_FOR_STAGED_VERSIONS ); |
| 372 | foreach ( $versions as $version ) { |
| 373 | $version_id = $version['version']; |
| 374 | $meta_key = self::get_staged_meta_name( KIRKI_GLOBAL_STYLE_BLOCK_META_KEY . '_random', $post_id, $version_id, true ); |
| 375 | $meta_names[] = $meta_key; |
| 376 | $meta_key = self::get_staged_meta_name( KIRKI_GLOBAL_STYLE_BLOCK_META_KEY, $post_id, $version_id, true ); |
| 377 | $meta_names[] = $meta_key; |
| 378 | $meta_key = self::get_staged_meta_name( KIRKI_META_NAME_FOR_USED_STYLE_BLOCK_IDS, $post_id, $version_id, true ); |
| 379 | $meta_names[] = $meta_key; |
| 380 | $meta_key = self::get_staged_meta_name( KIRKI_META_NAME_FOR_USED_STYLE_BLOCK_IDS . '_random', $post_id, $version_id, true ); |
| 381 | $meta_names[] = $meta_key; |
| 382 | $meta_key = self::get_staged_meta_name( KIRKI_META_NAME_FOR_USED_FONT_LIST, $post_id, $version_id, true ); |
| 383 | $meta_names[] = $meta_key; |
| 384 | $meta_key = self::get_staged_meta_name( 'kirki', $post_id, $version_id, true ); |
| 385 | $meta_names[] = $meta_key; |
| 386 | } |
| 387 | return $meta_names; |
| 388 | } |
| 389 | |
| 390 | private static function add_editor_names_into_stage_versions( $versions ) { |
| 391 | $versions = array_map( |
| 392 | function ( $item ) { |
| 393 | $user = get_user_by( 'id', $item['edited_by'] ); |
| 394 | if ( ! $user ) { |
| 395 | return $item; |
| 396 | } |
| 397 | return array_merge( $item, array( 'edited_by' => $user->data->display_name ) ); |
| 398 | }, |
| 399 | $versions |
| 400 | ); |
| 401 | return $versions; |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * @deprecated |
| 406 | * @see Kirki\App\Managers\PageManager::create_first_stage_version() |
| 407 | */ |
| 408 | private static function create_first_stage_version( $post_id ) { |
| 409 | $new_version = self::add_stage_version( $post_id, 1 ); |
| 410 | |
| 411 | $old_meta_key = KIRKI_GLOBAL_STYLE_BLOCK_META_KEY . '_random'; |
| 412 | $old_data = get_post_meta( $post_id, $old_meta_key, true ); |
| 413 | $new_meta_key = self::get_staged_meta_name( KIRKI_GLOBAL_STYLE_BLOCK_META_KEY . '_random', $post_id, $new_version ); |
| 414 | update_post_meta( $post_id, $new_meta_key, $old_data ); |
| 415 | |
| 416 | $new_meta_key = self::get_staged_meta_name( KIRKI_GLOBAL_STYLE_BLOCK_META_KEY, $post_id, $new_version ); |
| 417 | update_post_meta( $post_id, $new_meta_key, array() ); |
| 418 | |
| 419 | $old_meta_key = KIRKI_META_NAME_FOR_USED_STYLE_BLOCK_IDS; |
| 420 | $old_data = get_post_meta( $post_id, $old_meta_key, true ); |
| 421 | $new_meta_key = self::get_staged_meta_name( KIRKI_META_NAME_FOR_USED_STYLE_BLOCK_IDS, $post_id, $new_version ); |
| 422 | update_post_meta( $post_id, $new_meta_key, $old_data ); |
| 423 | |
| 424 | $old_meta_key = KIRKI_META_NAME_FOR_USED_STYLE_BLOCK_IDS . '_random'; |
| 425 | $old_data = get_post_meta( $post_id, $old_meta_key, true ); |
| 426 | $new_meta_key = self::get_staged_meta_name( KIRKI_META_NAME_FOR_USED_STYLE_BLOCK_IDS . '_random', $post_id, $new_version ); |
| 427 | update_post_meta( $post_id, $new_meta_key, $old_data ); |
| 428 | |
| 429 | $old_meta_key = KIRKI_META_NAME_FOR_USED_FONT_LIST; |
| 430 | $old_data = get_post_meta( $post_id, $old_meta_key, true ); |
| 431 | $new_meta_key = self::get_staged_meta_name( KIRKI_META_NAME_FOR_USED_FONT_LIST, $post_id, $new_version ); |
| 432 | update_post_meta( $post_id, $new_meta_key, $old_data ); |
| 433 | |
| 434 | $old_meta_key = 'kirki'; |
| 435 | $old_data = get_post_meta( $post_id, $old_meta_key, true ); |
| 436 | $new_meta_key = self::get_staged_meta_name( 'kirki', $post_id, $new_version ); |
| 437 | update_post_meta( $post_id, $new_meta_key, $old_data ); |
| 438 | |
| 439 | return self::publish_stage_version( true, $post_id ); |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * @deprecated |
| 444 | * @see Kirki\App\Managers\PageManager::set_last_edited_datetime_of_stage_version() |
| 445 | */ |
| 446 | private static function update_last_edited_datetime_of_stage_version( $post_id ) { |
| 447 | $staged_versions = self::get_all_staged_versions( $post_id, true, true ); |
| 448 | if ( count( $staged_versions ) === 0 ) { |
| 449 | return; |
| 450 | } |
| 451 | |
| 452 | $datetime = new \DateTime( 'now', wp_timezone() ); |
| 453 | $datetime = $datetime->format( 'Y-m-d H:i:s' ); |
| 454 | $staged_versions[ count( $staged_versions ) - 1 ] = array_merge( $staged_versions[ count( $staged_versions ) - 1 ], array( 'last_updated' => $datetime ) ); |
| 455 | update_post_meta( $post_id, KIRKI_META_NAME_FOR_STAGED_VERSIONS, $staged_versions ); |
| 456 | return $staged_versions; |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * @deprecated |
| 461 | * @see Kirki\App\Managers\PageManager::add_stage_version() |
| 462 | */ |
| 463 | private static function add_stage_version( $post_id, $version_number, $prev_versions = array(), $being_restored = false ) { |
| 464 | $format = 'F j g:i A'; |
| 465 | $datetime = new \DateTime( 'now', wp_timezone() ); |
| 466 | $date = $datetime->format( $format ); |
| 467 | $datetime = $datetime->format( 'Y-m-d H:i:s' ); |
| 468 | |
| 469 | $new_version = array( |
| 470 | 'version' => $version_number, |
| 471 | 'edited_by' => get_current_user_id(), |
| 472 | 'created_on' => $datetime, |
| 473 | 'last_updated' => $datetime, |
| 474 | 'name' => $being_restored ? '[Restored] ' . $being_restored['name'] : $date, |
| 475 | 'publish' => false, |
| 476 | ); |
| 477 | $prev_versions[] = $new_version; |
| 478 | update_post_meta( $post_id, KIRKI_META_NAME_FOR_STAGED_VERSIONS, $prev_versions ); |
| 479 | return $version_number; |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * @deprecated |
| 484 | * @see Kirki\App\Managers\PageManager::get_most_recent_unpublished_stage_version() |
| 485 | */ |
| 486 | public static function get_most_recent_unpublished_stage_id( $post_id ) { |
| 487 | $staged_versions = get_post_meta( $post_id, 'kirki_stage_versions', true ) ?: array(); |
| 488 | $most_recent_stage_id = false; |
| 489 | |
| 490 | foreach ( $staged_versions as $version ) { |
| 491 | if ( ! empty( $version['publish'] ) ) { |
| 492 | continue; // skip published versions |
| 493 | } |
| 494 | $most_recent_stage_id = $version['version']; |
| 495 | } |
| 496 | |
| 497 | return $most_recent_stage_id; |
| 498 | } |
| 499 | } |