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