Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements in FBT location #353

Merged
merged 4 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions admin/classes/class-merchant-admin-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -726,9 +726,53 @@ public static function disabled_field( $settings, $value, $module_id = '') {
public static function replace_field( $settings, $value, $search, $replace, $module_id = '') {
ob_start();
self::field( $settings, $value, $module_id );
$field = ob_get_clean();
$field_html = ob_get_clean();

echo wp_kses( str_replace( $search, $replace, $field ), merchant_kses_allowed_tags( array( 'all' ) ) );
// Replace attributes in the field
$field = str_replace( $search, $replace, $field_html );

// Process specific field types
if ( isset( $settings['type'] ) && $settings['type'] === 'hook_select' ) {
$field = self::update_field_attributes( $field, array(
'select' => '[hook_name]',
'input' => '[hook_priority]',
) );
}

echo wp_kses( $field, merchant_kses_allowed_tags( array( 'all' ) ) );
}

/**
* Update attributes for specific tags in the field HTML.
*
* @param string $field_html The field's HTML.
* @param array $tag_updates An associative array where keys are tag names
* and values are strings to append to the `name` attribute.
*
* @return string Updated field HTML.
*/
private static function update_field_attributes( $field, $updates ) {
if ( ! class_exists( 'WP_HTML_Tag_Processor' ) ) {
return $field;
}

foreach ( $updates as $tag => $append ) {
$processor = new WP_HTML_Tag_Processor( $field ); // Reset processor for each tag type

while ( $processor->next_tag( $tag ) ) {
$current_name = $processor->get_attribute( 'name' );

// Append the specified string if not already present
if ( $current_name && ! str_ends_with( $current_name, $append ) ) {
$processor->set_attribute( 'name', $current_name . $append );
}
}

// Update the field with the latest processed HTML
$field = $processor->get_updated_html();
}

return $field;
}

/**
Expand Down Expand Up @@ -2241,7 +2285,7 @@ public static function fields_group( $settings, $value, $module_id = '', $inside
if ( $inside_flexible ) {
static::replace_field(
$field,
isset( $args['value'][$settings['id']][ $field['id'] ] ) ? $args['value'][$settings['id']][ $field['id'] ] : ( $field['default'] ?? '' ),
$args['value'][ $settings['id'] ][ $field['id'] ] ?? ( $field['default'] ?? '' ),
"name=\"merchant[{$field['id']}]",
"name=\"merchant[{$args['id']}][{$args['option_key']}][{$settings['id']}][{$field['id']}]\" data-name=\"merchant[{$args['id']}][0][{$settings['id']}][{$field['id']}]",
$module_id
Expand Down
14 changes: 11 additions & 3 deletions assets/js/src/modules/frequently-bought-together/admin/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
initPreview();
});

$( document ).on( 'change', '.merchant-module-page-setting-field-select select', function () {
const selectedHook = $( this ).val();

let priority = merchant?.fbt_object?.hooks[selectedHook] || 10;

$( this ).closest( '.merchant-module-page-setting-field-hook_select' ).find( '.merchant-module-page-setting-field-number input' ).val( priority );
} );

function initPreview() {
let layout = $('.merchant-flexible-content-control.frequently-bought-together-style').find('.layout.active'),
titleTextColor = layout.find('.merchant-field-title input').val(),
Expand Down Expand Up @@ -49,11 +57,11 @@
$('.merchant-cart-preview .my-cart .cart-table .cart-item .product .product-info .upsell-product .upsell-info p').text(cartSaveLabel.replace('{amount}', '10%'));
$('.merchant-cart-preview .my-cart .cart-table .cart-item .product .product-info .upsell-product .upsell-info .add-to-cart').text(cartBundleButtonText);
$('.merchant-checkout-preview .offer-title').text(checkoutTitle.replace('{offer_quantity}', '3'));
$('.merchant-checkout-preview .product-details .product-info p').text(checkoutDiscountText.replace('{discount}', '10%').replace('{fbt_products}', fbt_object.product_names));
$('.merchant-checkout-preview .product-details .product-info p').text(checkoutDiscountText.replace('{discount}', '10%').replace('{fbt_products}', merchant?.fbt_object.product_names));
$('.merchant-checkout-preview .product-details .product-info .add-to-order').text(checkoutButtonText);
$('.merchant-thank-you-preview .offer-title').text(thankYouTitle.replace('{offer_quantity}', '3').replace('{discount}', '10%'));
$('.merchant-thank-you-preview .product-details .product-info p').text(thankYouDiscountText.replace('{discount}', '10%').replace('{fbt_products}', fbt_object.product_names));
$('.merchant-thank-you-preview .merchant-tooltip .tooltip-text').html(thankYouBonusTipText.replace('{discount}', '10%').replace('{fbt_products}', fbt_object.product_names));
$('.merchant-thank-you-preview .product-details .product-info p').text(thankYouDiscountText.replace('{discount}', '10%').replace('{fbt_products}', merchant?.fbt_object.product_names));
$('.merchant-thank-you-preview .merchant-tooltip .tooltip-text').html(thankYouBonusTipText.replace('{discount}', '10%').replace('{fbt_products}', merchant?.fbt_object.product_names));
$('.merchant-thank-you-preview .product-details .product-info .add-to-order').text(thankYouButtonText);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

.merchant-frequently-bought-together {
margin: 30px 0;
clear: both;

&.after-tabs {
padding-bottom: 40px;
Expand Down
14 changes: 11 additions & 3 deletions inc/modules/frequently-bought-together/admin/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,22 @@
'fields' => array(
array(
'id' => 'single_product_placement',
'type' => 'radio',
'title' => esc_html__( 'Placement on product page', 'merchant' ),
'type' => 'hook_select',
'title' => esc_html__('Placement on product page', 'merchant'),
'options' => array(
'after-summary' => esc_html__( 'After Product Summary', 'merchant' ),
'after-tabs' => esc_html__( 'After Product Tabs', 'merchant' ),
'bottom' => esc_html__( 'At the Bottom', 'merchant' ),
),
'default' => 'after-summary',
'min' => -999,
'max' => 999,
'step' => 1,
'unit' => '',
'order' => true,
'default' => array(
'hook_name' => 'after-summary',
'hook_priority' => 10,
),
),

// text formatting settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ public function __construct() {
// Enqueue admin styles.
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) );

// Enqueue admin scripts.
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_js' ) );

// Localize Script.
add_filter( 'merchant_admin_localize_script', array( $this, 'localize_script' ) );

// Admin preview box.
add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 );
}
Expand Down Expand Up @@ -138,16 +144,37 @@ public function admin_enqueue_css() {
if ( parent::is_module_settings_page() ) {
wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/frequently-bought-together.min.css', array(), MERCHANT_VERSION );
wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION );

}
}

/**
* Admin enqueue scripts.
*
* @return void
*/
public function admin_enqueue_js() {
if ( $this->is_module_settings_page() ) {
wp_enqueue_script( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/admin/preview.min.js', array( 'jquery' ),
MERCHANT_VERSION, true );
wp_localize_script(
'merchant-admin-' . self::MODULE_ID,
'fbt_object',
array(
'product_names' => esc_html__( 'Product 1, Product 2, Product 3', 'merchant' ),
)
);
}

}
}

/**
* Localize Script.
*/
public function localize_script( $script ) {
$script['fbt_object'] = array(
'product_names' => esc_html__( 'Product 1, Product 2, Product 3', 'merchant' ),
'hooks' => array(
'after-summary' => 10,
'after-tabs' => 15,
'bottom' => 20,
),
);

return $script;
}

/**
Expand Down