| @@ -1,195 +1,95 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | -/** | |
| 7 | - * Base class for both admin and frontend | |
| 3 | +/* | |
| 4 | + * base class for both admin and frontend | |
| 8 | 5 | * |
| 9 | 6 | * @since 1.2 |
| 10 | 7 | */ |
| 11 | 8 | abstract class PLL_Base { |
| 12 | - /** | |
| 13 | - * Stores the plugin options. | |
| 14 | - * | |
| 15 | - * @var array | |
| 16 | - */ | |
| 17 | - public $options; | |
| 9 | + public $links_model, $model, $options; | |
| 18 | 10 | |
| 19 | - /** | |
| 20 | - * Instance of PLL_Model. | |
| 11 | + /* | |
| 12 | + * constructor | |
| 21 | 13 | * |
| 22 | - * @var PLL_Model | |
| 23 | - */ | |
| 24 | - public $model; | |
| 25 | - | |
| 26 | - /** | |
| 27 | - * Instance of a child class of PLL_Links_Model. | |
| 28 | - * | |
| 29 | - * @var PLL_Links_Model | |
| 30 | - */ | |
| 31 | - public $links_model; | |
| 32 | - | |
| 33 | - /** | |
| 34 | - * Registers hooks on insert / update post related actions and filters. | |
| 35 | - * | |
| 36 | - * @var PLL_CRUD_Posts | |
| 37 | - */ | |
| 38 | - public $posts; | |
| 39 | - | |
| 40 | - /** | |
| 41 | - * Registers hooks on insert / update term related action and filters. | |
| 42 | - * | |
| 43 | - * @var PLL_CRUD_Terms | |
| 44 | - */ | |
| 45 | - public $terms; | |
| 46 | - | |
| 47 | - /** | |
| 48 | - * Constructor | |
| 49 | - * | |
| 50 | 14 | * @since 1.2 |
| 51 | 15 | * |
| 52 | 16 | * @param object $links_model |
| 53 | 17 | */ |
| 54 | - public function __construct( &$links_model ) { | |
| 18 | + public function __construct(&$links_model) { | |
| 55 | 19 | $this->links_model = &$links_model; |
| 56 | 20 | $this->model = &$links_model->model; |
| 57 | 21 | $this->options = &$this->model->options; |
| 58 | 22 | |
| 59 | - $GLOBALS['l10n_unloaded']['pll_string'] = true; // Short-circuit _load_textdomain_just_in_time() for 'pll_string' domain in WP 4.6+ | |
| 23 | + add_action('widgets_init', array(&$this, 'widgets_init')); | |
| 60 | 24 | |
| 61 | - add_action( 'widgets_init', array( $this, 'widgets_init' ) ); | |
| 62 | - | |
| 63 | - // User defined strings translations | |
| 64 | - add_action( 'pll_language_defined', array( $this, 'load_strings_translations' ), 5 ); | |
| 65 | - add_action( 'change_locale', array( $this, 'load_strings_translations' ) ); // Since WP 4.7 | |
| 66 | - | |
| 67 | - // Switch_to_blog | |
| 68 | - add_action( 'switch_blog', array( $this, 'switch_blog' ), 10, 2 ); | |
| 25 | + // user defined strings translations | |
| 26 | + add_action('pll_language_defined', array(&$this, 'load_strings_translations'), 5); | |
| 69 | 27 | } |
| 70 | 28 | |
| 71 | - /** | |
| 72 | - * Instantiates classes reacting to CRUD operations on posts and terms, | |
| 73 | - * only when at least one language is defined. | |
| 29 | + /* | |
| 30 | + * registers our widgets | |
| 74 | 31 | * |
| 75 | - * @since 2.6 | |
| 76 | - */ | |
| 77 | - public function init() { | |
| 78 | - if ( $this->model->get_languages_list() ) { | |
| 79 | - $this->posts = new PLL_CRUD_Posts( $this ); | |
| 80 | - $this->terms = new PLL_CRUD_Terms( $this ); | |
| 81 | - | |
| 82 | - // WordPress options. | |
| 83 | - new PLL_Translate_Option( 'blogname', array(), array( 'context' => 'WordPress' ) ); | |
| 84 | - new PLL_Translate_Option( 'blogdescription', array(), array( 'context' => 'WordPress' ) ); | |
| 85 | - new PLL_Translate_Option( 'date_format', array(), array( 'context' => 'WordPress' ) ); | |
| 86 | - new PLL_Translate_Option( 'time_format', array(), array( 'context' => 'WordPress' ) ); | |
| 87 | - } | |
| 88 | - } | |
| 89 | - | |
| 90 | - /** | |
| 91 | - * Registers our widgets | |
| 92 | - * | |
| 93 | 32 | * @since 0.1 |
| 94 | 33 | */ |
| 95 | 34 | public function widgets_init() { |
| 96 | - register_widget( 'PLL_Widget_Languages' ); | |
| 35 | + register_widget('PLL_Widget_Languages'); | |
| 97 | 36 | |
| 98 | - // Overwrites the calendar widget to filter posts by language | |
| 99 | - if ( ! defined( 'PLL_WIDGET_CALENDAR' ) || PLL_WIDGET_CALENDAR ) { | |
| 100 | - unregister_widget( 'WP_Widget_Calendar' ); | |
| 101 | - register_widget( 'PLL_Widget_Calendar' ); | |
| 37 | + // overwrites the calendar widget to filter posts by language | |
| 38 | + if (!defined('PLL_WIDGET_CALENDAR') || PLL_WIDGET_CALENDAR) { | |
| 39 | + unregister_widget('WP_Widget_Calendar'); | |
| 40 | + register_widget('PLL_Widget_Calendar'); | |
| 102 | 41 | } |
| 103 | - } | |
| 104 | 42 | |
| 105 | - /** | |
| 106 | - * Loads user defined strings translations | |
| 107 | - * | |
| 108 | - * @since 1.2 | |
| 109 | - * @since 2.1.3 $locale parameter added. | |
| 110 | - * | |
| 111 | - * @param string $locale Locale. Defaults to current locale. | |
| 112 | - */ | |
| 113 | - public function load_strings_translations( $locale = '' ) { | |
| 114 | - if ( empty( $locale ) ) { | |
| 115 | - $locale = get_locale(); | |
| 116 | - } | |
| 43 | + // overwrites the recent posts and recent comments widget to use a language dependant cache key | |
| 44 | + // useful only if using a cache plugin | |
| 45 | + if (defined('WP_CACHE') && WP_CACHE) { | |
| 46 | + if (!defined('PLL_WIDGET_RECENT_POSTS') || PLL_WIDGET_RECENT_POSTS) { | |
| 47 | + unregister_widget('WP_Widget_Recent_Posts'); | |
| 48 | + register_widget('PLL_Widget_Recent_Posts'); | |
| 49 | + } | |
| 117 | 50 | |
| 118 | - $language = $this->model->get_language( $locale ); | |
| 119 | - | |
| 120 | - if ( ! empty( $language ) ) { | |
| 121 | - $mo = new PLL_MO(); | |
| 122 | - $mo->import_from_db( $language ); | |
| 123 | - $GLOBALS['l10n']['pll_string'] = &$mo; | |
| 124 | - } else { | |
| 125 | - unset( $GLOBALS['l10n']['pll_string'] ); | |
| 51 | + if (!defined('PLL_WIDGET_RECENT_COMMENTS') || PLL_WIDGET_RECENT_COMMENTS) { | |
| 52 | + unregister_widget('WP_Widget_Recent_Comments'); | |
| 53 | + register_widget('PLL_Widget_Recent_Comments'); | |
| 54 | + } | |
| 126 | 55 | } |
| 127 | 56 | } |
| 128 | 57 | |
| 129 | - /** | |
| 130 | - * Resets some variables when switching blog | |
| 131 | - * Applies only if Polylang is active on the new blog | |
| 58 | + /* | |
| 59 | + * loads user defined strings translations | |
| 132 | 60 | * |
| 133 | - * @since 1.5.1 | |
| 134 | - * | |
| 135 | - * @param int $new_blog | |
| 136 | - * @param int $old_blog | |
| 137 | - * @return bool not used by WP but by child class | |
| 61 | + * @since 1.2 | |
| 138 | 62 | */ |
| 139 | - public function switch_blog( $new_blog, $old_blog ) { | |
| 140 | - $plugins = ( $sitewide_plugins = get_site_option( 'active_sitewide_plugins' ) ) && is_array( $sitewide_plugins ) ? array_keys( $sitewide_plugins ) : array(); | |
| 141 | - $plugins = array_merge( $plugins, get_option( 'active_plugins', array() ) ); | |
| 142 | - | |
| 143 | - // 2nd test needed when Polylang is not networked activated | |
| 144 | - // 3rd test needed when Polylang is networked activated and a new site is created | |
| 145 | - if ( $new_blog != $old_blog && in_array( POLYLANG_BASENAME, $plugins ) && get_option( 'polylang' ) ) { | |
| 146 | - $this->options = get_option( 'polylang' ); // Needed for menus | |
| 147 | - remove_action( 'pre_option_rewrite_rules', array( $this->links_model, 'prepare_rewrite_rules' ) ); | |
| 148 | - $this->links_model = $this->model->get_links_model(); | |
| 149 | - return true; | |
| 150 | - } | |
| 151 | - return false; | |
| 63 | + public function load_strings_translations() { | |
| 64 | + $mo = new PLL_MO(); | |
| 65 | + $mo->import_from_db($this->model->get_language(get_locale())); | |
| 66 | + $GLOBALS['l10n']['pll_string'] = &$mo; | |
| 152 | 67 | } |
| 153 | 68 | |
| 154 | - /** | |
| 155 | - * Some backward compatibility with Polylang < 1.2 | |
| 156 | - * Allows for example to call $polylang->get_languages_list() instead of $polylang->model->get_languages_list() | |
| 157 | - * This works but should be slower than the direct call, thus an error is triggered in debug mode | |
| 69 | + /* | |
| 70 | + * some backward compatibility with Polylang < 1.2 | |
| 71 | + * allows for example to call $polylang->get_languages_list() instead of $polylang->model->get_languages_list() | |
| 72 | + * this works but should be slower than the direct call, thus an error is triggered in debug mode | |
| 158 | 73 | * |
| 159 | 74 | * @since 1.2 |
| 160 | 75 | * |
| 161 | 76 | * @param string $func function name |
| 162 | - * @param array $args function arguments | |
| 77 | + * @param array $args function arguments | |
| 163 | 78 | */ |
| 164 | - public function __call( $func, $args ) { | |
| 165 | - foreach ( $this as $prop => &$obj ) { | |
| 166 | - if ( is_object( $obj ) && method_exists( $obj, $func ) ) { | |
| 167 | - if ( WP_DEBUG ) { | |
| 168 | - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 169 | - $i = 1 + empty( $debug[1]['line'] ); // The file and line are in $debug[2] if the function was called using call_user_func | |
| 170 | - trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 171 | - sprintf( | |
| 172 | - '%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", | |
| 173 | - esc_html( $func ), | |
| 174 | - esc_html( $prop ), | |
| 175 | - esc_html( $debug[ $i ]['file'] ), | |
| 176 | - absint( $debug[ $i ]['line'] ) | |
| 177 | - ) | |
| 178 | - ); | |
| 79 | + public function __call($func, $args) { | |
| 80 | + foreach ($this as $prop => &$obj) | |
| 81 | + if (is_object($obj) && method_exists($obj, $func)) { | |
| 82 | + if (WP_DEBUG) { | |
| 83 | + $debug = debug_backtrace(); | |
| 84 | + trigger_error(sprintf( | |
| 85 | + '%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", | |
| 86 | + $func, $prop, $debug[1]['file'], $debug[1]['line'] | |
| 87 | + )); | |
| 179 | 88 | } |
| 180 | - return call_user_func_array( array( $obj, $func ), $args ); | |
| 89 | + return call_user_func_array(array($obj, $func), $args); | |
| 181 | 90 | } |
| 182 | - } | |
| 183 | 91 | |
| 184 | - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 185 | - trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 186 | - sprintf( | |
| 187 | - 'Call to undefined function PLL()->%1$s() in %2$s on line %3$s' . "\nError handler", | |
| 188 | - esc_html( $func ), | |
| 189 | - esc_html( $debug[0]['file'] ), | |
| 190 | - absint( $debug[0]['line'] ) | |
| 191 | - ), | |
| 192 | - E_USER_ERROR | |
| 193 | - ); | |
| 92 | + $debug = debug_backtrace(); | |
| 93 | + 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); | |
| 194 | 94 | } |
| 195 | 95 | } |