| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Base class to manage the static front page and the page for posts |
| 8 |
* |
| 9 |
* @since 1.8 |
| 10 |
*/ |
| 11 |
class PLL_Static_Pages { |
| 12 |
/** |
| 13 |
* Id of the page on front. |
| 14 |
* |
| 15 |
* @var int |
| 16 |
*/ |
| 17 |
public $page_on_front; |
| 18 |
|
| 19 |
/** |
| 20 |
* Id of the page for posts. |
| 21 |
* |
| 22 |
* @var int |
| 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 |
| 42 |
*/ |
| 43 |
protected $curlang; |
| 44 |
|
| 45 |
/** |
| 46 |
* Constructor: setups filters and actions |
| 47 |
* |
| 48 |
* @since 1.8 |
| 49 |
* |
| 50 |
* @param object $polylang |
| 51 |
*/ |
| 52 |
public function __construct( &$polylang ) { |
| 53 |
$this->model = &$polylang->model; |
| 54 |
$this->options = &$polylang->options; |
| 55 |
$this->curlang = &$polylang->curlang; |
| 56 |
|
| 57 |
$this->init(); |
| 58 |
|
| 59 |
// Modifies the page link in case the front page is not in the default language |
| 60 |
add_filter( 'page_link', array( $this, 'page_link' ), 20, 2 ); |
| 61 |
|
| 62 |
// Clean the languages cache when editing page of front, page for posts |
| 63 |
add_action( 'update_option_show_on_front', array( $this->model, 'clean_languages_cache' ) ); |
| 64 |
add_action( 'update_option_page_on_front', array( $this->model, 'clean_languages_cache' ) ); |
| 65 |
add_action( 'update_option_page_for_posts', array( $this->model, 'clean_languages_cache' ) ); |
| 66 |
|
| 67 |
// Refresh rewrite rules when the page on front is modified |
| 68 |
add_action( 'update_option_page_on_front', 'flush_rewrite_rules' ); |
| 69 |
|
| 70 |
// OEmbed |
| 71 |
add_filter( 'oembed_request_post_id', array( $this, 'oembed_request_post_id' ), 10, 2 ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Stores the page on front and page for posts ids |
| 76 |
* |
| 77 |
* @since 1.8 |
| 78 |
* |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
public function init() { |
| 82 |
if ( 'page' == get_option( 'show_on_front' ) ) { |
| 83 |
$this->page_on_front = get_option( 'page_on_front' ); |
| 84 |
$this->page_for_posts = get_option( 'page_for_posts' ); |
| 85 |
} else { |
| 86 |
$this->page_on_front = 0; |
| 87 |
$this->page_for_posts = 0; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Modifies the page link in case the front page is not in the default language |
| 93 |
* |
| 94 |
* @since 0.7.2 |
| 95 |
* |
| 96 |
* @param string $link link to the page |
| 97 |
* @param int $id post id of the page |
| 98 |
* @return string modified link |
| 99 |
*/ |
| 100 |
public function page_link( $link, $id ) { |
| 101 |
$lang = $this->model->post->get_language( $id ); |
| 102 |
|
| 103 |
if ( $lang && $id == $lang->page_on_front ) { |
| 104 |
return $lang->home_url; |
| 105 |
} |
| 106 |
return $link; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Adds page_on_front and page_for_posts properties to the language objects. |
| 111 |
* |
| 112 |
* @since 1.8 |
| 113 |
* |
| 114 |
* @param PLL_Language[] $languages The list of languages. |
| 115 |
* @param PLL_Model $model The instance of PLL_Model. |
| 116 |
* @return PLL_Language[] |
| 117 |
*/ |
| 118 |
public static function pll_languages_list( $languages, $model ) { |
| 119 |
if ( 'page' === get_option( 'show_on_front' ) ) { |
| 120 |
foreach ( $languages as $k => $language ) { |
| 121 |
$languages[ $k ]->page_on_front = $model->post->get( get_option( 'page_on_front' ), $language ); |
| 122 |
$languages[ $k ]->page_for_posts = $model->post->get( get_option( 'page_for_posts' ), $language ); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return $languages; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Translates page for posts |
| 131 |
* |
| 132 |
* @since 1.8 |
| 133 |
* |
| 134 |
* @param int $v page for posts page id |
| 135 |
* @return int |
| 136 |
*/ |
| 137 |
public function translate_page_for_posts( $v ) { |
| 138 |
// Don't attempt to translate in a 'switch_blog' action as there is a risk to call this function while initializing the languages cache |
| 139 |
return isset( $this->curlang->page_for_posts ) && ( $this->curlang->page_for_posts ) && ! doing_action( 'switch_blog' ) ? $this->curlang->page_for_posts : $v; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Fixes the oembed for the translated static front page |
| 144 |
* when the language page is redirected to the front page |
| 145 |
* |
| 146 |
* @since 2.6 |
| 147 |
* |
| 148 |
* @param int $post_id The post ID. |
| 149 |
* @param string $url The requested URL. |
| 150 |
* @return int |
| 151 |
*/ |
| 152 |
public function oembed_request_post_id( $post_id, $url ) { |
| 153 |
foreach ( $this->model->get_languages_list() as $lang ) { |
| 154 |
if ( trailingslashit( $url ) === trailingslashit( $lang->home_url ) ) { |
| 155 |
$post_id = $lang->page_on_front; |
| 156 |
} |
| 157 |
} |
| 158 |
return $post_id; |
| 159 |
} |
| 160 |
} |
| 161 |
|