| 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 |
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 |
// OEmbed |
| 38 |
add_filter( 'oembed_request_post_id', array( $this, 'oembed_request_post_id' ), 10, 2 ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Stores the page on front and page for posts ids |
| 43 |
* |
| 44 |
* @since 1.8 |
| 45 |
*/ |
| 46 |
public function init() { |
| 47 |
if ( 'page' == get_option( 'show_on_front' ) ) { |
| 48 |
$this->page_on_front = get_option( 'page_on_front' ); |
| 49 |
$this->page_for_posts = get_option( 'page_for_posts' ); |
| 50 |
} else { |
| 51 |
$this->page_on_front = 0; |
| 52 |
$this->page_for_posts = 0; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Modifies the page link in case the front page is not in the default language |
| 58 |
* |
| 59 |
* @since 0.7.2 |
| 60 |
* |
| 61 |
* @param string $link link to the page |
| 62 |
* @param int $id post id of the page |
| 63 |
* @return string modified link |
| 64 |
*/ |
| 65 |
public function page_link( $link, $id ) { |
| 66 |
$lang = $this->model->post->get_language( $id ); |
| 67 |
|
| 68 |
if ( $lang && $id == $lang->page_on_front ) { |
| 69 |
return $lang->home_url; |
| 70 |
} |
| 71 |
return $link; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Adds page_on_front and page_for_posts properties to the language objects |
| 76 |
* |
| 77 |
* @since 1.8 |
| 78 |
* |
| 79 |
* @param array $languages |
| 80 |
* @param object $model |
| 81 |
*/ |
| 82 |
public static function pll_languages_list( $languages, $model ) { |
| 83 |
if ( 'page' === get_option( 'show_on_front' ) ) { |
| 84 |
foreach ( $languages as $k => $language ) { |
| 85 |
$languages[ $k ]->page_on_front = $model->post->get( get_option( 'page_on_front' ), $language ); |
| 86 |
$languages[ $k ]->page_for_posts = $model->post->get( get_option( 'page_for_posts' ), $language ); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return $languages; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Translates page for posts |
| 95 |
* |
| 96 |
* @since 1.8 |
| 97 |
* |
| 98 |
* @param int $v page for posts page id |
| 99 |
* @return int |
| 100 |
*/ |
| 101 |
public function translate_page_for_posts( $v ) { |
| 102 |
// Don't attempt to translate in a 'switch_blog' action as there is a risk to call this function while initializing the languages cache |
| 103 |
return isset( $this->curlang->page_for_posts ) && ! doing_action( 'switch_blog' ) ? $this->curlang->page_for_posts : $v; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Fixes the oembed for the translated static front page |
| 108 |
* when the language page is redirected to the front page |
| 109 |
* |
| 110 |
* @since 2.6 |
| 111 |
* |
| 112 |
* @param int $post_id The post ID. |
| 113 |
* @param string $url The requested URL. |
| 114 |
*/ |
| 115 |
public function oembed_request_post_id( $post_id, $url ) { |
| 116 |
foreach ( $this->model->get_languages_list() as $lang ) { |
| 117 |
if ( trailingslashit( $url ) === trailingslashit( $lang->home_url ) ) { |
| 118 |
$post_id = $lang->page_on_front; |
| 119 |
} |
| 120 |
} |
| 121 |
return $post_id; |
| 122 |
} |
| 123 |
} |
| 124 |
|