admin
2 years ago
field-groups
2 years ago
fields
2 years ago
fields-settings
2 years ago
locations
3 years ago
modules
2 years ago
screens
3 years ago
acfe-deprecated-functions.php
2 years ago
acfe-field-functions.php
2 years ago
acfe-field-group-functions.php
2 years ago
acfe-file-functions.php
3 years ago
acfe-form-functions.php
2 years ago
acfe-helper-functions.php
2 years ago
acfe-meta-functions.php
3 years ago
acfe-post-functions.php
3 years ago
acfe-screen-functions.php
3 years ago
acfe-template-functions.php
3 years ago
acfe-term-functions.php
3 years ago
acfe-user-functions.php
3 years ago
acfe-wp-functions.php
3 years ago
assets.php
3 years ago
compatibility-6.0.php
2 years ago
compatibility.php
2 years ago
field-extend.php
3 years ago
field.php
3 years ago
hooks.php
3 years ago
init.php
3 years ago
local-meta.php
3 years ago
module-acf.php
2 years ago
module-db.php
3 years ago
module-l10n.php
3 years ago
module-legacy.php
3 years ago
module-manager.php
3 years ago
module-post.php
2 years ago
module-posts.php
2 years ago
module-upgrades.php
3 years ago
module.php
2 years ago
multilang.php
3 years ago
settings.php
3 years ago
template-tags.php
2 years ago
third-party.php
3 years ago
upgrades.php
3 years ago
module-db.php
198 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(!class_exists('acfe_module_db')): |
| 8 | |
| 9 | class acfe_module_db{ |
| 10 | |
| 11 | /** |
| 12 | * construct |
| 13 | */ |
| 14 | function __construct(){ |
| 15 | |
| 16 | add_action('acfe/module/updated_item', array($this, 'updated_item'), 10, 2); |
| 17 | add_action('acfe/module/trashed_item', array($this, 'deleted_item'), 10, 2); |
| 18 | add_action('acfe/module/deleted_item', array($this, 'deleted_item'), 10, 2); |
| 19 | add_action('acfe/module/include_items', array($this, 'include_items')); |
| 20 | |
| 21 | } |
| 22 | |
| 23 | |
| 24 | /** |
| 25 | * updated_item |
| 26 | * |
| 27 | * acfe/module/updated_item |
| 28 | * |
| 29 | * @param $item |
| 30 | * @param $module |
| 31 | */ |
| 32 | function updated_item($item, $module){ |
| 33 | |
| 34 | if(!$this->has_settings($module)){ |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | // cleanup keys |
| 39 | $export = $module->prepare_item_for_export($item); |
| 40 | |
| 41 | // on update |
| 42 | if($item['ID']){ |
| 43 | |
| 44 | // get raw item from db |
| 45 | $raw_item = $module->get_item($item['ID']); |
| 46 | |
| 47 | // delete old settings if name changed |
| 48 | if($raw_item['name'] && $raw_item['name'] !== $item['name'] && !acf_is_filter_enabled('acfe/module/update_unique_name')){ |
| 49 | $this->delete_settings($module, $raw_item['name']); |
| 50 | } |
| 51 | |
| 52 | } |
| 53 | |
| 54 | // settings |
| 55 | $settings = $this->get_settings($module); |
| 56 | $settings[ $item['name'] ] = $export; |
| 57 | |
| 58 | // update setting |
| 59 | ksort($settings); |
| 60 | $this->update_settings($module, $settings); |
| 61 | |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * deleted_item |
| 67 | * |
| 68 | * acfe/module/deleted_item |
| 69 | * |
| 70 | * @param $item |
| 71 | * @param $module |
| 72 | */ |
| 73 | function deleted_item($item, $module){ |
| 74 | |
| 75 | if(!$this->has_settings($module)){ |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | // WP appends '__trashed' to end of 'name' (post_name). |
| 80 | $name = str_replace('__trashed', '', $item['name']); |
| 81 | |
| 82 | // delete settings |
| 83 | $this->delete_settings($module, $name); |
| 84 | |
| 85 | } |
| 86 | |
| 87 | |
| 88 | /** |
| 89 | * include_items |
| 90 | * |
| 91 | * acfe/module/include_items |
| 92 | * |
| 93 | * @param $module |
| 94 | */ |
| 95 | function include_items($module){ |
| 96 | |
| 97 | if(!$this->has_settings($module)){ |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | // get db settings |
| 102 | $settings = acf_get_array($this->get_settings($module)); |
| 103 | |
| 104 | // loop |
| 105 | foreach($settings as $key => $item){ |
| 106 | |
| 107 | // set local |
| 108 | $item['local'] = 'db'; |
| 109 | $item['local_file'] = "{$module->settings}.{$key}"; |
| 110 | |
| 111 | // add local item |
| 112 | $module->add_local_item($item); |
| 113 | |
| 114 | } |
| 115 | |
| 116 | } |
| 117 | |
| 118 | |
| 119 | /** |
| 120 | * has_settings |
| 121 | * |
| 122 | * @param $module |
| 123 | * |
| 124 | * @return bool |
| 125 | */ |
| 126 | function has_settings($module){ |
| 127 | return !empty($module->settings); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /** |
| 132 | * get_settings |
| 133 | * |
| 134 | * @param $module |
| 135 | * @param $selector |
| 136 | * @param $default |
| 137 | * |
| 138 | * @return mixed |
| 139 | */ |
| 140 | function get_settings($module, $selector = null, $default = null){ |
| 141 | |
| 142 | if($selector === null){ |
| 143 | $selector = $module->settings; |
| 144 | }else{ |
| 145 | $selector = "{$module->settings}.{$selector}"; |
| 146 | } |
| 147 | |
| 148 | return acfe_get_settings($selector, $default); |
| 149 | |
| 150 | } |
| 151 | |
| 152 | |
| 153 | /** |
| 154 | * update_settings |
| 155 | * |
| 156 | * @param $module |
| 157 | * @param $selector |
| 158 | * @param $value |
| 159 | */ |
| 160 | function update_settings($module, $selector = null, $value = null){ |
| 161 | |
| 162 | if($value === null){ |
| 163 | $value = $selector; |
| 164 | $selector = $module->settings; |
| 165 | }else{ |
| 166 | $selector = "{$module->settings}.{$selector}"; |
| 167 | } |
| 168 | |
| 169 | acfe_update_settings($selector, $value); |
| 170 | |
| 171 | } |
| 172 | |
| 173 | |
| 174 | /** |
| 175 | * delete_settings |
| 176 | * |
| 177 | * @param $module |
| 178 | * @param $selector |
| 179 | * |
| 180 | * @return mixed |
| 181 | */ |
| 182 | function delete_settings($module, $selector = null){ |
| 183 | |
| 184 | if($selector === null){ |
| 185 | $selector = $module->settings; |
| 186 | }else{ |
| 187 | $selector = "{$module->settings}.{$selector}"; |
| 188 | } |
| 189 | |
| 190 | return acfe_delete_settings($selector); |
| 191 | |
| 192 | } |
| 193 | |
| 194 | } |
| 195 | |
| 196 | acf_new_instance('acfe_module_db'); |
| 197 | |
| 198 | endif; |