|
1 | 1 | <?php |
2 | 2 | /** |
3 | 3 | * WARNING! THIS SNIPPET IS DEPRECATED. 🚧 |
| 4 | + * This snippet's functionality has been moved to [GP Inventory][1] in version 1.0. |
4 | 5 | * |
5 | | - * Gravity Perks // Inventory // Inventory Shortcode |
6 | | - * https://gravitywiz.com/documentation/gravity-forms-inventory/ |
7 | | - * |
8 | | - * Instruction Video: https://www.loom.com/share/9d1897075b7b435389479d4f17fc6807 |
9 | | - * |
10 | | - * Update "123" to your form ID and "4" to your field ID with inventory. |
11 | | - * |
12 | | - * `[gravityforms action="inventory" id="123" field="4"]` |
13 | | - * |
14 | | - * If using a field with scopes, you may provide the scope values in a comma-delimited list: |
15 | | - * |
16 | | - * `[gravityforms action="inventory" id="123" field="4" scope_values="Scope Value,Another Scope Value"]` |
17 | | - * |
18 | | - * This snippet requires GP Inventory 1.0-beta-1.7 or newer. |
19 | | - * |
20 | | - * @todo |
21 | | - * - Add support for excluding field parameter and showing a consolidated list of all inventories. |
| 6 | + * [1] https://gravitywiz.com/documentation/gravity-forms-inventory/ |
22 | 7 | */ |
23 | | -add_filter( 'gform_shortcode_inventory', 'gpi_inventory_shortcode', 10, 3 ); |
24 | | -function gpi_inventory_shortcode( $output, $atts, $content ) { |
25 | | - |
26 | | - if ( ! is_callable( 'gp_inventory' ) ) { |
27 | | - return $output; |
28 | | - } |
29 | | - |
30 | | - $atts = shortcode_atts( array( |
31 | | - 'id' => false, |
32 | | - 'field' => false, |
33 | | - 'scope_values' => false, |
34 | | - ), $atts, 'gpi_inventory' ); |
35 | | - |
36 | | - if ( empty( $atts['id'] ) || empty( $atts['field'] ) ) { |
37 | | - return $content; |
38 | | - } |
39 | | - |
40 | | - $form = GFAPI::get_form( $atts['id'] ); |
41 | | - |
42 | | - /* Ensure choices populated by Populate Anything are loaded in. */ |
43 | | - if ( function_exists( 'gp_populate_anything' ) && is_callable( array( gp_populate_anything(), 'modify_admin_field_choices' ) ) ) { |
44 | | - $form = gp_populate_anything()->modify_admin_field_choices( $form ); |
45 | | - } |
46 | | - |
47 | | - $field = GFAPI::get_field( $form, $atts['field'] ); |
48 | | - |
49 | | - if ( ! $field ) { |
50 | | - return $content; |
51 | | - } |
52 | | - |
53 | | - /** |
54 | | - * Some scopes have infinite values (i.e. Date fields). Use the * scope value to look up submitted scope values and |
55 | | - * display the content template for each. Default template when using the * scope is: |
56 | | - * |
57 | | - * {scope}: {count} |
58 | | - * |
59 | | - * NOTE: This only works with inventories with a single scope. Will continue exploring this in the future. |
60 | | - */ |
61 | | - if ( $atts['scope_values'] && $atts['scope_values'] === '*' ) { |
62 | | - global $wpdb; |
63 | | - |
64 | | - $resource_field_id = reset( $field->gpiResourcePropertyMap ); |
65 | | - $sql = gf_apply_filters( array( 'gpis_scopes_sql', $form['id'], $resource_field_id ), $wpdb->prepare( "SELECT DISTINCT meta_value FROM {$wpdb->prefix}gf_entry_meta WHERE form_id = %d AND meta_key = %d", $form['id'], $resource_field_id ), $form, $field, $resource_field_id ); |
66 | | - |
67 | | - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
68 | | - $items = $wpdb->get_results( $sql ); |
69 | | - |
70 | | - if ( empty( $items ) ) { |
71 | | - return $content; |
72 | | - } |
73 | | - |
74 | | - $output = array(); |
75 | | - $is_choice_field = ! empty( $field->choices ); |
76 | | - |
77 | | - if ( ! $content ) { |
78 | | - if ( $is_choice_field ) { |
79 | | - $content = '{label}: {count}'; |
80 | | - } else { |
81 | | - $content = '{scope}: {count}'; |
82 | | - } |
83 | | - } |
84 | | - |
85 | | - foreach ( $items as $item ) { |
86 | | - |
87 | | - $atts['scope_values'] = $item->meta_value; |
88 | | - $scope_display_value = gf_apply_filters( array( 'gpis_scope_display_value', $form['id'], $resource_field_id ), $item->meta_value, $field, $resource_field_id, $atts ); |
89 | | - |
90 | | - $item_output = ''; |
91 | | - if ( $is_choice_field ) { |
92 | | - $item_output = $scope_display_value; |
93 | | - } |
94 | | - |
95 | | - $item_output .= str_replace( '{scope}', $scope_display_value, gpi_inventory_shortcode( null, $atts, $content ) ); |
96 | | - $output[] = $item_output; |
97 | | - |
98 | | - } |
99 | | - |
100 | | - $label = $field->get_field_label( false, '' ); |
101 | | - |
102 | | - $output = sprintf( |
103 | | - '<ul class="gpi-inventory-list gpi-inventory-list-%d-%d"><li>%s</li></ul>', |
104 | | - $form['id'], $field->id, implode( '</li><li>', $output ) |
105 | | - ); |
106 | | - |
107 | | - return $output; |
108 | | - } |
109 | | - |
110 | | - /** |
111 | | - * Callback for mapping property values from the scope_values attribute to the gpi_property_map_values filter to |
112 | | - * get the current inventory for scoped inventory fields. |
113 | | - * |
114 | | - * @param $property_values |
115 | | - * |
116 | | - * @return mixed |
117 | | - */ |
118 | | - $map_property_values = function ( $property_values ) use ( $atts ) { |
119 | | - $scope_values = explode( ',', $atts['scope_values'] ); |
120 | | - |
121 | | - if ( empty( $scope_values ) || empty( array_filter( $scope_values ) ) ) { |
122 | | - return $property_values; |
123 | | - } |
124 | | - |
125 | | - /* Take the order of property value keys and map them according to the order of the values in scope_values |
126 | | - as the order of the properties should never change. */ |
127 | | - $property_value_keys = array_keys( $property_values ); |
128 | | - |
129 | | - foreach ( $scope_values as $scope_value_index => $scope_value ) { |
130 | | - if ( ! isset( $property_value_keys [ $scope_value_index ] ) ) { |
131 | | - continue; |
132 | | - } |
133 | | - |
134 | | - $property_id = $property_value_keys [ $scope_value_index ]; |
135 | | - |
136 | | - $property_values [ $property_id ] = $scope_value; |
137 | | - } |
138 | | - |
139 | | - return $property_values; |
140 | | - }; |
141 | | - |
142 | | - $items = array(); |
143 | | - |
144 | | - add_filter( 'gpi_property_map_values_' . $form['id'] . '_' . $field->id, $map_property_values ); |
145 | | - |
146 | | - if ( is_array( $field->choices ) ) { |
147 | | - $field->gpiShowAvailableInventory = true; |
148 | | - |
149 | | - // Delete choice count cache before and after getting choice counts to prevent scopes from interfering. |
150 | | - $cache_key = 'gpi_choice_counts_' . $form['id'] . '_' . $field->id; |
151 | | - GFCache::delete( $cache_key ); |
152 | | - |
153 | | - if ( $content ) { |
154 | | - $counts = gp_inventory_type_choices()->get_choice_counts( $form['id'], $field ); |
155 | | - foreach ( $field->choices as $choice ) { |
156 | | - |
157 | | - $limit = (int) $choice['inventory_limit']; |
158 | | - $count = (int) rgar( $counts, $choice['value'] ); |
159 | | - $available = $limit - $count; |
160 | | - |
161 | | - $items[] = gpis_get_item_markup( $content, array( |
162 | | - 'limit' => $limit, |
163 | | - 'count' => $count, |
164 | | - 'available' => $available, |
165 | | - 'label' => $choice['text'], |
166 | | - 'value' => $choice['value'], |
167 | | - ) ); |
168 | | - |
169 | | - } |
170 | | - } else { |
171 | | - $choices = gp_inventory_type_choices()->apply_choice_limits( $field->choices, $field, $form ); |
172 | | - foreach ( $choices as $choice ) { |
173 | | - $items[] = $choice['text']; |
174 | | - } |
175 | | - } |
176 | | - |
177 | | - GFCache::delete( $cache_key ); |
178 | | - |
179 | | - $output = sprintf( |
180 | | - '<ul class="gpi-inventory-list gpi-inventory-list-%d-%d"><li>%s</li></ul>', |
181 | | - $form['id'], $field->id, implode( '</li><li>', $items ) |
182 | | - ); |
183 | | - } else { |
184 | | - if ( gp_inventory_type_advanced()->is_applicable_field( $field ) ) { |
185 | | - $available = gp_inventory_type_advanced()->get_available_stock( $field ); |
186 | | - $limit = gp_inventory_type_advanced()->get_stock_quantity( $field ); |
187 | | - $count = gp_inventory_type_advanced()->get_claimed_inventory( $field ); |
188 | | - |
189 | | - /** |
190 | | - * Temporarily remove filter for resources to get the inventory count specific to the field rather than the |
191 | | - * count of all fields using the same resource. |
192 | | - */ |
193 | | - remove_filter( 'gpi_query', array( gp_inventory_type_advanced(), 'resource_and_properties' ), 9 ); |
194 | | - $count_current_field = gp_inventory_type_simple()->get_claimed_inventory( $field ); |
195 | | - add_filter( 'gpi_query', array( gp_inventory_type_advanced(), 'resource_and_properties' ), 9, 2 ); |
196 | | - } else { |
197 | | - $available = gp_inventory_type_simple()->get_available_stock( $field ); |
198 | | - $limit = gp_inventory_type_simple()->get_stock_quantity( $field ); |
199 | | - $count = gp_inventory_type_simple()->get_claimed_inventory( $field ); |
200 | | - $count_current_field = $count; |
201 | | - } |
202 | | - |
203 | | - $label = $field->get_field_label( false, '' ); |
204 | | - |
205 | | - if ( $content ) { |
206 | | - $output .= gpis_get_item_markup( $content, array( |
207 | | - 'limit' => $limit, |
208 | | - 'count' => $count, |
209 | | - 'count_current_field' => $count_current_field, |
210 | | - 'available' => $available, |
211 | | - 'label' => $label, |
212 | | - 'value' => '', |
213 | | - ) ); |
214 | | - } else { |
215 | | - $output .= $label . ': ' . $available; |
216 | | - } |
217 | | - } |
218 | | - |
219 | | - remove_filter( 'gpi_property_map_values_' . $form['id'] . '_' . $field->id, $map_property_values ); |
220 | | - |
221 | | - return $output; |
222 | | -} |
223 | | - |
224 | | -function gpis_get_item_markup( $template, $args ) { |
225 | | - |
226 | | - $markup = $template; |
227 | | - |
228 | | - $markup = str_replace( '{limit}', $args['limit'], $markup ); |
229 | | - $markup = str_replace( '{count}', $args['count'], $markup ); |
230 | | - $markup = str_replace( '{count_current_field}', $args['count_current_field'], $markup ); |
231 | | - $markup = str_replace( '{available}', $args['available'], $markup ); |
232 | | - $markup = str_replace( '{label}', $args['label'], $markup ); |
233 | | - $markup = str_replace( '{value}', $args['value'], $markup ); |
234 | | - |
235 | | - return $markup; |
236 | | -} |
0 commit comments