| 1 |
<?php |
| 2 |
/* |
| 3 |
* SOFTWARE LICENSE INFORMATION |
| 4 |
* |
| 5 |
* Copyright (c) 2017 Buttonizer, all rights reserved. |
| 6 |
* |
| 7 |
* This file is part of Buttonizer |
| 8 |
* |
| 9 |
* For detailed information regarding to the licensing of |
| 10 |
* this software, please review the license.txt or visit: |
| 11 |
* https://buttonizer.pro/license/ |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace Buttonizer\Legacy\Api\Buttons; |
| 15 |
|
| 16 |
use Buttonizer\Core\Utils\PermissionCheck; |
| 17 |
|
| 18 |
/** |
| 19 |
* Buttons API |
| 20 |
* |
| 21 |
* @endpoint /wp-json/buttonizer/buttons |
| 22 |
* @methods GET POST |
| 23 |
*/ |
| 24 |
class ApiButtons |
| 25 |
{ |
| 26 |
private $_settings = null; |
| 27 |
|
| 28 |
/** |
| 29 |
* Register route |
| 30 |
*/ |
| 31 |
public function registerRoute() |
| 32 |
{ |
| 33 |
register_rest_route('buttonizer', '/buttons', [ |
| 34 |
[ |
| 35 |
'methods' => ['GET'], |
| 36 |
'args' => [ |
| 37 |
'qpu' => [ |
| 38 |
'required' => false, |
| 39 |
"type" => "number" |
| 40 |
], |
| 41 |
'preview' => [ |
| 42 |
'required' => false, |
| 43 |
"type" => "boolean" |
| 44 |
], |
| 45 |
'data' => [ |
| 46 |
'required' => true, |
| 47 |
"type" => "object" |
| 48 |
], |
| 49 |
], |
| 50 |
'callback' => [$this, 'get'], |
| 51 |
'permission_callback' => '__return_true' |
| 52 |
], |
| 53 |
[ |
| 54 |
'methods' => ['POST'], |
| 55 |
'args' => [ |
| 56 |
'data' => [ |
| 57 |
'required' => true, |
| 58 |
'type' => "array", |
| 59 |
'items' => [ |
| 60 |
'type' => 'object' |
| 61 |
] |
| 62 |
], |
| 63 |
'nonce' => [ |
| 64 |
'validate_callback' => function ($value) { |
| 65 |
return wp_verify_nonce($value, 'wp_rest'); |
| 66 |
}, |
| 67 |
'required' => true |
| 68 |
], |
| 69 |
], |
| 70 |
'callback' => [$this, 'post'], |
| 71 |
'permission_callback' => function () { |
| 72 |
return PermissionCheck::hasPermission(); |
| 73 |
} |
| 74 |
] |
| 75 |
]); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get frontend data |
| 80 |
*/ |
| 81 |
public function get($isNoAjaxRequest = false) |
| 82 |
{ |
| 83 |
// Check if it's not in preview so it won't migrate twice in the dashboard. |
| 84 |
if (!isset($_GET['preview']) || (isset($_GET['preview']) && $_GET['preview'] != true)) { |
| 85 |
$this->_settings = get_option("buttonizer_settings"); |
| 86 |
|
| 87 |
// Migrate if we need to do that |
| 88 |
if (!isset($this->_settings['migration_version'])) { |
| 89 |
(new \Buttonizer\Legacy\Utils\Update())->run(); |
| 90 |
} else if ($this->_settings['migration_version'] !== BUTTONIZER_LAST_MIGRATION) { |
| 91 |
(new \Buttonizer\Legacy\Utils\Update())->selfMigrate($this->_settings['migration_version']); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// Allow XHR requests from subdomains |
| 96 |
if (!$isNoAjaxRequest && isset($this->_settings['allow_subdomains']) && $this->_settings['allow_subdomains'] == 'true' && isset($_SERVER['HTTP_ORIGIN'])) { |
| 97 |
$siteUrl = parse_url(get_site_url()); |
| 98 |
$currentUrl = parse_url($_SERVER['HTTP_ORIGIN']); |
| 99 |
|
| 100 |
if (isset($siteUrl['host']) && isset($currentUrl['host']) && $siteUrl['host'] === substr($currentUrl['host'], strlen($currentUrl['host']) - strlen($siteUrl['host']))) { |
| 101 |
header("Access-Control-Allow-Origin: " . $_SERVER['HTTP_ORIGIN']); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
return [ |
| 106 |
'plugin' => 'buttonizer', |
| 107 |
'status' => 'success', |
| 108 |
'result' => (new \Buttonizer\Legacy\Frontend\Buttonizer($isNoAjaxRequest))->returnArray(), |
| 109 |
'warning' => \Buttonizer\Legacy\Frontend\Buttonizer::getLogs(), |
| 110 |
// ButtonizerLicense() can be null in the free build (Freemius SDK stripped), so null-check before use. |
| 111 |
'premium' => (ButtonizerLicense() && ButtonizerLicense()->can_use_premium_code()) |
| 112 |
]; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Save dashboard button data |
| 117 |
*/ |
| 118 |
public function post($request) |
| 119 |
{ |
| 120 |
$data = json_decode($request->get_body(), true); |
| 121 |
|
| 122 |
// Data read correctly? |
| 123 |
if (!json_last_error() === JSON_ERROR_NONE) { |
| 124 |
return new \WP_Error('rest_invalid_data', 'The data couldn\'t be converted.', [ |
| 125 |
'status' => 400 |
| 126 |
]); |
| 127 |
} |
| 128 |
|
| 129 |
// Register settings |
| 130 |
register_setting('buttonizer', 'buttonizer_buttons', [ |
| 131 |
'type' => 'array', |
| 132 |
'sanitize_callback' => function ($value) { |
| 133 |
return is_array($value) ? $value : []; |
| 134 |
} |
| 135 |
]); |
| 136 |
register_setting('buttonizer', 'buttonizer_has_changes', [ |
| 137 |
'type' => 'boolean', |
| 138 |
'sanitize_callback' => 'rest_sanitize_boolean' |
| 139 |
]); |
| 140 |
|
| 141 |
// Save buttons |
| 142 |
update_option('buttonizer_buttons', $data['data']); |
| 143 |
update_option('buttonizer_has_changes', true); |
| 144 |
|
| 145 |
return [ |
| 146 |
'status' => 'success' |
| 147 |
]; |
| 148 |
} |
| 149 |
} |
| 150 |
|