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