PLL_Base

Base class for both admin and frontend.

Defined (1)

The class is defined in the following location(s).

/include/base.php  
  1. abstract class PLL_Base { 
  2. public $links_model, $model, $options; 
  3.  
  4. /** 
  5. * Constructor 
  6. * @since 1.2 
  7. * @param object $links_model 
  8. */ 
  9. public function __construct( &$links_model ) { 
  10. $this->links_model = &$links_model; 
  11. $this->model = &$links_model->model; 
  12. $this->options = &$this->model->options; 
  13.  
  14. $GLOBALS['l10n_unloaded']['pll_string'] = true; // Short-circuit _load_textdomain_just_in_time() for 'pll_string' domain in WP 4.6+ 
  15.  
  16. add_action( 'widgets_init', array( $this, 'widgets_init' ) ); 
  17.  
  18. // User defined strings translations 
  19. add_action( 'pll_language_defined', array( $this, 'load_strings_translations' ), 5 ); 
  20. add_action( 'change_locale', array( $this, 'load_strings_translations' ) ); // Since WP 4.7 
  21.  
  22. // Switch_to_blog 
  23. add_action( 'switch_blog', array( $this, 'switch_blog' ), 10, 2 ); 
  24.  
  25. /** 
  26. * Registers our widgets 
  27. * @since 0.1 
  28. */ 
  29. public function widgets_init() { 
  30. register_widget( 'PLL_Widget_Languages' ); 
  31.  
  32. // Overwrites the calendar widget to filter posts by language 
  33. if ( ! defined( 'PLL_WIDGET_CALENDAR' ) || PLL_WIDGET_CALENDAR ) { 
  34. unregister_widget( 'WP_Widget_Calendar' ); 
  35. register_widget( 'PLL_Widget_Calendar' ); 
  36.  
  37. /** 
  38. * Loads user defined strings translations 
  39. * @since 1.2 
  40. * @since 2.1.3 $locale parameter added. 
  41. * @param string $locale Locale. Defaults to current locale. 
  42. */ 
  43. public function load_strings_translations( $locale = '' ) { 
  44. if ( empty( $locale ) ) { 
  45. $locale = get_locale(); 
  46.  
  47. $language = $this->model->get_language( $locale ); 
  48.  
  49. if ( ! empty( $language ) ) { 
  50. $mo = new PLL_MO(); 
  51. $mo->import_from_db( $language ); 
  52. $GLOBALS['l10n']['pll_string'] = &$mo; 
  53. } else { 
  54. unset( $GLOBALS['l10n']['pll_string'] ); 
  55.  
  56. /** 
  57. * Resets some variables when switching blog 
  58. * Applies only if Polylang is active on the new blog 
  59. * @since 1.5.1 
  60. * @return bool not used by WP but by child class 
  61. */ 
  62. public function switch_blog( $new_blog, $old_blog ) { 
  63. $plugins = ( $sitewide_plugins = get_site_option( 'active_sitewide_plugins' ) ) && is_array( $sitewide_plugins ) ? array_keys( $sitewide_plugins ) : array(); 
  64. $plugins = array_merge( $plugins, get_option( 'active_plugins', array() ) ); 
  65.  
  66. // 2nd test needed when Polylang is not networked activated 
  67. // 3rd test needed when Polylang is networked activated and a new site is created 
  68. if ( $new_blog != $old_blog && in_array( POLYLANG_BASENAME, $plugins ) && get_option( 'polylang' ) ) { 
  69. $this->options = get_option( 'polylang' ); // Needed for menus 
  70. $this->links_model = $this->model->get_links_model(); 
  71. return true; 
  72. return false; 
  73.  
  74. /** 
  75. * Some backward compatibility with Polylang < 1.2 
  76. * Allows for example to call $polylang->get_languages_list() instead of $polylang->model->get_languages_list() 
  77. * This works but should be slower than the direct call, thus an error is triggered in debug mode 
  78. * @since 1.2 
  79. * @param string $func function name 
  80. * @param array $args function arguments 
  81. */ 
  82. public function __call( $func, $args ) { 
  83. foreach ( $this as $prop => &$obj ) { 
  84. if ( is_object( $obj ) && method_exists( $obj, $func ) ) { 
  85. if ( WP_DEBUG ) { 
  86. $debug = debug_backtrace(); 
  87. $i = 1 + empty( $debug[1]['line'] ); // The file and line are in $debug[2] if the function was called using call_user_func 
  88. trigger_error( sprintf( 
  89. '%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",  
  90. $func, $prop, $debug[ $i ]['file'], $debug[ $i ]['line'] 
  91. ) ); 
  92. return call_user_func_array( array( $obj, $func ), $args ); 
  93.  
  94. $debug = debug_backtrace(); 
  95. trigger_error( sprintf( 'Call to undefined function PLL()->%1$s() in %2$s on line %3$s' . "\nError handler", $func, $debug[0]['file'], $debug[0]['line'] ), E_USER_ERROR );