PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.16.7
Translate and Go multilingual – Automatic AI translation – wpLingua v2.16.7
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
← All changes | inc/link-media.php +215 -210 2.12.22.16.7 View file →
@@ -1,210 +1,215 @@
1 -<?php
2 -
3 -// If this file is called directly, abort.
4 -if ( ! defined( 'WPINC' ) ) {
5 - die;
6 -}
7 -
8 -
9 -/**
10 - * Get links and medias entries
11 - *
12 - * @return array
13 - */
14 -function wplng_link_media_get_entries() {
15 -
16 - global $wplng_link_media_entries;
17 -
18 - if ( null !== $wplng_link_media_entries ) {
19 - return $wplng_link_media_entries;
20 - }
21 -
22 - $entries_clear = array();
23 - $entries_json = get_option( 'wplng_link_media_entries' );
24 - $entries = json_decode( $entries_json, true );
25 -
26 - if ( empty( $entries ) || ! is_array( $entries ) ) {
27 - return array();
28 - }
29 -
30 - foreach ( $entries as $entry ) {
31 -
32 - /**
33 - * Get and check the source
34 - */
35 -
36 - if ( ! isset( $entry['source'] )
37 - || ! is_string( $entry['source'] )
38 - || strlen( $entry['source'] ) >= 256
39 - ) {
40 - continue;
41 - }
42 -
43 - $source_clear = esc_attr( $entry['source'] );
44 - $source_clear = str_replace(
45 - '[WPLNG_BACKSLASH]',
46 - '\\',
47 - $source_clear
48 - );
49 -
50 - /**
51 - * Check if rule already exist
52 - */
53 -
54 - $already_in = false;
55 - foreach ( $entries_clear as $entry_clear ) {
56 - if ( $source_clear === $entry_clear['source'] ) {
57 - $already_in = true;
58 - break;
59 - }
60 - }
61 -
62 - if ( $already_in ) {
63 - continue;
64 - }
65 -
66 - /**
67 - * Get and check the modde
68 - */
69 -
70 - $mode_clear = 'exactly';
71 -
72 - if ( ! empty( $entry['mode'] )
73 - && (
74 - 'exactly' === $entry['mode']
75 - || 'partially' === $entry['mode']
76 - || 'regex' === $entry['mode']
77 - )
78 - ) {
79 - $mode_clear = $entry['mode'];
80 - }
81 -
82 - /**
83 - * Get and check the rules
84 - */
85 -
86 - if ( ! isset( $entry['rules'] )
87 - || ! is_array( $entry['rules'] )
88 - ) {
89 - continue;
90 - }
91 -
92 - $rules_clear = array();
93 -
94 - foreach ( $entry['rules'] as $language_id => $rule ) {
95 - if ( ! wplng_is_valid_language_id( $language_id )
96 - || ! is_string( $rule )
97 - || '' === trim( $rule )
98 - || $rule === $source_clear
99 - || strlen( $rule ) >= 256
100 - ) {
101 - continue;
102 - }
103 -
104 - $rules_clear[ $language_id ] = wplng_text_esc( $rule );
105 - }
106 -
107 - /**
108 - * Create the clear entries
109 - */
110 -
111 - if ( empty( $rules_clear ) ) {
112 - continue;
113 - }
114 -
115 - $entries_clear[] = array(
116 - 'source' => $source_clear,
117 - 'mode' => $mode_clear,
118 - 'rules' => $rules_clear,
119 - );
120 - }
121 -
122 - /**
123 - * Sort links and medias entries by sources length
124 - */
125 -
126 - usort(
127 - $entries_clear,
128 - function ( $a, $b ) {
129 - return strlen( $b['source'] ) - strlen( $a['source'] );
130 - }
131 - );
132 -
133 - /**
134 - * Apply wplng_link_media_entries filter
135 - */
136 -
137 - $entries_clear = apply_filters(
138 - 'wplng_link_media_entries',
139 - $entries_clear
140 - );
141 -
142 - $wplng_link_media_entries = $entries_clear;
143 -
144 - return $entries_clear;
145 -}
146 -
147 -
148 -/**
149 - * Get links and medias entries in JSON format
150 - *
151 - * @return string JSON
152 - */
153 -function wplng_link_media_get_entries_json() {
154 - return wp_json_encode(
155 - wplng_link_media_get_entries(),
156 - JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
157 - );
158 -}
159 -
160 -
161 -/**
162 - * Apply media rules to a given URL for a specific language.
163 - *
164 - * @param string $url The URL to which the rules will be applied.
165 - * @param string $language_id The target language ID for which rules should be applied.
166 - * @return string The URL after applying applicable media rules.
167 - */
168 -function wplng_link_media_apply_rules( $url, $language_id ) {
169 -
170 - $entries = wplng_link_media_get_entries();
171 - $url_processed = $url;
172 -
173 - foreach ( $entries as $entry ) {
174 -
175 - if ( ! isset( $entry['rules'][ $language_id ] ) ) {
176 - continue;
177 - }
178 -
179 - switch ( $entry['mode'] ) {
180 - case 'exactly':
181 - if ( $url === $entry['source'] ) {
182 - $url_processed = $entry['rules'][ $language_id ];
183 - }
184 - break;
185 -
186 - case 'partially':
187 - $url_processed = str_replace(
188 - $entry['source'],
189 - $entry['rules'][ $language_id ],
190 - $url
191 - );
192 - break;
193 -
194 - case 'regex':
195 - $url_processed = preg_replace(
196 - '#' . $entry['source'] . '#',
197 - $entry['rules'][ $language_id ],
198 - $url
199 - );
200 - break;
201 - }
202 -
203 - // Apply only one entry rule
204 - if ( $url_processed !== $url ) {
205 - break;
206 - }
207 - }
208 -
209 - return $url_processed;
210 -}
1 +<?php
2 +
3 +// If this file is called directly, abort.
4 +if ( ! defined( 'WPINC' ) ) {
5 + die;
6 +}
7 +
8 +
9 +/**
10 + * Get links and medias entries
11 + *
12 + * @return array
13 + */
14 +function wplng_link_media_get_entries() {
15 +
16 + global $wplng_link_media_entries;
17 +
18 + if ( null !== $wplng_link_media_entries ) {
19 + return $wplng_link_media_entries;
20 + }
21 +
22 + $entries_clear = array();
23 + $entries_json = get_option( 'wplng_link_media_entries' );
24 + $entries = json_decode( $entries_json, true );
25 +
26 + if ( empty( $entries ) || ! is_array( $entries ) ) {
27 + return array();
28 + }
29 +
30 + foreach ( $entries as $entry ) {
31 +
32 + /**
33 + * Get and check the source
34 + */
35 +
36 + if ( ! isset( $entry['source'] )
37 + || ! is_string( $entry['source'] )
38 + || strlen( $entry['source'] ) >= 256
39 + ) {
40 + continue;
41 + }
42 +
43 + $source_clear = esc_attr( $entry['source'] );
44 + $source_clear = str_replace(
45 + '[WPLNG_BACKSLASH]',
46 + '\\',
47 + $source_clear
48 + );
49 +
50 + /**
51 + * Check if rule already exist
52 + */
53 +
54 + $already_in = false;
55 + foreach ( $entries_clear as $entry_clear ) {
56 + if ( $source_clear === $entry_clear['source'] ) {
57 + $already_in = true;
58 + break;
59 + }
60 + }
61 +
62 + if ( $already_in ) {
63 + continue;
64 + }
65 +
66 + /**
67 + * Get and check the modde
68 + */
69 +
70 + $mode_clear = 'exactly';
71 +
72 + if ( ! empty( $entry['mode'] )
73 + && (
74 + 'exactly' === $entry['mode']
75 + || 'partially' === $entry['mode']
76 + || 'regex' === $entry['mode']
77 + )
78 + ) {
79 + $mode_clear = $entry['mode'];
80 + }
81 +
82 + /**
83 + * Get and check the rules
84 + */
85 +
86 + if ( ! isset( $entry['rules'] )
87 + || ! is_array( $entry['rules'] )
88 + ) {
89 + continue;
90 + }
91 +
92 + $rules_clear = array();
93 +
94 + foreach ( $entry['rules'] as $language_id => $rule ) {
95 + if ( ! wplng_is_valid_language_id( $language_id )
96 + || ! is_string( $rule )
97 + || '' === trim( $rule )
98 + || $rule === $source_clear
99 + || strlen( $rule ) >= 256
100 + ) {
101 + continue;
102 + }
103 +
104 + $rules_clear[ $language_id ] = wplng_text_esc( $rule );
105 + }
106 +
107 + /**
108 + * Create the clear entries
109 + */
110 +
111 + if ( empty( $rules_clear ) ) {
112 + continue;
113 + }
114 +
115 + $entries_clear[] = array(
116 + 'source' => $source_clear,
117 + 'mode' => $mode_clear,
118 + 'rules' => $rules_clear,
119 + );
120 + }
121 +
122 + /**
123 + * Sort links and medias entries by sources length
124 + */
125 +
126 + usort(
127 + $entries_clear,
128 + function ( $a, $b ) {
129 + return strlen( $b['source'] ) - strlen( $a['source'] );
130 + }
131 + );
132 +
133 + /**
134 + * Apply wplng_link_media_entries filter
135 + */
136 +
137 + $entries_clear = apply_filters(
138 + 'wplng_link_media_entries',
139 + $entries_clear
140 + );
141 +
142 + $wplng_link_media_entries = $entries_clear;
143 +
144 + return $entries_clear;
145 +}
146 +
147 +
148 +/**
149 + * Get links and medias entries in JSON format
150 + *
151 + * @return string JSON
152 + */
153 +function wplng_link_media_get_entries_json() {
154 + return wp_json_encode(
155 + wplng_link_media_get_entries(),
156 + JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
157 + );
158 +}
159 +
160 +
161 +/**
162 + * Apply media rules to a given URL for a specific language.
163 + *
164 + * @param string $url The URL to which the rules will be applied.
165 + * @param string $language_id The target language ID for which rules should be applied.
166 + * @return string The URL after applying applicable media rules.
167 + */
168 +function wplng_link_media_apply_rules( $url, $language_id ) {
169 +
170 + $entries = wplng_link_media_get_entries();
171 + $url_processed = $url;
172 +
173 + foreach ( $entries as $entry ) {
174 +
175 + if ( ! isset( $entry['rules'][ $language_id ] ) ) {
176 + continue;
177 + }
178 +
179 + switch ( $entry['mode'] ) {
180 + case 'exactly':
181 + if ( $url === $entry['source'] ) {
182 + $url_processed = $entry['rules'][ $language_id ];
183 + }
184 + break;
185 +
186 + case 'partially':
187 + $url_processed = str_replace(
188 + $entry['source'],
189 + $entry['rules'][ $language_id ],
190 + $url
191 + );
192 + break;
193 +
194 + case 'regex':
195 + if ( false !== @preg_match( '#' . $entry['source'] . '#', '' ) ) {
196 + $url_processed = preg_replace(
197 + '#' . $entry['source'] . '#',
198 + $entry['rules'][ $language_id ],
199 + $url
200 + );
201 + if ( null === $url_processed ) {
202 + $url_processed = $url;
203 + }
204 + }
205 + break;
206 + }
207 +
208 + // Apply only one entry rule
209 + if ( $url_processed !== $url ) {
210 + break;
211 + }
212 + }
213 +
214 + return $url_processed;
215 +}