namespace MO_CAW\Common\Settings;
use MO_CAW\Common\Constants;
use MO_CAW\Common\DB_Utils;
class SQL_API_Creation
{
protected $sql_endpoint_config;
public function __construct()
{
$this->form_action_identifier();
}
private function form_action_identifier()
{
if (isset($_REQUEST["MO_CAW_SQL_API_Creation_Nonce"]) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST["MO_CAW_SQL_API_Creation_Nonce"])), "MO_CAW_SQL_API_Creation")) {
goto df;
}
if (isset($_REQUEST["MO_CAW_SQL_API_Creation_Nonce"]) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST["MO_CAW_SQL_API_Creation_Nonce"])), "MO_CAW_SQL_API_Creation_Delete")) {
goto is;
}
if (isset($_REQUEST["MO_CAW_SQL_API_Creation_Nonce"]) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST["MO_CAW_SQL_API_Creation_Nonce"])), "MO_CAW_SQL_API_Creation_Export")) {
goto J0;
}
goto s6;
df:
$this->save_settings($_POST);
goto s6;
is:
$this->delete_settings($_POST);
goto s6;
J0:
$this->export_settings($_POST);
s6:
}
private function save_settings($post)
{
self::set_settings($post);
if (!self::is_first($this->sql_endpoint_config)) {
goto fs;
}
self::save_to_database();
goto xA;
fs:
DB_Utils::update_option("mo_caw_message", "Oops! You can only create one SQL based custom API with the standard plan. Please <a class="text-danger fw-bolder" href="admin.php?page=custom_api_wp_settings&tab=pricing-plan">upgrade</a> to a higher plan to unlock this feature.");
DB_Utils::update_option("mo_caw_message_status", Constants::MESSAGE_STATUS_DANGER);
wp_safe_redirect("admin.php?page=custom_api_wp_settings&tab=custom-sql-api");
exit;
xA:
self::redirect_after_save($post);
}
protected function set_settings($post)
{
$this->sql_endpoint_config["type"] = Constants::SQL_ENDPOINT;
$this->sql_endpoint_config["namespace"] = !empty($this->sql_endpoint_config["namespace"]) ? substr($this->sql_endpoint_config["namespace"], 0, 15) : "mo/v1";
$this->sql_endpoint_config["connection_name"] = isset($post["mo-caw-custom-sql-api-name"]) ? substr(sanitize_text_field(wp_unslash($post["mo-caw-custom-sql-api-name"])), 0, 25) : '';
$this->sql_endpoint_config["is_enabled"] = isset($post["mo-caw-custom-sql-api-is-enabled"]) ? sanitize_text_field(wp_unslash($post["mo-caw-custom-sql-api-is-enabled"])) : true;
$this->sql_endpoint_config["method"] = isset($post["mo-caw-custom-sql-api-method"]) ? sanitize_text_field(wp_unslash($post["mo-caw-custom-sql-api-method"])) : Constants::HTTP_GET;
$UK = array("connection_name" => $this->sql_endpoint_config["connection_name"], "type" => Constants::GUI_ENDPOINT, "method" => $this->sql_endpoint_config["method"], "namespace" => $this->sql_endpoint_config["namespace"]);
$eS = DB_Utils::get_configuration($UK);
if (!empty($eS)) {
goto N1;
}
if (isset($_SESSION["MO_CAW_SQL_API_Creation_Form_Data"])) {
goto JK;
}
goto xB;
N1:
$this->save_in_session(Constants::GUI_ENDPOINT_ALREADY_EXISTS);
goto xB;
JK:
unset($_SESSION["MO_CAW_SQL_API_Creation_Form_Data"]);
session_destroy();
xB:
$aj = $this->sql_endpoint_config["configuration"] ?? array();
$aj["table"] = isset($post["mo-caw-custom-sql-api-table"]) ? sanitize_text_field(wp_unslash($post["mo-caw-custom-sql-api-table"])) : '';
$aj["sql_queries"] = isset($aj["sql_queries"]) ? array_map("wp_unslash", $aj["sql_queries"]) : (isset($post["mo-caw-custom-sql-api-query"]) ? (array) array_map("wp_unslash", $post["mo-caw-custom-sql-api-query"])[0] : array());
$nm = $aj["response"] ?? array();
$nm["response_type"] = isset($nm["response_type"]) ? sanitize_text_field(wp_unslash($nm["response_type"])) : Constants::DEFAULT;
$nm["response_content_type"] = isset($nm["response_content_type"]) ? sanitize_text_field(wp_unslash($nm["response_content_type"])) : Constants::JSON;
$aj["response"] = $nm;
$this->sql_endpoint_config["configuration"] = $aj;
}
protected function save_to_database()
{
if (DB_Utils::update_configuration($this->sql_endpoint_config)) {
goto rA;
}
DB_Utils::update_option("mo_caw_message", Constants::SAVE_ERROR);
DB_Utils::update_option("mo_caw_message_status", Constants::MESSAGE_STATUS_DANGER);
return false;
goto WX;
rA:
DB_Utils::update_option("mo_caw_message", Constants::SAVE_SUCCESS);
DB_Utils::update_option("mo_caw_message_status", Constants::MESSAGE_STATUS_SUCCESS);
return true;
WX:
}
protected function redirect_after_save($post)
{
$iZ = $post["mo-caw-custom-sql-api-test-mode"] ?? false;
if (filter_var($iZ, FILTER_VALIDATE_BOOLEAN)) {
goto WZ;
}
wp_safe_redirect("admin.php?page=custom_api_wp_settings&tab=custom-sql-api&action=view&api-name=" . $this->sql_endpoint_config["connection_name"] . "&method=" . $this->sql_endpoint_config["method"] . "&namespace=" . $this->sql_endpoint_config["namespace"] . "&_wpnonce=" . wp_create_nonce("MO_CAW_SQL_API_Creation_View_Nonce"), 302);
exit;
goto oG;
WZ:
wp_safe_redirect("admin.php?page=custom_api_wp_settings&tab=custom-sql-api&action=edit&api-name=" . $this->sql_endpoint_config["connection_name"] . "&method=" . $this->sql_endpoint_config["method"] . "&namespace=" . $this->sql_endpoint_config["namespace"] . "&test-mode=" . $iZ . "&_wpnonce=" . wp_create_nonce("MO_CAW_SQL_API_Creation_Edit_Nonce"), 302);
exit;
oG:
}
protected function delete_settings($post)
{
$hW = isset($post["api-name"]) ? sanitize_text_field(wp_unslash($post["api-name"])) : '';
$we = isset($post["method"]) ? sanitize_text_field(wp_unslash($post["method"])) : '';
$Qi = isset($post["namespace"]) ? sanitize_text_field(wp_unslash($post["namespace"])) : '';
$ax = array("connection_name" => $hW, "type" => Constants::SQL_ENDPOINT, "method" => $we, "namespace" => $Qi);
if (DB_Utils::delete_configuration($ax)) {
goto zX;
}
DB_Utils::update_option("mo_caw_message", Constants::DELETION_ERROR);
DB_Utils::update_option("mo_caw_message_status", Constants::MESSAGE_STATUS_DANGER);
goto LS;
zX:
DB_Utils::update_option("mo_caw_message", Constants::DELETION_SUCCESS);
DB_Utils::update_option("mo_caw_message_status", Constants::MESSAGE_STATUS_SUCCESS);
LS:
wp_safe_redirect("admin.php?page=custom_api_wp_settings&tab=custom-sql-api", 302);
exit;
}
private function save_in_session($rX)
{
DB_Utils::update_option("mo_caw_message", $rX);
DB_Utils::update_option("mo_caw_message_status", Constants::MESSAGE_STATUS_WARNING);
$_SESSION["MO_CAW_SQL_API_Creation_Form_Data"] = $this->sql_endpoint_config;
$gI = isset($_SERVER["HTTP_REFERER"]) ? esc_url_raw(wp_unslash($_SERVER["HTTP_REFERER"])) : '';
wp_safe_redirect($gI);
exit;
}
protected function export_settings($post)
{
}
private static function is_first($Dj)
{
$fK = DB_Utils::get_configuration(array("type" => Constants::SQL_ENDPOINT));
$dW = array();
if (!empty($fK)) {
goto ZB;
}
$jx = true;
goto Ui;
ZB:
$dW = $fK[0];
$jx = $dW["namespace"] === $Dj["namespace"] && $dW["method"] === $Dj["method"] && $dW["connection_name"] === $Dj["connection_name"] ? true : false;
Ui:
return $jx;
}
}
© 2023 Quttera Ltd. All rights reserved.