PluginProbe
Polylang / 3.7.1
Polylang v3.7.1
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-filters-post.php

admin-filters-post.php in Polylang 3.7.1, at admin/admin-filters-post.php

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