PluginProbe
Polylang / 2.3.6
Polylang v2.3.6
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
← All changes | include/class-polylang.php +68 -100 3.0.22.3.6 View file →
@@ -1,8 +1,5 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 if ( ! defined( 'ABSPATH' ) ) {
7 4 exit; // Don't access directly
8 5 };
@@ -29,17 +26,15 @@
29 26 *
30 27 * @since 0.1
31 28 */
32 29 public function __construct() {
33 - require_once __DIR__ . '/functions.php'; // VIP functions
30 + require_once PLL_INC . '/functions.php'; // VIP functions
31 + spl_autoload_register( array( $this, 'autoload' ) ); // Autoload classes
34 32
35 - // register an action when plugin is activating.
36 - register_activation_hook( POLYLANG_BASENAME, array( 'PLL_Wizard', 'start_wizard' ) );
37 -
38 33 $install = new PLL_Install( POLYLANG_BASENAME );
39 34
40 35 // Stopping here if we are going to deactivate the plugin ( avoids breaking rewrite rules )
41 - if ( $install->is_deactivation() || ! $install->can_activate() ) {
36 + if ( $install->is_deactivation() ) {
42 37 return;
43 38 }
44 39
45 40 // Plugin initialization
@@ -51,18 +46,52 @@
51 46 if ( ! defined( 'PLL_OLT' ) || PLL_OLT ) {
52 47 PLL_OLT_Manager::instance();
53 48 }
54 49
55 - /*
56 - * Loads the compatibility with some plugins and themes.
57 - * Loaded as soon as possible as we may need to act before other plugins are loaded.
58 - */
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
59 52 if ( ! defined( 'PLL_PLUGINS_COMPAT' ) || PLL_PLUGINS_COMPAT ) {
60 - PLL_Integrations::instance();
53 + PLL_Plugins_Compat::instance();
61 54 }
62 55 }
63 56
64 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 + $to_find = array( 'media', 'share', 'slug', 'slugs', 'sync', 'translate', 'wpml', 'xdata', 'rest' );
72 + $dir = implode( '-', array_intersect( explode( '-', $class ), $to_find ) );
73 +
74 + $dirs = array(
75 + PLL_FRONT_INC,
76 + PLL_MODULES_INC,
77 + PLL_MODULES_INC . "/$dir",
78 + PLL_MODULES_INC . '/plugins',
79 + PLL_INSTALL_INC,
80 + PLL_ADMIN_INC,
81 + PLL_SETTINGS_INC,
82 + PLL_INC,
83 + );
84 +
85 + foreach ( $dirs as $dir ) {
86 + if ( file_exists( $file = "$dir/$class.php" ) ) {
87 + require_once $file;
88 + return;
89 + }
90 + }
91 + }
92 +
93 + /**
65 94 * Tells whether the current request is an ajax request on frontend or not
66 95 *
67 96 * @since 2.2
68 97 *
@@ -67,13 +96,13 @@
67 96 * @since 2.2
68 97 *
69 98 * @return bool
70 99 */
71 - public static function is_ajax_on_front() {
100 + static public function is_ajax_on_front() {
72 101 // Special test for plupload which does not use jquery ajax and thus does not pass our ajax prefilter
73 102 // Special test for customize_save done in frontend but for which we want to load the admin
74 - $in = isset( $_REQUEST['action'] ) && in_array( sanitize_key( $_REQUEST['action'] ), array( 'upload-attachment', 'customize_save' ) ); // phpcs:ignore WordPress.Security.NonceVerification
75 - $is_ajax_on_front = wp_doing_ajax() && empty( $_REQUEST['pll_ajax_backend'] ) && ! $in; // phpcs:ignore WordPress.Security.NonceVerification
103 + $in = isset( $_REQUEST['action'] ) && in_array( $_REQUEST['action'], array( 'upload-attachment', 'customize_save' ) );
104 + $is_ajax_on_front = wp_doing_ajax() && empty( $_REQUEST['pll_ajax_backend'] ) && ! $in;
76 105
77 106 /**
78 107 * Filters whether the current request is an ajax request on front.
79 108 *
@@ -84,55 +113,14 @@
84 113 return apply_filters( 'pll_is_ajax_on_front', $is_ajax_on_front );
85 114 }
86 115
87 116 /**
88 - * Is the current request a REST API request?
89 - * Inspired by WP::parse_request()
90 - * Needed because at this point, the constant REST_REQUEST is not defined yet
91 - *
92 - * @since 2.4.1
93 - *
94 - * @return bool
95 - */
96 - public static function is_rest_request() {
97 - // Handle pretty permalinks.
98 - $home_path = trim( wp_parse_url( home_url(), PHP_URL_PATH ), '/' );
99 - $home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) );
100 -
101 - $req_uri = trim( wp_parse_url( pll_get_requested_url(), PHP_URL_PATH ), '/' );
102 - $req_uri = preg_replace( $home_path_regex, '', $req_uri );
103 - $req_uri = trim( $req_uri, '/' );
104 - $req_uri = str_replace( 'index.php', '', $req_uri );
105 - $req_uri = trim( $req_uri, '/' );
106 -
107 - // And also test rest_route query string parameter is not empty for plain permalinks.
108 - $query_string = array();
109 - wp_parse_str( wp_parse_url( pll_get_requested_url(), PHP_URL_QUERY ), $query_string );
110 - $rest_route = isset( $query_string['rest_route'] ) ? trim( $query_string['rest_route'], '/' ) : false;
111 -
112 - return 0 === strpos( $req_uri, rest_get_url_prefix() . '/' ) || ! empty( $rest_route );
113 - }
114 -
115 - /**
116 - * Tells if we are in the wizard process.
117 - *
118 - * @since 2.7
119 - *
120 - * @return bool
121 - */
122 - public static function is_wizard() {
123 - return isset( $_GET['page'] ) && ! empty( $_GET['page'] ) && 'mlang_wizard' === sanitize_key( $_GET['page'] ); // phpcs:ignore WordPress.Security.NonceVerification
124 - }
125 -
126 - /**
127 117 * Defines constants
128 118 * May be overridden by a plugin if set before plugins_loaded, 1
129 119 *
130 120 * @since 1.6
131 - *
132 - * @return void
133 121 */
134 - public static function define_constants() {
122 + static public function define_constants() {
135 123 // Cookie name. no cookie will be used if set to false
136 124 if ( ! defined( 'PLL_COOKIE' ) ) {
137 125 define( 'PLL_COOKIE', 'pll_language' );
138 126 }
@@ -143,14 +131,14 @@
143 131 }
144 132
145 133 // Admin
146 134 if ( ! defined( 'PLL_ADMIN' ) ) {
147 - define( 'PLL_ADMIN', wp_doing_cron() || ( defined( 'WP_CLI' ) && WP_CLI ) || ( is_admin() && ! PLL_AJAX_ON_FRONT ) );
135 + define( 'PLL_ADMIN', defined( 'DOING_CRON' ) || ( defined( 'WP_CLI' ) && WP_CLI ) || ( is_admin() && ! PLL_AJAX_ON_FRONT ) );
148 136 }
149 137
150 - // Settings page whatever the tab except for the wizard which needs to be an admin process.
138 + // Settings page whatever the tab
151 139 if ( ! defined( 'PLL_SETTINGS' ) ) {
152 - define( 'PLL_SETTINGS', is_admin() && ( ( isset( $_GET['page'] ) && 0 === strpos( sanitize_key( $_GET['page'] ), 'mlang' ) && ! self::is_wizard() ) || ! empty( $_REQUEST['pll_ajax_settings'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification
140 + define( 'PLL_SETTINGS', is_admin() && ( ( isset( $_GET['page'] ) && 0 === strpos( $_GET['page'], 'mlang' ) ) || ! empty( $_REQUEST['pll_ajax_settings'] ) ) );
153 141 }
154 142 }
155 143
156 144 /**
@@ -157,10 +145,8 @@
157 145 * Polylang initialization
158 146 * setups models and separate admin and frontend
159 147 *
160 148 * @since 1.2
161 - *
162 - * @return void
163 149 */
164 150 public function init() {
165 151 global $polylang;
166 152
@@ -174,13 +160,8 @@
174 160 return;
175 161 }
176 162 }
177 163
178 - // In some edge cases, it's possible that no options were found in the database. Load default options as we need some.
179 - if ( ! $options ) {
180 - $options = PLL_Install::get_default_options();
181 - }
182 -
183 164 // Make sure that this filter is *always* added before PLL_Model::get_languages_list() is called for the first time
184 165 add_filter( 'pll_languages_list', array( 'PLL_Static_Pages', 'pll_languages_list' ), 2, 2 ); // Before PLL_Links_Model
185 166
186 167 /**
@@ -190,12 +171,23 @@
190 171 * @since 1.5
191 172 *
192 173 * @param string $class either PLL_Model or PLL_Admin_Model
193 174 */
194 - $class = apply_filters( 'pll_model', PLL_SETTINGS || self::is_wizard() ? 'PLL_Admin_Model' : 'PLL_Model' );
175 + $class = apply_filters( 'pll_model', PLL_SETTINGS ? 'PLL_Admin_Model' : 'PLL_Model' );
195 176 $model = new $class( $options );
196 177 $links_model = $model->get_links_model();
197 178
179 + if ( PLL_SETTINGS ) {
180 + $polylang = new PLL_Settings( $links_model );
181 + }
182 + elseif ( PLL_ADMIN ) {
183 + $polylang = new PLL_Admin( $links_model );
184 + }
185 + // Do nothing on frontend if no language is defined
186 + elseif ( $model->get_languages_list() && empty( $_GET['deactivate-polylang'] ) ) {
187 + $polylang = new PLL_Frontend( $links_model );
188 + }
189 +
198 190 if ( ! $model->get_languages_list() ) {
199 191 /**
200 192 * Fires when no language has been defined yet
201 193 * Used to load overridden textdomains
@@ -204,32 +196,9 @@
204 196 */
205 197 do_action( 'pll_no_language_defined' );
206 198 }
207 199
208 - $class = '';
209 -
210 - if ( PLL_SETTINGS ) {
211 - $class = 'PLL_Settings';
212 - } elseif ( PLL_ADMIN ) {
213 - $class = 'PLL_Admin';
214 - } elseif ( self::is_rest_request() ) {
215 - $class = 'PLL_REST_Request';
216 - } elseif ( $model->get_languages_list() ) {
217 - $class = 'PLL_Frontend';
218 - }
219 -
220 - /**
221 - * Filters the class to use to instantiate the $polylang object
222 - *
223 - * @since 2.6
224 - *
225 - * @param string $class A class name.
226 - */
227 - $class = apply_filters( 'pll_context', $class );
228 -
229 - if ( ! empty( $class ) ) {
230 - $polylang = new $class( $links_model );
231 -
200 + if ( ! empty( $polylang ) ) {
232 201 /**
233 202 * Fires after the $polylang object is created and before the API is loaded
234 203 *
235 204 * @since 2.0
@@ -237,16 +206,13 @@
237 206 * @param object $polylang
238 207 */
239 208 do_action_ref_array( 'pll_pre_init', array( &$polylang ) );
240 209
241 - require_once __DIR__ . '/api.php'; // Loads the API
210 + require_once PLL_INC . '/api.php'; // Loads the API
242 211
243 - // Loads the modules.
244 - $load_scripts = glob( POLYLANG_DIR . '/modules/*/load.php', GLOB_NOSORT );
245 - if ( is_array( $load_scripts ) ) {
246 - foreach ( $load_scripts as $load_script ) {
247 - require_once $load_script; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable
248 - }
212 + if ( ! defined( 'PLL_WPML_COMPAT' ) || PLL_WPML_COMPAT ) {
213 + PLL_WPML_Compat::instance(); // WPML API
214 + PLL_WPML_Config::instance(); // wpml-config.xml
249 215 }
250 216
251 217 $polylang->init();
252 218
@@ -260,4 +226,6 @@
260 226 do_action_ref_array( 'pll_init', array( &$polylang ) );
261 227 }
262 228 }
263 229 }
230 +
231 +new Polylang();