Created
March 26, 2019 11:10
-
-
Save daniloparrajr/0c89bee0bd64492f55f38b0e95878ae2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Broadbean Integration Class that handles custom endpoints in adding, updating and deleting job adverts. | |
* | |
* @package Divi | |
*/ | |
/** | |
* Custom WP ENDPOINT for Broadbean integration | |
*/ | |
class Broadbean_WP_REST_Controller extends WP_REST_Controller { | |
/** | |
* Raw application password generated for broadbean integration. | |
* | |
* @var string | |
*/ | |
private $application_password = 'DmAH AvZ8 Bldl eV4u aw9Q viXp'; | |
/** | |
* Username and Application password converted to base64 FORMAT: USERNAME:APPLICATION_PASSWORD | |
* | |
* @var string | |
*/ | |
private $app_token_base64 = 'ZmVydGlsZWZyb2c6RG1BSCBBdlo4IEJsZGwgZVY0dSBhdzlRIHZpWHA='; | |
/** | |
* Register the routes for the objects of the controller. | |
*/ | |
public function register_routes() { | |
$version = 1; | |
$namespace = 'broadbean/v' . $version; | |
$base = 'route'; | |
register_rest_route( $namespace, '/' . $base, array( | |
array( | |
'methods' => WP_REST_Server::ALLMETHODS, | |
'callback' => array( $this, 'command_route' ), | |
'args' => $this->get_endpoint_args_for_item_schema( true ), | |
), | |
) ); | |
} | |
/** | |
* Hook Routes to Rest Api Init. | |
* | |
* @return void | |
*/ | |
public function hook_rest_server() { | |
add_action( 'rest_api_init', array( $this, 'register_routes' ) ); | |
} | |
/** | |
* Undocumented function | |
* | |
* @param array $request | |
* @return void | |
*/ | |
public function command_route( $request ) { | |
$json_data = $request->get_json_params(); | |
$command = $json_data['command']; | |
$callback = ''; | |
switch ( $command ) { | |
case 'add' : | |
$callback = $this->create_job_advert( $request ); | |
break; | |
case 'delete' : | |
$callback = $this->delete_job_advert( $request ); | |
break; | |
default: | |
$callback = new WP_Error( 'no-appropriate-command', __( 'No Appropriate command found for ' . $command, 'divi' ), array( 'status' => 500 ) ); | |
break; | |
} | |
return $callback; | |
} | |
/** | |
* Create Jod Advert | |
* | |
* @param WP_REST_Request $request Full data about the request. | |
* @return WP_Error|WP_REST_Request | |
*/ | |
public function create_job_advert( $request ) { | |
$json_data = $request->get_json_params(); | |
$contact_name = $json_data['contact_name']; | |
$contact_email = $json_data['contact_email']; | |
$contact_telephone = $json_data['contact_telephone']; | |
$contact_url = $json_data['contact_url']; | |
$days_to_advertise = $json_data['days_to_advertise']; | |
$application_email = $json_data['application_email']; | |
$application_url = $json_data['application_url']; | |
$job_reference = $json_data['job_reference']; | |
$job_title = $json_data['job_title']; | |
$job_type = $json_data['job_type']; | |
$job_duration = $json_data['job_duration']; | |
$job_startdate = $json_data['job_startdate']; | |
$job_skills = $json_data['job_skills']; | |
$job_description = $json_data['job_description']; | |
$job_location = $json_data['job_location']; | |
$job_industry = $json_data['job_industry']; | |
$salary_currency = $json_data['salary_currency']; | |
$salary_from = $json_data['salary_from']; | |
$salary_to = $json_data['salary_to']; | |
if ( is_null( $job_title ) ) { | |
return new WP_Error( 'job-title-required', __( 'Please provide Job title in your request.', 'divi' ), array( 'status' => 500 ) ); | |
} | |
if ( is_null( $job_description ) ) { | |
return new WP_Error( 'job-description-required', __( 'Please provide Job description in your request.', 'divi' ), array( 'status' => 500 ) ); | |
} | |
if ( is_null( $job_reference ) ) { | |
return new WP_Error( 'job-reference-required', __( 'Please provide Job reference in your request.', 'divi' ), array( 'status' => 500 ) ); | |
} | |
$job_advert_data = array( | |
'post_title' => $job_title, | |
'post_content' => wp_specialchars_decode( $job_description ), | |
'post_type' => 'job_advert', | |
'post_status' => 'publish', | |
); | |
$job_advert_id = wp_insert_post( $job_advert_data ); | |
if ( $job_advert_id ) { | |
// Add Job advert data using WP post meta. | |
add_post_meta( $job_advert_id, 'contact_name', $contact_name ); | |
add_post_meta( $job_advert_id, 'contact_email', $contact_email ); | |
add_post_meta( $job_advert_id, 'contact_telephone', $contact_telephone ); | |
add_post_meta( $job_advert_id, 'contact_url', $contact_url ); | |
add_post_meta( $job_advert_id, 'days_to_advertise', $days_to_advertise ); | |
add_post_meta( $job_advert_id, 'application_email', $application_email ); | |
add_post_meta( $job_advert_id, 'application_url', $application_url ); | |
add_post_meta( $job_advert_id, 'job_reference', $job_reference ); | |
add_post_meta( $job_advert_id, 'job_type', $job_type ); | |
add_post_meta( $job_advert_id, 'job_duration', $job_duration ); | |
add_post_meta( $job_advert_id, 'job_startdate', $job_startdate ); | |
add_post_meta( $job_advert_id, 'job_skills', $job_skills ); | |
add_post_meta( $job_advert_id, 'job_location', $job_location ); | |
add_post_meta( $job_advert_id, 'job_industry', $job_industry ); | |
add_post_meta( $job_advert_id, 'salary_currency', $salary_currency ); | |
add_post_meta( $job_advert_id, 'salary_from', $salary_from ); | |
add_post_meta( $job_advert_id, 'salary_to', $salary_to ); | |
return new WP_REST_Response( array( 'job_advert_ID' => $job_advert_id ), 200 ); | |
} | |
return new WP_Error( 'cant-create', __( 'Unable to create Job Advertisement', 'divi' ), array( 'status' => 500 ) ); | |
} | |
/** | |
* Delete one job advert depending on job_reference id | |
* | |
* @param WP_REST_Request $request Full data about the request. | |
* @return WP_Error|WP_REST_Request | |
*/ | |
public function delete_job_advert( $request ) { | |
$json_data = $request->get_json_params(); | |
$job_reference = $json_data['job_reference']; | |
if ( is_null( $job_reference ) ) { | |
return new WP_Error( 'job-reference-required', __( 'Please include a valid job_reference in your request', 'divi' ), array( 'status' => 500 ) ); | |
} | |
$job_advert_id = $this->get_job_advert_id( $job_reference ); | |
if ( wp_delete_post( $job_advert_id, true ) ) { | |
return new WP_REST_Response( true, 200 ); | |
} | |
return new WP_Error( 'cant-delete', __( 'Unable to Delete', 'divi' ), array( 'status' => 500 ) ); | |
} | |
public function get_job_advert_id( $job_reference ) { | |
global $wpdb; | |
$sql = "select post_id from " . $wpdb->prefix . "postmeta where | |
meta_key = 'job_reference' && | |
meta_value like '%%%s%%'"; | |
$sql = $wpdb->prepare( $sql, $job_reference ); | |
$res = $wpdb->get_results( $sql ); | |
return (int) $res[0]->post_id; | |
} | |
} | |
$broadbean_intergration = new Broadbean_WP_REST_Controller(); | |
$broadbean_intergration->hook_rest_server(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment