| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * Manages the static front page and the page for posts on admin side |
| 8 | 5 | * |
| @@ -8,11 +5,8 @@ | ||
| 8 | 5 | * |
| 9 | 6 | * @since 1.8 |
| 10 | 7 | */ |
| 11 | 8 | class PLL_Admin_Static_Pages extends PLL_Static_Pages { |
| 12 | - /** | |
| 13 | - * @var PLL_Admin_Links | |
| 14 | - */ | |
| 15 | 9 | protected $links; |
| 16 | 10 | |
| 17 | 11 | /** |
| 18 | 12 | * Constructor: setups filters and actions |
| @@ -35,8 +29,12 @@ | ||
| 35 | 29 | |
| 36 | 30 | // Refresh language cache when a static front page has been translated |
| 37 | 31 | add_action( 'pll_save_post', array( $this, 'pll_save_post' ), 10, 3 ); |
| 38 | 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 | + | |
| 39 | 37 | // Prevents WP resetting the option |
| 40 | 38 | add_filter( 'pre_update_option_show_on_front', array( $this, 'update_show_on_front' ), 10, 2 ); |
| 41 | 39 | |
| 42 | 40 | add_action( 'admin_notices', array( $this, 'notice_must_translate' ) ); |
| @@ -63,16 +61,15 @@ | ||
| 63 | 61 | return $use_block_editor; |
| 64 | 62 | } |
| 65 | 63 | |
| 66 | 64 | /** |
| 67 | - * Removes the editor for the translations of the pages for posts. | |
| 68 | - * Removes the page template select dropdown in page attributes metabox too. | |
| 65 | + * Removes the editor for translations of the pages for posts | |
| 66 | + * Removes the page template select dropdown in page attributes metabox too | |
| 69 | 67 | * |
| 70 | 68 | * @since 2.2.2 |
| 71 | 69 | * |
| 72 | - * @param string $post_type Current post type. | |
| 73 | - * @param WP_Post $post Current post. | |
| 74 | - * @return void | |
| 70 | + * @param string $post_type Current post type | |
| 71 | + * @param object $post Current post | |
| 75 | 72 | */ |
| 76 | 73 | public function add_meta_boxes( $post_type, $post ) { |
| 77 | 74 | if ( 'page' === $post_type ) { |
| 78 | 75 | add_filter( 'option_page_for_posts', array( $this, 'translate_page_for_posts' ) ); |
| @@ -84,15 +81,15 @@ | ||
| 84 | 81 | } |
| 85 | 82 | } |
| 86 | 83 | |
| 87 | 84 | /** |
| 88 | - * Adds post state for translations of the front page and posts page. | |
| 85 | + * Add post state for translations of the front page and posts page | |
| 89 | 86 | * |
| 90 | 87 | * @since 1.8 |
| 91 | 88 | * |
| 92 | - * @param string[] $post_states An array of post display states. | |
| 93 | - * @param WP_Post $post The current post object. | |
| 94 | - * @return string[] | |
| 89 | + * @param array $post_states | |
| 90 | + * @param object $post | |
| 91 | + * @return array | |
| 95 | 92 | */ |
| 96 | 93 | public function display_post_states( $post_states, $post ) { |
| 97 | 94 | if ( in_array( $post->ID, $this->model->get_languages_list( array( 'fields' => 'page_on_front' ) ) ) ) { |
| 98 | 95 | $post_states['page_on_front'] = __( 'Front Page', 'polylang' ); |
| @@ -105,16 +102,15 @@ | ||
| 105 | 102 | return $post_states; |
| 106 | 103 | } |
| 107 | 104 | |
| 108 | 105 | /** |
| 109 | - * Refreshes the language cache when a static front page has been translated. | |
| 106 | + * Refresh language cache when a static front page has been translated | |
| 110 | 107 | * |
| 111 | 108 | * @since 1.8 |
| 112 | 109 | * |
| 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 | |
| 110 | + * @param int $post_id Not used | |
| 111 | + * @param object $post Not used | |
| 112 | + * @param array $translations | |
| 117 | 113 | */ |
| 118 | 114 | public function pll_save_post( $post_id, $post, $translations ) { |
| 119 | 115 | if ( in_array( $this->page_on_front, $translations ) ) { |
| 120 | 116 | $this->model->clean_languages_cache(); |
| @@ -121,8 +117,65 @@ | ||
| 121 | 117 | } |
| 122 | 118 | } |
| 123 | 119 | |
| 124 | 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 | + /** | |
| 125 | 178 | * Prevents WP resetting the option if the admin language filter is active for a language with no pages |
| 126 | 179 | * |
| 127 | 180 | * @since 1.9.3 |
| 128 | 181 | * |
| @@ -142,37 +195,13 @@ | ||
| 142 | 195 | * This is especially useful after a new language is created. |
| 143 | 196 | * The notice is not dismissible and displayed on the Languages pages and the list of pages. |
| 144 | 197 | * |
| 145 | 198 | * @since 2.6 |
| 146 | - * | |
| 147 | - * @return void | |
| 148 | 199 | */ |
| 149 | 200 | public function notice_must_translate() { |
| 150 | 201 | $screen = get_current_screen(); |
| 151 | 202 | |
| 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 ) { | |
| 203 | + if ( $this->page_on_front && ( 'toplevel_page_mlang' === $screen->id || 'edit-page' === $screen->id ) ) { | |
| 175 | 204 | $untranslated = array(); |
| 176 | 205 | |
| 177 | 206 | foreach ( $this->model->get_languages_list() as $language ) { |
| 178 | 207 | if ( ! $this->model->post->get( $this->page_on_front, $language ) ) { |
| @@ -184,15 +213,16 @@ | ||
| 184 | 213 | } |
| 185 | 214 | } |
| 186 | 215 | |
| 187 | 216 | 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 | |
| 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 | + ) | |
| 192 | 224 | ); |
| 193 | 225 | } |
| 194 | 226 | } |
| 195 | - | |
| 196 | - return $message; | |
| 197 | 227 | } |
| 198 | 228 | } |