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.php

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

149 lines 4.9 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 * Setup miscellaneous admin filters as well as filters common to admin and frontend
8 *
9 * @since 1.2
10 */
11 class PLL_Admin_Filters extends PLL_Filters {
12
13 /**
14 * Constructor: setups filters and actions.
15 *
16 * @since 1.2
17 *
18 * @param PLL_Admin $polylang The Polylang object.
19 */
20 public function __construct( &$polylang ) {
21 parent::__construct( $polylang );
22
23 // Language management for users
24 add_action( 'personal_options_update', array( $this, 'personal_options_update' ) );
25 add_action( 'edit_user_profile_update', array( $this, 'personal_options_update' ) );
26 add_action( 'personal_options', array( $this, 'user_profile_enqueue_scripts' ) );
27
28 // Upgrades plugins and themes translations files
29 add_filter( 'themes_update_check_locales', array( $this, 'update_check_locales' ) );
30 add_filter( 'plugins_update_check_locales', array( $this, 'update_check_locales' ) );
31
32 add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
33
34 // Add post state for translations of the privacy policy page
35 add_filter( 'display_post_states', array( $this, 'display_post_states' ), 10, 2 );
36 }
37
38 /**
39 * Updates the user biographies.
40 *
41 * @since 0.4
42 *
43 * @param int $user_id User ID.
44 * @return void
45 */
46 public function personal_options_update( $user_id ) {
47 // Biography translations
48 foreach ( $this->model->get_languages_list() as $lang ) {
49 $meta = $lang->is_default ? 'description' : 'description_' . $lang->slug;
50 $description = empty( $_POST[ 'description_' . $lang->slug ] ) ? '' : trim( $_POST[ 'description_' . $lang->slug ] ); // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput
51
52 /** This filter is documented in wp-includes/user.php */
53 $description = apply_filters( 'pre_user_description', $description ); // Applies WP default filter wp_filter_kses
54 update_user_meta( $user_id, $meta, $description );
55 }
56 }
57
58 /**
59 * Enqueues scripts for the multilingual biography on the user's profile admin page.
60 *
61 * @since 3.8.4
62 *
63 * @param WP_User $profileuser The current WP_User object.
64 * @return void
65 */
66 public function user_profile_enqueue_scripts( $profileuser ): void {
67 $screen = get_current_screen();
68
69 if ( empty( $screen ) || ! in_array( $screen->base, array( 'profile', 'user-edit' ), true ) ) {
70 return;
71 }
72
73 if ( ! $this->model->has_languages() ) {
74 return;
75 }
76
77 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
78 $data = array();
79
80 wp_enqueue_script( 'pll_user', plugins_url( "/js/build/user{$suffix}.js", POLYLANG_ROOT_FILE ), array(), POLYLANG_VERSION, array( 'in_footer' => true ) );
81
82 foreach ( $this->model->languages->get_list() as $lang ) {
83 $meta = $lang->is_default ? 'description' : "description_{$lang->slug}";
84 $description = get_user_meta( $profileuser->ID, $meta, true );
85 $description = is_string( $description ) ? $description : '';
86
87 $data[] = array(
88 'slug' => esc_attr( $lang->slug ),
89 'name' => esc_html( $lang->name ),
90 'lang' => esc_attr( $lang->get_locale( 'display' ) ),
91 'direction' => $lang->is_rtl ? 'rtl' : 'ltr',
92 'flag' => PLL_Language::get_flag_information( $lang->flag_code ),
93 'description' => sanitize_user_field( 'description', $description, $profileuser->ID, 'edit' ),
94 );
95 }
96
97 $script = sprintf( 'const pllDescriptionData = %s;', wp_json_encode( $data ) );
98 wp_add_inline_script( 'pll_user', $script, 'before' );
99 }
100
101 /**
102 * Allows to update translations files for plugins and themes.
103 *
104 * @since 1.6
105 *
106 * @param string[] $locales List of locales to update for plugins and themes.
107 * @return string[]
108 */
109 public function update_check_locales( $locales ) {
110 return array_merge( $locales, $this->model->get_languages_list( array( 'fields' => 'locale' ) ) );
111 }
112
113 /**
114 * Adds custom classes to the body
115 *
116 * @since 2.2 Adds a text direction dependent class to the body.
117 * @since 3.4 Adds a language dependent class to the body.
118 *
119 * @param string $classes Space-separated list of CSS classes.
120 * @return string
121 */
122 public function admin_body_class( $classes ) {
123 if ( ! empty( $this->curlang ) ) {
124 $classes .= ' pll-dir-' . ( $this->curlang->is_rtl ? 'rtl' : 'ltr' );
125 $classes .= ' pll-lang-' . $this->curlang->slug;
126 }
127 return $classes;
128 }
129
130 /**
131 * Adds post state for translations of the privacy policy page.
132 *
133 * @since 2.7
134 *
135 * @param string[] $post_states An array of post display states.
136 * @param WP_Post $post The current post object.
137 * @return string[]
138 */
139 public function display_post_states( $post_states, $post ) {
140 $page_for_privacy_policy = get_option( 'wp_page_for_privacy_policy' );
141
142 if ( $page_for_privacy_policy && in_array( $post->ID, $this->model->post->get_translations( $page_for_privacy_policy ) ) ) {
143 $post_states['page_for_privacy_policy'] = __( 'Privacy Policy Page', 'polylang' );
144 }
145
146 return $post_states;
147 }
148 }
149