PluginProbe
Polylang / trunk
Polylang vtrunk
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / src / frontend / frontend-filters-widgets.php

frontend-filters-widgets.php in Polylang trunk, at src/frontend/frontend-filters-widgets.php

154 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 /**
7 * Filters widgets by language on frontend
8 *
9 * @since 3.1
10 */
11 class PLL_Frontend_Filters_Widgets {
12 /**
13 * Internal non persistent cache object.
14 *
15 * @var PLL_Cache<array>
16 */
17 public $cache;
18
19 /**
20 * Current language.
21 *
22 * @var PLL_Language|null
23 */
24 public $curlang;
25
26 /**
27 * Constructor: setups filters and actions.
28 *
29 * @since 1.2
30 *
31 * @param object $polylang The Polylang object.
32 */
33 public function __construct( &$polylang ) {
34 $this->curlang = &$polylang->curlang;
35 $this->cache = new PLL_Cache();
36
37 add_filter( 'sidebars_widgets', array( $this, 'sidebars_widgets' ) );
38 }
39
40 /**
41 * Remove widgets from sidebars if they are not visible in the current language
42 * Needed to allow is_active_sidebar() to return false if all widgets are not for the current language. See #54
43 *
44 * @since 2.1
45 * @since 2.4 The result is cached as the function can be very expensive in case there are a lot of widgets
46 *
47 * @param array $sidebars_widgets An associative array of sidebars and their widgets
48 * @return array
49 */
50 public function sidebars_widgets( $sidebars_widgets ) {
51 global $wp_registered_widgets;
52
53 if ( empty( $wp_registered_widgets ) ) {
54 return $sidebars_widgets;
55 }
56
57 $cache_key = $this->cache->get_unique_key( 'sidebars_widgets_', $sidebars_widgets );
58 $_sidebars_widgets = $this->cache->get( $cache_key );
59
60 if ( false !== $_sidebars_widgets ) {
61 return $_sidebars_widgets;
62 }
63
64 $sidebars_widgets = $this->filter_widgets_sidebars( $sidebars_widgets, $wp_registered_widgets );
65
66 return $this->cache->set( $cache_key, $sidebars_widgets );
67 }
68
69 /**
70 * Method that handles the removal of widgets in the sidebars depending on their display language.
71 *
72 * @since 3.1
73 *
74 * @param array $widget_data An array containing the widget data
75 * @param array $sidebars_widgets An associative array of sidebars and their widgets
76 * @param string $sidebar Sidebar name
77 * @param int $key Widget number
78 * @return array An associative array of sidebars and their widgets
79 */
80 public function handle_widget_in_sidebar_callback( $widget_data, $sidebars_widgets, $sidebar, $key ) {
81 // Remove the widget if not visible in the current language
82 if ( ! empty( $widget_data['settings'][ $widget_data['number'] ]['pll_lang'] ) && $widget_data['settings'][ $widget_data['number'] ]['pll_lang'] !== $this->curlang->slug ) {
83 unset( $sidebars_widgets[ $sidebar ][ $key ] );
84 }
85 return $sidebars_widgets;
86 }
87
88 /**
89 * Browse the widgets sidebars and sort the ones that should be displayed or not.
90 *
91 * @since 3.1
92 *
93 * @param array $sidebars_widgets An associative array of sidebars and their widgets
94 * @param array $wp_registered_widgets Array of all registered widgets.
95 * @return array An associative array of sidebars and their widgets
96 */
97 public function filter_widgets_sidebars( $sidebars_widgets, $wp_registered_widgets ) {
98 foreach ( $sidebars_widgets as $sidebar => $widgets ) {
99 if ( 'wp_inactive_widgets' === $sidebar || empty( $widgets ) ) {
100 continue;
101 }
102
103 foreach ( $widgets as $key => $widget ) {
104 if ( ! $this->is_widget_object( $wp_registered_widgets, $widget ) ) {
105 continue;
106 }
107
108 $widget_data = $this->get_widget_data( $wp_registered_widgets, $widget );
109
110 $sidebars_widgets = $this->handle_widget_in_sidebar_callback( $widget_data, $sidebars_widgets, $sidebar, $key );
111 }
112 }
113 return $sidebars_widgets;
114 }
115
116 /**
117 * Test if the widget is an object.
118 *
119 * @since 3.1
120 *
121 * @param array $wp_registered_widgets Array of all registered widgets.
122 * @param string $widget String that identifies the widget.
123 * @return bool True if object, false otherwise.
124 */
125 protected function is_widget_object( $wp_registered_widgets, $widget ) {
126 // Nothing can be done if the widget is created using pre WP2.8 API :(
127 // There is no object, so we can't access it to get the widget options
128 return isset( $wp_registered_widgets[ $widget ]['callback'] ) &&
129 is_array( $wp_registered_widgets[ $widget ]['callback'] ) &&
130 isset( $wp_registered_widgets[ $widget ]['callback'][0] ) &&
131 is_object( $wp_registered_widgets[ $widget ]['callback'][0] ) &&
132 method_exists( $wp_registered_widgets[ $widget ]['callback'][0], 'get_settings' );
133 }
134
135 /**
136 * Get widgets settings and number.
137 *
138 * @since 3.1
139 *
140 * @param array $wp_registered_widgets Array of all registered widgets.
141 * @param string $widget String that identifies the widget.
142 * @return array An array containing the widget settings and number.
143 */
144 protected function get_widget_data( $wp_registered_widgets, $widget ) {
145 $widget_settings = $wp_registered_widgets[ $widget ]['callback'][0]->get_settings();
146 $number = $wp_registered_widgets[ $widget ]['params'][0]['number'];
147
148 return array(
149 'settings' => $widget_settings,
150 'number' => $number,
151 );
152 }
153 }
154