| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * Manages copy and synchronization of terms and post metas |
| 8 | 5 | * |
| @@ -20,11 +17,9 @@ | ||
| 20 | 17 | public function __construct( &$polylang ) { |
| 21 | 18 | parent::__construct( $polylang ); |
| 22 | 19 | |
| 23 | 20 | add_filter( 'wp_insert_post_parent', array( $this, 'wp_insert_post_parent' ), 10, 3 ); |
| 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 | |
| 21 | + add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 5, 2 ); // Before Types which populates custom fields in same hook with priority 10 | |
| 27 | 22 | } |
| 28 | 23 | |
| 29 | 24 | /** |
| 30 | 25 | * Translate post parent if exists when using "Add new" ( translation ) |
| @@ -36,72 +31,45 @@ | ||
| 36 | 31 | * @param array $postarr Array of parsed post data |
| 37 | 32 | * @return int |
| 38 | 33 | */ |
| 39 | 34 | 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 | - } | |
| 46 | - } | |
| 47 | - return $post_parent; | |
| 35 | + // Make sure not to impact media translations created at the same time | |
| 36 | + return isset( $_GET['from_post'], $_GET['new_lang'], $_GET['post_type'] ) && $_GET['post_type'] === $postarr['post_type'] && ( $id = wp_get_post_parent_id( (int) $_GET['from_post'] ) ) && ( $parent = $this->model->post->get_translation( $id, $_GET['new_lang'] ) ) ? $parent : $post_parent; | |
| 48 | 37 | } |
| 49 | 38 | |
| 50 | 39 | /** |
| 51 | - * Copy menu order, comment, ping status and optionally the date when creating a new tanslation | |
| 40 | + * Copy post metas, menu order, comment and ping status when using "Add new" ( translation ) | |
| 41 | + * formerly used dbx_post_advanced deprecated in WP 3.7 | |
| 52 | 42 | * |
| 53 | - * @since 2.5 | |
| 43 | + * @since 1.2 | |
| 54 | 44 | * |
| 55 | - * @param array $data An array of slashed post data. | |
| 56 | - * @return array | |
| 45 | + * @param string $post_type unused | |
| 46 | + * @param object $post current post object | |
| 57 | 47 | */ |
| 58 | - 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' ); | |
| 48 | + public function add_meta_boxes( $post_type, $post ) { | |
| 49 | + if ( 'post-new.php' == $GLOBALS['pagenow'] && isset( $_GET['from_post'], $_GET['new_lang'] ) && $this->model->is_translated_post_type( $post->post_type ) ) { | |
| 50 | + // Capability check already done in post-new.php | |
| 51 | + $from_post_id = (int) $_GET['from_post']; | |
| 52 | + $from_post = get_post( $from_post_id ); | |
| 53 | + $lang = $this->model->get_language( $_GET['new_lang'] ); | |
| 61 | 54 | |
| 62 | - $from_post_id = (int) $_GET['from_post']; | |
| 63 | - $from_post = get_post( $from_post_id ); | |
| 55 | + if ( ! $from_post || ! $lang ) { | |
| 56 | + return; | |
| 57 | + } | |
| 64 | 58 | |
| 59 | + $this->taxonomies->copy( $from_post_id, $post->ID, $lang->slug ); | |
| 60 | + $this->post_metas->copy( $from_post_id, $post->ID, $lang->slug ); | |
| 61 | + | |
| 65 | 62 | foreach ( array( 'menu_order', 'comment_status', 'ping_status' ) as $property ) { |
| 66 | - $data[ $property ] = $from_post->$property; | |
| 63 | + $post->$property = $from_post->$property; | |
| 67 | 64 | } |
| 68 | 65 | |
| 69 | 66 | // Copy the date only if the synchronization is activated |
| 70 | 67 | 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; | |
| 68 | + $post->post_date = $from_post->post_date; | |
| 69 | + $post->post_date_gmt = $from_post->post_date_gmt; | |
| 73 | 70 | } |
| 74 | - } | |
| 75 | 71 | |
| 76 | - return $data; | |
| 77 | - } | |
| 78 | - | |
| 79 | - /** | |
| 80 | - * Copy post metas, and taxonomies when using "Add new" ( translation ) | |
| 81 | - * | |
| 82 | - * @since 2.5 | |
| 83 | - */ | |
| 84 | - public function new_post_translation() { | |
| 85 | - global $post; | |
| 86 | - static $done = array(); | |
| 87 | - | |
| 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' ); | |
| 90 | - | |
| 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'] ) ); | |
| 94 | - | |
| 95 | - if ( ! $from_post_id || ! $lang || ! empty( $done[ $from_post_id ] ) ) { | |
| 96 | - return; | |
| 97 | - } | |
| 98 | - | |
| 99 | - $done[ $from_post_id ] = true; // Avoid a second duplication in the block editor. Using an array only to allow multiple phpunit tests. | |
| 100 | - | |
| 101 | - $this->taxonomies->copy( $from_post_id, $post->ID, $lang->slug ); | |
| 102 | - $this->post_metas->copy( $from_post_id, $post->ID, $lang->slug ); | |
| 103 | - | |
| 104 | 72 | if ( is_sticky( $from_post_id ) ) { |
| 105 | 73 | stick_post( $post->ID ); |
| 106 | 74 | } |
| 107 | 75 | } |
| @@ -107,9 +75,9 @@ | ||
| 107 | 75 | } |
| 108 | 76 | } |
| 109 | 77 | |
| 110 | 78 | /** |
| 111 | - * Get post fields to synchronize | |
| 79 | + * Get post fields to synchornize | |
| 112 | 80 | * |
| 113 | 81 | * @since 2.4 |
| 114 | 82 | * |
| 115 | 83 | * @param object $post Post object |
| @@ -120,11 +88,9 @@ | ||
| 120 | 88 | |
| 121 | 89 | $postarr = parent::get_fields_to_sync( $post ); |
| 122 | 90 | |
| 123 | 91 | // 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 | - | |
| 92 | + if ( in_array( 'post_date', $this->options['sync'] ) && 'post-new.php' === $GLOBALS['pagenow'] && isset( $_GET['from_post'], $_GET['new_lang'] ) ) { | |
| 127 | 93 | unset( $postarr['post_date'] ); |
| 128 | 94 | unset( $postarr['post_date_gmt'] ); |
| 129 | 95 | |
| 130 | 96 | $original = get_post( (int) $_GET['from_post'] ); |
| @@ -140,9 +106,9 @@ | ||
| 140 | 106 | |
| 141 | 107 | if ( isset( $GLOBALS['post_type'] ) ) { |
| 142 | 108 | $post_type = $GLOBALS['post_type']; |
| 143 | 109 | } elseif ( isset( $_REQUEST['post_type'] ) ) { |
| 144 | - $post_type = sanitize_key( $_REQUEST['post_type'] ); // 2nd case for quick edit | |
| 110 | + $post_type = $_REQUEST['post_type']; // 2nd case for quick edit | |
| 145 | 111 | } |
| 146 | 112 | |
| 147 | 113 | // Make sure not to impact media translations when creating them at the same time as post |
| 148 | 114 | if ( in_array( 'post_parent', $this->options['sync'] ) && ( ! isset( $post_type ) || $post_type !== $post->post_type ) ) { |
| @@ -166,9 +132,9 @@ | ||
| 166 | 132 | |
| 167 | 133 | // Sticky posts |
| 168 | 134 | if ( in_array( 'sticky_posts', $this->options['sync'] ) ) { |
| 169 | 135 | $stickies = get_option( 'sticky_posts' ); |
| 170 | - if ( isset( $_REQUEST['sticky'] ) && 'sticky' === $_REQUEST['sticky'] ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 136 | + if ( isset( $_REQUEST['sticky'] ) && 'sticky' === $_REQUEST['sticky'] ) { | |
| 171 | 137 | $stickies = array_merge( $stickies, array_values( $translations ) ); |
| 172 | 138 | } else { |
| 173 | 139 | $stickies = array_diff( $stickies, array_values( $translations ) ); |
| 174 | 140 | } |
| @@ -191,18 +157,18 @@ | ||
| 191 | 157 | $obj = substr( $func, 5 ); |
| 192 | 158 | |
| 193 | 159 | if ( is_object( $this->$obj ) && method_exists( $this->$obj, 'copy' ) ) { |
| 194 | 160 | if ( WP_DEBUG ) { |
| 195 | - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 161 | + $debug = debug_backtrace(); | |
| 196 | 162 | $i = 1 + empty( $debug[1]['line'] ); // The file and line are in $debug[2] if the function was called using call_user_func |
| 197 | 163 | |
| 198 | - trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 164 | + trigger_error( | |
| 199 | 165 | sprintf( |
| 200 | 166 | '%1$s was called incorrectly in %3$s on line %4$s: the call to PLL()->sync->%1$s() has been deprecated in Polylang 2.3, use PLL()->sync->%2$s->copy() instead.' . "\nError handler", |
| 201 | - esc_html( $func ), | |
| 202 | - esc_html( $obj ), | |
| 203 | - esc_html( $debug[ $i ]['file'] ), | |
| 204 | - absint( $debug[ $i ]['line'] ) | |
| 167 | + $func, | |
| 168 | + $obj, | |
| 169 | + $debug[ $i ]['file'], | |
| 170 | + $debug[ $i ]['line'] | |
| 205 | 171 | ) |
| 206 | 172 | ); |
| 207 | 173 | } |
| 208 | 174 | return call_user_func_array( array( $this->$obj, 'copy' ), $args ); |
| @@ -207,16 +173,8 @@ | ||
| 207 | 173 | } |
| 208 | 174 | return call_user_func_array( array( $this->$obj, 'copy' ), $args ); |
| 209 | 175 | } |
| 210 | 176 | |
| 211 | - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 212 | - trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 213 | - sprintf( | |
| 214 | - 'Call to undefined function PLL()->sync->%1$s() in %2$s on line %3$s' . "\nError handler", | |
| 215 | - esc_html( $func ), | |
| 216 | - esc_html( $debug[0]['file'] ), | |
| 217 | - absint( $debug[0]['line'] ) | |
| 218 | - ), | |
| 219 | - E_USER_ERROR | |
| 220 | - ); | |
| 177 | + $debug = debug_backtrace(); | |
| 178 | + trigger_error( sprintf( 'Call to undefined function PLL()->sync->%1$s() in %2$s on line %3$s' . "\nError handler", $func, $debug[0]['file'], $debug[0]['line'] ), E_USER_ERROR ); | |
| 221 | 179 | } |
| 222 | 180 | } |