| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Manages the static front page and the page for posts on admin side |
| 5 |
* |
| 6 |
* @since 1.8 |
| 7 |
*/ |
| 8 |
class PLL_Admin_Static_Pages extends PLL_Static_Pages { |
| 9 |
|
| 10 |
/** |
| 11 |
* Constructor: setups filters and actions |
| 12 |
* |
| 13 |
* @since 1.8 |
| 14 |
* |
| 15 |
* @param object $polylang |
| 16 |
*/ |
| 17 |
public function __construct( &$polylang ) { |
| 18 |
parent::__construct( $polylang ); |
| 19 |
|
| 20 |
// Removes the editor and the template select dropdown for pages for posts |
| 21 |
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 10, 2 ); |
| 22 |
|
| 23 |
// Add post state for translations of the front page and posts page |
| 24 |
add_filter( 'display_post_states', array( $this, 'display_post_states' ), 10, 2 ); |
| 25 |
|
| 26 |
// Refresh language cache when a static front page has been translated |
| 27 |
add_action( 'pll_save_post', array( $this, 'pll_save_post' ), 10, 3 ); |
| 28 |
|
| 29 |
// Checks if chosen page on front is translated |
| 30 |
add_filter( 'pre_update_option_page_on_front', array( $this, 'update_page_on_front' ), 10, 2 ); |
| 31 |
add_filter( 'customize_validate_page_on_front', array( $this, 'customize_validate_page_on_front' ), 10, 2 ); |
| 32 |
|
| 33 |
// Prevents WP resetting the option |
| 34 |
add_filter( 'pre_update_option_show_on_front', array( $this, 'update_show_on_front' ), 10, 2 ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Removes the editor for translations of the pages for posts |
| 39 |
* Removes the page template select dropdown in page attributes metabox too |
| 40 |
* |
| 41 |
* @since 2.2.2 |
| 42 |
* |
| 43 |
* @param string $post_type Current post type |
| 44 |
* @param object $post Current post |
| 45 |
*/ |
| 46 |
function add_meta_boxes( $post_type, $post ) { |
| 47 |
if ( 'page' === $post_type ) { |
| 48 |
add_filter( 'option_page_for_posts', array( $this, 'translate_page_for_posts' ) ); |
| 49 |
|
| 50 |
if ( ( get_option( 'page_for_posts' ) == $post->ID ) && empty( $post->post_content ) ) { |
| 51 |
add_action( 'edit_form_after_title', '_wp_posts_page_notice' ); |
| 52 |
remove_post_type_support( $post_type, 'editor' ); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Add post state for translations of the front page and posts page |
| 59 |
* |
| 60 |
* @since 1.8 |
| 61 |
* |
| 62 |
* @param array $post_states |
| 63 |
* @param object $post |
| 64 |
* @return array |
| 65 |
*/ |
| 66 |
public function display_post_states( $post_states, $post ) { |
| 67 |
if ( in_array( $post->ID, $this->model->get_languages_list( array( 'fields' => 'page_on_front' ) ) ) ) { |
| 68 |
$post_states['page_on_front'] = __( 'Front Page' ); |
| 69 |
} |
| 70 |
|
| 71 |
if ( in_array( $post->ID, $this->model->get_languages_list( array( 'fields' => 'page_for_posts' ) ) ) ) { |
| 72 |
$post_states['page_for_posts'] = __( 'Posts Page' ); |
| 73 |
} |
| 74 |
|
| 75 |
return $post_states; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Refresh language cache when a static front page has been translated |
| 80 |
* |
| 81 |
* @since 1.8 |
| 82 |
* |
| 83 |
* @param int $post_id Not used |
| 84 |
* @param object $post Not used |
| 85 |
* @param array $translations |
| 86 |
*/ |
| 87 |
public function pll_save_post( $post_id, $post, $translations ) { |
| 88 |
if ( in_array( $this->page_on_front, $translations ) ) { |
| 89 |
$this->model->clean_languages_cache(); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Checks if a page is translated in all languages |
| 95 |
* |
| 96 |
* @since 2.2 |
| 97 |
* |
| 98 |
* @param int $page_id |
| 99 |
* @return bool |
| 100 |
*/ |
| 101 |
protected function is_page_translated( $page_id ) { |
| 102 |
if ( $page_id ) { |
| 103 |
$translations = count( $this->model->post->get_translations( $page_id ) ); |
| 104 |
$languages = count( $this->model->get_languages_list() ); |
| 105 |
|
| 106 |
if ( $languages > 1 && $translations != $languages ) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Prevents choosing an untranslated static front page |
| 116 |
* Displays an error message |
| 117 |
* |
| 118 |
* @since 1.6 |
| 119 |
* |
| 120 |
* @param int $page_id New page on front page id |
| 121 |
* @param int $old_id Old page on front page_id |
| 122 |
* @return int |
| 123 |
*/ |
| 124 |
public function update_page_on_front( $page_id, $old_id ) { |
| 125 |
if ( ! $this->is_page_translated( $page_id ) ) { |
| 126 |
$page_id = $old_id; |
| 127 |
add_settings_error( 'reading', 'pll_page_on_front_error', __( 'The chosen static front page must be translated in all languages.', 'polylang' ) ); |
| 128 |
} |
| 129 |
|
| 130 |
return $page_id; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Displays an error message in the customizer when choosing an untranslated static front page |
| 135 |
* |
| 136 |
* @since 2.2 |
| 137 |
* |
| 138 |
* @param object $validity WP_Error object |
| 139 |
* @param int $page_id New page on front page id |
| 140 |
* @return object |
| 141 |
*/ |
| 142 |
public function customize_validate_page_on_front( $validity, $page_id ) { |
| 143 |
if ( ! $this->is_page_translated( $page_id ) ) { |
| 144 |
return new WP_Error( 'pll_page_on_front_error', __( 'The chosen static front page must be translated in all languages.', 'polylang' ) ); |
| 145 |
} |
| 146 |
|
| 147 |
return $validity; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Prevents WP resetting the option if the admin language filter is active for a language with no pages |
| 152 |
* |
| 153 |
* @since 1.9.3 |
| 154 |
* |
| 155 |
* @param string $value |
| 156 |
* @param string $old_value |
| 157 |
* @return string |
| 158 |
*/ |
| 159 |
public function update_show_on_front( $value, $old_value ) { |
| 160 |
if ( ! empty( $GLOBALS['pagenow'] ) && 'options-reading.php' === $GLOBALS['pagenow'] && 'posts' === $value && ! get_pages() && get_pages( array( 'lang' => '' ) ) ) { |
| 161 |
$value = $old_value; |
| 162 |
} |
| 163 |
return $value; |
| 164 |
} |
| 165 |
} |
| 166 |
|