| @@ -8,12 +8,42 @@ | ||
| 8 | 8 | * |
| 9 | 9 | * @since 1.8 |
| 10 | 10 | */ |
| 11 | 11 | class PLL_Static_Pages { |
| 12 | - public $model, $options; | |
| 13 | - public $page_on_front, $page_for_posts; | |
| 12 | + /** | |
| 13 | + * Id of the page on front. | |
| 14 | + * | |
| 15 | + * @var int|null | |
| 16 | + */ | |
| 17 | + public $page_on_front; | |
| 14 | 18 | |
| 15 | 19 | /** |
| 20 | + * Id of the page for posts. | |
| 21 | + * | |
| 22 | + * @var int|null | |
| 23 | + */ | |
| 24 | + public $page_for_posts; | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * Stores the plugin options. | |
| 28 | + * | |
| 29 | + * @var array | |
| 30 | + */ | |
| 31 | + protected $options; | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * @var PLL_Model | |
| 35 | + */ | |
| 36 | + protected $model; | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * Current language. | |
| 40 | + * | |
| 41 | + * @var PLL_Language|null | |
| 42 | + */ | |
| 43 | + protected $curlang; | |
| 44 | + | |
| 45 | + /** | |
| 16 | 46 | * Constructor: setups filters and actions |
| 17 | 47 | * |
| 18 | 48 | * @since 1.8 |
| 19 | 49 | * |
| @@ -25,8 +55,10 @@ | ||
| 25 | 55 | $this->curlang = &$polylang->curlang; |
| 26 | 56 | |
| 27 | 57 | $this->init(); |
| 28 | 58 | |
| 59 | + add_action( 'pll_language_defined', array( $this, 'pll_language_defined' ) ); | |
| 60 | + | |
| 29 | 61 | // Modifies the page link in case the front page is not in the default language |
| 30 | 62 | add_filter( 'page_link', array( $this, 'page_link' ), 20, 2 ); |
| 31 | 63 | |
| 32 | 64 | // Clean the languages cache when editing page of front, page for posts |
| @@ -44,8 +76,10 @@ | ||
| 44 | 76 | /** |
| 45 | 77 | * Stores the page on front and page for posts ids |
| 46 | 78 | * |
| 47 | 79 | * @since 1.8 |
| 80 | + * | |
| 81 | + * @return void | |
| 48 | 82 | */ |
| 49 | 83 | public function init() { |
| 50 | 84 | if ( 'page' == get_option( 'show_on_front' ) ) { |
| 51 | 85 | $this->page_on_front = get_option( 'page_on_front' ); |
| @@ -56,8 +90,21 @@ | ||
| 56 | 90 | } |
| 57 | 91 | } |
| 58 | 92 | |
| 59 | 93 | /** |
| 94 | + * Init the hooks that filter the "page on front" and "page for posts" options. | |
| 95 | + * | |
| 96 | + * @since 3.3 | |
| 97 | + * | |
| 98 | + * @return void | |
| 99 | + */ | |
| 100 | + public function pll_language_defined() { | |
| 101 | + // Translates page for posts and page on front. | |
| 102 | + add_filter( 'option_page_on_front', array( $this, 'translate_page_on_front' ) ); | |
| 103 | + add_filter( 'option_page_for_posts', array( $this, 'translate_page_for_posts' ) ); | |
| 104 | + } | |
| 105 | + | |
| 106 | + /** | |
| 60 | 107 | * Modifies the page link in case the front page is not in the default language |
| 61 | 108 | * |
| 62 | 109 | * @since 0.7.2 |
| 63 | 110 | * |
| @@ -74,14 +121,15 @@ | ||
| 74 | 121 | return $link; |
| 75 | 122 | } |
| 76 | 123 | |
| 77 | 124 | /** |
| 78 | - * Adds page_on_front and page_for_posts properties to the language objects | |
| 125 | + * Adds page_on_front and page_for_posts properties to the language objects. | |
| 79 | 126 | * |
| 80 | 127 | * @since 1.8 |
| 81 | 128 | * |
| 82 | - * @param array $languages | |
| 83 | - * @param object $model | |
| 129 | + * @param PLL_Language[] $languages The list of languages. | |
| 130 | + * @param PLL_Model $model The instance of PLL_Model. | |
| 131 | + * @return PLL_Language[] | |
| 84 | 132 | */ |
| 85 | 133 | public static function pll_languages_list( $languages, $model ) { |
| 86 | 134 | if ( 'page' === get_option( 'show_on_front' ) ) { |
| 87 | 135 | foreach ( $languages as $k => $language ) { |
| @@ -93,21 +141,35 @@ | ||
| 93 | 141 | return $languages; |
| 94 | 142 | } |
| 95 | 143 | |
| 96 | 144 | /** |
| 97 | - * Translates page for posts | |
| 145 | + * Translates the page on front option. | |
| 98 | 146 | * |
| 99 | 147 | * @since 1.8 |
| 148 | + * @since 3.3 Was previously defined in PLL_Frontend_Static_Pages. | |
| 100 | 149 | * |
| 101 | - * @param int $v page for posts page id | |
| 150 | + * @param int $page_id ID of the page on front. | |
| 102 | 151 | * @return int |
| 103 | 152 | */ |
| 104 | - public function translate_page_for_posts( $v ) { | |
| 105 | - // Don't attempt to translate in a 'switch_blog' action as there is a risk to call this function while initializing the languages cache | |
| 106 | - return isset( $this->curlang->page_for_posts ) && ! doing_action( 'switch_blog' ) ? $this->curlang->page_for_posts : $v; | |
| 153 | + public function translate_page_on_front( $page_id ) { | |
| 154 | + // Don't attempt to translate in a 'switch_blog' action as there is a risk to call this function while initializing the languages cache. | |
| 155 | + return ! empty( $this->curlang->page_on_front ) && ! doing_action( 'switch_blog' ) ? $this->curlang->page_on_front : $page_id; | |
| 107 | 156 | } |
| 108 | 157 | |
| 109 | 158 | /** |
| 159 | + * Translates the page for posts option. | |
| 160 | + * | |
| 161 | + * @since 1.8 | |
| 162 | + * | |
| 163 | + * @param int $page_id ID of the page for posts. | |
| 164 | + * @return int | |
| 165 | + */ | |
| 166 | + public function translate_page_for_posts( $page_id ) { | |
| 167 | + // Don't attempt to translate in a 'switch_blog' action as there is a risk to call this function while initializing the languages cache. | |
| 168 | + return ! empty( $this->curlang->page_for_posts ) && ! doing_action( 'switch_blog' ) ? $this->curlang->page_for_posts : $page_id; | |
| 169 | + } | |
| 170 | + | |
| 171 | + /** | |
| 110 | 172 | * Fixes the oembed for the translated static front page |
| 111 | 173 | * when the language page is redirected to the front page |
| 112 | 174 | * |
| 113 | 175 | * @since 2.6 |
| @@ -113,14 +175,16 @@ | ||
| 113 | 175 | * @since 2.6 |
| 114 | 176 | * |
| 115 | 177 | * @param int $post_id The post ID. |
| 116 | 178 | * @param string $url The requested URL. |
| 179 | + * @return int | |
| 117 | 180 | */ |
| 118 | 181 | public function oembed_request_post_id( $post_id, $url ) { |
| 119 | 182 | foreach ( $this->model->get_languages_list() as $lang ) { |
| 120 | - if ( trailingslashit( $url ) === trailingslashit( $lang->home_url ) ) { | |
| 121 | - $post_id = $lang->page_on_front; | |
| 183 | + if ( is_string( $lang->home_url ) && trailingslashit( $url ) === trailingslashit( $lang->home_url ) ) { | |
| 184 | + return (int) $lang->page_on_front; | |
| 122 | 185 | } |
| 123 | 186 | } |
| 187 | + | |
| 124 | 188 | return $post_id; |
| 125 | 189 | } |
| 126 | 190 | } |