PluginProbe
Polylang / 3.2
Polylang v3.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-static-pages.php

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

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