Skip to content

Commit

Permalink
add api
Browse files Browse the repository at this point in the history
  • Loading branch information
shouc committed Oct 13, 2019
1 parent fe1cdb8 commit 8584c09
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 12 deletions.
40 changes: 40 additions & 0 deletions api/add-inquiry.php
Original file line number Diff line number Diff line change
@@ -1 +1,41 @@
<?php

require_once __DIR__ . "/../const.php";
require_once __DIR__ . "/../utils/mail.php";

function add_inquiry(WP_REST_Request $request){
global $wpdb, $INQUIRY_TABLE_NAME;
$email = $request->get_param("email");
if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
wp_send_json(
array(
"success" => true,
"message" => "Email is not valid",
)
);
}
$age = (int) $request->get_param("age");
$gender = (int) $request->get_param("gender");
$country = $request->get_param("country");
$message = $request->get_param("message");

$wpdb->insert(
$INQUIRY_TABLE_NAME,
array(
'email' => $email,
'age' => $age,
'gender' => $gender,
'country' => $country,
'message' => $message,
)
);

// Send an email

wp_send_json(
array(
"success" => true,
"message" => "",
)
);
}
35 changes: 35 additions & 0 deletions api/assign.php
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
<?php

require_once __DIR__ . "/../const.php";
require_once __DIR__ . "/../utils/user-level.php";

function assign(WP_REST_Request $request){
global $wpdb, $INQUIRY_TABLE_NAME;

$id = (int) $request->get_param("id");
$assignee_id = (int) $request->get_param('assignee_id');
$assigner_id = wp_get_current_user()->ID;
if ($assignee_id != $assigner_id && !authorize("admin")){
wp_send_json(
array(
"success" => false,
"message" => "Not authorized",
)
);
}
$wpdb->update(
$INQUIRY_TABLE_NAME,
array(
'assigner_id'=>$assigner_id,
'assignee_id'=>$assignee_id,
'status'=>1
),
array('id'=>$id)
);

wp_send_json(
array(
"success" => true,
"message" => "",
)
);
}
23 changes: 23 additions & 0 deletions api/comment.php
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
<?php

function comment(WP_REST_Request $request){
global $wpdb, $INQUIRY_TABLE_NAME;
$id = $request->get_param("id");
$comment = $request->get_param("comment");

$wpdb->update(
$INQUIRY_TABLE_NAME,
array(
'comment' => $comment,
),
array('id' => $id)
);

// Send an email

wp_send_json(
array(
"success" => true,
"message" => "",
)
);
}
24 changes: 24 additions & 0 deletions api/delete-inquiry.php
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
<?php

function delete_inquiry(WP_REST_Request $request){
global $wpdb, $INQUIRY_TABLE_NAME;
if (!authorize("admin")){
wp_send_json(
array(
"success" => false,
"message" => "Not authorized",
)
);
}
$id = $request->get_param("id");
$wpdb->delete(
$INQUIRY_TABLE_NAME,
array('id'=>$id)
);

wp_send_json(
array(
"success" => true,
"message" => "",
)
);
}
31 changes: 31 additions & 0 deletions api/get-inquiry.php
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
<?php

function get_inquiry(WP_REST_Request $request){
global $wpdb, $INQUIRY_TABLE_NAME;
if (!authorize("admin")){
wp_send_json(
array(
"success" => false,
"message" => "Not authorized",
)
);
}
$id = $request->get_param("id");
$assignee_id = $request->get_param('assignee_id');
$assigner_id = wp_get_current_user()->ID;
$wpdb->update(
$INQUIRY_TABLE_NAME,
array(
'assigner_id'=>$assigner_id,
'assignee_id'=>$assignee_id,
'status'=>1
),
array('id'=>$id)
);

wp_send_json(
array(
"success" => true,
"message" => "",
)
);
}
31 changes: 31 additions & 0 deletions api/unassign.php
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
<?php

function unassign(WP_REST_Request $request){
global $wpdb, $INQUIRY_TABLE_NAME;
if (!authorize("admin")){
wp_send_json(
array(
"success" => false,
"message" => "Not authorized",
)
);
}
$id = $request->get_param("id");
$assignee_id = $request->get_param('assignee_id');
$assigner_id = wp_get_current_user()->ID;
$wpdb->update(
$INQUIRY_TABLE_NAME,
array(
'assigner_id'=>$assigner_id,
'assignee_id'=>$assignee_id,
'status'=>1
),
array('id'=>$id)
);

wp_send_json(
array(
"success" => true,
"message" => "",
)
);
}
19 changes: 19 additions & 0 deletions const.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
global $wpdb;
$USER_TABLE_NAME = $wpdb->prefix . "users";
$INQUIRY_TABLE_NAME = $wpdb->prefix . "sexpert_inquiries";
$COMMENT_TABLE_NAME = $wpdb->prefix . "sexpert_comments";
$GENDER_TABLE_NAME = $wpdb->prefix . "sexpert_genders";
$LIST_LIMIT = 5;

function CONVERT_STATUS($status){
switch ($status):
case 0:
return "Unassigned";
case 1:
return "Assigned";
case 2:
return "Replied";
case 3:
return "";
}
77 changes: 65 additions & 12 deletions sexpert.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,28 @@
// Get the db cursor
global $wpdb, $table_prefix;

if(!isset($wpdb))
{
require_once(ABSPATH . 'wp-settings.php');
}
require_once(ABSPATH . 'wp-settings.php');
require_once(__DIR__ . '/const.php');

// Activation
function activate_sexpert() {
// Setup tables
create_question_table();
create_inquiry_table();
create_comment_table();
}

function create_question_table(){
global $wpdb;
$table_name = $wpdb->prefix . "sexpert_question";
function create_inquiry_table(){
global $wpdb, $INQUIRY_TABLE_NAME;
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
$sql = "CREATE TABLE $INQUIRY_TABLE_NAME (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`gender` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`country` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`message` longtext COLLATE utf8_unicode_ci,
`status` int(2) DEFAULT NULL,
`comment` longtext COLLATE utf8_unicode_ci,
`response` longtext COLLATE utf8_unicode_ci,
`assignee_id` int(11) DEFAULT NULL,
`assigner_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
Expand All @@ -50,6 +48,22 @@ function create_question_table(){
dbDelta( $sql );
}

function create_comment_table(){
global $wpdb, $COMMENT_TABLE_NAME;
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $COMMENT_TABLE_NAME (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`inquiry_id` int(11) DEFAULT NULL,
`comment` longtext COLLATE utf8_unicode_ci,
`author_id` int(11) DEFAULT NULL,
`time` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}


register_activation_hook( __FILE__, 'activate_sexpert' );

// Deactivation
Expand All @@ -62,13 +76,52 @@ function deactivate_sexpert() {
require_once "admin/index.php";
function setup_menu(){
add_menu_page( 'Sexpert Page',
'Sexpert',
'Sexpert Admin',
'manage_options',
'sexpert_admin',
'init_admin' );
add_menu_page( 'Sexpert 2',
'Sexpert',
'read',
'sexpert',
'init_admin' );
}

add_action('admin_menu', 'setup_menu');

require_once __DIR__ . "/api/assign.php";
require_once __DIR__ . "/api/add-inquiry.php";
function setup_restful(){
register_rest_route( 'sexpert/v1', '/assignment/(?P<id>\d+)', array(
'methods' => 'POST',
'callback' => 'assign',
));
register_rest_route( 'sexpert/v1', '/assignment/(?P<id>\d+)', array(
'methods' => 'DELETE',
'callback' => 'unassign',
));
register_rest_route( 'sexpert/v1', '/inquiry', array(
'methods' => 'POST',
'callback' => 'add_inquiry',
));
register_rest_route( 'sexpert/v1', '/comment/(?P<id>\d+)', array(
'methods' => 'POST',
'callback' => 'comment',
));
register_rest_route( 'sexpert/v1', '/inquiry/(?P<id>\d+)', array(
'methods' => 'DELETE',
'callback' => 'delete_inquiry',
));
register_rest_route( 'sexpert/v1', '/inquiries', array(
'methods' => 'GET',
'callback' => 'get_inquiry',
));
}
add_action( 'rest_api_init', 'setup_restful');

require_once 'home/form.php';
add_shortcode("sexpertform", 'form_creation');

function setup_scripts() {
wp_enqueue_script( 'script', '/wp-content/plugins/sexpert/js/sexpert.js');
}
add_action( 'wp_enqueue_scripts', 'setup_scripts' );

0 comments on commit 8584c09

Please sign in to comment.