PluginProbe
Polylang / 2.8
Polylang v2.8
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / include / static-pages.php

static-pages.php in Polylang 2.8, at include/static-pages.php

127 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 public $model, $options;
13 public $page_on_front, $page_for_posts;
14
15 /**
16 * Constructor: setups filters and actions
17 *
18 * @since 1.8
19 *
20 * @param object $polylang
21 */
22 public function __construct( &$polylang ) {
23 $this->model = &$polylang->model;
24 $this->options = &$polylang->options;
25 $this->curlang = &$polylang->curlang;
26
27 $this->init();
28
29 // Modifies the page link in case the front page is not in the default language
30 add_filter( 'page_link', array( $this, 'page_link' ), 20, 2 );
31
32 // Clean the languages cache when editing page of front, page for posts
33 add_action( 'update_option_show_on_front', array( $this->model, 'clean_languages_cache' ) );
34 add_action( 'update_option_page_on_front', array( $this->model, 'clean_languages_cache' ) );
35 add_action( 'update_option_page_for_posts', array( $this->model, 'clean_languages_cache' ) );
36
37 // Refresh rewrite rules when the page on front is modified
38 add_action( 'update_option_page_on_front', 'flush_rewrite_rules' );
39
40 // OEmbed
41 add_filter( 'oembed_request_post_id', array( $this, 'oembed_request_post_id' ), 10, 2 );
42 }
43
44 /**
45 * Stores the page on front and page for posts ids
46 *
47 * @since 1.8
48 */
49 public function init() {
50 if ( 'page' == get_option( 'show_on_front' ) ) {
51 $this->page_on_front = get_option( 'page_on_front' );
52 $this->page_for_posts = get_option( 'page_for_posts' );
53 } else {
54 $this->page_on_front = 0;
55 $this->page_for_posts = 0;
56 }
57 }
58
59 /**
60 * Modifies the page link in case the front page is not in the default language
61 *
62 * @since 0.7.2
63 *
64 * @param string $link link to the page
65 * @param int $id post id of the page
66 * @return string modified link
67 */
68 public function page_link( $link, $id ) {
69 $lang = $this->model->post->get_language( $id );
70
71 if ( $lang && $id == $lang->page_on_front ) {
72 return $lang->home_url;
73 }
74 return $link;
75 }
76
77 /**
78 * Adds page_on_front and page_for_posts properties to the language objects
79 *
80 * @since 1.8
81 *
82 * @param array $languages
83 * @param object $model
84 */
85 public static function pll_languages_list( $languages, $model ) {
86 if ( 'page' === get_option( 'show_on_front' ) ) {
87 foreach ( $languages as $k => $language ) {
88 $languages[ $k ]->page_on_front = $model->post->get( get_option( 'page_on_front' ), $language );
89 $languages[ $k ]->page_for_posts = $model->post->get( get_option( 'page_for_posts' ), $language );
90 }
91 }
92
93 return $languages;
94 }
95
96 /**
97 * Translates page for posts
98 *
99 * @since 1.8
100 *
101 * @param int $v page for posts page id
102 * @return int
103 */
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;
107 }
108
109 /**
110 * Fixes the oembed for the translated static front page
111 * when the language page is redirected to the front page
112 *
113 * @since 2.6
114 *
115 * @param int $post_id The post ID.
116 * @param string $url The requested URL.
117 */
118 public function oembed_request_post_id( $post_id, $url ) {
119 foreach ( $this->model->get_languages_list() as $lang ) {
120 if ( trailingslashit( $url ) === trailingslashit( $lang->home_url ) ) {
121 $post_id = $lang->page_on_front;
122 }
123 }
124 return $post_id;
125 }
126 }
127