forked from toxcox/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaypalDigitalDownload_Plugin.php
248 lines (202 loc) · 8.26 KB
/
PaypalDigitalDownload_Plugin.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/*
"PayPal Digital Download" Copyright (C) 2013 Michael Simpson (email : [email protected])
This following part of this file is part of PayPal Digital Download for WordPress.
PayPal Digital Download is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PayPal Digital Download is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Contact Form to Database Extension.
If not, see <http://www.gnu.org/licenses/>.
*/
include_once('PaypalDigitalDownload_LifeCycle.php');
class PaypalDigitalDownload_Plugin extends PaypalDigitalDownload_LifeCycle
{
/**
* See: http://plugin.michael-simpson.com/?page_id=31
* @return array of option meta data.
*/
public function getOptionMetaData()
{
// http://plugin.michael-simpson.com/?page_id=31
return array( //'_version' => array('Installed Version'), // Leave this one commented-out. Uncomment to test upgrades.
);
}
public function getPluginDisplayName()
{
return 'PayPal Digital Download';
}
protected function getMainPluginFileName()
{
return 'paypal-digital-download.php';
}
/**
* See: http://plugin.michael-simpson.com/?page_id=101
* Called by install() to create any database tables if needed.
* Best Practice:
* (1) Prefix all table names with $wpdb->prefix
* (2) make table names lower case only
* @return void
*/
protected function installDatabaseTables()
{
global $wpdb;
$tableName = $this->prefixTableName('downloads');
$sql =
'CREATE TABLE IF NOT EXISTS ' . $tableName . ' ('
. ' id int NOT NULL AUTO_INCREMENT,'
. ' product varchar(64),'
. ' code varchar(256),'
. ' expire datetime not null,'
. ' PRIMARY KEY(id))';
$wpdb->query($sql);
}
/**
* See: http://plugin.michael-simpson.com/?page_id=101
* Drop plugin-created tables on uninstall.
* @return void
*/
protected function unInstallDatabaseTables()
{
// global $wpdb;
// $tableName = $this->prefixTableName('downloads');
// $wpdb->query("DROP TABLE IF EXISTS `$tableName`");
}
/**
* Perform actions when upgrading from version X to version Y
* See: http://plugin.michael-simpson.com/?page_id=35
* @return void
*/
public function upgrade()
{
}
public function addActionsAndFilters()
{
// Add options administration page
// http://plugin.michael-simpson.com/?page_id=47
add_action('admin_menu', array(&$this, 'addSettingsSubMenuPage'));
// Example adding a script & style just for the options administration page
// http://plugin.michael-simpson.com/?page_id=47
// if (strpos($_SERVER['REQUEST_URI'], $this->getSettingsSlug()) !== false) {
// wp_enqueue_script('my-script', plugins_url('/js/my-script.js', __FILE__));
// wp_enqueue_style('my-style', plugins_url('/css/my-style.css', __FILE__));
// }
// Add Actions & Filters
// http://plugin.michael-simpson.com/?page_id=37
// Adding scripts & styles to all pages
// Examples:
// wp_enqueue_script('jquery');
// wp_enqueue_style('my-style', plugins_url('/css/my-style.css', __FILE__));
// wp_enqueue_script('my-script', plugins_url('/js/my-script.js', __FILE__));
// Register short codes
// http://plugin.michael-simpson.com/?page_id=39
require_once('PayPalButtonSC.php');
$sc = new PayPalButtonSC();
$sc->register('pp-digital-download');
// Register AJAX hooks
// http://plugin.michael-simpson.com/?page_id=41
// add_action('wp_ajax_pddcheckout', array(&$this, 'ajaxCheckout'));
// add_action('wp_ajax_nopriv_pddcheckout', array(&$this, 'ajaxCheckout'));
add_action('wp_ajax_pddorderconfirm', array(&$this, 'ajaxOrderConfirm'));
add_action('wp_ajax_nopriv_pddorderconfirm', array(&$this, 'ajaxOrderConfirm'));
add_action('wp_ajax_pdddownload', array(&$this, 'ajaxDownload'));
add_action('wp_ajax_nopriv_pdddownload', array(&$this, 'ajaxDownload'));
// add_action('wp_ajax_pddcancel', array(&$this, 'ajaxCancel'));
// add_action('wp_ajax_nopriv_pddcancel', array(&$this, 'ajaxCancel'));
}
public function generateCode()
{
return md5(uniqid(rand(), true));
}
public function generateUrl($code)
{
return admin_url('admin-ajax.php') . '?action=pdddownload&c=' . $code;
}
public function saveCodeToDatabase($product, $code)
{
global $wpdb;
$tableName = $this->getTableName();
$wpdb->query(
$wpdb->prepare(
"INSERT INTO $tableName( product, code, expire ) VALUES (%s, %s, NOW( ) + INTERVAL 1 DAY)",
$product, $code));
}
public function generateDownloadLink()
{
$code = $this->generateCode();
$this->saveCodeToDatabase('CFDB Editor', $code); // todo hardcoded
return $this->generateUrl($code);
}
// public function ajaxCheckout()
// {
// die();
// }
// public function ajaxCancel()
// {
// die();
// }
public function ajaxOrderConfirm()
{
include("orderconfirm.php");
die();
}
public function ajaxDownload()
{
// Don't let IE cache this request
header("Pragma: no-cache");
header("Cache-Control: no-cache, must-revalidate");
header("Expires: 0");
if (isset($_REQUEST['c'])) {
global $wpdb;
$tableName = $this->getTableName();
$rows =
$wpdb->get_var(
$wpdb->prepare(
"select count(code) from $tableName where code = %s and expire >= NOW()", $_REQUEST['c']));
if ($rows > 0) {
header('Content-type: application/zip');
header('Content-Disposition: inline; filename="contact-form-to-database-extension-edit.zip"'); // todo hardcoded
if ($stream = fopen('/kunden/homepages/17/d274877261/htdocs/downloads/contact-form-to-database-extension-edit.zip', 'r')) {
echo stream_get_contents($stream);
fclose($stream);
}
} else {
header('Content-type: text/html');
//print_r($rows); // debug
?>
<p>Download link is not valid</p>
<p><a href="http://cfdbplugin.com/?page_id=939">Get a new link</a></p>
<p>If you have any problems with the download, please contact <a
href="mailto:[email protected]">[email protected]</a></p>
<?php
}
} else {
header('Content-type: text/html');
?>
<p>Download link is not valid</p>
<p><a href="http://cfdbplugin.com/?page_id=939">Get a new link</a></p>
<p>If you have any problems with the download, please contact <a href="mailto:[email protected]">[email protected]</a>
</p>
<?php
}
die();
}
public function getTableName() {
return $this->prefixTableName('downloads');
}
public function settingsPage() {
if (!current_user_can('manage_options')) {
wp_die(__('You do not have sufficient permissions to access this page.', 'paypal-digital-download'));
}
?>
<p>This CFDB Editor download link will be good for 24 hours:</p>
<p><a href="<?php echo $this->generateDownloadLink() ?>">contact-form-to-database-extension-edit.zip</a></p>
<p>If you have any problems with the download, please contact <a href="mailto:cfdbplugin.com">[email protected]</a></p>
<?php
}
}