Skip to content

Commit

Permalink
Fix #161
Browse files Browse the repository at this point in the history
  • Loading branch information
inc2734 committed Jan 19, 2024
1 parent 70dacbf commit c8c55d2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
18 changes: 16 additions & 2 deletions App/Model/Meta.php
Expand Up @@ -34,6 +34,11 @@ class Meta {
*/
protected static $method;

/**
* @var WP_User
*/
protected static $sender;

/**
* Constructor.
*
Expand Down Expand Up @@ -133,7 +138,7 @@ public static function the_meta_button( $action, $label ) {
}

/**
* Return set form ID.
* Return form ID.
*
* @return int
*/
Expand Down Expand Up @@ -167,7 +172,7 @@ public static function the_token() {
}

/**
* Return set method.
* Return method.
*
* @return string
*/
Expand All @@ -194,4 +199,13 @@ public static function set_method( $value ) {
public static function the_method( $value ) {
static::_the_meta( 'method', $value );
}

/**
* Return sender.
*
* @return WP_User
*/
public static function get_sender() {
return static::$sender;
}
}
11 changes: 11 additions & 0 deletions App/Model/Responser.php
Expand Up @@ -7,6 +7,8 @@

namespace Snow_Monkey\Plugin\Forms\App\Model;

use Snow_Monkey\Plugin\Forms\App\Model\Meta;

class Responser {

/**
Expand Down Expand Up @@ -89,4 +91,13 @@ public function get_all() {
public function update( $name, $value ) {
$this->data[ $name ] = $value;
}

/**
* Return form meta data.
*
* @return Snow_Monkey\Plugin\Forms\App\Model\Meta|null
*/
public function get_meta() {
return $this->get( Meta::get_key() ) ? $this->get( Meta::get_key() ) : null;
}
}
3 changes: 1 addition & 2 deletions App/Rest/Route/View.php
Expand Up @@ -44,8 +44,7 @@ class View {
public function __construct( array $data ) {
// Set form meta data and remove from post data.
if ( isset( $data[ Meta::get_key() ] ) ) {
Meta::init( $data[ Meta::get_key() ] );
unset( $data[ Meta::get_key() ] );
$data[ Meta::get_key() ] = Meta::init( $data[ Meta::get_key() ] );
}

$this->setting = DataStore::get( Meta::get_formid() );
Expand Down
9 changes: 8 additions & 1 deletion snow-monkey-forms.php
Expand Up @@ -18,6 +18,7 @@

use Snow_Monkey\Plugin\Forms\App\Model\Csrf;
use Snow_Monkey\Plugin\Forms\App\Model\Directory;
use Snow_Monkey\Plugin\Forms\App\Model\Meta;
use Snow_Monkey\Plugin\Forms\App\Rest;
use Snow_Monkey\Plugin\Forms\App\Service\Admin\Admin;
use Snow_Monkey\Plugin\Forms\App\Service\ReCaptcha\ReCaptcha;
Expand Down Expand Up @@ -164,12 +165,14 @@ public function _enqueue_block_editor_assets() {
* Register endpoint. This endpoint returns the form view.
*/
public function _endpoint() {
$user = wp_get_current_user();

register_rest_route(
'snow-monkey-form/v1',
'/view',
array(
'methods' => 'POST',
'callback' => function() {
'callback' => function() use ( $user ) {
$referer = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : false;
$homeurl = untrailingslashit( home_url( '/' ) );
if ( 0 !== strpos( $referer, $homeurl ) ) {
Expand All @@ -179,6 +182,10 @@ public function _endpoint() {
$data = filter_input_array( INPUT_POST );
$data = $data ? $data : array();

if ( isset( $data[ Meta::get_key() ] ) ) {
$data[ Meta::get_key() ]['sender'] = $user;
}

$route = new Rest\Route\View( $data );
return $route->send();
},
Expand Down

0 comments on commit c8c55d2

Please sign in to comment.