Commit 93c55226 authored by Skirmantas's avatar Skirmantas

session && endpoint

parent b046cf06
...@@ -292,9 +292,9 @@ class Biuro_Contacts_Public { ...@@ -292,9 +292,9 @@ class Biuro_Contacts_Public {
public static function delete_transients( $str ) { public static function delete_transients( $str ) {
foreach (static::FIELDS[$str] as $key) { foreach (static::FIELDS[$str] as $key) {
delete_transient($str . '--' . $key . '-value'); unset($_SESSION[$str . '--' . $key . '-message']);
delete_transient($str . '--' . $key . '-status'); unset($_SESSION[$str . '--' . $key . '-status']);
delete_transient($str . '--' . $key . '-message'); unset($_SESSION[$str . '--' . $key . '-value']);
} }
} }
...@@ -393,6 +393,8 @@ class Biuro_Contacts_Public { ...@@ -393,6 +393,8 @@ class Biuro_Contacts_Public {
$data = [ $data = [
'created' => current_time('Y-m-d H:i:s') 'created' => current_time('Y-m-d H:i:s')
]; ];
foreach (static::FIELDS[$str] as $key) { foreach (static::FIELDS[$str] as $key) {
$value = static::getValue($key, $post[$key]); $value = static::getValue($key, $post[$key]);
$validation = static::validate($str, $key, $value, $post); $validation = static::validate($str, $key, $value, $post);
...@@ -405,16 +407,16 @@ class Biuro_Contacts_Public { ...@@ -405,16 +407,16 @@ class Biuro_Contacts_Public {
$data[$key] = $value; $data[$key] = $value;
endif; endif;
set_transient($str . '--' . $key . '-value', $value); $_SESSION[$str . '--' . $key . '-value'] = $value;
set_transient($str . '--' . $key . '-status', $validation['status']); $_SESSION[$str . '--' . $key . '-status'] = $validation['status'];
set_transient($str . '--' . $key . '-message', $validation['message']); $_SESSION[$str . '--' . $key . '-message'] = $validation['message'];
} }
if ($canSubmit): if ($canSubmit):
$insert_id = static::insert_row_to_db($data); $insert_id = static::insert_row_to_db($data);
set_transient($str . '--step', 2); $_SESSION[$str . '--step'] = 2;
set_transient($str . '--id', $insert_id); $_SESSION[$str . '--id'] = $insert_id;
endif; endif;
} }
...@@ -444,18 +446,19 @@ class Biuro_Contacts_Public { ...@@ -444,18 +446,19 @@ class Biuro_Contacts_Public {
$nonce = $_POST['_wpnonce']; $nonce = $_POST['_wpnonce'];
$referer = $_POST['_wp_http_referer']; $referer = $_POST['_wp_http_referer'];
if ( !isset( $nonce ) || !wp_verify_nonce($nonce, 'employees_quick_post_nonce' ) ) { if ( !isset( $nonce ) || !wp_verify_nonce($nonce, 'employees_quick_post_nonce' ) ) {
wp_redirect( $referer ); wp_redirect( $referer );
exit; exit;
} }
delete_transient('employees-quick--step'); unset($_SESSION['employees-quick--step']);
if ($_POST['submit'] == "1"): if ($_POST['submit'] == "1"):
static::set_transients('employees-quick', $_POST); static::set_transients('employees-quick', $_POST);
elseif ($_POST['submit'] == "2" && get_transient( 'employees-quick--id' )): elseif ($_POST['submit'] == "2" && $_SESSION['employees-quick--id']):
$data = [ $data = [
'city' => static::getValue('city', $_POST['city']), 'city' => static::getValue('city', $_POST['city']),
...@@ -465,7 +468,7 @@ class Biuro_Contacts_Public { ...@@ -465,7 +468,7 @@ class Biuro_Contacts_Public {
'updated' => current_time('Y-m-d H:i:s') '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; endif;
...@@ -485,4 +488,31 @@ class Biuro_Contacts_Public { ...@@ -485,4 +488,31 @@ class Biuro_Contacts_Public {
// die(); // die();
//apparently when finished, die(); is required. //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; ...@@ -21,9 +21,9 @@ if ( ! defined( 'WPINC' ) ) die;
?> ?>
<?php <?php
$nameValue = get_transient( 'employees-quick--name-value' ); $nameValue = $_SESSION['employees-quick--name-value'];
$nameStatus = get_transient( 'employees-quick--name-status' ); $nameStatus = $_SESSION['employees-quick--name-status'];
$nameMessage = get_transient( 'employees-quick--name-message' ); $nameMessage = $_SESSION['employees-quick--name-message'];
?> ?>
<div class="c-form--row"> <div class="c-form--row">
<label class="c-form--label" for="form-name">Name, Surname*</label> <label class="c-form--label" for="form-name">Name, Surname*</label>
...@@ -38,9 +38,9 @@ if ( ! defined( 'WPINC' ) ) die; ...@@ -38,9 +38,9 @@ if ( ! defined( 'WPINC' ) ) die;
</div><!-- .c-form--row --> </div><!-- .c-form--row -->
<?php <?php
$phoneValue = get_transient( 'employees-quick--phone-value' ); $phoneValue = $_SESSION['employees-quick--phone-value'];
$phoneStatus = get_transient( 'employees-quick--phone-status' ); $phoneStatus = $_SESSION['employees-quick--phone-status'];
$phoneMessage = get_transient( 'employees-quick--phone-message' ); $phoneMessage = $_SESSION['employees-quick--phone-message'];
?> ?>
<div class="c-form--row"> <div class="c-form--row">
<label class="c-form--label" for="form-phone">Phone no.*</label> <label class="c-form--label" for="form-phone">Phone no.*</label>
...@@ -55,9 +55,9 @@ if ( ! defined( 'WPINC' ) ) die; ...@@ -55,9 +55,9 @@ if ( ! defined( 'WPINC' ) ) die;
</div><!-- .c-form--row --> </div><!-- .c-form--row -->
<?php <?php
$emailValue = get_transient( 'employees-quick--email-value' ); $emailValue = $_SESSION['employees-quick--email-value'];
$emailStatus = get_transient( 'employees-quick--email-status' ); $emailStatus = $_SESSION['employees-quick--email-status'];
$emailMessage = get_transient( 'employees-quick--email-message' ); $emailMessage = $_SESSION['employees-quick--email-message'];
?> ?>
<div class="c-form--row"> <div class="c-form--row">
<label class="c-form--label" for="form-email">Email address*</label> <label class="c-form--label" for="form-email">Email address*</label>
...@@ -79,9 +79,9 @@ if ( ! defined( 'WPINC' ) ) die; ...@@ -79,9 +79,9 @@ if ( ! defined( 'WPINC' ) ) die;
<?php <?php
$agreeValue = get_transient( 'employees-quick--agree-value' ); $agreeValue = $_SESSION['employees-quick--agree-value'];
$agreeStatus = get_transient( 'employees-quick--agree-status' ); $agreeStatus = $_SESSION['employees-quick--agree-status'];
$agreeMessage = get_transient( 'employees-quick--agree-message' ); $agreeMessage = $_SESSION['employees-quick--agree-message'];
?> ?>
<div class="c-form--row"> <div class="c-form--row">
<div class="c-form--checkbox-wrap"> <div class="c-form--checkbox-wrap">
......
...@@ -23,7 +23,8 @@ if ( ! defined( 'WPINC' ) ) die; ...@@ -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"> <form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post" enctype="multipart/form-data" class="c-form">
<?php <?php
if ( !get_transient( 'employees-quick--step' ) ):
if ( !$_SESSION['employees-quick--step'] ):
include_once('biuro-contacts-public--employees-quick--step-1.php'); include_once('biuro-contacts-public--employees-quick--step-1.php');
else: else:
include_once('biuro-contacts-public--employees-quick--step-2.php'); include_once('biuro-contacts-public--employees-quick--step-2.php');
......
...@@ -453,6 +453,15 @@ function getContacts ( $request ) { ...@@ -453,6 +453,15 @@ function getContacts ( $request ) {
return new WP_REST_Response( $res, 200 ); return new WP_REST_Response( $res, 200 );
} }
add_action('init', 'start_session', 1);
function start_session() {
if(!session_id()) {
session_start();
}
}
function getDivisions ( $request ) { function getDivisions ( $request ) {
$res = array(); $res = array();
$params = $request->get_params(); $params = $request->get_params();
...@@ -509,7 +518,19 @@ add_action( 'rest_api_init', function () { ...@@ -509,7 +518,19 @@ add_action( 'rest_api_init', function () {
register_rest_route( 'api/v1', '/contacts', array( register_rest_route( 'api/v1', '/contacts', array(
'methods' => WP_REST_Server::READABLE, '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