Complex classes like Wpml often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Wpml, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | final class Wpml { |
||
23 | |||
24 | /** |
||
25 | * The single instance of the class |
||
26 | * |
||
27 | * @var object |
||
28 | * @since 3.0.0 |
||
29 | */ |
||
30 | protected static $_instance = null; |
||
31 | |||
32 | /** |
||
33 | * Flag to keep track of removed WPML filter on widget title |
||
34 | * |
||
35 | * @var boolean |
||
36 | * @since 3.0.0 |
||
37 | */ |
||
38 | private $removed_widget_title_filter = false; |
||
39 | |||
40 | /** |
||
41 | * Flag to keep track of removed WPML filter on widget text |
||
42 | * |
||
43 | * @var boolean |
||
44 | * @since 3.0.0 |
||
45 | */ |
||
46 | private $removed_widget_text_filter = false; |
||
47 | |||
48 | /** |
||
49 | * Return the single class instance |
||
50 | * |
||
51 | * @return object |
||
52 | * @since 3.0.0 |
||
53 | */ |
||
54 | public static function instance() { |
||
60 | |||
61 | /** |
||
62 | * Class constructor |
||
63 | * |
||
64 | * @uses add_action() |
||
65 | * @uses add_filter() |
||
66 | * |
||
67 | * @since 3.0.0 |
||
68 | */ |
||
69 | protected function __construct() { |
||
80 | |||
81 | /** |
||
82 | * Prevent the class from being cloned |
||
83 | * |
||
84 | * @return void |
||
85 | * @since 3.0.0 |
||
86 | */ |
||
87 | protected function __clone() { |
||
90 | |||
91 | /** |
||
92 | * Helper function to get WPML version |
||
93 | * |
||
94 | * @uses get_plugin_data() |
||
95 | * |
||
96 | * @return string |
||
97 | * @since 3.0.0 |
||
98 | */ |
||
99 | public function get_version() { |
||
103 | |||
104 | /** |
||
105 | * Initialize compatibility with WPML and WPML Widgets plugins |
||
106 | * |
||
107 | * @uses is_plugin_active() |
||
108 | * @uses has_action() |
||
109 | * @uses remove_action() |
||
110 | * |
||
111 | * @return void |
||
112 | * @since 3.0.0 |
||
113 | */ |
||
114 | public function init() { |
||
121 | |||
122 | /** |
||
123 | * Disable WPML String translation native behavior |
||
124 | * |
||
125 | * @uses is_plugin_active() |
||
126 | * @uses has_filter() |
||
127 | * @uses remove_filter() |
||
128 | * |
||
129 | * @param mixed[] $args Array of arguments. |
||
130 | * @param mixed[] $instance Widget instance. |
||
131 | * @return void |
||
132 | * @since 3.0.0 |
||
133 | */ |
||
134 | public function widget_before( $args, $instance ) { |
||
160 | |||
161 | /** |
||
162 | * Re-Enable WPML String translation native behavior |
||
163 | * |
||
164 | * @uses add_filter() |
||
165 | * @uses has_filter() |
||
166 | * |
||
167 | * @return void |
||
168 | * @since 3.0.0 |
||
169 | */ |
||
170 | public function widget_after() { |
||
188 | |||
189 | /** |
||
190 | * Add widget text to WPML String translation |
||
191 | * |
||
192 | * @uses is_plugin_active() |
||
193 | * @uses icl_register_string() Part of WPML |
||
194 | * |
||
195 | * @param mixed[] $instance Array of arguments. |
||
196 | * @param object $widget Widget instance. |
||
197 | * @return mixed[] |
||
198 | * @since 3.0.0 |
||
199 | */ |
||
200 | public function widget_update( $instance, $widget ) { |
||
214 | |||
215 | /** |
||
216 | * Translate widget text |
||
217 | * |
||
218 | * @uses is_plugin_active() |
||
219 | * @uses icl_t() Part of WPML |
||
220 | * @uses icl_st_is_registered_string() Part of WPML |
||
221 | * |
||
222 | * @param string $text Widget text. |
||
223 | * @param mixed[]|null $instance Widget instance. |
||
224 | * @param object|null $widget Widget object. |
||
225 | * @return string |
||
226 | * @since 3.0.0 |
||
227 | */ |
||
228 | public function widget_text( $text, $instance = null, $widget = null ) { |
||
243 | |||
244 | /** |
||
245 | * Check for existing deprecated translations (made with WPML String Translations plugin) and display warning |
||
246 | * |
||
247 | * @uses is_plugin_active() |
||
248 | * @uses icl_st_is_registered_string() Part of WPML |
||
249 | * @uses admin_url() |
||
250 | * |
||
251 | * @param mixed[]|null $instance Widget instance. |
||
252 | * @param object|null $widget Widget object. |
||
253 | * @return void |
||
254 | * @since 2.6.0 |
||
255 | */ |
||
256 | public function check_deprecated_translations( $instance, $widget ) { |
||
269 | |||
270 | } // END class |
||
271 | |||
273 |