multilanguage
1 year ago
recommendations
6 days ago
colibri.php
1 year ago
contact-form.php
1 year ago
entity.php
4 years ago
get-body-class.php
1 year ago
get-classic-page-template.php
1 year ago
get-page-query.php
1 year ago
get-page-title.php
1 year ago
get-post-content.php
1 year ago
get-post-styles.php
1 year ago
index.php
11 months ago
kubio-cloud.php
1 year ago
page-templates.php
4 years ago
save-template-parts-filter.php
1 year ago
subscribe-form.php
4 years ago
typekit.php
4 years ago
update-settings-flags.php
1 year ago
save-template-parts-filter.php
71 lines
| 1 | <?php |
| 2 | |
| 3 | use Kubio\Core\Importer; |
| 4 | |
| 5 | function kubio_template_autosave( $post_data, $type = 'preview' ) { |
| 6 | |
| 7 | $id = $post_data->wp_id ? $post_data->wp_id : $post_data->id; |
| 8 | |
| 9 | //TODO make a query that gets the autosave with the correct type so there are not made multiple autosaves for the same |
| 10 | //post and type |
| 11 | $old_autosave = wp_get_post_autosave( $id ); |
| 12 | $same_autosave_type = true; |
| 13 | $autosave_meta_key = 'kubio_template_autosave_type'; |
| 14 | if ( $old_autosave ) { |
| 15 | $autosave_type = get_post_meta( $id, $autosave_meta_key, true ); |
| 16 | |
| 17 | //no meta is present or if the meta is present is the same type |
| 18 | $same_autosave_type = ! $autosave_type || ( $autosave_type && $autosave_type === $type ); |
| 19 | } |
| 20 | if ( $old_autosave && $same_autosave_type ) { |
| 21 | wp_update_post( |
| 22 | array( |
| 23 | 'ID' => $old_autosave->ID, |
| 24 | 'post_content' => wp_slash( $post_data->content ), |
| 25 | ) |
| 26 | ); |
| 27 | update_post_meta( $old_autosave->ID, $autosave_meta_key, $type ); |
| 28 | return $old_autosave; |
| 29 | } else { |
| 30 | |
| 31 | // if possible save the post with initial value and then create the autosave |
| 32 | if ( ! is_numeric( $id ) && property_exists( $post_data, 'slug' ) && property_exists( $post_data, 'content' ) ) { |
| 33 | $id = Importer::createTemplate( $post_data->slug, $post_data->content, false ); |
| 34 | } |
| 35 | |
| 36 | $new_data = get_post( $id ); |
| 37 | $new_data->post_content = $post_data->content; |
| 38 | |
| 39 | $autosave_id = _wp_put_post_revision( $new_data, true ); |
| 40 | |
| 41 | if ( ! is_wp_error( $autosave_id ) ) { |
| 42 | update_post_meta( $autosave_id, $autosave_meta_key, $type ); |
| 43 | return get_post( $autosave_id ); |
| 44 | } |
| 45 | |
| 46 | return $autosave_id; |
| 47 | } |
| 48 | |
| 49 | return $post_data; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | register_post_meta( |
| 54 | 'revision', |
| 55 | 'kubio_template_autosave_type', |
| 56 | array( |
| 57 | 'show_in_rest' => true, |
| 58 | 'single' => true, |
| 59 | 'type' => 'string', |
| 60 | 'auth_callback' => function () { |
| 61 | return current_user_can( 'edit_posts' ); |
| 62 | }, |
| 63 | ) |
| 64 | ); |
| 65 | |
| 66 | |
| 67 | |
| 68 | add_filter( 'kubio/save-template-entity/page/autosave', 'kubio_template_autosave', 10, 2 ); |
| 69 | add_filter( 'kubio/save-template-entity/wp_template/autosave', 'kubio_template_autosave', 10, 2 ); |
| 70 | add_filter( 'kubio/save-template-entity/wp_template_part/autosave', 'kubio_template_autosave', 10, 2 ); |
| 71 |