'string', ); } return $args; } add_filter( 'register_block_type_args', 'block_rewind_register_block_id_attribute' ); /** * Checks whether the current admin screen is the post editor. * * Block editor assets also load in the Site Editor, Widgets screen and * Customizer, which the history runtime does not support. Shared site content * (templates, patterns, menus) is left out in the post editor too. * * @return bool */ function block_rewind_is_post_editor_screen() { if ( ! function_exists( 'get_current_screen' ) ) { return false; } $screen = get_current_screen(); return $screen && 'post' === $screen->base && $screen->is_block_editor() && ! in_array( $screen->post_type, array( 'wp_template', 'wp_template_part', 'wp_block', 'wp_navigation' ), true ); } /** * Returns the settings passed to the editor runtime. * * @return array */ function block_rewind_get_editor_settings() { /** * Filters how long block history is kept in the browser, in seconds. * * @param int $ttl Retention in seconds. Default 4 hours, minimum 5 minutes. */ $snapshot_ttl = (int) apply_filters( 'block_rewind_snapshot_ttl', 4 * HOUR_IN_SECONDS ); /** * Filters the maximum number of snapshots kept per block; the oldest are removed first. * * @param int $max_snapshots Snapshots per block. Default 100, minimum 5. */ $max_snapshots = (int) apply_filters( 'block_rewind_max_snapshots_per_block', 100 ); return array( // History is stored per site and per user, as browsers share storage per origin. 'storageKey' => substr( md5( home_url() ), 0, 12 ) . '-' . get_current_user_id(), 'snapshotTtl' => max( 5 * MINUTE_IN_SECONDS, $snapshot_ttl ), 'maxSnapshotsPerBlock' => max( 5, $max_snapshots ), ); } /** * Enqueue editor assets for Gutenberg. */ function block_rewind_enqueue_block_editor_assets() { if ( ! block_rewind_is_post_editor_screen() ) { return; } $asset_path = plugin_dir_path( __FILE__ ) . 'build/index.asset.php'; if ( ! file_exists( $asset_path ) ) { return; } $asset = include $asset_path; wp_enqueue_script( 'block-rewind-editor', plugin_dir_url( __FILE__ ) . 'build/index.js', $asset['dependencies'], $asset['version'], true ); wp_set_script_translations( 'block-rewind-editor', 'block-rewind' ); wp_add_inline_script( 'block-rewind-editor', 'window.blockRewindSettings = ' . wp_json_encode( block_rewind_get_editor_settings() ) . ';', 'before' ); wp_enqueue_style( 'block-rewind-editor', plugin_dir_url( __FILE__ ) . 'build/index.css', array( 'wp-components' ), $asset['version'] ); wp_style_add_data( 'block-rewind-editor', 'rtl', 'replace' ); } add_action( 'enqueue_block_editor_assets', 'block_rewind_enqueue_block_editor_assets' );