Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active October 5, 2022 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/5a016069dccfe1414512810ef0f77a5f to your computer and use it in GitHub Desktop.
Save wpmudev-sls/5a016069dccfe1414512810ef0f77a5f to your computer and use it in GitHub Desktop.
[Forminator] - Support Barbar js
<?php
/**
* Plugin Name: [Forminator] - Support Barbar js
* Description: [Forminator] - Support Barbar js - 1147344407571863
* Author: Thobk @ WPMUDEV
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'plugins_loaded', 'wpmudev_forminator_barba_loader_func', 100 );
function wpmudev_forminator_barba_loader_func() {
// if you want to copy to your child's functions.php file please copy from here:
if ( defined('FORMINATOR_PRO') && class_exists( 'Forminator' ) ) {
// add_filter( 'the_content', 'wpmudev_barba_forminator_filter_content', 100 );
function wpmudev_barba_forminator_filter_content( $content ){
if( isset($_SERVER['HTTP_X_BARBA']) ){
global $wpmudev_fm_barba;
if( $wpmudev_fm_barba && false !== strpos( $content, 'forminator-module-' ) ){
global $wp_locale;
$datepicker_date_format = str_replace(
array(
'd', 'j', 'l', 'z', // Day.
'F', 'M', 'n', 'm', // Month.
'Y', 'y' // Year.
),
array(
'dd', 'd', 'DD', 'o',
'MM', 'M', 'm', 'mm',
'yy', 'y'
),
get_option( 'date_format' )
);
$datepicker_data = array(
'monthNames' => array_values( $wp_locale->month ),
'monthNamesShort' => array_values( $wp_locale->month_abbrev ),
'dayNames' => array_values( $wp_locale->weekday ),
'dayNamesShort' => array_values( $wp_locale->weekday_abbrev ),
'dayNamesMin' => array_values( $wp_locale->weekday_initial ),
'dateFormat' => $datepicker_date_format,
'firstDay' => absint( get_option( 'start_of_week' ) ),
'isRTL' => $wp_locale->is_rtl(),
);
$content .= "<div id='wpmudev-forminator-data-scripts' style='display:none' data-ids='". json_encode( $wpmudev_fm_barba['form_ids'] ) ."' data-forminator-front='". json_encode( forminator_localize_data() ) ."' data-datepicker_data='". json_encode( $datepicker_data ) ."' data-js='". json_encode( $wpmudev_fm_barba['scripts'] ) ."'></div>";
$wpmudev_fm_barba = null;
}
}
return $content;
}
function wpmudev_barba_forminator_common_data(){
static $rendered_datepicker_data;
if( $rendered_datepicker_data ){
return;
}
$rendered_datepicker_data = 1;
global $wp_locale;
$datepicker_date_format = str_replace(
array(
'd', 'j', 'l', 'z', // Day.
'F', 'M', 'n', 'm', // Month.
'Y', 'y' // Year.
),
array(
'dd', 'd', 'DD', 'o',
'MM', 'M', 'm', 'mm',
'yy', 'y'
),
get_option( 'date_format' )
);
$datepicker_data = array(
'monthNames' => array_values( $wp_locale->month ),
'monthNamesShort' => array_values( $wp_locale->month_abbrev ),
'dayNames' => array_values( $wp_locale->weekday ),
'dayNamesShort' => array_values( $wp_locale->weekday_abbrev ),
'dayNamesMin' => array_values( $wp_locale->weekday_initial ),
'dateFormat' => $datepicker_date_format,
'firstDay' => absint( get_option( 'start_of_week' ) ),
'isRTL' => $wp_locale->is_rtl(),
);
return " data-forminator-front='". json_encode( forminator_localize_data() ) ."' data-datepicker_data='". json_encode( $datepicker_data ) ."'";
}
add_filter( 'forminator_render_form_placeholder_markup', 'wpmudev_barba_forminator_render_form_placeholder_markup', 10, 6 );
function wpmudev_barba_forminator_get_script_href( $handle, $src, $ver ){
global $wp_scripts;
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && 0 === strpos( $src, $wp_scripts->content_url ) ) ) {
$src = $wp_scripts->base_url . $src;
}
if ( ! empty( $ver ) ) {
$src = add_query_arg( 'ver', $ver, $src );
}
/** This filter is documented in wp-includes/class.wp-scripts.php */
return esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
}
function wpmudev_barba_forminator_render_form_placeholder_markup( $html, $form_type, $render_id ){
if( isset($_SERVER['HTTP_X_BARBA']) ){
global $wpmudev_fm_barba, $wp_scripts, $wp_old_scripts, $wp_styles, $wp_old_styles;
$wp_old_scripts = $wp_scripts;
$scripts = array();
$wp_scripts = new WP_Scripts();
if( ! $wpmudev_fm_barba ){
$wp_old_styles = $wp_styles;
$wp_styles = new WP_Styles();
forminator_print_front_styles( FORMINATOR_VERSION );
forminator_print_front_scripts( FORMINATOR_VERSION );
ob_start();
$wp_styles->do_items();
$html .= ob_get_clean();
$wp_styles = $wp_old_styles;
$wp_old_styles = null;
}
if( ! $wpmudev_fm_barba || ! in_array( $form_type, $wpmudev_fm_barba['form_types'] ) ){
$wpmudev_fm_barba['form_types'][] = $form_type;
if( $form_type === 'custom-form' ){
wp_enqueue_script( 'jquery-ui-datepicker' );
}elseif( 'polls' === $form_type ){
wp_enqueue_script( 'google-charts', 'https://www.gstatic.com/charts/loader.js', array( 'jquery' ), '1.0', false );
}
}
if( $wp_scripts->queue ){
foreach( $wp_scripts->queue as $handle ){
if( $handle === 'wp-color-picker-alpha' ) continue;
if( isset( $wp_scripts->registered[ $handle ] ) && ! isset( $wpmudev_fm_barba['scripts'][ $handle ] ) ){
$obj = $wp_scripts->registered[ $handle ];
if( ! empty( $obj->deps ) ){
foreach( $obj->deps as $dep_handle ){
if( isset( $wp_scripts->registered[ $dep_handle ] ) && ! isset( $wpmudev_fm_barba['scripts'][ $dep_handle ] ) && ( $src = $wp_scripts->registered[ $dep_handle ]->src ) ){
// $wpmudev_fm_barba['scripts'][] = wpmudev_barba_forminator_get_script_href( $dep_handle, $src, $wp_scripts->registered[ $dep_handle ]->ver );
$scripts[ $dep_handle ] = wpmudev_barba_forminator_get_script_href( $dep_handle, $src, $wp_scripts->registered[ $dep_handle ]->ver );
$wpmudev_fm_barba['scripts'][ $dep_handle ] = 1;
}
}
}
$scripts[ $handle ] = wpmudev_barba_forminator_get_script_href( $handle, $obj->src, $obj->ver );
// $wpmudev_fm_barba['scripts'][] = wpmudev_barba_forminator_get_script_href( $handle, $obj->src, $obj->ver );
$wpmudev_fm_barba['scripts'][ $handle ] = 1;
}
}
}
$wp_scripts = $wp_old_scripts;
$wp_old_scripts = null;
// get form_id
if( preg_match('/forminator-module-([\d]+)/', $html, $form_ids) ){
if( $form_ids ){
$id = $form_ids[1];
$render_id = 0;
if( preg_match('/data-forminator-render="([\d]+)"/', $html, $render_ids) ){
$render_id = $render_ids[1];
}
$args = array(
'action' => "forminator_load_cform",
'type' => ( 'custom-form' === $form_type ) ? 'forminator_forms' : "forminator_{$form_type}",
'id' => (int) $id,
'render_id' => (int) $render_id,
'is_preview' => false,
'preview_data' => [],
'last_submit_data' => [],
'extra' => array(
'_wp_http_referer' => isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '#barba',
'page_id' => get_the_ID(),
)
);
// $wpmudev_fm_barba['form_ids'][$form_ids[0]] = $args;
$comon_data = wpmudev_barba_forminator_common_data();
$html .= "<div class='wpmudev-forminator-data-script' style='display:none' data-type='". $form_type ."' data-args='". json_encode( $args ) ."' data-js='". json_encode( array_values( $scripts ) ) ."'". $comon_data ."></div>";
$scripts = null;
}
}
}
return $html;
}
add_action( 'wp_footer', 'wpmudev_barba_load_forminator', 21 );
function wpmudev_barba_load_forminator(){
ob_start();
?>
<script>
(function($){
$(function(){
function wpmudev_barba_load_forminator(){
if( window.barba ){
function wpmudev_forminator_loader({next}){
var _forminator_scripts = $(next.container).find('.wpmudev-forminator-data-script');
if( _forminator_scripts.length ){
_forminator_scripts.each( function(){
let js_data = $(this).data();
if( js_data ){
window.ForminatorFront = window.ForminatorFront || js_data.forminatorFront;
window.datepickerLang = window.datepickerLang || js_data.datepicker_data;
forminator_load_js( js_data );
$(this).remove();
}
});
function forminator_load_js( js_data ){
if( ! js_data.js.length ){
return $(next.container).find('#forminator-module-'+ js_data.args['id'] +'[data-forminator-render="'+ js_data.args['render_id'] +'"]').forminatorLoader( js_data.args );
}
let jslib = js_data.js.shift();
if( jslib ){
$.getScript(jslib, function(){
if( ! js_data.js.length ){
// $.each(js_data.ids, function(form_id, js_data.args){
$(next.container).find('#forminator-module-'+ js_data.args['id'] +'[data-forminator-render="'+ js_data.args['render_id'] +'"]').forminatorLoader( js_data.args );
// });
}else{
forminator_load_js( js_data );
}
});
}
}
}
}
$.each(barba.transitions.store.all, function(i, events){
if( events ){
$.each(['afterLeave', 'beforeEnter', 'enter', 'afterEnter', 'after'], function( event_key, event ){
if( ! ( event in events ) ){
barba.transitions.store.all[i][event] = wpmudev_forminator_loader;
return false;
}
});
}
});
}
}
if( window.barba && window.barba.transitions ){
wpmudev_barba_load_forminator();
}else{
$(window).on('load', function(){
wpmudev_barba_load_forminator();
});
}
});
})(window.jQuery)
</script>
<?php
echo ob_get_clean();
}
}
// and end here!
}
@treforcoster
Copy link

Hi there

This doesn't seem to work for me.
Is there anything I may be missing?

@svsdesign
Copy link

Hello,

Dito - doesn't seem to work for me;

Forminator Version 1.18.2
Barba V2

Any advice?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment