| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* base class to manage the static front page and the page for posts |
| 5 |
* |
| 6 |
* @since 1.8 |
| 7 |
*/ |
| 8 |
abstract class PLL_Static_Pages { |
| 9 |
public $model, $options; |
| 10 |
public $page_on_front, $page_for_posts; |
| 11 |
|
| 12 |
/** |
| 13 |
* constructor: setups filters and actions |
| 14 |
* |
| 15 |
* @since 1.8 |
| 16 |
* |
| 17 |
* @param object $polylang |
| 18 |
*/ |
| 19 |
public function __construct( &$polylang ) { |
| 20 |
$this->model = &$polylang->model; |
| 21 |
$this->options = &$polylang->options; |
| 22 |
$this->curlang = &$polylang->curlang; |
| 23 |
|
| 24 |
$this->init(); |
| 25 |
|
| 26 |
// Modifies the page link in case the front page is not in the default language |
| 27 |
add_filter( 'page_link', array( $this, 'page_link' ), 20, 2 ); |
| 28 |
|
| 29 |
// clean the languages cache when editing page of front, page for posts |
| 30 |
add_action( 'update_option_show_on_front', array( $this->model, 'clean_languages_cache' ) ); |
| 31 |
add_action( 'update_option_page_on_front', array( $this->model, 'clean_languages_cache' ) ); |
| 32 |
add_action( 'update_option_page_for_posts', array( $this->model, 'clean_languages_cache' ) ); |
| 33 |
|
| 34 |
// refresh rewrite rules when the page on front is modified |
| 35 |
add_action( 'update_option_page_on_front', 'flush_rewrite_rules' ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* stores the page on front and page for posts ids |
| 40 |
* |
| 41 |
* @since 1.8 |
| 42 |
*/ |
| 43 |
public function init() { |
| 44 |
if ( 'page' == get_option( 'show_on_front' ) ) { |
| 45 |
$this->page_on_front = get_option( 'page_on_front' ); |
| 46 |
$this->page_for_posts = get_option( 'page_for_posts' ); |
| 47 |
} |
| 48 |
|
| 49 |
else { |
| 50 |
$this->page_on_front = 0; |
| 51 |
$this->page_for_posts = 0; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Modifies the page link in case the front page is not in the default language |
| 57 |
* |
| 58 |
* @since 0.7.2 |
| 59 |
* |
| 60 |
* @param string $link link to the page |
| 61 |
* @param int $id post id of the page |
| 62 |
* @return string modified link |
| 63 |
*/ |
| 64 |
public function page_link( $link, $id ) { |
| 65 |
if ( ( $lang = $this->model->post->get_language( $id ) ) && $id == $lang->page_on_front ) { |
| 66 |
return $lang->home_url; |
| 67 |
} |
| 68 |
return $link; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* adds page_on_front and page_for_posts properties to the language objects |
| 73 |
* |
| 74 |
* @since 1.8 |
| 75 |
* |
| 76 |
* @param array $languages |
| 77 |
* @param object $model |
| 78 |
*/ |
| 79 |
public static function pll_languages_list( $languages, $model ) { |
| 80 |
if ( 'page' === get_option( 'show_on_front' ) ) { |
| 81 |
foreach ( $languages as $k => $language ) { |
| 82 |
$languages[ $k ]->page_on_front = $model->post->get( get_option( 'page_on_front' ), $language ); |
| 83 |
$languages[ $k ]->page_for_posts = $model->post->get( get_option( 'page_for_posts' ), $language ); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
return $languages; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Translates page for posts |
| 92 |
* |
| 93 |
* @since 1.8 |
| 94 |
* |
| 95 |
* @param int $v page for posts page id |
| 96 |
* @return int |
| 97 |
*/ |
| 98 |
public function translate_page_for_posts( $v ) { |
| 99 |
// Returns the current page if there is no translation to avoid ugly notices |
| 100 |
return isset( $this->curlang->page_for_posts ) ? $this->curlang->page_for_posts : $v; |
| 101 |
} |
| 102 |
} |
| 103 |
|