PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 1.0.3
Translate and Go multilingual – Automatic AI translation – wpLingua v1.0.3
2.16.7 2.16.6 2.16.5 2.16.4 2.16.3 2.16.2 2.16.1 2.16.0 2.15.2 2.15.1 2.15.0 2.14.3 2.14.2 2.14.1 2.14.0 2.13.1 2.13.0 2.12.3 2.12.2 2.12.1 trunk 1.0.3 1.0.4 1.0.5 1.1.0 All 112 releases
wplingua / inc / util.php

util.php in Translate and Go multilingual – Automatic AI translation – wpLingua 1.0.3, at inc/util.php

164 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // If this file is called directly, abort.
4 if ( ! defined( 'WPINC' ) ) {
5 die;
6 }
7
8
9 /**
10 * Return true is $str is an URL
11 *
12 * @param string $str
13 * @return bool
14 */
15 function wplng_str_is_url( $str ) {
16
17 $is_url = false;
18
19 if ( '' !== wp_parse_url( $str, PHP_URL_SCHEME ) ) {
20 // URL has http/https/...
21 $is_url = ! ( filter_var( $str, FILTER_VALIDATE_URL ) === false );
22 } else {
23 // PHP filter_var does not support relative urls, so we simulate a full URL
24 $is_url = ! ( filter_var( 'https://website.com/' . ltrim( $str, '/' ), FILTER_VALIDATE_URL ) === false );
25 }
26
27 if ( ! $is_url ) {
28 $is_url = esc_url( $str ) === $str;
29 }
30
31 return $is_url;
32 }
33
34
35 /**
36 * Return true is $str is a translatable text
37 * Return false if $str is a number, mail addredd, symbol, ...
38 *
39 * @param string $text
40 * @return bool
41 */
42 function wplng_text_is_translatable( $text ) {
43
44 // Check if it's a mail address
45 if ( filter_var( $text, FILTER_VALIDATE_EMAIL ) ) {
46 return false;
47 }
48
49 // Get letters only
50 $letters = $text;
51 $letters = html_entity_decode( $letters );
52 $letters = preg_replace( '#[^\p{L}\p{N}]#u', '', $letters );
53 $letters = preg_replace( '#[\d\s]#u', '', $letters );
54
55 return ! empty( $letters );
56 }
57
58
59 /**
60 * Escape texte (used for comparison)
61 *
62 * @param string $text
63 * @return string
64 */
65 function wplng_text_esc( $text ) {
66
67 $text = html_entity_decode( $text );
68 $text = esc_html( $text );
69 $text = esc_attr( $text );
70 $text = str_replace( '\\', '', $text );
71 $text = preg_replace( '#\s+#', ' ', $text );
72 $text = trim( $text );
73
74 return $text;
75 }
76
77
78 /**
79 * Return true if $str is HTML
80 *
81 * @param string $str
82 * @return string
83 */
84 function wplng_str_is_html( $str ) {
85 return $str !== wp_strip_all_tags( $str );
86 }
87
88
89 /**
90 * Return true is $str is a JSON
91 *
92 * @param string $str
93 * @return string
94 */
95 function wplng_str_is_json( $str ) {
96 return ( json_decode( $str ) == null ) ? false : true;
97 }
98
99
100 /**
101 * Return true if $str is a local ID
102 * Ex: fr_FR, fr, FR, ...
103 *
104 * @param string $str
105 * @return bool
106 */
107 function wplng_str_is_locale_id( $str ) {
108
109 $locale = get_locale();
110 $locales = array(
111 $locale, // Ex: fr_FR
112 strtolower( $locale ), // Ex: fr_fr
113 str_replace( '_', '-', $locale ), // Ex: fr-FR
114 strtolower( str_replace( '_', '-', $locale ) ), // Ex: fr-fr
115 substr( $locale, 0, 2 ), // Ex: FR
116 strtolower( substr( $locale, 0, 2 ) ), // Ex: fr
117 );
118
119 return in_array( $str, $locales );
120 }
121
122
123 /**
124 * Return true if a JSON string element is translatable
125 *
126 * @param string $element
127 * @param array $parents
128 * @return bool
129 */
130 function wplng_json_element_is_translatable( $element, $parents ) {
131
132 $is_translatable = false;
133 $json_excluded = wplng_data_excluded_json();
134 $json_to_translate = wplng_data_json_to_translate();
135
136 if ( in_array( $parents, $json_excluded ) ) {
137 $is_translatable = false;
138 } elseif ( in_array( $parents, $json_to_translate ) ) {
139 $is_translatable = true;
140 } else {
141
142 // Is schema-graph
143 if (
144 ! empty( $parents[0] )
145 && $parents[0] === '@graph'
146 && count( $parents ) > 0
147 && $parents[ count( $parents ) - 1 ] === 'name'
148 ) {
149 $is_translatable = true;
150 }
151
152 if ( ! wplng_text_is_translatable( $element ) ) {
153 $is_translatable = false;
154 }
155 }
156
157 return apply_filters(
158 'wplng_json_element_is_translatable',
159 $is_translatable,
160 $element,
161 $parents
162 );
163 }
164