class-foo-plugin-options.php
| 1 | <?php |
| 2 | /* |
| 3 | * Foo_Plugin_Options class |
| 4 | * |
| 5 | * A helper class for storing all your plugin options as a single WP option. Multi-site friendly. |
| 6 | * |
| 7 | * Version: 2.1 |
| 8 | * Author: Brad Vincent |
| 9 | * Author URI: http://fooplugins.com |
| 10 | * License: GPL2 |
| 11 | */ |
| 12 | |
| 13 | if ( !class_exists( 'Foo_Plugin_Options_v2_1' ) ) { |
| 14 | class Foo_Plugin_Options_v2_1 { |
| 15 | |
| 16 | /** |
| 17 | * @var string The name of the option that will be saved to the options table. |
| 18 | */ |
| 19 | protected $option_name; |
| 20 | |
| 21 | /** |
| 22 | * Foo_Plugin_Options Constructor |
| 23 | * |
| 24 | * @param string $option_name The name of the single option we want to save in the options table. Usually the plugin slug. |
| 25 | */ |
| 26 | function __construct($option_name) { |
| 27 | $this->option_name = $option_name; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Private function used to return the merged array of all options. |
| 32 | * @return array |
| 33 | */ |
| 34 | private function get_options() { |
| 35 | |
| 36 | //get the options based on the type of install (multisite or not) |
| 37 | if ( is_network_admin() ) { |
| 38 | $options = get_site_option( $this->option_name ); |
| 39 | } else { |
| 40 | $options = wp_parse_args( get_option( $this->option_name ), get_site_option( $this->option_name ) ); |
| 41 | } |
| 42 | |
| 43 | //get some defaults (if available) |
| 44 | $default_options = apply_filters( $this->option_name . '_default_options', array() ); |
| 45 | |
| 46 | //merge! |
| 47 | return wp_parse_args( $options, $default_options ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns all the options in an array |
| 52 | * @return array |
| 53 | */ |
| 54 | public function get_all() { |
| 55 | return $this->get_options(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Save an individual option. |
| 60 | * |
| 61 | * @param string $key The key of the individual option that will be stored. |
| 62 | * @param mixed $value The value of the individual option that will be stored. |
| 63 | */ |
| 64 | public function save($key, $value) { |
| 65 | //first get the options |
| 66 | $options = $this->get_options(); |
| 67 | |
| 68 | if ( !$options ) { |
| 69 | //no options have been saved yet, so add it |
| 70 | |
| 71 | if ( is_network_admin() ) { |
| 72 | add_site_option( $this->option_name, array($key => $value) ); |
| 73 | } else { |
| 74 | add_option( $this->option_name, array($key => $value) ); |
| 75 | } |
| 76 | |
| 77 | } else { |
| 78 | //update the existing option |
| 79 | $options[$key] = $value; |
| 80 | |
| 81 | if ( is_network_admin() ) { |
| 82 | update_site_option( $this->option_name, $options ); |
| 83 | } else { |
| 84 | update_option( $this->option_name, $options ); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get an individual option. |
| 91 | * |
| 92 | * @param string $key The key of the individual option that will be stored. |
| 93 | * @param mixed $default Optional. The default value to return if the key was not found. |
| 94 | * |
| 95 | * @return mixed |
| 96 | */ |
| 97 | public function get($key, $default = false) { |
| 98 | $options = $this->get_options(); |
| 99 | |
| 100 | if ( $options ) { |
| 101 | return (array_key_exists( $key, $options )) ? $options[$key] : $default; |
| 102 | } |
| 103 | |
| 104 | return $default; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Delete an individual option. |
| 109 | * |
| 110 | * @param $key The key of the individual option we want to delete. |
| 111 | */ |
| 112 | public function delete($key) { |
| 113 | $options = $this->get_options(); |
| 114 | |
| 115 | if ( $options ) { |
| 116 | unset($options[$key]); |
| 117 | |
| 118 | if ( is_network_admin() ) { |
| 119 | update_site_option( $this->option_name, $options ); |
| 120 | } else { |
| 121 | update_option( $this->option_name, $options ); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Used to determine if an option is checked (for checkbox options only). |
| 128 | * @param string $key |
| 129 | * @param bool $default |
| 130 | * |
| 131 | * @return bool |
| 132 | */ |
| 133 | function is_checked($key, $default = false) { |
| 134 | $options = $this->get_options(); |
| 135 | |
| 136 | if ($options) { |
| 137 | return array_key_exists($key, $options); |
| 138 | } |
| 139 | |
| 140 | return $default; |
| 141 | } |
| 142 | } |
| 143 | } |