PluginProbe
Datafeedr API / 1.4.3
Datafeedr API v1.4.3
1.4.3 1.4.2 1.0.125 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.3 1.0.30 1.0.31 1.0.32 All 182 releases
datafeedr-api / hooks / admin / merchants.php

merchants.php in Datafeedr API 1.4.3, at hooks/admin/merchants.php

169 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 defined( 'ABSPATH' ) || exit;
4
5 /**
6 * @param array $merchants
7 * @param array $network
8 *
9 * @return array|WP_Error|null
10 */
11 function dfrapi_remove_unapproved_awin_merchants( $merchants, $network ) {
12
13 // Return if not in Awin network.
14 if ( 10006 != $network['group_id'] ) {
15 return $merchants;
16 }
17
18 $affiliate_id = dfrapi_get_affiliate_and_tracking_id( $network['_id'], 'aid' );
19
20 if ( is_wp_error( $affiliate_id ) ) {
21 return new WP_Error(
22 'missing_awin_affiliate_id',
23 'Please enter your Awin affiliate ID for ' . esc_html( $network['name'] ) . ' <a href="' . admin_url( 'admin.php?page=dfrapi_networks' ) . '#group_affiliatewindow" target="_blank">here</a>.'
24 );
25 }
26
27 static $awin_access_token = null;
28
29 if ( null === $awin_access_token ) {
30
31 $config = get_option( 'dfrapi_configuration', [] );
32
33 $awin_access_token = ( isset( $config['awin_access_token'] ) && ! empty( $config['awin_access_token'] ) ) ?
34 trim( $config['awin_access_token'] ) :
35 new WP_Error(
36 'awin_access_token_missing',
37 'Please enter your Awin API Token <a href="' . admin_url( 'admin.php?page=dfrapi' ) . '" target="_blank">here</a>.'
38 );
39 }
40
41 if ( is_wp_error( $awin_access_token ) ) {
42 return $awin_access_token;
43 }
44
45 $cache_key = 'dfrapi_awin_joined_' . $affiliate_id;
46 $cooldown_key = 'dfrapi_awin_429_' . $affiliate_id;
47 $approved_program_ids = get_transient( $cache_key );
48
49 // Awin's Publisher API is rate-limited to 20 requests per minute per user.
50 // This filter runs on every merchants-page load, so we only call Awin when
51 // we have no cached program list AND we're not in a 429 cool-down window.
52 // Without this, each reload re-calls Awin and keeps the rate limit tripped.
53 if ( false === $approved_program_ids ) {
54
55 if ( false !== get_transient( $cooldown_key ) ) {
56 return new WP_Error(
57 'awin_rate_limited',
58 esc_html( $network['name'] ) . ': Awin is temporarily rate-limiting API requests (max 20 per minute). Please wait a minute or two, then reload this page.'
59 );
60 }
61
62 $url = sprintf(
63 'https://api.awin.com/publishers/%1$s/programmes?relationship=joined',
64 rawurlencode( $affiliate_id )
65 );
66
67 // Send the token in a header so it isn't written to proxy and server access logs.
68 $response = wp_remote_get( $url, [
69 'timeout' => 15,
70 'headers' => [ 'Authorization' => 'Bearer ' . $awin_access_token ],
71 ] );
72 $code = (int) wp_remote_retrieve_response_code( $response );
73
74 if ( ! is_wp_error( $response ) && 200 === $code ) {
75 $programs = json_decode( wp_remote_retrieve_body( $response ), true );
76 if ( is_array( $programs ) ) {
77 $approved_program_ids = wp_list_pluck( $programs, 'id' );
78 set_transient( $cache_key, $approved_program_ids, 12 * HOUR_IN_SECONDS );
79 dfrapi_update_transient_whitelist( $cache_key );
80 }
81 } elseif ( 429 === $code ) {
82 // Back off so repeated page loads stop hammering Awin and keeping
83 // the limit tripped. Honor Retry-After if Awin sends it.
84 $retry_after = absint( wp_remote_retrieve_header( $response, 'retry-after' ) );
85 set_transient( $cooldown_key, 1, max( 120, $retry_after ) );
86 dfrapi_update_transient_whitelist( $cooldown_key );
87 return new WP_Error(
88 'awin_rate_limited',
89 esc_html( $network['name'] ) . ': Awin is temporarily rate-limiting API requests (max 20 per minute). Please wait a minute or two, then reload this page.'
90 );
91 }
92 }
93
94 if ( false === $approved_program_ids ) {
95 return new WP_Error(
96 'unable_to_retrieve_approved_awin_program_ids',
97 'Unable to get your list of joined ' . esc_html( $network['name'] ) . ' programs. Please ensure your Awin Access Token is correct <a href="' . admin_url( 'admin.php?page=dfrapi' ) . '" target="_blank">here</a> and your affiliate ID is correct <a href="' . admin_url( 'admin.php?page=dfrapi_networks' ) . '#group_affiliatewindow" target="_blank">here</a>.' );
98 }
99
100 foreach ( $merchants as $key => $merchant ) {
101
102 $approved = false;
103 $suids = isset( $merchant['suids'] ) ? explode( ',', $merchant['suids'] ) : [];
104
105 foreach ( $suids as $suid ) {
106 if ( in_array( $suid, $approved_program_ids ) ) {
107 $approved = true;
108 }
109 }
110
111 if ( ! $approved ) {
112 unset( $merchants[ $key ] );
113 }
114 }
115
116 return $merchants;
117 }
118
119 add_filter( 'dfrapi_list_merchants', 'dfrapi_remove_unapproved_awin_merchants', 10, 2 );
120
121 /**
122 * @param array $merchants
123 * @param array $network
124 *
125 * @return array|WP_Error|null
126 * @since 1.0.102
127 */
128 function dfrapi_disable_affiliate_gateway_merchant_selection_when_sid_empty( $merchants, $network ) {
129
130 // Return if not in The Affiliate Gateway network.
131 if ( 10033 != $network['group_id'] ) {
132 return $merchants;
133 }
134
135 $sid = dfrapi_get_affiliate_gateway_sid();
136
137 if ( is_wp_error( $sid ) ) {
138 return $sid;
139 }
140
141 return $merchants;
142 }
143
144 add_filter( 'dfrapi_list_merchants', 'dfrapi_disable_affiliate_gateway_merchant_selection_when_sid_empty', 10, 2 );
145
146 /**
147 * @param array $merchants
148 * @param array $network
149 *
150 * @return array|WP_Error|null
151 * @since 1.0.124
152 */
153 function dfrapi_disable_belboon_merchant_selection_when_aid_empty( $merchants, $network ) {
154
155 // Return if not in Belboon network.
156 if ( 10007 != $network['group_id'] ) {
157 return $merchants;
158 }
159
160 $aid = dfrapi_get_belboon_adspace_id();
161
162 if ( is_wp_error( $aid ) ) {
163 return $aid;
164 }
165
166 return $merchants;
167 }
168
169 add_filter( 'dfrapi_list_merchants', 'dfrapi_disable_belboon_merchant_selection_when_aid_empty', 10, 2 );