11<?php
22if ( ! 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 );
0 commit comments