field-group.php
373 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | /** |
| 7 | * Field Group Options: Native Note |
| 8 | */ |
| 9 | add_action('acf/render_field_group_settings', 'acfe_render_field_group_options'); |
| 10 | function acfe_render_field_group_options($field_group){ |
| 11 | acf_render_field_wrap(array( |
| 12 | 'label' => __('Note'), |
| 13 | 'name' => 'acfe_note', |
| 14 | 'prefix' => 'acf_field_group', |
| 15 | 'type' => 'textarea', |
| 16 | 'instructions' => __('Personal note. Only visible to administrators'), |
| 17 | 'value' => (isset($field_group['acfe_note'])) ? $field_group['acfe_note'] : '', |
| 18 | 'required' => false, |
| 19 | )); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Field Group Options: Data |
| 24 | */ |
| 25 | add_action('acf/field_group/admin_head', 'acfe_render_field_group_settings'); |
| 26 | function acfe_render_field_group_settings(){ |
| 27 | add_meta_box('acf-field-group-acfe', __('Data', 'acfe'), function(){ |
| 28 | |
| 29 | global $field_group; |
| 30 | |
| 31 | acf_render_field_wrap(array( |
| 32 | 'label' => __('Custom meta data'), |
| 33 | 'name' => 'acfe_meta', |
| 34 | 'key' => 'acfe_meta', |
| 35 | 'instructions' => '', |
| 36 | 'prefix' => 'acf_field_group', |
| 37 | 'type' => 'repeater', |
| 38 | 'button_label' => __('+ Add row'), |
| 39 | 'required' => false, |
| 40 | 'layout' => 'table', |
| 41 | 'value' => (isset($field_group['acfe_meta'])) ? $field_group['acfe_meta'] : array(), |
| 42 | 'sub_fields' => array( |
| 43 | array( |
| 44 | 'label' => __('Key'), |
| 45 | 'name' => 'acfe_meta_key', |
| 46 | 'key' => 'acfe_meta_key', |
| 47 | 'prefix' => '', |
| 48 | '_name' => '', |
| 49 | '_prepare' => '', |
| 50 | 'type' => 'text', |
| 51 | 'instructions' => false, |
| 52 | 'required' => false, |
| 53 | 'wrapper' => array( |
| 54 | 'width' => '', |
| 55 | 'class' => '', |
| 56 | 'id' => '', |
| 57 | ), |
| 58 | ), |
| 59 | array( |
| 60 | 'label' => __('Value'), |
| 61 | 'name' => 'acfe_meta_value', |
| 62 | 'key' => 'acfe_meta_value', |
| 63 | 'prefix' => '', |
| 64 | '_name' => '', |
| 65 | '_prepare' => '', |
| 66 | 'type' => 'text', |
| 67 | 'instructions' => false, |
| 68 | 'required' => false, |
| 69 | 'wrapper' => array( |
| 70 | 'width' => '', |
| 71 | 'class' => '', |
| 72 | 'id' => '', |
| 73 | ), |
| 74 | ), |
| 75 | ) |
| 76 | )); |
| 77 | |
| 78 | acf_render_field_wrap(array( |
| 79 | 'label' => __('Field group data'), |
| 80 | 'instructions' => __('View raw data'), |
| 81 | 'type' => 'acfe_dynamic_message', |
| 82 | 'name' => 'acfe_data', |
| 83 | 'prefix' => 'acf_field_group', |
| 84 | 'value' => $field_group['key'], |
| 85 | )); |
| 86 | |
| 87 | ?> |
| 88 | <script type="text/javascript"> |
| 89 | if(typeof acf !== 'undefined'){ |
| 90 | acf.postbox.render({ |
| 91 | 'id': 'acf-field-group-acfe', |
| 92 | 'label': 'left' |
| 93 | }); |
| 94 | } |
| 95 | |
| 96 | jQuery(document).ready(function($){ |
| 97 | $('#post_name').on('keyup', function(){ |
| 98 | var val = $(this).val(); |
| 99 | if(!val.startsWith('group_')){ |
| 100 | var val = 'group_' + val; |
| 101 | $(this).val(val); |
| 102 | } |
| 103 | |
| 104 | $('[name="acf_field_group[key]"]').val(val); |
| 105 | $('.misc-pub-acfe-field-group-key code').html(val); |
| 106 | }); |
| 107 | }); |
| 108 | </script> |
| 109 | <?php |
| 110 | }, 'acf-field-group', 'normal', 'high'); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Field Group Options: Sidebar - Submit Div |
| 115 | */ |
| 116 | add_action('post_submitbox_misc_actions', 'acfe_render_field_group_submitbox'); |
| 117 | function acfe_render_field_group_submitbox(){ |
| 118 | if(get_post_type(get_the_ID()) !== 'acf-field-group') |
| 119 | return; |
| 120 | |
| 121 | $field_group = acf_get_field_group(get_the_ID()); |
| 122 | ?> |
| 123 | <div class="misc-pub-section misc-pub-acfe-field-group-key"> |
| 124 | <span style="font-size:16px;color: #82878c;" class="dashicons dashicons-tag"></span> <code style="font-size: 12px;"><?php echo $field_group['key']; ?></code> |
| 125 | </div> |
| 126 | <script type="text/javascript"> |
| 127 | (function($) { |
| 128 | $('.misc-pub-acfe-field-group-key').insertBefore('.misc-pub-post-status'); |
| 129 | })(jQuery); |
| 130 | </script> |
| 131 | <?php |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Field Group Options: Sidebar |
| 136 | */ |
| 137 | add_action('acf/field_group/admin_head', 'acfe_render_field_group_settings_side'); |
| 138 | function acfe_render_field_group_settings_side(){ |
| 139 | add_meta_box('acf-field-group-acfe-side', __('Advanced Settings', 'acfe'), function(){ |
| 140 | |
| 141 | global $field_group; |
| 142 | |
| 143 | acf_render_field_wrap(array( |
| 144 | 'label' => __('Display title', 'acfe'), |
| 145 | 'instructions' => __('Render this title on edit post screen', 'acfe'), |
| 146 | 'type' => 'text', |
| 147 | 'name' => 'acfe_display_title', |
| 148 | 'prefix' => 'acf_field_group', |
| 149 | 'value' => (isset($field_group['acfe_display_title'])) ? $field_group['acfe_display_title'] : '', |
| 150 | 'placeholder' => '', |
| 151 | 'prepend' => '', |
| 152 | 'append' => '' |
| 153 | )); |
| 154 | |
| 155 | if(acfe_is_field_group_json_desync($field_group)){ |
| 156 | acf_render_field_wrap(array( |
| 157 | 'label' => __('Json Desync'), |
| 158 | 'instructions' => __('Local json file is different from this version. If you manually synchronize it, you will lose your current field group settings'), |
| 159 | 'type' => 'acfe_dynamic_message', |
| 160 | 'name' => 'acfe_sync_available', |
| 161 | 'prefix' => 'acf_field_group', |
| 162 | 'value' => $field_group['key'], |
| 163 | )); |
| 164 | } |
| 165 | |
| 166 | acf_render_field_wrap(array( |
| 167 | 'label' => __('Auto Sync'), |
| 168 | 'instructions' => __('Synchronize field group upon update'), |
| 169 | 'type' => 'checkbox', |
| 170 | 'name' => 'acfe_autosync', |
| 171 | 'prefix' => 'acf_field_group', |
| 172 | 'value' => (isset($field_group['acfe_autosync']) && !empty($field_group['acfe_autosync'])) ? $field_group['acfe_autosync'] : array(), |
| 173 | 'choices' => array( |
| 174 | 'php' => 'PHP', |
| 175 | 'json' => 'Json', |
| 176 | ) |
| 177 | )); |
| 178 | |
| 179 | acf_render_field_wrap(array( |
| 180 | 'label' => __('Permissions'), |
| 181 | 'name' => 'acfe_permissions', |
| 182 | 'prefix' => 'acf_field_group', |
| 183 | 'type' => 'checkbox', |
| 184 | 'instructions' => __('Select user roles that are allowed to view and edit this field group in post edition'), |
| 185 | 'required' => false, |
| 186 | 'default_value' => false, |
| 187 | 'choices' => acfe_get_roles(), |
| 188 | 'value' => (isset($field_group['acfe_permissions'])) ? $field_group['acfe_permissions'] : array(), |
| 189 | 'layout' => 'vertical' |
| 190 | )); |
| 191 | |
| 192 | ?> |
| 193 | <script type="text/javascript"> |
| 194 | if(typeof acf !== 'undefined'){ |
| 195 | acf.postbox.render({ |
| 196 | 'id': 'acf-field-group-acfe-side', |
| 197 | 'label': 'top' |
| 198 | }); |
| 199 | } |
| 200 | |
| 201 | (function($){ |
| 202 | if($('[data-name=acfe_sync_available]').length){ |
| 203 | |
| 204 | if($('[data-name=acfe_sync_available]').find('[data-acfe-autosync-json-active]').attr('data-acfe-autosync-json-active') == '0'){ |
| 205 | $('#acf_field_group-acfe_autosync-json').change(function(e){ |
| 206 | if($(this).prop('checked')){ |
| 207 | if(!confirm('Local json file was found and is different from this version.' + "\n" + 'Enabling json auto sync will erase the local file with the current field group settings')){ |
| 208 | $(this).prop('checked', false); |
| 209 | return false; |
| 210 | } |
| 211 | } |
| 212 | }); |
| 213 | } |
| 214 | |
| 215 | else{ |
| 216 | |
| 217 | $('#publish').click(function(e){ |
| 218 | if(!confirm('Local json file was found and is different from this version.' + "\n" + 'Proceed to erase the local file with the current field group settings')) |
| 219 | e.preventDefault(); |
| 220 | }); |
| 221 | |
| 222 | } |
| 223 | } |
| 224 | })(jQuery); |
| 225 | </script> |
| 226 | <?php |
| 227 | }, 'acf-field-group', 'side'); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Render: Sync Available |
| 232 | */ |
| 233 | add_action('acf/render_field/name=acfe_sync_available', 'acfe_render_field_sync_available'); |
| 234 | function acfe_render_field_sync_available($field){ |
| 235 | $field_group = acf_get_field_group($field['value']); |
| 236 | |
| 237 | $acfe_autosync_active = 0; |
| 238 | if(isset($field_group['acfe_autosync']) && is_array($field_group['acfe_autosync']) && in_array('json', $field_group['acfe_autosync'])) |
| 239 | $acfe_autosync_active = 1; |
| 240 | |
| 241 | $nonce = wp_create_nonce('bulk-posts'); |
| 242 | echo '<a data-acfe-autosync-json-active="'.$acfe_autosync_active.'" class="button" href="'.admin_url('edit.php?post_type=acf-field-group&post_status=sync&acfsync=' . $field['value'] . '&_wpnonce=' . $nonce).'">Synchronize</a>'; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Render: Sync Warnings |
| 247 | */ |
| 248 | add_action('acf/render_field/name=acfe_autosync', 'acfe_render_field_acfe_sync_warnings'); |
| 249 | function acfe_render_field_acfe_sync_warnings($field){ |
| 250 | global $field_group; |
| 251 | |
| 252 | // PHP |
| 253 | |
| 254 | // Fix to load local fiel groups |
| 255 | acf_enable_filters(); |
| 256 | |
| 257 | if(acfe_has_field_group_autosync($field_group, 'php') && !acf_get_setting('acfe_php_found')){ |
| 258 | echo '<p class="description"><span style="color:#ccc;font-size:16px;vertical-align:text-top;" class="dashicons dashicons-warning"></span> Folder <code style="font-size:11px;">/acfe-php</code> was not found in your theme. You must create it to activate PHP Sync</p>'; |
| 259 | } |
| 260 | |
| 261 | elseif(!acfe_has_field_group_autosync($field_group, 'php') && acfe_has_field_group_autosync_file($field_group, 'php')){ |
| 262 | echo '<p class="description"><span style="color:#ccc;font-size:16px;vertical-align:text-top;" class="dashicons dashicons-warning"></span> This field group is registered via a third-party PHP code</p>'; |
| 263 | } |
| 264 | |
| 265 | elseif(acfe_has_field_group_autosync($field_group, 'php') && !acfe_has_field_group_autosync_file($field_group, 'php')){ |
| 266 | echo '<p class="description"><span style="color:#ccc;font-size:16px;vertical-align:text-top;" class="dashicons dashicons-warning"></span> This field group is not registered locally. Update it to resync PHP</p>'; |
| 267 | } |
| 268 | |
| 269 | // Re-disable filters, as natively |
| 270 | acf_disable_filters(); |
| 271 | |
| 272 | // Json |
| 273 | |
| 274 | if(!acfe_has_field_group_autosync($field_group, 'json') && acfe_has_field_group_autosync_file($field_group, 'json')){ |
| 275 | echo '<p class="description"><span style="color:#ccc;font-size:16px;vertical-align:text-top;" class="dashicons dashicons-warning"></span> <code style="font-size:11px;">' . $field_group['key'] . '.json</code> was found. Activate Json sync to keep it updated</p>'; |
| 276 | } |
| 277 | |
| 278 | elseif(acfe_has_field_group_autosync($field_group, 'json') && !acfe_has_field_group_autosync_file($field_group, 'json')){ |
| 279 | echo '<p class="description"><span style="color:#ccc;font-size:16px;vertical-align:text-top;" class="dashicons dashicons-warning"></span> <code style="font-size:11px;">' . $field_group['key'] . '.json</code> was not found. Update this field group to regenerate it</p>'; |
| 280 | } |
| 281 | |
| 282 | |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Render: Data button |
| 287 | */ |
| 288 | add_action('acf/render_field/name=acfe_data', 'acfe_render_field_group_data'); |
| 289 | function acfe_render_field_group_data($field){ |
| 290 | $field_group = acf_get_field_group($field['value']); |
| 291 | if(!$field_group){ |
| 292 | echo '<a href="#" class="button disabled" disabled>' . __('Data') . '</a>'; |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | echo '<a href="#" class="button acfe_modal_open" data-modal-key="' . $field_group['key'] . '">' . __('Data') . '</a>'; |
| 297 | echo '<div class="acfe_modal acfe_modal_data hidden" data-modal-key="' . $field_group['key'] . '"><pre>' . print_r($field_group, true) . '</pre></div>'; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Default: Label placement - Left |
| 302 | */ |
| 303 | add_filter('acf/validate_field_group', 'acfc_field_group_options'); |
| 304 | function acfc_field_group_options($field_group){ |
| 305 | $field_group['label_placement'] = 'left'; |
| 306 | return $field_group; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Field Group Title (post edit) |
| 311 | */ |
| 312 | add_filter('acf/get_field_groups', 'acfe_render_field_groups', 999); |
| 313 | function acfe_render_field_groups($field_groups){ |
| 314 | $check_current_screen = acf_is_screen(array( |
| 315 | 'edit-acf-field-group', |
| 316 | 'acf-field-group', |
| 317 | 'acf_page_acf-tools' |
| 318 | )); |
| 319 | |
| 320 | if($check_current_screen) |
| 321 | return $field_groups; |
| 322 | |
| 323 | foreach($field_groups as &$field_group){ |
| 324 | if(!isset($field_group['acfe_display_title']) || empty($field_group['acfe_display_title'])) |
| 325 | continue; |
| 326 | |
| 327 | $field_group['title'] = $field_group['acfe_display_title']; |
| 328 | } |
| 329 | |
| 330 | return $field_groups; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Field Group Permissions (post edit) |
| 335 | */ |
| 336 | add_filter('acf/get_field_groups', 'acfe_permissions_field_groups', 999); |
| 337 | function acfe_permissions_field_groups($field_groups){ |
| 338 | $check_current_screen = acf_is_screen(array( |
| 339 | 'edit-acf-field-group', |
| 340 | 'acf-field-group', |
| 341 | 'acf_page_acf-tools' |
| 342 | )); |
| 343 | |
| 344 | if($check_current_screen) |
| 345 | return $field_groups; |
| 346 | |
| 347 | $current_user_roles = acfe_get_current_user_roles(); |
| 348 | |
| 349 | foreach($field_groups as $key => $field_group){ |
| 350 | if(!isset($field_group['acfe_permissions']) || empty($field_group['acfe_permissions'])) |
| 351 | continue; |
| 352 | |
| 353 | $render_field_group = false; |
| 354 | |
| 355 | foreach($current_user_roles as $current_user_role){ |
| 356 | foreach($field_group['acfe_permissions'] as $field_group_role){ |
| 357 | if($current_user_role !== $field_group_role) |
| 358 | continue; |
| 359 | |
| 360 | $render_field_group = true; |
| 361 | break; |
| 362 | } |
| 363 | |
| 364 | if($render_field_group) |
| 365 | break; |
| 366 | } |
| 367 | |
| 368 | if(!$render_field_group) |
| 369 | unset($field_groups[$key]); |
| 370 | } |
| 371 | |
| 372 | return $field_groups; |
| 373 | } |