Skip to content

Commit e29268d

Browse files
k1sul1Antti Kuosmanen
authored andcommitted
String translation support, closes #33
* polylang support using {{ Template tags }}
1 parent b57fabe commit e29268d

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

classes/class-wplf-polylang.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
if ( !class_exists( 'WPLF_Polylang' ) ) {
3+
class WPLF_Polylang {
4+
/**
5+
* CPT for the forms
6+
*/
7+
public static $instance;
8+
protected $regular_expression = "/{{\s*.+\s*}}/";
9+
protected $strings = array();
10+
11+
public static function init() {
12+
if ( is_null( self::$instance ) ) {
13+
self::$instance = new WPLF_Polylang();
14+
}
15+
return self::$instance;
16+
}
17+
18+
/**
19+
* Hook our actions, filters and such
20+
*/
21+
private function __construct() {
22+
add_filter( 'wplf_form', array( $this, 'render_form' ) );
23+
add_filter( 'save_post_wplf-form', array( $this, 'save_form' ), 10, 3);
24+
add_action( 'after_setup_theme', array ( $this, 'register_strings') );
25+
26+
$this->strings = get_option( 'wplf-translation-strings', array() );
27+
$this->register_strings();
28+
}
29+
30+
public function render_form ( $form_content ) {
31+
// Get all strings inside double curly braces.
32+
preg_match_all( $this->regular_expression, $form_content, $matches );
33+
foreach( $matches[0] as $match ){
34+
// match contains the braces, get rid of them.
35+
$string = trim( str_replace( array( "{" , "}" ), array( "", "" ), $match ) );
36+
$form_content = str_replace( $match, $this->translate_string ( $string ), $form_content );
37+
}
38+
39+
return $form_content;
40+
}
41+
42+
function save_form ( $post_id, $post, $update ) {
43+
preg_match_all( $this->regular_expression, $post->post_content, $matches );
44+
if( !empty( $matches ) ) {
45+
foreach( $matches[0] as $match ){
46+
// match contains the braces, get rid of them.
47+
$string = trim( str_replace( array( "{" , "}" ), array( "", "" ), $match ) );
48+
$this->strings[$string] = null;
49+
// By storing the string as the array key, we don't need to use array_unique.
50+
}
51+
}
52+
53+
update_option( 'wplf-translation-strings', $this->strings ); // Let's be optimistic.
54+
}
55+
56+
public function register_strings () {
57+
foreach( $this->strings as $string => $value ) {
58+
$this->register_string( $string );
59+
}
60+
}
61+
62+
public function register_string ( $string ) {
63+
if( function_exists( 'pll_register_string' ) ) {
64+
pll_register_string( $string, $string, 'WP Libre Form' );
65+
} else {
66+
// Don't kill anything.
67+
}
68+
}
69+
70+
public function translate_string ( $string ) {
71+
if( function_exists( 'pll__' ) ) {
72+
return pll__( $string );
73+
} else {
74+
return $string; // Don't kill anything.
75+
}
76+
}
77+
}
78+
}

readme.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,20 @@ function wplf_recaptcha( $return ) {
185185
return $return;
186186
}
187187
```
188+
189+
### Multilingual
190+
191+
You can create multilingual forms using Polylang. WPLF will register and automatically fetch the translation when you use special template tags.
192+
193+
Example:
194+
```html
195+
<input type="text" placeholder="{{ Test string }}" name="test">
196+
```
197+
198+
You can also disable this feature, and create your own middleware for WPML, if you'd like.
199+
200+
```php
201+
add_filter( 'wplf_load_polylang' , function() {
202+
return false;
203+
} );
204+
```

wp-libre-form.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ private function __construct() {
5656
CPT_WPLF_Form::init();
5757
CPT_WPLF_Submission::init();
5858

59+
add_action( 'after_setup_theme', function() {
60+
if ( apply_filters( 'wplf_load_polylang', true ) ) {
61+
require_once 'classes/class-wplf-polylang.php';
62+
WPLF_Polylang::init();
63+
}
64+
} );
65+
5966
add_action( 'plugins_loaded', array( $this, 'load_our_textdomain' ) );
6067

6168
// flush rewrites on activation since we have slugs for our cpts

0 commit comments

Comments
 (0)