| @@ -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,12 +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 | - protected $links; | |
| 16 | 9 | |
| 17 | 10 | /** |
| 18 | 11 | * Constructor: setups filters and actions |
| 19 | 12 | * |
| @@ -23,10 +16,8 @@ | ||
| 23 | 16 | */ |
| 24 | 17 | public function __construct( &$polylang ) { |
| 25 | 18 | parent::__construct( $polylang ); |
| 26 | 19 | |
| 27 | - $this->links = &$polylang->links; | |
| 28 | - | |
| 29 | 20 | // Removes the editor and the template select dropdown for pages for posts |
| 30 | 21 | add_filter( 'use_block_editor_for_post', array( $this, 'use_block_editor_for_post' ), 10, 2 ); // Since WP 5.0 |
| 31 | 22 | add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 10, 2 ); |
| 32 | 23 | |
| @@ -35,12 +26,14 @@ | ||
| 35 | 26 | |
| 36 | 27 | // Refresh language cache when a static front page has been translated |
| 37 | 28 | add_action( 'pll_save_post', array( $this, 'pll_save_post' ), 10, 3 ); |
| 38 | 29 | |
| 30 | + // Checks if chosen page on front is translated | |
| 31 | + add_filter( 'pre_update_option_page_on_front', array( $this, 'update_page_on_front' ), 10, 2 ); | |
| 32 | + add_filter( 'customize_validate_page_on_front', array( $this, 'customize_validate_page_on_front' ), 10, 2 ); | |
| 33 | + | |
| 39 | 34 | // Prevents WP resetting the option |
| 40 | 35 | 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 | 36 | } |
| 44 | 37 | |
| 45 | 38 | /** |
| 46 | 39 | * Don't use the block editor for the translations of the pages for posts |
| @@ -63,16 +56,15 @@ | ||
| 63 | 56 | return $use_block_editor; |
| 64 | 57 | } |
| 65 | 58 | |
| 66 | 59 | /** |
| 67 | - * Removes the editor for the translations of the pages for posts. | |
| 68 | - * Removes the page template select dropdown in page attributes metabox too. | |
| 60 | + * Removes the editor for translations of the pages for posts | |
| 61 | + * Removes the page template select dropdown in page attributes metabox too | |
| 69 | 62 | * |
| 70 | 63 | * @since 2.2.2 |
| 71 | 64 | * |
| 72 | - * @param string $post_type Current post type. | |
| 73 | - * @param WP_Post $post Current post. | |
| 74 | - * @return void | |
| 65 | + * @param string $post_type Current post type | |
| 66 | + * @param object $post Current post | |
| 75 | 67 | */ |
| 76 | 68 | public function add_meta_boxes( $post_type, $post ) { |
| 77 | 69 | if ( 'page' === $post_type ) { |
| 78 | 70 | add_filter( 'option_page_for_posts', array( $this, 'translate_page_for_posts' ) ); |
| @@ -84,23 +76,23 @@ | ||
| 84 | 76 | } |
| 85 | 77 | } |
| 86 | 78 | |
| 87 | 79 | /** |
| 88 | - * Adds post state for translations of the front page and posts page. | |
| 80 | + * Add post state for translations of the front page and posts page | |
| 89 | 81 | * |
| 90 | 82 | * @since 1.8 |
| 91 | 83 | * |
| 92 | - * @param string[] $post_states An array of post display states. | |
| 93 | - * @param WP_Post $post The current post object. | |
| 94 | - * @return string[] | |
| 84 | + * @param array $post_states | |
| 85 | + * @param object $post | |
| 86 | + * @return array | |
| 95 | 87 | */ |
| 96 | 88 | public function display_post_states( $post_states, $post ) { |
| 97 | 89 | if ( in_array( $post->ID, $this->model->get_languages_list( array( 'fields' => 'page_on_front' ) ) ) ) { |
| 98 | - $post_states['page_on_front'] = __( 'Front Page', 'polylang' ); | |
| 90 | + $post_states['page_on_front'] = __( 'Front Page' ); | |
| 99 | 91 | } |
| 100 | 92 | |
| 101 | 93 | if ( in_array( $post->ID, $this->model->get_languages_list( array( 'fields' => 'page_for_posts' ) ) ) ) { |
| 102 | - $post_states['page_for_posts'] = __( 'Posts Page', 'polylang' ); | |
| 94 | + $post_states['page_for_posts'] = __( 'Posts Page' ); | |
| 103 | 95 | } |
| 104 | 96 | |
| 105 | 97 | return $post_states; |
| 106 | 98 | } |
| @@ -105,16 +97,15 @@ | ||
| 105 | 97 | return $post_states; |
| 106 | 98 | } |
| 107 | 99 | |
| 108 | 100 | /** |
| 109 | - * Refreshes the language cache when a static front page has been translated. | |
| 101 | + * Refresh language cache when a static front page has been translated | |
| 110 | 102 | * |
| 111 | 103 | * @since 1.8 |
| 112 | 104 | * |
| 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 | |
| 105 | + * @param int $post_id Not used | |
| 106 | + * @param object $post Not used | |
| 107 | + * @param array $translations | |
| 117 | 108 | */ |
| 118 | 109 | public function pll_save_post( $post_id, $post, $translations ) { |
| 119 | 110 | if ( in_array( $this->page_on_front, $translations ) ) { |
| 120 | 111 | $this->model->clean_languages_cache(); |
| @@ -121,78 +112,76 @@ | ||
| 121 | 112 | } |
| 122 | 113 | } |
| 123 | 114 | |
| 124 | 115 | /** |
| 125 | - * Prevents WP resetting the option if the admin language filter is active for a language with no pages | |
| 116 | + * Checks if a page is translated in all languages | |
| 126 | 117 | * |
| 127 | - * @since 1.9.3 | |
| 118 | + * @since 2.2 | |
| 128 | 119 | * |
| 129 | - * @param string $value | |
| 130 | - * @param string $old_value | |
| 131 | - * @return string | |
| 120 | + * @param int $page_id | |
| 121 | + * @return bool | |
| 132 | 122 | */ |
| 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; | |
| 123 | + protected function is_page_translated( $page_id ) { | |
| 124 | + if ( $page_id ) { | |
| 125 | + $translations = count( $this->model->post->get_translations( $page_id ) ); | |
| 126 | + $languages = count( $this->model->get_languages_list() ); | |
| 127 | + | |
| 128 | + if ( $languages > 1 && $translations != $languages ) { | |
| 129 | + return false; | |
| 130 | + } | |
| 136 | 131 | } |
| 137 | - return $value; | |
| 132 | + | |
| 133 | + return true; | |
| 138 | 134 | } |
| 139 | 135 | |
| 140 | 136 | /** |
| 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. | |
| 137 | + * Prevents choosing an untranslated static front page | |
| 138 | + * Displays an error message | |
| 144 | 139 | * |
| 145 | - * @since 2.6 | |
| 140 | + * @since 1.6 | |
| 146 | 141 | * |
| 147 | - * @return void | |
| 142 | + * @param int $page_id New page on front page id | |
| 143 | + * @param int $old_id Old page on front page_id | |
| 144 | + * @return int | |
| 148 | 145 | */ |
| 149 | - public function notice_must_translate() { | |
| 150 | - $screen = get_current_screen(); | |
| 146 | + public function update_page_on_front( $page_id, $old_id ) { | |
| 147 | + if ( ! $this->is_page_translated( $page_id ) ) { | |
| 148 | + $page_id = $old_id; | |
| 149 | + add_settings_error( 'reading', 'pll_page_on_front_error', __( 'The chosen static front page must be translated in all languages.', 'polylang' ) ); | |
| 150 | + } | |
| 151 | 151 | |
| 152 | - if ( ! empty( $screen ) && ( 'toplevel_page_mlang' === $screen->id || 'edit-page' === $screen->id ) ) { | |
| 153 | - $message = $this->get_must_translate_message(); | |
| 152 | + return $page_id; | |
| 153 | + } | |
| 154 | 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 | - } | |
| 155 | + /** | |
| 156 | + * Displays an error message in the customizer when choosing an untranslated static front page | |
| 157 | + * | |
| 158 | + * @since 2.2 | |
| 159 | + * | |
| 160 | + * @param object $validity WP_Error object | |
| 161 | + * @param int $page_id New page on front page id | |
| 162 | + * @return object | |
| 163 | + */ | |
| 164 | + public function customize_validate_page_on_front( $validity, $page_id ) { | |
| 165 | + if ( ! $this->is_page_translated( $page_id ) ) { | |
| 166 | + return new WP_Error( 'pll_page_on_front_error', __( 'The chosen static front page must be translated in all languages.', 'polylang' ) ); | |
| 161 | 167 | } |
| 168 | + | |
| 169 | + return $validity; | |
| 162 | 170 | } |
| 163 | 171 | |
| 164 | 172 | /** |
| 165 | - * Returns the message asking to translate the static front page in all languages. | |
| 173 | + * Prevents WP resetting the option if the admin language filter is active for a language with no pages | |
| 166 | 174 | * |
| 167 | - * @since 2.8 | |
| 175 | + * @since 1.9.3 | |
| 168 | 176 | * |
| 177 | + * @param string $value | |
| 178 | + * @param string $old_value | |
| 169 | 179 | * @return string |
| 170 | 180 | */ |
| 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 | - } | |
| 181 | + public function update_show_on_front( $value, $old_value ) { | |
| 182 | + if ( ! empty( $GLOBALS['pagenow'] ) && 'options-reading.php' === $GLOBALS['pagenow'] && 'posts' === $value && ! get_pages() && get_pages( array( 'lang' => '' ) ) ) { | |
| 183 | + $value = $old_value; | |
| 194 | 184 | } |
| 195 | - | |
| 196 | - return $message; | |
| 185 | + return $value; | |
| 197 | 186 | } |
| 198 | 187 | } |