autosaves-preview.php
4 years ago
index.php
4 years ago
menu-preview.php
4 years ago
site-data-preview.php
4 years ago
index.php
253 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | use IlluminateAgnostic\Arr\Support\Arr; |
| 5 | |
| 6 | |
| 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | exit; |
| 9 | } |
| 10 | |
| 11 | /** |
| 12 | * @param $uuid |
| 13 | * |
| 14 | * @return array|null |
| 15 | */ |
| 16 | function kubio_get_changeset_by_uuid( $uuid ) { |
| 17 | if ( ! empty( $uuid ) ) { |
| 18 | |
| 19 | if ( $cached = wp_cache_get( "{$uuid}-data", 'kubio/preview' ) ) { |
| 20 | return $cached; |
| 21 | } |
| 22 | |
| 23 | $args = array( |
| 24 | 'name' => $uuid, |
| 25 | 'post_type' => 'kubio_changeset', |
| 26 | 'post_status' => array( 'publish', 'draft' ), |
| 27 | ); |
| 28 | |
| 29 | $posts = get_posts( $args ); |
| 30 | |
| 31 | if ( ! empty( $posts ) ) { |
| 32 | /** @var WP_Post $my_posts */ |
| 33 | $changeset = $posts[0]; |
| 34 | |
| 35 | $content = json_decode( $changeset->post_content, true ); |
| 36 | wp_cache_set( "{$uuid}-data", $content, 'kubio/preview' ); |
| 37 | |
| 38 | return $content; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return null; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | function kubio_get_current_changeset_data( $path = '', $fallback = null ) { |
| 47 | static $chanset_data; |
| 48 | |
| 49 | if ( ! $chanset_data ) { |
| 50 | $uuid = sanitize_text_field( Arr::get( $_REQUEST, 'kubio-preview', false ) ); |
| 51 | $chanset_data = kubio_get_changeset_by_uuid( $uuid ); |
| 52 | } |
| 53 | |
| 54 | if ( empty( $path ) ) { |
| 55 | return $chanset_data; |
| 56 | } |
| 57 | |
| 58 | return Arr::get( $chanset_data, $path, $fallback ); |
| 59 | |
| 60 | } |
| 61 | |
| 62 | function kubio_prepare_changest_post() { |
| 63 | |
| 64 | if ( $cached = wp_cache_get( 'uuid', 'kubio/preview' ) ) { |
| 65 | return $cached; |
| 66 | } |
| 67 | |
| 68 | $post_type = 'kubio_changeset'; |
| 69 | $uuid = wp_generate_uuid4(); |
| 70 | |
| 71 | // make a bit of a cleanup first |
| 72 | $allposts = get_posts( |
| 73 | array( |
| 74 | 'post_type' => $post_type, |
| 75 | 'numberposts' => - 1, |
| 76 | 'date_query' => array( |
| 77 | 'column' => 'post_date', |
| 78 | 'before' => '- 1 day', |
| 79 | ), |
| 80 | ) |
| 81 | ); |
| 82 | |
| 83 | foreach ( $allposts as $eachpost ) { |
| 84 | wp_delete_post( $eachpost->ID, true ); |
| 85 | } |
| 86 | |
| 87 | wp_insert_post( |
| 88 | array( |
| 89 | 'post_content' => '', // remove the preery prints |
| 90 | 'post_status' => 'publish', |
| 91 | 'post_type' => $post_type, |
| 92 | 'post_name' => $uuid, |
| 93 | 'post_title' => $uuid, |
| 94 | 'guid' => uniqid( "$post_type-" . time() . '-' ), |
| 95 | ), |
| 96 | false |
| 97 | ); |
| 98 | |
| 99 | wp_cache_set( 'uuid', $uuid, 'kubio/preview' ); |
| 100 | wp_cache_set( "{$uuid}-data", array(), 'kubio/preview' ); |
| 101 | |
| 102 | return $uuid; |
| 103 | } |
| 104 | |
| 105 | function kubio_register_preivew_data_post() { |
| 106 | |
| 107 | $args = array( |
| 108 | 'label' => __( 'Kubio Preview', 'kubio' ), |
| 109 | 'public' => false, |
| 110 | 'show_ui' => false, |
| 111 | 'show_in_rest' => true, |
| 112 | 'rest_base' => 'kubio/preview-changeset', |
| 113 | 'hierarchical' => false, |
| 114 | 'rewrite' => false, |
| 115 | 'query_var' => false, |
| 116 | 'can_export' => false, |
| 117 | 'delete_with_user' => false, |
| 118 | 'capabilities' => array( |
| 119 | 'read' => 'edit_theme_options', |
| 120 | 'create_posts' => 'edit_theme_options', |
| 121 | 'edit_posts' => 'edit_theme_options', |
| 122 | 'edit_published_posts' => 'edit_theme_options', |
| 123 | 'delete_published_posts' => 'edit_theme_options', |
| 124 | 'edit_others_posts' => 'edit_theme_options', |
| 125 | 'delete_others_posts' => 'edit_theme_options', |
| 126 | ), |
| 127 | 'map_meta_cap' => true, |
| 128 | 'supports' => array( |
| 129 | 'title', |
| 130 | 'author', |
| 131 | 'editor', |
| 132 | ), |
| 133 | ); |
| 134 | register_post_type( 'kubio_changeset', $args ); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | add_action( 'init', 'kubio_register_preivew_data_post' ); |
| 139 | |
| 140 | add_filter( |
| 141 | 'kubio/block_editor_settings', |
| 142 | function ( $settings ) { |
| 143 | $settings['changeset_uuid'] = kubio_prepare_changest_post(); |
| 144 | |
| 145 | return $settings; |
| 146 | } |
| 147 | ); |
| 148 | |
| 149 | |
| 150 | // delete changeset when browser refreshes |
| 151 | add_action( |
| 152 | 'wp_ajax_kubio-delete-changeset', |
| 153 | function () { |
| 154 | $uuid = sanitize_text_field( Arr::get( $_REQUEST, 'uuid', false ) ); |
| 155 | |
| 156 | $changeset = kubio_get_changeset_by_uuid( $uuid ); |
| 157 | |
| 158 | if ( $changeset && intval( $changeset->post_author ) === get_current_user_id() ) { |
| 159 | wp_delete_post( $changeset->ID, true ); |
| 160 | } |
| 161 | |
| 162 | } |
| 163 | ); |
| 164 | |
| 165 | function kubio_get_template_part_id_by_slug( $slug, $theme ) { |
| 166 | $stylesheet = get_stylesheet(); |
| 167 | $template_part_query = new WP_Query( |
| 168 | array( |
| 169 | 'post_type' => 'wp_template_part', |
| 170 | 'post_status' => array( 'publish' ), |
| 171 | 'post_name__in' => array( $slug ), |
| 172 | 'posts_per_page' => 1, |
| 173 | 'no_found_rows' => true, |
| 174 | 'tax_query' => array( |
| 175 | array( |
| 176 | 'taxonomy' => 'wp_theme', |
| 177 | 'field' => 'name', |
| 178 | 'terms' => array( $stylesheet ), |
| 179 | ), |
| 180 | ), |
| 181 | ) |
| 182 | ); |
| 183 | |
| 184 | return $template_part_query->have_posts() ? $template_part_query->next_post()->ID : null; |
| 185 | } |
| 186 | |
| 187 | function kubio_get_template_part_block_id( $block ) { |
| 188 | $id = Arr::get( $block, 'attrs.postId', 0 ); |
| 189 | $slug = Arr::get( $block, 'attrs.slug', '' ); |
| 190 | $theme = Arr::get( $block, 'attrs.theme', '' ); |
| 191 | |
| 192 | if ( $id ) { |
| 193 | return $id; |
| 194 | } |
| 195 | |
| 196 | return kubio_get_template_part_id_by_slug( $slug, $theme ); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | require_once __DIR__ . '/autosaves-preview.php'; |
| 201 | require_once __DIR__ . '/menu-preview.php'; |
| 202 | require_once __DIR__ . '/site-data-preview.php'; |
| 203 | |
| 204 | |
| 205 | function kubio_is_page_preview() { |
| 206 | return Arr::has( $_REQUEST, 'kubio-preview', false ); |
| 207 | } |
| 208 | |
| 209 | add_action( |
| 210 | 'init', |
| 211 | function () { |
| 212 | $uuid = sanitize_text_field( Arr::get( $_REQUEST, 'kubio-preview', false ) ); |
| 213 | |
| 214 | if ( ! empty( $uuid ) ) { |
| 215 | |
| 216 | $changeset_data = kubio_get_current_changeset_data(); |
| 217 | |
| 218 | if ( empty( $changeset_data ) ) { |
| 219 | wp_die( esc_html__( 'Current preview state is unavailable', 'kubio' ) ); |
| 220 | } |
| 221 | |
| 222 | show_admin_bar( false ); |
| 223 | kubio_handle_autosaved_posts_and_templates(); |
| 224 | |
| 225 | $custom_entities = kubio_get_current_changeset_data( 'customEntities', array() ); |
| 226 | $custom_data = (array) kubio_get_current_changeset_data( 'customData', array() ); |
| 227 | |
| 228 | foreach ( $custom_entities as $item ) { |
| 229 | do_action( 'kubio/preview/handle_custom_entities', $item ); |
| 230 | } |
| 231 | |
| 232 | do_action( 'kubio/preview/handle_custom_data', $custom_data ); |
| 233 | |
| 234 | add_action( 'wp_enqueue_scripts', 'kubio_enqueue_preview_url_maintainer' ); |
| 235 | } else { |
| 236 | if ( isset( $_REQUEST['kubio-random'] ) && ! ( is_admin() || is_customize_preview() ) ) { |
| 237 | add_action( 'wp_enqueue_scripts', 'kubio_enqueue_preview_url_maintainer' ); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | } |
| 242 | ); |
| 243 | |
| 244 | |
| 245 | function kubio_enqueue_preview_url_maintainer() { |
| 246 | if ( is_user_logged_in() ) { |
| 247 | wp_enqueue_script( 'kubio-maintain-preview-url', kubio_url( '/static/maintain-preview-url.js' ), array( 'wp-url' ), KUBIO_VERSION, true ); |
| 248 | wp_add_inline_script( 'kubio-maintain-preview-url', 'kubioMaintainPreviewURLBase="' . site_url() . '"', 'before' ); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | |
| 253 |