| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: CF7 HTML Editor |
| 4 |
* Plugin URI: https://wordpress.org/cf7-coder |
| 5 |
* Description: Add HTML editor to Contact Form 7. |
| 6 |
* Version: 1.0 |
| 7 |
* Author: Wow-Company |
| 8 |
* Author URI: https://wow-estore.com/ |
| 9 |
* License: GPL-2.0+ |
| 10 |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt |
| 11 |
* Text Domain: cf7-coder |
| 12 |
* Domain Path: /languages |
| 13 |
* |
| 14 |
* PHP version 7.4 |
| 15 |
* |
| 16 |
* @category Wordpress_Plugin |
| 17 |
* @package Wow_Plugin |
| 18 |
* @copyright 2021 Wow-Company |
| 19 |
* @license GNU Public License |
| 20 |
* @version 0.1 |
| 21 |
*/ |
| 22 |
|
| 23 |
if ( ! defined( "ABSPATH" ) ) { |
| 24 |
exit(); |
| 25 |
} |
| 26 |
|
| 27 |
class CF7_Coder { |
| 28 |
public function __construct() { |
| 29 |
add_action( "admin_enqueue_scripts", [ $this, "style_script" ] ); |
| 30 |
add_action( 'wpcf7_admin_misc_pub_section', [ $this, 'wpcf7_add_test_mode' ] ); |
| 31 |
add_filter( 'wpcf7_contact_form_properties', [ $this, 'wpcf7_add_properties' ] ); |
| 32 |
add_action( 'wpcf7_save_contact_form', [ $this, 'wpcf7_save' ] ); |
| 33 |
add_filter( 'do_shortcode_tag', [ $this, 'wpcf7_frontend' ], 10, 4 ); |
| 34 |
|
| 35 |
// Conditional loading of CF7 assets |
| 36 |
if ( get_option( 'wpcf7_load_assets_shortcode' ) ) { |
| 37 |
add_action( 'wp_enqueue_scripts', [ $this, 'dequeue_cf7_assets' ], 100 ); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
// Include script and style in CF7 page |
| 42 |
public function style_script( $hook ) { |
| 43 |
$page_new = 'contact_page_wpcf7-new'; |
| 44 |
$page = 'toplevel_page_wpcf7'; |
| 45 |
|
| 46 |
|
| 47 |
if ( $page === $hook || $page_new === $hook ) { |
| 48 |
$version = '1.0'; |
| 49 |
|
| 50 |
wp_enqueue_script( 'code-editor' ); |
| 51 |
wp_enqueue_style( 'code-editor' ); |
| 52 |
wp_enqueue_script( 'htmlhint' ); |
| 53 |
|
| 54 |
|
| 55 |
$url_style = plugin_dir_url( __FILE__ ) . 'assets/style.css'; |
| 56 |
wp_enqueue_style( "coder-wpcf7", $url_style, array(), $version ); |
| 57 |
|
| 58 |
$url_matirial = plugin_dir_url( __FILE__ ) . 'assets/material.css'; |
| 59 |
wp_enqueue_style( "coder-wpcf7-matirial", $url_matirial, array(), $version ); |
| 60 |
|
| 61 |
$url_script = plugin_dir_url( __FILE__ ) . 'assets/script.js'; |
| 62 |
wp_enqueue_script( "coder-wpcf7", $url_script, [ "jquery" ], $version, false ); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
// Add checkbox 'Test Mode' in sidebar |
| 68 |
public function wpcf7_add_test_mode() { |
| 69 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verified by CF7 |
| 70 |
$post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : '-1'; |
| 71 |
$checked = get_post_meta( $post_id, '_wpcf7_test_mode', true ); |
| 72 |
?> |
| 73 |
<div class="misc-pub-section"> |
| 74 |
<label class="wpcf7-test-mode"> |
| 75 |
<input value="1" type="checkbox" name="wpcf7-test-mode" <?php checked( $checked ); ?>> |
| 76 |
<?php esc_html_e( 'Test Mode', 'cf7-coder' ); ?> |
| 77 |
<sup class="has-tooltip" data-tooltip="The Form will only be displayed for administrators.">ℹ</sup> |
| 78 |
</label> |
| 79 |
</div> |
| 80 |
<?php |
| 81 |
$checked = get_post_meta( $post_id, '_wpcf7_remove_auto_tags', true ); |
| 82 |
?> |
| 83 |
<div class="misc-pub-section"> |
| 84 |
<label class="wpcf7-remove-auto-tags"> |
| 85 |
<input value="1" type="checkbox" name="wpcf7-remove-auto-tags" <?php checked( $checked ); ?>> |
| 86 |
<?php esc_html_e( 'Remove Auto tags p and br', 'cf7-coder' ); ?> |
| 87 |
</label> |
| 88 |
</div> |
| 89 |
<?php |
| 90 |
$checked = get_post_meta( $post_id, '_wpcf7_codemiror_dark', true ); |
| 91 |
?> |
| 92 |
<div class="misc-pub-section"> |
| 93 |
<label class="wpcf7-remove-auto-tags"> |
| 94 |
<input value="1" type="checkbox" id="wpcf7_codemiror_dark" |
| 95 |
name="wpcf7_codemiror_dark" <?php checked( $checked ); ?>> |
| 96 |
<?php esc_html_e( 'Enable dark theme (Material)', 'cf7-coder' ); ?> |
| 97 |
</label> |
| 98 |
</div> |
| 99 |
<div class="misc-pub-section"> |
| 100 |
<label class="wpcf7-load-assets"> |
| 101 |
<input value="1" type="checkbox" name="wpcf7_load_assets_shortcode" <?php checked( get_option( 'wpcf7_load_assets_shortcode' ) ); ?>> |
| 102 |
<?php esc_html_e( 'Load scripts only on pages with shortcode', 'cf7-coder' ); ?> |
| 103 |
<sup class="has-tooltip on-left" data-tooltip="CF7 scripts and styles will only be loaded on pages containing the contact form shortcode.">ℹ</sup> |
| 104 |
</label> |
| 105 |
</div> |
| 106 |
<?php |
| 107 |
$redirect_enabled = get_post_meta( $post_id, '_wpcf7_redirect_enabled', true ); |
| 108 |
$redirect_url = get_post_meta( $post_id, '_wpcf7_redirect_url', true ); |
| 109 |
$redirect_acf_field = get_post_meta( $post_id, '_wpcf7_redirect_acf_field', true ); |
| 110 |
?> |
| 111 |
<div class="misc-pub-section"> |
| 112 |
<label class="wpcf7-redirect"> |
| 113 |
<input value="1" type="checkbox" id="wpcf7_redirect_enabled" name="wpcf7-redirect-enabled" <?php checked( $redirect_enabled ); ?>> |
| 114 |
<?php esc_html_e( 'Redirect after submit', 'cf7-coder' ); ?> |
| 115 |
<sup class="has-tooltip on-left" data-tooltip="Redirect to a URL after successful form submission.">ℹ</sup> |
| 116 |
</label> |
| 117 |
<div id="wpcf7-redirect-url-wrap" style="margin-top: 8px; <?php echo $redirect_enabled ? '' : 'display: none;'; ?>"> |
| 118 |
<input type="url" name="wpcf7-redirect-url" id="wpcf7-redirect-url" |
| 119 |
value="<?php echo esc_attr( $redirect_url ); ?>" |
| 120 |
placeholder="https://example.com/thank-you" style="width: 100%;"> |
| 121 |
<?php if ( class_exists( 'ACF' ) ) : ?> |
| 122 |
<?php $acf_field = get_post_meta( $post_id, '_wpcf7_redirect_acf_field', true ); ?> |
| 123 |
<input type="text" name="wpcf7-redirect-acf-field" id="wpcf7-redirect-acf-field" |
| 124 |
value="<?php echo esc_attr( $acf_field ); ?>" |
| 125 |
placeholder="ACF field name" style="width: 100%; margin-top: 5px;"> |
| 126 |
<small style="color: #666;">ACF field from current page (overrides URL above)</small> |
| 127 |
<?php endif; ?> |
| 128 |
<?php $redirect_new_tab = get_post_meta( $post_id, '_wpcf7_redirect_new_tab', true ); ?> |
| 129 |
<?php $redirect_download = get_post_meta( $post_id, '_wpcf7_redirect_download', true ); ?> |
| 130 |
<label style="display: block; margin-top: 8px;"> |
| 131 |
<input type="checkbox" name="wpcf7-redirect-new-tab" value="1" <?php checked( $redirect_new_tab ); ?>> |
| 132 |
<?php esc_html_e( 'Open in new tab', 'cf7-coder' ); ?> |
| 133 |
</label> |
| 134 |
<label style="display: block; margin-top: 4px;"> |
| 135 |
<input type="checkbox" name="wpcf7-redirect-download" value="1" <?php checked( $redirect_download ); ?>> |
| 136 |
<?php esc_html_e( 'Force download', 'cf7-coder' ); ?> |
| 137 |
</label> |
| 138 |
</div> |
| 139 |
</div> |
| 140 |
<?php |
| 141 |
$hide_form = get_post_meta( $post_id, '_wpcf7_hide_form', true ); |
| 142 |
?> |
| 143 |
<div class="misc-pub-section"> |
| 144 |
<label class="wpcf7-hide-form"> |
| 145 |
<input value="1" type="checkbox" name="wpcf7-hide-form" <?php checked( $hide_form ); ?>> |
| 146 |
<?php esc_html_e( 'Hide form after submit', 'cf7-coder' ); ?> |
| 147 |
<sup class="has-tooltip on-left" data-tooltip="Hide the form after successful submission, show only the success message.">ℹ</sup> |
| 148 |
</label> |
| 149 |
</div> |
| 150 |
<?php |
| 151 |
$remove_refill = get_post_meta( $post_id, '_wpcf7_remove_refill', true ); |
| 152 |
?> |
| 153 |
<div class="misc-pub-section"> |
| 154 |
<label class="wpcf7-remove-refill"> |
| 155 |
<input value="1" type="checkbox" name="wpcf7-remove-refill" <?php checked( $remove_refill ); ?>> |
| 156 |
<?php esc_html_e( 'Remove refill', 'cf7-coder' ); ?> |
| 157 |
<sup class="has-tooltip on-left" data-tooltip="Clear form fields after validation error instead of keeping entered values.">ℹ</sup> |
| 158 |
</label> |
| 159 |
</div> |
| 160 |
<?php |
| 161 |
$disable_submit = get_post_meta( $post_id, '_wpcf7_disable_submit', true ); |
| 162 |
?> |
| 163 |
<div class="misc-pub-section"> |
| 164 |
<label class="wpcf7-disable-submit"> |
| 165 |
<input value="1" type="checkbox" name="wpcf7-disable-submit" <?php checked( $disable_submit ); ?>> |
| 166 |
<?php esc_html_e( 'Disable submit button while sending', 'cf7-coder' ); ?> |
| 167 |
<sup class="has-tooltip on-left" data-tooltip="Disable submit button during form submission to prevent double submissions.">ℹ</sup> |
| 168 |
</label> |
| 169 |
</div> |
| 170 |
<?php |
| 171 |
$prefill_url = get_post_meta( $post_id, '_wpcf7_prefill_url', true ); |
| 172 |
?> |
| 173 |
<div class="misc-pub-section"> |
| 174 |
<label class="wpcf7-prefill-url"> |
| 175 |
<input value="1" type="checkbox" name="wpcf7-prefill-url" <?php checked( $prefill_url ); ?>> |
| 176 |
<?php esc_html_e( 'Pre-fill fields from URL', 'cf7-coder' ); ?> |
| 177 |
<sup class="has-tooltip on-left" data-tooltip="Fill form fields from URL parameters (e.g., ?your-email=test@example.com).">ℹ</sup> |
| 178 |
</label> |
| 179 |
</div> |
| 180 |
<?php |
| 181 |
$ga_event = get_post_meta( $post_id, '_wpcf7_ga_event', true ); |
| 182 |
$ga_event_name = get_post_meta( $post_id, '_wpcf7_ga_event_name', true ); |
| 183 |
?> |
| 184 |
<div class="misc-pub-section"> |
| 185 |
<label class="wpcf7-ga-event"> |
| 186 |
<input value="1" type="checkbox" id="wpcf7_ga_event" name="wpcf7-ga-event" <?php checked( $ga_event ); ?>> |
| 187 |
<?php esc_html_e( 'GA/GTM Event on submit', 'cf7-coder' ); ?> |
| 188 |
<sup class="has-tooltip on-left" data-tooltip="Send event to Google Analytics/GTM dataLayer on successful submission.">ℹ</sup> |
| 189 |
</label> |
| 190 |
<div id="wpcf7-ga-event-wrap" style="margin-top: 8px; <?php echo ! empty( $ga_event ) ? '' : 'display: none;'; ?>"> |
| 191 |
<input type="text" name="wpcf7-ga-event-name" id="wpcf7-ga-event-name" |
| 192 |
value="<?php echo esc_attr( $ga_event_name ); ?>" |
| 193 |
placeholder="cf7_form_submit" style="width: 100%;"> |
| 194 |
<small style="color: #666;">Event name for dataLayer</small> |
| 195 |
</div> |
| 196 |
</div> |
| 197 |
<?php |
| 198 |
$scroll_to_message = get_post_meta( $post_id, '_wpcf7_scroll_to_message', true ); |
| 199 |
?> |
| 200 |
<div class="misc-pub-section"> |
| 201 |
<label class="wpcf7-scroll-to-message"> |
| 202 |
<input value="1" type="checkbox" name="wpcf7-scroll-to-message" <?php checked( $scroll_to_message ); ?>> |
| 203 |
<?php esc_html_e( 'Scroll to message after submit', 'cf7-coder' ); ?> |
| 204 |
<sup class="has-tooltip on-left" data-tooltip="Automatically scroll to success/error message after form submission.">ℹ</sup> |
| 205 |
</label> |
| 206 |
</div> |
| 207 |
<?php |
| 208 |
$auto_hide_message = get_post_meta( $post_id, '_wpcf7_auto_hide_message', true ); |
| 209 |
$auto_hide_timeout = get_post_meta( $post_id, '_wpcf7_auto_hide_timeout', true ); |
| 210 |
if ( empty( $auto_hide_timeout ) ) { |
| 211 |
$auto_hide_timeout = 5; |
| 212 |
} |
| 213 |
?> |
| 214 |
<div class="misc-pub-section"> |
| 215 |
<label class="wpcf7-auto-hide-message"> |
| 216 |
<input value="1" type="checkbox" id="wpcf7_auto_hide_message" name="wpcf7-auto-hide-message" <?php checked( $auto_hide_message ); ?>> |
| 217 |
<?php esc_html_e( 'Auto-hide success message', 'cf7-coder' ); ?> |
| 218 |
<sup class="has-tooltip on-left" data-tooltip="Automatically hide success message after specified seconds.">ℹ</sup> |
| 219 |
</label> |
| 220 |
<div id="wpcf7-auto-hide-wrap" style="margin-top: 8px; <?php echo ! empty( $auto_hide_message ) ? '' : 'display: none;'; ?>"> |
| 221 |
<input type="number" name="wpcf7-auto-hide-timeout" id="wpcf7-auto-hide-timeout" |
| 222 |
value="<?php echo esc_attr( $auto_hide_timeout ); ?>" |
| 223 |
min="1" max="60" style="width: 60px;"> <?php esc_html_e( 'seconds', 'cf7-coder' ); ?> |
| 224 |
</div> |
| 225 |
</div> |
| 226 |
<div class="misc-pub-section"> |
| 227 |
<a href="https://wordpress.org/plugins/wp-coder/" target="_blank">Check out WP Coder – advanced code |
| 228 |
injection</a> |
| 229 |
</div> |
| 230 |
<?php |
| 231 |
} |
| 232 |
|
| 233 |
// Add properties for form |
| 234 |
public function wpcf7_add_properties( $properties ) { |
| 235 |
$more_properties = array( |
| 236 |
'wpcf7_test_mode' => '', |
| 237 |
'wpcf7_remove_auto_tags' => '', |
| 238 |
'wpcf7_codemiror_dark' => '', |
| 239 |
); |
| 240 |
|
| 241 |
return array_merge( $more_properties, $properties ); |
| 242 |
} |
| 243 |
|
| 244 |
// Save custom properties |
| 245 |
// phpcs:disable WordPress.Security.NonceVerification.Missing -- Nonce verified by Contact Form 7 |
| 246 |
public function wpcf7_save( $contact_form ) { |
| 247 |
$post_id = $contact_form->id(); |
| 248 |
$properties = $contact_form->get_properties(); |
| 249 |
|
| 250 |
$properties['wpcf7_test_mode'] = isset( $_POST['wpcf7-test-mode'] ) ? '1' : ''; |
| 251 |
$properties['wpcf7_remove_auto_tags'] = isset( $_POST['wpcf7-remove-auto-tags'] ) ? '1' : ''; |
| 252 |
$properties['wpcf7_codemiror_dark'] = isset( $_POST['wpcf7_codemiror_dark'] ) ? '1' : ''; |
| 253 |
|
| 254 |
$contact_form->set_properties( $properties ); |
| 255 |
|
| 256 |
// Save global option for conditional assets loading |
| 257 |
$load_assets_shortcode = isset( $_POST['wpcf7_load_assets_shortcode'] ) ? '1' : ''; |
| 258 |
update_option( 'wpcf7_load_assets_shortcode', $load_assets_shortcode ); |
| 259 |
|
| 260 |
// Save redirect enabled checkbox |
| 261 |
$redirect_enabled = isset( $_POST['wpcf7-redirect-enabled'] ) ? '1' : ''; |
| 262 |
update_post_meta( $post_id, '_wpcf7_redirect_enabled', $redirect_enabled ); |
| 263 |
|
| 264 |
// Save redirect URL |
| 265 |
$redirect_url = isset( $_POST['wpcf7-redirect-url'] ) ? esc_url_raw( wp_unslash( $_POST['wpcf7-redirect-url'] ) ) : ''; |
| 266 |
update_post_meta( $post_id, '_wpcf7_redirect_url', $redirect_url ); |
| 267 |
|
| 268 |
// Save ACF field name for redirect |
| 269 |
$acf_field = isset( $_POST['wpcf7-redirect-acf-field'] ) ? sanitize_text_field( wp_unslash( $_POST['wpcf7-redirect-acf-field'] ) ) : ''; |
| 270 |
update_post_meta( $post_id, '_wpcf7_redirect_acf_field', $acf_field ); |
| 271 |
|
| 272 |
// Save redirect new tab option |
| 273 |
$redirect_new_tab = isset( $_POST['wpcf7-redirect-new-tab'] ) ? '1' : ''; |
| 274 |
update_post_meta( $post_id, '_wpcf7_redirect_new_tab', $redirect_new_tab ); |
| 275 |
|
| 276 |
// Save redirect download option |
| 277 |
$redirect_download = isset( $_POST['wpcf7-redirect-download'] ) ? '1' : ''; |
| 278 |
update_post_meta( $post_id, '_wpcf7_redirect_download', $redirect_download ); |
| 279 |
|
| 280 |
// Save hide form option |
| 281 |
$hide_form = isset( $_POST['wpcf7-hide-form'] ) ? '1' : ''; |
| 282 |
update_post_meta( $post_id, '_wpcf7_hide_form', $hide_form ); |
| 283 |
|
| 284 |
// Save remove refill option |
| 285 |
$remove_refill = isset( $_POST['wpcf7-remove-refill'] ) ? '1' : ''; |
| 286 |
update_post_meta( $post_id, '_wpcf7_remove_refill', $remove_refill ); |
| 287 |
|
| 288 |
// Save disable submit button option |
| 289 |
$disable_submit = isset( $_POST['wpcf7-disable-submit'] ) ? '1' : ''; |
| 290 |
update_post_meta( $post_id, '_wpcf7_disable_submit', $disable_submit ); |
| 291 |
|
| 292 |
// Save pre-fill from URL option |
| 293 |
$prefill_url = isset( $_POST['wpcf7-prefill-url'] ) ? '1' : ''; |
| 294 |
update_post_meta( $post_id, '_wpcf7_prefill_url', $prefill_url ); |
| 295 |
|
| 296 |
// Save GA/GTM event options |
| 297 |
$ga_event = isset( $_POST['wpcf7-ga-event'] ) ? '1' : ''; |
| 298 |
update_post_meta( $post_id, '_wpcf7_ga_event', $ga_event ); |
| 299 |
|
| 300 |
$ga_event_name = isset( $_POST['wpcf7-ga-event-name'] ) ? sanitize_text_field( wp_unslash( $_POST['wpcf7-ga-event-name'] ) ) : ''; |
| 301 |
update_post_meta( $post_id, '_wpcf7_ga_event_name', $ga_event_name ); |
| 302 |
|
| 303 |
// Save scroll to message option |
| 304 |
$scroll_to_message = isset( $_POST['wpcf7-scroll-to-message'] ) ? '1' : ''; |
| 305 |
update_post_meta( $post_id, '_wpcf7_scroll_to_message', $scroll_to_message ); |
| 306 |
|
| 307 |
// Save auto-hide message options |
| 308 |
$auto_hide_message = isset( $_POST['wpcf7-auto-hide-message'] ) ? '1' : ''; |
| 309 |
update_post_meta( $post_id, '_wpcf7_auto_hide_message', $auto_hide_message ); |
| 310 |
|
| 311 |
$auto_hide_timeout = isset( $_POST['wpcf7-auto-hide-timeout'] ) ? absint( $_POST['wpcf7-auto-hide-timeout'] ) : 5; |
| 312 |
if ( $auto_hide_timeout < 1 ) { |
| 313 |
$auto_hide_timeout = 1; |
| 314 |
} |
| 315 |
if ( $auto_hide_timeout > 60 ) { |
| 316 |
$auto_hide_timeout = 60; |
| 317 |
} |
| 318 |
update_post_meta( $post_id, '_wpcf7_auto_hide_timeout', $auto_hide_timeout ); |
| 319 |
} |
| 320 |
// phpcs:enable WordPress.Security.NonceVerification.Missing |
| 321 |
|
| 322 |
// Work with Frontend |
| 323 |
public function wpcf7_frontend( $output, $tag, $atts, $m ) { |
| 324 |
if ( $tag === 'contact-form-7' ) { |
| 325 |
// Enqueue CF7 assets if conditional loading is enabled |
| 326 |
if ( get_option( 'wpcf7_load_assets_shortcode' ) ) { |
| 327 |
$this->enqueue_cf7_assets(); |
| 328 |
} |
| 329 |
|
| 330 |
if ( ! function_exists( 'wpcf7_get_contact_form_by_hash' ) ) { |
| 331 |
return $output; |
| 332 |
} |
| 333 |
|
| 334 |
$form = wpcf7_get_contact_form_by_hash( $atts['id'] ); |
| 335 |
|
| 336 |
if ( ! $form instanceof WPCF7_ContactForm ) { |
| 337 |
return $output; |
| 338 |
} |
| 339 |
|
| 340 |
$form_id = $form->id(); |
| 341 |
|
| 342 |
$remove_tags = get_post_meta( $form_id, '_wpcf7_remove_auto_tags', true ); |
| 343 |
if ( ! empty( $remove_tags ) ) { |
| 344 |
$output = str_replace( array( '<p>', '</p>', '<br/>' ), '', $output ); |
| 345 |
} |
| 346 |
|
| 347 |
$test_mode = get_post_meta( $form_id, '_wpcf7_test_mode', true ); |
| 348 |
if ( ! empty( $test_mode ) && ! current_user_can( 'administrator' ) ) { |
| 349 |
$output = ''; |
| 350 |
} |
| 351 |
|
| 352 |
// Redirect after submit - check if enabled first |
| 353 |
$redirect_enabled = get_post_meta( $form_id, '_wpcf7_redirect_enabled', true ); |
| 354 |
if ( ! empty( $redirect_enabled ) ) { |
| 355 |
$redirect_url = ''; |
| 356 |
$acf_field_name = get_post_meta( $form_id, '_wpcf7_redirect_acf_field', true ); |
| 357 |
|
| 358 |
// Check ACF field first (from current page) |
| 359 |
if ( ! empty( $acf_field_name ) && function_exists( 'get_field' ) ) { |
| 360 |
$current_post_id = get_the_ID(); |
| 361 |
$acf_url = get_field( $acf_field_name, $current_post_id ); |
| 362 |
if ( ! empty( $acf_url ) ) { |
| 363 |
$redirect_url = $acf_url; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
// Fallback to static URL only if ACF field name is not set |
| 368 |
if ( empty( $redirect_url ) && empty( $acf_field_name ) ) { |
| 369 |
$redirect_url = get_post_meta( $form_id, '_wpcf7_redirect_url', true ); |
| 370 |
} |
| 371 |
|
| 372 |
if ( ! empty( $redirect_url ) ) { |
| 373 |
$redirect_new_tab = get_post_meta( $form_id, '_wpcf7_redirect_new_tab', true ); |
| 374 |
$redirect_download = get_post_meta( $form_id, '_wpcf7_redirect_download', true ); |
| 375 |
|
| 376 |
// Determine redirect action: download > new tab > same window |
| 377 |
if ( ! empty( $redirect_download ) ) { |
| 378 |
$redirect_action = 'var link = document.createElement("a"); link.href = "' . esc_url( $redirect_url ) . '"; link.download = ""; document.body.appendChild(link); link.click(); document.body.removeChild(link);'; |
| 379 |
} elseif ( ! empty( $redirect_new_tab ) ) { |
| 380 |
$redirect_action = 'window.open("' . esc_url( $redirect_url ) . '", "_blank");'; |
| 381 |
} else { |
| 382 |
$redirect_action = 'window.location.href = "' . esc_url( $redirect_url ) . '";'; |
| 383 |
} |
| 384 |
|
| 385 |
$output .= '<script> |
| 386 |
document.addEventListener("wpcf7mailsent", function(event) { |
| 387 |
if (event.detail.contactFormId == ' . (int) $form_id . ') { |
| 388 |
' . $redirect_action . ' |
| 389 |
} |
| 390 |
}); |
| 391 |
</script>'; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
// Hide form after submit |
| 396 |
$hide_form = get_post_meta( $form_id, '_wpcf7_hide_form', true ); |
| 397 |
if ( ! empty( $hide_form ) ) { |
| 398 |
$output .= '<script> |
| 399 |
document.addEventListener("wpcf7mailsent", function(event) { |
| 400 |
if (event.detail.contactFormId == ' . (int) $form_id . ') { |
| 401 |
var wrapper = event.target.closest(".wpcf7"); |
| 402 |
if (wrapper) { |
| 403 |
var form = wrapper.querySelector("form"); |
| 404 |
if (form) { |
| 405 |
Array.from(form.children).forEach(function(child) { |
| 406 |
if (!child.classList.contains("wpcf7-response-output")) { |
| 407 |
child.style.display = "none"; |
| 408 |
} |
| 409 |
}); |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
}); |
| 414 |
</script>'; |
| 415 |
} |
| 416 |
|
| 417 |
// Remove refill - clear form fields after validation error |
| 418 |
$remove_refill = get_post_meta( $form_id, '_wpcf7_remove_refill', true ); |
| 419 |
if ( ! empty( $remove_refill ) ) { |
| 420 |
$output .= '<script> |
| 421 |
document.addEventListener("wpcf7invalid", function(event) { |
| 422 |
if (event.detail.contactFormId == ' . (int) $form_id . ') { |
| 423 |
var wrapper = event.target.closest(".wpcf7"); |
| 424 |
if (wrapper) { |
| 425 |
var form = wrapper.querySelector("form"); |
| 426 |
if (form) { form.reset(); } |
| 427 |
} |
| 428 |
} |
| 429 |
}); |
| 430 |
</script>'; |
| 431 |
} |
| 432 |
|
| 433 |
// Disable submit button while sending |
| 434 |
$disable_submit = get_post_meta( $form_id, '_wpcf7_disable_submit', true ); |
| 435 |
if ( ! empty( $disable_submit ) ) { |
| 436 |
$output .= '<script> |
| 437 |
(function() { |
| 438 |
var formId = ' . (int) $form_id . '; |
| 439 |
document.addEventListener("wpcf7beforesubmit", function(event) { |
| 440 |
if (event.detail.contactFormId == formId) { |
| 441 |
var wrapper = event.target.closest(".wpcf7"); |
| 442 |
if (wrapper) { |
| 443 |
var btn = wrapper.querySelector("input[type=submit], button[type=submit]"); |
| 444 |
if (btn) { |
| 445 |
btn.disabled = true; |
| 446 |
btn.dataset.originalValue = btn.value || btn.textContent; |
| 447 |
if (btn.tagName === "INPUT") { |
| 448 |
btn.value = "' . esc_js( __( 'Sending...', 'cf7-coder' ) ) . '"; |
| 449 |
} else { |
| 450 |
btn.textContent = "' . esc_js( __( 'Sending...', 'cf7-coder' ) ) . '"; |
| 451 |
} |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
}); |
| 456 |
document.addEventListener("wpcf7mailsent", function(event) { |
| 457 |
if (event.detail.contactFormId == formId) { |
| 458 |
var wrapper = event.target.closest(".wpcf7"); |
| 459 |
if (wrapper) { |
| 460 |
var btn = wrapper.querySelector("input[type=submit], button[type=submit]"); |
| 461 |
if (btn) { |
| 462 |
btn.disabled = false; |
| 463 |
if (btn.tagName === "INPUT") { |
| 464 |
btn.value = btn.dataset.originalValue; |
| 465 |
} else { |
| 466 |
btn.textContent = btn.dataset.originalValue; |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
}); |
| 472 |
document.addEventListener("wpcf7mailfailed", function(event) { |
| 473 |
if (event.detail.contactFormId == formId) { |
| 474 |
var wrapper = event.target.closest(".wpcf7"); |
| 475 |
if (wrapper) { |
| 476 |
var btn = wrapper.querySelector("input[type=submit], button[type=submit]"); |
| 477 |
if (btn) { |
| 478 |
btn.disabled = false; |
| 479 |
if (btn.tagName === "INPUT") { |
| 480 |
btn.value = btn.dataset.originalValue; |
| 481 |
} else { |
| 482 |
btn.textContent = btn.dataset.originalValue; |
| 483 |
} |
| 484 |
} |
| 485 |
} |
| 486 |
} |
| 487 |
}); |
| 488 |
document.addEventListener("wpcf7invalid", function(event) { |
| 489 |
if (event.detail.contactFormId == formId) { |
| 490 |
var wrapper = event.target.closest(".wpcf7"); |
| 491 |
if (wrapper) { |
| 492 |
var btn = wrapper.querySelector("input[type=submit], button[type=submit]"); |
| 493 |
if (btn) { |
| 494 |
btn.disabled = false; |
| 495 |
if (btn.tagName === "INPUT") { |
| 496 |
btn.value = btn.dataset.originalValue; |
| 497 |
} else { |
| 498 |
btn.textContent = btn.dataset.originalValue; |
| 499 |
} |
| 500 |
} |
| 501 |
} |
| 502 |
} |
| 503 |
}); |
| 504 |
})(); |
| 505 |
</script>'; |
| 506 |
} |
| 507 |
|
| 508 |
// Pre-fill fields from URL parameters |
| 509 |
$prefill_url = get_post_meta( $form_id, '_wpcf7_prefill_url', true ); |
| 510 |
if ( ! empty( $prefill_url ) ) { |
| 511 |
$output .= '<script> |
| 512 |
(function() { |
| 513 |
var wrapper = document.querySelector(".wpcf7[data-wpcf7-id=\"' . (int) $form_id . '\"]"); |
| 514 |
if (wrapper) { |
| 515 |
var params = new URLSearchParams(window.location.search); |
| 516 |
params.forEach(function(value, key) { |
| 517 |
var field = wrapper.querySelector("[name=\"" + key + "\"]"); |
| 518 |
if (field) { |
| 519 |
if (field.type === "checkbox" || field.type === "radio") { |
| 520 |
if (field.value === value || value === "1" || value === "true") { |
| 521 |
field.checked = true; |
| 522 |
} |
| 523 |
} else if (field.tagName === "SELECT") { |
| 524 |
var option = field.querySelector("option[value=\"" + value + "\"]"); |
| 525 |
if (option) { field.value = value; } |
| 526 |
} else { |
| 527 |
field.value = value; |
| 528 |
} |
| 529 |
} |
| 530 |
}); |
| 531 |
} |
| 532 |
})(); |
| 533 |
</script>'; |
| 534 |
} |
| 535 |
|
| 536 |
// GA/GTM Event on successful submit |
| 537 |
$ga_event = get_post_meta( $form_id, '_wpcf7_ga_event', true ); |
| 538 |
if ( ! empty( $ga_event ) ) { |
| 539 |
$ga_event_name = get_post_meta( $form_id, '_wpcf7_ga_event_name', true ); |
| 540 |
if ( empty( $ga_event_name ) ) { |
| 541 |
$ga_event_name = 'cf7_form_submit'; |
| 542 |
} |
| 543 |
$output .= '<script> |
| 544 |
document.addEventListener("wpcf7mailsent", function(event) { |
| 545 |
if (event.detail.contactFormId == ' . (int) $form_id . ') { |
| 546 |
if (typeof dataLayer !== "undefined") { |
| 547 |
dataLayer.push({ |
| 548 |
"event": "' . esc_js( $ga_event_name ) . '", |
| 549 |
"cf7FormId": ' . (int) $form_id . ', |
| 550 |
"cf7FormTitle": "' . esc_js( $form->title() ) . '" |
| 551 |
}); |
| 552 |
} |
| 553 |
} |
| 554 |
}); |
| 555 |
</script>'; |
| 556 |
} |
| 557 |
|
| 558 |
// Scroll to message after submit |
| 559 |
$scroll_to_message = get_post_meta( $form_id, '_wpcf7_scroll_to_message', true ); |
| 560 |
if ( ! empty( $scroll_to_message ) ) { |
| 561 |
$output .= '<script> |
| 562 |
(function() { |
| 563 |
var formId = ' . (int) $form_id . '; |
| 564 |
function scrollToMessage(event) { |
| 565 |
if (event.detail.contactFormId == formId) { |
| 566 |
var wrapper = event.target.closest(".wpcf7"); |
| 567 |
if (wrapper) { |
| 568 |
var message = wrapper.querySelector(".wpcf7-response-output"); |
| 569 |
if (message) { |
| 570 |
message.scrollIntoView({ behavior: "smooth", block: "center" }); |
| 571 |
} |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
document.addEventListener("wpcf7mailsent", scrollToMessage); |
| 576 |
document.addEventListener("wpcf7mailfailed", scrollToMessage); |
| 577 |
document.addEventListener("wpcf7invalid", scrollToMessage); |
| 578 |
})(); |
| 579 |
</script>'; |
| 580 |
} |
| 581 |
|
| 582 |
// Auto-hide success message |
| 583 |
$auto_hide_message = get_post_meta( $form_id, '_wpcf7_auto_hide_message', true ); |
| 584 |
if ( ! empty( $auto_hide_message ) ) { |
| 585 |
$auto_hide_timeout = get_post_meta( $form_id, '_wpcf7_auto_hide_timeout', true ); |
| 586 |
if ( empty( $auto_hide_timeout ) ) { |
| 587 |
$auto_hide_timeout = 5; |
| 588 |
} |
| 589 |
$output .= '<script> |
| 590 |
document.addEventListener("wpcf7mailsent", function(event) { |
| 591 |
if (event.detail.contactFormId == ' . (int) $form_id . ') { |
| 592 |
var wrapper = event.target.closest(".wpcf7"); |
| 593 |
if (wrapper) { |
| 594 |
var message = wrapper.querySelector(".wpcf7-response-output"); |
| 595 |
if (message) { |
| 596 |
setTimeout(function() { |
| 597 |
message.style.transition = "opacity 0.5s"; |
| 598 |
message.style.opacity = "0"; |
| 599 |
setTimeout(function() { |
| 600 |
message.style.display = "none"; |
| 601 |
message.style.opacity = "1"; |
| 602 |
}, 500); |
| 603 |
}, ' . (int) $auto_hide_timeout * 1000 . '); |
| 604 |
} |
| 605 |
} |
| 606 |
} |
| 607 |
}); |
| 608 |
</script>'; |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
return $output; |
| 613 |
} |
| 614 |
|
| 615 |
// Enqueue CF7 assets when shortcode is found |
| 616 |
public function enqueue_cf7_assets() { |
| 617 |
if ( function_exists( 'wpcf7_enqueue_scripts' ) ) { |
| 618 |
wpcf7_enqueue_scripts(); |
| 619 |
} |
| 620 |
if ( function_exists( 'wpcf7_enqueue_styles' ) ) { |
| 621 |
wpcf7_enqueue_styles(); |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
// Dequeue CF7 scripts and styles |
| 626 |
public function dequeue_cf7_assets() { |
| 627 |
wp_dequeue_script( 'contact-form-7' ); |
| 628 |
wp_dequeue_style( 'contact-form-7' ); |
| 629 |
} |
| 630 |
|
| 631 |
} |
| 632 |
|
| 633 |
add_action( 'plugins_loaded', 'wpcf7_coder_load', 999 ); |
| 634 |
function wpcf7_coder_load() { |
| 635 |
if ( ! class_exists( 'WPCF7' ) ) { |
| 636 |
require_once 'class.wpcf7coder-extension-activation.php'; |
| 637 |
$activation = new wpcf7_Coder_Extension_Activation( plugin_dir_path( __FILE__ ), basename( __FILE__ ) ); |
| 638 |
$activation = $activation->run(); |
| 639 |
deactivate_plugins( plugin_basename( __FILE__ ) ); |
| 640 |
} else { |
| 641 |
new CF7_Coder(); |
| 642 |
} |
| 643 |
} |
| 644 |
|