Commit 93c55226 authored by Skirmantas's avatar Skirmantas

session && endpoint

parent b046cf06
......@@ -292,9 +292,9 @@ class Biuro_Contacts_Public {
public static function delete_transients( $str ) {
foreach (static::FIELDS[$str] as $key) {
delete_transient($str . '--' . $key . '-value');
delete_transient($str . '--' . $key . '-status');
delete_transient($str . '--' . $key . '-message');
unset($_SESSION[$str . '--' . $key . '-message']);
unset($_SESSION[$str . '--' . $key . '-status']);
unset($_SESSION[$str . '--' . $key . '-value']);
}
}
......@@ -393,6 +393,8 @@ class Biuro_Contacts_Public {
$data = [
'created' => current_time('Y-m-d H:i:s')
];
foreach (static::FIELDS[$str] as $key) {
$value = static::getValue($key, $post[$key]);
$validation = static::validate($str, $key, $value, $post);
......@@ -405,16 +407,16 @@ class Biuro_Contacts_Public {
$data[$key] = $value;
endif;
set_transient($str . '--' . $key . '-value', $value);
set_transient($str . '--' . $key . '-status', $validation['status']);
set_transient($str . '--' . $key . '-message', $validation['message']);
$_SESSION[$str . '--' . $key . '-value'] = $value;
$_SESSION[$str . '--' . $key . '-status'] = $validation['status'];
$_SESSION[$str . '--' . $key . '-message'] = $validation['message'];
}
if ($canSubmit):
$insert_id = static::insert_row_to_db($data);
set_transient($str . '--step', 2);
set_transient($str . '--id', $insert_id);
$_SESSION[$str . '--step'] = 2;
$_SESSION[$str . '--id'] = $insert_id;
endif;
}
......@@ -443,19 +445,20 @@ class Biuro_Contacts_Public {
$nonce = $_POST['_wpnonce'];
$referer = $_POST['_wp_http_referer'];
if ( !isset( $nonce ) || !wp_verify_nonce($nonce, 'employees_quick_post_nonce' ) ) {
wp_redirect( $referer );
exit;
}
delete_transient('employees-quick--step');
unset($_SESSION['employees-quick--step']);
if ($_POST['submit'] == "1"):
static::set_transients('employees-quick', $_POST);
elseif ($_POST['submit'] == "2" && get_transient( 'employees-quick--id' )):
elseif ($_POST['submit'] == "2" && $_SESSION['employees-quick--id']):
$data = [
'city' => static::getValue('city', $_POST['city']),
......@@ -465,7 +468,7 @@ class Biuro_Contacts_Public {
'updated' => current_time('Y-m-d H:i:s')
];
static::update_row_in_db($data, get_transient( 'employees-quick--id' ));
static::update_row_in_db($data, $_SESSION['employees-quick--id']);
endif;
......@@ -485,4 +488,31 @@ class Biuro_Contacts_Public {
// die();
//apparently when finished, die(); is required.
}
/**
* @method getContacts
* get contacts data from DB for API endpoint "contacts" endpoint;
* @param WP_REST_Request $request
* @return json;
*/
public static function getContacts(WP_REST_Request $request)
{
global $wpdb;
$params = $request->get_params();
if ( !$params ) {
exit;
}
$from = (string) $params['from'];
$orderBy = (string) $params['by'];
if(!$from || !in_array($orderBy, ['created', 'updated'])) {
exit;
}
$sql = "SELECT * FROM `" . $wpdb->prefix . "biuro_employees`
where ". $orderBy. " >= '". $from. "' ORDER BY ". $orderBy ." DESC LIMIT 50";
return $wpdb->get_results($sql, ARRAY_A);
}
}
......@@ -21,9 +21,9 @@ if ( ! defined( 'WPINC' ) ) die;
?>
<?php
$nameValue = get_transient( 'employees-quick--name-value' );
$nameStatus = get_transient( 'employees-quick--name-status' );
$nameMessage = get_transient( 'employees-quick--name-message' );
$nameValue = $_SESSION['employees-quick--name-value'];
$nameStatus = $_SESSION['employees-quick--name-status'];
$nameMessage = $_SESSION['employees-quick--name-message'];
?>
<div class="c-form--row">
<label class="c-form--label" for="form-name">Name, Surname*</label>
......@@ -38,9 +38,9 @@ if ( ! defined( 'WPINC' ) ) die;
</div><!-- .c-form--row -->
<?php
$phoneValue = get_transient( 'employees-quick--phone-value' );
$phoneStatus = get_transient( 'employees-quick--phone-status' );
$phoneMessage = get_transient( 'employees-quick--phone-message' );
$phoneValue = $_SESSION['employees-quick--phone-value'];
$phoneStatus = $_SESSION['employees-quick--phone-status'];
$phoneMessage = $_SESSION['employees-quick--phone-message'];
?>
<div class="c-form--row">
<label class="c-form--label" for="form-phone">Phone no.*</label>
......@@ -55,9 +55,9 @@ if ( ! defined( 'WPINC' ) ) die;
</div><!-- .c-form--row -->
<?php
$emailValue = get_transient( 'employees-quick--email-value' );
$emailStatus = get_transient( 'employees-quick--email-status' );
$emailMessage = get_transient( 'employees-quick--email-message' );
$emailValue = $_SESSION['employees-quick--email-value'];
$emailStatus = $_SESSION['employees-quick--email-status'];
$emailMessage = $_SESSION['employees-quick--email-message'];
?>
<div class="c-form--row">
<label class="c-form--label" for="form-email">Email address*</label>
......@@ -79,9 +79,9 @@ if ( ! defined( 'WPINC' ) ) die;
<?php
$agreeValue = get_transient( 'employees-quick--agree-value' );
$agreeStatus = get_transient( 'employees-quick--agree-status' );
$agreeMessage = get_transient( 'employees-quick--agree-message' );
$agreeValue = $_SESSION['employees-quick--agree-value'];
$agreeStatus = $_SESSION['employees-quick--agree-status'];
$agreeMessage = $_SESSION['employees-quick--agree-message'];
?>
<div class="c-form--row">
<div class="c-form--checkbox-wrap">
......
......@@ -23,7 +23,8 @@ if ( ! defined( 'WPINC' ) ) die;
<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post" enctype="multipart/form-data" class="c-form">
<?php
if ( !get_transient( 'employees-quick--step' ) ):
if ( !$_SESSION['employees-quick--step'] ):
include_once('biuro-contacts-public--employees-quick--step-1.php');
else:
include_once('biuro-contacts-public--employees-quick--step-2.php');
......
......@@ -453,6 +453,15 @@ function getContacts ( $request ) {
return new WP_REST_Response( $res, 200 );
}
add_action('init', 'start_session', 1);
function start_session() {
if(!session_id()) {
session_start();
}
}
function getDivisions ( $request ) {
$res = array();
$params = $request->get_params();
......@@ -509,7 +518,19 @@ add_action( 'rest_api_init', function () {
register_rest_route( 'api/v1', '/contacts', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'getContacts'
'callback' => 'Biuro_Contacts_Public::getContacts',
'args' => [
'from' => [
'validate_callback' => function($param, $request, $key) {
return is_string( $param );
}
],
'by' => [
'validate_callback' => function($param, $request, $key) {
return is_string( $param );
}
]
]
));
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment