-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvoice.php
More file actions
396 lines (327 loc) · 11.3 KB
/
envoice.php
File metadata and controls
396 lines (327 loc) · 11.3 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php
/*
Plugin Name: Envoice
Plugin URI:
Description: Use Envoice Buy Button on Wordpress platfrom with simple shortcode
Version: 0.4.5
Author: Aleksey Developer
Author URI: https://aleksey.co
*/
define('ENVOICE_PLUGIN_VERSION', '0.4.5');
// include Envoice API class helper
include plugin_dir_path(__FILE__) . 'EnvoiceService.php';
// products list page
include plugin_dir_path(__FILE__) . 'products.php';
/* Check if plugin settings not empty*/
function isEnvoiceSettingsSet()
{
$api_key = get_option('auth_key');
$api_secret = get_option('auth_secret');
return $api_key && $api_secret;
}
/* Add envoice section to admin menu */
function envoice_section_page()
{
// add top level menu page Envoice
add_menu_page(
'Envoice',
'Envoice',
'manage_options',
'envoice',
'envoice_options_page_html',
plugin_dir_url(__FILE__) . 'assets/images/icon.png'
);
/* If plugin settings is set, add products list page*/
if (isEnvoiceSettingsSet()) {
// add submenu with Products
add_submenu_page(
'envoice',
'Products list',
'Products',
'manage_options',
'envoice-products',
'envoice_products_list_html'
);
}
}
/* Generate Settings page*/
function envoice_options_page_html()
{
// check if the user have submitted the settings
if (isset($_GET['settings-updated'])) {
// add settings saved message with the class of "updated"
echo '<div class="updated">
<p>Settings were saved!</p>
</div>';
}
?>
<div class="wrap">
<h1>Settings</h1>
<div id="envoice-container">
<div class="envoice-content">
<div class="header">
<i class="fas fa-lock"></i>
<h2>Plugin settings</h2>
<p>Settings to integrate with your existing Envoice account</p>
</div>
<div class="col-row">
<div class="col-left">
<div class="caption">
<span class="step">1</span>
<h3>Connect your Envoice account</h3>
<p>Providing your auth key and secret will allow the plugin to connect
to your Envoice account</p>
</div>
<p>
To get started configure your account details. You can find your API KEY
and
SECRET KEY on this <a href="https://www.envoice.in/account/settings#api-tab"
target="_blank">link.</a>
</p>
<form action="options.php" method="post">
<?php
settings_fields('envoice_fields');
?>
<div class="form-input">
<input class="text-input"
type="text" placeholder=""
id="auth_key" name="auth_key"
value="<?=get_option('auth_key')?>">
<label>API KEY</label>
<p class="info-text">
<i class="fas fa-info-circle orange-clr"></i>
API key is a public unique identifier for your app.
</p>
</div>
<div class="form-input">
<input class="text-input"
type="text" placeholder=""
id="auth_secret" name="auth_secret"
value="<?=get_option('auth_secret')?>">
<label>SECRET KEY</label>
<p class="info-text">
<i class="fas fa-info-circle orange-clr"></i>
Secret key is a secret shared between Envoice and your app.
</p>
</div>
<button type="submit" class="save-btn">Save</button>
</form>
</div>
<div class="col-right">
<div class="video-wrap">
<iframe width="100%" height="auto" src="https://www.youtube.com/watch?v=HZWdejjWA0g" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}
// hook for Envoice menu
add_action('admin_menu', 'envoice_section_page');
// register settings in Wordpress Settings API
function setup_fields()
{
// settings that should be stored via API
$fields = array(
array(
'uid' => 'auth_key',
'label' => 'Auth key',
'section' => 'api_section',
'type' => 'text',
'options' => false,
'placeholder' => 'API KEY',
'helper' => '',
),
array(
'uid' => 'auth_secret',
'label' => 'Auth secret',
'section' => 'api_section',
'type' => 'text',
'options' => false,
'placeholder' => 'SECRET KEY',
'helper' => '',
),
);
// go through each on and register it
foreach ($fields as $field) {
add_settings_field($field['uid'], $field['label'], null, 'envoice_fields', $field['section'], $field);
register_setting('envoice_fields', $field['uid']);
}
}
// register Envoice options section
add_action('admin_init', 'setup_fields');
// hook to load scripts and styles
add_action('admin_enqueue_scripts', 'load_assets');
// register assetsfor plugin
function load_assets($hook)
{
// load assets only on plugin page
if ($hook != 'toplevel_page_envoice' && $hook != 'envoice_page_envoice-products') {
return;
}
// include jsgrid script
wp_register_script(
'envoice-admin-jsgrid',
//'https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.js',
plugins_url('/assets/js/jsgrid.min.js', plugin_basename(__FILE__)),
array('jquery'),
ENVOICE_PLUGIN_VERSION
);
wp_enqueue_script('envoice-admin-jsgrid');
// clipboard js
wp_register_script(
'envoice-admin-clipboard',
plugins_url('/assets/js/clipboard.min.js', plugin_basename(__FILE__)),
array('jquery'),
ENVOICE_PLUGIN_VERSION
);
wp_enqueue_script('envoice-admin-clipboard');
// notifyjs
wp_register_script(
'envoice-admin-notify',
plugins_url('/assets/js/notify.min.js', plugin_basename(__FILE__)),
array('jquery'),
ENVOICE_PLUGIN_VERSION
);
wp_enqueue_script('envoice-admin-notify');
// admin scripts
wp_register_script(
'envoice-admin',
plugins_url('/assets/js/admin.js', plugin_basename(__FILE__)),
array('jquery'),
ENVOICE_PLUGIN_VERSION
);
wp_enqueue_script('envoice-admin');
// load styles
wp_enqueue_style(
'envoice-admin',
plugins_url('/assets/css/admin.css', plugin_basename(__FILE__)),
array(),
// ENVOICE_PLUGIN_VERSION
time()
);
// include font awesome
wp_enqueue_style(
'envoice-admin-fonts',
'https://use.fontawesome.com/releases/v5.3.1/css/all.css',
array(),
ENVOICE_PLUGIN_VERSION
);
// jsgrid styles
wp_enqueue_style(
'envoice-admin-jsgrid',
//'https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.css',
plugins_url('/assets/css/jsgrid.css', plugin_basename(__FILE__)),
array(),
ENVOICE_PLUGIN_VERSION
);
}
// helper, if plugin activated and API settings sets,
// add new button to TinyMCE to insert Shorcode with Product ID
if (isEnvoiceSettingsSet()) {
add_action('init', 'add_button');
}
/**
* Register a hook and create new table to store id-token pairsof products
* such as we use product Id in shortcodes, we have to know what is that
* and generate a correct URL for checkout page that work with product Token
*
* @todo: we can remove using database if checkout link will have Id instead
*/
register_activation_hook(__FILE__, 'install_envoice');
/**
* Create a table to store Id - AccessToken data
*
* @return void
*/
function install_envoice()
{
global $wpdb;
$table_name = $wpdb->prefix . 'envoice_products';
$charset_collate = $wpdb->get_charset_collate();
// database schema uid is Product Id
$sql = "CREATE TABLE $table_name (
uid mediumint(9) NOT NULL,
name tinytext NOT NULL,
token text NOT NULL,
PRIMARY KEY (uid)
) $charset_collate;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta($sql);
}
/**
* Add shortcode support
* example: [envoice product_id=123]
* will be replaced with <a href="[CHECKOUT_URL_WITH_PRODUCT_TOKEN]">Item name - Buy now</a>
*/
add_shortcode('envoice', 'envoice_shortcode');
/**
* Generate a special link for checkout
*
* @param mixed $atts Attributes from shortcode
*
* @return String $btn
*/
function envoice_shortcode($atts)
{
// get token by id from database
global $wpdb;
$uid = $atts['product_id'];
$item = $wpdb->get_row('select * from ' . $wpdb->prefix . 'envoice_products where uid=' . $uid);
// generate a button view
$btn = '<a href="https://www.envoice.in/secure/checkout?token=' . $item->token . '" class="envoice_btn"
style="width:300px; height:100px; background-color:#007bbd; padding:10px 40px; color: #fff; border-radius: 3px; text-decoration: none;">
' . $item->name . ' - Buy Now
</a>';
return $btn;
}
/**
* Add select2 support for shortcode insterting
*/
function enqueue_select2_jquery()
{
wp_register_style('select2css', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css', false, ENVOICE_PLUGIN_VERSION);
wp_register_script('select2', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js', array('jquery'), ENVOICE_PLUGIN_VERSION);
wp_enqueue_style('select2css');
wp_enqueue_script('select2');
}
// initi the scripts
add_action('admin_enqueue_scripts', 'enqueue_select2_jquery');
function select2jquery()
{
?>
<script type='text/javascript'>
jQuery(document).ready(function ($) {
// init select2 for media button
$( '#envoiceSelector' ).select2({
placeholder: 'Insert Envoice Product',
});
// manage on select event
$('#envoiceSelector').on('select2:select', function (e) {
var shortcode = '[envoice product_id="' + e.params.data.id + '"]';
// insert shorcode to tinyMCE
tinymce.editors.content.selection.setContent(shortcode);
// clear selected item
$('#envoiceSelector').val(null).trigger('change');
});
});
</script>
<?php
}
add_action('admin_head', 'select2jquery');
// add media button
add_action('media_buttons', 'add_envoice_media_button');
function add_envoice_media_button()
{
$api = new EnvoiceService(get_option('auth_key'), get_option('auth_secret'));
$products = $api->getProductsListJson();
$html = '<select id="envoiceSelector">';
$html .= '<option></option>';
foreach ($products as $p) {
$html .= '<option value="' . $p['Id'] . '">' . $p['Name'] . '</option>';
}
$html .= '</select>';
echo $html;
}