Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Onboarding
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 3
30
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 complete_steps
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 get_current_user_progress
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Class file for managing the user onboarding experience.
4 *
5 * @package automattic/jetpack-protect-plugin
6 */
7
8namespace Automattic\Jetpack\Protect;
9
10/**
11 * Onboarding
12 */
13class Onboarding {
14
15    const OPTION_NAME = 'protect_onboarding_progress';
16
17    /**
18     * The current user's ID
19     *
20     * @var int
21     */
22    private static $user_id;
23
24    /**
25     * Current User Progress
26     *
27     * @var array<string>
28     */
29    private static $current_user_progress;
30
31    /**
32     * Onboarding Init
33     *
34     * @return void
35     */
36    private static function init() {
37        self::$user_id = get_current_user_id();
38
39        $current_user_progress       = get_user_meta( self::$user_id, self::OPTION_NAME, true );
40        self::$current_user_progress = $current_user_progress ? $current_user_progress : array();
41    }
42
43    /**
44     * Set Onboarding Items As Completed
45     *
46     * @param array $step_ids The IDs of the steps to complete.
47     * @return bool True if the update was successful, false otherwise.
48     */
49    public static function complete_steps( $step_ids ) {
50        self::init();
51
52        if ( empty( self::$current_user_progress ) ) {
53            self::$current_user_progress = $step_ids;
54        } else {
55            // Find step IDs that are not already in the current user progress
56            $new_steps = array_diff( $step_ids, self::$current_user_progress );
57
58            // Merge new steps with current progress
59            self::$current_user_progress = array_merge( self::$current_user_progress, $new_steps );
60        }
61
62        // Update the user meta only once
63        return (bool) update_user_meta(
64            self::$user_id,
65            self::OPTION_NAME,
66            self::$current_user_progress
67        );
68    }
69
70    /**
71     * Get Current User's Onboarding Progress
72     *
73     * @return array<string>
74     */
75    public static function get_current_user_progress() {
76        self::init();
77
78        return self::$current_user_progress;
79    }
80}