PluginProbe ʕ •ᴥ•ʔ
Email Encoder – Protect Email Addresses and Phone Numbers / 2.3.6
Email Encoder – Protect Email Addresses and Phone Numbers v2.3.6
2.5.3 2.5.2 2.5.1 2.5.0 2.4.8 trunk 0.10 0.11 0.12 0.20 0.21 0.22 0.30 0.31 0.32 0.40 0.41 0.42 0.50 0.60 0.70 0.71 0.80 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.5 1.5.2 1.51 1.53 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.10 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7
email-encoder-bundle / core / includes / classes / class-email-encoder-bundle-helpers.php
email-encoder-bundle / core / includes / classes Last commit date
class-email-encoder-bundle-ajax.php 8 months ago class-email-encoder-bundle-helpers.php 8 months ago class-email-encoder-bundle-settings.php 8 months ago class-email-encoder-bundle-validate.php 8 months ago index.php 6 years ago
class-email-encoder-bundle-helpers.php
270 lines
1 <?php
2
3 namespace Legacy\EmailEncoderBundle;
4
5 class Email_Encoder_Helpers {
6
7 /**
8 * Checks if the parsed param is available on the current site
9 *
10 * @param $param
11 * @return bool
12 */
13 public function is_page( ?string $param ): bool {
14 if( empty( $param ) ){
15 return false;
16 }
17
18 if( isset( $_GET['page'] ) ) {
19 if( $_GET['page'] == $param ) {
20 return true;
21 }
22 }
23
24 return false;
25 }
26
27 /**
28 * Creates a formatted admin notice
29 *
30 * @param $content - notice content
31 * @param string $type - Status of the specified notice
32 * @param bool $is_dismissible - If the message should be dismissible
33 * @return string - The formatted admin notice
34 */
35 public function create_admin_notice( $content, $type = 'info', $is_dismissible = true ) {
36 if( empty( $content ) )
37 return '';
38
39 /**
40 * Block an admin notice based onn the specified values
41 */
42 $throwit = apply_filters( 'eeb/helpers/throw_admin_notice', true, $content, $type, $is_dismissible );
43 if( ! $throwit )
44 return '';
45
46 if( $is_dismissible !== true ) {
47 $isit = '';
48 } else {
49 $isit = 'is-dismissible';
50 }
51
52
53 switch( $type ) {
54 case 'info':
55 $notice = 'notice-info';
56 break;
57 case 'success':
58 $notice = 'notice-success';
59 break;
60 case 'warning':
61 $notice = 'notice-warning';
62 break;
63 case 'error':
64 $notice = 'notice-error';
65 break;
66 default:
67 $notice = 'notice-info';
68 break;
69 }
70
71 if( is_array( $content ) ) {
72 $validated_content = sprintf( __( $content[0], 'email-encoder-bundle' ), $content[1] );
73 } else {
74 $validated_content = __( $content, 'email-encoder-bundle' );
75 }
76
77 ob_start();
78 ?>
79 <div class="notice <?php echo $notice; ?> <?php echo $isit; ?>">
80 <p><?php echo $validated_content; ?></p>
81 </div>
82 <?php
83 $res = ob_get_clean();
84
85 return $res;
86 }
87
88 /**
89 * Formats a specific date to datetime
90 *
91 * @param $date
92 * @return DateTime
93 */
94 public function get_datetime( $date ) {
95 $date_new = date( 'Y-m-d H:i:s', strtotime( $date ) );
96 $date_new_formatted = new DateTime( $date_new );
97
98 return $date_new_formatted;
99 }
100
101 /**
102 * Builds an url out of the mai values
103 *
104 * @param $url - the default url to set the params to
105 * @param $args - the available args
106 * @return string - the url
107 */
108 public function built_url( $url, $args ) {
109 if( ! empty( $args ) ) {
110 $url .= '?' . http_build_query( $args );
111 }
112
113 return $url;
114 }
115
116 /**
117 * Get Parameters from URL string
118 *
119 * @param $url - the url
120 *
121 * @return array - the parameters of the url
122 */
123 public function get_parameters_from_url( $url ) {
124
125 $parts = parse_url( $url );
126
127 parse_str( $parts['query'], $url_parameter );
128
129 return empty( $url_parameter ) ? array() : $url_parameter;
130
131 }
132
133 /**
134 * Builds an url out of the main values
135 *
136 * @param $url - the default url to set the params to
137 * @param $args - the available args
138 * @return string - the url
139 */
140 public function get_current_url( $with_args = true ) {
141
142 $current_url = ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' ) ? 'https://' : 'http://';
143
144 $host_part = $_SERVER['SERVER_NAME'];
145 if( strpos( $host_part, $_SERVER['HTTP_HOST'] ) === false ) {
146
147 //Validate against HTTP_HOST in case SERVER_NAME has no "www" set
148 if( strpos( $_SERVER['HTTP_HOST'], '://www.' ) !== false && strpos( $host_part, '://www.' ) === false ) {
149 $host_part = str_replace( '://', '://www.', $host_part );
150 }
151
152 }
153
154 $current_url .= sanitize_text_field( $host_part ) . sanitize_text_field( $_SERVER['REQUEST_URI'] );
155
156 if( $with_args ) {
157 return $current_url;
158 } else {
159 return strtok( $current_url, '?' );
160 }
161 }
162
163 /**
164 * This is the opponent of JavaScripts decodeURIComponent()
165 * @link http://stackoverflow.com/questions/1734250/what-is-the-equivalent-of-javascripts-encodeuricomponent-in-php
166 * @param string $str
167 * @return string
168 */
169 public function encode_uri_components( $content ) {
170 $revert = array( '%21'=>'!', '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')' );
171 return strtr( rawurlencode( $content ), $revert );
172 }
173
174 /**
175 * Generate a random bool value
176 *
177 * @return bool
178 */
179 public function get_random_bool() {
180 return ( rand( 0, 1 ) == 1 ) ? true : false;
181 }
182
183 /**
184 * Better attribute parsing for HTML strings
185 *
186 * @since 2.1.4
187 * @return mixed Array on success, empty string otherwise
188 */
189 public function parse_html_attributes( $text ) {
190
191 $attributes = shortcode_parse_atts( $text );
192
193 if( is_array( $attributes ) ) {
194 foreach( $attributes as $ak => $av ){
195
196 //Check if a given string contains an @ as this breaks attributes by default
197 $ident = '@';
198 if( substr( $av, 0, strlen( $ident ) ) === $ident ){
199 $validated_attribute = substr( $av, strlen( $ident ) );
200 $new_attr = shortcode_parse_atts( $validated_attribute );
201
202 if( is_array( $new_attr ) ){
203 foreach( $new_attr as $nak => $nav ){
204
205 $index = array_search( $ak , array_keys( $attributes ) );
206
207 // Create a new array with the updated key and value.
208 $new_array = array();
209 $i = 0;
210 foreach( $attributes as $key => $value ) {
211 if ($i == $index) {
212 $new_key = $ident . $nak;
213 $new_array[ $new_key ] = $nav;
214 } else {
215 $new_array[$key] = $value;
216 }
217 $i++;
218 }
219
220 $attributes = $new_array;
221 }
222 }
223 }
224
225 }
226 }
227
228 return apply_filters( 'eeb/helpers/parse_html_attributes', $attributes, $text );
229 }
230
231 /**
232 * Sanitize a string of HTML attributes
233 *
234 * @since 2.1.9
235 * @param string $extra_attrs
236 * @return string
237 */
238 public function sanitize_html_attributes( $extra_attrs ){
239
240 $allowed_attrs = [ 'href', 'title', 'rel', 'class', 'id', 'style', 'target' ];
241
242 // Use a regular expression to match attributes and their values
243 preg_match_all('/(\w+)=("[^"]*"|\'[^\']*\')/', $extra_attrs, $matches, PREG_SET_ORDER);
244
245 $sanitized_attrs = array();
246
247 foreach ( $matches as $match ) {
248
249 //Skip undefined arguments
250 if( ! in_array( $match[1], $allowed_attrs ) ){
251 continue;
252 }
253
254 // $match[1] is the attribute name, $match[2] is the attribute value including quotes
255 $sanitized_name = sanitize_key( $match[1] ); // Sanitize the attribute name
256 $sanitized_value = esc_attr( trim( $match[2], '"\'' ) ); // Remove quotes and escape the value
257 $sanitized_value = str_replace( '\\', '', $sanitized_value ); // Remove quotes and escape the value
258
259 // Reconstruct the attribute
260 $sanitized_attrs[] = $sanitized_name . '="' . $sanitized_value . '"';
261 }
262
263 // Join the sanitized attributes back into a string
264 $sanitized_extra_attrs = implode( ' ', $sanitized_attrs );
265
266 return apply_filters( 'eeb/helpers/sanitize_html_attributes', $sanitized_extra_attrs, $sanitized_attrs, $extra_attrs );
267 }
268
269 }
270