| @@ -8,8 +8,12 @@ | ||
| 8 | 8 | * |
| 9 | 9 | * @since 1.2 |
| 10 | 10 | */ |
| 11 | 11 | class PLL_Admin_Sync extends PLL_Sync { |
| 12 | + /** | |
| 13 | + * @var PLL_Admin_Links | |
| 14 | + */ | |
| 15 | + private $links; | |
| 12 | 16 | |
| 13 | 17 | /** |
| 14 | 18 | * Constructor |
| 15 | 19 | * |
| @@ -14,42 +18,55 @@ | ||
| 14 | 18 | * Constructor |
| 15 | 19 | * |
| 16 | 20 | * @since 1.2 |
| 17 | 21 | * |
| 18 | - * @param object $polylang | |
| 22 | + * @param object $polylang The Polylang object. | |
| 19 | 23 | */ |
| 20 | 24 | public function __construct( &$polylang ) { |
| 21 | 25 | parent::__construct( $polylang ); |
| 22 | 26 | |
| 27 | + $this->links = &$polylang->links; | |
| 28 | + | |
| 23 | 29 | add_filter( 'wp_insert_post_parent', array( $this, 'wp_insert_post_parent' ), 10, 3 ); |
| 24 | 30 | 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 | |
| 31 | + add_filter( 'use_block_editor_for_post', array( $this, 'new_post_translation' ), 5000 ); // After content duplication. | |
| 27 | 32 | } |
| 28 | 33 | |
| 29 | 34 | /** |
| 30 | - * Translate post parent if exists when using "Add new" ( translation ) | |
| 35 | + * Translates the post parent if it exists when using "Add new" (translation). | |
| 31 | 36 | * |
| 32 | 37 | * @since 0.6 |
| 33 | 38 | * |
| 34 | - * @param int $post_parent Post parent ID | |
| 35 | - * @param int $post_id Post ID, unused | |
| 36 | - * @param array $postarr Array of parsed post data | |
| 39 | + * @param int $post_parent Post parent ID. | |
| 40 | + * @param int $post_id Post ID, unused. | |
| 41 | + * @param array $postarr Array of parsed post data. | |
| 37 | 42 | * @return int |
| 38 | 43 | */ |
| 39 | 44 | public function wp_insert_post_parent( $post_parent, $post_id, $postarr ) { |
| 40 | - if ( isset( $_GET['from_post'], $_GET['new_lang'], $_GET['post_type'] ) ) { | |
| 41 | - check_admin_referer( 'new-post-translation' ); | |
| 42 | - // Make sure not to impact media translations created at the same time | |
| 43 | - if ( $_GET['post_type'] === $postarr['post_type'] && ( $id = wp_get_post_parent_id( (int) $_GET['from_post'] ) ) && $parent = $this->model->post->get_translation( $id, sanitize_key( $_GET['new_lang'] ) ) ) { | |
| 44 | - $post_parent = $parent; | |
| 45 | - } | |
| 45 | + $context_data = $this->links->get_data_from_new_post_translation_request( $postarr['post_type'] ?? '' ); | |
| 46 | + | |
| 47 | + if ( empty( $context_data ) ) { | |
| 48 | + return $post_parent; | |
| 46 | 49 | } |
| 47 | - return $post_parent; | |
| 50 | + | |
| 51 | + // Make sure not to impact media translations created at the same time. | |
| 52 | + $parent_id = wp_get_post_parent_id( $context_data['from_post'] ); | |
| 53 | + | |
| 54 | + if ( empty( $parent_id ) ) { | |
| 55 | + return $post_parent; | |
| 56 | + } | |
| 57 | + | |
| 58 | + $tr_parent = $this->model->post->get_translation( $parent_id, $context_data['new_lang'] ); | |
| 59 | + | |
| 60 | + if ( empty( $tr_parent ) ) { | |
| 61 | + return $post_parent; | |
| 62 | + } | |
| 63 | + | |
| 64 | + return $tr_parent; | |
| 48 | 65 | } |
| 49 | 66 | |
| 50 | 67 | /** |
| 51 | - * Copy menu order, comment, ping status and optionally the date when creating a new tanslation | |
| 68 | + * Copies menu order, comment, ping status and optionally the date when creating a new translation. | |
| 52 | 69 | * |
| 53 | 70 | * @since 2.5 |
| 54 | 71 | * |
| 55 | 72 | * @param array $data An array of slashed post data. |
| @@ -55,23 +72,22 @@ | ||
| 55 | 72 | * @param array $data An array of slashed post data. |
| 56 | 73 | * @return array |
| 57 | 74 | */ |
| 58 | 75 | public function wp_insert_post_data( $data ) { |
| 59 | - if ( isset( $GLOBALS['pagenow'], $_GET['from_post'], $_GET['new_lang'] ) && 'post-new.php' === $GLOBALS['pagenow'] && $this->model->is_translated_post_type( $data['post_type'] ) ) { | |
| 60 | - check_admin_referer( 'new-post-translation' ); | |
| 76 | + $context_data = $this->links->get_data_from_new_post_translation_request( $data['post_type'] ?? '' ); | |
| 61 | 77 | |
| 62 | - $from_post_id = (int) $_GET['from_post']; | |
| 63 | - $from_post = get_post( $from_post_id ); | |
| 78 | + if ( empty( $context_data ) ) { | |
| 79 | + return $data; | |
| 80 | + } | |
| 64 | 81 | |
| 65 | - foreach ( array( 'menu_order', 'comment_status', 'ping_status' ) as $property ) { | |
| 66 | - $data[ $property ] = $from_post->$property; | |
| 67 | - } | |
| 82 | + foreach ( array( 'menu_order', 'comment_status', 'ping_status' ) as $property ) { | |
| 83 | + $data[ $property ] = $context_data['from_post']->$property; | |
| 84 | + } | |
| 68 | 85 | |
| 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 | - } | |
| 86 | + // Copy the date only if the synchronization is activated. | |
| 87 | + if ( in_array( 'post_date', $this->options['sync'], true ) ) { | |
| 88 | + $data['post_date'] = $context_data['from_post']->post_date; | |
| 89 | + $data['post_date_gmt'] = $context_data['from_post']->post_date_gmt; | |
| 74 | 90 | } |
| 75 | 91 | |
| 76 | 92 | return $data; |
| 77 | 93 | } |
| @@ -76,64 +92,72 @@ | ||
| 76 | 92 | return $data; |
| 77 | 93 | } |
| 78 | 94 | |
| 79 | 95 | /** |
| 80 | - * Copy post metas, and taxonomies when using "Add new" ( translation ) | |
| 96 | + * Copies post metas and taxonomies when using "Add new" (translation). | |
| 81 | 97 | * |
| 82 | 98 | * @since 2.5 |
| 99 | + * @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. | |
| 100 | + * | |
| 101 | + * @param bool $is_block_editor Whether the post can be edited or not. | |
| 102 | + * @return bool | |
| 83 | 103 | */ |
| 84 | - public function new_post_translation() { | |
| 104 | + public function new_post_translation( $is_block_editor ) { | |
| 85 | 105 | global $post; |
| 86 | 106 | static $done = array(); |
| 87 | 107 | |
| 88 | - if ( isset( $GLOBALS['pagenow'], $_GET['from_post'], $_GET['new_lang'] ) && 'post-new.php' === $GLOBALS['pagenow'] && $this->model->is_translated_post_type( $post->post_type ) ) { | |
| 89 | - check_admin_referer( 'new-post-translation' ); | |
| 108 | + if ( empty( $post ) ) { | |
| 109 | + return $is_block_editor; | |
| 110 | + } | |
| 90 | 111 | |
| 91 | - // Capability check already done in post-new.php | |
| 92 | - $from_post_id = (int) $_GET['from_post']; | |
| 93 | - $lang = $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ); | |
| 112 | + $context_data = $this->links->get_data_from_new_post_translation_request( $post->post_type ); | |
| 94 | 113 | |
| 95 | - if ( ! $from_post_id || ! $lang || ! empty( $done[ $from_post_id ] ) ) { | |
| 96 | - return; | |
| 97 | - } | |
| 114 | + if ( empty( $context_data ) || ! empty( $done[ $context_data['from_post']->ID ] ) ) { | |
| 115 | + return $is_block_editor; | |
| 116 | + } | |
| 98 | 117 | |
| 99 | - $done[ $from_post_id ] = true; // Avoid a second duplication in the block editor. Using an array only to allow multiple phpunit tests. | |
| 118 | + $lang = $this->model->get_language( $context_data['new_lang'] ); | |
| 100 | 119 | |
| 101 | - $this->taxonomies->copy( $from_post_id, $post->ID, $lang->slug ); | |
| 102 | - $this->post_metas->copy( $from_post_id, $post->ID, $lang->slug ); | |
| 120 | + if ( empty( $lang ) ) { | |
| 121 | + return $is_block_editor; | |
| 122 | + } | |
| 103 | 123 | |
| 104 | - if ( is_sticky( $from_post_id ) ) { | |
| 105 | - stick_post( $post->ID ); | |
| 106 | - } | |
| 124 | + $done[ $context_data['from_post']->ID ] = true; // Avoid a second duplication in the block editor. Using an array only to allow multiple phpunit tests. | |
| 125 | + | |
| 126 | + $this->taxonomies->copy( $context_data['from_post']->ID, $post->ID, $lang->slug ); | |
| 127 | + $this->post_metas->copy( $context_data['from_post']->ID, $post->ID, $lang->slug ); | |
| 128 | + | |
| 129 | + if ( is_sticky( $context_data['from_post']->ID ) ) { | |
| 130 | + stick_post( $post->ID ); | |
| 107 | 131 | } |
| 132 | + | |
| 133 | + return $is_block_editor; | |
| 108 | 134 | } |
| 109 | 135 | |
| 110 | 136 | /** |
| 111 | - * Get post fields to synchronize | |
| 137 | + * Get post fields to synchronize. | |
| 112 | 138 | * |
| 113 | 139 | * @since 2.4 |
| 114 | 140 | * |
| 115 | - * @param object $post Post object | |
| 116 | - * @return array | |
| 141 | + * @param WP_Post $post Post object. | |
| 142 | + * @return array Fields to synchronize. | |
| 117 | 143 | */ |
| 118 | 144 | protected function get_fields_to_sync( $post ) { |
| 119 | 145 | global $wpdb; |
| 120 | 146 | |
| 121 | - $postarr = parent::get_fields_to_sync( $post ); | |
| 147 | + $postarr = parent::get_fields_to_sync( $post ); | |
| 148 | + $context_data = $this->links->get_data_from_new_post_translation_request( $post->post_type ); | |
| 122 | 149 | |
| 123 | - // For new drafts, save the date now otherwise it is overriden by WP. Thanks to JoryHogeveen. See #32. | |
| 124 | - if ( in_array( 'post_date', $this->options['sync'] ) && isset( $GLOBALS['pagenow'], $_GET['from_post'], $_GET['new_lang'] ) && 'post-new.php' === $GLOBALS['pagenow'] ) { | |
| 125 | - check_admin_referer( 'new-post-translation' ); | |
| 126 | - | |
| 150 | + // For new drafts, save the date now otherwise it is overridden by WP. Thanks to JoryHogeveen. See #32. | |
| 151 | + if ( ! empty( $context_data ) && in_array( 'post_date', $this->options['sync'], true ) ) { | |
| 127 | 152 | unset( $postarr['post_date'] ); |
| 128 | 153 | unset( $postarr['post_date_gmt'] ); |
| 129 | 154 | |
| 130 | - $original = get_post( (int) $_GET['from_post'] ); | |
| 131 | 155 | $wpdb->update( |
| 132 | 156 | $wpdb->posts, |
| 133 | 157 | array( |
| 134 | - 'post_date' => $original->post_date, | |
| 135 | - 'post_date_gmt' => $original->post_date_gmt, | |
| 158 | + 'post_date' => $context_data['from_post']->post_date, | |
| 159 | + 'post_date_gmt' => $context_data['from_post']->post_date_gmt, | |
| 136 | 160 | ), |
| 137 | 161 | array( 'ID' => $post->ID ) |
| 138 | 162 | ); |
| 139 | 163 | } |
| @@ -139,14 +163,15 @@ | ||
| 139 | 163 | } |
| 140 | 164 | |
| 141 | 165 | if ( isset( $GLOBALS['post_type'] ) ) { |
| 142 | 166 | $post_type = $GLOBALS['post_type']; |
| 143 | - } elseif ( isset( $_REQUEST['post_type'] ) ) { | |
| 144 | - $post_type = sanitize_key( $_REQUEST['post_type'] ); // 2nd case for quick edit | |
| 167 | + } elseif ( isset( $_REQUEST['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 168 | + // 2nd case for quick edit. | |
| 169 | + $post_type = sanitize_key( $_REQUEST['post_type'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 145 | 170 | } |
| 146 | 171 | |
| 147 | 172 | // Make sure not to impact media translations when creating them at the same time as post |
| 148 | - if ( in_array( 'post_parent', $this->options['sync'] ) && ( ! isset( $post_type ) || $post_type !== $post->post_type ) ) { | |
| 173 | + if ( in_array( 'post_parent', $this->options['sync'], true ) && ( ! isset( $post_type ) || $post_type !== $post->post_type ) ) { | |
| 149 | 174 | unset( $postarr['post_parent'] ); |
| 150 | 175 | } |
| 151 | 176 | |
| 152 | 177 | return $postarr; |
| @@ -152,15 +177,15 @@ | ||
| 152 | 177 | return $postarr; |
| 153 | 178 | } |
| 154 | 179 | |
| 155 | 180 | /** |
| 156 | - * Synchronizes post fields in translations | |
| 181 | + * Synchronizes post fields in translations. | |
| 157 | 182 | * |
| 158 | 183 | * @since 1.2 |
| 159 | 184 | * |
| 160 | - * @param int $post_id post id | |
| 161 | - * @param object $post post object | |
| 162 | - * @param array $translations post translations | |
| 185 | + * @param int $post_id Post id. | |
| 186 | + * @param WP_Post $post Post object. | |
| 187 | + * @param int[] $translations Post translations. | |
| 163 | 188 | */ |
| 164 | 189 | public function pll_save_post( $post_id, $post, $translations ) { |
| 165 | 190 | parent::pll_save_post( $post_id, $post, $translations ); |
| 166 | 191 | |
| @@ -185,8 +210,9 @@ | ||
| 185 | 210 | * @since 2.3 |
| 186 | 211 | * |
| 187 | 212 | * @param string $func Function name |
| 188 | 213 | * @param array $args Function arguments |
| 214 | + * @return mixed|void | |
| 189 | 215 | */ |
| 190 | 216 | public function __call( $func, $args ) { |
| 191 | 217 | $obj = substr( $func, 5 ); |
| 192 | 218 | |