| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || exit; |
| 4 |
|
| 5 |
function dfrapi_email_user_about_usage() { |
| 6 |
|
| 7 |
$percentage = dfrapi_get_api_usage_as_percentage( 0 ); |
| 8 |
$status = get_option( 'dfrapi_account', array() ); |
| 9 |
|
| 10 |
$request_count = ( isset( $status['request_count'] ) ) ? abs( $status['request_count'] ) : 0; |
| 11 |
$remaining_requests = ( isset( $status['max_requests'] ) ) ? abs( $status['max_requests'] - $request_count ) : 0; |
| 12 |
|
| 13 |
$reset_date = ''; |
| 14 |
if ( isset( $status['bill_day'] ) ) { |
| 15 |
$today = date( 'j' ); |
| 16 |
$num_days = date( 't' ); |
| 17 |
if ( $status['bill_day'] > $num_days ) { |
| 18 |
$bill_day = $num_days; |
| 19 |
} else { |
| 20 |
$bill_day = $status['bill_day']; |
| 21 |
} |
| 22 |
if ( $bill_day == 0 ) { |
| 23 |
$reset_date .= '<em>' . __( 'Never', 'datafeedr-api' ) . '</em>'; |
| 24 |
} elseif ( $today >= $bill_day ) { |
| 25 |
$reset_date .= date( 'F', strtotime( '+1 month' ) ) . ' ' . $bill_day . ', ' . date( 'Y', strtotime( '+1 month' ) ); |
| 26 |
} else { |
| 27 |
$reset_date .= date( 'F' ) . ' ' . $bill_day . ', ' . date( 'Y' ); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
$default = array( |
| 32 |
'90_percent' => '', |
| 33 |
'100_percent' => '' |
| 34 |
); |
| 35 |
|
| 36 |
// Don't do anything if less than 90%. |
| 37 |
if ( $percentage < 90 ) { |
| 38 |
update_option( 'dfrapi_usage_notification_tracker', $default ); |
| 39 |
|
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
$tracker = get_option( 'dfrapi_usage_notification_tracker', $default ); |
| 44 |
|
| 45 |
$params = array(); |
| 46 |
$params['to'] = get_bloginfo( 'admin_email' ); |
| 47 |
$params['message'] = "<p>" . __( "This is an automated message generated by: ", 'datafeedr-api' ) . get_bloginfo( 'wpurl' ) . "</p>"; |
| 48 |
|
| 49 |
if ( $percentage >= 100 && empty( $tracker['100_percent'] ) ) { |
| 50 |
|
| 51 |
$params['subject'] = get_bloginfo( 'name' ) . __( ': Datafeedr API Usage (Critical)', 'datafeedr-api' ); |
| 52 |
|
| 53 |
$params['message'] .= "<p>" . __( "You have used <strong>100%</strong> of your allocated Datafeedr API requests for this period. <u>You are no longer able to query the Datafeedr API to get product information.</u>", 'datafeedr-api' ) . "</p>"; |
| 54 |
$params['message'] .= "<p><strong>" . __( "What to do next?", 'datafeedr-api' ) . "</strong></p>"; |
| 55 |
$params['message'] .= "<p>" . __( "We strongly recommend that you upgrade to prevent your product information from becoming outdated.", 'datafeedr-api' ) . "</p>"; |
| 56 |
$params['message'] .= "<p><a href=\"" . dfrapi_user_pages( 'change' ) . "?utm_source=email&utm_medium=link&utm_campaign=upgrade100percentnotice\"><strong>" . __( "UPGRADE NOW", 'datafeedr-api' ) . "</strong></a></p>"; |
| 57 |
$params['message'] .= "<p>" . __( "Upgrading only takes a minute. You will have <strong>instant access</strong> to more API requests. Any remaining credit for your current plan will be applied to your new plan.", 'datafeedr-api' ) . "</p>"; |
| 58 |
$params['message'] .= "<p>" . __( "You are under no obligation to upgrade. You may continue using your current plan for as long as you would like.", 'datafeedr-api' ) . "</p>"; |
| 59 |
$params['message'] .= "<p>" . __( "If you have any questions about your account, please ", 'datafeedr-api' ); |
| 60 |
$params['message'] .= "<a href=\"" . DFRAPI_EMAIL_US_URL . "?utm_source=email&utm_medium=link&utm_campaign=upgrade100percentnotice\">" . __( "contact us", 'datafeedr-api' ) . "</a>.</p>"; |
| 61 |
$params['message'] .= "<p>" . __( "Thanks,<br />Eric & Stefan<br />The Datafeedr Team", 'datafeedr-api' ) . "</p>"; |
| 62 |
$params['message'] .= "<p>"; |
| 63 |
$params['message'] .= "<a href=\"" . admin_url( 'admin.php?page=dfrapi_account' ) . "\">" . __( "Account Information", 'datafeedr-api' ) . "</a> | "; |
| 64 |
$params['message'] .= "<a href=\"" . dfrapi_user_pages( 'change' ) . "?utm_source=email&utm_medium=link&utm_campaign=upgrade100percentnotice\">" . __( "Upgrade Account", 'datafeedr-api' ) . "</a>"; |
| 65 |
$params['message'] .= "</p>"; |
| 66 |
|
| 67 |
$tracker['100_percent'] = 1; |
| 68 |
update_option( 'dfrapi_usage_notification_tracker', $tracker ); |
| 69 |
|
| 70 |
add_filter( 'wp_mail_content_type', 'dfrapi_set_html_content_type' ); |
| 71 |
wp_mail( $params['to'], $params['subject'], $params['message'] ); |
| 72 |
remove_filter( 'wp_mail_content_type', 'dfrapi_set_html_content_type' ); |
| 73 |
|
| 74 |
} elseif ( $percentage >= 90 && $percentage < 100 && empty( $tracker['90_percent'] ) ) { |
| 75 |
|
| 76 |
$params['subject'] = get_bloginfo( 'name' ) . __( ': Datafeedr API Usage (Warning)', 'datafeedr-api' ); |
| 77 |
$params['message'] .= "<p>" . __( "You have used <strong>90%</strong> of your allocated Datafeedr API requests for this period.", 'datafeedr-api' ) . "</p>"; |
| 78 |
$params['message'] .= "<p><strong>" . __( "API Usage", 'datafeedr-api' ) . "</strong></p>"; |
| 79 |
$params['message'] .= "<ul>"; |
| 80 |
$params['message'] .= "<li>" . __( "API requests used: ", 'datafeedr-api' ) . $request_count . "</li>"; |
| 81 |
$params['message'] .= "<li>" . __( "API requests remaining: ", 'datafeedr-api' ) . $remaining_requests . "</li>"; |
| 82 |
$params['message'] .= "<li>" . __( "API requests will reset on: ", 'datafeedr-api' ) . $reset_date . "</li>"; |
| 83 |
$params['message'] .= "</ul>"; |
| 84 |
$params['message'] .= "<p><strong>" . __( "What to do next?", 'datafeedr-api' ) . "</strong></p>"; |
| 85 |
$params['message'] .= "<p>" . __( "We recommend that you upgrade to prevent your product information from becoming outdated.", 'datafeedr-api' ) . "</p>"; |
| 86 |
$params['message'] .= "<p><a href=\"" . dfrapi_user_pages( 'change' ) . "?utm_source=email&utm_medium=link&utm_campaign=upgrade90percentnotice\"><strong>" . __( "UPGRADE NOW", 'datafeedr-api' ) . "</strong></a></p>"; |
| 87 |
$params['message'] .= "<p>" . __( "Upgrading only takes a minute. You will have <strong>instant access</strong> to more API requests. Any remaining credit for your current plan will be applied to your new plan.", 'datafeedr-api' ) . "</p>"; |
| 88 |
$params['message'] .= "<p>" . __( "You are under no obligation to upgrade. You may continue using your current plan for as long as you would like.", 'datafeedr-api' ) . "</p>"; |
| 89 |
$params['message'] .= "<p>" . __( "If you have any questions about your account, please ", 'datafeedr-api' ); |
| 90 |
$params['message'] .= "<a href=\"" . DFRAPI_EMAIL_US_URL . "?utm_source=email&utm_medium=link&utm_campaign=upgrade90percentnotice\">" . __( "contact us", 'datafeedr-api' ) . "</a>.</p>"; |
| 91 |
$params['message'] .= "<p>" . __( "Thanks,<br />Eric & Stefan<br />The Datafeedr Team", 'datafeedr-api' ) . "</p>"; |
| 92 |
$params['message'] .= "<p>"; |
| 93 |
$params['message'] .= "<a href=\"" . admin_url( 'admin.php?page=dfrapi_account' ) . "\">" . __( "Account Information", 'datafeedr-api' ) . "</a> | "; |
| 94 |
$params['message'] .= "<a href=\"" . dfrapi_user_pages( 'change' ) . "?utm_source=email&utm_medium=link&utm_campaign=upgrade90percentnotice\">" . __( "Upgrade Account", 'datafeedr-api' ) . "</a>"; |
| 95 |
$params['message'] .= "</p>"; |
| 96 |
|
| 97 |
$tracker['90_percent'] = 1; |
| 98 |
update_option( 'dfrapi_usage_notification_tracker', $tracker ); |
| 99 |
|
| 100 |
add_filter( 'wp_mail_content_type', 'dfrapi_set_html_content_type' ); |
| 101 |
wp_mail( $params['to'], $params['subject'], $params['message'] ); |
| 102 |
remove_filter( 'wp_mail_content_type', 'dfrapi_set_html_content_type' ); |
| 103 |
|
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
add_action( 'init', 'dfrapi_email_user_about_usage' ); |
| 108 |
|