PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / vendor / averta / wordpress / src / Models / WPOptions.php

WPOptions.php in Depicter — Popup & Slider Builder trunk, at vendor/averta/wordpress/src/Models/WPOptions.php

85 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Averta\WordPress\Models;
4
5 /**
6 * An interface for WordPress Options API.
7 */
8 class WPOptions {
9
10 /**
11 * The prefix added to all option keys.
12 *
13 * @var string
14 */
15 private $prefix = '__';
16
17 /**
18 * Create new Options instance.
19 *
20 * @param string $prefix The prefix that will be added to option names automatically.
21 */
22 public function __construct( $prefix ) {
23 $this->prefix = $prefix;
24 }
25
26 /**
27 * Retrieves an option value.
28 *
29 * @param string $option The option name (without the prefix).
30 * @param mixed $default Optional. Default value to return if the option does not exist. Default null.
31 * @param bool $raw Optional. Use the raw option name (i.e. don't call get_option_name). Default false.
32 *
33 * @return mixed Value set for the option.
34 */
35 public function get( $option, $default = null, $raw = false ) {
36 $value = \get_option( $raw ? $option : $this->get_option_name( $option ), $default );
37
38 if ( is_array( $default ) && '' === $value ) {
39 $value = [];
40 }
41
42 return $value;
43 }
44
45 /**
46 * Sets or updates an option.
47 *
48 * @param string $option The option name (without the prefix).
49 * @param mixed $value The value to store.
50 * @param bool $autoload Optional. Whether to load the option when WordPress
51 * starts up. For existing options, $autoload can only
52 * be updated using update_option() if $value is also
53 * changed. Default true.
54 * @param bool $raw Optional. Use the raw option name (i.e. don't call get_option_name). Default false.
55 *
56 * @return bool False if value was not updated and true if value was updated.
57 */
58 public function set( $option, $value, $autoload = true, $raw = false ) {
59 return \update_option( $raw ? $option : $this->get_option_name( $option ), $value, $autoload );
60 }
61
62 /**
63 * Deletes an option.
64 *
65 * @param string $option The option name (without the prefix).
66 * @param bool $raw Optional. Use the raw option name (i.e. don't call get_option_name). Default false.
67 *
68 * @return bool True, if option is successfully deleted. False on failure.
69 */
70 public function delete( $option, $raw = false ) {
71 return \delete_option( $raw ? $option : $this->get_option_name( $option ) );
72 }
73
74 /**
75 * Retrieves the complete option name to use.
76 *
77 * @param string $option The option name (without the prefix).
78 *
79 * @return string
80 */
81 public function get_option_name( $option ) {
82 return "{$this->prefix}{$option}";
83 }
84 }
85