PluginProbe
Polylang / 3.8.10
Polylang v3.8.10
3.8.10 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 All 234 releases
polylang / src / admin / admin-filters-post.php

admin-filters-post.php in Polylang 3.8.10, at src/admin/admin-filters-post.php

271 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 use WP_Syntex\Polylang\Capabilities\Capabilities;
7
8 /**
9 * Manages filters and actions related to posts on admin side
10 *
11 * @since 1.2
12 */
13 class PLL_Admin_Filters_Post extends PLL_Admin_Filters_Post_Base {
14 /**
15 * Current language (used to filter the content).
16 *
17 * @var PLL_Language|null
18 */
19 public $curlang;
20
21 /**
22 * Constructor: setups filters and actions
23 *
24 * @since 1.2
25 *
26 * @param object $polylang The Polylang object.
27 */
28 public function __construct( &$polylang ) {
29 parent::__construct( $polylang );
30 $this->curlang = &$polylang->curlang;
31
32 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
33
34 // Filters posts, pages and media by language
35 add_action( 'parse_query', array( $this, 'parse_query' ) );
36
37 // Adds actions and filters related to languages when creating, saving or deleting posts and pages
38 add_action( 'load-post.php', array( $this, 'edit_post' ) );
39 add_action( 'load-edit.php', array( $this, 'bulk_edit_posts' ) );
40 add_action( 'wp_ajax_inline-save', array( $this, 'inline_edit_post' ), 0 ); // Before WordPress
41
42 // Sets the language in Tiny MCE
43 add_filter( 'tiny_mce_before_init', array( $this, 'tiny_mce_before_init' ) );
44 }
45
46 /**
47 * Outputs a javascript list of terms ordered by language and hierarchical taxonomies
48 * to filter the category checklist per post language in quick edit
49 * Outputs a javascript list of pages ordered by language
50 * to filter the parent dropdown per post language in quick edit
51 *
52 * @since 1.7
53 *
54 * @return void
55 */
56 public function admin_enqueue_scripts() {
57 $screen = get_current_screen();
58
59 if ( empty( $screen ) ) {
60 return;
61 }
62
63 // Hierarchical taxonomies
64 if ( 'edit' == $screen->base && $taxonomies = get_object_taxonomies( $screen->post_type, 'objects' ) ) {
65 // Get translated hierarchical taxonomies
66 $hierarchical_taxonomies = array();
67 foreach ( $taxonomies as $taxonomy ) {
68 if ( $taxonomy->hierarchical && $taxonomy->show_in_quick_edit && $this->model->is_translated_taxonomy( $taxonomy->name ) ) {
69 $hierarchical_taxonomies[] = $taxonomy->name;
70 }
71 }
72
73 if ( ! empty( $hierarchical_taxonomies ) ) {
74 $terms = get_terms( array( 'taxonomy' => $hierarchical_taxonomies, 'get' => 'all' ) );
75 $term_languages = array();
76
77 if ( is_array( $terms ) ) {
78 foreach ( $terms as $term ) {
79 if ( $lang = $this->model->term->get_language( $term->term_id ) ) {
80 $term_languages[ $lang->slug ][ $term->taxonomy ][] = $term->term_id;
81 }
82 }
83 }
84
85 // Send all these data to javascript
86 if ( ! empty( $term_languages ) ) {
87 wp_localize_script( 'pll_post', 'pll_term_languages', $term_languages );
88 }
89 }
90 }
91
92 // Hierarchical post types
93 if ( 'edit' == $screen->base && is_post_type_hierarchical( $screen->post_type ) && post_type_supports( $screen->post_type, 'page-attributes' ) ) {
94 $pages = get_pages(
95 array(
96 'sort_column' => 'menu_order, post_title',
97 'post_type' => $screen->post_type,
98 )
99 ); // Same arguments as the parent pages dropdown to avoid an extra query.
100
101 if ( ! is_array( $pages ) ) {
102 return;
103 }
104
105 update_object_term_cache( wp_list_pluck( $pages, 'ID' ), $screen->post_type );
106
107 $page_languages = array();
108
109 foreach ( $pages as $page ) {
110 if ( $lang = $this->model->post->get_language( $page->ID ) ) {
111 $page_languages[ $lang->slug ][] = $page->ID;
112 }
113 }
114
115 // Send all these data to javascript
116 if ( ! empty( $page_languages ) ) {
117 wp_localize_script( 'pll_post', 'pll_page_languages', $page_languages );
118 }
119 }
120 }
121
122 /**
123 * Filters posts, pages and media by language.
124 *
125 * @since 0.1
126 *
127 * @param WP_Query $query WP_Query object.
128 * @return void
129 */
130 public function parse_query( $query ) {
131 $pll_query = new PLL_Query( $query, $this->model );
132 $pll_query->filter_query( $this->curlang );
133 }
134
135 /**
136 * Save language and translation when editing a post (post.php).
137 *
138 * @since 2.3
139 *
140 * @return void
141 */
142 public function edit_post() {
143 if ( ! isset( $_POST['post_lang_choice'], $_POST['post_ID'] ) ) {
144 return;
145 }
146
147 check_admin_referer( 'pll_language', '_pll_nonce' );
148
149 $post_id = (int) $_POST['post_ID'];
150 $post = get_post( $post_id );
151
152 if ( empty( $post ) ) {
153 return;
154 }
155
156 $post_type_object = get_post_type_object( $post->post_type );
157
158 if ( empty( $post_type_object ) ) {
159 return;
160 }
161
162 $user = Capabilities::get_user();
163 if ( ! $user->has_cap( $post_type_object->cap->edit_post, $post_id ) ) {
164 return;
165 }
166
167 $language = $this->model->get_language( sanitize_key( $_POST['post_lang_choice'] ) );
168
169 if ( empty( $language ) ) {
170 return;
171 }
172
173 $user->can_translate_or_die( $language );
174
175 $this->model->post->set_language( $post_id, $language );
176
177 if ( ! isset( $_POST['post_tr_lang'] ) ) {
178 return;
179 }
180
181 $this->save_translations( $post_id, array_map( 'absint', $_POST['post_tr_lang'] ) );
182 }
183
184 /**
185 * Save language when bulk editing posts.
186 *
187 * @since 2.3
188 *
189 * @return void
190 */
191 public function bulk_edit_posts() {
192 if ( ! isset( $_GET['bulk_edit'], $_GET['inline_lang_choice'], $_REQUEST['post'], $_REQUEST['_wpnonce'] ) ) {
193 return;
194 }
195
196 if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'bulk-posts' ) ) {
197 return;
198 }
199
200 if ( -1 === $_GET['inline_lang_choice'] ) {
201 return;
202 }
203
204 $language = $this->model->get_language( sanitize_key( $_GET['inline_lang_choice'] ) );
205
206 if ( empty( $language ) ) {
207 return;
208 }
209
210 $user = Capabilities::get_user();
211 $user->can_translate_or_die( $language );
212
213 $post_ids = array_map( 'intval', (array) $_REQUEST['post'] );
214 foreach ( $post_ids as $post_id ) {
215 if ( $user->has_cap( 'edit_post', $post_id ) ) {
216 $this->model->post->set_language( $post_id, $language );
217 }
218 }
219 }
220
221 /**
222 * Save language when inline editing a post.
223 *
224 * @since 2.3
225 *
226 * @return void
227 */
228 public function inline_edit_post() {
229 if ( ! isset( $_POST['post_ID'], $_POST['inline_lang_choice'], $_REQUEST['_inline_edit'] ) ) {
230 return;
231 }
232
233 if ( ! wp_verify_nonce( $_REQUEST['_inline_edit'], 'inlineeditnonce' ) ) {
234 return;
235 }
236
237 $language = $this->model->get_language( sanitize_key( $_POST['inline_lang_choice'] ) );
238
239 if ( empty( $language ) ) {
240 return;
241 }
242
243 $user = Capabilities::get_user();
244 $user->can_translate_or_die( $language );
245
246 $post_id = (int) $_POST['post_ID'];
247
248 if ( ! $post_id || ! $user->has_cap( 'edit_post', $post_id ) ) {
249 return;
250 }
251
252 $this->model->post->set_language( $post_id, $language );
253 }
254
255 /**
256 * Sets the language attribute and text direction for Tiny MCE.
257 *
258 * @since 2.2
259 *
260 * @param array $mce_init TinyMCE config.
261 * @return array
262 */
263 public function tiny_mce_before_init( $mce_init ) {
264 if ( ! empty( $this->curlang ) ) {
265 $mce_init['wp_lang_attr'] = $this->curlang->get_locale( 'display' );
266 $mce_init['directionality'] = $this->curlang->is_rtl ? 'rtl' : 'ltr';
267 }
268 return $mce_init;
269 }
270 }
271