1 | <?php
|
---|
2 | /**
|
---|
3 | * Copyright 2006, 2007, 2008, 2009 Eric D. Hough (http://ehough.com)
|
---|
4 | *
|
---|
5 | * This file is part of TubePress (http://tubepress.org)
|
---|
6 | *
|
---|
7 | * TubePress is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * TubePress is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with TubePress. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | *
|
---|
20 | */
|
---|
21 |
|
---|
22 | function_exists('tubepress_load_classes')
|
---|
23 | || require(dirname(__FILE__) . '/../../../../tubepress_classloader.php');
|
---|
24 | tubepress_load_classes(array('org_tubepress_options_storage_AbstractStorageManager',
|
---|
25 | 'org_tubepress_options_reference_SimpleOptionsReference',
|
---|
26 | 'org_tubepress_message_WordPressMessageService',
|
---|
27 | 'org_tubepress_options_validation_SimpleInputValidationService'));
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Implementation of org_tubepress_options_storage_StorageManager that uses the
|
---|
31 | * regular WordPress options API
|
---|
32 | *
|
---|
33 | */
|
---|
34 | class org_tubepress_options_storage_WordPressStorageManager extends org_tubepress_options_storage_AbstractStorageManager
|
---|
35 | {
|
---|
36 | /*
|
---|
37 | * Prefix all our option names in the WordPress DB
|
---|
38 | * with this value. Helps avoid naming conflicts.
|
---|
39 | */
|
---|
40 | const OPTION_PREFIX = "tubepress-";
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Constructor. Until I can come up with a better way to validate options, this is gonna be how we
|
---|
44 | * check to make sure that the db is initialized.
|
---|
45 | *
|
---|
46 | * @return unknown_type
|
---|
47 | */
|
---|
48 | public function __construct()
|
---|
49 | {
|
---|
50 | $needToInit = false;
|
---|
51 |
|
---|
52 | if ($this->exists("version")) {
|
---|
53 | $version = $this->get("version");
|
---|
54 | if (!is_numeric($version) || $version < 180) {
|
---|
55 | $needToInit = true;
|
---|
56 | }
|
---|
57 | } else {
|
---|
58 | $this->create("version", 180);
|
---|
59 | $needToInit = true;
|
---|
60 | }
|
---|
61 |
|
---|
62 | if ($needToInit) {
|
---|
63 | $reference = new org_tubepress_options_reference_SimpleOptionsReference();
|
---|
64 | $msgService = new org_tubepress_message_WordPressMessageService();
|
---|
65 | $validationService = new org_tubepress_options_validation_SimpleInputValidationService();
|
---|
66 | $validationService->setMessageService($msgService);
|
---|
67 | $ref = new org_tubepress_options_reference_SimpleOptionsReference();
|
---|
68 | $this->setValidationService($validationService);
|
---|
69 | $this->setOptionsReference($ref);
|
---|
70 | $this->init();
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Creates an option in storage
|
---|
76 | *
|
---|
77 | * @param unknown_type $optionName The name of the option to create
|
---|
78 | * @param unknown_type $optionValue The default value of the new option
|
---|
79 | *
|
---|
80 | * @return void
|
---|
81 | */
|
---|
82 | protected function create($optionName, $optionValue)
|
---|
83 | {
|
---|
84 | add_option(org_tubepress_options_storage_WordPressStorageManager::OPTION_PREFIX . $optionName,
|
---|
85 | $optionValue);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Deletes an option from storage
|
---|
90 | *
|
---|
91 | * @param unknown_type $optionName The name of the option to delete
|
---|
92 | *
|
---|
93 | * @return void
|
---|
94 | */
|
---|
95 | protected function delete($optionName)
|
---|
96 | {
|
---|
97 | delete_option(org_tubepress_options_storage_WordPressStorageManager::OPTION_PREFIX . $optionName);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Determines if an option exists
|
---|
102 | *
|
---|
103 | * @param string $optionName The name of the option in question
|
---|
104 | *
|
---|
105 | * @return boolean True if the option exists, false otherwise
|
---|
106 | */
|
---|
107 | public function exists($optionName)
|
---|
108 | {
|
---|
109 | return get_option(org_tubepress_options_storage_WordPressStorageManager::OPTION_PREFIX . $optionName)
|
---|
110 | !== false;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Retrieve the current value of an option
|
---|
115 | *
|
---|
116 | * @param string $optionName The name of the option
|
---|
117 | *
|
---|
118 | * @return unknown_type The option's value
|
---|
119 | */
|
---|
120 | public function get($optionName)
|
---|
121 | {
|
---|
122 | return get_option(org_tubepress_options_storage_WordPressStorageManager::OPTION_PREFIX . $optionName);
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Wipes out all TubePress options and replaces them with their defaults
|
---|
127 | *
|
---|
128 | * @return void
|
---|
129 | */
|
---|
130 | public function nuclear()
|
---|
131 | {
|
---|
132 | $allOptions = get_alloptions();
|
---|
133 | foreach ($allOptions as $key => $value) {
|
---|
134 | if (preg_match("/^tubepress.*/", $key)) {
|
---|
135 | delete_option($key);
|
---|
136 | }
|
---|
137 | }
|
---|
138 | $this->init();
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Sets an option to a new value, without validation
|
---|
143 | *
|
---|
144 | * @param string $optionName The name of the option to update
|
---|
145 | * @param unknown_type $optionValue The new option value
|
---|
146 | *
|
---|
147 | * @return void
|
---|
148 | */
|
---|
149 | protected function setOption($optionName, $optionValue)
|
---|
150 | {
|
---|
151 | update_option(org_tubepress_options_storage_WordPressStorageManager::OPTION_PREFIX . $optionName,
|
---|
152 | $optionValue);
|
---|
153 | }
|
---|
154 | }
|
---|