| 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 |
add_action('widgets_init', array(&$this, 'widgets_init')); |
| 24 |
|
| 25 |
// user defined strings translations |
| 26 |
add_action('pll_language_defined', array(&$this, 'load_strings_translations'), 5); |
| 27 |
|
| 28 |
// switch_to_blog |
| 29 |
add_action('switch_blog', array(&$this, 'switch_blog'), 10, 2); |
| 30 |
} |
| 31 |
|
| 32 |
/* |
| 33 |
* registers our widgets |
| 34 |
* |
| 35 |
* @since 0.1 |
| 36 |
*/ |
| 37 |
public function widgets_init() { |
| 38 |
register_widget('PLL_Widget_Languages'); |
| 39 |
|
| 40 |
// overwrites the calendar widget to filter posts by language |
| 41 |
if (!defined('PLL_WIDGET_CALENDAR') || PLL_WIDGET_CALENDAR) { |
| 42 |
unregister_widget('WP_Widget_Calendar'); |
| 43 |
register_widget('PLL_Widget_Calendar'); |
| 44 |
} |
| 45 |
|
| 46 |
// overwrites the recent posts and recent comments widget to use a language dependant cache key |
| 47 |
// useful only if using a cache plugin |
| 48 |
if (defined('WP_CACHE') && WP_CACHE) { |
| 49 |
if (!defined('PLL_WIDGET_RECENT_POSTS') || PLL_WIDGET_RECENT_POSTS) { |
| 50 |
unregister_widget('WP_Widget_Recent_Posts'); |
| 51 |
register_widget('PLL_Widget_Recent_Posts'); |
| 52 |
} |
| 53 |
|
| 54 |
if (!defined('PLL_WIDGET_RECENT_COMMENTS') || PLL_WIDGET_RECENT_COMMENTS) { |
| 55 |
unregister_widget('WP_Widget_Recent_Comments'); |
| 56 |
register_widget('PLL_Widget_Recent_Comments'); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/* |
| 62 |
* loads user defined strings translations |
| 63 |
* |
| 64 |
* @since 1.2 |
| 65 |
*/ |
| 66 |
public function load_strings_translations() { |
| 67 |
$mo = new PLL_MO(); |
| 68 |
$mo->import_from_db($this->model->get_language(get_locale())); |
| 69 |
$GLOBALS['l10n']['pll_string'] = &$mo; |
| 70 |
} |
| 71 |
|
| 72 |
/* |
| 73 |
* resets some variables when switching blog |
| 74 |
* applies only if Polylang is active on the new blog |
| 75 |
* |
| 76 |
* @since 1.5.1 |
| 77 |
* |
| 78 |
* @return bool not used by WP but by child class |
| 79 |
*/ |
| 80 |
public function switch_blog($new_blog, $old_blog) { |
| 81 |
$plugins = ($sitewide_plugins = get_site_option('active_sitewide_plugins')) && is_array($sitewide_plugins) ? array_keys($sitewide_plugins) : array(); |
| 82 |
$plugins = array_merge($plugins, get_option('active_plugins', array())); |
| 83 |
|
| 84 |
// 2nd test needed when Polylang is not networked activated |
| 85 |
// 3rd test needed when Polylang is networked activated and a new site is created |
| 86 |
if ($new_blog != $old_blog && in_array(POLYLANG_BASENAME, $plugins) && get_option('polylang')) { |
| 87 |
$this->model->blog_id = $new_blog; |
| 88 |
$this->options = get_option('polylang'); // needed for menus |
| 89 |
$this->links_model = $this->model->get_links_model(); |
| 90 |
return true; |
| 91 |
} |
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
/* |
| 96 |
* some backward compatibility with Polylang < 1.2 |
| 97 |
* allows for example to call $polylang->get_languages_list() instead of $polylang->model->get_languages_list() |
| 98 |
* this works but should be slower than the direct call, thus an error is triggered in debug mode |
| 99 |
* |
| 100 |
* @since 1.2 |
| 101 |
* |
| 102 |
* @param string $func function name |
| 103 |
* @param array $args function arguments |
| 104 |
*/ |
| 105 |
public function __call($func, $args) { |
| 106 |
foreach ($this as $prop => &$obj) |
| 107 |
if (is_object($obj) && method_exists($obj, $func)) { |
| 108 |
if (WP_DEBUG) { |
| 109 |
$debug = debug_backtrace(); |
| 110 |
trigger_error(sprintf( |
| 111 |
'%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 $polylang->%2$s->%1$s() instead.' . "\nError handler", |
| 112 |
$func, $prop, $debug[1]['file'], $debug[1]['line'] |
| 113 |
)); |
| 114 |
} |
| 115 |
return call_user_func_array(array($obj, $func), $args); |
| 116 |
} |
| 117 |
|
| 118 |
$debug = debug_backtrace(); |
| 119 |
trigger_error(sprintf('Call to undefined function $polylang->%1$s() in %2$s on line %3$s' . "\nError handler", $func, $debug[0]['file'], $debug[0]['line']), E_USER_ERROR); |
| 120 |
} |
| 121 |
} |
| 122 |
|