| 1 |
<?php |
| 2 |
/** |
| 3 |
* Simple / Advanced mode: how much of ThinkRank's navigation a user sees. |
| 4 |
* |
| 5 |
* @package ThinkRank\Admin |
| 6 |
* @since 2.11.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace ThinkRank\Admin; |
| 12 |
|
| 13 |
// Prevent direct access |
| 14 |
if (!defined('ABSPATH')) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* UI Mode |
| 20 |
* |
| 21 |
* One switch in the ThinkRank header (#730). Simple shows the sections a site |
| 22 |
* owner needs day to day and hides the rest of the menu; Advanced is the full |
| 23 |
* navigation. It only changes what the menu and search list: every screen |
| 24 |
* still opens by its URL, and no setting changes. |
| 25 |
* |
| 26 |
* Per user, in user meta, so a site owner and their agency each see their own |
| 27 |
* — and it follows the user across a multisite network. |
| 28 |
* |
| 29 |
* A user who never chose gets Advanced: that is the navigation everyone had |
| 30 |
* before the switch existed, and hiding sections from them unasked would read |
| 31 |
* as features disappearing. The setup wizard asks new sites. |
| 32 |
* |
| 33 |
* @since 2.11.0 |
| 34 |
*/ |
| 35 |
final class UI_Mode { |
| 36 |
|
| 37 |
public const META_KEY = 'thinkrank_ui_mode'; |
| 38 |
|
| 39 |
public const SIMPLE = 'simple'; |
| 40 |
|
| 41 |
public const ADVANCED = 'advanced'; |
| 42 |
|
| 43 |
/** |
| 44 |
* Accepted modes. |
| 45 |
* |
| 46 |
* @var string[] |
| 47 |
*/ |
| 48 |
public const MODES = [self::SIMPLE, self::ADVANCED]; |
| 49 |
|
| 50 |
/** |
| 51 |
* A user's mode. |
| 52 |
* |
| 53 |
* @param int $user_id User id; the current user when 0. |
| 54 |
* @return string One of MODES. |
| 55 |
*/ |
| 56 |
public static function get(int $user_id = 0): string { |
| 57 |
$user_id = $user_id > 0 ? $user_id : get_current_user_id(); |
| 58 |
if ($user_id <= 0) { |
| 59 |
return self::ADVANCED; |
| 60 |
} |
| 61 |
|
| 62 |
$stored = (string) get_user_meta($user_id, self::META_KEY, true); |
| 63 |
|
| 64 |
return in_array($stored, self::MODES, true) ? $stored : self::ADVANCED; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Store a user's mode and return what was stored. |
| 69 |
* |
| 70 |
* @param string $mode One of MODES. |
| 71 |
* @param int $user_id User id; the current user when 0. |
| 72 |
* @return string|null The stored mode, or null for an unknown mode or no user. |
| 73 |
*/ |
| 74 |
public static function set(string $mode, int $user_id = 0): ?string { |
| 75 |
$user_id = $user_id > 0 ? $user_id : get_current_user_id(); |
| 76 |
if ($user_id <= 0 || !in_array($mode, self::MODES, true)) { |
| 77 |
return null; |
| 78 |
} |
| 79 |
|
| 80 |
update_user_meta($user_id, self::META_KEY, $mode); |
| 81 |
|
| 82 |
return self::get($user_id); |
| 83 |
} |
| 84 |
} |
| 85 |
|