field-groups.php
322 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | /** |
| 7 | * Display Title (post states) |
| 8 | */ |
| 9 | add_filter('display_post_states', 'acfe_field_groups_states', 10, 2); |
| 10 | function acfe_field_groups_states($states, $post){ |
| 11 | if(!acf_is_screen('edit-acf-field-group')) |
| 12 | return $states; |
| 13 | |
| 14 | if(get_post_type($post->ID) != 'acf-field-group') |
| 15 | return $states; |
| 16 | |
| 17 | $field_group = acf_get_field_group($post->ID); |
| 18 | |
| 19 | if(!$field_group || !isset($field_group['acfe_display_title']) || empty($field_group['acfe_display_title'])) |
| 20 | return $states; |
| 21 | |
| 22 | $states[] = $field_group['acfe_display_title']; |
| 23 | return $states; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Table Columns |
| 28 | */ |
| 29 | add_filter('manage_edit-acf-field-group_columns', 'acfe_field_groups_column', 999); |
| 30 | function acfe_field_groups_column($columns){ |
| 31 | |
| 32 | // Fix 'Sync' screen columns |
| 33 | if(acf_maybe_get_GET('post_status') === 'sync'){ |
| 34 | unset($columns['acf-field-group-type']); |
| 35 | return $columns; |
| 36 | } |
| 37 | |
| 38 | // Locations |
| 39 | $columns['acfe-locations'] = __('Locations'); |
| 40 | |
| 41 | // Load |
| 42 | $columns['acfe-local'] = __('Load'); |
| 43 | |
| 44 | // PHP sync |
| 45 | if(acf_get_setting('acfe_php')) |
| 46 | $columns['acfe-autosync-php'] = __('PHP sync'); |
| 47 | |
| 48 | // Json sync |
| 49 | if(acf_get_setting('json')) |
| 50 | $columns['acfe-autosync-json'] = __('Json sync'); |
| 51 | |
| 52 | return $columns; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Table Columns HTML |
| 57 | */ |
| 58 | add_action('manage_acf-field-group_posts_custom_column', 'acfe_field_groups_column_html', 10, 2); |
| 59 | function acfe_field_groups_column_html($column, $post_id){ |
| 60 | |
| 61 | /** |
| 62 | * Locations |
| 63 | */ |
| 64 | if($column == 'acfe-locations'){ |
| 65 | $field_group = acf_get_field_group($post_id); |
| 66 | $choices = acf_get_location_rule_types(); |
| 67 | |
| 68 | if(!isset($field_group['location']) || empty($field_group['location']) ||empty($choices)) |
| 69 | return; |
| 70 | |
| 71 | $final = array(); |
| 72 | |
| 73 | $icon_default = 'admin-generic'; |
| 74 | |
| 75 | $icons = array( |
| 76 | 'edit' => array( |
| 77 | 'post_type', |
| 78 | 'post_template', |
| 79 | 'post_status', |
| 80 | 'post_format', |
| 81 | 'post', |
| 82 | ), |
| 83 | 'media-default' => array( |
| 84 | 'page_template', |
| 85 | 'page_type', |
| 86 | 'page_parent', |
| 87 | 'page', |
| 88 | ), |
| 89 | 'admin-users' => array( |
| 90 | 'current_user', |
| 91 | 'user_form', |
| 92 | ), |
| 93 | 'welcome-widgets-menus' => array( |
| 94 | 'widget', |
| 95 | 'nav_menu', |
| 96 | 'nav_menu_item', |
| 97 | ), |
| 98 | 'category' => array( |
| 99 | 'taxonomy', |
| 100 | 'post_category', |
| 101 | 'post_taxonomy', |
| 102 | ), |
| 103 | 'admin-comments' => array( |
| 104 | 'comment', |
| 105 | ), |
| 106 | 'paperclip' => array( |
| 107 | 'attachment', |
| 108 | ), |
| 109 | 'admin-settings' => array( |
| 110 | 'options_page', |
| 111 | ), |
| 112 | 'businessman' => array( |
| 113 | 'current_user_role', |
| 114 | 'user_role', |
| 115 | ), |
| 116 | ); |
| 117 | |
| 118 | foreach($choices as $key => $sub_choices){ |
| 119 | foreach($sub_choices as $choice_slug => $choice_name){ |
| 120 | |
| 121 | $final_icon = $icon_default; |
| 122 | foreach($icons as $icon => $icon_slugs){ |
| 123 | foreach($icon_slugs as $icon_slug){ |
| 124 | if($choice_slug != $icon_slug) |
| 125 | continue; |
| 126 | |
| 127 | $final_icon = $icon; |
| 128 | break(2); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | $final[$choice_slug] = array( |
| 133 | 'name' => $choice_name, |
| 134 | 'icon' => $final_icon |
| 135 | ); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | |
| 140 | |
| 141 | $html = array(); |
| 142 | foreach($field_group['location'] as $or){ |
| 143 | foreach($or as $and){ |
| 144 | if(!isset($final[$and['param']])) |
| 145 | continue; |
| 146 | |
| 147 | $final_name = $and['value']; |
| 148 | $values = acf_get_location_rule_values($and); |
| 149 | |
| 150 | if(!empty($values) && is_array($values)){ |
| 151 | foreach($values as $value_slug => $value_name){ |
| 152 | if($and['value'] != $value_slug) |
| 153 | continue; |
| 154 | |
| 155 | $final_name = $value_name; |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | $name = '<span class="acf-js-tooltip dashicons dashicons-' . $final[$and['param']]['icon'] . '" title="' . $final[$and['param']]['name'] . ' = ' . $final_name . '"></span>'; |
| 161 | if($and['operator'] == '!=') |
| 162 | $name = '<span class="acf-js-tooltip dashicons dashicons-' . $final[$and['param']]['icon'] . '" title="' . $final[$and['param']]['name'] . ' != ' . $final_name . '" style="color:#ccc;"></span>'; |
| 163 | |
| 164 | $html[] = $name; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | echo implode(' ', $html); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Load |
| 173 | */ |
| 174 | elseif($column == 'acfe-local'){ |
| 175 | |
| 176 | if(!$field_group = acf_get_field_group($post_id)) |
| 177 | return; |
| 178 | |
| 179 | $local_field_group = acf_get_local_field_group($field_group['key']); |
| 180 | $local_field_group_type = acf_maybe_get($local_field_group, 'local', false); |
| 181 | |
| 182 | if($local_field_group_type != 'php'){ |
| 183 | echo __('DB'); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | echo '<span class="acf-js-tooltip" title="' . $field_group['key'] . ' is registered locally">php</span>'; |
| 188 | |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * PHP sync |
| 193 | */ |
| 194 | elseif($column == 'acfe-autosync-php'){ |
| 195 | |
| 196 | if(!$field_group = acf_get_field_group($post_id)) |
| 197 | return; |
| 198 | |
| 199 | if(!acfe_has_field_group_autosync($field_group, 'php')){ |
| 200 | echo '<span style="color:#ccc" class="dashicons dashicons-no-alt"></span>'; |
| 201 | |
| 202 | if(acfe_has_field_group_autosync_file($field_group, 'php')){ |
| 203 | echo '<span style="color:#ccc;font-size:16px;vertical-align:text-top;" class="acf-js-tooltip dashicons dashicons-warning" title="Field group: ' . $field_group['key'] . ' is registered via a third-party PHP code"></span>'; |
| 204 | } |
| 205 | |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | if(!acf_get_setting('acfe_php_found')){ |
| 210 | echo '<span style="color:#ccc" class="dashicons dashicons-yes"></span>'; |
| 211 | |
| 212 | echo '<span style="color:#ccc;font-size:16px;vertical-align:text-top;" class="acf-js-tooltip dashicons dashicons-warning" title="Folder \'/acfe-php\' was not found in your theme.<br />You must create it to activate this setting"></span>'; |
| 213 | } |
| 214 | |
| 215 | elseif(!acfe_has_field_group_autosync_file($field_group, 'php')){ |
| 216 | echo '<span class="dashicons dashicons-yes"></span>'; |
| 217 | |
| 218 | echo '<span style="color:#ccc;font-size:16px;vertical-align:text-top;" class="acf-js-tooltip dashicons dashicons-warning" title="Field group: ' . $field_group['key'] . ' is not registered locally.<br />Update the field group to resync PHP"></span>'; |
| 219 | } |
| 220 | |
| 221 | else{ |
| 222 | |
| 223 | echo '<span class="dashicons dashicons-yes"></span>'; |
| 224 | |
| 225 | } |
| 226 | |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Json sync |
| 231 | */ |
| 232 | elseif($column == 'acfe-autosync-json'){ |
| 233 | |
| 234 | if(!$field_group = acf_get_field_group($post_id)) |
| 235 | return; |
| 236 | |
| 237 | if(!acfe_has_field_group_autosync($field_group, 'json')){ |
| 238 | echo '<span style="color:#ccc" class="dashicons dashicons-no-alt"></span>'; |
| 239 | if(acfe_has_field_group_autosync_file($field_group, 'json')) |
| 240 | echo '<span style="color:#ccc;font-size:16px;vertical-align:text-top;" class="acf-js-tooltip dashicons dashicons-warning" title="Local file: ' . $field_group['key'] . '.json was found.<br />Activate Json sync in the field group settings to keep it updated"></span>'; |
| 241 | |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | echo '<span class="dashicons dashicons-yes"></span>'; |
| 246 | if(!acfe_has_field_group_autosync_file($field_group, 'json')) |
| 247 | echo '<span style="color:#ccc;font-size:16px;vertical-align:text-top;" class="acf-js-tooltip dashicons dashicons-warning" title="Local file: ' . $field_group['key'] . '.json was not found.<br />Update the field group to regenerate it"></span>'; |
| 248 | |
| 249 | } |
| 250 | } |
| 251 | /** |
| 252 | * Table Row Actions |
| 253 | */ |
| 254 | add_filter('page_row_actions', 'hwk_post_type_exemple_row_actions', 10, 2); |
| 255 | function hwk_post_type_exemple_row_actions($actions, $post){ |
| 256 | if(!isset($post->post_type) || $post->post_type != 'acf-field-group') |
| 257 | return $actions; |
| 258 | |
| 259 | $field_group = acf_get_field_group($post->ID); |
| 260 | |
| 261 | $actions['acfe-export-php'] = '<a href="' . admin_url('edit.php?post_type=acf-field-group&page=acf-tools&tool=export&keys=' . $field_group['key']) . '">PHP</a>'; |
| 262 | $actions['acfe-export-json'] = '<span class="acfe-form" data-action="'.admin_url('edit.php?post_type=acf-field-group&page=acf-tools&tool=export').'"><input type="hidden" name="_acf_nonce" value="' . wp_create_nonce('export') . '" /><input type="hidden" name="action" value="download" /><input type="hidden" name="keys" value="' . $field_group['key'] . '" /><a href="#">Json</a></span>'; |
| 263 | //$actions['acfe-id'] = '<span style="color:#555;">ID: ' . $field_group['ID'] . '</span>'; |
| 264 | $actions['acfe-key'] = '<span style="color:#555;"><code style="-webkit-user-select: all;-moz-user-select: all;-ms-user-select: all;user-select: all;font-size: 12px;">' . $field_group['key'] . '</code></span>'; |
| 265 | return $actions; |
| 266 | } |
| 267 | |
| 268 | //add_filter('acf/validate_field_group', 'acfe_test_local_fg', 999); |
| 269 | function acfe_test_local_fg($field_group){ |
| 270 | |
| 271 | error_log(print_r($field_group, true)); |
| 272 | //_print_r($field_group); |
| 273 | |
| 274 | return $field_group; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Sidebar |
| 279 | */ |
| 280 | add_action('current_screen',function(){ |
| 281 | if(!acf_is_screen('edit-acf-field-group')) |
| 282 | return; |
| 283 | |
| 284 | add_action('admin_footer', function(){ |
| 285 | ?> |
| 286 | |
| 287 | <!-- ACFE: Label --> |
| 288 | <script type="text/html" id="tmpl-acfe-label"> |
| 289 | <span style="word-wrap: break-word;padding: 2px 6px;margin-left:1px;border-radius:2px;background:#ca4a1f;color: #fff; font-size: 14px;vertical-align: text-bottom;font-style: italic;">Extended</span> |
| 290 | </script> |
| 291 | |
| 292 | <!-- ACFE: Debug --> |
| 293 | <script type="text/html" id="tmpl-acfe-debug"> |
| 294 | <div class="acf-box"> |
| 295 | <?php _print_r(acf_get_local_field_groups()); ?> |
| 296 | </div> |
| 297 | </script> |
| 298 | |
| 299 | <script type="text/javascript"> |
| 300 | (function($){ |
| 301 | |
| 302 | // ACFE: Label |
| 303 | $('.acf-column-2 > .acf-box > .inner > h2').append($('#tmpl-acfe-label').html()); |
| 304 | |
| 305 | // ACFE: Debug |
| 306 | //$('#posts-filter').append($('#tmpl-acfe-debug').html()); |
| 307 | |
| 308 | // ACFE: Table Row Actions - Export Wrap Form |
| 309 | $('.acfe-export-json .acfe-form').each(function(k, v){ |
| 310 | $(this).wrapAll('<form action="' + $(this).attr('data-action') + '" method="post" style="display:inline;"></form>'); |
| 311 | }); |
| 312 | |
| 313 | // ACFE: Table Row Actions - Export Submit Form |
| 314 | $('.acfe-export-json a').click(function(e){ |
| 315 | e.preventDefault(); |
| 316 | $(this).closest('form').submit(); |
| 317 | }); |
| 318 | })(jQuery); |
| 319 | </script> |
| 320 | <?php |
| 321 | }); |
| 322 | }); |