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 / admin / admin-classic-editor.php

admin-classic-editor.php in Polylang 2.4, at admin/admin-classic-editor.php

286 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Manages filters and actions related to the editor
5 *
6 * @since 2.4
7 */
8 class PLL_Admin_Classic_Editor {
9 public $model, $links, $curlang, $pref_lang;
10
11 /**
12 * Constructor: setups filters and actions
13 *
14 * @since 2.4
15 *
16 * @param object $polylang
17 */
18 public function __construct( &$polylang ) {
19 $this->model = &$polylang->model;
20 $this->links = &$polylang->links;
21 $this->curlang = &$polylang->curlang;
22 $this->pref_lang = &$polylang->pref_lang;
23
24 // Adds the Languages box in the 'Edit Post' and 'Edit Page' panels
25 add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 10, 2 );
26
27 // Ajax response for changing the language in the post metabox
28 add_action( 'wp_ajax_post_lang_choice', array( $this, 'post_lang_choice' ) );
29 add_action( 'wp_ajax_pll_posts_not_translated', array( $this, 'ajax_posts_not_translated' ) );
30
31 // Filters the pages by language in the parent dropdown list in the page attributes metabox
32 add_filter( 'page_attributes_dropdown_pages_args', array( $this, 'page_attributes_dropdown_pages_args' ), 10, 2 );
33 }
34
35 /**
36 * Adds the Language box in the 'Edit Post' and 'Edit Page' panels ( as well as in custom post types panels )
37 *
38 * @since 0.1
39 *
40 * @param string $post_type Current post type
41 * @param object $post Current post
42 */
43 public function add_meta_boxes( $post_type, $post ) {
44 if ( $this->model->is_translated_post_type( $post_type ) ) {
45 add_meta_box( 'ml_box', __( 'Languages', 'polylang' ), array( $this, 'post_language' ), $post_type, 'side', 'high' );
46 }
47 }
48
49 /**
50 * Displays the Languages metabox in the 'Edit Post' and 'Edit Page' panels
51 *
52 * @since 0.1
53 */
54 public function post_language() {
55 global $post_ID;
56 $post_id = $post_ID;
57 $post_type = get_post_type( $post_ID );
58
59 $lang = ( $lg = $this->model->post->get_language( $post_ID ) ) ? $lg :
60 ( isset( $_GET['new_lang'] ) ? $this->model->get_language( $_GET['new_lang'] ) :
61 $this->pref_lang );
62
63 $dropdown = new PLL_Walker_Dropdown();
64
65 wp_nonce_field( 'pll_language', '_pll_nonce' );
66
67 // NOTE: the class "tags-input" allows to include the field in the autosave $_POST ( see autosave.js )
68 printf(
69 '<p><strong>%1$s</strong></p>
70 <label class="screen-reader-text" for="%2$s">%1$s</label>
71 <div id="select-%3$s-language">%4$s</div>',
72 esc_html__( 'Language', 'polylang' ),
73 $id = ( 'attachment' === $post_type ) ? sprintf( 'attachments[%d][language]', $post_ID ) : 'post_lang_choice',
74 'attachment' === $post_type ? 'media' : 'post',
75 $dropdown->walk(
76 $this->model->get_languages_list(),
77 array(
78 'name' => $id,
79 'class' => 'post_lang_choice tags-input',
80 'selected' => $lang ? $lang->slug : '',
81 'flag' => true,
82 )
83 )
84 );
85
86 /**
87 * Fires before displaying the list of translations in the Languages metabox for posts
88 *
89 * @since 1.8
90 */
91 do_action( 'pll_before_post_translations', $post_type );
92
93 echo '<div id="post-translations" class="translations">';
94 if ( $lang ) {
95 include PLL_ADMIN_INC . '/view-translations-' . ( 'attachment' == $post_type ? 'media' : 'post' ) . '.php';
96 }
97 echo '</div>' . "\n";
98 }
99
100 /**
101 * Ajax response for changing the language in the post metabox
102 *
103 * @since 0.2
104 */
105 public function post_lang_choice() {
106 check_ajax_referer( 'pll_language', '_pll_nonce' );
107
108 global $post_ID; // Obliged to use the global variable for wp_popular_terms_checklist
109 $post_id = $post_ID = (int) $_POST['post_id'];
110 $lang = $this->model->get_language( $_POST['lang'] );
111
112 $post_type = $_POST['post_type'];
113 $post_type_object = get_post_type_object( $post_type );
114 if ( ! current_user_can( $post_type_object->cap->edit_post, $post_ID ) ) {
115 wp_die( -1 );
116 }
117
118 $this->model->post->set_language( $post_ID, $lang ); // Save language, useful to set the language when uploading media from post
119
120 // We also need to save the translations to match the language change
121 $translations = $this->model->post->get_translations( $post_ID );
122 $translations = array_diff( $translations, array( $post_ID ) );
123 $this->model->post->save_translations( $post_ID, $translations );
124
125 ob_start();
126 if ( $lang ) {
127 include PLL_ADMIN_INC . '/view-translations-' . ( 'attachment' == $post_type ? 'media' : 'post' ) . '.php';
128 }
129 $x = new WP_Ajax_Response( array( 'what' => 'translations', 'data' => ob_get_contents() ) );
130 ob_end_clean();
131
132 // Categories
133 if ( isset( $_POST['taxonomies'] ) ) {
134 // Not set for pages
135 foreach ( $_POST['taxonomies'] as $taxname ) {
136 $taxonomy = get_taxonomy( $taxname );
137
138 ob_start();
139 $popular_ids = wp_popular_terms_checklist( $taxonomy->name );
140 $supplemental['populars'] = ob_get_contents();
141 ob_end_clean();
142
143 ob_start();
144 // Use $post_ID to remember checked terms in case we come back to the original language
145 wp_terms_checklist( $post_ID, array( 'taxonomy' => $taxonomy->name, 'popular_cats' => $popular_ids ) );
146 $supplemental['all'] = ob_get_contents();
147 ob_end_clean();
148
149 $supplemental['dropdown'] = wp_dropdown_categories(
150 array(
151 'taxonomy' => $taxonomy->name,
152 'hide_empty' => 0,
153 'name' => 'new' . $taxonomy->name . '_parent',
154 'orderby' => 'name',
155 'hierarchical' => 1,
156 'show_option_none' => '&mdash; ' . $taxonomy->labels->parent_item . ' &mdash;',
157 'echo' => 0,
158 )
159 );
160
161 $x->Add( array( 'what' => 'taxonomy', 'data' => $taxonomy->name, 'supplemental' => $supplemental ) );
162 }
163 }
164
165 // Parent dropdown list ( only for hierarchical post types )
166 if ( in_array( $post_type, get_post_types( array( 'hierarchical' => true ) ) ) ) {
167 $post = get_post( $post_ID );
168
169 // Args and filter from 'page_attributes_meta_box' in wp-admin/includes/meta-boxes.php of WP 4.2.1
170 $dropdown_args = array(
171 'post_type' => $post->post_type,
172 'exclude_tree' => $post->ID,
173 'selected' => $post->post_parent,
174 'name' => 'parent_id',
175 'show_option_none' => __( '(no parent)' ),
176 'sort_column' => 'menu_order, post_title',
177 'echo' => 0,
178 );
179
180 /** This filter is documented in wp-admin/includes/meta-boxes.php */
181 $dropdown_args = apply_filters( 'page_attributes_dropdown_pages_args', $dropdown_args, $post ); // Since WP 3.3
182
183 $x->Add( array( 'what' => 'pages', 'data' => wp_dropdown_pages( $dropdown_args ) ) );
184 }
185
186 // Flag
187 $x->Add( array( 'what' => 'flag', 'data' => empty( $lang->flag ) ? esc_html( $lang->slug ) : $lang->flag ) );
188
189 // Sample permalink
190 $x->Add( array( 'what' => 'permalink', 'data' => get_sample_permalink_html( $post_ID ) ) );
191
192 $x->send();
193 }
194
195 /**
196 * Ajax response for input in translation autocomplete input box
197 *
198 * @since 1.5
199 */
200 public function ajax_posts_not_translated() {
201 check_ajax_referer( 'pll_language', '_pll_nonce' );
202
203 if ( ! post_type_exists( $_GET['post_type'] ) ) {
204 die( 0 );
205 }
206
207 $post_language = $this->model->get_language( $_GET['post_language'] );
208 $translation_language = $this->model->get_language( $_GET['translation_language'] );
209
210 // Don't order by title: see https://wordpress.org/support/topic/find-translated-post-when-10-is-not-enough
211 $args = array(
212 's' => wp_unslash( $_GET['term'] ),
213 'suppress_filters' => 0, // To make the post_fields filter work
214 'lang' => 0, // Avoid admin language filter
215 'numberposts' => 20, // Limit to 20 posts
216 'post_status' => 'any',
217 'post_type' => $_GET['post_type'],
218 'tax_query' => array(
219 array(
220 'taxonomy' => 'language',
221 'field' => 'term_taxonomy_id', // WP 3.5+
222 'terms' => $translation_language->term_taxonomy_id,
223 ),
224 ),
225 );
226
227 /**
228 * Filter the query args when auto suggesting untranslated posts in the Languages metabox
229 * This should help plugins to fix some edge cases
230 *
231 * @see https://wordpress.org/support/topic/find-translated-post-when-10-is-not-enough
232 *
233 * @since 1.7
234 *
235 * @param array $args WP_Query arguments
236 */
237 $args = apply_filters( 'pll_ajax_posts_not_translated_args', $args );
238 $posts = get_posts( $args );
239
240 $return = array();
241
242 foreach ( $posts as $key => $post ) {
243 if ( ! $this->model->post->get_translation( $post->ID, $post_language ) ) {
244 $return[] = array(
245 'id' => $post->ID,
246 'value' => $post->post_title,
247 'link' => $this->links->edit_post_translation_link( $post->ID ),
248 );
249 }
250 }
251
252 // Add current translation in list
253 if ( $post_id = $this->model->post->get_translation( (int) $_GET['pll_post_id'], $translation_language ) ) {
254 $post = get_post( $post_id );
255 array_unshift(
256 $return,
257 array(
258 'id' => $post_id,
259 'value' => $post->post_title,
260 'link' => $this->links->edit_post_translation_link( $post_id ),
261 )
262 );
263 }
264
265 wp_die( json_encode( $return ) );
266 }
267
268 /**
269 * Filters the pages by language in the parent dropdown list in the page attributes metabox
270 *
271 * @since 0.6
272 *
273 * @param array $dropdown_args Arguments passed to wp_dropdown_pages
274 * @param object $post
275 * @return array Modified arguments
276 */
277 public function page_attributes_dropdown_pages_args( $dropdown_args, $post ) {
278 $dropdown_args['lang'] = isset( $_POST['lang'] ) ? $this->model->get_language( $_POST['lang'] ) : $this->model->post->get_language( $post->ID ); // ajax or not ?
279 if ( ! $dropdown_args['lang'] ) {
280 $dropdown_args['lang'] = $this->pref_lang;
281 }
282
283 return $dropdown_args;
284 }
285 }
286