PluginProbe
Polylang / 2.4
Polylang v2.4
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / modules / sync / admin-sync.php

admin-sync.php in Polylang 2.4, at modules/sync/admin-sync.php

181 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Manages copy and synchronization of terms and post metas
5 *
6 * @since 1.2
7 */
8 class PLL_Admin_Sync extends PLL_Sync {
9
10 /**
11 * Constructor
12 *
13 * @since 1.2
14 *
15 * @param object $polylang
16 */
17 public function __construct( &$polylang ) {
18 parent::__construct( $polylang );
19
20 add_filter( 'wp_insert_post_parent', array( $this, 'wp_insert_post_parent' ), 10, 3 );
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
22 }
23
24 /**
25 * Translate post parent if exists when using "Add new" ( translation )
26 *
27 * @since 0.6
28 *
29 * @param int $post_parent Post parent ID
30 * @param int $post_id Post ID, unused
31 * @param array $postarr Array of parsed post data
32 * @return int
33 */
34 public function wp_insert_post_parent( $post_parent, $post_id, $postarr ) {
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;
37 }
38
39 /**
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
42 *
43 * @since 1.2
44 *
45 * @param string $post_type unused
46 * @param object $post current post object
47 */
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'] );
54
55 if ( ! $from_post || ! $lang ) {
56 return;
57 }
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
62 foreach ( array( 'menu_order', 'comment_status', 'ping_status' ) as $property ) {
63 $post->$property = $from_post->$property;
64 }
65
66 // Copy the date only if the synchronization is activated
67 if ( in_array( 'post_date', $this->options['sync'] ) ) {
68 $post->post_date = $from_post->post_date;
69 $post->post_date_gmt = $from_post->post_date_gmt;
70 }
71
72 if ( is_sticky( $from_post_id ) ) {
73 stick_post( $post->ID );
74 }
75 }
76 }
77
78 /**
79 * Get post fields to synchornize
80 *
81 * @since 2.4
82 *
83 * @param object $post Post object
84 * @return array
85 */
86 protected function get_fields_to_sync( $post ) {
87 global $wpdb;
88
89 $postarr = parent::get_fields_to_sync( $post );
90
91 // For new drafts, save the date now otherwise it is overriden by WP. Thanks to JoryHogeveen. See #32.
92 if ( in_array( 'post_date', $this->options['sync'] ) && 'post-new.php' === $GLOBALS['pagenow'] && isset( $_GET['from_post'], $_GET['new_lang'] ) ) {
93 unset( $postarr['post_date'] );
94 unset( $postarr['post_date_gmt'] );
95
96 $original = get_post( (int) $_GET['from_post'] );
97 $wpdb->update(
98 $wpdb->posts,
99 array(
100 'post_date' => $original->post_date,
101 'post_date_gmt' => $original->post_date_gmt,
102 ),
103 array( 'ID' => $post->ID )
104 );
105 }
106
107 if ( isset( $GLOBALS['post_type'] ) ) {
108 $post_type = $GLOBALS['post_type'];
109 } elseif ( isset( $_REQUEST['post_type'] ) ) {
110 $post_type = $_REQUEST['post_type']; // 2nd case for quick edit
111 }
112
113 // Make sure not to impact media translations when creating them at the same time as post
114 if ( in_array( 'post_parent', $this->options['sync'] ) && ( ! isset( $post_type ) || $post_type !== $post->post_type ) ) {
115 unset( $postarr['post_parent'] );
116 }
117
118 return $postarr;
119 }
120
121 /**
122 * Synchronizes post fields in translations
123 *
124 * @since 1.2
125 *
126 * @param int $post_id post id
127 * @param object $post post object
128 * @param array $translations post translations
129 */
130 public function pll_save_post( $post_id, $post, $translations ) {
131 parent::pll_save_post( $post_id, $post, $translations );
132
133 // Sticky posts
134 if ( in_array( 'sticky_posts', $this->options['sync'] ) ) {
135 $stickies = get_option( 'sticky_posts' );
136 if ( isset( $_REQUEST['sticky'] ) && 'sticky' === $_REQUEST['sticky'] ) {
137 $stickies = array_merge( $stickies, array_values( $translations ) );
138 } else {
139 $stickies = array_diff( $stickies, array_values( $translations ) );
140 }
141 update_option( 'sticky_posts', array_unique( $stickies ) );
142 }
143 }
144
145 /**
146 * Some backward compatibility with Polylang < 2.3
147 * allows to call PLL()->sync->copy_post_metas() and PLL()->sync->copy_taxonomies()
148 * used for example in Polylang for WooCommerce
149 * the compatibility is however only partial as the 4th argument $sync is lost
150 *
151 * @since 2.3
152 *
153 * @param string $func Function name
154 * @param array $args Function arguments
155 */
156 public function __call( $func, $args ) {
157 $obj = substr( $func, 5 );
158
159 if ( is_object( $this->$obj ) && method_exists( $this->$obj, 'copy' ) ) {
160 if ( WP_DEBUG ) {
161 $debug = debug_backtrace();
162 $i = 1 + empty( $debug[1]['line'] ); // The file and line are in $debug[2] if the function was called using call_user_func
163
164 trigger_error(
165 sprintf(
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",
167 $func,
168 $obj,
169 $debug[ $i ]['file'],
170 $debug[ $i ]['line']
171 )
172 );
173 }
174 return call_user_func_array( array( $this->$obj, 'copy' ), $args );
175 }
176
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 );
179 }
180 }
181