Tasks
1 month ago
DeprecatedExtendedTask.php
2 years ago
DeprecatedOptions.php
3 months ago
Init.php
3 years ago
Task.php
11 months ago
TaskList.php
4 weeks ago
TaskListSection.php
3 years ago
TaskLists.php
4 months ago
TaskTraits.php
4 years ago
DeprecatedOptions.php
103 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Filters for maintaining backwards compatibility with deprecated options. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks; |
| 7 | |
| 8 | use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\TaskList; |
| 9 | use WC_Install; |
| 10 | |
| 11 | /** |
| 12 | * DeprecatedOptions class. |
| 13 | */ |
| 14 | class DeprecatedOptions { |
| 15 | /** |
| 16 | * Initialize. |
| 17 | * |
| 18 | * @internal |
| 19 | */ |
| 20 | final public static function init(): void { |
| 21 | add_filter( 'pre_option_woocommerce_task_list_complete', array( __CLASS__, 'get_deprecated_options' ), 10, 2 ); |
| 22 | add_filter( 'pre_option_woocommerce_task_list_hidden', array( __CLASS__, 'get_deprecated_options' ), 10, 2 ); |
| 23 | add_filter( 'pre_option_woocommerce_extended_task_list_hidden', array( __CLASS__, 'get_deprecated_options' ), 10, 2 ); |
| 24 | add_filter( 'pre_update_option_woocommerce_task_list_complete', array( __CLASS__, 'update_deprecated_options' ), 10, 3 ); |
| 25 | add_filter( 'pre_update_option_woocommerce_task_list_hidden', array( __CLASS__, 'update_deprecated_options' ), 10, 3 ); |
| 26 | add_filter( 'pre_update_option_woocommerce_extended_task_list_hidden', array( __CLASS__, 'update_deprecated_options' ), 10, 3 ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Get the values from the correct source when attempting to retrieve deprecated options. |
| 31 | * |
| 32 | * @param mixed $pre_option Pre option value. |
| 33 | * @param string $option Option name. |
| 34 | * @return mixed |
| 35 | */ |
| 36 | public static function get_deprecated_options( $pre_option, $option ) { |
| 37 | if ( defined( 'WC_INSTALLING' ) && WC_INSTALLING === true ) { |
| 38 | return $pre_option; |
| 39 | } |
| 40 | |
| 41 | switch ( $option ) { |
| 42 | case 'woocommerce_task_list_complete': |
| 43 | $completed = get_option( 'woocommerce_task_list_completed_lists', array() ); |
| 44 | return is_array( $completed ) && in_array( 'setup', $completed, true ) ? 'yes' : 'no'; |
| 45 | case 'woocommerce_task_list_hidden': |
| 46 | $hidden = get_option( 'woocommerce_task_list_hidden_lists', array() ); |
| 47 | return is_array( $hidden ) && in_array( 'setup', $hidden, true ) ? 'yes' : 'no'; |
| 48 | case 'woocommerce_extended_task_list_hidden': |
| 49 | $hidden = get_option( 'woocommerce_task_list_hidden_lists', array() ); |
| 50 | return is_array( $hidden ) && in_array( 'extended', $hidden, true ) ? 'yes' : 'no'; |
| 51 | default: |
| 52 | return $pre_option; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Updates the new option names when deprecated options are updated. |
| 58 | * This is a temporary fallback until we can fully remove the old task list components. |
| 59 | * |
| 60 | * @param mixed $value New value. |
| 61 | * @param mixed $old_value Old value. |
| 62 | * @param string $option Option name. |
| 63 | * @return mixed |
| 64 | */ |
| 65 | public static function update_deprecated_options( $value, $old_value, $option ) { |
| 66 | switch ( $option ) { |
| 67 | case 'woocommerce_task_list_complete': |
| 68 | $completed = get_option( 'woocommerce_task_list_completed_lists', array() ); |
| 69 | if ( is_array( $completed ) ) { |
| 70 | if ( 'yes' === $value ) { |
| 71 | if ( ! in_array( 'setup', $completed, true ) ) { |
| 72 | $completed[] = 'setup'; |
| 73 | update_option( 'woocommerce_task_list_completed_lists', $completed, true ); |
| 74 | } |
| 75 | } else { |
| 76 | $completed = array_diff( $completed, array( 'setup' ) ); |
| 77 | update_option( 'woocommerce_task_list_completed_lists', array_values( $completed ), true ); |
| 78 | } |
| 79 | delete_option( 'woocommerce_task_list_complete' ); |
| 80 | } |
| 81 | return $old_value; |
| 82 | case 'woocommerce_task_list_hidden': |
| 83 | $task_list = TaskLists::get_list( 'setup' ); |
| 84 | if ( ! $task_list ) { |
| 85 | return $value; |
| 86 | } |
| 87 | $update = 'yes' === $value ? $task_list->hide() : $task_list->unhide(); |
| 88 | delete_option( 'woocommerce_task_list_hidden' ); |
| 89 | return $old_value; |
| 90 | case 'woocommerce_extended_task_list_hidden': |
| 91 | $task_list = TaskLists::get_list( 'extended' ); |
| 92 | if ( ! $task_list ) { |
| 93 | return $value; |
| 94 | } |
| 95 | $update = 'yes' === $value ? $task_list->hide() : $task_list->unhide(); |
| 96 | delete_option( 'woocommerce_extended_task_list_hidden' ); |
| 97 | return $old_value; |
| 98 | default: |
| 99 | return $value; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 |