PluginProbe
Polylang / 3.8.6
Polylang v3.8.6
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 / src / admin / admin-filters-post.php

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

262 lines 6.6 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 ) ) {
94 $pages = get_pages( array( 'sort_column' => 'menu_order, post_title' ) ); // Same arguments as the parent pages dropdown to avoid an extra query.
95
96 update_post_caches( $pages, $screen->post_type, true, false );
97
98 $page_languages = array();
99
100 foreach ( $pages as $page ) {
101 if ( $lang = $this->model->post->get_language( $page->ID ) ) {
102 $page_languages[ $lang->slug ][] = $page->ID;
103 }
104 }
105
106 // Send all these data to javascript
107 if ( ! empty( $page_languages ) ) {
108 wp_localize_script( 'pll_post', 'pll_page_languages', $page_languages );
109 }
110 }
111 }
112
113 /**
114 * Filters posts, pages and media by language.
115 *
116 * @since 0.1
117 *
118 * @param WP_Query $query WP_Query object.
119 * @return void
120 */
121 public function parse_query( $query ) {
122 $pll_query = new PLL_Query( $query, $this->model );
123 $pll_query->filter_query( $this->curlang );
124 }
125
126 /**
127 * Save language and translation when editing a post (post.php).
128 *
129 * @since 2.3
130 *
131 * @return void
132 */
133 public function edit_post() {
134 if ( ! isset( $_POST['post_lang_choice'], $_POST['post_ID'] ) ) {
135 return;
136 }
137
138 check_admin_referer( 'pll_language', '_pll_nonce' );
139
140 $post_id = (int) $_POST['post_ID'];
141 $post = get_post( $post_id );
142
143 if ( empty( $post ) ) {
144 return;
145 }
146
147 $post_type_object = get_post_type_object( $post->post_type );
148
149 if ( empty( $post_type_object ) ) {
150 return;
151 }
152
153 $user = Capabilities::get_user();
154 if ( ! $user->has_cap( $post_type_object->cap->edit_post, $post_id ) ) {
155 return;
156 }
157
158 $language = $this->model->get_language( sanitize_key( $_POST['post_lang_choice'] ) );
159
160 if ( empty( $language ) ) {
161 return;
162 }
163
164 $user->can_translate_or_die( $language );
165
166 $this->model->post->set_language( $post_id, $language );
167
168 if ( ! isset( $_POST['post_tr_lang'] ) ) {
169 return;
170 }
171
172 $this->save_translations( $post_id, array_map( 'absint', $_POST['post_tr_lang'] ) );
173 }
174
175 /**
176 * Save language when bulk editing posts.
177 *
178 * @since 2.3
179 *
180 * @return void
181 */
182 public function bulk_edit_posts() {
183 if ( ! isset( $_GET['bulk_edit'], $_GET['inline_lang_choice'], $_REQUEST['post'], $_REQUEST['_wpnonce'] ) ) {
184 return;
185 }
186
187 if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'bulk-posts' ) ) {
188 return;
189 }
190
191 if ( -1 === $_GET['inline_lang_choice'] ) {
192 return;
193 }
194
195 $language = $this->model->get_language( sanitize_key( $_GET['inline_lang_choice'] ) );
196
197 if ( empty( $language ) ) {
198 return;
199 }
200
201 $user = Capabilities::get_user();
202 $user->can_translate_or_die( $language );
203
204 $post_ids = array_map( 'intval', (array) $_REQUEST['post'] );
205 foreach ( $post_ids as $post_id ) {
206 if ( $user->has_cap( 'edit_post', $post_id ) ) {
207 $this->model->post->set_language( $post_id, $language );
208 }
209 }
210 }
211
212 /**
213 * Save language when inline editing a post.
214 *
215 * @since 2.3
216 *
217 * @return void
218 */
219 public function inline_edit_post() {
220 if ( ! isset( $_POST['post_ID'], $_POST['inline_lang_choice'], $_REQUEST['_inline_edit'] ) ) {
221 return;
222 }
223
224 if ( ! wp_verify_nonce( $_REQUEST['_inline_edit'], 'inlineeditnonce' ) ) {
225 return;
226 }
227
228 $language = $this->model->get_language( sanitize_key( $_POST['inline_lang_choice'] ) );
229
230 if ( empty( $language ) ) {
231 return;
232 }
233
234 $user = Capabilities::get_user();
235 $user->can_translate_or_die( $language );
236
237 $post_id = (int) $_POST['post_ID'];
238
239 if ( ! $post_id || ! $user->has_cap( 'edit_post', $post_id ) ) {
240 return;
241 }
242
243 $this->model->post->set_language( $post_id, $language );
244 }
245
246 /**
247 * Sets the language attribute and text direction for Tiny MCE.
248 *
249 * @since 2.2
250 *
251 * @param array $mce_init TinyMCE config.
252 * @return array
253 */
254 public function tiny_mce_before_init( $mce_init ) {
255 if ( ! empty( $this->curlang ) ) {
256 $mce_init['wp_lang_attr'] = $this->curlang->get_locale( 'display' );
257 $mce_init['directionality'] = $this->curlang->is_rtl ? 'rtl' : 'ltr';
258 }
259 return $mce_init;
260 }
261 }
262