|
| 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 | +} |
0 commit comments