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() { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Prevent the class from being cloned |
||
| 85 | * |
||
| 86 | * @return void |
||
| 87 | * @since 3.0.0 |
||
| 88 | */ |
||
| 89 | protected function __clone() { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Helper function to get WPML version |
||
| 95 | * |
||
| 96 | * @uses get_plugin_data() |
||
| 97 | * |
||
| 98 | * @return string |
||
| 99 | * @since 3.0.0 |
||
| 100 | */ |
||
| 101 | public function get_version() { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Initialize compatibility with WPML and WPML Widgets plugins |
||
| 108 | * |
||
| 109 | * @uses is_plugin_active() |
||
| 110 | * @uses has_action() |
||
| 111 | * @uses remove_action() |
||
| 112 | * |
||
| 113 | * @return void |
||
| 114 | * @since 3.0.0 |
||
| 115 | */ |
||
| 116 | public function init() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Avoid native WPML string translation of widget titles |
||
| 126 | * for widgets inserted in pages built with Page Builder (SiteOrigin panels) |
||
| 127 | * and also when WPML Widgets is active and for WPML versions from 3.8.0 on |
||
| 128 | * |
||
| 129 | * @uses is_plugin_active() |
||
| 130 | * @uses has_filter() |
||
| 131 | * @uses remove_filter() |
||
| 132 | * |
||
| 133 | * @param mixed[] $args Array of arguments. |
||
| 134 | * @param mixed[] $instance Widget instance. |
||
| 135 | * @return void |
||
| 136 | * @since 3.0.0 |
||
| 137 | */ |
||
| 138 | public function disable_title_translation( $args, $instance ) { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Avoid native WPML string translation of widget texts (for all widgets) |
||
| 151 | * Note: Black Studio TinyMCE Widget already supports WPML string translation, |
||
| 152 | * so this is needed to prevent duplicate translations |
||
| 153 | * |
||
| 154 | * @uses is_plugin_active() |
||
| 155 | * @uses has_filter() |
||
| 156 | * @uses remove_filter() |
||
| 157 | * |
||
| 158 | * @return void |
||
| 159 | * @since 3.0.0 |
||
| 160 | */ |
||
| 161 | public function disable_text_translation() { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Restore widget title's native WPML string translation filter if it was removed |
||
| 172 | * |
||
| 173 | * @uses add_filter() |
||
| 174 | * @uses has_filter() |
||
| 175 | * |
||
| 176 | * @return void |
||
| 177 | * @since 3.0.0 |
||
| 178 | */ |
||
| 179 | public function restore_title_translation() { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Restore widget text's native WPML string translation filter if it was removed |
||
| 192 | * |
||
| 193 | * @uses add_filter() |
||
| 194 | * @uses has_filter() |
||
| 195 | * |
||
| 196 | * @return void |
||
| 197 | * @since 3.0.0 |
||
| 198 | */ |
||
| 199 | public function restore_text_translation() { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Add widget text to WPML String translation |
||
| 212 | * |
||
| 213 | * @uses is_plugin_active() |
||
| 214 | * @uses icl_register_string() Part of WPML |
||
| 215 | * |
||
| 216 | * @param mixed[] $instance Array of arguments. |
||
| 217 | * @param object $widget Widget instance. |
||
| 218 | * @return mixed[] |
||
| 219 | * @since 3.0.0 |
||
| 220 | */ |
||
| 221 | public function widget_update( $instance, $widget ) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Translate widget text |
||
| 238 | * |
||
| 239 | * @uses is_plugin_active() |
||
| 240 | * @uses icl_t() Part of WPML |
||
| 241 | * @uses icl_st_is_registered_string() Part of WPML |
||
| 242 | * |
||
| 243 | * @param string $text Widget text. |
||
| 244 | * @param mixed[]|null $instance Widget instance. |
||
| 245 | * @param object|null $widget Widget object. |
||
| 246 | * @return string |
||
| 247 | * @since 3.0.0 |
||
| 248 | */ |
||
| 249 | public function widget_text( $text, $instance = null, $widget = null ) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Check for existing deprecated translations (made with WPML String Translations plugin) and display warning |
||
| 267 | * |
||
| 268 | * @uses is_plugin_active() |
||
| 269 | * @uses icl_st_is_registered_string() Part of WPML |
||
| 270 | * @uses admin_url() |
||
| 271 | * |
||
| 272 | * @param mixed[]|null $instance Widget instance. |
||
| 273 | * @param object|null $widget Widget object. |
||
| 274 | * @return void |
||
| 275 | * @since 2.6.0 |
||
| 276 | */ |
||
| 277 | public function check_deprecated_translations( $instance, $widget ) { |
||
| 291 | |||
| 292 | } // END class |
||
| 293 | |||
| 295 |