helper.php
158 lines
| 1 | <?php namespace CheckEmail\Util; |
| 2 | |
| 3 | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 4 | |
| 5 | function sanitize_email( $email, $multiple = true ) { |
| 6 | $emails = explode( ',', $email ); |
| 7 | if ( ! $multiple ) { |
| 8 | $emails = array_slice( $emails, 0, 1 ); |
| 9 | } |
| 10 | |
| 11 | $cleaned_emails = array_map( __NAMESPACE__ . '\\wpchill_sanitize_check_email_with_name', $emails ); |
| 12 | |
| 13 | return implode( ', ', $cleaned_emails ); |
| 14 | } |
| 15 | |
| 16 | function wpchill_sanitize_check_email_with_name( $string ) { |
| 17 | $string = trim( $string ); |
| 18 | |
| 19 | $bracket_pos = strpos( $string, '<' ); |
| 20 | if ( false !== $bracket_pos ) { |
| 21 | if ( $bracket_pos > 0 ) { |
| 22 | $name = substr( $string, 0, $bracket_pos ); |
| 23 | $name = trim( $name ); |
| 24 | |
| 25 | $email = substr( $string, $bracket_pos + 1 ); |
| 26 | $email = str_replace( '>', '', $email ); |
| 27 | |
| 28 | return sanitize_text_field( $name ) . ' <' . \sanitize_email( $email ) . '>'; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | return \sanitize_email( $string ); |
| 33 | } |
| 34 | |
| 35 | function wp_chill_check_email_is_admin_non_ajax() { |
| 36 | if ( function_exists( 'wp_doing_ajax' ) && wp_doing_ajax() ) { |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | return is_admin(); |
| 45 | } |
| 46 | |
| 47 | function wp_chill_check_email_array_checked( $values, $current ) { |
| 48 | if ( ! is_array( $values ) ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | if ( in_array( $current, $values, true ) ) { |
| 53 | echo "checked='checked'"; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | function wp_chill_check_email_get_dismiss_icon() { |
| 58 | return '<span class="dashicons dashicons-dismiss"></span>'; |
| 59 | } |
| 60 | |
| 61 | function wp_chill_check_email_get_confirm_icon() { |
| 62 | return '<span class="dashicons dashicons-yes-alt"></span>'; |
| 63 | |
| 64 | } |
| 65 | |
| 66 | function wp_chill_check_email_stringify( $may_be_array, $delimiter = ',' ) { |
| 67 | if ( ! is_array( $may_be_array ) ) { |
| 68 | return (string) $may_be_array; |
| 69 | }else{ |
| 70 | if( isset( $may_be_array[0] ) && is_object( $may_be_array[0] ) && is_a( $may_be_array[0], 'PostmanEmailAddress' ) ){ |
| 71 | return (string) $may_be_array[0]->getEmail(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return implode( $delimiter, $may_be_array ); |
| 76 | } |
| 77 | |
| 78 | function wp_chill_check_email_get_user_defined_date_format() { |
| 79 | return sprintf( '%1$s %2$s', get_option( 'date_format', 'Y-m-d' ), get_option( 'time_format', 'g:i a' ) ); |
| 80 | } |
| 81 | |
| 82 | function wp_chill_check_email_array_get( $array, $key, $default = null ) { |
| 83 | return isset( $array[ $key ] ) ? $array[ $key ] : $default; |
| 84 | } |
| 85 | |
| 86 | function wp_chill_check_email_advanced_search_term( $term ) { |
| 87 | if ( ! is_string( $term ) ) { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | $predicates = wp_chill_check_email_get_advanced_search_term_predicates( $term ); |
| 92 | |
| 93 | return ! empty( $predicates ); |
| 94 | } |
| 95 | |
| 96 | function wp_chill_check_email_get_advanced_search_term_predicates( $term ) { |
| 97 | if ( ! is_string( $term ) ) { |
| 98 | return array(); |
| 99 | } |
| 100 | |
| 101 | $predicates = explode( ' ', $term ); |
| 102 | $predicates_organized = array(); |
| 103 | |
| 104 | foreach ( $predicates as $predicate ) { |
| 105 | $is_match = preg_match( '/(id|email|to|cc|bcc|reply-to):(.*)$/', $predicate, $matches ); |
| 106 | if ( 1 === $is_match ) { |
| 107 | $predicates_organized[ $matches[1] ] = $matches[2]; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return $predicates_organized; |
| 112 | } |
| 113 | |
| 114 | function wp_chill_check_email_get_advanced_search_url() { |
| 115 | $admin_url = get_admin_url( null, 'admin.php?page=check-email-logs' ); |
| 116 | |
| 117 | return add_query_arg( 'check_email_as', 1, $admin_url ); |
| 118 | } |
| 119 | |
| 120 | function wp_chill_check_email_get_column_label_by_db_column( $db_column ) { |
| 121 | return wp_chill_check_email_get_column_label( $db_column ); |
| 122 | } |
| 123 | |
| 124 | function wp_chill_check_email_get_column_label( $column_name ) { |
| 125 | $labels = wp_chill_check_email_get_column_label_map(); |
| 126 | |
| 127 | if ( ! array_key_exists( $column_name, $labels ) ) { |
| 128 | return $column_name; |
| 129 | } |
| 130 | |
| 131 | return $labels[ $column_name ]; |
| 132 | } |
| 133 | |
| 134 | function wp_chill_check_email_get_column_label_map() { |
| 135 | $labels = array( |
| 136 | 'id' => esc_html__( 'ID', 'check-email' ), |
| 137 | 'to_email' => esc_html__( 'To', 'check-email' ), |
| 138 | 'from_email' => esc_html__( 'From', 'check-email' ), |
| 139 | 'subject' => esc_html__( 'Subject', 'check-email' ), |
| 140 | 'message' => esc_html__( 'Message', 'check-email' ), |
| 141 | 'attachments' => esc_html__( 'Attachment', 'check-email' ), |
| 142 | 'sent_date' => esc_html__( 'Sent at', 'check-email' ), |
| 143 | 'from' => esc_html__( 'From', 'check-email' ), |
| 144 | 'cc' => esc_html__( 'CC', 'check-email' ), |
| 145 | 'bcc' => esc_html__( 'BCC', 'check-email' ), |
| 146 | 'reply_to' => esc_html__( 'Reply To', 'check-email' ), |
| 147 | 'ip_address' => esc_html__( 'Host IP', 'check-email' ), |
| 148 | 'result' => esc_html__( 'Sent Status', 'check-email' ), |
| 149 | 'content' => esc_html__( 'Content', 'check-email' ), |
| 150 | 'created_at' => esc_html__( 'Date', 'check-email' ), |
| 151 | 'initiator' => esc_html__( 'Source', 'check-email' ), |
| 152 | 'check_email_log_id' => esc_html__( 'Log ID', 'check-email' ), |
| 153 | 'action' => esc_html__( 'Action', 'check-email' ), |
| 154 | ); |
| 155 | // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 156 | return apply_filters( 'check_email_db_column_labels', $labels ); |
| 157 | } |
| 158 |