• Resolved Tofandel

    (@tofandel)


    Hello, currently there is an issue with WPML

    Basically it has to do with the dropdown to select the page to redirect to

    Let’s say you have a contact form in EN and you are editing it in EN (you can switch language on the edit page) you can choose the EN page to redirect to and no problem because it lists all EN pages, however if now you switch language on the same page (Say FR) the plugin will now list all FR pages in the dropdown and will lose the previously saved page (even if you are not on the tab) as such you loose the redirection without knowing when you save the page in another language (which is very frequent)

    I included a file with the fix (apply_filter(‘wpml_object_id’ …) in 3 places) and also some warning fixes, feel free to use it

    <?php
    /**
     * Plugin Name:  Contact Form 7 Redirection
     * Plugin URI:   https://querysol.com
     * Description:  Contact Form 7 Add-on - Redirect after mail sent.
     * Version:	  1.3.3
     * Author:	   Query Solutions
     * Author URI:   https://querysol.com
     * Contributors: querysolutions, yuvalsabar
     * Requires at least: 4.7.0
     *
     * Text Domain: wpcf7-redirect
     * Domain Path: /lang
     *
     * @package  Contact Form 7 Redirection
     * @category Contact Form 7 Addon
     * @author   Query Solutions
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
    
    /**
     * Main WPCF7_Redirect Class
     */
    class WPCF7_Redirect {
    	public $version = '1.3.3';
    	public $plugin_path;
    	public $plugin_url;
    
    	protected $redirect_url;
    	protected $fields;
    	protected $enqueue_new_tab_script;
    
    	/**
    	 * Construct class
    	 */
    	public function __construct() {
    		$this->plugin_url = plugin_dir_url( __FILE__ );
    		$this->plugin_path = plugin_dir_path( __FILE__ );
    		$this->add_actions();
    	}
    
    	public static function install() {
    		update_option( 'wpcf7_redirect_admin_notice_dismiss', 0 );
    		update_option( 'wpcf7_redirect_banner_dismiss', 0 );
    	}
    
    	public function wpcf7_redirect_dismiss_ads() {
    		if ( isset( $_GET['wpcf7_redirect_dismiss_notice'] ) && $_GET['wpcf7_redirect_dismiss_notice'] == 1 ) {
    			update_option( 'wpcf7_redirect_admin_notice_dismiss', 1 );
    		}
    
    		if ( isset( $_GET['wpcf7_redirect_dismiss_banner'] ) && $_GET['wpcf7_redirect_dismiss_banner'] == 1 ) {
    			update_option( 'wpcf7_redirect_banner_dismiss', 1 );
    		}
    	}
    
    	/**
    	 * Add Actions
    	 */
    	private function add_actions() {
    		add_action( 'plugins_loaded', [ $this, 'load_textdomain' ] );
    		add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_backend' ] );
    		add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_frontend' ] );
    		add_action( 'wpcf7_editor_panels', [ $this, 'add_panel' ] );
    		add_action( 'wpcf7_after_save', [ $this, 'store_meta' ] );
    		add_action( 'wpcf7_after_create', [ $this, 'duplicate_form_support' ] );
    		add_action( 'wpcf7_submit', [ $this, 'non_ajax_redirection' ] );
    		add_action( 'admin_init', [ $this, 'wpcf7_redirect_dismiss_ads' ] );
    		add_action( 'admin_notices', [ $this, 'dependencies_notice' ] );
    		add_action( 'admin_notices', [ $this, 'pro_notice' ] );
    	}
    
    	/**
    	 * Load plugin textdomain.
    	 */
    	public function load_textdomain() {
    		load_plugin_textdomain( 'wpcf7-redirect', false, basename( dirname( __FILE__ ) ) . '/lang' );
    	}
    
    	/**
    	 * Enqueue theme styles and scripts - back-end
    	 */
    	public function enqueue_backend() {
    		wp_enqueue_style( 'wpcf7-redirect-admin-style', $this->plugin_url . 'admin/wpcf7-redirect-admin-style.css' );
    		wp_enqueue_script( 'wpcf7-redirect-admin-script', $this->plugin_url . 'admin/wpcf7-redirect-admin-script.js', [], null, true );
    	}
    
    	/**
    	 * Enqueue theme styles and scripts - front-end
    	 */
    	public function enqueue_frontend() {
    		wp_enqueue_script( 'wpcf7-redirect-script', $this->plugin_url . 'js/wpcf7-redirect-script.js', [], null, true );
    		wp_localize_script( 'wpcf7-redirect-script', 'wpcf7_redirect_forms', $this->get_forms() );
    
    		if ( isset( $this->enqueue_new_tab_script ) && $this->enqueue_new_tab_script ) {
    			wp_add_inline_script( 'wpcf7-redirect-script', 'window.open("' . $this->redirect_url . '");' );
    		}
    	}
    
    	/**
    	 * Adds a tab to the editor on the form edit page
    	 *
    	 * @param array $panels An array of panels. Each panel has a callback function.
    	 *
    	 * @return array
    	 */
    	public function add_panel( $panels ) {
    		$panels['redirect-panel'] = [
    			'title' => __( 'Redirect Settings', 'wpcf7-redirect' ),
    			'callback' => [ $this, 'create_panel_inputs' ],
    		];
    		return $panels;
    	}
    
    	/**
    	 * Create plugin fields
    	 *
    	 * @return array of plugin fields: name and type (type is for validation)
    	 */
    	public function get_plugin_fields() {
    		$fields = [
    			[
    				'name' => 'page_id',
    				'type' => 'number',
    			],
    			[
    				'name' => 'external_url',
    				'type' => 'url',
    			],
    			[
    				'name' => 'use_external_url',
    				'type' => 'checkbox',
    			],
    			[
    				'name' => 'open_in_new_tab',
    				'type' => 'checkbox',
    			],
    			[
    				'name' => 'http_build_query',
    				'type' => 'checkbox',
    			],
    			[
    				'name' => 'http_build_query_selectively',
    				'type' => 'checkbox',
    			],
    			[
    				'name' => 'http_build_query_selectively_fields',
    				'type' => 'text',
    			],
    			[
    				'name' => 'delay_redirect',
    				'type' => 'number',
    			],
    			[
    				'name' => 'after_sent_script',
    				'type' => 'textarea',
    			],
    		];
    
    		return $fields;
    	}
    
    	/**
    	 * Get all fields values
    	 *
    	 * @param integer $post_id Form ID.
    	 *
    	 * @return array of fields values keyed by fields name
    	 */
    	public function get_fields_values( $post_id ) {
    		$fields = $this->get_plugin_fields();
    
    		$values = [];
    		foreach ( $fields as $field ) {
    			$values[$field['name']] = get_post_meta( $post_id, '_wpcf7_redirect_' . $field['name'], true );
    		}
    
    		return $values;
    	}
    
    	/**
    	 * Validate and store meta data
    	 *
    	 * @param object $contact_form WPCF7_ContactForm Object - All data that is related to the form.
    	 */
    	public function store_meta( $contact_form ) {
    		if ( ! isset( $_POST ) || empty( $_POST ) ) {
    			return;
    		} else {
    			if ( ! wp_verify_nonce( $_POST['wpcf7_redirect_page_metaboxes_nonce'], 'wpcf7_redirect_page_metaboxes' ) ) {
    				return;
    			}
    
    			$form_id = $contact_form->id();
    			$fields = $this->get_plugin_fields();
    			$data = $_POST['wpcf7-redirect'];
    
    			foreach ( $fields as $field ) {
    				$value = isset( $data[$field['name']] ) ? $data[$field['name']] : '';
    
    				switch ( $field['type'] ) {
    					case 'text':
    					case 'checkbox':
    						$value = sanitize_text_field( $value );
    						break;
    
    					case 'textarea':
    						$value = htmlspecialchars( $value );
    						break;
    
    					case 'number':
    						$value = intval( $value );
    						break;
    
    					case 'url':
    						$value = esc_url_raw( $value );
    						break;
    				}
    
    				update_post_meta( $form_id, '_wpcf7_redirect_' . $field['name'], $value );
    			}
    		}
    	}
    
    	/**
    	 * Push all forms redirect settings data into an array.
    	 *
    	 * @return array  Form redirect settings data
    	 */
    	public function get_forms() {
    		$args = [
    			'post_type' => 'wpcf7_contact_form',
    			'posts_per_page' => -1,
    			'suppress_filters' => true,
    		];
    		$query = new WP_Query( $args );
    
    		$forms = [];
    
    		if ( $query->have_posts() ) :
    
    			$fields = $this->get_plugin_fields();
    
    			while ( $query->have_posts() ) : $query->the_post();
    
    				$post_id = get_the_ID();
    
    				foreach ( $fields as $field ) {
    					$forms[$post_id][$field['name']] = get_post_meta( $post_id, '_wpcf7_redirect_' . $field['name'], true );
    				}
    
    				// Thank you page URL is a little bit different...
    				$forms[$post_id]['thankyou_page_url'] = $forms[$post_id]['page_id'] ? get_permalink( apply_filters( 'wpml_object_id', $forms[$post_id]['page_id'], 'page', true ) ) : '';
    			endwhile;
    			wp_reset_postdata();
    		endif;
    
    		return $forms;
    	}
    
    	/**
    	 * Copy Redirect page key and assign it to duplicate form
    	 *
    	 * @param object $contact_form WPCF7_ContactForm Object - All data that is related to the form.
    	 */
    	public function duplicate_form_support( $contact_form ) {
    		$contact_form_id = $contact_form->id();
    
    		if ( ! empty( $_REQUEST['post'] ) && ! empty( $_REQUEST['_wpnonce'] ) ) {
    			$post_id = intval( $_REQUEST['post'] );
    
    			$fields = $this->get_plugin_fields();
    
    			foreach ( $fields as $field ) {
    				update_post_meta( $contact_form_id, '_wpcf7_redirect_' . $field['name'], get_post_meta( $post_id, '_wpcf7_redirect_' . $field['name'], true ) );
    			}
    		}
    	}
    
    	/**
    	 * Verify CF7 dependencies.
    	 */
    	public function dependencies_notice() {
    		if ( is_plugin_active( 'contact-form-7/wp-contact-form-7.php' ) ) {
    			$wpcf7_path = plugin_dir_path( dirname( __FILE__ ) ) . 'contact-form-7/wp-contact-form-7.php';
    			$wpcf7_data = get_plugin_data( $wpcf7_path, false, false );
    
    			// If CF7 version is < 4.8.
    			if ( $wpcf7_data['Version'] < 4.8 ) {
    				?>
    
    				<div class="wpcf7-redirect-error error notice">
    					<h3>
    						<?php esc_html_e( 'Contact Form Redirection', 'wpcf7-redirect' ); ?>
    					</h3>
    					<p>
    						<?php esc_html_e( 'Error: Contact Form 7 version is too old. Contact Form Redirection is compatible from version 4.8 and above. Please update Contact Form 7.', 'wpcf7-redirect' ); ?>
    					</p>
    				</div>
    
    				<?php
    			}
    		} else {
    			// If CF7 isn't installed and activated, throw an error.
    			?>
    			<div class="wpcf7-redirect-error error notice">
    				<h3>
    					<?php esc_html_e( 'Contact Form Redirection', 'wpcf7-redirect' ); ?>
    				</h3>
    				<p>
    					<?php esc_html_e( 'Error: Please install and activate Contact Form 7.', 'wpcf7-redirect' ); ?>
    				</p>
    			</div>
    
    			<?php
    		}
    	}
    
    	public function pro_notice() {
    		if ( ! get_option( 'wpcf7_redirect_admin_notice_dismiss' ) ) : ?>
    
    			<div class="wpcf7-redirect-pro-admin-notice updated notice is-dismissible">
    				<p>
    					<a href="https://querysol.com/product/contact-form-7-redirection/" target="_blank">
    						Contact Form 7 Redirection Pro - We've added exciting new features!
    					</a>
    				</p>
    			</div>
    
    		<?php endif;
    	}
    
    	/**
    	 * Add plugin support to browsers that don't support ajax
    	 *
    	 * @param WPCF7_ContactForm $contact_form
    	 */
    	public function non_ajax_redirection( $contact_form ) {
    		$this->fields = $this->get_fields_values( $contact_form->id() );
    
    		if ( isset( $this->fields ) && ! WPCF7_Submission::is_restful() ) {
    			$submission = WPCF7_Submission::get_instance();
    
    			if ( $submission->get_status() == 'mail_sent' ) {
    
    				// Use extrnal url
    				if ( $this->fields['external_url'] && $this->fields['use_external_url'] == 'on' ) {
    					$this->redirect_url = $this->fields['external_url'];
    				} else {
    					$this->redirect_url = get_permalink( apply_filters( 'wpml_object_id', $this->fields['page_id'], 'page', true ) );
    				}
    
    				// Pass all fields from the form as URL query parameters
    				if ( isset( $this->redirect_url ) && $this->redirect_url ) {
    					if ( $this->fields['http_build_query'] == 'on' ) {
    						$posted_data = $submission->get_posted_data();
    						// Remove WPCF7 keys from posted data
    						$remove_keys = [ '_wpcf7', '_wpcf7_version', '_wpcf7_locale', '_wpcf7_unit_tag', '_wpcf7_container_post' ];
    						$posted_data = array_diff_key( $posted_data, array_flip( $remove_keys ) );
    						$this->redirect_url = add_query_arg( $posted_data, $this->redirect_url );
    					}
    				}
    
    				// Open link in a new tab
    				if ( isset( $this->redirect_url ) && $this->redirect_url ) {
    					if ( $this->fields['open_in_new_tab'] == 'on' ) {
    						$this->enqueue_new_tab_script = true;
    					} else {
    						wp_redirect( $this->redirect_url );
    						exit;
    					}
    				}
    			}
    		}
    	}
    
    	/**
    	 * Create the panel inputs
    	 *
    	 * @param object $post Post object.
    	 */
    	public function create_panel_inputs( $post ) {
    		wp_nonce_field( 'wpcf7_redirect_page_metaboxes', 'wpcf7_redirect_page_metaboxes_nonce' );
    
    		$fields = $this->get_fields_values( $post->id() );
    		?>
    
    		<?php if ( ! get_option( 'wpcf7_redirect_banner_dismiss' ) ) : ?>
    
    			<div class="banner-wrap">
    				<button type="button" class="notice-dismiss">
    					<span class="screen-reader-text"><?php _e( 'Close Banner', 'qstheme' ); ?>.</span>
    				</button>
    				<a href="https://querysol.com/product/contact-form-7-redirection/" target="_blank">
    					<img src="<?php echo $this->plugin_url; ?>/img/banner-pro.png"
    						 alt="<?php _e( 'Banner - Contact Form 7 Redirection Pro', 'wpcf7-redirect' ); ?>">
    				</a>
    			</div>
    
    		<?php endif; ?>
    
    		<h2>
    			<?php esc_html_e( 'Redirect Settings', 'wpcf7-redirect' ); ?>
    		</h2>
    
    		<fieldset>
    			<div class="field-wrap field-wrap-page-id">
    				<label for="wpcf7-redirect-page-id">
    					<?php esc_html_e( 'Select a page to redirect to on successful form submission.', 'wpcf7-redirect' ); ?>
    				</label>
    				<?php
    				wp_dropdown_pages( [
    						'name' => 'wpcf7-redirect[page_id]',
    						'show_option_no_change' => ! empty( $fields['page_id'] ) ? get_the_title( $fields['page_id'] ) : '',
    						'show_option_none' => __( 'Choose Page', 'wpcf7-redirect' ),
    						'option_none_value' => '0',
    						'selected' => apply_filters( 'wpml_object_id', $fields['page_id'], 'page', true ),
    						'id' => 'wpcf7-redirect-page-id',
    					]
    				);
    				?>
    			</div>
    
    			<div class="field-wrap field-wrap-external-url">
    				<input type="url" id="wpcf7-redirect-external-url" placeholder="<?php esc_html_e( 'External URL', 'wpcf7-redirect' ); ?>"
    					   name="wpcf7-redirect[external_url]" value="<?php echo $fields['external_url']; ?>">
    			</div>
    
    			<div class="field-wrap field-wrap-use-external-url">
    				<input type="checkbox" id="wpcf7-redirect-use-external-url"
    					   name="wpcf7-redirect[use_external_url]" <?php checked( $fields['use_external_url'], 'on', true ); ?>/>
    				<label for="wpcf7-redirect-use-external-url">
    					<?php esc_html_e( 'Use external URL', 'wpcf7-redirect' ); ?>
    				</label>
    			</div>
    
    			<div class="field-wrap field-wrap-open-in-new-tab">
    				<input type="checkbox" id="wpcf7-redirect-open-in-new-tab"
    					   name="wpcf7-redirect[open_in_new_tab]" <?php checked( $fields['open_in_new_tab'], 'on', true ); ?>/>
    				<label for="wpcf7-redirect-open-in-new-tab"><?php esc_html_e( 'Open page in a new tab', 'wpcf7-redirect' ); ?></label>
    				<div class="field-notice field-notice-alert field-notice-hidden">
    					<strong>
    						<?php esc_html_e( 'Notice!', 'wpcf7-redirect' ); ?>
    					</strong>
    					<?php esc_html_e( 'This option might not work as expected, since browsers often block popup windows. This option depends on the browser settings.', 'wpcf7-redirect' ); ?>
    				</div>
    			</div>
    
    			<div class="field-wrap field-wrap-http-build-query">
    				<input type="checkbox" id="wpcf7-redirect-http-build-query" class="checkbox-radio-1"
    					   name="wpcf7-redirect[http_build_query]" <?php checked( $fields['http_build_query'], 'on', true ); ?>/>
    				<label for="wpcf7-redirect-http-build-query">
    					<?php esc_html_e( 'Pass all the fields from the form as URL query parameters', 'wpcf7-redirect' ); ?>
    				</label>
    			</div>
    
    			<div class="field-wrap field-wrap-http-build-query-selectively">
    				<input type="checkbox" id="wpcf7-redirect-http-build-query-selectively" class="checkbox-radio-1"
    					   name="wpcf7-redirect[http_build_query_selectively]" <?php checked( $fields['http_build_query_selectively'], 'on', true ); ?>/>
    				<label for="wpcf7-redirect-http-build-query-selectively">
    					<?php esc_html_e( 'Pass specific fields from the form as URL query parameters', 'wpcf7-redirect' ); ?>
    				</label>
    				<input type="text" id="wpcf7-redirect-http-build-query-selectively-fields" class="field-hidden"
    					   placeholder="<?php esc_html_e( 'Fields to pass, separated by commas', 'wpcf7-redirect' ); ?>"
    					   name="wpcf7-redirect[http_build_query_selectively_fields]" value="<?php echo $fields['http_build_query_selectively_fields']; ?>">
    			</div>
    
    			<div class="field-wrap field-wrap-delay-redirect">
    				<label for="wpcf7-redirect-delay-redirect">
    					<?php esc_html_e( 'Delay redirect (in milliseconds)', 'wpcf7-redirect' ); ?>
    				</label>
    				<input type="number" id="wpcf7-redirect-delay-redirect" name="wpcf7-redirect[delay_redirect]" value="<?php echo $fields['delay_redirect']; ?>">
    			</div>
    
    			<hr/>
    
    			<div class="field-wrap field-wrap-after-sent-script">
    				<label for="wpcf7-redirect-after-sent-script">
    					<?php esc_html_e( 'Here you can add scripts to run after form sent successfully.', 'wpcf7-redirect' ); ?>
    				</label>
    				<div class="field-message">
    					<?php esc_html_e( 'Do not include <script> tags.', 'wpcf7-redirect' ); ?>
    				</div>
    				<textarea id="wpcf7-redirect-after-sent-script" name="wpcf7-redirect[after_sent_script]" rows="8"
    						  cols="100"><?php echo $fields['after_sent_script']; ?></textarea>
    			</div>
    			<div class="field-notice field-warning-alert field-notice-hidden">
    				<strong>
    					<?php esc_html_e( 'Warning!', 'wpcf7-redirect' ); ?>
    				</strong>
    				<?php esc_html_e( 'This option is for developers only - use with caution. If the plugin does not redirect after you have added scripts, it means you have a problem with your script. Either fix the script, or remove it.', 'wpcf7-redirect' ); ?>
    			</div>
    		</fieldset>
    
    		<div class="get-pro-wrap">
    			<div class="get-pro">
    				<span class="dashicons dashicons-star-filled"></span>
    				<a href="https://querysol.com/product/contact-form-7-redirection/" target="_blank">Contact Form 7 Redirection Pro - We've added exciting new
    					features!</a>
    				<span class="dashicons dashicons-star-filled"></span>
    			</div>
    		</div>
    
    		<?php
    	}
    }
    
    register_activation_hook( __FILE__, [ 'WPCF7_Redirect', 'install' ] );
    
    $cf7_redirect = new WPCF7_Redirect();
    
    • This topic was modified 5 years, 7 months ago by Tofandel.
    • This topic was modified 5 years, 7 months ago by Tofandel.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter Tofandel

    (@tofandel)

    A pull request has been submitted as well

    Plugin Contributor Yuval

    (@yuvalsabar)

    Hi @tofandel,

    Thank you for letting us know. I already saw your pull request, but did not find a time to check. We’ll test everything in the next few weeks and we’ll upload a new version with a fix.

    Yuval.

    Is this fixes?
    I have problem that the redirect only works on the default language.

    Plugin Contributor Yuval

    (@yuvalsabar)

    Hi @simondev,

    What seems to be the problem? When you translate the form, you can set different redirect settings. If not, please send me your website credentials to [email protected] and I’ll check.

    Yuval.

    Hi Yuval

    I’m having the same issue as described here.

    WPML recently changed the way you translate forms. You can see their new instructions at https://wpml.org/documentation/plugins-compatibility/using-contact-form-7-with-wpml/.

    So, we’re not actually building the form from scratch, in which we’d be able to choose the new redirect page, but instead we’re translating the form.

    Can you take another look at this?

    Thanks,
    Todd

    Plugin Contributor Yuval

    (@yuvalsabar)

    Hi @toddedelman,

    Will do, but it might take few weeks. We’ll update.
    Meanwhile, you can duplicate the form and translate manually just like you did before. They have just added another option, it doesn’t mean the old way will not work anymore (personally, I found the new way a little bit buggy at the moment, so I keep duplicating and translating the forms manually).

    Yuval.

    • This reply was modified 5 years, 3 months ago by Yuval.
    • This reply was modified 5 years, 3 months ago by Yuval.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Issue with WPML’ is closed to new replies.