Skip to content

Commit 00a106e

Browse files
k1sul1anttiviljami
authored andcommitted
Add multilingual support for success messages (#72)
* Remove bad comment * Filter success message in class-wplf-polylang.php * Pass language with front end requests * Conform to CS * Check that Polylang exists * Fix phpcs errors after wpcs update
1 parent d10dc18 commit 00a106e

5 files changed

Lines changed: 55 additions & 8 deletions

File tree

assets/scripts/wplf-form.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
submitHandler: function (e) {
1616
var form = e.target;
1717
var data = new FormData(form);
18+
19+
// Pass language if it exists.
20+
ajax_object.lang && data.append('lang', ajax_object.lang);
21+
1822
// add class to enable css changes to indicate ajax loading
1923
form.classList.add("sending");
2024

classes/class-cpt-wplf-form.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,9 @@ function save_cpt( $post_id ) {
423423

424424
// save success message
425425
if ( isset( $_POST['wplf_thank_you'] ) ) {
426-
update_post_meta( $post_id, '_wplf_thank_you', wp_kses_post( $_POST['wplf_thank_you'] ) );
426+
$success = wp_kses_post( $_POST['wplf_thank_you'] );
427+
$success = apply_filters( 'wplf_save_success_message', $success, $post_id );
428+
update_post_meta( $post_id, '_wplf_thank_you', $success );
427429
}
428430

429431
// save fields
@@ -587,10 +589,10 @@ function maybe_enqueue_frontend_script() {
587589
);
588590

589591
// add dynamic variables to the script's scope
590-
wp_localize_script( 'wplf-form-js', 'ajax_object', array(
592+
wp_localize_script( 'wplf-form-js', 'ajax_object', apply_filters( 'wplf_ajax_object', array(
591593
'ajax_url' => admin_url( 'admin-ajax.php' ),
592594
'ajax_credentials' => apply_filters( 'wplf_ajax_fetch_credentials_mode', 'same-origin' ),
593-
) );
595+
) ) );
594596
}
595597

596598

classes/class-wplf-polylang.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22
if ( ! class_exists( 'WPLF_Polylang' ) ) {
33
class WPLF_Polylang {
4-
/**
5-
* CPT for the forms
6-
*/
4+
75
public static $instance;
86
protected $regular_expression = "/{{[^{}\n]+}}/";
97
protected $strings = array();
@@ -23,6 +21,11 @@ public function __construct() {
2321
add_filter( 'save_post_wplf-form', array( $this, 'save_form' ), 10, 3 );
2422
add_action( 'after_setup_theme', array( $this, 'register_strings' ) );
2523

24+
// Earlier than default. User probably wants to filter the translated message.
25+
add_action( 'wplf_success_message', array( $this, 'render_success_message' ), 9 );
26+
add_action( 'wplf_save_success_message', array( $this, 'save_success_message' ) );
27+
add_action( 'wplf_ajax_object', array( $this, 'ajax_object' ) );
28+
2629
$this->strings = get_option( 'wplf-translation-strings', array() );
2730
$this->register_strings();
2831
}
@@ -55,6 +58,39 @@ function save_form( $post_id, $post, $update ) {
5558
update_option( 'wplf-translation-strings', $this->strings ); // Let's be optimistic.
5659
}
5760

61+
public function render_success_message( $message ) {
62+
// Get all strings inside double curly braces.
63+
preg_match_all( $this->regular_expression, $message, $matches );
64+
foreach ( $matches[0] as $match ) {
65+
// match contains the braces, get rid of them.
66+
$string = trim( str_replace( array( '{', '}' ), array( '', '' ), $match ) );
67+
$message = str_replace( $match, $this->translate_string( $string ), $message );
68+
}
69+
70+
return $message;
71+
}
72+
73+
public function save_success_message( $message ) {
74+
preg_match_all( $this->regular_expression, $message, $matches );
75+
if ( ! empty( $matches ) ) {
76+
foreach ( $matches[0] as $match ) {
77+
// match contains the braces, get rid of them.
78+
$string = trim( str_replace( array( '{', '}' ), array( '', '' ), $match ) );
79+
$this->strings[ $string ] = null;
80+
// By storing the string as the array key, we don't need to use array_unique.
81+
}
82+
}
83+
84+
update_option( 'wplf-translation-strings', $this->strings ); // Let's be optimistic.
85+
86+
return $message;
87+
}
88+
89+
public function ajax_object( $array ) {
90+
$array['lang'] = pll_current_language();
91+
return $array;
92+
}
93+
5894
public function register_strings() {
5995
foreach ( $this->strings as $string => $value ) {
6096
$this->register_string( $string );

inc/wplf-ajax.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,13 @@ function wplf_ajax_submit_handler() {
8181
$return->submission_title = $post_title;
8282
$return->form_id = $form->ID;
8383

84+
$success = get_post_meta( $form->ID, '_wplf_thank_you', true );
85+
$success = apply_filters( "wplf_{$form->post_name}_success_message", $success );
86+
$success = apply_filters( "wplf_{$form->ID}_success_message", $success );
87+
$success = apply_filters( 'wplf_success_message', $success );
88+
8489
// return the success message for the form
85-
$return->success = apply_filters( 'the_content', get_post_meta( $form->ID, '_wplf_thank_you', true ) );
90+
$return->success = $success;
8691

8792
// allow user to attach custom actions after the submission has been received
8893
// these could be confirmation emails, additional processing for the submission fields, e.g.

wp-libre-form.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public static function load_our_textdomain() {
8888
* Enable Polylang support
8989
*/
9090
public function init_polylang_support() {
91-
if ( apply_filters( 'wplf_load_polylang', true ) ) {
91+
if ( apply_filters( 'wplf_load_polylang', true ) && class_exists( 'Polylang ' ) ) {
9292
require_once 'classes/class-wplf-polylang.php';
9393
WPLF_Polylang::init();
9494
}

0 commit comments

Comments
 (0)