PluginProbe
Polylang / 2.6.2
Polylang v2.6.2
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 / base.php

base.php in Polylang 2.6.2, at include/base.php

165 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 /**
4 * Base class for both admin and frontend
5 *
6 * @since 1.2
7 */
8 abstract class PLL_Base {
9 public $links_model, $model, $options;
10
11 /**
12 * Constructor
13 *
14 * @since 1.2
15 *
16 * @param object $links_model
17 */
18 public function __construct( &$links_model ) {
19 $this->links_model = &$links_model;
20 $this->model = &$links_model->model;
21 $this->options = &$this->model->options;
22
23 $GLOBALS['l10n_unloaded']['pll_string'] = true; // Short-circuit _load_textdomain_just_in_time() for 'pll_string' domain in WP 4.6+
24
25 add_action( 'widgets_init', array( $this, 'widgets_init' ) );
26
27 // User defined strings translations
28 add_action( 'pll_language_defined', array( $this, 'load_strings_translations' ), 5 );
29 add_action( 'change_locale', array( $this, 'load_strings_translations' ) ); // Since WP 4.7
30
31 // Switch_to_blog
32 add_action( 'switch_blog', array( $this, 'switch_blog' ), 10, 2 );
33 }
34
35 /**
36 * Instantiate classes always needed
37 *
38 * @since 2.6
39 */
40 public function init() {
41 // REST API
42 if ( class_exists( 'PLL_REST_API' ) ) {
43 $this->rest_api = new PLL_REST_API( $this );
44 }
45
46 if ( $this->model->get_languages_list() ) {
47 // Active languages
48 if ( class_exists( 'PLL_Active_Languages' ) ) {
49 $this->active_languages = new PLL_Active_Languages( $this );
50 }
51
52 // Share post slugs
53 if ( get_option( 'permalink_structure' ) && $this->options['force_lang'] && class_exists( 'PLL_Share_Post_Slug' ) ) {
54 $this->share_post_slug = new PLL_Share_Post_Slug( $this );
55 }
56 }
57 }
58
59 /**
60 * Registers our widgets
61 *
62 * @since 0.1
63 */
64 public function widgets_init() {
65 register_widget( 'PLL_Widget_Languages' );
66
67 // Overwrites the calendar widget to filter posts by language
68 if ( ! defined( 'PLL_WIDGET_CALENDAR' ) || PLL_WIDGET_CALENDAR ) {
69 unregister_widget( 'WP_Widget_Calendar' );
70 register_widget( 'PLL_Widget_Calendar' );
71 }
72 }
73
74 /**
75 * Loads user defined strings translations
76 *
77 * @since 1.2
78 * @since 2.1.3 $locale parameter added.
79 *
80 * @param string $locale Locale. Defaults to current locale.
81 */
82 public function load_strings_translations( $locale = '' ) {
83 if ( empty( $locale ) ) {
84 $locale = get_locale();
85 }
86
87 $language = $this->model->get_language( $locale );
88
89 if ( ! empty( $language ) ) {
90 $mo = new PLL_MO();
91 $mo->import_from_db( $language );
92 $GLOBALS['l10n']['pll_string'] = &$mo;
93 } else {
94 unset( $GLOBALS['l10n']['pll_string'] );
95 }
96 }
97
98 /**
99 * Resets some variables when switching blog
100 * Applies only if Polylang is active on the new blog
101 *
102 * @since 1.5.1
103 *
104 * @param int $new_blog
105 * @param int $old_blog
106 * @return bool not used by WP but by child class
107 */
108 public function switch_blog( $new_blog, $old_blog ) {
109 $plugins = ( $sitewide_plugins = get_site_option( 'active_sitewide_plugins' ) ) && is_array( $sitewide_plugins ) ? array_keys( $sitewide_plugins ) : array();
110 $plugins = array_merge( $plugins, get_option( 'active_plugins', array() ) );
111
112 // 2nd test needed when Polylang is not networked activated
113 // 3rd test needed when Polylang is networked activated and a new site is created
114 if ( $new_blog != $old_blog && in_array( POLYLANG_BASENAME, $plugins ) && get_option( 'polylang' ) ) {
115 $this->options = get_option( 'polylang' ); // Needed for menus
116 remove_action( 'pre_option_rewrite_rules', array( $this->links_model, 'prepare_rewrite_rules' ) );
117 $this->links_model = $this->model->get_links_model();
118 return true;
119 }
120 return false;
121 }
122
123 /**
124 * Some backward compatibility with Polylang < 1.2
125 * Allows for example to call $polylang->get_languages_list() instead of $polylang->model->get_languages_list()
126 * This works but should be slower than the direct call, thus an error is triggered in debug mode
127 *
128 * @since 1.2
129 *
130 * @param string $func function name
131 * @param array $args function arguments
132 */
133 public function __call( $func, $args ) {
134 foreach ( $this as $prop => &$obj ) {
135 if ( is_object( $obj ) && method_exists( $obj, $func ) ) {
136 if ( WP_DEBUG ) {
137 $debug = debug_backtrace(); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
138 $i = 1 + empty( $debug[1]['line'] ); // The file and line are in $debug[2] if the function was called using call_user_func
139 trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions
140 sprintf(
141 '%1$s was called incorrectly in %3$s on line %4$s: the call to $polylang->%1$s() has been deprecated in Polylang 1.2, use PLL()->%2$s->%1$s() instead.' . "\nError handler",
142 esc_html( $func ),
143 esc_html( $prop ),
144 esc_html( $debug[ $i ]['file'] ),
145 absint( $debug[ $i ]['line'] )
146 )
147 );
148 }
149 return call_user_func_array( array( $obj, $func ), $args );
150 }
151 }
152
153 $debug = debug_backtrace(); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
154 trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions
155 sprintf(
156 'Call to undefined function PLL()->%1$s() in %2$s on line %3$s' . "\nError handler",
157 esc_html( $func ),
158 esc_html( $debug[0]['file'] ),
159 absint( $debug[0]['line'] )
160 ),
161 E_USER_ERROR
162 );
163 }
164 }
165