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