1<?php
2function wfp_documents_setup_id_incr()
3{
4
5 // Check if user has rights to set it up and ACF is enabled.
6 if (current_user_can('administrator') && function_exists('get_field')):
7
8 // Initial value
9 // === YOU NEED TO UPDATE HERE ===
10 // Replace <code>custom_invoice_id</code> with your desired id name.
11 add_option('custom_invoice_id', '0001', '', 'yes');
12
13 /**
14 * Wrapper to get the id (if i would need to add something to it)
15 * @return mixed|void – stored next available id
16 */
17 function wfp_get_current_invoice_id()
18 {
19 return get_option('custom_invoice_id');
20 }
21
22 /**
23 * Count up the id by one and update the globally stored id
24 */
25 function wfp_increase_invoice_id()
26 {
27 $curr_invoice_id = get_option('custom_invoice_id');
28 $next_invoice_id = intval($curr_invoice_id) + 1;
29 update_option('custom_invoice_id', $next_invoice_id);
30 }
31
32 /**
33 * Populate the acf field when loading it.
34 */
35 function wfp_auto_get_current_document_id($value, $post_id, $field)
36 {
37 // If the custom field already has a value in it, just return this one (we don't want to overwrite it every single time)
38 if ($value !== null && $value !== "") {
39 return $value;
40 }
41
42 // If the field is empty, get the currently stored next available id and fill it in the field.
43 $value = wfp_get_current_invoice_id();
44 return $value;
45 }
46
47 // Use ACF hooks to populate the field on load
48 // ==== YOU NEED TO UPDATE HERE ====
49 // Replace <code>invoice_id</code> with the name of your field.
50 add_filter('acf/load_value/name=invoice_id', 'wfp_auto_get_current_document_id', 10, 3);
51
52 /**
53 * Registers function to check if the globally stored id needs to be updated after a post is saved.
54 */
55 function wfp_acf_save_post($post_id)
56 {
57 // Check if the post had any ACF to begin with.
58 if (!isset($_POST['acf'])) {
59 return;
60 }
61
62 $fields = $_POST['acf'];
63
64 // Only move to the next id when any ACF fields were added to that post, otherwise this might be an accident and would skip an id.
65 if ($_POST['_acf_changed'] == 0) {
66 return;
67 }
68
69 // Next we need to find the field out id is stored in
70 foreach ($fields as $field_key => $value) {
71 $field_object = get_field_object($field_key);
72
73 /**
74 * If we found our field and the value of that field is the same as our currently "next available id" –
75 * we need to increase this id, so the next post doesn't use the same id.
76 */
77 if ($field_object['name'] == "invoice_id"
78 && wfp_get_current_invoice_id() == $value) {
79 wfp_increase_invoice_id();
80
81 return;
82 }
83 }
84 }
85
86 // Use a hook to run this function every time a post is saved.
87 add_action('acf/save_post', 'wfp_acf_save_post', 20);
88
89 /**
90 * The code below just displays the currently stored next id on an acf-options-page,
91 * so it's easy to see which id you're on. The field is disabled to prevent easy tinkering with the id.
92 */
93 function wfp_load_current_document_ids_settingspage($value, $postid, $field)
94 {
95 if ($field['name'] == "document_ids-group_current_invoice_id") {
96 return wfp_get_current_invoice_id();
97 }
98 return $value;
99 }
100
101 function wfp_disable_acf_field($field)
102 {
103 $field['disabled'] = 1;
104
105 return $field;
106 }
107
108 add_filter('acf/load_field/name=current_invoice_id', 'wfp_disable_acf_field', 10, 3);
109 add_filter('acf/load_value/name=current_invoice_id', 'wfp_load_current_document_ids_settingspage', 10, 3);
110
111 endif;
112}
113
114add_action('init', 'wfp_documents_setup_id_incr');
115?>