Crypto.php
5 days ago
DB.php
5 days ago
Data.php
5 days ago
Helpers.php
5 days ago
PluginImportDataRetriever.php
5 days ago
UI.php
5 days ago
Helpers.php
219 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPMailSMTP\Helpers; |
| 4 | |
| 5 | use WPMailSMTP\Options; |
| 6 | use WPMailSMTP\WP; |
| 7 | use WP_Error; |
| 8 | |
| 9 | /** |
| 10 | * Class with all the misc helper functions that don't belong elsewhere. |
| 11 | * |
| 12 | * @since 3.0.0 |
| 13 | */ |
| 14 | class Helpers { |
| 15 | |
| 16 | /** |
| 17 | * Check if the current active mailer has email send confirmation functionality. |
| 18 | * |
| 19 | * @since 3.0.0 |
| 20 | * |
| 21 | * @return bool |
| 22 | */ |
| 23 | public static function mailer_without_send_confirmation() { |
| 24 | |
| 25 | return ! in_array( |
| 26 | Options::init()->get( 'mail', 'mailer' ), |
| 27 | [ |
| 28 | 'sendlayer', |
| 29 | 'smtpcom', |
| 30 | 'sendinblue', |
| 31 | 'mailgun', |
| 32 | 'postmark', |
| 33 | 'sparkpost', |
| 34 | 'elasticemail', |
| 35 | 'smtp2go', |
| 36 | 'mailjet', |
| 37 | 'mailersend', |
| 38 | 'mandrill', |
| 39 | 'resend', |
| 40 | ], |
| 41 | true |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Include mbstring polyfill. |
| 47 | * |
| 48 | * @since 3.1.0 |
| 49 | */ |
| 50 | public static function include_mbstring_polyfill() { |
| 51 | |
| 52 | static $included = false; |
| 53 | |
| 54 | if ( $included === true ) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | require_once wp_mail_smtp()->plugin_path . '/vendor_prefixed/symfony/polyfill-mbstring/Mbstring.php'; |
| 59 | require_once wp_mail_smtp()->plugin_path . '/vendor_prefixed/symfony/polyfill-mbstring/bootstrap.php'; |
| 60 | |
| 61 | $included = true; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Test if the REST API is accessible. |
| 66 | * |
| 67 | * @since 3.3.0 |
| 68 | * |
| 69 | * @return true|\WP_Error |
| 70 | */ |
| 71 | public static function test_rest_availability() { |
| 72 | |
| 73 | $headers = [ |
| 74 | 'Cache-Control' => 'no-cache', |
| 75 | ]; |
| 76 | |
| 77 | /** This filter is documented in wp-includes/class-wp-http-streams.php */ |
| 78 | $sslverify = apply_filters( 'https_local_ssl_verify', false ); |
| 79 | |
| 80 | $url = rest_url( 'wp-mail-smtp/v1' ); |
| 81 | |
| 82 | $response = wp_remote_get( |
| 83 | $url, |
| 84 | [ |
| 85 | 'headers' => $headers, |
| 86 | 'sslverify' => $sslverify, |
| 87 | ] |
| 88 | ); |
| 89 | |
| 90 | if ( is_wp_error( $response ) ) { |
| 91 | return $response; |
| 92 | } elseif ( wp_remote_retrieve_response_code( $response ) !== 200 ) { |
| 93 | return new WP_Error( wp_remote_retrieve_response_code( $response ), wp_remote_retrieve_body( $response ) ); |
| 94 | } |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get string size in bytes. |
| 101 | * |
| 102 | * @since 3.4.0 |
| 103 | * |
| 104 | * @param string $str String. |
| 105 | * |
| 106 | * @return int |
| 107 | */ |
| 108 | public static function strsize( $str ) { |
| 109 | |
| 110 | if ( ! function_exists( 'mb_strlen' ) ) { |
| 111 | self::include_mbstring_polyfill(); |
| 112 | } |
| 113 | |
| 114 | return mb_strlen( $str, '8bit' ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Format a variable for debug-style output as an inline <code> block. |
| 119 | * |
| 120 | * Bools and empty values are rendered via var_dump (which shows the type); |
| 121 | * everything else via print_r. Line breaks are stripped so the output fits |
| 122 | * on a single rendered line. |
| 123 | * |
| 124 | * @since 4.9.0 |
| 125 | * |
| 126 | * @param mixed $var Variable to format. |
| 127 | * |
| 128 | * @return string |
| 129 | */ |
| 130 | public static function pvar( $var = '' ) { |
| 131 | |
| 132 | ob_start(); |
| 133 | |
| 134 | echo '<code>'; |
| 135 | |
| 136 | if ( is_bool( $var ) || empty( $var ) ) { |
| 137 | var_dump( $var ); |
| 138 | } else { |
| 139 | print_r( $var ); |
| 140 | } |
| 141 | |
| 142 | echo '</code>'; |
| 143 | |
| 144 | $output = ob_get_clean(); |
| 145 | |
| 146 | return str_replace( [ "\r\n", "\r", "\n" ], '', $output ); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Format error message. |
| 151 | * |
| 152 | * @since 3.4.0 |
| 153 | * |
| 154 | * @param string $message Error message. |
| 155 | * @param string $code Error code. |
| 156 | * @param string $description Error description. |
| 157 | * |
| 158 | * @return string |
| 159 | */ |
| 160 | public static function format_error_message( $message, $code = '', $description = '' ) { |
| 161 | |
| 162 | $error_text = ''; |
| 163 | |
| 164 | if ( ! empty( $code ) ) { |
| 165 | $error_text .= $code . ': '; |
| 166 | } |
| 167 | |
| 168 | if ( ! is_string( $message ) ) { |
| 169 | $error_text .= wp_json_encode( $message ); |
| 170 | } else { |
| 171 | $error_text .= $message; |
| 172 | } |
| 173 | |
| 174 | if ( ! empty( $description ) ) { |
| 175 | $error_text .= WP::EOL . $description; |
| 176 | } |
| 177 | |
| 178 | return $error_text; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Get the default user agent. |
| 183 | * |
| 184 | * @since 3.9.0 |
| 185 | * |
| 186 | * @return string |
| 187 | */ |
| 188 | public static function get_default_user_agent() { |
| 189 | |
| 190 | $license_type = wp_mail_smtp()->get_license_type(); |
| 191 | |
| 192 | return 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ) . '; WPMailSMTP/' . $license_type . '-' . WPMS_PLUGIN_VER; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Import Plugin_Upgrader class from core. |
| 197 | * |
| 198 | * @since 3.11.0 |
| 199 | */ |
| 200 | public static function include_plugin_upgrader() { |
| 201 | |
| 202 | /** \WP_Upgrader class */ |
| 203 | require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 204 | |
| 205 | /** \Plugin_Upgrader class */ |
| 206 | require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php'; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Whether the current request is a WP CLI request. |
| 211 | * |
| 212 | * @since 4.0.0 |
| 213 | */ |
| 214 | public static function is_wp_cli() { |
| 215 | |
| 216 | return defined( 'WP_CLI' ) && WP_CLI; |
| 217 | } |
| 218 | } |
| 219 |