PluginProbe
Disable Comments & Delete All Comments / trunk
Disable Comments & Delete All Comments vtrunk
1.3.3 1.3.2 trunk 1.0.0 1.0.4 1.0.5 1.0.8 1.0.9 1.1.1 1.1.3 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.3.0 1.3.1
comments-plus / libs / factory / forms / includes / providers / meta-value-provider.class.php

meta-value-provider.class.php in Disable Comments & Delete All Comments trunk, at libs/factory/forms/includes/providers/meta-value-provider.class.php

303 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The file contains the class of Factory Meta Value Provider.
4 *
5 * @author Alex Kovalev <[email protected]>
6 * @copyright (c) 2018, Webcraftic Ltd
7 *
8 * @package factory-forms
9 * @since 1.0.0
10 */
11
12 // Exit if accessed directly
13 if( !defined('ABSPATH') ) {
14 exit;
15 }
16
17 if( !class_exists('Wbcr_FactoryForms480_OptionsValueProvider') ) {
18
19 /**
20 * Factory Meta Value Provider
21 *
22 * This provide works with meta values like a lazy key-value storage and
23 * provides methods to commit changes on demand. It increases perfomance on form saving.
24 *
25 * @since 1.0.0
26 */
27 class Wbcr_FactoryForms480_MetaValueProvider implements Wbcr_IFactoryForms480_ValueProvider {
28
29
30 public $scope;
31
32 protected $post_id;
33
34 /**
35 * Values to save $metaName => $metaValue
36 * @var array
37 */
38 private $values = array();
39
40 /**
41 * Chanched meta keys (indexed array)
42 * @var array
43 */
44 private $keys = array();
45
46 private $meta = array();
47
48
49 /**
50 * Creates a new instance of a meta value provider.
51 *
52 * @param array $options
53 */
54 public function __construct($options = array())
55 {
56 global $post;
57
58 $this->scope = (isset($options['scope']))
59 ? $options['scope']
60 : null;
61
62 $this->scope = preg_replace('/\_meta\_box$/', '', $this->formatCamelCase($this->scope));
63
64 /*$this->post_id = (isset($options['post_id']))
65 ? $options['post_id']
66 : $post->ID;
67
68 // the second parameter for compatibility with wordpress 3.0
69 $temp = get_post_meta($this->post_id, '', true);
70
71 foreach($temp as $key => &$content) {
72 if( strpos($key, $this->scope) === 0 ) {
73 $this->meta[$key] = $content;
74 }
75 }*/
76
77 $this->init();
78 }
79
80 /**
81 * Initizalize an instance of the provider.
82 * This method should be invoked before the provider usage.
83 *
84 * @param bool $post_id
85 */
86 public function init($post_id = false)
87 {
88 global $post;
89
90 $this->post_id = $post_id
91 ? $post_id
92 : $post->ID;
93
94 // the second parameter for compatibility with wordpress 3.0
95 $temp = get_post_meta($this->post_id, '', true);
96
97 foreach($temp as $key => &$content) {
98 if( strpos($key, $this->scope) === 0 ) {
99 $this->meta[$key] = $content;
100 }
101 }
102 }
103
104 /**
105 * Saves changes into a database.
106 * The method is optimized for bulk updates.
107 */
108 public function saveChanges()
109 {
110
111 $this->deleteValues();
112 $this->insertValues();
113 /**
114 * foreach ($this->values as $key => $value) {
115 * update_post_meta($this->postId, $key, $value);
116 * }
117 */
118 }
119
120 /**
121 * Removes all actual values from a database.
122 */
123 private function deleteValues()
124 {
125 if( count($this->keys) == 0 ) {
126 return;
127 }
128
129 global $wpdb;
130
131 $values = array();
132 $keys[] = $this->post_id;
133
134 for($i = 0; $i < count($this->keys); $i++) {
135 $values[] = '%s';
136 $keys[] = $this->keys[$i];
137 }
138
139 $clause = implode(',', $values);
140 $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->postmeta} WHERE post_id='%d' AND meta_key IN ($clause)", $keys));
141 }
142
143 /**
144 * /**
145 * Inserts new values by using bulk insert directly into a database.
146 *
147 * @return bool|false|int
148 */
149 private function insertValues()
150 {
151 global $wpdb;
152
153 $sql = "INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) VALUES ";
154 $rows = array();
155
156 foreach($this->values as $meta_key => $meta_value) {
157 if( is_array($meta_value) ) {
158 foreach($meta_value as $value) {
159 $rows[] = $wpdb->prepare('(%d,%s,%s)', $this->post_id, $meta_key, $value);
160 }
161 } else {
162 $rows[] = $wpdb->prepare('(%d,%s,%s)', $this->post_id, $meta_key, $meta_value);
163 }
164 }
165
166 if( empty($rows) ) {
167 return false;
168 }
169
170 $sql = $sql . implode(',', $rows);
171
172 return $wpdb->query($sql);
173 }
174
175 /**
176 * @param string $name
177 * @param null $default
178 * @param bool $multiple
179 * @return array|int|null
180 */
181 public function getValue($name, $default = null, $multiple = false)
182 {
183 if( is_array($name) ) {
184
185 $values = array();
186 $index = 0;
187
188 foreach($name as $item) {
189 $item_default = ($default && is_array($default) && isset($default[$index]))
190 ? $default[$index]
191 : null;
192
193 $values[] = $this->getValueBySingleName($item, $item_default, $multiple);
194 $index++;
195 }
196
197 return $values;
198 }
199
200 $value = $this->getValueBySingleName($name, $default, $multiple);
201
202 return $value;
203 }
204
205 /**
206 * @param $single_name
207 * @param null $default
208 * @param bool $multiple
209 * @return int|null
210 */
211 protected function getValueBySingleName($single_name, $default = null, $multiple = false)
212 {
213
214 $value = isset($this->meta[$this->scope . '_' . $single_name])
215 ? ($multiple)
216 ? $this->meta[$this->scope . '_' . $single_name]
217 : $this->meta[$this->scope . '_' . $single_name][0]
218 : $default;
219
220 if( $value === 'true' ) {
221 $value = 1;
222 }
223 if( $value === 'false' ) {
224 $value = 0;
225 }
226
227 return $value;
228 }
229
230 /**
231 * @param string $name
232 * @param mixed $value
233 */
234 public function setValue($name, $value)
235 {
236
237 if( is_array($name) ) {
238 $index = 0;
239
240 foreach($name as $item) {
241 $itemValue = ($value && is_array($value) && isset($value[$index]))
242 ? $value[$index]
243 : null;
244
245 $this->setValueBySingleName($item, $itemValue);
246 $index++;
247 }
248
249 return;
250 }
251
252 $this->setValueBySingleName($name, $value);
253
254 return;
255 }
256
257 /**
258 * @param string $single_name
259 * @param mixed $singe_value
260 */
261 protected function setValueBySingleName($single_name, $singe_value)
262 {
263 $name = $this->scope . '_' . $single_name;
264
265 if( is_array($singe_value) ) {
266
267 foreach($singe_value as $index => $value) {
268
269 $singe_value[$index] = empty($singe_value[$index])
270 ? $singe_value[$index]
271 : stripslashes($singe_value[$index]);
272 }
273
274 $value = $singe_value;
275 } else {
276 $value = empty($singe_value)
277 ? $singe_value
278 : stripslashes($singe_value);
279 }
280
281 $this->values[$name] = $value;
282 $this->keys[] = $name;
283 }
284
285 /**
286 * @param string $string
287 * @return string
288 */
289 private function formatCamelCase($string)
290 {
291 $output = "";
292 foreach(str_split($string) as $char) {
293 if( strtoupper($char) == $char && !in_array($char, array('_', '-')) ) {
294 $output .= "_";
295 }
296 $output .= $char;
297 }
298 $output = strtolower($output);
299
300 return $output;
301 }
302 }
303 }