class-email-encoder-bundle-ajax.php
3 months ago
class-email-encoder-bundle-helpers.php
3 months ago
class-email-encoder-bundle-settings.php
2 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 string|array< string > $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], $content[1] ); |
| 73 | } else { |
| 74 | $validated_content = $content; |
| 75 | } |
| 76 | |
| 77 | ob_start(); |
| 78 | ?> |
| 79 | <div class="notice <?php echo esc_attr( $notice ); ?> <?php echo esc_attr( $isit ); ?>"> |
| 80 | <p><?php echo wp_kses_post( $validated_content ); ?></p> |
| 81 | </div> |
| 82 | <?php |
| 83 | $res = ob_get_clean(); |
| 84 | |
| 85 | return (string) $res; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Formats a specific date to datetime |
| 90 | * |
| 91 | * @param non-empty-string $date |
| 92 | * @return \DateTime |
| 93 | */ |
| 94 | public function get_datetime( $date ) { |
| 95 | $date_new = gmdate( 'Y-m-d H:i:s', (int) 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 string $url - the default url to set the params to |
| 105 | * @param array< string, string > $args - the available args |
| 106 | * @return string - the url |
| 107 | */ |
| 108 | public function built_url( string $url, array $args = [] ): string { |
| 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 string $url - the url |
| 120 | * @return array< mixed > - the parameters of the url |
| 121 | */ |
| 122 | public function get_parameters_from_url( $url ) { |
| 123 | |
| 124 | $parts = wp_parse_url( $url ); |
| 125 | $url_parameter = []; |
| 126 | |
| 127 | if ( ! empty( $parts['query'] ) ) { |
| 128 | parse_str( $parts['query'], $url_parameter ); |
| 129 | } |
| 130 | |
| 131 | return empty( $url_parameter ) ? [] : $url_parameter; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Builds an url out of the main values |
| 136 | * |
| 137 | * @param bool $with_args |
| 138 | * @return string |
| 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 $content |
| 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 ( wp_rand( 0, 1 ) == 1 ) ? true : false; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Better attribute parsing for HTML strings |
| 185 | * |
| 186 | * @since 2.1.4 |
| 187 | * @param string $text |
| 188 | * @return mixed Array on success, empty string otherwise |
| 189 | */ |
| 190 | public function parse_html_attributes( string $text ) { |
| 191 | |
| 192 | $attributes = shortcode_parse_atts( $text ); |
| 193 | |
| 194 | // if( is_array( $attributes ) ) { |
| 195 | foreach( $attributes as $ak => $av ){ |
| 196 | |
| 197 | //Check if a given string contains an @ as this breaks attributes by default |
| 198 | $ident = '@'; |
| 199 | if( substr( $av, 0, strlen( $ident ) ) === $ident ){ |
| 200 | $validated_attribute = substr( $av, strlen( $ident ) ); |
| 201 | $new_attr = shortcode_parse_atts( $validated_attribute ); |
| 202 | |
| 203 | // if( is_array( $new_attr ) ){ |
| 204 | foreach( $new_attr as $nak => $nav ){ |
| 205 | |
| 206 | $index = array_search( $ak , array_keys( $attributes ) ); |
| 207 | |
| 208 | // Create a new array with the updated key and value. |
| 209 | $new_array = []; |
| 210 | $i = 0; |
| 211 | foreach( $attributes as $key => $value ) { |
| 212 | if ($i == $index) { |
| 213 | $new_key = $ident . $nak; |
| 214 | $new_array[ $new_key ] = $nav; |
| 215 | } else { |
| 216 | $new_array[$key] = $value; |
| 217 | } |
| 218 | $i++; |
| 219 | } |
| 220 | |
| 221 | $attributes = $new_array; |
| 222 | } |
| 223 | // } |
| 224 | } |
| 225 | |
| 226 | } |
| 227 | // } |
| 228 | |
| 229 | return apply_filters( 'eeb/helpers/parse_html_attributes', $attributes, $text ); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Sanitize a string of HTML attributes |
| 234 | * |
| 235 | * @since 2.1.9 |
| 236 | * @param string $extra_attrs |
| 237 | * @return string |
| 238 | */ |
| 239 | public function sanitize_html_attributes( $extra_attrs ){ |
| 240 | |
| 241 | $allowed_attrs = [ 'href', 'title', 'rel', 'class', 'id', 'style', 'target' ]; |
| 242 | |
| 243 | // Use a regular expression to match attributes and their values |
| 244 | preg_match_all('/(\w+)=("[^"]*"|\'[^\']*\')/', $extra_attrs, $matches, PREG_SET_ORDER); |
| 245 | |
| 246 | $sanitized_attrs = []; |
| 247 | |
| 248 | foreach ( $matches as $match ) { |
| 249 | |
| 250 | //Skip undefined arguments |
| 251 | if( ! in_array( $match[1], $allowed_attrs ) ){ |
| 252 | continue; |
| 253 | } |
| 254 | |
| 255 | // $match[1] is the attribute name, $match[2] is the attribute value including quotes |
| 256 | $sanitized_name = sanitize_key( $match[1] ); // Sanitize the attribute name |
| 257 | $sanitized_value = esc_attr( trim( $match[2], '"\'' ) ); // 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 |