PluginProbe
Polylang / 2.8.1
Polylang v2.8.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 2.8.1, at admin/admin-filters-post.php

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