PluginProbe
Polylang / 2.5
Polylang v2.5
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.5, at include/static-pages.php

103 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // Don't attempt to translate in a 'switch_blog' action as there is a risk to call this function while initializing the languages cache
100 return isset( $this->curlang->page_for_posts ) && ! doing_action( 'switch_blog' ) ? $this->curlang->page_for_posts : $v;
101 }
102 }
103