This repository has been archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlovidy_Links.php
190 lines (182 loc) · 6.81 KB
/
Flovidy_Links.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
class Flovidy_links {
public $urls;
public $orRef;
public $stringifiedUrls;
public $newRef;
public $records;
public $link;
private $results;
function __construct($urls, $country, $link, $newRef) {
$this->country = $country;
$this->urls = $urls;
$this->link = $link;
$this->newRef = $newRef;
$remove[] = "'";
$remove[] = '"';
$remove[] = '%22';
$remove[] = '%20';
foreach ($urls as $url) {
$this->urls[$i] = str_replace($remove, "", $url);
}
$this->stringifiedUrls = "'". implode( "','", $urls) ."'";
global $wpdb;
$this->results = $wpdb->get_results('SELECT us_link, '.$link.' FROM '.$wpdb->prefix . 'ai_link WHERE us_link in ('.$this->stringifiedUrls.')', OBJECT );
}
function create_shopping_chart() {
$j=0;
$rec = [];
$temp_urls = $this->urls;
foreach($temp_urls as $url){
if(strpos($url, ";") !== false){
$i = 1;
if($this->country != 'us'){
$finalUrl = 'https://www.amazon.'. $this->country.'/gp/aws/cart/add.html?AssociateTag='.$this->newRef;
} else {
$finalUrl = 'https://www.amazon.com/gp/aws/cart/add.html?AssociateTag='.$this->newRef;
}
preg_match("/B0(?:(?!\/).)*/", $url, $number);
$finalUrl = $finalUrl . '&ASIN.'.$i.'='.$number[0].'&Quantity.'.$i.'=1';
$i++;
$item = array(us_link => $url, new_link=>$finalUrl);
array_push($rec, $item);
unset($temp_urls[$j]);
}
$j++;
}
$this->urls = $temp_urls;
$this->records = $rec;
return $rec;
}
function create_new_links(){
$records = [];
foreach ($this->urls as $oldUrl) {
$orUrl = $oldUrl;
$item = [];
$exists = True;
$item = $this->link_exists($oldUrl);
// if it does not, create the array and say it doesn't exist
if(!$item){
$item = array(us_link => $oldUrl, new_link=>'');
$exists = False;
}
// it if does, check if new link exists
$current_link = $this->link;
if($item->$current_link){
$new_item = array(us_link => $item->us_link, $current_link => $item->$current_link);
array_push($records, $new_item);
} else {
if(strpos($oldUrl, "amzn.to") !== false){
$oldUrl = $this->get_redirect_url($oldUrl);
if ($oldUrl == false) {
continue;
}
}
// rebuild link and remove all extra ids and tokens
$newUrl = $this->rebuild_url($oldUrl);
// change domain and check if url exists
$url = $this->create_new_url($newUrl);
// create a new record if not exists, else just update record with new url
global $wpdb;
if(!$exists){
$wpdb->insert(
$wpdb->prefix .'ai_link',
array(
us_link => $orUrl,
$this->link => $url
),
array( '%s', '%s' )
);
} else {
$wpdb->update(
$wpdb->prefix .'ai_link',
array(
$this->link => $url
),
array( 'us_link' => $orUrl ),
array( '%s' ),
array( '%s' )
);
};
$newRecord = array(us_link => $orUrl, $this->link => $url);
array_push($records, $newRecord);
}
}
return $records;
}
function link_exists($oldUrl){
// check if us_links exist
foreach($this->results as $result){
if($oldUrl == $result->us_link){
return $result;
}
}
return False;
}
function get_redirect_url($oldUrl){
$url = 'https://api-ssl.bitly.com/v3/expand?access_token='.trim(get_option("Flovidy_Plugin_bitly_access_token")).'&shortUrl='.urlencode($oldUrl);
$response = wp_remote_retrieve_body(wp_remote_get($url));
$response_json = json_decode(trim($response), true);
if ($response_json['status_txt'] == 'OK') {
return $response_json['data']['expand'][0]['long_url'];
}
return false;
}
function rebuild_url($oldUrl) {
$index = 0;
$code = '';
preg_match("/B0(?:(?!\/).)*/", $oldUrl, $number);
if (count($number) > 0) {
$code = $number[0];
}
$parts = parse_url($oldUrl);
parse_str($parts['query'], $query);
if($code == ''){
$newUrl = 'https://www.amazon.com/s/';
} else {
$newUrl = 'https://www.amazon.com/dp/'.$code.'/';
}
if(strpos($oldUrl, 'keywords')!== false){
if(strpos($oldUrl, 'field-keywords')!== false){
$newUrl .= '?keywords='.$query['field-keywords'];
} else {
$newUrl .= '?keywords='.$query['keywords'];
}
$newUrl = strtr($newUrl,array(" " => "+"));
}
return $newUrl;
}
function create_new_url($oldUrl){
$url = strtr($oldUrl,array("amazon.com" => "amazon.".$this->country));
if (strpos($url, '/s/?keywords=')){
return $url;
}
$counter = 0;
$response = wp_remote_get($url,
array(
'timeout' => 20,
'user-agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
)
);
$httpcode = wp_remote_retrieve_response_code($response);
// 500 generally means we are blocked or Amazon is having interal issues. No point in doing more requests.
if ($httpcode == 500){
wp_die();
}
// if page does not exist in new store
if($httpcode == 404 || $httpcode == 504){
if(strpos($url, 'keywords')!== false){
$parts = parse_url($url);
parse_str($parts['query'], $query);
$url = 'https://www.amazon.'.$this->country.'/s/?keywords='.$query['keywords'];
} else {
preg_match("/B0(?:(?!\/).)*/", $url, $number);
if (count($number) > 0) {
$code = $number[0];
}
$url = 'https://www.amazon.'.$this->country.'/s/?keywords='.strtr($code, array("-" => "+"));
}
}
return $url;
}
}