PluginProbe
Polylang / 3.3
Polylang v3.3
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 3.3, at include/static-pages.php

191 lines 5.0 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 /**
13 * Id of the page on front.
14 *
15 * @var int|null
16 */
17 public $page_on_front;
18
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 /**
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 add_action( 'pll_language_defined', array( $this, 'pll_language_defined' ) );
60
61 // Modifies the page link in case the front page is not in the default language
62 add_filter( 'page_link', array( $this, 'page_link' ), 20, 2 );
63
64 // Clean the languages cache when editing page of front, page for posts
65 add_action( 'update_option_show_on_front', array( $this->model, 'clean_languages_cache' ) );
66 add_action( 'update_option_page_on_front', array( $this->model, 'clean_languages_cache' ) );
67 add_action( 'update_option_page_for_posts', array( $this->model, 'clean_languages_cache' ) );
68
69 // Refresh rewrite rules when the page on front is modified
70 add_action( 'update_option_page_on_front', 'flush_rewrite_rules' );
71
72 // OEmbed
73 add_filter( 'oembed_request_post_id', array( $this, 'oembed_request_post_id' ), 10, 2 );
74 }
75
76 /**
77 * Stores the page on front and page for posts ids
78 *
79 * @since 1.8
80 *
81 * @return void
82 */
83 public function init() {
84 if ( 'page' == get_option( 'show_on_front' ) ) {
85 $this->page_on_front = get_option( 'page_on_front' );
86 $this->page_for_posts = get_option( 'page_for_posts' );
87 } else {
88 $this->page_on_front = 0;
89 $this->page_for_posts = 0;
90 }
91 }
92
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 /**
107 * Modifies the page link in case the front page is not in the default language
108 *
109 * @since 0.7.2
110 *
111 * @param string $link link to the page
112 * @param int $id post id of the page
113 * @return string modified link
114 */
115 public function page_link( $link, $id ) {
116 $lang = $this->model->post->get_language( $id );
117
118 if ( $lang && $id == $lang->page_on_front ) {
119 return $lang->home_url;
120 }
121 return $link;
122 }
123
124 /**
125 * Adds page_on_front and page_for_posts properties to the language objects.
126 *
127 * @since 1.8
128 *
129 * @param PLL_Language[] $languages The list of languages.
130 * @param PLL_Model $model The instance of PLL_Model.
131 * @return PLL_Language[]
132 */
133 public static function pll_languages_list( $languages, $model ) {
134 if ( 'page' === get_option( 'show_on_front' ) ) {
135 foreach ( $languages as $k => $language ) {
136 $languages[ $k ]->page_on_front = $model->post->get( get_option( 'page_on_front' ), $language );
137 $languages[ $k ]->page_for_posts = $model->post->get( get_option( 'page_for_posts' ), $language );
138 }
139 }
140
141 return $languages;
142 }
143
144 /**
145 * Translates the page on front option.
146 *
147 * @since 1.8
148 * @since 3.3 Was previously defined in PLL_Frontend_Static_Pages.
149 *
150 * @param int $page_id ID of the page on front.
151 * @return int
152 */
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;
156 }
157
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 /**
172 * Fixes the oembed for the translated static front page
173 * when the language page is redirected to the front page
174 *
175 * @since 2.6
176 *
177 * @param int $post_id The post ID.
178 * @param string $url The requested URL.
179 * @return int
180 */
181 public function oembed_request_post_id( $post_id, $url ) {
182 foreach ( $this->model->get_languages_list() as $lang ) {
183 if ( is_string( $lang->home_url ) && trailingslashit( $url ) === trailingslashit( $lang->home_url ) ) {
184 return (int) $lang->page_on_front;
185 }
186 }
187
188 return $post_id;
189 }
190 }
191