2021-09-21 15:01:08 +02:00
< ? php
2021-09-21 15:06:29 +02:00
/**
* @ package Polylang
*/
2021-09-21 15:01:08 +02:00
/**
* Base class for both admin and frontend
*
* @ since 1.2
*/
abstract class PLL_Base {
2021-09-21 15:06:29 +02:00
/**
* Stores the plugin options .
*
* @ var array
*/
public $options ;
/**
* @ var PLL_Model
*/
public $model ;
/**
* Instance of a child class of PLL_Links_Model .
*
* @ var PLL_Links_Model
*/
public $links_model ;
/**
* Registers hooks on insert / update post related actions and filters .
*
* @ var PLL_CRUD_Posts
*/
public $posts ;
/**
* Registers hooks on insert / update term related action and filters .
*
* @ var PLL_CRUD_Terms
*/
public $terms ;
2021-09-21 15:01:08 +02:00
/**
2021-09-21 15:06:29 +02:00
* Constructor .
2021-09-21 15:01:08 +02:00
*
* @ since 1.2
*
2021-09-21 15:06:29 +02:00
* @ param PLL_Links_Model $links_model Links Model .
2021-09-21 15:01:08 +02:00
*/
public function __construct ( & $links_model ) {
$this -> links_model = & $links_model ;
$this -> model = & $links_model -> model ;
$this -> options = & $this -> model -> options ;
$GLOBALS [ 'l10n_unloaded' ][ 'pll_string' ] = true ; // Short-circuit _load_textdomain_just_in_time() for 'pll_string' domain in WP 4.6+
add_action ( 'widgets_init' , array ( $this , 'widgets_init' ) );
// User defined strings translations
add_action ( 'pll_language_defined' , array ( $this , 'load_strings_translations' ), 5 );
add_action ( 'change_locale' , array ( $this , 'load_strings_translations' ) ); // Since WP 4.7
2021-09-21 15:06:29 +02:00
add_action ( 'personal_options_update' , array ( $this , 'load_strings_translations' ), 1 , 0 ); // Before WP, for confirmation request when changing the user email.
2021-09-21 15:01:08 +02:00
// Switch_to_blog
add_action ( 'switch_blog' , array ( $this , 'switch_blog' ), 10 , 2 );
}
2021-09-21 15:06:29 +02:00
/**
* Instantiates classes reacting to CRUD operations on posts and terms ,
* only when at least one language is defined .
*
* @ since 2.6
*
* @ return void
*/
public function init () {
if ( $this -> model -> get_languages_list () ) {
$this -> posts = new PLL_CRUD_Posts ( $this );
$this -> terms = new PLL_CRUD_Terms ( $this );
// WordPress options.
new PLL_Translate_Option ( 'blogname' , array (), array ( 'context' => 'WordPress' ) );
new PLL_Translate_Option ( 'blogdescription' , array (), array ( 'context' => 'WordPress' ) );
new PLL_Translate_Option ( 'date_format' , array (), array ( 'context' => 'WordPress' ) );
new PLL_Translate_Option ( 'time_format' , array (), array ( 'context' => 'WordPress' ) );
}
}
2021-09-21 15:01:08 +02:00
/**
* Registers our widgets
*
* @ since 0.1
2021-09-21 15:06:29 +02:00
*
* @ return void
2021-09-21 15:01:08 +02:00
*/
public function widgets_init () {
register_widget ( 'PLL_Widget_Languages' );
// Overwrites the calendar widget to filter posts by language
if ( ! defined ( 'PLL_WIDGET_CALENDAR' ) || PLL_WIDGET_CALENDAR ) {
unregister_widget ( 'WP_Widget_Calendar' );
register_widget ( 'PLL_Widget_Calendar' );
}
}
/**
* Loads user defined strings translations
*
* @ since 1.2
* @ since 2.1 . 3 $locale parameter added .
*
* @ param string $locale Locale . Defaults to current locale .
2021-09-21 15:06:29 +02:00
* @ return void
2021-09-21 15:01:08 +02:00
*/
public function load_strings_translations ( $locale = '' ) {
if ( empty ( $locale ) ) {
2021-09-21 15:06:29 +02:00
$locale = ( is_admin () && ! Polylang :: is_ajax_on_front () ) ? get_user_locale () : get_locale ();
2021-09-21 15:01:08 +02:00
}
$language = $this -> model -> get_language ( $locale );
if ( ! empty ( $language ) ) {
$mo = new PLL_MO ();
$mo -> import_from_db ( $language );
$GLOBALS [ 'l10n' ][ 'pll_string' ] = & $mo ;
} else {
unset ( $GLOBALS [ 'l10n' ][ 'pll_string' ] );
}
}
/**
2021-09-21 15:06:29 +02:00
* Resets some variables when the blog is switched .
* Applied only if Polylang is active on the new blog .
2021-09-21 15:01:08 +02:00
*
* @ since 1.5 . 1
*
2021-09-21 15:06:29 +02:00
* @ param int $new_blog_id New blog ID .
* @ param int $prev_blog_id Previous blog ID .
* @ return void
2021-09-21 15:01:08 +02:00
*/
2021-09-21 15:06:29 +02:00
public function switch_blog ( $new_blog_id , $prev_blog_id ) {
if ( $this -> is_active_on_new_blog ( $new_blog_id , $prev_blog_id ) ) {
$this -> options = get_option ( 'polylang' ); // Needed for menus.
remove_action ( 'pre_option_rewrite_rules' , array ( $this -> links_model , 'prepare_rewrite_rules' ) );
2021-09-21 15:01:08 +02:00
$this -> links_model = $this -> model -> get_links_model ();
}
}
/**
2021-09-21 15:06:29 +02:00
* Checks if Polylang is active on the new blog when the blog is switched .
2021-09-21 15:01:08 +02:00
*
2021-09-21 15:06:29 +02:00
* @ since 3.0
2021-09-21 15:01:08 +02:00
*
2021-09-21 15:06:29 +02:00
* @ param int $new_blog_id New blog ID .
* @ param int $prev_blog_id Previous blog ID .
* @ return bool
2021-09-21 15:01:08 +02:00
*/
2021-09-21 15:06:29 +02:00
protected function is_active_on_new_blog ( $new_blog_id , $prev_blog_id ) {
$plugins = ( $sitewide_plugins = get_site_option ( 'active_sitewide_plugins' ) ) && is_array ( $sitewide_plugins ) ? array_keys ( $sitewide_plugins ) : array ();
$plugins = array_merge ( $plugins , get_option ( 'active_plugins' , array () ) );
2021-09-21 15:01:08 +02:00
2021-09-21 15:06:29 +02:00
/*
* The 2 nd test is needed when Polylang is not networked activated .
* The 3 rd test is needed when Polylang is networked activated and a new site is created .
*/
return $new_blog_id !== $prev_blog_id && in_array ( POLYLANG_BASENAME , $plugins ) && get_option ( 'polylang' );
2021-09-21 15:01:08 +02:00
}
}