| @@ -1,5 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * @package Polylang | |
| 4 | + */ | |
| 2 | 5 | |
| 3 | 6 | /** |
| 4 | 7 | * Manages copy and synchronization of terms and post metas |
| 5 | 8 | * |
| @@ -11,9 +14,9 @@ | ||
| 11 | 14 | * Constructor |
| 12 | 15 | * |
| 13 | 16 | * @since 1.2 |
| 14 | 17 | * |
| 15 | - * @param object $polylang | |
| 18 | + * @param object $polylang The Polylang object. | |
| 16 | 19 | */ |
| 17 | 20 | public function __construct( &$polylang ) { |
| 18 | 21 | parent::__construct( $polylang ); |
| 19 | 22 | |
| @@ -18,10 +21,9 @@ | ||
| 18 | 21 | parent::__construct( $polylang ); |
| 19 | 22 | |
| 20 | 23 | add_filter( 'wp_insert_post_parent', array( $this, 'wp_insert_post_parent' ), 10, 3 ); |
| 21 | 24 | add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ) ); |
| 22 | - add_action( 'rest_api_init', array( $this, 'new_post_translation' ) ); // Block editor | |
| 23 | - 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. | |
| 24 | 26 | } |
| 25 | 27 | |
| 26 | 28 | /** |
| 27 | 29 | * Translate post parent if exists when using "Add new" ( translation ) |
| @@ -44,9 +46,9 @@ | ||
| 44 | 46 | return $post_parent; |
| 45 | 47 | } |
| 46 | 48 | |
| 47 | 49 | /** |
| 48 | - * Copy menu order, comment, ping status and optionally the date when creating a new tanslation | |
| 50 | + * Copy menu order, comment, ping status and optionally the date when creating a new translation | |
| 49 | 51 | * |
| 50 | 52 | * @since 2.5 |
| 51 | 53 | * |
| 52 | 54 | * @param array $data An array of slashed post data. |
| @@ -58,16 +60,18 @@ | ||
| 58 | 60 | |
| 59 | 61 | $from_post_id = (int) $_GET['from_post']; |
| 60 | 62 | $from_post = get_post( $from_post_id ); |
| 61 | 63 | |
| 62 | - foreach ( array( 'menu_order', 'comment_status', 'ping_status' ) as $property ) { | |
| 63 | - $data[ $property ] = $from_post->$property; | |
| 64 | - } | |
| 64 | + if ( $from_post instanceof WP_Post ) { | |
| 65 | + foreach ( array( 'menu_order', 'comment_status', 'ping_status' ) as $property ) { | |
| 66 | + $data[ $property ] = $from_post->$property; | |
| 67 | + } | |
| 65 | 68 | |
| 66 | - // Copy the date only if the synchronization is activated | |
| 67 | - if ( in_array( 'post_date', $this->options['sync'] ) ) { | |
| 68 | - $data['post_date'] = $from_post->post_date; | |
| 69 | - $data['post_date_gmt'] = $from_post->post_date_gmt; | |
| 69 | + // Copy the date only if the synchronization is activated | |
| 70 | + if ( in_array( 'post_date', $this->options['sync'] ) ) { | |
| 71 | + $data['post_date'] = $from_post->post_date; | |
| 72 | + $data['post_date_gmt'] = $from_post->post_date_gmt; | |
| 73 | + } | |
| 70 | 74 | } |
| 71 | 75 | } |
| 72 | 76 | |
| 73 | 77 | return $data; |
| @@ -76,14 +80,18 @@ | ||
| 76 | 80 | /** |
| 77 | 81 | * Copy post metas, and taxonomies when using "Add new" ( translation ) |
| 78 | 82 | * |
| 79 | 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 | + * | |
| 86 | + * @param bool $is_block_editor Whether the post can be edited or not. | |
| 87 | + * @return bool | |
| 80 | 88 | */ |
| 81 | - public function new_post_translation() { | |
| 89 | + public function new_post_translation( $is_block_editor ) { | |
| 82 | 90 | global $post; |
| 83 | 91 | static $done = array(); |
| 84 | 92 | |
| 85 | - 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 ) ) { | |
| 86 | 94 | check_admin_referer( 'new-post-translation' ); |
| 87 | 95 | |
| 88 | 96 | // Capability check already done in post-new.php |
| 89 | 97 | $from_post_id = (int) $_GET['from_post']; |
| @@ -89,9 +97,9 @@ | ||
| 89 | 97 | $from_post_id = (int) $_GET['from_post']; |
| 90 | 98 | $lang = $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ); |
| 91 | 99 | |
| 92 | 100 | if ( ! $from_post_id || ! $lang || ! empty( $done[ $from_post_id ] ) ) { |
| 93 | - return; | |
| 101 | + return $is_block_editor; | |
| 94 | 102 | } |
| 95 | 103 | |
| 96 | 104 | $done[ $from_post_id ] = true; // Avoid a second duplication in the block editor. Using an array only to allow multiple phpunit tests. |
| 97 | 105 | |
| @@ -101,17 +109,19 @@ | ||
| 101 | 109 | if ( is_sticky( $from_post_id ) ) { |
| 102 | 110 | stick_post( $post->ID ); |
| 103 | 111 | } |
| 104 | 112 | } |
| 113 | + | |
| 114 | + return $is_block_editor; | |
| 105 | 115 | } |
| 106 | 116 | |
| 107 | 117 | /** |
| 108 | - * Get post fields to synchronize | |
| 118 | + * Get post fields to synchronize. | |
| 109 | 119 | * |
| 110 | 120 | * @since 2.4 |
| 111 | 121 | * |
| 112 | - * @param object $post Post object | |
| 113 | - * @return array | |
| 122 | + * @param WP_Post $post Post object. | |
| 123 | + * @return array Fields to synchronize. | |
| 114 | 124 | */ |
| 115 | 125 | protected function get_fields_to_sync( $post ) { |
| 116 | 126 | global $wpdb; |
| 117 | 127 | |
| @@ -116,9 +126,9 @@ | ||
| 116 | 126 | global $wpdb; |
| 117 | 127 | |
| 118 | 128 | $postarr = parent::get_fields_to_sync( $post ); |
| 119 | 129 | |
| 120 | - // For new drafts, save the date now otherwise it is overriden by WP. Thanks to JoryHogeveen. See #32. | |
| 130 | + // For new drafts, save the date now otherwise it is overridden by WP. Thanks to JoryHogeveen. See #32. | |
| 121 | 131 | if ( in_array( 'post_date', $this->options['sync'] ) && isset( $GLOBALS['pagenow'], $_GET['from_post'], $_GET['new_lang'] ) && 'post-new.php' === $GLOBALS['pagenow'] ) { |
| 122 | 132 | check_admin_referer( 'new-post-translation' ); |
| 123 | 133 | |
| 124 | 134 | unset( $postarr['post_date'] ); |
| @@ -124,16 +134,19 @@ | ||
| 124 | 134 | unset( $postarr['post_date'] ); |
| 125 | 135 | unset( $postarr['post_date_gmt'] ); |
| 126 | 136 | |
| 127 | 137 | $original = get_post( (int) $_GET['from_post'] ); |
| 128 | - $wpdb->update( | |
| 129 | - $wpdb->posts, | |
| 130 | - array( | |
| 131 | - 'post_date' => $original->post_date, | |
| 132 | - 'post_date_gmt' => $original->post_date_gmt, | |
| 133 | - ), | |
| 134 | - array( 'ID' => $post->ID ) | |
| 135 | - ); | |
| 138 | + | |
| 139 | + if ( $original instanceof WP_Post ) { | |
| 140 | + $wpdb->update( | |
| 141 | + $wpdb->posts, | |
| 142 | + array( | |
| 143 | + 'post_date' => $original->post_date, | |
| 144 | + 'post_date_gmt' => $original->post_date_gmt, | |
| 145 | + ), | |
| 146 | + array( 'ID' => $post->ID ) | |
| 147 | + ); | |
| 148 | + } | |
| 136 | 149 | } |
| 137 | 150 | |
| 138 | 151 | if ( isset( $GLOBALS['post_type'] ) ) { |
| 139 | 152 | $post_type = $GLOBALS['post_type']; |
| @@ -149,15 +162,15 @@ | ||
| 149 | 162 | return $postarr; |
| 150 | 163 | } |
| 151 | 164 | |
| 152 | 165 | /** |
| 153 | - * Synchronizes post fields in translations | |
| 166 | + * Synchronizes post fields in translations. | |
| 154 | 167 | * |
| 155 | 168 | * @since 1.2 |
| 156 | 169 | * |
| 157 | - * @param int $post_id post id | |
| 158 | - * @param object $post post object | |
| 159 | - * @param array $translations post translations | |
| 170 | + * @param int $post_id Post id. | |
| 171 | + * @param WP_Post $post Post object. | |
| 172 | + * @param int[] $translations Post translations. | |
| 160 | 173 | */ |
| 161 | 174 | public function pll_save_post( $post_id, $post, $translations ) { |
| 162 | 175 | parent::pll_save_post( $post_id, $post, $translations ); |
| 163 | 176 | |
| @@ -182,8 +195,9 @@ | ||
| 182 | 195 | * @since 2.3 |
| 183 | 196 | * |
| 184 | 197 | * @param string $func Function name |
| 185 | 198 | * @param array $args Function arguments |
| 199 | + * @return mixed|void | |
| 186 | 200 | */ |
| 187 | 201 | public function __call( $func, $args ) { |
| 188 | 202 | $obj = substr( $func, 5 ); |
| 189 | 203 | |