sk_list_id = null ) { $task_list = $task_list_id ? self::get_list( $task_list_id ) : null; if ( $task_list_id && ! $task_list ) { return null; } $tasks_to_search = $task_list ? $task_list->tasks : array_reduce( self::get_lists(), function ( $all, $curr ) { return array_merge( $all, $curr->tasks ); }, array() ); foreach ( $tasks_to_search as $task ) { if ( $id === $task->get_id() ) { return $task; } } return null; } /** * Return number of setup tasks remaining * * This is not updated immediately when a task is completed, but rather when task is marked as complete in the database to reduce performance impact. * * @return int|null */ public static function setup_tasks_remaining() { $setup_list = self::get_list( 'setup' ); if ( ! $setup_list || $setup_list->is_hidden() || $setup_list->has_previously_completed() ) { return; } $viewable_tasks = $setup_list->get_viewable_tasks(); $completed_tasks = get_option( Task::COMPLETED_OPTION, array() ); if ( ! is_array( $completed_tasks ) ) { $completed_tasks = array(); } return count( array_filter( $viewable_tasks, function ( $task ) use ( $completed_tasks ) { return ! in_array( $task->get_id(), $completed_tasks, true ); } ) ); } /** * Add badge to homescreen menu item for remaining tasks */ public static function menu_task_count() { global $submenu; $tasks_count = self::setup_tasks_remaining(); if ( ! $tasks_count || ! isset( $submenu['woocommerce'] ) ) { return; } foreach ( $submenu['woocommerce'] as $key => $menu_item ) { if ( 0 === strpos( $menu_item[0], _x( 'Home', 'Admin menu name', 'woocommerce' ) ) ) { $submenu['woocommerce'][ $key ][0] .= ' ' . absint( $tasks_count ) . ''; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited break; } } } /** * Add visible list ids to component settings. * * @param array $settings Component settings. * * @return array */ public static function task_list_preloaded_settings( $settings ) { $settings['visibleTaskListIds'] = self::all_hidden() ? array() : array_keys( self::get_visible() ); $settings['completedTaskListIds'] = get_option( TaskList::COMPLETED_OPTION, array() ); return $settings; } /** * Check if all task lists are hidden. * * @return bool */ public static function all_hidden() { $hidden_lists = get_option( TaskList::HIDDEN_OPTION, array() ); return count( $hidden_lists ) === count( self::get_lists() ); } }