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

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