| @@ -21,10 +21,9 @@ | ||
| 21 | 21 | parent::__construct( $polylang ); |
| 22 | 22 | |
| 23 | 23 | add_filter( 'wp_insert_post_parent', array( $this, 'wp_insert_post_parent' ), 10, 3 ); |
| 24 | 24 | add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ) ); |
| 25 | - add_action( 'rest_api_init', array( $this, 'new_post_translation' ) ); // Block editor | |
| 26 | - add_action( 'add_meta_boxes', array( $this, 'new_post_translation' ), 5 ); // Classic editor, before Types which populates custom fields in same hook with priority 10 | |
| 25 | + add_filter( 'use_block_editor_for_post', array( $this, 'new_post_translation' ), 5000 ); // After content duplication. | |
| 27 | 26 | } |
| 28 | 27 | |
| 29 | 28 | /** |
| 30 | 29 | * Translate post parent if exists when using "Add new" ( translation ) |
| @@ -81,16 +80,18 @@ | ||
| 81 | 80 | /** |
| 82 | 81 | * Copy post metas, and taxonomies when using "Add new" ( translation ) |
| 83 | 82 | * |
| 84 | 83 | * @since 2.5 |
| 84 | + * @since 3.1 Use of use_block_editor_for_post filter instead of rest_api_init which is triggered too early in WP 5.8. | |
| 85 | 85 | * |
| 86 | - * @return void | |
| 86 | + * @param bool $is_block_editor Whether the post can be edited or not. | |
| 87 | + * @return bool | |
| 87 | 88 | */ |
| 88 | - public function new_post_translation() { | |
| 89 | + public function new_post_translation( $is_block_editor ) { | |
| 89 | 90 | global $post; |
| 90 | 91 | static $done = array(); |
| 91 | 92 | |
| 92 | - if ( isset( $GLOBALS['pagenow'], $_GET['from_post'], $_GET['new_lang'] ) && 'post-new.php' === $GLOBALS['pagenow'] && $this->model->is_translated_post_type( $post->post_type ) ) { | |
| 93 | + if ( ! empty( $post ) && isset( $GLOBALS['pagenow'], $_GET['from_post'], $_GET['new_lang'] ) && 'post-new.php' === $GLOBALS['pagenow'] && $this->model->is_translated_post_type( $post->post_type ) ) { | |
| 93 | 94 | check_admin_referer( 'new-post-translation' ); |
| 94 | 95 | |
| 95 | 96 | // Capability check already done in post-new.php |
| 96 | 97 | $from_post_id = (int) $_GET['from_post']; |
| @@ -96,9 +97,9 @@ | ||
| 96 | 97 | $from_post_id = (int) $_GET['from_post']; |
| 97 | 98 | $lang = $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ); |
| 98 | 99 | |
| 99 | 100 | if ( ! $from_post_id || ! $lang || ! empty( $done[ $from_post_id ] ) ) { |
| 100 | - return; | |
| 101 | + return $is_block_editor; | |
| 101 | 102 | } |
| 102 | 103 | |
| 103 | 104 | $done[ $from_post_id ] = true; // Avoid a second duplication in the block editor. Using an array only to allow multiple phpunit tests. |
| 104 | 105 | |
| @@ -108,8 +109,10 @@ | ||
| 108 | 109 | if ( is_sticky( $from_post_id ) ) { |
| 109 | 110 | stick_post( $post->ID ); |
| 110 | 111 | } |
| 111 | 112 | } |
| 113 | + | |
| 114 | + return $is_block_editor; | |
| 112 | 115 | } |
| 113 | 116 | |
| 114 | 117 | /** |
| 115 | 118 | * Get post fields to synchronize. |