Complex classes like GravityView_Edit_Entry_Locking 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 GravityView_Edit_Entry_Locking, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class GravityView_Edit_Entry_Locking { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Load extension entry point. |
||
| 17 | * |
||
| 18 | * DO NOT RENAME this method. Required by the class-edit-entry.php component loader. |
||
| 19 | * @see GravityView_Edit_Entry::load_components() |
||
| 20 | * |
||
| 21 | * @since 2.5.2 |
||
| 22 | * |
||
| 23 | * @return void |
||
| 24 | */ |
||
| 25 | 22 | public function load() { |
|
| 35 | |||
| 36 | // TODO: Convert to extending Gravity Forms |
||
| 37 | public function ajax_lock_request() { |
||
| 43 | |||
| 44 | // TODO: Convert to extending Gravity Forms |
||
| 45 | public function ajax_reject_lock_request() { |
||
| 51 | |||
| 52 | // TODO: Convert to extending Gravity Forms |
||
| 53 | protected function delete_lock_request_meta( $object_id ) { |
||
| 58 | |||
| 59 | // TODO: Convert to extending Gravity Forms |
||
| 60 | protected function request_lock( $object_id ) { |
||
| 88 | |||
| 89 | protected function update_lock_request_meta( $object_id, $lock_request_value ) { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Checks whether to enqueue scripts based on: |
||
| 95 | * |
||
| 96 | * - Is it Edit Entry? |
||
| 97 | * - Is the entry connected to a View that has `edit_locking` enabled? |
||
| 98 | * - Is the entry connected to a form connected to a currently-loaded View? |
||
| 99 | * |
||
| 100 | * @internal |
||
| 101 | * @since 2.6.1 |
||
| 102 | * |
||
| 103 | * @global WP_Post $post |
||
| 104 | * |
||
| 105 | * @return void |
||
| 106 | */ |
||
| 107 | public function maybe_enqueue_scripts() { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Enqueue the required scripts and styles from Gravity Forms. |
||
| 155 | * |
||
| 156 | * Called via load() and `wp_enqueue_scripts` |
||
| 157 | * |
||
| 158 | * @since 2.5.2 |
||
| 159 | * |
||
| 160 | * @param array $entry Gravity Forms entry array |
||
| 161 | * |
||
| 162 | * @return void |
||
| 163 | */ |
||
| 164 | protected function enqueue_scripts( $entry ) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Returns a string with the Lock UI HTML markup. |
||
| 198 | * |
||
| 199 | * Called script enqueuing, added to JavaScript gforms_locking global variable. |
||
| 200 | * |
||
| 201 | * @since 2.5.2 |
||
| 202 | * |
||
| 203 | * @see GravityView_Edit_Entry_Locking::check_lock |
||
| 204 | * |
||
| 205 | * @param int $user_id The User ID that has the current lock. Will be empty if entry is not locked |
||
| 206 | * or is locked to the current user. |
||
| 207 | * |
||
| 208 | * @return string The Lock UI dialog box, etc. |
||
| 209 | */ |
||
| 210 | public function get_lock_ui( $user_id ) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Localized string for the UI. |
||
| 268 | * |
||
| 269 | * Uses gravityforms textdomain unchanged. |
||
| 270 | * |
||
| 271 | * @since 2.5.2 |
||
| 272 | * |
||
| 273 | * @return array An array of translations. |
||
| 274 | */ |
||
| 275 | public function get_strings() { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Get a localized string. |
||
| 298 | * |
||
| 299 | * @param string $string The string to get. |
||
| 300 | * |
||
| 301 | * @return string A localized string. See self::get_strings() |
||
| 302 | */ |
||
| 303 | public function get_string( $string ) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Lock the entry... maybe. |
||
| 309 | * |
||
| 310 | * Has 3 modes of locking: |
||
| 311 | * |
||
| 312 | * - acquire (get), which reloads the page after locking the entry |
||
| 313 | * - release, which reloads the page after unlocking the entry |
||
| 314 | * - default action to lock on load if not locked |
||
| 315 | * |
||
| 316 | * @param int $entry_id The entry ID. |
||
| 317 | * |
||
| 318 | * @return void |
||
| 319 | */ |
||
| 320 | 22 | public function maybe_lock_object( $entry_id ) { |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Is this entry locked to some other user? |
||
| 343 | * |
||
| 344 | * @param int $entry_id The entry ID. |
||
| 345 | * |
||
| 346 | * @return boolean Yes or no. |
||
| 347 | */ |
||
| 348 | 22 | public function check_lock( $entry_id ) { |
|
| 359 | |||
| 360 | /** |
||
| 361 | * The lock for an entry. |
||
| 362 | * |
||
| 363 | * Leverages Gravity Forms' persistent caching mechanisms. |
||
| 364 | * |
||
| 365 | * @param int $entry_id The entry ID. |
||
| 366 | * |
||
| 367 | * @return int|null The User ID or null. |
||
| 368 | */ |
||
| 369 | 22 | public function get_lock_meta( $entry_id ) { |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Set the lock for an entry. |
||
| 375 | * |
||
| 376 | * @param int $entry_id The entry ID. |
||
| 377 | * @param int $user_id The user ID to lock the entry to. |
||
| 378 | * |
||
| 379 | * @return void |
||
| 380 | */ |
||
| 381 | 22 | public function update_lock_meta( $entry_id, $user_id ) { |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Release the lock for an entry. |
||
| 387 | * |
||
| 388 | * @param int $entry_id The entry ID. |
||
| 389 | * |
||
| 390 | * @return void |
||
| 391 | */ |
||
| 392 | public function delete_lock_meta( $entry_id ) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Lock the entry to the current user. |
||
| 398 | * |
||
| 399 | * @since 2.5.2 |
||
| 400 | * |
||
| 401 | * @param int $entry_id The entry ID. |
||
| 402 | * |
||
| 403 | * @return int|false Locked or not. |
||
| 404 | */ |
||
| 405 | 22 | public function set_lock( $entry_id ) { |
|
| 421 | } |
||
| 422 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: