Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Collection
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 5
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 autoload
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 get
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 append
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 delete
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Automattic\Jetpack_Boost\Lib;
4
5/**
6 * A collection of WordPress options that's stored as a single ar
7 */
8class Collection {
9
10    private $key;
11
12    /**
13     * Collections imply that they may carry more data than regular options,
14     * This might unnecessarily slow down sites.
15     * Disable autoloading by default.
16     *
17     * @see autoload() to enable autoloading.
18     *
19     * @var bool
20     */
21    private $autoload = false;
22
23    /**
24     * @param string $key Collection key.
25     */
26    public function __construct( $key ) {
27        $this->key = $key;
28    }
29
30    /**
31     * Allow autoloading collections
32     */
33    public function autoload() {
34        $this->autoload = true;
35        return $this;
36    }
37
38    /**
39     * Get the whole collection
40     *
41     * @return array
42     */
43    public function get() {
44        $result = get_option( $this->key, array() );
45        if ( is_array( $result ) ) {
46            return $result;
47        }
48        return array();
49    }
50
51    /**
52     * Append a single item to the collection
53     *
54     * @param mixed $item
55     *
56     * @return bool
57     */
58    public function append( $item ) {
59        $items = $this->get();
60
61        if ( ! in_array( $item, $items, true ) ) {
62            $items[] = $item;
63            return update_option( $this->key, $items, $this->autoload );
64        }
65
66        return false;
67    }
68
69    /**
70     * Delete the whole collection
71     *
72     * @return bool
73     */
74    public function delete() {
75        return delete_option( $this->key );
76    }
77}