| 1 |
<?php |
| 2 |
|
| 3 |
class HappyForms_Form_Assets { |
| 4 |
|
| 5 |
private static $instance; |
| 6 |
private static $hooked = false; |
| 7 |
|
| 8 |
const MODE_NONE = 0; |
| 9 |
const MODE_ADMIN = 1; |
| 10 |
const MODE_BLOCK = 2; |
| 11 |
const MODE_CUSTOMIZER = 4; |
| 12 |
const MODE_COMPLETE = 8; |
| 13 |
|
| 14 |
private $forms = array(); |
| 15 |
|
| 16 |
public static function instance() { |
| 17 |
if ( is_null( self::$instance ) ) { |
| 18 |
self::$instance = new self(); |
| 19 |
} |
| 20 |
|
| 21 |
self::$instance->hook(); |
| 22 |
|
| 23 |
return self::$instance; |
| 24 |
} |
| 25 |
|
| 26 |
public function hook() { |
| 27 |
if ( self::$hooked ) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
add_filter( 'happyforms_frontend_dependencies', array( $this, 'frontend_dependencies' ) ); |
| 32 |
add_action( 'wp_print_footer_scripts', array( $this, 'wp_print_footer_scripts' ), 0 ); |
| 33 |
add_action( 'elementor/theme/after_do_popup', array( $this, 'elementor_popup_compatibility' ) ); |
| 34 |
} |
| 35 |
|
| 36 |
public function output_frontend_styles( $form ) { |
| 37 |
$output = apply_filters( 'happyforms_enqueue_style', true ); |
| 38 |
|
| 39 |
if ( ! $output ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
happyforms_the_form_styles( $form ); |
| 44 |
happyforms_additional_css( $form ); |
| 45 |
} |
| 46 |
|
| 47 |
public function output_admin_styles( $form ) { |
| 48 |
?> |
| 49 |
<link rel="stylesheet" type="text/css" href="<?php echo happyforms_get_plugin_url() . 'core/assets/css/no-interaction.css'; ?>"> |
| 50 |
<?php |
| 51 |
} |
| 52 |
|
| 53 |
private function get_frontend_script_url() { |
| 54 |
$script_url = happyforms_get_plugin_url() . 'bundles/js/frontend.js'; |
| 55 |
|
| 56 |
if ( ! happyforms_concatenate_scripts() ) { |
| 57 |
$script_url = happyforms_get_plugin_url() . 'inc/assets/js/frontend.js'; |
| 58 |
} |
| 59 |
|
| 60 |
return $script_url; |
| 61 |
} |
| 62 |
|
| 63 |
private function get_frontend_script_dependencies( $forms ) { |
| 64 |
wp_register_script( 'happyforms-settings', '', array(), happyforms_get_version(), true ); |
| 65 |
|
| 66 |
$settings = apply_filters( 'happyforms_frontend_settings', array( |
| 67 |
'ajaxUrl' => admin_url( 'admin-ajax.php' ) |
| 68 |
), $forms ); |
| 69 |
|
| 70 |
wp_localize_script( 'happyforms-settings', '_happyFormsSettings', $settings ); |
| 71 |
|
| 72 |
$dependencies = array( 'jquery', 'happyforms-settings' ); |
| 73 |
|
| 74 |
if ( ! happyforms_concatenate_scripts() ) { |
| 75 |
$dependencies = apply_filters( |
| 76 |
'happyforms_frontend_dependencies', |
| 77 |
$dependencies, $forms |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
return $dependencies; |
| 82 |
} |
| 83 |
|
| 84 |
public function output_frontend_scripts( $form ) { |
| 85 |
if ( wp_doing_ajax() ) { |
| 86 |
global $wp_scripts; |
| 87 |
|
| 88 |
add_filter( 'script_loader_tag', function( $tag, $handle, $src ) { |
| 89 |
return ''; |
| 90 |
}, 10, 3 ); |
| 91 |
|
| 92 |
$dependencies = $this->get_frontend_script_dependencies( [ $form ] ); |
| 93 |
$script_url = $this->get_frontend_script_url(); |
| 94 |
|
| 95 |
wp_register_script( |
| 96 |
'happyforms-frontend', |
| 97 |
$script_url, |
| 98 |
$dependencies, happyforms_get_version(), true |
| 99 |
); |
| 100 |
|
| 101 |
wp_scripts()->all_deps( 'happyforms-frontend' ); |
| 102 |
|
| 103 |
$queue = array(); |
| 104 |
|
| 105 |
foreach ( wp_scripts()->to_do as $handle ) { |
| 106 |
$dep = wp_scripts()->registered[ $handle ]; |
| 107 |
|
| 108 |
if ( $dep->src ) { |
| 109 |
$queue[] = array( |
| 110 |
'id' => $handle, |
| 111 |
'src' => $dep->src |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
wp_scripts()->print_extra_script( $handle ); |
| 116 |
} |
| 117 |
|
| 118 |
wp_scripts()->print_extra_script( 'happyforms-frontend' ); |
| 119 |
?> |
| 120 |
<script type="text/javascript"> |
| 121 |
var queue = <?php echo json_encode( $queue ); ?>; |
| 122 |
var loaded = 0; |
| 123 |
|
| 124 |
queue = queue.filter( function( item ) { |
| 125 |
if ( document.querySelector( 'script[id="' + item.id + '-js"]' ) ) { |
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
return true; |
| 130 |
} ); |
| 131 |
|
| 132 |
function enqueueNextScript() { |
| 133 |
if ( queue.length === 0 ) { |
| 134 |
jQuery( '.happyforms-form' ).happyForm(); |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
var item = queue.shift(); |
| 139 |
var script = document.createElement( 'script' ); |
| 140 |
script.id = item.id + '-js'; |
| 141 |
script.src = item.src; |
| 142 |
|
| 143 |
script.addEventListener( 'load', function() { |
| 144 |
enqueueNextScript(); |
| 145 |
} ); |
| 146 |
|
| 147 |
document.body.appendChild( script ); |
| 148 |
} |
| 149 |
|
| 150 |
enqueueNextScript(); |
| 151 |
</script> |
| 152 |
<?php |
| 153 |
|
| 154 |
do_action( 'happyforms_print_scripts', [ $form ] ); |
| 155 |
} else { |
| 156 |
$this->forms[] = $form; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
public function output( $form, $mode = self::MODE_COMPLETE ) { |
| 161 |
switch( $mode ) { |
| 162 |
case self::MODE_NONE: |
| 163 |
break; |
| 164 |
case self::MODE_ADMIN: |
| 165 |
case self::MODE_BLOCK: |
| 166 |
$this->output_frontend_styles( $form ); |
| 167 |
$this->output_admin_styles( $form ); |
| 168 |
break; |
| 169 |
case self::MODE_CUSTOMIZER: |
| 170 |
$this->output_frontend_styles( $form ); |
| 171 |
$this->output_admin_styles( $form ); |
| 172 |
$this->output_frontend_scripts( $form ); |
| 173 |
break; |
| 174 |
case self::MODE_COMPLETE: |
| 175 |
$this->output_frontend_styles( $form ); |
| 176 |
$this->output_frontend_scripts( $form ); |
| 177 |
break; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
public function print_frontend_styles( $form ) { |
| 182 |
if ( ! happyforms_concatenate_styles() ) { |
| 183 |
wp_register_style( |
| 184 |
'happyforms-color', |
| 185 |
happyforms_get_frontend_stylesheet_url( 'color.css' ), |
| 186 |
array(), happyforms_get_version() |
| 187 |
); |
| 188 |
|
| 189 |
$dependencies = apply_filters( |
| 190 |
'happyforms_style_dependencies', |
| 191 |
array( 'happyforms-color' ), [ $form ] |
| 192 |
); |
| 193 |
|
| 194 |
wp_register_style( |
| 195 |
'happyforms-layout', |
| 196 |
happyforms_get_frontend_stylesheet_url( 'layout.css' ), |
| 197 |
$dependencies, happyforms_get_version() |
| 198 |
); |
| 199 |
|
| 200 |
$dependencies[] = 'happyforms-layout'; |
| 201 |
|
| 202 |
global $wp_styles; |
| 203 |
|
| 204 |
foreach( $dependencies as $dependency ) { |
| 205 |
if ( isset( $wp_styles->registered[$dependency] ) ) { |
| 206 |
$stylesheet_url = $wp_styles->registered[$dependency]->src; |
| 207 |
?><link rel="stylesheet" property="stylesheet" href="<?php echo $stylesheet_url; ?>" /><?php |
| 208 |
} |
| 209 |
} |
| 210 |
} else { |
| 211 |
?> |
| 212 |
<link rel="stylesheet" property="stylesheet" href="<?php echo happyforms_get_plugin_url() . 'bundles/css/frontend.css'; ?>" /> |
| 213 |
<?php |
| 214 |
} |
| 215 |
|
| 216 |
do_action( 'happyforms_print_frontend_styles', $form ); |
| 217 |
} |
| 218 |
|
| 219 |
public function frontend_dependencies( $deps ) { |
| 220 |
wp_register_script( |
| 221 |
'happyforms-md5', |
| 222 |
happyforms_get_plugin_url() . 'core/assets/js/lib/md5.min.js', |
| 223 |
array(), happyforms_get_version(), true |
| 224 |
); |
| 225 |
|
| 226 |
wp_register_script( |
| 227 |
'happyforms-antispam', |
| 228 |
happyforms_get_plugin_url() . 'core/assets/js/frontend/antispam.js', |
| 229 |
array( 'happyforms-md5' ), happyforms_get_version(), true |
| 230 |
); |
| 231 |
|
| 232 |
$deps[] = 'happyforms-antispam'; |
| 233 |
|
| 234 |
return $deps; |
| 235 |
} |
| 236 |
|
| 237 |
public function wp_print_footer_scripts() { |
| 238 |
if ( empty( $this->forms ) ) { |
| 239 |
return; |
| 240 |
} |
| 241 |
|
| 242 |
$dependencies = $this->get_frontend_script_dependencies( $this->forms ); |
| 243 |
$script_url = $this->get_frontend_script_url(); |
| 244 |
|
| 245 |
wp_enqueue_script( |
| 246 |
'happyforms-frontend', |
| 247 |
$script_url, |
| 248 |
$dependencies, happyforms_get_version(), true |
| 249 |
); |
| 250 |
|
| 251 |
do_action( 'happyforms_print_scripts', $this->forms ); |
| 252 |
} |
| 253 |
|
| 254 |
public function elementor_popup_compatibility() { |
| 255 |
?> |
| 256 |
<script type="text/javascript"> |
| 257 |
( function( $ ) { |
| 258 |
$( function() { |
| 259 |
$( document ).on( 'elementor/popup/show', () => { |
| 260 |
if ( $.fn.happyForm ) { |
| 261 |
$( '.happyforms-form' ).happyForm(); |
| 262 |
} |
| 263 |
} ); |
| 264 |
} ); |
| 265 |
} )( jQuery ); |
| 266 |
</script> |
| 267 |
<?php |
| 268 |
} |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
if ( ! function_exists( 'happyforms_get_form_assets' ) ): |
| 273 |
|
| 274 |
function happyforms_get_form_assets() { |
| 275 |
return HappyForms_Form_Assets::instance(); |
| 276 |
} |
| 277 |
|
| 278 |
endif; |
| 279 |
|
| 280 |
happyforms_get_form_assets(); |
| 281 |
|