CompiledData.php
3 years ago
Cookie.php
4 years ago
Option.php
9 years ago
Permissions.php
1 year ago
Preferences.php
4 years ago
RecentItems.php
2 years ago
Serializable.php
3 years ago
Session.php
4 years ago
Settings.php
1 year ago
Transient.php
7 years ago
Upload.php
2 years ago
RecentItems.php
129 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Recently items to display on home page |
| 4 | */ |
| 5 | class Loco_data_RecentItems extends Loco_data_Option { |
| 6 | |
| 7 | /** |
| 8 | * Global instance of recent items |
| 9 | * @var Loco_data_RecentItems |
| 10 | */ |
| 11 | private static $current; |
| 12 | |
| 13 | |
| 14 | /** |
| 15 | * {@inheritdoc} |
| 16 | */ |
| 17 | public function getKey(){ |
| 18 | return 'recent'; |
| 19 | } |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * @return Loco_data_RecentItems |
| 24 | */ |
| 25 | public static function get(){ |
| 26 | if( ! self::$current ){ |
| 27 | self::$current = new Loco_data_RecentItems; |
| 28 | self::$current->fetch(); |
| 29 | } |
| 30 | return self::$current; |
| 31 | } |
| 32 | |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * Trash data and remove from memory |
| 37 | */ |
| 38 | public static function destroy(){ |
| 39 | $tmp = new Loco_data_RecentItems; |
| 40 | $tmp->remove(); |
| 41 | self::$current = null; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * @internal |
| 48 | * @return Loco_data_RecentItems |
| 49 | */ |
| 50 | private function push( $object, array $indexes ){ |
| 51 | foreach( $indexes as $key => $id ){ |
| 52 | $stack = isset($this[$key]) ? $this[$key] : []; |
| 53 | // remove before add ensures latest item appended to hashmap |
| 54 | unset($stack[$id]); |
| 55 | $stack[$id] = time(); |
| 56 | $this[$key] = $stack; |
| 57 | // TODO prune stack to maximum length |
| 58 | } |
| 59 | return $this; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | |
| 64 | /** |
| 65 | * @return array |
| 66 | */ |
| 67 | private function getItems( $key, $offset, $count ){ |
| 68 | $stack = isset($this[$key]) ? $this[$key] : []; |
| 69 | // hash map should automatically be in "push" order, meaning most recent last |
| 70 | // sorting gives wrong order for same-second updates (only relevant in tests, but still..) |
| 71 | // asort( $stack, SORT_NUMERIC ); |
| 72 | $stack = array_reverse( array_keys( $stack ) ); |
| 73 | if( is_null($count) && 0 === $offset ){ |
| 74 | return $stack; |
| 75 | } |
| 76 | return array_slice( $stack, $offset, $count, false ); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | /** |
| 81 | * @return int |
| 82 | */ |
| 83 | private function hasItem( $key, $id ){ |
| 84 | if( isset($this[$key]) && ( $items = $this[$key] ) && isset($items[$id]) ){ |
| 85 | return $items[$id]; |
| 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /** |
| 92 | * Push bundle to the front of recent bundles stack |
| 93 | * @return Loco_data_RecentItems |
| 94 | */ |
| 95 | public function pushBundle( Loco_package_Bundle $bundle ){ |
| 96 | return $this->push( $bundle, [ 'bundle' => $bundle->getId() ] ); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | /** |
| 101 | * Get bundle IDs |
| 102 | * @return array |
| 103 | */ |
| 104 | public function getBundles( $offset = 0, $count = null ){ |
| 105 | return $this->getItems('bundle', $offset, $count ); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /** |
| 110 | * Check if a bundle has been recently used |
| 111 | * @return int timestamp item was added, 0 if absent |
| 112 | */ |
| 113 | public function hasBundle( $id ){ |
| 114 | return $this->hasItem( 'bundle', $id ); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | /** |
| 119 | * TODO other types of item |
| 120 | * Push project to the front of recent bundles stack |
| 121 | * @return Loco_data_RecentItems |
| 122 | * |
| 123 | public function pushProject( Loco_package_Project $project ){ |
| 124 | return $this; |
| 125 | }*/ |
| 126 | |
| 127 | |
| 128 | } |
| 129 |