module-performance-connector.php
1 month ago
module-performance-functions.php
1 month ago
module-performance-ui.php
3 years ago
module-performance-ultra-fields.php
2 years ago
module-performance-ultra-revisions.php
1 month ago
module-performance-ultra.php
1 month ago
module-performance-upgrades.php
2 years ago
module-performance.php
1 month ago
module-performance-ultra.php
333 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(!class_exists('acfe_performance_ultra')): |
| 8 | |
| 9 | class acfe_performance_ultra extends acfe_performance{ |
| 10 | |
| 11 | /** |
| 12 | * initialize |
| 13 | */ |
| 14 | function initialize(){ |
| 15 | |
| 16 | $this->name = 'ultra'; |
| 17 | $this->meta_key = 'acf'; |
| 18 | $this->option_key = '%id%'; |
| 19 | |
| 20 | } |
| 21 | |
| 22 | |
| 23 | /** |
| 24 | * pre_load_meta |
| 25 | * |
| 26 | * @param $return |
| 27 | * @param $post_id |
| 28 | * |
| 29 | * @hook acf/pre_load_meta:999 |
| 30 | * |
| 31 | * @function acf_get_meta() + get_fields() |
| 32 | * |
| 33 | * @return array|mixed|null |
| 34 | */ |
| 35 | function pre_load_meta($return, $post_id){ |
| 36 | |
| 37 | // disabled module or bypass |
| 38 | if(!$this->is_enabled($post_id) || $this->bypass){ |
| 39 | return $return; |
| 40 | } |
| 41 | |
| 42 | return $this->do_pre_load_meta($return, $post_id); |
| 43 | |
| 44 | } |
| 45 | |
| 46 | |
| 47 | /** |
| 48 | * do_pre_load_meta |
| 49 | * |
| 50 | * @param $return |
| 51 | * @param $post_id |
| 52 | * |
| 53 | * @return array |
| 54 | */ |
| 55 | function do_pre_load_meta($return, $post_id){ |
| 56 | |
| 57 | // get store |
| 58 | $acf = $this->get_store($post_id); |
| 59 | |
| 60 | // acf is empty |
| 61 | // fallback to normal meta, just in case |
| 62 | if(empty($acf)){ |
| 63 | return $return; |
| 64 | } |
| 65 | |
| 66 | // unslash values if required |
| 67 | // this filter is enabled in pre_update_metadata() |
| 68 | // it is used when page is not reloaded on save (ie: wp admin menu screen) |
| 69 | // this also fix an issue with post meta copied to revisions with slashes |
| 70 | if(acf_is_filter_enabled('acfe/performance_ultra/unslash')){ |
| 71 | $acf = wp_unslash($acf); |
| 72 | } |
| 73 | |
| 74 | // return store data |
| 75 | return $acf; |
| 76 | |
| 77 | } |
| 78 | |
| 79 | |
| 80 | /** |
| 81 | * pre_load_metadata |
| 82 | * |
| 83 | * @param $return |
| 84 | * @param $post_id |
| 85 | * @param $name |
| 86 | * @param $hidden |
| 87 | * |
| 88 | * @hook acf/pre_load_metadata:999 |
| 89 | * |
| 90 | * @function acf_get_metadata() + acf_get_value() |
| 91 | * |
| 92 | * @return mixed |
| 93 | */ |
| 94 | function pre_load_metadata($return, $post_id, $name, $hidden){ |
| 95 | |
| 96 | // bail early |
| 97 | // if acf |
| 98 | // or disabled module |
| 99 | // or bypass |
| 100 | if($name === $this->meta_key || !$this->is_enabled($post_id) || $this->bypass){ |
| 101 | return $return; |
| 102 | } |
| 103 | |
| 104 | return $this->do_pre_load_metadata($return, $post_id, $name, $hidden); |
| 105 | |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /** |
| 110 | * do_pre_load_metadata |
| 111 | * |
| 112 | * @param $return |
| 113 | * @param $post_id |
| 114 | * @param $name |
| 115 | * @param $hidden |
| 116 | * |
| 117 | * @return mixed |
| 118 | */ |
| 119 | function do_pre_load_metadata($return, $post_id, $name, $hidden){ |
| 120 | |
| 121 | // get store |
| 122 | $acf = $this->get_store($post_id); |
| 123 | |
| 124 | // acf is empty |
| 125 | // fallback to normal meta |
| 126 | if(empty($acf)){ |
| 127 | return $return; |
| 128 | } |
| 129 | |
| 130 | // unslash values if required |
| 131 | // this filter is enabled in pre_update_metadata() |
| 132 | // it is used when page is not reloaded on save (ie: wp admin menu screen) |
| 133 | if(acf_is_filter_enabled('acfe/performance_ultra/unslash')){ |
| 134 | $acf = wp_unslash($acf); |
| 135 | } |
| 136 | |
| 137 | // prefix |
| 138 | $prefix = $hidden ? '_' : ''; |
| 139 | |
| 140 | // retrieve meta |
| 141 | if(isset($acf["{$prefix}{$name}"])){ |
| 142 | $return = $acf["{$prefix}{$name}"]; |
| 143 | } |
| 144 | |
| 145 | // not found in acf |
| 146 | // fallback to normal meta |
| 147 | return $return; |
| 148 | |
| 149 | } |
| 150 | |
| 151 | |
| 152 | /** |
| 153 | * pre_update_metadata |
| 154 | * |
| 155 | * @param $return |
| 156 | * @param $post_id |
| 157 | * @param $name |
| 158 | * @param $value |
| 159 | * @param $hidden |
| 160 | * |
| 161 | * @hook acf/pre_update_metadata:999 |
| 162 | * |
| 163 | * @function acf_update_metadata() + acf_update_value() + acf_copy_metadata() |
| 164 | * |
| 165 | * @return bool|mixed|null |
| 166 | */ |
| 167 | function pre_update_metadata($return, $post_id, $name, $value, $hidden){ |
| 168 | |
| 169 | // bail early |
| 170 | // if acf |
| 171 | // or disabled module |
| 172 | // or bypass |
| 173 | if($name === $this->meta_key || !$this->is_enabled($post_id) || $this->bypass){ |
| 174 | return $return; |
| 175 | } |
| 176 | |
| 177 | // get store |
| 178 | $acf = $this->get_store($post_id); |
| 179 | |
| 180 | // prefix |
| 181 | $prefix = $hidden ? '_' : ''; |
| 182 | |
| 183 | // value |
| 184 | $acf["{$prefix}{$name}"] = $value; |
| 185 | |
| 186 | // update store |
| 187 | $this->update_store($post_id, $acf); |
| 188 | |
| 189 | // unlash for preload on same page as update |
| 190 | acf_enable_filter('acfe/performance_ultra/unslash'); |
| 191 | |
| 192 | // manual update |
| 193 | // outside acf/save_post, probably in update_field() |
| 194 | if($this->compile !== $post_id){ |
| 195 | $this->update_meta($acf, $post_id); |
| 196 | } |
| 197 | |
| 198 | // disallow on revision |
| 199 | // use 'save as individual meta' on post only |
| 200 | if(!wp_is_post_revision($post_id)){ |
| 201 | |
| 202 | // save normal meta |
| 203 | if(acf_is_filter_enabled('acfe/performance_ultra/individual_meta')){ |
| 204 | return $return; |
| 205 | } |
| 206 | |
| 207 | } |
| 208 | |
| 209 | // get config |
| 210 | $config = $this->get_config(); |
| 211 | |
| 212 | switch($config['mode']){ |
| 213 | |
| 214 | // test + rollback |
| 215 | // save normal meta |
| 216 | case 'test': |
| 217 | case 'rollback': { |
| 218 | return $return; |
| 219 | } |
| 220 | |
| 221 | // production |
| 222 | // delete normal meta |
| 223 | case 'production': { |
| 224 | |
| 225 | // use normal acf logic |
| 226 | $this->do_bypass(function($name, $post_id, $hidden){ |
| 227 | |
| 228 | // check if meta exists |
| 229 | // this will get meta cache instead of db call |
| 230 | if(acf_get_metadata($post_id, $name, $hidden) !== null){ |
| 231 | acf_delete_metadata($post_id, $name, $hidden); |
| 232 | } |
| 233 | |
| 234 | }, array($name, $post_id, $hidden)); |
| 235 | |
| 236 | // do not save normal meta |
| 237 | return true; |
| 238 | |
| 239 | } |
| 240 | |
| 241 | } |
| 242 | |
| 243 | // return |
| 244 | return $return; |
| 245 | |
| 246 | } |
| 247 | |
| 248 | |
| 249 | /** |
| 250 | * pre_delete_metadata |
| 251 | * |
| 252 | * @param $return |
| 253 | * @param $post_id |
| 254 | * @param $name |
| 255 | * @param $hidden |
| 256 | * |
| 257 | * @hook acf/pre_delete_metadata:999 |
| 258 | * |
| 259 | * @function acf_delete_metadata() + acf_delete_value() |
| 260 | * |
| 261 | * @return bool|mixed |
| 262 | */ |
| 263 | function pre_delete_metadata($return, $post_id, $name, $hidden){ |
| 264 | |
| 265 | // bail early |
| 266 | // if acf |
| 267 | // or disabled module |
| 268 | // or bypass |
| 269 | if($name === $this->meta_key || !$this->is_enabled($post_id) || $this->bypass){ |
| 270 | return $return; |
| 271 | } |
| 272 | |
| 273 | // get store |
| 274 | $acf = $this->get_store($post_id); |
| 275 | |
| 276 | // acf is empty |
| 277 | // fallback to normal acf logic |
| 278 | if(empty($acf)){ |
| 279 | return $return; |
| 280 | } |
| 281 | |
| 282 | // prefix |
| 283 | $prefix = $hidden ? '_' : ''; |
| 284 | |
| 285 | // found in array |
| 286 | if(isset($acf["{$prefix}{$name}"])){ |
| 287 | |
| 288 | // unset |
| 289 | unset($acf["{$prefix}{$name}"]); |
| 290 | |
| 291 | // update store |
| 292 | $this->update_store($post_id, $acf); |
| 293 | |
| 294 | // update meta |
| 295 | $this->update_meta($acf, $post_id); |
| 296 | |
| 297 | } |
| 298 | |
| 299 | // return |
| 300 | return $return; |
| 301 | |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * update_value |
| 306 | * |
| 307 | * @param $value |
| 308 | * @param $post_id |
| 309 | * @param $field |
| 310 | * |
| 311 | * @function acf_update_value() |
| 312 | * |
| 313 | * @return mixed |
| 314 | */ |
| 315 | function update_value($value, $post_id, $field){ |
| 316 | |
| 317 | // disabled by default |
| 318 | acf_disable_filter('acfe/performance_ultra/individual_meta'); |
| 319 | |
| 320 | // check if save as individual meta |
| 321 | if(acfe_get($field, 'acfe_save_meta')){ |
| 322 | acf_enable_filter('acfe/performance_ultra/individual_meta'); |
| 323 | } |
| 324 | |
| 325 | return $value; |
| 326 | |
| 327 | } |
| 328 | |
| 329 | } |
| 330 | |
| 331 | acfe_register_performance_engine('acfe_performance_ultra'); |
| 332 | |
| 333 | endif; |