PluginProbe
Polylang / 2.7.3
Polylang v2.7.3
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-static-pages.php

admin-static-pages.php in Polylang 2.7.3, at admin/admin-static-pages.php

229 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Manages the static front page and the page for posts on admin side
5 *
6 * @since 1.8
7 */
8 class PLL_Admin_Static_Pages extends PLL_Static_Pages {
9 protected $links;
10
11 /**
12 * Constructor: setups filters and actions
13 *
14 * @since 1.8
15 *
16 * @param object $polylang
17 */
18 public function __construct( &$polylang ) {
19 parent::__construct( $polylang );
20
21 $this->links = &$polylang->links;
22
23 // Removes the editor and the template select dropdown for pages for posts
24 add_filter( 'use_block_editor_for_post', array( $this, 'use_block_editor_for_post' ), 10, 2 ); // Since WP 5.0
25 add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 10, 2 );
26
27 // Add post state for translations of the front page and posts page
28 add_filter( 'display_post_states', array( $this, 'display_post_states' ), 10, 2 );
29
30 // Refresh language cache when a static front page has been translated
31 add_action( 'pll_save_post', array( $this, 'pll_save_post' ), 10, 3 );
32
33 // Checks if chosen page on front is translated
34 add_filter( 'pre_update_option_page_on_front', array( $this, 'update_page_on_front' ), 10, 2 );
35 add_filter( 'customize_validate_page_on_front', array( $this, 'customize_validate_page_on_front' ), 10, 2 );
36
37 // Prevents WP resetting the option
38 add_filter( 'pre_update_option_show_on_front', array( $this, 'update_show_on_front' ), 10, 2 );
39
40 add_action( 'admin_notices', array( $this, 'notice_must_translate' ) );
41 }
42
43 /**
44 * Don't use the block editor for the translations of the pages for posts
45 *
46 * @since 2.5
47 *
48 * @param bool $use_block_editor Whether the post can be edited or not.
49 * @param WP_Post $post The post being checked.
50 * @return bool
51 */
52 public function use_block_editor_for_post( $use_block_editor, $post ) {
53 if ( 'page' === $post->post_type ) {
54 add_filter( 'option_page_for_posts', array( $this, 'translate_page_for_posts' ) );
55
56 if ( ( get_option( 'page_for_posts' ) == $post->ID ) && empty( $post->post_content ) ) {
57 return false;
58 }
59 }
60
61 return $use_block_editor;
62 }
63
64 /**
65 * Removes the editor for translations of the pages for posts
66 * Removes the page template select dropdown in page attributes metabox too
67 *
68 * @since 2.2.2
69 *
70 * @param string $post_type Current post type
71 * @param object $post Current post
72 */
73 public function add_meta_boxes( $post_type, $post ) {
74 if ( 'page' === $post_type ) {
75 add_filter( 'option_page_for_posts', array( $this, 'translate_page_for_posts' ) );
76
77 if ( ( get_option( 'page_for_posts' ) == $post->ID ) && empty( $post->post_content ) ) {
78 add_action( 'edit_form_after_title', '_wp_posts_page_notice' );
79 remove_post_type_support( $post_type, 'editor' );
80 }
81 }
82 }
83
84 /**
85 * Add post state for translations of the front page and posts page
86 *
87 * @since 1.8
88 *
89 * @param array $post_states An array of post display states.
90 * @param object $post The current post object.
91 * @return array
92 */
93 public function display_post_states( $post_states, $post ) {
94 if ( in_array( $post->ID, $this->model->get_languages_list( array( 'fields' => 'page_on_front' ) ) ) ) {
95 $post_states['page_on_front'] = __( 'Front Page', 'polylang' );
96 }
97
98 if ( in_array( $post->ID, $this->model->get_languages_list( array( 'fields' => 'page_for_posts' ) ) ) ) {
99 $post_states['page_for_posts'] = __( 'Posts Page', 'polylang' );
100 }
101
102 return $post_states;
103 }
104
105 /**
106 * Refresh language cache when a static front page has been translated
107 *
108 * @since 1.8
109 *
110 * @param int $post_id Not used
111 * @param object $post Not used
112 * @param array $translations
113 */
114 public function pll_save_post( $post_id, $post, $translations ) {
115 if ( in_array( $this->page_on_front, $translations ) ) {
116 $this->model->clean_languages_cache();
117 }
118 }
119
120 /**
121 * Checks if a page is translated in all languages
122 *
123 * @since 2.2
124 *
125 * @param int $page_id
126 * @return bool
127 */
128 protected function is_page_translated( $page_id ) {
129 if ( $page_id ) {
130 $translations = count( $this->model->post->get_translations( $page_id ) );
131 $languages = count( $this->model->get_languages_list() );
132
133 if ( $languages > 1 && $translations != $languages ) {
134 return false;
135 }
136 }
137
138 return true;
139 }
140
141 /**
142 * Prevents choosing an untranslated static front page
143 * Displays an error message
144 *
145 * @since 1.6
146 *
147 * @param int $page_id New page on front page id
148 * @param int $old_id Old page on front page_id
149 * @return int
150 */
151 public function update_page_on_front( $page_id, $old_id ) {
152 if ( ! $this->is_page_translated( $page_id ) ) {
153 $page_id = $old_id;
154 add_settings_error( 'reading', 'pll_page_on_front_error', __( 'The chosen static front page must be translated in all languages.', 'polylang' ) );
155 }
156
157 return $page_id;
158 }
159
160 /**
161 * Displays an error message in the customizer when choosing an untranslated static front page
162 *
163 * @since 2.2
164 *
165 * @param object $validity WP_Error object
166 * @param int $page_id New page on front page id
167 * @return object
168 */
169 public function customize_validate_page_on_front( $validity, $page_id ) {
170 if ( ! $this->is_page_translated( $page_id ) ) {
171 return new WP_Error( 'pll_page_on_front_error', __( 'The chosen static front page must be translated in all languages.', 'polylang' ) );
172 }
173
174 return $validity;
175 }
176
177 /**
178 * Prevents WP resetting the option if the admin language filter is active for a language with no pages
179 *
180 * @since 1.9.3
181 *
182 * @param string $value
183 * @param string $old_value
184 * @return string
185 */
186 public function update_show_on_front( $value, $old_value ) {
187 if ( ! empty( $GLOBALS['pagenow'] ) && 'options-reading.php' === $GLOBALS['pagenow'] && 'posts' === $value && ! get_pages() && get_pages( array( 'lang' => '' ) ) ) {
188 $value = $old_value;
189 }
190 return $value;
191 }
192
193 /**
194 * Add a notice to translate the static front page if it is not translated in all languages
195 * This is especially useful after a new language is created.
196 * The notice is not dismissible and displayed on the Languages pages and the list of pages.
197 *
198 * @since 2.6
199 */
200 public function notice_must_translate() {
201 $screen = get_current_screen();
202
203 if ( $this->page_on_front && ( 'toplevel_page_mlang' === $screen->id || 'edit-page' === $screen->id ) ) {
204 $untranslated = array();
205
206 foreach ( $this->model->get_languages_list() as $language ) {
207 if ( ! $this->model->post->get( $this->page_on_front, $language ) ) {
208 $untranslated[] = sprintf(
209 '<a href="%s">%s</a>',
210 esc_url( $this->links->get_new_post_translation_link( $this->page_on_front, $language ) ),
211 esc_html( $language->name )
212 );
213 }
214 }
215
216 if ( ! empty( $untranslated ) ) {
217 printf(
218 '<div class="error"><p>%s</p></div>',
219 sprintf(
220 /* translators: %s is a comma separated list of native language names */
221 esc_html__( 'You must translate your static front page in %s.', 'polylang' ),
222 implode( ', ', $untranslated ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
223 )
224 );
225 }
226 }
227 }
228 }
229