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