PluginProbe
Polylang / 3.1.1
Polylang v3.1.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.1.1, at admin/admin-filters-post.php

214 lines 6.1 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
16 */
17 public $curlang;
18
19 /**
20 * Constructor: setups filters and actions
21 *
22 * @since 1.2
23 *
24 * @param object $polylang
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, 'object' ) ) {
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( $hierarchical_taxonomies, array( '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 $this->model->post->set_language( $post_id, $this->model->get_language( sanitize_key( $_POST['post_lang_choice'] ) ) );
149
150 if ( isset( $_POST['post_tr_lang'] ) ) {
151 $this->save_translations( $post_id, array_map( 'absint', $_POST['post_tr_lang'] ) );
152 }
153 }
154 }
155 }
156
157 /**
158 * Save language when bulk editing a post
159 *
160 * @since 2.3
161 *
162 * @return void
163 */
164 public function bulk_edit_posts() {
165 if ( isset( $_GET['bulk_edit'], $_GET['inline_lang_choice'], $_REQUEST['post'] ) && -1 !== $_GET['inline_lang_choice'] ) { // phpcs:ignore WordPress.Security.NonceVerification
166 check_admin_referer( 'bulk-posts' );
167
168 if ( $lang = $this->model->get_language( sanitize_key( $_GET['inline_lang_choice'] ) ) ) {
169 $post_ids = array_map( 'intval', (array) $_REQUEST['post'] );
170 foreach ( $post_ids as $post_id ) {
171 if ( current_user_can( 'edit_post', $post_id ) ) {
172 $this->model->post->update_language( $post_id, $lang );
173 }
174 }
175 }
176 }
177 }
178
179 /**
180 * Save language when inline editing a post
181 *
182 * @since 2.3
183 *
184 * @return void
185 */
186 public function inline_edit_post() {
187 check_admin_referer( 'inlineeditnonce', '_inline_edit' );
188
189 if ( isset( $_POST['post_ID'], $_POST['inline_lang_choice'] ) ) {
190 $post_id = (int) $_POST['post_ID'];
191 $lang = $this->model->get_language( sanitize_key( $_POST['inline_lang_choice'] ) );
192 if ( $post_id && $lang && current_user_can( 'edit_post', $post_id ) ) {
193 $this->model->post->update_language( $post_id, $lang );
194 }
195 }
196 }
197
198 /**
199 * Sets the language attribute and text direction for Tiny MCE
200 *
201 * @since 2.2
202 *
203 * @param array $mce_init TinyMCE config
204 * @return array
205 */
206 public function tiny_mce_before_init( $mce_init ) {
207 if ( ! empty( $this->curlang ) ) {
208 $mce_init['wp_lang_attr'] = $this->curlang->get_locale( 'display' );
209 $mce_init['directionality'] = $this->curlang->is_rtl ? 'rtl' : 'ltr';
210 }
211 return $mce_init;
212 }
213 }
214