This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
affilbox.php
248 lines (166 loc) · 6.53 KB
/
affilbox.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
/*
Plugin Name: AffilBox
Plugin URI: http://www.affilbox.cz
Description: AffilBox plugin
Version: 1.1
Author: AffilBox
Author URI: http://www.affilbox.cz
License:
*/
add_action('admin_menu', 'affilboxMenu');
function affilboxMenu() {
add_options_page('Nastavení affilboxu', 'AffilBox', 'manage_options', 'options.php', 'affilboxOptions');
}
/* plugin options */
add_action( 'admin_init', 'register_AffilboxPluginSettings' );
function register_AffilboxPluginSettings() {
register_setting( 'affilboxOptions', 'affilboxOptions' );
add_settings_section( "affilboxMain", "Nastavení údajů do FAPI", "affilboxOptionsSection", "affilbox" );
add_settings_field('affilboxSettingsUsername', 'Přihlašovací jméno', 'affilboxFieldsUsername', 'affilbox', 'affilboxMain');
add_settings_field('affilboxSettingsPassword', 'Heslo', 'affilboxFieldsPassword', 'affilbox', 'affilboxMain');
add_settings_section( "affilboxTracking", "Tracking kód", "affilboxOptionsSectionTracking", "affilbox" );
add_settings_field('affilboxSettingsCode', 'Kód', 'affilboxFieldsCode', 'affilbox', 'affilboxTracking');
}
register_deactivation_hook( __FILE__, 'affilboxDeactivate' );
function affilboxDeactivate(){
//
}
register_uninstall_hook( __FILE__, 'affilboxUninstall' );
function affilboxUninstall(){
if ( !defined( 'WP_UNINSTALL_PLUGIN' ) )
exit ();
delete_option('affilboxOptions');
delete_post_meta_by_key( 'affilboxTrackingCode' );
delete_post_meta_by_key( 'affilboxConversionCode' );
}
function affilboxOptionsSection() {
}
function affilboxFieldsUsername() {
$options = get_option('affilboxOptions');
echo "<input id='affilboxSettingsUsername' name='affilboxOptions[username]' size='40' type='text' value='{$options['username']}' />";
}
function affilboxFieldsPassword() {
$options = get_option('affilboxOptions');
echo "<input id='affilboxSettingsPassword' name='affilboxOptions[password]' size='40' type='password' value='{$options['password']}' />";
}
function affilboxOptionsSectionTracking() {
}
function affilboxFieldsCode() {
$options = get_option('affilboxOptions');
echo "<textarea id='affilboxSettingsCode' name='affilboxOptions[code]'>{$options['code']}</textarea>";
}
function affilboxOptions(){
echo "
<div class=\"wrap\">
".screen_icon()."
<h2>AffilBox</h2>
<form method=\"post\" action=\"options.php\">
<p>Pomocí těchto údajů se plugin přihlásí do systému FAPI a získá potřebné údaje pro kontrolu zaplaceného obsahu</p>
"; settings_fields( 'affilboxOptions' );
do_settings_sections( 'affilbox' );
submit_button();
echo "
</form>
</div>
";
}
/* // plugin options */
/* post meta */
add_action( 'add_meta_boxes', 'affilboxMetaBox' );
function affilboxMetaBox() {
add_meta_box( 'affilboxMeta', 'Affilbox', 'affilboxMetaOutput', 'post', 'normal', 'high' );
add_meta_box( 'affilboxMeta', 'Affilbox', 'affilboxMetaOutput', 'page', 'normal', 'high' );
}
function affilboxInit() {
wp_register_style( 'affilboxBackendCSS', plugins_url('/css/backend.css', __FILE__), array(), '1.0' );
wp_enqueue_style( 'affilboxBackendCSS' );
}
add_action('admin_enqueue_scripts','affilboxInit');
function affilboxMetaOutput()
{
global $post;
$values = get_post_custom( $post->ID );
?>
<div id="affilboxMeta">
<?php
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<label for="trackingCode">Tracking kód</label>
<textarea name="trackingCode" id="trackingCode"><?php echo $values["affilboxTrackingCode"][0]; ?></textarea>
<div class="clear"></div>
<label for="conversionCode">Konverzní kód</label>
<textarea name="conversionCode" id="conversionCode"><?php echo $values["affilboxConversionCode"][0]; ?></textarea>
<div class="clear"></div>
</div>
<?php
}
add_action( 'save_post', 'affilboxMetaSave' );
function affilboxMetaSave( $post_id )
{
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
if( !current_user_can( 'edit_post' ) ) return;
$allowed = array(
'a' => array(
'href' => array()
)
);
if( isset( $_POST['trackingCode'] ) )
update_post_meta( $post_id, 'affilboxTrackingCode', $_POST['trackingCode'] );
if( isset( $_POST['conversionCode'] ) )
update_post_meta( $post_id, 'affilboxConversionCode', $_POST['conversionCode'] );
}
/* // post meta */
/* post functions */
function connectFapiAffilbox($vs){
// TODO FAPI CONNECT
$fapiSettings = get_option("affilboxOptions");
$fapiUsername = $fapiSettings["username"];
$fapiPassword = $fapiSettings["password"];
if(!isset($fapiUsername) or !isset($fapiPassword))
return 0;
if (!class_exists('FAPIClient')) {
require_once("FAPIClient.php");
}
$fapi = new FAPIClient($fapiUsername, $fapiPassword, 'http://api.fapi.cz');
$invoice = $fapi->invoice->search(array('variable_symbol' => $vs, 'single' => true));
return $invoice ?: false;
}
function formatAffilboxAmount($amount)
{
return number_format(round($amount,2), 2, '.', '');
}
function footerTrackingCode() {
global $post;
if (get_post_meta($post->ID, "affilboxTrackingCode", TRUE)){
echo get_post_meta($post->ID, "affilboxTrackingCode", TRUE);
}else{
$options = get_option('affilboxOptions');
echo $options["code"];
}
}
add_action('wp_footer', 'footerTrackingCode');
function footerConversionCode() {
global $post;
if (get_post_meta($post->ID, "affilboxConversionCode", TRUE)){
$conversionCode = get_post_meta($post->ID, "affilboxConversionCode", TRUE);
$replacements['ID_TRANSAKCE'] = isset($_GET['email']) ? htmlspecialchars($_GET["email"]) : 'not-provided';
if (isset($_GET["cena"])) {
$replacements['CENA'] = number_format((float) $_GET["cena"]);
$replacements['OZNACENI_MENY'] = isset($_GET["mena"]) ? htmlspecialchars($_GET["mena"]) : 'CZK';
} elseif (isset($_GET["vs"])) {
$invoice = connectFapiAffilbox(intval($_GET["vs"]));
if ($invoice['total_czk'] !== null) {
$replacements['CENA'] = formatAffilboxAmount($invoice['total_czk'] - $invoice['total_vat_czk']);
$replacements['OZNACENI_MENY'] = 'CZK';
} else {
$replacements['CENA'] = formatAffilboxAmount($invoice['total'] - $invoice['total_vat']);
$replacements['OZNACENI_MENY'] = $invoice['currency'];
}
}
$conversionCode = strtr($conversionCode, $replacements);
echo $conversionCode;
}
}
add_action('wp_footer', 'footerConversionCode');