PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 1.1.8
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v1.1.8
1.1.9 1.1.8 1.1.7 1.1.6 1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.1 1.0.0 0.9.8 0.9.7 0.9.6 0.9.4 0.9.5 0.9.3 0.9.2 0.9.1 0.9.0 0.8.9 0.8.8 0.8.7 0.8.6 All 33 releases
desktop-mode / includes / framework / app / wordpress / class-store.php

class-store.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin 1.1.8, at includes/framework/app/wordpress/class-store.php

76 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * OpenStation App Framework — WordPress Store adapter.
4 *
5 * `user` scope is one user-meta row holding a key → value map;
6 * `site` scope is one non-autoloaded option holding the same shape.
7 * One row per scope keeps an app from spraying meta keys across the
8 * table and makes "forget everything this app stored" one delete.
9 *
10 * @package OpenStation
11 */
12
13 namespace OpenStation\App\WordPress;
14
15 use OpenStation\App\Contracts\Store as StoreContract;
16
17 defined( 'ABSPATH' ) || exit;
18
19 /**
20 * User-meta + option backed store.
21 */
22 final class Store implements StoreContract {
23
24 const META_KEY = 'openstation_app_store';
25
26 /**
27 * Read the whole map for a scope.
28 *
29 * @param string $scope `user` | `site`.
30 * @return array<string,mixed>
31 */
32 private function map( $scope ) {
33 if ( 'site' === $scope ) {
34 $map = get_option( self::META_KEY, array() );
35 } else {
36 $map = get_user_meta( get_current_user_id(), self::META_KEY, true );
37 }
38 return is_array( $map ) ? $map : array();
39 }
40
41 /**
42 * Write the whole map for a scope.
43 *
44 * @param string $scope `user` | `site`.
45 * @param array<string,mixed> $map Map.
46 * @return void
47 */
48 private function save( $scope, array $map ) {
49 if ( 'site' === $scope ) {
50 update_option( self::META_KEY, $map, false );
51 } else {
52 update_user_meta( get_current_user_id(), self::META_KEY, $map );
53 }
54 }
55
56 /** {@inheritDoc} */
57 public function get( $scope, $key, $fallback = null ) {
58 $map = $this->map( $scope );
59 return array_key_exists( $key, $map ) ? $map[ $key ] : $fallback;
60 }
61
62 /** {@inheritDoc} */
63 public function set( $scope, $key, $value ) {
64 $map = $this->map( $scope );
65 $map[ $key ] = $value;
66 $this->save( $scope, $map );
67 }
68
69 /** {@inheritDoc} */
70 public function delete( $scope, $key ) {
71 $map = $this->map( $scope );
72 unset( $map[ $key ] );
73 $this->save( $scope, $map );
74 }
75 }
76