· 5 years ago · Oct 30, 2020, 10:12 AM
1<?php
2/**
3 * Oxy-Marketplace
4 *
5 * @wordpress-plugin
6 * Plugin Name: Oxy-Marketplace
7 * Description: Access to design sets collections managed by the Asura plugin
8 * Version: 1.0.0
9 * Author: thelostasura
10 * Author URI: https://thelostasura.com/
11 * Requires at least: 5.5
12 * Tested up to: 5.5.1
13 * Requires PHP: 7.3
14 *
15 * @package Oxy-Marketplace
16 * @author thelostasura
17 * @link https://thelostasura.com/
18 * @since 1.0.0
19 * @copyright 2020 thelostasura
20 *
21 * Romans 12:12 (ASV)
22 * rejoicing in hope; patient in tribulation; continuing stedfastly in prayer;
23 *
24 * Roma 12:12 (TB)
25 * Bersukacitalah dalam pengharapan, sabarlah dalam kesesakan, dan bertekunlah dalam doa!
26 *
27 * https://alkitab.app/v/f27a6d7e714e
28 */
29
30defined( 'ABSPATH' ) || exit;
31
32define( 'ZL_VERSION', '1.0.0' );
33define( 'ZL_PLUGIN_FILE', __FILE__ );
34define( 'ZL_PLUGIN_DIR', __DIR__ );
35define( 'ZL_PLUGIN_URL', plugins_url( '', __FILE__ ) . '/' );
36
37require_once ZL_PLUGIN_DIR . '/vendor/autoload.php';
38
39use Illuminate\Cache\CacheManager;
40use Illuminate\Container\Container;
41use Illuminate\Filesystem\Filesystem;
42use Illuminate\Http\Client\Factory as HttpFactory;
43use Illuminate\Support\Carbon;
44use Illuminate\Support\Str;
45use Medoo\Medoo;
46
47
48/*
49|--------------------------------------------------------------------------
50| Activation & Deactivation Hook
51|--------------------------------------------------------------------------
52*/
53
54function activate_zl() {
55 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
56
57 global $wpdb;
58 $charset_collate = $wpdb->get_charset_collate();
59
60 $zl_providers = "CREATE TABLE {$wpdb->prefix}zl_providers (
61 id BIGINT NOT NULL AUTO_INCREMENT,
62 uid VARCHAR(255) NOT NULL,
63 site_title VARCHAR(255) NOT NULL,
64 provider VARCHAR(255) NOT NULL,
65 namespace VARCHAR(255) NOT NULL,
66 version VARCHAR(255) NOT NULL,
67 api_key VARCHAR(255) NOT NULL,
68 api_secret VARCHAR(255) NOT NULL,
69 status TINYINT(1) NOT NULL DEFAULT 1,
70 created_at TIMESTAMP NULL,
71 updated_at TIMESTAMP NULL,
72 PRIMARY KEY (id)
73 ) {$charset_collate};";
74 dbDelta( $zl_providers );
75
76 $zl_licenses = "CREATE TABLE {$wpdb->prefix}zl_licenses (
77 id BIGINT NOT NULL AUTO_INCREMENT,
78 uid VARCHAR(255) NOT NULL,
79 provider_id BIGINT NOT NULL,
80 license VARCHAR(255) NOT NULL,
81 hash VARCHAR(255) NOT NULL,
82 status TINYINT(1) NOT NULL DEFAULT 1,
83 expire_at TIMESTAMP NULL,
84 created_at TIMESTAMP NULL,
85 updated_at TIMESTAMP NULL,
86 PRIMARY KEY (id)
87 ) {$charset_collate};";
88 dbDelta( $zl_licenses );
89
90}
91register_activation_hook( __FILE__, 'activate_zl' );
92
93
94/*
95|--------------------------------------------------------------------------
96| Helper
97|--------------------------------------------------------------------------
98*/
99
100class ZLNotice
101{
102 protected $types = [
103 'error',
104 'success',
105 'warning',
106 'info',
107 ];
108
109 public function __construct() {}
110
111 public function init() {
112 foreach ( $this->types as $type ) {
113 $messages = get_transient( 'zl_notice_' . $type );
114
115 if ( $messages && is_array( $messages ) ) {
116 foreach ( $messages as $message ) {
117 echo sprintf(
118 '<div class="notice notice-%s is-dismissible"><p><b>Oxy-Marketplace</b>: %s</p></div>',
119 $type,
120 $message
121 );
122 }
123
124 delete_transient( 'zl_notice_' . $type );
125 }
126 }
127 }
128
129 public static function add( $level, $message, $code = 0, $duration = 60 ) {
130 $messages = get_transient( 'zl_notice_' . $level );
131
132 if ( $messages && is_array( $messages ) ) {
133 if (!in_array($message, $messages)) {
134 $messages[] = $message;
135 }
136 } else {
137 $messages = [ $message ];
138 }
139
140 set_transient( 'zl_notice_' . $level, $messages, $duration );
141 }
142
143 public static function error( $message ) {
144 self::add( 'error', $message );
145 }
146
147 public static function success( $message ) {
148 self::add( 'success', $message );
149 }
150
151 public static function warning( $message ) {
152 self::add( 'warning', $message );
153 }
154
155 public static function info( $message ) {
156 self::add( 'info', $message );
157 }
158}
159add_action('admin_notices', function() {
160 $notices = new ZLNotice();
161 $notices->init();
162});
163
164class ZLCache
165{
166 private static $instances = [];
167
168 private static $cache;
169
170 protected function __construct()
171 {
172 // Create a new Container object, needed by the cache manager.
173 $container = new Container;
174
175 // The CacheManager creates the cache "repository" based on config values
176 // which are loaded from the config class in the container.
177 // More about the config class can be found in the config component; for now we will use an array
178 $container['config'] = [
179 'cache.default' => 'file',
180 'cache.stores.file' => [
181 'driver' => 'file',
182 'path' => __DIR__ . '/storage/framework/cache/data'
183 ]
184 ];
185
186 // To use the file cache driver we need an instance of Illuminate's Filesystem, also stored in the container
187 $container['files'] = new Filesystem;
188
189 // Create the CacheManager
190 $cacheManager = new CacheManager($container);
191
192 // Get the default cache driver (file in this case)
193 self::$cache = $cacheManager->store();
194
195 // Or, if you have multiple drivers:
196 // $cache = $cacheManager->store('file');
197 }
198
199 protected function __clone() { }
200
201 public function __wakeup()
202 {
203 throw new \Exception("Cannot unserialize a singleton.");
204 }
205
206 public static function getInstance(): ZLCache
207 {
208 $cls = static::class;
209 if (!isset(self::$instances[$cls])) {
210 self::$instances[$cls] = new static();
211 }
212
213 return self::$instances[$cls];
214 }
215
216 public static function __callStatic($method, $args)
217 {
218 return self::getInstance()::$cache->{$method}(...$args);
219 }
220}
221
222class ZLDB
223{
224 private static $instances = [];
225
226 private static $medoo;
227
228 protected function __construct()
229 {
230 global $wpdb;
231 self::$medoo = new Medoo([
232 'database_type' => 'mysql',
233 'database_name' => $wpdb->dbname,
234 'server' => $wpdb->dbhost,
235 'username' => $wpdb->dbuser,
236 'password' => $wpdb->dbpassword,
237 'prefix' => $wpdb->prefix,
238 ]);
239 }
240
241 protected function __clone() { }
242
243 public function __wakeup()
244 {
245 throw new \Exception("Cannot unserialize a singleton.");
246 }
247
248 public static function getInstance(): ZLDB
249 {
250 $cls = static::class;
251 if (!isset(self::$instances[$cls])) {
252 self::$instances[$cls] = new static();
253 }
254
255 return self::$instances[$cls];
256 }
257
258 public static function __callStatic($method, $args)
259 {
260 return self::getInstance()::$medoo->{$method}(...$args);
261 }
262}
263
264class ZLHttp
265{
266 private static $instances = [];
267
268 private static $client;
269
270 protected function __construct()
271 {
272 self::$client = new HttpFactory();
273 }
274
275 protected function __clone() { }
276
277 public function __wakeup()
278 {
279 throw new \Exception("Cannot unserialize a singleton.");
280 }
281
282 public static function getInstance(): ZLHttp
283 {
284 $cls = static::class;
285 if (!isset(self::$instances[$cls])) {
286 self::$instances[$cls] = new static();
287 }
288
289 return self::$instances[$cls];
290 }
291
292 public static function __callStatic($method, $args)
293 {
294 return self::getInstance()::$client->{$method}(...$args);
295 }
296}
297
298function zl_redirect_js($location)
299{
300 echo "<script>window.location.href='{$location}';</script>";
301}
302
303
304/*
305|--------------------------------------------------------------------------
306| Scripts & Styles
307|--------------------------------------------------------------------------
308*/
309
310wp_register_style('fontawesome_css', 'https://kit-pro.fontawesome.com/releases/v5.15.1/css/pro.css', [], null);
311
312/*
313|--------------------------------------------------------------------------
314| Providers & License
315|--------------------------------------------------------------------------
316*/
317
318
319function zl_source_sites()
320{
321 $sources_sites = [];
322 $providers = ZLDB::select('zl_providers', '*');
323
324 foreach ($providers as $provider) {
325 $licenses = ZLDB::select('zl_licenses', '*', [
326 'provider_id' => $provider['id'],
327 ]);
328
329 foreach ($licenses as $license) {
330 $tmp_term = ZLCache::remember("terms_{$license['uid']}", Carbon::now()->addHour(), function() use ($provider, $license) {
331 $response = ZLHttp::acceptJson()->get("{$provider['provider']}/wp-json/{$provider['namespace']}/{$provider['version']}/licenses/{$license['license']}/terms", [
332 'api_key' => $provider['api_key'],
333 'api_secret' => $provider['api_secret'],
334 'domain' => home_url(),
335 ]);
336 $rBody = json_decode($response->body());
337
338 if ( !$response->successful() ) {
339 ZLNotice::error("<code>{$rBody->code}</code>: {$rBody->message} ");
340 } else {
341 return $rBody;
342 }
343 });
344
345
346 foreach ($tmp_term as $term) {
347 $sources_sites["tla_{$provider['uid']}_{$license['uid']}_{$term->slug}"] = [
348 'label' => ucfirst($term->name). " [{$provider['provider']}]",
349 'url' => $provider['provider'],
350 'accesskey' => '',
351 'system' => true
352 ];
353
354 }
355
356 }
357 }
358
359 return $sources_sites;
360}
361
362if ( defined( 'CT_VERSION' ) ) {
363 global $ct_source_sites;
364 $zl_site = zl_source_sites();
365 $ct_source_sites = array_merge($zl_site, $ct_source_sites);
366
367 // dd($ct_source_sites);
368}
369
370/*
371|--------------------------------------------------------------------------
372| Asura API
373|--------------------------------------------------------------------------
374*/
375
376
377function zl_get_items_from_source()
378{
379 $name = isset( $_REQUEST['name'] ) ? sanitize_text_field( $_REQUEST['name'] ) : false;
380
381 if ( ! Str::startsWith( $name, 'tla_' ) ) {
382 return ct_get_items_from_source();
383 }
384 $name = Str::replaceFirst( 'tla_', '', $name );
385
386 $zl_pair = Str::of( $name )->explode( '_', 3 );
387
388 list($provider_uid, $license_uid, $slug) = $zl_pair;
389
390 $provider = ZLDB::get('zl_providers', '*', [
391 'uid' => $provider_uid,
392 ]);
393
394 if (!$provider) {
395 exit;
396 }
397
398 $license = ZLDB::get('zl_licenses', '*', [
399 'provider_id' => $provider['id'],
400 'uid' => $license_uid,
401 ]);
402
403 if (!$license) {
404 exit;
405 }
406
407 $data = ZLCache::remember("getItemsFromSource_{$provider['uid']}_{$license['uid']}_{$slug}", Carbon::now()->addMinutes(5), function() use ($provider, $license, $slug) {
408 $response = ZLHttp::acceptJson()->get("{$provider['provider']}/wp-json/{$provider['namespace']}/{$provider['version']}/oxygenbuilder/items", [
409 'api_key' => $provider['api_key'],
410 'api_secret' => $provider['api_secret'],
411 'domain' => home_url(),
412 'hash' => $license['hash'],
413 'term' => $slug,
414 ]);
415 $rBody = json_decode($response->body());
416 if ( $response->successful() ) {
417 foreach ($rBody->components as $key => $component) {
418 $rBody->components[$key]->source = "tla_{$provider['uid']}_{$license['uid']}_{$slug}";
419 }
420 foreach ($rBody->pages as $key => $page) {
421 $rBody->pages[$key]->source = "tla_{$provider['uid']}_{$license['uid']}_{$slug}";
422 }
423 return $rBody;
424 }
425 });
426
427 if ($data) {
428 return wp_send_json($data);
429 }
430
431}
432
433
434function zl_get_page_from_source()
435{
436 $source = isset( $_REQUEST['source'] ) ? sanitize_text_field( base64_decode($_REQUEST['source']) ) : false;
437
438 if ( ! Str::startsWith( $source, 'tla_' ) ) {
439 return ct_get_page_from_source();
440 }
441 $source = Str::replaceFirst( 'tla_', '', $source );
442
443 $zl_pair = Str::of( $source )->explode( '_', 3 );
444
445 list($provider_uid, $license_uid, $slug) = $zl_pair;
446
447 $provider = ZLDB::get('zl_providers', '*', [
448 'uid' => $provider_uid,
449 ]);
450
451 if (!$provider) {
452 exit;
453 }
454
455 $license = ZLDB::get('zl_licenses', '*', [
456 'provider_id' => $provider['id'],
457 'uid' => $license_uid,
458 ]);
459
460 if (!$license) {
461 exit;
462 }
463
464 $data = ZLCache::remember("getPageFromSource_{$provider['uid']}_{$license['uid']}_{$slug}_{$_REQUEST['id']}", Carbon::now()->addMinutes(5), function() use ($provider, $license, $slug) {
465 $response = ZLHttp::acceptJson()->get("{$provider['provider']}/wp-json/{$provider['namespace']}/{$provider['version']}/oxygenbuilder/pagesclasses/{$_REQUEST['id']}", [
466 'api_key' => $provider['api_key'],
467 'api_secret' => $provider['api_secret'],
468 'domain' => home_url(),
469 'hash' => $license['hash'],
470 'term' => $slug,
471 ]);
472
473 $components = [];
474 $classes = [];
475 $colors = [];
476 $lookupTable = [];
477
478 $rBody = json_decode($response->body(), true);
479
480 if ( $response->successful() ) {
481 if(isset($rBody['components'])){
482 $components = $rBody['components'];
483 }
484 if(isset($rBody['classes']))
485 $classes = $rBody['classes'];
486 if(isset($rBody['colors']))
487 $colors = $rBody['colors'];
488 if(isset($rBody['lookuptable']))
489 $lookupTable = $rBody['lookuptable'];
490 }
491
492 foreach ($components as $key => $component) {
493
494 // if it is a reusable do something about it.
495 if($component['name'] === 'ct_reusable') {
496 unset($components[$key]);
497 }
498
499 if(!isset($components[$key])) {
500 continue; // it could have bene deleted while dealing with a reusable in the previous step
501 }
502
503 $component[$key] = ct_base64_encode_decode_tree([$component], true)[0];
504
505 if(isset($component['children'])) {
506 if(is_array($components[$key]['children'])) {
507 $components[$key]['children'] = ct_recursively_manage_reusables($components[$key]['children'], [], 'asura');
508 }
509 }
510
511 }
512
513 $output = [
514 'components' => $components
515 ];
516
517 if(sizeof($classes) > 0) {
518 $output['classes'] = $classes;
519 }
520
521 if(sizeof($colors) > 0) {
522 $output['colors'] = $colors;
523 }
524
525 if(sizeof($lookupTable) > 0) {
526 $output['lookuptable'] = $lookupTable;
527 }
528
529 return $output;
530
531 });
532
533 if ($data) {
534 return wp_send_json($data);
535 }
536
537}
538
539function zl_get_component_from_source()
540{
541 $source = isset( $_REQUEST['source'] ) ? sanitize_text_field( base64_decode($_REQUEST['source']) ) : false;
542
543 if ( ! Str::startsWith( $source, 'tla_' ) ) {
544 return ct_get_component_from_source();
545 }
546 $source = Str::replaceFirst( 'tla_', '', $source );
547
548 $zl_pair = Str::of( $source )->explode( '_', 3 );
549
550 list($provider_uid, $license_uid, $slug) = $zl_pair;
551
552 $provider = ZLDB::get('zl_providers', '*', [
553 'uid' => $provider_uid,
554 ]);
555
556 if (!$provider) {
557 exit;
558 }
559
560 $license = ZLDB::get('zl_licenses', '*', [
561 'provider_id' => $provider['id'],
562 'uid' => $license_uid,
563 ]);
564
565 if (!$license) {
566 exit;
567 }
568
569 $data = ZLCache::remember("getComponentFromSource_{$provider['uid']}_{$license['uid']}_{$slug}_{$_REQUEST['id']}_{$_REQUEST['page']}", Carbon::now()->addMinutes(5), function() use ($provider, $license, $slug) {
570 $response = ZLHttp::acceptJson()->get("{$provider['provider']}/wp-json/{$provider['namespace']}/{$provider['version']}/oxygenbuilder/componentsclasses/{$_REQUEST['id']}/{$_REQUEST['page']}", [
571 'api_key' => $provider['api_key'],
572 'api_secret' => $provider['api_secret'],
573 'domain' => home_url(),
574 'hash' => $license['hash'],
575 'term' => $slug,
576 ]);
577
578 $component = [];
579 $classes = [];
580 $colors = [];
581 $lookupTable = [];
582
583 $rBody = json_decode($response->body(), true);
584
585 if ( $response->successful() ) {
586 if(isset($rBody['component']))
587 $component = $rBody['component'];
588 if(isset($rBody['classes']))
589 $classes = $rBody['classes'];
590 if(isset($rBody['colors']))
591 $colors = $rBody['colors'];
592 if(isset($rBody['lookuptable']))
593 $lookupTable = $rBody['lookuptable'];
594 }
595
596 $component = ct_base64_encode_decode_tree([$component], true)[0];
597
598 $output = [
599 'component' => $component
600 ];
601
602 if(sizeof($classes) > 0) {
603 $output['classes'] = $classes;
604 }
605
606 if(sizeof($colors) > 0) {
607 $output['colors'] = $colors;
608 }
609
610 if(sizeof($lookupTable) > 0) {
611 $output['lookuptable'] = $lookupTable;
612 }
613
614 return $output;
615
616 });
617
618 if ($data) {
619 return wp_send_json($data);
620 }
621
622}
623
624/*
625|--------------------------------------------------------------------------
626| Ajax
627|--------------------------------------------------------------------------
628*/
629
630function zl_new_style_api_call() {
631 $call_type = isset( $_REQUEST['call_type'] ) ? sanitize_text_field( $_REQUEST['call_type'] ) : false;
632
633 ct_new_style_api_call_security_check( $call_type );
634
635 switch( $call_type ) {
636 case 'setup_default_data':
637 ct_setup_default_data();
638 break;
639 case 'get_component_from_source':
640 zl_get_component_from_source();
641 break;
642 case 'get_page_from_source':
643 zl_get_page_from_source();
644 break;
645 case 'get_items_from_source':
646 zl_get_items_from_source();
647 break;
648 case 'get_stuff_from_source':
649 ct_get_stuff_from_source();
650 break;
651 }
652
653 die();
654}
655
656remove_action( 'wp_ajax_ct_new_style_api_call', 'ct_new_style_api_call' );
657add_action( 'wp_ajax_ct_new_style_api_call', 'zl_new_style_api_call' );
658
659
660/*
661|--------------------------------------------------------------------------
662| AdminMenu
663|--------------------------------------------------------------------------
664*/
665
666function zl_providers_page()
667{
668 add_submenu_page(
669 'ct_dashboard_page',
670 'Oxy-Marketplace Providers',
671 'Oxy-Marketplace Providers',
672 'manage_options',
673 'zl',
674 'zl_providers_page_callback'
675 );
676
677 add_submenu_page(
678 null,
679 'License',
680 'License',
681 'manage_options',
682 'zl_licenses',
683 'zl_licenses_page_callback'
684 );
685}
686add_action('admin_menu', 'zl_providers_page');
687
688function zl_licenses_page_callback()
689{
690 wp_enqueue_style('fontawesome_css');
691
692 if ( ! isset( $_REQUEST['provider'] ) || empty( $_REQUEST['provider'] ) ) {
693 zl_redirect_js( add_query_arg( 'page', 'zl', get_admin_url().'admin.php' ) );
694 exit;
695 }
696
697 $provider = ZLDB::get('zl_providers', '*', [
698 'id' => $_REQUEST['provider'],
699 ]);
700
701 if (!$provider) {
702 zl_redirect_js( add_query_arg( 'page', 'zl', get_admin_url().'admin.php' ) );
703 exit;
704 }
705
706 if (
707 $_SERVER['REQUEST_METHOD'] === 'GET'
708 && isset($_REQUEST['action'])
709 && $_REQUEST['action'] == 'sync'
710 && wp_verify_nonce( $_REQUEST['_wpnonce'], 'zl_licenses_sync' )
711 ) {
712
713 $lics = ZLDB::select('zl_licenses', '*', [
714 'provider_id' => $provider['id'],
715 ]);
716
717 if ($lics) {
718 foreach ($lics as $lic) {
719
720 $url = "{$provider['provider']}/wp-json/{$provider['namespace']}/{$provider['version']}";
721 $response = ZLHttp::acceptJson()->post("{$url}/licenses/{$lic['license']}/activate", [
722 'api_key' => $provider['api_key'],
723 'api_secret' => $provider['api_secret'],
724 'domain' => home_url(),
725 ]);
726 $rBody = json_decode($response->body());
727
728 if ( !$response->successful() ) {
729 ZLNotice::error("<code><b>{$lic['license']}</b></code>: <code>{$rBody->code}</code>: {$rBody->message} ");
730 } else {
731 $update = ZLDB::update('zl_licenses', [
732 'hash' => $rBody->hash,
733 'expire_at' => $rBody->expire_at ? Carbon::parse($rBody->expire_at)->toDateTimeString() : null,
734 ], [
735 'provider_id' => $provider['id'],
736 'id' => $lic['id'],
737 ]);
738 }
739
740 }
741
742 ZLCache::flush();
743
744 ZLNotice::success('Successfully sync Licenses\' data.');
745 ZLNotice::warning('Sync the Licenses\' data may temporarily degrade performance for your website and increase load on your server');
746 zl_redirect_js( add_query_arg( [
747 'page' => 'zl_licenses',
748 'provider' => $provider['id']
749 ], get_admin_url().'admin.php' ) );
750 exit;
751 }
752
753
754
755 }
756
757
758 if (
759 $_SERVER['REQUEST_METHOD'] === 'GET'
760 && isset($_REQUEST['action'])
761 && $_REQUEST['action'] == 'revoke'
762 && isset($_REQUEST['license'])
763 && wp_verify_nonce( $_REQUEST['_wpnonce'], 'zl_revoke_license' )
764 ) {
765 $exist = ZLDB::get('zl_licenses', '*', [
766 'provider_id' => $provider['id'],
767 'id' => $_REQUEST['license'],
768 ]);
769
770 if ($exist) {
771 ZLDB::delete('zl_licenses', [
772 'provider_id' => $provider['id'],
773 'id' => $_REQUEST['license'],
774 ]);
775
776 ZLCache::flush();
777 ZLNotice::success( 'The license key removed from database' );
778 } else {
779 ZLNotice::error( 'The license key not exist in database' );
780 }
781
782 zl_redirect_js( add_query_arg( [
783 'page' => 'zl_licenses',
784 'provider' => $provider['id']
785 ], get_admin_url().'admin.php' ) );
786 exit;
787 }
788
789 if (
790 $_SERVER['REQUEST_METHOD'] === 'POST'
791 && isset($_REQUEST['zl_license_key'])
792 && !empty($_REQUEST['zl_license_key'])
793 && wp_verify_nonce( $_REQUEST['_wpnonce'], 'zl_add_license' )
794 ) {
795
796 $license_key = $_REQUEST['zl_license_key'];
797
798 $exist = ZLDB::get('zl_licenses', '*', [
799 'provider_id' => $provider['id'],
800 'license' => $license_key,
801 ]);
802
803 if ($exist) {
804 ZLNotice::error( 'The license key already exist in database' );
805 zl_redirect_js( add_query_arg( [
806 'page' => 'zl_licenses',
807 'provider' => $provider['id']
808 ], get_admin_url().'admin.php' ) );
809 exit;
810 }
811
812 $url = "{$provider['provider']}/wp-json/{$provider['namespace']}/{$provider['version']}";
813 $response = ZLHttp::acceptJson()->post("{$url}/licenses/{$license_key}/activate", [
814 'api_key' => $provider['api_key'],
815 'api_secret' => $provider['api_secret'],
816 'domain' => home_url(),
817 ]);
818 $rBody = json_decode($response->body());
819
820 if ( !$response->successful() ) {
821
822 ZLNotice::error("<code>{$rBody->code}</code>: {$rBody->message} ");
823 zl_redirect_js( add_query_arg( [
824 'page' => 'zl_licenses',
825 'provider' => $provider['id']
826 ], get_admin_url().'admin.php' ) );
827 exit;
828
829 } else {
830 $insert = ZLDB::insert('zl_licenses', [
831 'uid' => Str::random(5),
832 'provider_id' => $provider['id'],
833 'license' => $rBody->key,
834 'hash' => $rBody->hash,
835 'expire_at' => $rBody->expire_at ? Carbon::parse($rBody->expire_at)->toDateTimeString() : null,
836 'created_at' => Carbon::now()->toDateTimeString(),
837 ]);
838
839 if (!$insert) {
840 ZLNotice::error( 'Failed to add the provider to database' );
841 zl_redirect_js( add_query_arg( [
842 'page' => 'zl_licenses',
843 'provider' => $provider['id']
844 ], get_admin_url().'admin.php' ) );
845 exit;
846 }
847
848 }
849
850 ZLCache::flush();
851 ZLNotice::success('License key added successfully.');
852 zl_redirect_js( add_query_arg( [
853 'page' => 'zl_licenses',
854 'provider' => $provider['id']
855 ], get_admin_url().'admin.php' ) );
856 exit;
857
858 // return ZLDB::id();
859 }
860
861 $licenses = ZLCache::remember( 'licenses', Carbon::now()->addMinutes( 10 ), function() use ($provider) {
862 return ZLDB::select( 'zl_licenses', '*', [
863 'provider_id' => $provider['id']
864 ] );
865 } );
866
867 ?>
868
869
870<div class="wrap nosubsub">
871 <h1 class="wp-heading-inline">Oxy-Marketplace</h1>
872 <h2 class="wp-heading-inline">Licenses — <?php echo "{$provider['site_title']} ({$provider['provider']})"; ?> </h2>
873
874 <a href="<?php echo add_query_arg( 'page', 'zl', get_admin_url().'admin.php' ); ?>"><i class="fas fa-long-arrow-alt-left"></i> Back To Provider Page</a>
875
876 <hr class="wp-header-end">
877
878 <div id="col-container" class="wp-clearfix">
879
880 <div id="col-left" style="width: 25%;">
881 <div class="col-wrap">
882 <div class="form-wrap">
883 <h2>Add License</h2>
884 <form method="post">
885 <?php wp_nonce_field('zl_add_license');?>
886
887 <div class="form-field form-required term-name-wrap">
888 <label for="zl_license_key">License Key</label>
889 <input name="zl_license_key" id="zl_license_key" type="text" value="" size="40" aria-required="true">
890 <p>Get it from your email or customer page after you purchase a design set.</p>
891 </div>
892 <?php submit_button('Add New License'); ?>
893 </form>
894 </div>
895 </div>
896 </div>
897 <div id="col-right" style="width: 75%;">
898 <div class="col-wrap">
899 <table class="wp-list-table widefat fixed striped table-view-list tags">
900 <thead>
901 <tr>
902 <th scope="col" id="licensekey" class="manage-column column-licensekey">
903 <span>License Key</span>
904 </th>
905 <th scope="col" id="expire" class="manage-column column-expire">
906 <span>Expire At</span>
907 </th>
908 <th scope="col" id="terms" class="manage-column column-terms">
909 <span>Design Sets</span>
910 </th>
911 </tr>
912 </thead>
913 <tbody id="the-list">
914 <?php foreach ($licenses as $key => $license): ?>
915 <tr class="level-0">
916
917 <td class="name column-name has-row-actions column-primary">
918 <strong>
919 <a class="row-title" title="<?php echo $license['uid']; ?>"> <?php echo $license['license']; ?> </a>
920 </strong>
921 <br>
922 <div class="row-actions">
923 <span class="edit">
924 <a href="">Edit</a> |
925 </span>
926 <span class="delete">
927 <a href="<?php
928 echo add_query_arg([
929 'page' => 'zl_licenses',
930 'provider' => $provider['id'],
931 'license' => $license['id'],
932 'action' => 'revoke',
933 '_wpnonce' => wp_create_nonce( 'zl_revoke_license' )
934 ], get_admin_url().'admin.php');
935 ?>" class="delete-tag">Revoke</a>
936 </span>
937 <!-- <span class="view">
938 <a href="<?php
939 echo add_query_arg([
940 'page' => 'zl_licenses',
941 'provider' => $provider['id'],
942 ], get_admin_url().'admin.php');
943 ?>">License Keys</a>
944 </span> -->
945 </div>
946 </td>
947
948 <td>
949 <?php if ($license['expire_at']): ?>
950 <a style="<?php echo (Carbon::parse($license['expire_at'])->lessThan(Carbon::today())) ? 'color: red;' : ''; ?>"
951 >
952 <?php echo Carbon::parse($license['expire_at'])->format('M d, Y'); ?>
953 </a>
954 <?php endif; ?>
955 </td>
956
957 <td>
958 <?php
959 $tmp_term = ZLCache::remember("terms_{$license['uid']}", Carbon::now()->addHour(), function() use ($provider, $license) {
960 $response = ZLHttp::acceptJson()->get("{$provider['provider']}/wp-json/{$provider['namespace']}/{$provider['version']}/licenses/{$license['license']}/terms", [
961 'api_key' => $provider['api_key'],
962 'api_secret' => $provider['api_secret'],
963 'domain' => home_url(),
964 ]);
965 $rBody = json_decode($response->body());
966
967 if ( !$response->successful() ) {
968 ZLNotice::error("<code>{$rBody->code}</code>: {$rBody->message} ");
969 } else {
970 return $rBody;
971 }
972 });
973
974 // dd($tmp_term);
975
976 foreach ($tmp_term as $term): ?>
977
978 <b> <?php echo ucfirst($term->name); ?> </b>
979 — expire:
980 <?php if ($term->pivot->expire_at): ?>
981 <a style="<?php echo (Carbon::parse($term->pivot->expire_at)->lessThan(Carbon::today())) ? 'color: red;' : ''; ?>"
982 >
983 <?php echo Carbon::parse($term->pivot->expire_at)->format('M d, Y'); ?>
984 </a>
985 <?php else: ?>
986 non-expiring
987 <?php endif; ?>
988 <br/>
989 <?php endforeach; ?>
990
991
992
993 </td>
994
995
996
997 </tr>
998 <?php endforeach; ?>
999 </tbody>
1000
1001 </table>
1002
1003 <div>
1004 <div style="float: left;padding-top:10px;">
1005 <span style="padding:4px;">
1006 <a title="Force Oxy-Marketplace to sync data from provider's server." href="<?php
1007 echo add_query_arg([
1008 'page' => 'zl_licenses',
1009 'provider' => $provider['id'],
1010 'action' => 'sync',
1011 '_wpnonce' => wp_create_nonce( 'zl_licenses_sync' )
1012 ], get_admin_url().'admin.php');
1013 ?>" style="color:#444444;text-decoration: none !important;">
1014 <i class="fas fa-sync fa-lg" style="color: #dc3545"></i>
1015 <b>Sync Licenses' data</b>
1016 </a>
1017 </span>
1018 </div>
1019 <div style="float: right;padding-top:10px;">
1020 <span style="padding:4px;background-color:#fff1a8;transition: opacity 0.4s ease-out 0s; opacity: 1;">
1021 <a target="_blank" href="https://thelostasura.com/go/asura" style="color:#444444;text-decoration: none !important;">
1022 <i class="fas fa-ad fa-lg"></i> Powered by
1023 <b> Oxy-Marketplace</b>
1024 </a>
1025 </span>
1026 </div>
1027 </div>
1028 </div>
1029 </div>
1030 </div>
1031</div>
1032
1033 <?php
1034}
1035
1036function zl_providers_page_callback()
1037{
1038 wp_enqueue_style('fontawesome_css');
1039
1040 if (
1041 $_SERVER['REQUEST_METHOD'] === 'GET'
1042 && isset($_REQUEST['action'])
1043 && $_REQUEST['action'] == 'purge'
1044 && wp_verify_nonce( $_REQUEST['_wpnonce'], 'zl_purge' )
1045 ) {
1046 ZLCache::flush();
1047
1048 ZLNotice::success('Successfully purged cache');
1049 ZLNotice::warning('Purging the cache may temporarily degrade performance for your website and increase load on your server');
1050 zl_redirect_js( add_query_arg( 'page', 'zl', get_admin_url().'admin.php' ) );
1051 exit;
1052 }
1053
1054 if (
1055 $_SERVER['REQUEST_METHOD'] === 'POST'
1056 && wp_verify_nonce( $_REQUEST['_wpnonce'], 'zl_add_provider' )
1057 ) {
1058
1059 if( ! base64_decode( $_REQUEST['zl_provider_string'] ) ) {
1060 ZLNotice::error( 'Provider String should be base64 encoded string.' );
1061 zl_redirect_js( add_query_arg( 'page', 'zl', get_admin_url().'admin.php' ) );
1062 exit;
1063 }
1064
1065 if ( ! is_scalar( $_REQUEST['zl_provider_string'] ) && ! method_exists( $_REQUEST['zl_provider_string'], '__toString' ) ) {
1066 ZLNotice::error( 'Zoro String should be json string. [1]' );
1067 zl_redirect_js( add_query_arg( 'page', 'zl', get_admin_url().'admin.php' ) );
1068 exit;
1069 }
1070
1071 $provider = json_decode( base64_decode( $_REQUEST['zl_provider_string'] ) );
1072
1073 if ( json_last_error() !== JSON_ERROR_NONE ) {
1074 ZLNotice::error( 'Zoro String should be json string. [2]' );
1075 zl_redirect_js( add_query_arg( 'page', 'zl', get_admin_url().'admin.php' ) );
1076 exit;
1077 }
1078
1079 if ( ! is_object( $provider )
1080 || ! isset( $provider->api_key )
1081 || ! isset( $provider->api_secret )
1082 || ! isset( $provider->site_title )
1083 || ! isset( $provider->provider )
1084 || ! isset( $provider->namespace )
1085 || ! isset( $provider->version )
1086 ) {
1087 ZLNotice::error( 'Zoro String doesn\'t contain connection config' );
1088 zl_redirect_js( add_query_arg( 'page', 'zl', get_admin_url().'admin.php' ) );
1089 exit;
1090 }
1091
1092 $exist = ZLDB::get('zl_providers', '*', [
1093 'provider' => $provider->provider,
1094 ]);
1095
1096 if ($exist) {
1097 ZLNotice::error( 'The provider already exist in database' );
1098 zl_redirect_js( add_query_arg( 'page', 'zl', get_admin_url().'admin.php' ) );
1099 exit;
1100 }
1101
1102 $insert = ZLDB::insert('zl_providers', [
1103 'uid' => Str::random(5),
1104 'api_key' => $provider->api_key,
1105 'api_secret' => $provider->api_secret,
1106 'site_title' => $provider->site_title,
1107 'provider' => $provider->provider,
1108 'namespace' => $provider->namespace,
1109 'version' => $provider->version,
1110 'created_at' => Carbon::now()->toDateTimeString(),
1111 ]);
1112
1113
1114 if (!$insert) {
1115 ZLNotice::error( 'Failed to add the provider to database' );
1116 zl_redirect_js( add_query_arg( 'page', 'zl', get_admin_url().'admin.php' ) );
1117 exit;
1118 }
1119
1120 ZLCache::flush();
1121
1122 ZLNotice::success( 'Success added the provider to database' );
1123 zl_redirect_js( add_query_arg( 'page', 'zl', get_admin_url().'admin.php' ) );
1124 exit;
1125
1126 // return ZLDB::id();
1127
1128 }
1129
1130 if (
1131 $_SERVER['REQUEST_METHOD'] === 'GET'
1132 && isset($_REQUEST['action'])
1133 && $_REQUEST['action'] == 'revoke'
1134 && isset($_REQUEST['provider'])
1135 && wp_verify_nonce( $_REQUEST['_wpnonce'], 'zl_revoke_provider' )
1136 ) {
1137 $exist = ZLDB::get('zl_providers', '*', [
1138 'id' => $_REQUEST['provider'],
1139 ]);
1140
1141 if ($exist) {
1142 ZLDB::delete('zl_licenses', [
1143 'provider_id' => $_REQUEST['provider'],
1144 ]);
1145
1146 ZLDB::delete('zl_providers', [
1147 'id' => $_REQUEST['provider'],
1148 ]);
1149
1150 ZLCache::flush();
1151 ZLNotice::success( 'The provider removed from database' );
1152 } else {
1153 ZLNotice::error( 'The provider not exist in database' );
1154 }
1155
1156 zl_redirect_js( add_query_arg( 'page', 'zl', get_admin_url().'admin.php' ) );
1157 exit;
1158 }
1159
1160 $providers = ZLCache::remember( 'providers', Carbon::now()->addMinutes( 10 ), function() {
1161 return ZLDB::select( 'zl_providers', '*' );
1162 } );
1163
1164 ?>
1165
1166<div class="wrap nosubsub">
1167 <h1 class="wp-heading-inline">Oxy-Marketplace</h1>
1168 <h2 class="wp-heading-inline">Providers</h2>
1169
1170 <hr class="wp-header-end">
1171
1172 <div id="col-container" class="wp-clearfix">
1173
1174 <div id="col-left">
1175 <div class="col-wrap">
1176 <div class="form-wrap">
1177 <h2>Add Provider</h2>
1178 <form method="post">
1179 <?php wp_nonce_field('zl_add_provider');?>
1180
1181 <div class="form-field form-required term-name-wrap">
1182 <label for="zl_provider_string">Zoro String</label>
1183 <input name="zl_provider_string" id="zl_provider_string" type="text" value="" size="40" aria-required="true">
1184 <p>Ask your design set seller.</p>
1185 </div>
1186 <?php submit_button('Add New Provider'); ?>
1187 </form>
1188 </div>
1189 </div>
1190 </div>
1191 <div id="col-right">
1192 <div class="col-wrap">
1193 <table class="wp-list-table widefat fixed striped table-view-list tags">
1194 <thead>
1195 <tr>
1196 <th scope="col" id="provider" class="manage-column column-provider">
1197 <span>Provider</span>
1198 </th>
1199 </tr>
1200 </thead>
1201 <tbody id="the-list">
1202 <?php foreach ($providers as $key => $provider): ?>
1203 <tr class="level-0">
1204
1205 <td class="name column-name has-row-actions column-primary">
1206 <strong>
1207 <a class="row-title" title="<?php echo $provider['uid']; ?>"> <?php echo "{$provider['site_title']} ({$provider['provider']})"; ?> </a>
1208 </strong>
1209 <br>
1210 <div class="row-actions">
1211 <span class="edit">
1212 <a href="">Edit</a> |
1213 </span>
1214 <span class="delete">
1215 <a href="<?php
1216 echo add_query_arg([
1217 'page' => 'zl',
1218 'provider' => $provider['id'],
1219 'action' => 'revoke',
1220 '_wpnonce' => wp_create_nonce( 'zl_revoke_provider' )
1221 ], get_admin_url().'admin.php');
1222 ?>" class="delete-tag">Revoke</a> |
1223 </span>
1224 <span class="view">
1225 <a href="<?php
1226 echo add_query_arg([
1227 'page' => 'zl_licenses',
1228 'provider' => $provider['id'],
1229 ], get_admin_url().'admin.php');
1230 ?>">License Keys</a>
1231 </span>
1232 </div>
1233 </td>
1234 </tr>
1235 <?php endforeach; ?>
1236 </tbody>
1237
1238 </table>
1239 <div>
1240 <div style="float: left;padding-top:10px;">
1241 <span style="padding:4px;">
1242 <a title="Clear cached files to force Zoro to fetch a fresh version of those datas." href="<?php
1243 echo add_query_arg([
1244 'page' => 'zl',
1245 'action' => 'purge',
1246 '_wpnonce' => wp_create_nonce( 'zl_purge' )
1247 ], get_admin_url().'admin.php');
1248 ?>" style="color:#444444;text-decoration: none !important;">
1249 <i class="fas fa-broom fa-lg" style="color: #dc3545"></i>
1250 <b>Purge cache</b>
1251 </a>
1252 </span>
1253 </div>
1254 <div style="float: right;padding-top:10px;">
1255 <span style="padding:4px;background-color:#fff1a8;transition: opacity 0.4s ease-out 0s; opacity: 1;">
1256 <a target="_blank" href="https://thelostasura.com/go/asura" style="color:#444444;text-decoration: none !important;">
1257 <i class="fas fa-ad fa-lg"></i> Powered by
1258 <b> Oxy-Marketplace</b>
1259 </a>
1260 </span>
1261 </div>
1262 </div>
1263 </div>
1264 </div>
1265 </div>
1266</div>
1267 <?php
1268}
1269