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

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

153 lines 4.4 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|null
14 */
15 protected $links;
16
17 /**
18 * Constructor: setups filters and actions.
19 *
20 * @since 1.8
21 *
22 * @param object $polylang An array of attachment metadata.
23 */
24 public function __construct( &$polylang ) {
25 parent::__construct( $polylang );
26
27 $this->links = &$polylang->links;
28
29 // Add post state for translations of the front page and posts page
30 add_filter( 'display_post_states', array( $this, 'display_post_states' ), 10, 2 );
31
32 // Refreshes the language cache when a static front page or page for for posts has been translated.
33 add_action( 'pll_save_post', array( $this, 'pll_save_post' ), 10, 3 );
34
35 // Prevents WP resetting the option
36 add_filter( 'pre_update_option_show_on_front', array( $this, 'update_show_on_front' ), 10, 2 );
37
38 add_action( 'admin_notices', array( $this, 'notice_must_translate' ) );
39 }
40
41 /**
42 * Adds post state for translations of the front page and posts page.
43 *
44 * @since 1.8
45 *
46 * @param string[] $post_states An array of post display states.
47 * @param WP_Post $post The current post object.
48 * @return string[]
49 */
50 public function display_post_states( $post_states, $post ) {
51 if ( in_array( $post->ID, $this->model->get_languages_list( array( 'fields' => 'page_on_front' ) ) ) ) {
52 $post_states['page_on_front'] = __( 'Front Page', 'polylang' );
53 }
54
55 if ( in_array( $post->ID, $this->model->get_languages_list( array( 'fields' => 'page_for_posts' ) ) ) ) {
56 $post_states['page_for_posts'] = __( 'Posts Page', 'polylang' );
57 }
58
59 return $post_states;
60 }
61
62 /**
63 * Refreshes the language cache when a static front page or page for for posts has been translated.
64 *
65 * @since 1.8
66 *
67 * @param int $post_id Not used.
68 * @param WP_Post $post Not used.
69 * @param int[] $translations Translations of the post being saved.
70 * @return void
71 */
72 public function pll_save_post( $post_id, $post, $translations ) {
73 if ( in_array( $this->page_on_front, $translations ) || in_array( $this->page_for_posts, $translations ) ) {
74 $this->model->clean_languages_cache();
75 }
76 }
77
78 /**
79 * Prevents WP resetting the option if the admin language filter is active for a language with no pages.
80 *
81 * @since 1.9.3
82 *
83 * @param string $value The new, unserialized option value.
84 * @param string $old_value The old option value.
85 * @return string
86 */
87 public function update_show_on_front( $value, $old_value ) {
88 if ( ! empty( $GLOBALS['pagenow'] ) && 'options-reading.php' === $GLOBALS['pagenow'] && 'posts' === $value && ! get_pages() && get_pages( array( 'lang' => '' ) ) ) {
89 $value = $old_value;
90 }
91 return $value;
92 }
93
94 /**
95 * Add a notice to translate the static front page if it is not translated in all languages
96 * This is especially useful after a new language is created.
97 * The notice is not dismissible and displayed on the Languages pages and the list of pages.
98 *
99 * @since 2.6
100 *
101 * @return void
102 */
103 public function notice_must_translate() {
104 $screen = get_current_screen();
105
106 if ( ! empty( $screen ) && ( 'toplevel_page_mlang' === $screen->id || 'edit-page' === $screen->id ) ) {
107 $message = $this->get_must_translate_message();
108
109 if ( ! empty( $message ) ) {
110 printf(
111 '<div class="error"><p>%s</p></div>',
112 $message // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
113 );
114 }
115 }
116 }
117
118 /**
119 * Returns the message asking to translate the static front page in all languages.
120 *
121 * @since 2.8
122 *
123 * @return string
124 */
125 public function get_must_translate_message() {
126 $message = '';
127
128 if ( $this->page_on_front ) {
129 $untranslated = array();
130
131 foreach ( $this->model->get_languages_list() as $language ) {
132 if ( ! $this->model->post->get( $this->page_on_front, $language ) ) {
133 $untranslated[] = sprintf(
134 '<a href="%s">%s</a>',
135 esc_url( $this->links->get_new_post_translation_link( $this->page_on_front, $language ) ),
136 esc_html( $language->name )
137 );
138 }
139 }
140
141 if ( ! empty( $untranslated ) ) {
142 $message = sprintf(
143 /* translators: %s is a comma separated list of native language names */
144 esc_html__( 'You must translate your static front page in %s.', 'polylang' ),
145 implode( ', ', $untranslated ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
146 );
147 }
148 }
149
150 return $message;
151 }
152 }
153