Skip to content

Commit c918cbe

Browse files
author
Woo
committed
Updates to 3.6.0
1 parent 6d3f879 commit c918cbe

15 files changed

+1426
-385
lines changed

admin/gravityforms-product-addons-admin.php

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,59 @@ public static function register() {
1212

1313
private function __construct() {
1414

15-
add_action( 'admin_enqueue_scripts', [ $this, 'on_admin_enqueue_scripts' ], 100 );
16-
add_action( 'admin_notices', [ $this, 'admin_install_notices' ] );
15+
add_action( 'admin_enqueue_scripts', array( $this, 'on_admin_enqueue_scripts' ), 100 );
16+
add_action( 'admin_notices', array( $this, 'admin_install_notices' ) );
1717

18-
add_action( 'woocommerce_process_product_meta', [ $this, 'process_meta_box' ], 1, 2 );
19-
add_action( 'admin_notices', [ $this, 'on_admin_notices' ] );
18+
add_action( 'woocommerce_process_product_meta', array( $this, 'process_meta_box' ), 1, 2 );
19+
add_action( 'admin_notices', array( $this, 'on_admin_notices' ) );
2020

21-
add_action( 'woocommerce_product_write_panel_tabs', [ $this, 'add_tab' ] );
22-
add_action( 'woocommerce_product_data_panels', [ $this, 'render_panel' ] );
23-
add_action( 'woocommerce_process_product_meta', [ $this, 'process_meta_box' ], 1, 2 );
21+
add_action( 'woocommerce_product_write_panel_tabs', array( $this, 'add_tab' ) );
22+
add_action( 'woocommerce_product_data_panels', array( $this, 'render_panel' ) );
23+
add_action( 'woocommerce_process_product_meta', array( $this, 'process_meta_box' ), 1, 2 );
2424

25-
add_action( 'wp_ajax_wc_gravityforms_get_form_data', [ $this, 'on_wc_gravityforms_get_form_data' ] );
25+
add_action( 'wp_ajax_wc_gravityforms_get_form_data', array( $this, 'on_wc_gravityforms_get_form_data' ) );
2626
}
2727

2828
public function on_admin_enqueue_scripts() {
2929
wp_enqueue_style( 'woocommerce_gravityforms_product_addons_css', plugins_url( basename( dirname( __DIR__ ) ) ) . '/assets/css/admin.css' );
3030

31-
$params = [
31+
$params = array(
3232
'nonce' => wp_create_nonce( 'wc_gravityforms_get_products' ),
3333
'text_edit_form' => __( 'Edit ', 'wc_gf_addons' ),
3434
'url_edit_form' => sprintf( '%s/admin.php?page=gf_edit_forms&id=FORMID', get_admin_url() ),
3535
'duplicate_form_notice' => __( 'The singular and the bulk form can not be the same form. Make a duplicate of your singular form if need be. ', 'wc_gf_addons' ),
3636
'product_id' => get_the_ID(),
37-
];
37+
);
38+
39+
// Add labels to the params
40+
$params['labels'] = array(
41+
'label_subtotal' => __( 'Subtotal', 'wc_gf_addons' ),
42+
'label_options' => __( 'Options', 'wc_gf_addons' ),
43+
'label_total' => __( 'Total', 'wc_gf_addons' ),
44+
);
45+
46+
$params['merge_tags'] = WC_GFPA_Merge_Tags::instance()->get_merge_tags();
47+
48+
// Add filter to modify the params
49+
$params = apply_filters( 'woocommerce_gravityforms_product_addons_js_params', $params );
3850

3951
wp_enqueue_script(
4052
'woocommerce_gravityforms_product_addons_js',
4153
plugins_url( basename( dirname( __DIR__ ) ) ) . '/assets/js/admin.js',
42-
[
54+
array(
4355
'jquery',
4456
'jquery-blockui',
45-
],
46-
wc_gfpa()->assets_version
57+
),
58+
wc_gfpa()->assets_version,
59+
true
60+
);
61+
62+
wp_enqueue_script(
63+
'wc-gfpa-admin-js',
64+
plugins_url( basename( dirname( __DIR__ ) ) ) . '/assets/js/gravityforms-product-addons-admin.js',
65+
array( 'jquery', 'jquery-blockui', 'woocommerce_gravityforms_product_addons_js' ),
66+
wc_gfpa()->assets_version,
67+
true
4768
);
4869

4970
wp_localize_script( 'woocommerce_gravityforms_product_addons_js', 'wc_gf_addons', $params );
@@ -53,15 +74,15 @@ public function on_admin_enqueue_scripts() {
5374
public function admin_install_notices() {
5475
if ( ! class_exists( 'RGForms' ) ) {
5576
?>
56-
<div id="message" class="updated woocommerce-error wc-connect">
57-
<div class="squeezer">
58-
<h4><?php _e( '<strong>Gravity Forms Not Found</strong> &#8211; The Gravity Forms Plugin is required to build and manage the forms for your products.', 'wc_gf_addons' ); ?></h4>
59-
<p class="submit">
60-
<a href="https://www.gravityforms.com/"
61-
class="button-primary"><?php _e( 'Get Gravity Forms', 'wc_gf_addons' ); ?></a>
62-
</p>
63-
</div>
64-
</div>
77+
<div id="message" class="updated woocommerce-error wc-connect">
78+
<div class="squeezer">
79+
<h4><?php _e( '<strong>Gravity Forms Not Found</strong> &#8211; The Gravity Forms Plugin is required to build and manage the forms for your products.', 'wc_gf_addons' ); ?></h4>
80+
<p class="submit">
81+
<a href="https://www.gravityforms.com/"
82+
class="button-primary"><?php _e( 'Get Gravity Forms', 'wc_gf_addons' ); ?></a>
83+
</p>
84+
</div>
85+
</div>
6586
<?php
6687
}
6788
}
@@ -71,11 +92,11 @@ public function on_admin_notices() {
7192
if ( is_admin() ) {
7293
if ( is_plugin_active( 'gravity-forms-duplicate-prevention/gravityforms-duplicateprevention.php' ) || is_plugin_active_for_network( 'gravity-forms-duplicate-prevention/gravityforms-duplicateprevention.php' ) ) {
7394
?>
74-
<div id="message" class="error woocommerce-error wc-connect">
75-
<div class="squeezer">
76-
<h4><?php printf( __( '<strong>Gravity Forms Duplicate Prevention Active</strong></h4><p>The <strong>Gravity Forms Product Addon Extension</strong> can not function properly if this additional plugin is active. Please <a href="%s">disable</a> it for proper functionality of the extension.</p>', 'wc_gf_addons' ), $this->na_action_link( 'gravity-forms-duplicate-prevention/gravityforms-duplicateprevention.php', 'deactivate' ) ); ?></h4>
77-
</div>
78-
</div>
95+
<div id="message" class="error woocommerce-error wc-connect">
96+
<div class="squeezer">
97+
<h4><?php printf( __( '<strong>Gravity Forms Duplicate Prevention Active</strong></h4><p>The <strong>Gravity Forms Product Addon Extension</strong> can not function properly if this additional plugin is active. Please <a href="%s">disable</a> it for proper functionality of the extension.</p>', 'wc_gf_addons' ), $this->na_action_link( 'gravity-forms-duplicate-prevention/gravityforms-duplicateprevention.php', 'deactivate' ) ); ?></h4>
98+
</div>
99+
</div>
79100
<?php
80101
}
81102
}
@@ -103,8 +124,8 @@ private function na_action_link( $plugin, $action = 'activate' ) {
103124

104125
public function add_tab() {
105126
?>
106-
<li class="gravityforms_addons_tab gravityforms_addons">
107-
<a href="#gravityforms_addons_data"><span><?php _e( 'Gravity Forms', 'wc_gf_addons' ); ?></span></a></li>
127+
<li class="gravityforms_addons_tab gravityforms_addons">
128+
<a href="#gravityforms_addons_data"><span><?php _e( 'Gravity Forms', 'wc_gf_addons' ); ?></span></a></li>
108129
<?php
109130
}
110131

@@ -126,7 +147,7 @@ public function process_meta_box( $post_id, $post ) {
126147

127148
$product = wc_get_product( $post );
128149

129-
$gravity_form_data = [
150+
$gravity_form_data = array(
130151
'id' => $_POST['gravityform-id'],
131152
'bulk_id' => isset( $_POST['gravityform-bulk-id'] ) ? $_POST['gravityform-bulk-id'] : 0,
132153
'display_title' => isset( $_POST['gravityform-display_title'] ) ? true : false,
@@ -155,7 +176,7 @@ public function process_meta_box( $post_id, $post ) {
155176
'structured_data_low_price' => isset( $_POST['gravityform-structured_data_low_price'] ) ? $_POST['gravityform-structured_data_low_price'] : '',
156177
'structured_data_high_price' => isset( $_POST['gravityform-structured_data_high_price'] ) ? $_POST['gravityform-structured_data_high_price'] : '',
157178
'structured_data_override_type' => isset( $_POST['gravityform-structured_data_override_type'] ) ? $_POST['gravityform-structured_data_override_type'] : 'append',
158-
];
179+
);
159180

160181
$gravity_form_data = apply_filters( 'woocommerce_gravityforms_before_save_metadata', $gravity_form_data, $product->get_id() );
161182
$product->update_meta_data( '_gravity_form_data', $gravity_form_data );
@@ -175,10 +196,10 @@ public function on_wc_gravityforms_get_form_data() {
175196
$form_id = isset( $_POST['form_id'] ) ? $_POST['form_id'] : 0;
176197
if ( empty( $form_id ) ) {
177198
wp_send_json_error(
178-
[
199+
array(
179200
'status' => 'error',
180201
'message' => __( 'No Form ID', 'wc_gf_addons' ),
181-
]
202+
)
182203
);
183204
die();
184205
}
@@ -196,10 +217,10 @@ public function on_wc_gravityforms_get_form_data() {
196217
}
197218

198219
$form = GFAPI::get_form( $form_id );
199-
$fields = GFAPI::get_fields_by_type( $form, [ 'quantity', 'number', 'singleproduct' ], false );
220+
$fields = GFAPI::get_fields_by_type( $form, array( 'quantity', 'number', 'singleproduct' ), false );
200221

201222
if ( $fields ) {
202-
$options = [];
223+
$options = array();
203224
foreach ( $fields as $field ) {
204225
if ( $field['disableQuantity'] !== true ) {
205226
$options[ $field['id'] ] = $field['label'];
@@ -208,13 +229,13 @@ public function on_wc_gravityforms_get_form_data() {
208229

209230
ob_start();
210231
woocommerce_wp_select(
211-
[
232+
array(
212233
'id' => 'gravityform-cart_quantity_field',
213234
'label' => __( 'Quantity Field', 'wc_gf_addons' ),
214235
'value' => $selected_field,
215236
'options' => $options,
216237
'description' => __( 'A field to use to control cart item quantity.', 'wc_gf_addons' ),
217-
]
238+
)
218239
);
219240

220241
$markup = ob_get_clean();
@@ -232,11 +253,11 @@ public function on_wc_gravityforms_get_form_data() {
232253
$markup .= '</select>';
233254
*/
234255

235-
$response = [
256+
$response = array(
236257
'status' => 'success',
237258
'message' => '',
238259
'markup' => $markup,
239-
];
260+
);
240261

241262
wp_send_json_success( $response );
242263
die();

admin/views/html-gravityforms-addons-panel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
'id' => 'gravityform-display_description',
5454
'label' => __( 'Display Description', 'wc_gf_addons' ),
5555
'value' => isset( $gravity_form_data['display_description'] ) && $gravity_form_data['display_description'] ? 'yes' : ''
56-
) );;
56+
) );
5757
?>
5858
</div>
5959

assets/css/frontend.css

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,44 @@
11
/* Helper for store front */
22
.single-product div.product form.cart .button.gform_button_select_files {
33
float:none;
4-
}
4+
}
5+
6+
form.cart .hidden-total {
7+
display: none !important;
8+
}
9+
10+
/* Product Totals */
11+
form.cart .product_totals {
12+
margin: 20px 0 40px 0;
13+
padding: 20px 0 0 0;
14+
border-top: 1px solid #eee;
15+
border-bottom: 1px solid #eee;
16+
overflow: hidden;
17+
}
18+
19+
form.cart .product_totals ul {
20+
list-style: none;
21+
margin: 0;
22+
padding: 0;
23+
}
24+
25+
form.cart .product_totals ul li {
26+
list-style: none;
27+
display: flex;
28+
justify-content: space-between;
29+
padding: 5px 0;
30+
}
31+
32+
form.cart .product_totals ul li.wcgfpa_grand_total {
33+
justify-content: flex-end;
34+
border-top: 1px solid #eee;
35+
padding: 10px 0;
36+
margin-top: 20px;
37+
}
38+
39+
form.cart .product_totals ul li.wcgfpa_grand_total p.price {
40+
margin: 0;
41+
}
42+
43+
44+
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Gravity Forms Product Add-Ons Admin
3+
*/
4+
5+
class GravityFormsProductAddonsAdmin {
6+
static instance;
7+
8+
static register(settings) {
9+
if (!this.instance) {
10+
this.instance = new GravityFormsProductAddonsAdmin(settings);
11+
}
12+
13+
return this.instance;
14+
}
15+
16+
constructor(settings) {
17+
this.settings = settings;
18+
this.observeGFormAvailability();
19+
}
20+
21+
observeGFormAvailability() {
22+
const observer = new MutationObserver((mutations, obs) => {
23+
if (window.gform) {
24+
console.log('gform is now available');
25+
this.init();
26+
obs.disconnect(); // Disconnect observer once gform is available
27+
}
28+
});
29+
30+
observer.observe(document, {
31+
childList: true,
32+
subtree: true
33+
});
34+
}
35+
36+
init() {
37+
gform.addFilter('gform_merge_tags', this.addMergeTags.bind(this));
38+
}
39+
40+
addMergeTags(mergeTags, elementId, hideAllFields, excludeFieldTypes, isPrepop, option) {
41+
42+
const groups = this.settings.merge_tags;
43+
// Loop though the groups and add the tags to the mergeTags
44+
for (const index in groups) {
45+
const key = groups[index].key;
46+
const tags = groups[index].tags;
47+
const label = groups[index].label;
48+
49+
const filtered_tags = tags.filter(tag => {
50+
const tag_allowed_on = tag.allowed_on || [];
51+
// If the elementId starts with field_default_value check if allowed on is 'field_default_value_*' or matches the elementId exactly.
52+
if (elementId.startsWith('field_default_value')) {
53+
return tag_allowed_on.includes('field_default_value_*') || tag_allowed_on.includes(elementId);
54+
}
55+
56+
// Otherwise check if the tag is allowed on the elementId or if it's allowed on all elements or if no allowed_on is set.
57+
if (tag_allowed_on.includes(elementId) || tag_allowed_on.includes('all') || tag_allowed_on.length === 0) {
58+
return true;
59+
}
60+
});
61+
62+
console.log('Filtered Tags: ', filtered_tags);
63+
64+
mergeTags[key] = {
65+
label: label,
66+
tags: filtered_tags
67+
};
68+
}
69+
70+
return mergeTags; // Return the modified mergeTags
71+
}
72+
}
73+
74+
// Instantiate the class to ensure the code is executed
75+
GravityFormsProductAddonsAdmin.register(wc_gf_addons);

0 commit comments

Comments
 (0)