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