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 / class-polylang.php

class-polylang.php in Polylang 2.6.2, at include/class-polylang.php

278 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Don't access directly
5 };
6
7 // Default directory to store user data such as custom flags
8 if ( ! defined( 'PLL_LOCAL_DIR' ) ) {
9 define( 'PLL_LOCAL_DIR', WP_CONTENT_DIR . '/polylang' );
10 }
11
12 // Includes local config file if exists
13 if ( file_exists( PLL_LOCAL_DIR . '/pll-config.php' ) ) {
14 include_once PLL_LOCAL_DIR . '/pll-config.php';
15 }
16
17 /**
18 * Controls the plugin, as well as activation, and deactivation
19 *
20 * @since 0.1
21 */
22 class Polylang {
23
24 /**
25 * Constructor
26 *
27 * @since 0.1
28 */
29 public function __construct() {
30 require_once PLL_INC . '/functions.php'; // VIP functions
31 spl_autoload_register( array( $this, 'autoload' ) ); // Autoload classes
32
33 $install = new PLL_Install( POLYLANG_BASENAME );
34
35 // Stopping here if we are going to deactivate the plugin ( avoids breaking rewrite rules )
36 if ( $install->is_deactivation() ) {
37 return;
38 }
39
40 // Plugin initialization
41 // Take no action before all plugins are loaded
42 add_action( 'plugins_loaded', array( $this, 'init' ), 1 );
43
44 // Override load text domain waiting for the language to be defined
45 // Here for plugins which load text domain as soon as loaded :(
46 if ( ! defined( 'PLL_OLT' ) || PLL_OLT ) {
47 PLL_OLT_Manager::instance();
48 }
49
50 // Extra code for compatibility with some plugins
51 // Loaded as soon as possible as we may need to act before other plugins are loaded
52 if ( ! defined( 'PLL_PLUGINS_COMPAT' ) || PLL_PLUGINS_COMPAT ) {
53 PLL_Plugins_Compat::instance();
54 }
55 }
56
57 /**
58 * Autoload classes
59 *
60 * @since 1.2
61 *
62 * @param string $class
63 */
64 public function autoload( $class ) {
65 // Not a Polylang class
66 if ( 0 !== strncmp( 'PLL_', $class, 4 ) ) {
67 return;
68 }
69
70 $class = str_replace( '_', '-', strtolower( substr( $class, 4 ) ) );
71 $dirs = array();
72 $parts = explode( '-', $class );
73 $parts = array_values( array_diff( $parts, array( 'frontend', 'admin', 'settings', 'advanced' ) ) );
74 if ( isset( $parts[0] ) ) {
75 $dirs[] = PLL_MODULES_INC . "/{$parts[0]}";
76 if ( isset( $parts[1] ) ) {
77 $dirs[] = PLL_MODULES_INC . "/{$parts[0]}-{$parts[1]}";
78 if ( isset( $parts[2] ) && in_array( $parts[1], array( 'post', 'term' ) ) ) {
79 $dirs[] = PLL_MODULES_INC . "/{$parts[0]}-{$parts[2]}";
80 }
81 }
82 }
83
84 $dirs = array_merge(
85 array(
86 PLL_FRONT_INC,
87 PLL_MODULES_INC,
88 ),
89 $dirs,
90 array(
91 PLL_MODULES_INC . '/plugins',
92 PLL_INSTALL_INC,
93 PLL_ADMIN_INC,
94 PLL_SETTINGS_INC,
95 PLL_INC,
96 )
97 );
98
99 foreach ( $dirs as $dir ) {
100 if ( file_exists( $file = "$dir/$class.php" ) ) {
101 require_once $file;
102 return;
103 }
104 }
105 }
106
107 /**
108 * Tells whether the current request is an ajax request on frontend or not
109 *
110 * @since 2.2
111 *
112 * @return bool
113 */
114 public static function is_ajax_on_front() {
115 // Special test for plupload which does not use jquery ajax and thus does not pass our ajax prefilter
116 // Special test for customize_save done in frontend but for which we want to load the admin
117 $in = isset( $_REQUEST['action'] ) && in_array( sanitize_key( $_REQUEST['action'] ), array( 'upload-attachment', 'customize_save' ) ); // phpcs:ignore WordPress.Security.NonceVerification
118 $is_ajax_on_front = wp_doing_ajax() && empty( $_REQUEST['pll_ajax_backend'] ) && ! $in; // phpcs:ignore WordPress.Security.NonceVerification
119
120 /**
121 * Filters whether the current request is an ajax request on front.
122 *
123 * @since 2.3
124 *
125 * @param bool $is_ajax_on_front Whether the current request is an ajax request on front.
126 */
127 return apply_filters( 'pll_is_ajax_on_front', $is_ajax_on_front );
128 }
129
130 /**
131 * Is the current request a REST API request?
132 * Inspired by WP::parse_request()
133 * Needed because at this point, the constant REST_REQUEST is not defined yet
134 *
135 * @since 2.4.1
136 *
137 * @return bool
138 */
139 public static function is_rest_request() {
140 $home_path = trim( wp_parse_url( home_url(), PHP_URL_PATH ), '/' );
141 $home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) );
142
143 $req_uri = trim( wp_parse_url( pll_get_requested_url(), PHP_URL_PATH ), '/' );
144 $req_uri = preg_replace( $home_path_regex, '', $req_uri );
145 $req_uri = trim( $req_uri, '/' );
146 $req_uri = str_replace( 'index.php', '', $req_uri );
147 $req_uri = trim( $req_uri, '/' );
148
149 return 0 === strpos( $req_uri, rest_get_url_prefix() . '/' );
150 }
151
152 /**
153 * Defines constants
154 * May be overridden by a plugin if set before plugins_loaded, 1
155 *
156 * @since 1.6
157 */
158 public static function define_constants() {
159 // Cookie name. no cookie will be used if set to false
160 if ( ! defined( 'PLL_COOKIE' ) ) {
161 define( 'PLL_COOKIE', 'pll_language' );
162 }
163
164 // Backward compatibility with Polylang < 2.3
165 if ( ! defined( 'PLL_AJAX_ON_FRONT' ) ) {
166 define( 'PLL_AJAX_ON_FRONT', self::is_ajax_on_front() );
167 }
168
169 // Admin
170 if ( ! defined( 'PLL_ADMIN' ) ) {
171 define( 'PLL_ADMIN', wp_doing_cron() || ( defined( 'WP_CLI' ) && WP_CLI ) || ( is_admin() && ! PLL_AJAX_ON_FRONT ) );
172 }
173
174 // Settings page whatever the tab
175 if ( ! defined( 'PLL_SETTINGS' ) ) {
176 define( 'PLL_SETTINGS', is_admin() && ( ( isset( $_GET['page'] ) && 0 === strpos( sanitize_key( $_GET['page'] ), 'mlang' ) ) || ! empty( $_REQUEST['pll_ajax_settings'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification
177 }
178 }
179
180 /**
181 * Polylang initialization
182 * setups models and separate admin and frontend
183 *
184 * @since 1.2
185 */
186 public function init() {
187 global $polylang;
188
189 self::define_constants();
190 $options = get_option( 'polylang' );
191
192 // Plugin upgrade
193 if ( $options && version_compare( $options['version'], POLYLANG_VERSION, '<' ) ) {
194 $upgrade = new PLL_Upgrade( $options );
195 if ( ! $upgrade->upgrade() ) { // If the version is too old
196 return;
197 }
198 }
199
200 // Make sure that this filter is *always* added before PLL_Model::get_languages_list() is called for the first time
201 add_filter( 'pll_languages_list', array( 'PLL_Static_Pages', 'pll_languages_list' ), 2, 2 ); // Before PLL_Links_Model
202
203 /**
204 * Filter the model class to use
205 * /!\ this filter is fired *before* the $polylang object is available
206 *
207 * @since 1.5
208 *
209 * @param string $class either PLL_Model or PLL_Admin_Model
210 */
211 $class = apply_filters( 'pll_model', PLL_SETTINGS ? 'PLL_Admin_Model' : 'PLL_Model' );
212 $model = new $class( $options );
213 $links_model = $model->get_links_model();
214
215 if ( ! $model->get_languages_list() ) {
216 /**
217 * Fires when no language has been defined yet
218 * Used to load overridden textdomains
219 *
220 * @since 1.2
221 */
222 do_action( 'pll_no_language_defined' );
223 }
224
225 $class = '';
226
227 if ( PLL_SETTINGS ) {
228 $class = 'PLL_Settings';
229 } elseif ( PLL_ADMIN ) {
230 $class = 'PLL_Admin';
231 } elseif ( self::is_rest_request() ) {
232 $class = 'PLL_REST_Request';
233 } elseif ( $model->get_languages_list() && empty( $_GET['deactivate-polylang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
234 $class = 'PLL_Frontend';
235 }
236
237 /**
238 * Filters the class to use to instantiate the $polylang object
239 *
240 * @since 2.6
241 *
242 * @param string $class A class name.
243 */
244 $class = apply_filters( 'pll_context', $class );
245
246 if ( ! empty( $class ) ) {
247 $polylang = new $class( $links_model );
248
249 /**
250 * Fires after the $polylang object is created and before the API is loaded
251 *
252 * @since 2.0
253 *
254 * @param object $polylang
255 */
256 do_action_ref_array( 'pll_pre_init', array( &$polylang ) );
257
258 require_once PLL_INC . '/api.php'; // Loads the API
259
260 if ( ! defined( 'PLL_WPML_COMPAT' ) || PLL_WPML_COMPAT ) {
261 PLL_WPML_Compat::instance(); // WPML API
262 PLL_WPML_Config::instance(); // wpml-config.xml
263 }
264
265 $polylang->init();
266
267 /**
268 * Fires after the $polylang object and the API is loaded
269 *
270 * @since 1.7
271 *
272 * @param object $polylang
273 */
274 do_action_ref_array( 'pll_init', array( &$polylang ) );
275 }
276 }
277 }
278