0 ) { $percentage = floor ( ( intval( $account['request_count'] ) / intval( $account['max_requests'] ) * 100 ) ); } else { $percentage = 0; } return $percentage; } return false; } add_action( 'init', 'dfrapi_email_user_about_usage' ); function dfrapi_email_user_about_usage() { $percentage = dfrapi_get_api_usage_percentage(); $status = get_option( 'dfrapi_account', array() ); $request_count = ( isset( $status['request_count'] ) ) ? abs( $status['request_count'] ) : 0; $remaining_requests = ( isset( $status['max_requests'] ) ) ? abs( $status['max_requests'] - $request_count ) : 0; $reset_date = ''; if ( isset( $status['bill_day'] ) ) { $today = date('j'); $num_days = date('t'); if ( $status['bill_day'] > $num_days ) { $bill_day = $num_days; } else { $bill_day = $status['bill_day']; } if ( $bill_day == 0 ) { $reset_date .= '' . __( 'Never', DFRAPI_DOMAIN ) . ''; } elseif ( $today >= $bill_day ) { $reset_date .= date('F', strtotime('+1 month')) . ' ' . $bill_day . ', ' . date('Y', strtotime('+1 month')); } else { $reset_date .= date('F') . ' ' . $bill_day . ', ' . date('Y'); } } $default = array( '90_percent' => '', '100_percent' => '' ); // Don't do anything if less than 90%. if ( $percentage < 90 ) { update_option( 'dfrapi_usage_notification_tracker', $default ); return; } $tracker = get_option( 'dfrapi_usage_notification_tracker', $default ); $params = array(); $params['to'] = get_bloginfo( 'admin_email' ); $params['message'] = "
" . __( "This is an automated message generated by: ", DFRAPI_DOMAIN ) . get_bloginfo( 'wpurl' ) . "
"; if ( $percentage >= 100 && empty( $tracker['100_percent'] ) ) { $params['subject'] = get_bloginfo( 'name' ) . __( ': Datafeedr API Usage (Critical)', DFRAPI_DOMAIN ); $params['message'] .= "" . __( "You have used 100% of your allocated Datafeedr API requests for this period. You are no longer able to query the Datafeedr API to get product information.", DFRAPI_DOMAIN ) . "
"; $params['message'] .= "" . __( "What to do next?", DFRAPI_DOMAIN ) . "
"; $params['message'] .= "" . __( "We strongly recommend that you upgrade to prevent your product information from becoming outdated.", DFRAPI_DOMAIN ) . "
"; $params['message'] .= "" . __( "UPGRADE NOW", DFRAPI_DOMAIN ) . "
"; $params['message'] .= "" . __( "Upgrading only takes a minute. You will have instant access to more API requests. Any remaining credit for your current plan will be applied to your new plan.", DFRAPI_DOMAIN ) . "
"; $params['message'] .= "" . __( "You are under no obligation to upgrade. You may continue using your current plan for as long as you would like.", DFRAPI_DOMAIN ) . "
"; $params['message'] .= "" . __( "If you have any questions about your account, please ", DFRAPI_DOMAIN ); $params['message'] .= "" . __( "contact us", DFRAPI_DOMAIN ) . ".
"; $params['message'] .= "" . __( "Thanks,
Eric & Stefan
The Datafeedr Team", DFRAPI_DOMAIN ) . "
"; $params['message'] .= "" . __( "Account Information", DFRAPI_DOMAIN ) . " | "; $params['message'] .= "" . __( "Upgrade Account", DFRAPI_DOMAIN ) . ""; $params['message'] .= "
"; $tracker['100_percent'] = 1; update_option( 'dfrapi_usage_notification_tracker', $tracker ); add_filter( 'wp_mail_content_type', 'dfrapi_set_html_content_type' ); wp_mail( $params['to'], $params['subject'], $params['message'] ); remove_filter( 'wp_mail_content_type', 'dfrapi_set_html_content_type' ); } elseif ( $percentage >= 90 && $percentage < 100 && empty( $tracker['90_percent'] ) ) { $params['subject'] = get_bloginfo( 'name' ) . __( ': Datafeedr API Usage (Warning)', DFRAPI_DOMAIN ); $params['message'] .= "" . __( "You have used 90% of your allocated Datafeedr API requests for this period.", DFRAPI_DOMAIN ) . "
"; $params['message'] .= "" . __( "API Usage", DFRAPI_DOMAIN ) . "
"; $params['message'] .= "" . __( "What to do next?", DFRAPI_DOMAIN ) . "
"; $params['message'] .= "" . __( "We recommend that you upgrade to prevent your product information from becoming outdated.", DFRAPI_DOMAIN ) . "
"; $params['message'] .= "" . __( "UPGRADE NOW", DFRAPI_DOMAIN ) . "
"; $params['message'] .= "" . __( "Upgrading only takes a minute. You will have instant access to more API requests. Any remaining credit for your current plan will be applied to your new plan.", DFRAPI_DOMAIN ) . "
"; $params['message'] .= "" . __( "You are under no obligation to upgrade. You may continue using your current plan for as long as you would like.", DFRAPI_DOMAIN ) . "
"; $params['message'] .= "" . __( "If you have any questions about your account, please ", DFRAPI_DOMAIN ); $params['message'] .= "" . __( "contact us", DFRAPI_DOMAIN ) . ".
"; $params['message'] .= "" . __( "Thanks,
Eric & Stefan
The Datafeedr Team", DFRAPI_DOMAIN ) . "
"; $params['message'] .= "" . __( "Account Information", DFRAPI_DOMAIN ) . " | "; $params['message'] .= "" . __( "Upgrade Account", DFRAPI_DOMAIN ) . ""; $params['message'] .= "
"; $tracker['90_percent'] = 1; update_option( 'dfrapi_usage_notification_tracker', $tracker ); add_filter( 'wp_mail_content_type', 'dfrapi_set_html_content_type' ); wp_mail( $params['to'], $params['subject'], $params['message'] ); remove_filter( 'wp_mail_content_type', 'dfrapi_set_html_content_type' ); } return; } function dfrapi_set_html_content_type() { return 'text/html'; } /** * Modify affiliate ID if product is a Zanox product. * Replaces $affiliate_id with "zmid". */ add_filter( 'dfrapi_affiliate_id', 'dfrapi_get_zanox_zmid', 10, 3 ); function dfrapi_get_zanox_zmid( $affiliate_id, $product, $networks ) { if ( isset( $product['source'] ) && preg_match( "/\bZanox\b/", $product['source'] ) ) { $zanox = dfrapi_api_get_zanox_zmid( $product['merchant_id'], $affiliate_id ); $affiliate_id = ( !isset( $zanox[0]['zmid']) ) ? '___MISSING___' : $zanox[0]['zmid']; } return $affiliate_id; } /** * Modify affiliate ID if product is a Partnerize product. * Replaces $affiliate_id with "camref". * * @since 1.0.66 * * @return string Affiliate ID. */ add_filter( 'dfrapi_affiliate_id', 'dfrapi_get_ph_camref', 10, 3 ); function dfrapi_get_ph_camref( $affiliate_id, $product, $networks ) { if ( isset( $product['source'] ) && preg_match( "/\bPartnerize\b/", $product['source'] ) ) { $ph = dfrapi_api_get_ph_camref( $product['merchant_id'] ); $affiliate_id = ( ! isset( $ph[0]['camref'] ) ) ? '___MISSING___' : $ph[0]['camref']; } return $affiliate_id; } /** * Modify affiliate ID if product is a Effiliation product. * Replaces $affiliate_id with "affiliate ID". * * @return string Affiliate ID. * @since 1.0.81 * */ function dfrapi_get_effiliation_affiliate_id( $affiliate_id, $product, $networks ) { if ( isset( $product['source'] ) && preg_match( "/\bEffiliation\b/", $product['source'] ) ) { $effiliation = dfrapi_api_get_effiliation_affiliate_id( $product['merchant_id'] ); $affiliate_id = $effiliation == 'dfrapi_unapproved_effiliation_merchant' ? '___MISSING___' : $effiliation; } return $affiliate_id; } add_filter( 'dfrapi_affiliate_id', 'dfrapi_get_effiliation_affiliate_id', 10, 3 ); function dfrapi_get_zanox_keys() { $configuration = (array) get_option( 'dfrapi_configuration' ); $zanox_connection_key = false; $zanox_secret_key = false; if ( isset( $configuration['zanox_connection_key'] ) && ( $configuration['zanox_connection_key'] != '' ) ) { $zanox_connection_key = $configuration['zanox_connection_key']; } if ( isset( $configuration['zanox_secret_key'] ) && ( $configuration['zanox_secret_key'] != '' ) ) { $zanox_secret_key = $configuration['zanox_secret_key']; } if ( $zanox_connection_key && $zanox_secret_key ) { return array( 'connection_key'=> $zanox_connection_key, 'secret_key' => $zanox_secret_key, ); } return false; } /** * Get Partnerize Keys. * * @since 1.0.66 * * @return array|bool Array of keys or false if they do not exist. */ function dfrapi_get_ph_keys() { $configuration = (array) get_option( 'dfrapi_configuration' ); $ph_application_key = false; $ph_user_api_key = false; $ph_publisher_id = false; if ( isset( $configuration['ph_application_key'] ) && ( $configuration['ph_application_key'] != '' ) ) { $ph_application_key = $configuration['ph_application_key']; } if ( isset( $configuration['ph_user_api_key'] ) && ( $configuration['ph_user_api_key'] != '' ) ) { $ph_user_api_key = $configuration['ph_user_api_key']; } if ( isset( $configuration['ph_publisher_id'] ) && ( $configuration['ph_publisher_id'] != '' ) ) { $ph_publisher_id = $configuration['ph_publisher_id']; } if ( $ph_application_key && $ph_user_api_key && $ph_publisher_id ) { return array( 'application_key' => $ph_application_key, 'user_api_key' => $ph_user_api_key, 'publisher_id' => $ph_publisher_id, ); } return false; } /** * Get Effiliation Keys. * * @since 1.0.81 * * @return array|bool Array of keys or false if they do not exist. */ function dfrapi_get_effiliation_keys() { $configuration = (array) get_option( 'dfrapi_configuration' ); $effiliation_key = false; if ( isset( $configuration['effiliation_key'] ) && ( $configuration['effiliation_key'] != '' ) ) { $effiliation_key = $configuration['effiliation_key']; } if ( $effiliation_key ) { return array( 'effiliation_key' => $effiliation_key, ); } return false; } /** * Returns Amazon API key credentials if they exist. * * If the Amazon API keys such as the Access Key ID, Secret Access Key, Tracking ID and Locale all * exists, then this function returns them in array format. Otherwise it returns false. * * @since 1.0.33 * * @return array|bool Returns array of values if all values exist, otherwise false. */ function dfrapi_get_amazon_keys() { $configuration = (array) get_option( 'dfrapi_configuration' ); $amazon_access_key_id = false; $amazon_secret_access_key = false; $amazon_tracking_id = false; $amazon_locale = false; if ( isset( $configuration['amazon_access_key_id'] ) && ( $configuration['amazon_access_key_id'] != '' ) ) { $amazon_access_key_id = $configuration['amazon_access_key_id']; } if ( isset( $configuration['amazon_secret_access_key'] ) && ( $configuration['amazon_secret_access_key'] != '' ) ) { $amazon_secret_access_key = $configuration['amazon_secret_access_key']; } if ( isset( $configuration['amazon_tracking_id'] ) && ( $configuration['amazon_tracking_id'] != '' ) ) { $amazon_tracking_id = $configuration['amazon_tracking_id']; } if ( isset( $configuration['amazon_locale'] ) && ( $configuration['amazon_locale'] != '' ) ) { $amazon_locale = $configuration['amazon_locale']; } if ( $amazon_access_key_id && $amazon_secret_access_key && $amazon_tracking_id && $amazon_locale ) { return array( 'amazon_access_key_id' => $amazon_access_key_id, 'amazon_secret_access_key' => $amazon_secret_access_key, 'amazon_tracking_id' => $amazon_tracking_id, 'amazon_locale' => $amazon_locale, ); } return false; } /** * Returns a link to a user page on v4.datafeedr.com. */ function dfrapi_user_pages( $page ) { $pages = array( 'edit' => 'https://members.datafeedr.com/account', 'invoices' => 'https://members.datafeedr.com/invoices', 'billing' => 'https://members.datafeedr.com/billing', 'cancel' => 'https://members.datafeedr.com/', 'change' => 'https://members.datafeedr.com/subscription', 'signup' => 'https://members.datafeedr.com/subscribe', 'summary' => 'https://members.datafeedr.com/subscription', 'api' => 'https://members.datafeedr.com/api', 'factory' => 'https://members.datafeedr.com/factory', ); return $pages[$page]; } /** * Adds option name to transient whitelist. This is so we know * all transient options that can be deleted when deleting the * API cache on Tools page. */ function dfrapi_update_transient_whitelist( $option_name ) { $whitelist = get_option( 'dfrapi_transient_whitelist', array() ); $whitelist[] = $option_name; update_option( 'dfrapi_transient_whitelist', array_unique( $whitelist ) ); } /** * Add affiliate ID and tracking ID to an affiliate link. * * @param array $product An array of a single Datafeedr $product. * * @return string A URL with affiliate ID inserted or an empty string if the affiliate ID is missing. */ function dfrapi_url( $product ) { // Get all the user's selected networks. $networks = (array) get_option( 'dfrapi_networks' ); // Support added for Amazon in version 1.0.60 (2017-10-18) Ticket #15201 if ( substr( $product['source'], 0, 6 ) === "Amazon" ) { // Get the user's Amazon Associate Tag $affiliate_id = dfrapi_get_amazon_associate_tag(); } else { // Extract the affiliate ID from the $networks array. $affiliate_id = isset( $networks['ids'][ $product['source_id'] ]['aid'] ) ? $networks['ids'][ $product['source_id'] ]['aid'] : ''; } $affiliate_id = apply_filters( 'dfrapi_affiliate_id', $affiliate_id, $product, $networks ); $affiliate_id = trim( $affiliate_id ); // Extract the Tracking ID from the $networks array. $tracking_id = ( isset( $networks['ids'][ $product['source_id'] ]['tid'] ) ) ? $networks['ids'][ $product['source_id'] ]['tid'] : ''; $tracking_id = apply_filters( 'dfrapi_tracking_id', $tracking_id, $product, $networks ); $tracking_id = trim( $tracking_id ); // Affiliate ID is missing. Do action and return empty string. if ( $affiliate_id == '' ) { do_action( 'dfrapi_affiliate_id_is_missing', $product ); return ''; } // Determine which URL field to get: 'url' OR 'ref_url'. Return 'url' if $tracking_id is empty, otherwise, use 'ref_url'. $url = ( $tracking_id !== '' && isset( $product['ref_url'] ) ) ? $product['ref_url'] : $product['url']; // Apply filters to URL before affiliate & tracking ID insertion. $url = apply_filters( 'dfrapi_before_affiliate_id_insertion', $url, $product, $affiliate_id ); $url = apply_filters( 'dfrapi_before_tracking_id_insertion', $url, $product, $tracking_id ); // Replace placeholders in URL. $placeholders = array( "@@@", "###" ); $replacements = array( $affiliate_id, $tracking_id ); $url = str_replace( $placeholders, $replacements, $url ); // Apply filters to URL after affiliate & tracking ID insertion. $url = apply_filters( 'dfrapi_after_affiliate_id_insertion', $url, $product, $affiliate_id ); $url = apply_filters( 'dfrapi_after_tracking_id_insertion', $url, $product, $tracking_id ); // Return URL return $url; } /** * Return Amazon Associate Tag (ie. Tracking ID). * * @since 1.0.60 * * @return string Associate Tag or empty string if it does not exist. */ function dfrapi_get_amazon_associate_tag() { $config = get_option( 'dfrapi_configuration' ); return ( isset( $config['amazon_tracking_id'] ) ) ? $config['amazon_tracking_id'] : ''; } /** * Add affiliate ID to impression URL. * * Since 1.0.39 * * @param $product - An array of a single's product's information. */ function dfrapi_impression_url( $product ) { $impression_url = ( isset( $product['impressionurl'] ) ) ? trim( $product['impressionurl'] ) : false; if ( ! $impression_url ) { return ''; } // Get all the user's selected networks. $networks = (array) get_option( 'dfrapi_networks' ); // Extract the affiliate ID from the $networks array. $affiliate_id = $networks['ids'][ $product['source_id'] ]['aid']; $affiliate_id = apply_filters( 'dfrapi_affiliate_id', $affiliate_id, $product, $networks ); $affiliate_id = trim( $affiliate_id ); // Affiliate ID is missing. Do action and return empty string. if ( $affiliate_id == '' ) { do_action( 'dfrapi_affiliate_id_is_missing_impression', $product ); return ''; } // Apply filters to URL before affiliate & tracking ID insertion. $impression_url = apply_filters( 'dfrapi_before_affiliate_id_insertion_impression', $impression_url, $product, $affiliate_id ); // Replace placeholders in URL. $placeholders = array( "@@@" ); $replacements = array( $affiliate_id ); $impression_url = str_replace( $placeholders, $replacements, $impression_url ); // Apply filters to URL after affiliate & tracking ID insertion. $impression_url = apply_filters( 'dfrapi_after_affiliate_id_insertion_impression', $impression_url, $product, $affiliate_id ); // Return URL return $impression_url; } /** * Output an error message generated by the API. */ function dfrapi_output_api_error( $data ) { $error = @$data['dfrapi_api_error']; $params = @$data['dfrapi_api_error']['params']; ?>