| @@ -1,0 +1,293 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Functions | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Functions | |
| 6 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Generates the required HTML with the dashicon provided | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + * | |
| 17 | + * @param string $dashicon Dashicon class | |
| 18 | + * @param string $tag Optional, tag used (recommended i or span) | |
| 19 | + * | |
| 20 | + * @return string | |
| 21 | + */ | |
| 22 | +function automatorwp_dashicon( $dashicon = 'automatorwp', $tag = 'i' ) { | |
| 23 | + | |
| 24 | + return '<' . $tag . ' class="dashicons dashicons-' . $dashicon . '"></' . $tag . '>'; | |
| 25 | + | |
| 26 | +} | |
| 27 | + | |
| 28 | +/** | |
| 29 | + * Helper function to send an email | |
| 30 | + * | |
| 31 | + * @since 1.4.0 | |
| 32 | + * | |
| 33 | + * @param array $args Arguments passed to this function. | |
| 34 | + * Note: Pass additional args to be used in the function's filters | |
| 35 | + * | |
| 36 | + * @return bool Whether the email contents were sent successfully. | |
| 37 | + */ | |
| 38 | +function automatorwp_send_email( $args = array() ) { | |
| 39 | + | |
| 40 | + global $automatorwp_email_args; | |
| 41 | + | |
| 42 | + // Parse the email required args | |
| 43 | + $email = wp_parse_args( $args, array( | |
| 44 | + 'from' => '', | |
| 45 | + 'from_name' => '', | |
| 46 | + 'to' => '', | |
| 47 | + 'cc' => '', | |
| 48 | + 'bcc' => '', | |
| 49 | + 'subject' => '', | |
| 50 | + 'message' => '', | |
| 51 | + 'headers' => array(), | |
| 52 | + 'attachments' => array(), | |
| 53 | + ) ); | |
| 54 | + | |
| 55 | + // Initialize from | |
| 56 | + if( empty( $email['from'] ) ) { | |
| 57 | + $email['from'] = get_bloginfo( 'admin_email' ); | |
| 58 | + } | |
| 59 | + | |
| 60 | + // Initialize from name | |
| 61 | + if( empty( $email['from_name'] ) ) { | |
| 62 | + $email['from_name'] = get_bloginfo( 'name' ); | |
| 63 | + } | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * Filter available to override the email arguments before process them | |
| 67 | + * | |
| 68 | + * @since 1.4.0 | |
| 69 | + * | |
| 70 | + * @param array $email The email arguments | |
| 71 | + * @param array $args The original arguments received | |
| 72 | + * | |
| 73 | + * @return array | |
| 74 | + */ | |
| 75 | + $email = apply_filters( 'automatorwp_pre_email_args', $email, $args ); | |
| 76 | + | |
| 77 | + // Setup subject | |
| 78 | + $email['subject'] = do_shortcode( $email['subject'] ); | |
| 79 | + | |
| 80 | + // Setup message | |
| 81 | + $email['message'] = wpautop( $email['message'] ); | |
| 82 | + $email['message'] = do_shortcode( $email['message'] ); | |
| 83 | + | |
| 84 | + // Setup headers | |
| 85 | + if( ! is_array( $email['headers'] ) ) { | |
| 86 | + $email['headers'] = array(); | |
| 87 | + } | |
| 88 | + | |
| 89 | + if( ! empty( $email['from'] ) ) { | |
| 90 | + $email['headers'][] = 'From: <' . $email['from'] . '>'; | |
| 91 | + } | |
| 92 | + | |
| 93 | + if ( ! empty( $email['cc'] ) ) { | |
| 94 | + $email['headers'][] = 'Cc: ' . $email['cc']; | |
| 95 | + } | |
| 96 | + | |
| 97 | + if ( ! empty( $email['bcc'] ) ) { | |
| 98 | + $email['headers'][] = 'Bcc: ' . $email['bcc']; | |
| 99 | + } | |
| 100 | + | |
| 101 | + $email['headers'][] = 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ); | |
| 102 | + | |
| 103 | + // Setup attachments | |
| 104 | + if( ! is_array( $email['attachments'] ) ) { | |
| 105 | + $email['attachments'] = array(); | |
| 106 | + } | |
| 107 | + | |
| 108 | + /** | |
| 109 | + * Filter available to override the email arguments after process them | |
| 110 | + * | |
| 111 | + * @since 1.4.0 | |
| 112 | + * | |
| 113 | + * @param array $email The email arguments | |
| 114 | + * @param array $args The original arguments received | |
| 115 | + * | |
| 116 | + * @return array | |
| 117 | + */ | |
| 118 | + $email = apply_filters( 'automatorwp_email_args', $email, $args ); | |
| 119 | + | |
| 120 | + $automatorwp_email_args = $email; | |
| 121 | + | |
| 122 | + add_filter( 'wp_mail_from', 'automatorwp_get_email_from_address' ); | |
| 123 | + add_filter( 'wp_mail_from_name', 'automatorwp_get_email_from_name' ); | |
| 124 | + add_filter( 'wp_mail_content_type', 'automatorwp_set_html_content_type' ); | |
| 125 | + | |
| 126 | + // Send the email | |
| 127 | + $result = wp_mail( $email['to'], $email['subject'], $email['message'], $email['headers'], $email['attachments'] ); | |
| 128 | + | |
| 129 | + remove_filter( 'wp_mail_from', 'automatorwp_get_email_from_address' ); | |
| 130 | + remove_filter( 'wp_mail_from_name', 'automatorwp_get_email_from_name' ); | |
| 131 | + remove_filter( 'wp_mail_content_type', 'automatorwp_set_html_content_type' ); | |
| 132 | + | |
| 133 | + /** | |
| 134 | + * Filter available to decide to log email errors or not | |
| 135 | + * | |
| 136 | + * @since 1.4.0 | |
| 137 | + * | |
| 138 | + * @param bool $log_errors Whatever to log email errors or not, by default true | |
| 139 | + * @param array $email The email arguments | |
| 140 | + * @param array $args The original arguments received | |
| 141 | + * | |
| 142 | + * @return bool | |
| 143 | + */ | |
| 144 | + $log_errors = apply_filters( 'automatorwp_log_email_errors', true, $email, $args ); | |
| 145 | + | |
| 146 | + if( ! $result && $log_errors === true) { | |
| 147 | + $log_message = sprintf( | |
| 148 | + __( "[AutomatorWP] Email failed to send to %s with subject: %s", 'autoamtorwp' ), | |
| 149 | + ( is_array( $email['to'] ) ? implode( ',', $email['to'] ) : $email['to'] ), | |
| 150 | + $email['subject'] | |
| 151 | + ); | |
| 152 | + | |
| 153 | + error_log( $log_message ); | |
| 154 | + } | |
| 155 | + | |
| 156 | + return $result; | |
| 157 | + | |
| 158 | +} | |
| 159 | + | |
| 160 | +/** | |
| 161 | + * Function to get the mail from email address | |
| 162 | + * | |
| 163 | + * @since 1.0.0 | |
| 164 | + * | |
| 165 | + * @param string $from | |
| 166 | + * | |
| 167 | + * @return string | |
| 168 | + */ | |
| 169 | +function automatorwp_get_email_from_address( $from = '' ) { | |
| 170 | + | |
| 171 | + global $automatorwp_email_args; | |
| 172 | + | |
| 173 | + if( is_array( $automatorwp_email_args ) && isset( $automatorwp_email_args['from'] ) ) { | |
| 174 | + return sanitize_email( $automatorwp_email_args['from'] ); | |
| 175 | + } | |
| 176 | + | |
| 177 | + return $from; | |
| 178 | + | |
| 179 | +} | |
| 180 | + | |
| 181 | +/** | |
| 182 | + * Function to get the mail from name | |
| 183 | + * | |
| 184 | + * @since 1.0.0 | |
| 185 | + * | |
| 186 | + * @param string $from_name | |
| 187 | + * | |
| 188 | + * @return string | |
| 189 | + */ | |
| 190 | +function automatorwp_get_email_from_name( $from_name = '' ) { | |
| 191 | + | |
| 192 | + global $automatorwp_email_args; | |
| 193 | + | |
| 194 | + if( is_array( $automatorwp_email_args ) && isset( $automatorwp_email_args['from_name'] ) ) { | |
| 195 | + return wp_specialchars_decode( $automatorwp_email_args['from_name'] ); | |
| 196 | + } | |
| 197 | + | |
| 198 | + return $from_name; | |
| 199 | + | |
| 200 | +} | |
| 201 | + | |
| 202 | +/** | |
| 203 | + * Function to set the mail content type | |
| 204 | + * | |
| 205 | + * @since 1.0.0 | |
| 206 | + * | |
| 207 | + * @param string $content_type | |
| 208 | + * | |
| 209 | + * @return string | |
| 210 | + */ | |
| 211 | +function automatorwp_set_html_content_type( $content_type = 'text/html' ) { | |
| 212 | + return 'text/html'; | |
| 213 | +} | |
| 214 | + | |
| 215 | +/** | |
| 216 | + * Helper function to get the date format | |
| 217 | + * | |
| 218 | + * @since 1.0.0 | |
| 219 | + * | |
| 220 | + * @param array $allowed_formats | |
| 221 | + * | |
| 222 | + * @return string | |
| 223 | + */ | |
| 224 | +function automatorwp_get_date_format( $allowed_formats = array(), $default_format = 'Y-m-d' ) { | |
| 225 | + | |
| 226 | + $format = $default_format; | |
| 227 | + | |
| 228 | + $option = get_option( 'date_format' ); | |
| 229 | + | |
| 230 | + if( empty( $allowed_formats ) ) { | |
| 231 | + $allowed_formats = array( 'Y-m-d', 'm/d/Y', 'd/m/Y', 'd.m.Y' ); | |
| 232 | + } | |
| 233 | + | |
| 234 | + if( in_array( $option, $allowed_formats ) ) { | |
| 235 | + $format = $option; | |
| 236 | + } | |
| 237 | + | |
| 238 | + return $format; | |
| 239 | + | |
| 240 | +} | |
| 241 | + | |
| 242 | +/** | |
| 243 | + * Helper function to get the time format | |
| 244 | + * | |
| 245 | + * @since 1.0.0 | |
| 246 | + * | |
| 247 | + * @param array $allowed_formats | |
| 248 | + * | |
| 249 | + * @return string | |
| 250 | + */ | |
| 251 | +function automatorwp_get_time_format( $allowed_formats = array() ) { | |
| 252 | + | |
| 253 | + $format = 'H:i'; | |
| 254 | + | |
| 255 | + $option = get_option( 'time_format' ); | |
| 256 | + | |
| 257 | + if( empty( $allowed_formats ) ) { | |
| 258 | + $allowed_formats = array( 'g:i a', 'g:i A', 'H:i' ); | |
| 259 | + } | |
| 260 | + | |
| 261 | + if( in_array( $option, $allowed_formats ) ) { | |
| 262 | + $format = $option; | |
| 263 | + } | |
| 264 | + | |
| 265 | + return $format; | |
| 266 | + | |
| 267 | +} | |
| 268 | + | |
| 269 | +/** | |
| 270 | + * Get timestamp from text date | |
| 271 | + * | |
| 272 | + * @since 2.2.2 | |
| 273 | + * | |
| 274 | + * @param string $value Date value. | |
| 275 | + * @param string $format Expected date format. | |
| 276 | + * | |
| 277 | + * @return mixed Unix timestamp representing the date. | |
| 278 | + */ | |
| 279 | +function automatorwp_get_timestamp_from_value( $value, $format = 'Y-m-d' ) { | |
| 280 | + | |
| 281 | + // Create a new date object from the given format | |
| 282 | + $date_object = date_create_from_format( $format, $value ); | |
| 283 | + | |
| 284 | + // get the timestamp from the date object | |
| 285 | + $timestamp = ( $date_object ? $date_object->getTimeStamp() : strtotime( $value ) ); | |
| 286 | + | |
| 287 | + // Ensure to make a valid timestamp | |
| 288 | + if ( empty( $timestamp ) && CMB2_Utils::is_valid_date( $value ) ) { | |
| 289 | + $timestamp = CMB2_Utils::make_valid_time_stamp( $value ); | |
| 290 | + } | |
| 291 | + | |
| 292 | + return $timestamp; | |
| 293 | +} | |