1 | <?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase |
---|
2 | /** |
---|
3 | * This file contains some 'remember' functions inspired by the core Classic Editor Plugin |
---|
4 | * Used to align the 'last editor' metadata so that it is set on all Jetpack and WPCOM sites |
---|
5 | * |
---|
6 | * @package automattic/jetpack |
---|
7 | */ |
---|
8 | |
---|
9 | namespace Jetpack\EditorType; |
---|
10 | |
---|
11 | /** |
---|
12 | * Remember when the classic editor was used to edit a post. |
---|
13 | * |
---|
14 | * @param object $post The post being editted. |
---|
15 | */ |
---|
16 | function remember_classic_editor( $post ) { |
---|
17 | $post_type = get_post_type( $post ); |
---|
18 | |
---|
19 | if ( $post_type && post_type_supports( $post_type, 'editor' ) ) { |
---|
20 | remember_editor( $post->ID, 'classic-editor' ); |
---|
21 | } |
---|
22 | } |
---|
23 | |
---|
24 | /** |
---|
25 | * Remember when the block editor was used to edit a post. |
---|
26 | * |
---|
27 | * @param array $editor_settings This is hooked into a filter and this is the settings that are passed in. |
---|
28 | * @param WP_Block_Editor_Context $post The block editor context. |
---|
29 | * |
---|
30 | * @return array The unmodified $editor_settings parameter. |
---|
31 | */ |
---|
32 | function remember_block_editor( $editor_settings, $post ) { |
---|
33 | if ( ! empty( $post->post ) ) { |
---|
34 | $post = $post->post; |
---|
35 | } |
---|
36 | |
---|
37 | if ( empty( $post ) ) { |
---|
38 | return $editor_settings; |
---|
39 | } |
---|
40 | |
---|
41 | $post_type = get_post_type( $post ); |
---|
42 | if ( $post_type && can_edit_post_type( $post_type ) ) { |
---|
43 | remember_editor( $post->ID, 'block-editor' ); |
---|
44 | } |
---|
45 | |
---|
46 | return $editor_settings; |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | * Sets the metadata for the specified post and editor. |
---|
51 | * |
---|
52 | * @param int $post_id The ID of the post to set the metadata for. |
---|
53 | * @param string $editor String name of the editor, 'classic-editor' or 'block-editor'. |
---|
54 | */ |
---|
55 | function remember_editor( $post_id, $editor ) { |
---|
56 | if ( get_post_meta( $post_id, '_last_editor_used_jetpack', true ) !== $editor ) { |
---|
57 | update_post_meta( $post_id, '_last_editor_used_jetpack', $editor ); |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * Checks whether the block editor can be used with the given post type. |
---|
63 | * |
---|
64 | * @param string $post_type The post type to check. |
---|
65 | * @return bool Whether the block editor can be used to edit the supplied post type. |
---|
66 | */ |
---|
67 | function can_edit_post_type( $post_type ) { |
---|
68 | $can_edit = false; |
---|
69 | |
---|
70 | if ( function_exists( 'gutenberg_can_edit_post_type' ) ) { |
---|
71 | $can_edit = gutenberg_can_edit_post_type( $post_type ); |
---|
72 | } elseif ( function_exists( 'use_block_editor_for_post_type' ) ) { |
---|
73 | $can_edit = use_block_editor_for_post_type( $post_type ); |
---|
74 | } |
---|
75 | |
---|
76 | return $can_edit; |
---|
77 | } |
---|