Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Job Category REST API Endpoints #1415

Merged
merged 2 commits into from
Apr 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Declaration of Job Categories Custom Fields Model
*
* @package WPJM/REST
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Class WP_Job_Manager_Models_Job_Categories_Custom_Fields
*/
class WP_Job_Manager_Models_Job_Categories_Custom_Fields extends WP_Job_Manager_REST_Model
implements WP_Job_Manager_REST_Interfaces_Model {

/**
* Declare Fields.
*
* @return array
*/
public function declare_fields() {
return array();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Exposes Job Categories Taxonomy REST Api
*
* @package WPJM/REST
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Class WP_Job_Manager_Registrable_Job_Categories
*/
class WP_Job_Manager_Registrable_Job_Categories extends WP_Job_Manager_Registrable_Taxonomy_Type {
/**
* Gets the taxonomy type to register.
*
* @return string Taxonomy type to expose.
*/
public function get_taxonomy_type() {
return 'job_listing_category';
}

/**
* Gets the REST API base slug.
*
* @return string Slug for REST API base.
*/
public function get_rest_base() {
return 'job-categories';
}

/**
* Gets the REST API model class name.
*
* @return string Class name for the taxonomy type's model.
*/
public function get_model_class_name() {
return 'WP_Job_Manager_Models_Job_Categories_Custom_Fields';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
* @package WPJM/REST
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Class MT_Controller_Extension
*/
Expand Down
197 changes: 13 additions & 184 deletions includes/rest-api/class-wp-job-manager-registrable-job-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,202 +12,31 @@
/**
* Class WP_Job_Manager_Registrable_Job_Types
*/
class WP_Job_Manager_Registrable_Job_Types implements WP_Job_Manager_REST_Interfaces_Registrable {

/**
* The Model Prototype
*
* @var WP_Job_Manager_REST_Model
*/
private $model_prototype;

/**
* Rest Field Name
*
* @var string
*/
private $rest_field_name;

class WP_Job_Manager_Registrable_Job_Types extends WP_Job_Manager_Registrable_Taxonomy_Type {
/**
* Taxonomy Type
* Gets the taxonomy type to register.
*
* @var string
* @return string Taxonomy type to expose.
*/
private $taxonomy_type;

/**
* Register Job Types
*
* @param WP_Job_Manager_REST_Environment $environment The Environment to use.
* @throws WP_Job_Manager_REST_Exception Throws.
*
* @return bool|WP_Error true if valid otherwise error.
*/
public function register( $environment ) {
global $wp_taxonomies;
$this->taxonomy_type = 'job_listing_type';
$this->rest_field_name = 'fields';

if ( ! isset( $wp_taxonomies[ $this->taxonomy_type ] ) ) {
return false;
}

if ( $wp_taxonomies[ $this->taxonomy_type ]->show_in_rest ) {
return true;
}

$wp_taxonomies[ $this->taxonomy_type ]->show_in_rest = true;
$wp_taxonomies[ $this->taxonomy_type ]->rest_base = 'job-types';

$this->model_prototype = $environment->model( 'WP_Job_Manager_Models_Job_Types_Custom_Fields' );

if ( ! $this->model_prototype ) {
return new WP_Error( 'model-not-found' );
}
register_rest_field( $this->taxonomy_type, $this->rest_field_name, array(
'get_callback' => array( $this, 'get_employment_type' ),
'update_callback' => array( $this, 'update_employment_type' ),
'schema' => $this->get_item_schema(),
) );

return true;
public function get_taxonomy_type() {
return 'job_listing_type';
}

/**
* Get Item Schema
* Gets the REST API base slug.
*
* @return array
* @return string Slug for REST API base.
*/
public function get_item_schema() {
$fields = $this->model_prototype->get_fields();
$properties = array();
$required = array();
foreach ( $fields as $field_declaration ) {
/**
* Our declaration
*
* @var WP_Job_Manager_REST_Field_Declaration $field_declaration
*/
$properties[ $field_declaration->get_data_transfer_name() ] = $field_declaration->as_item_schema_property();
if ( $field_declaration->is_required() ) {
$required[] = $field_declaration->get_data_transfer_name();
}
}
$schema = array(
'$schema' => 'http://json-schema.org/schema#',
'title' => $this->model_prototype->get_name(),
'type' => 'object',
'properties' => (array) apply_filters( 'mixtape_rest_api_schema_properties', $properties, $this->model_prototype ),
);

if ( ! empty( $required ) ) {
$schema['required'] = $required;
}

return $schema;
public function get_rest_base() {
return 'job-types';
}

/**
* Our Get Fields.
*
* @param array $object Object.
* @param string $field_name Field Name.
* @param WP_REST_Request $request Request.
* @param string $object_type Object Type.
* Gets the REST API model class name.
*
* @return mixed|string
* @throws WP_Job_Manager_REST_Exception If type not there.
* @return string Class name for the taxonomy type's model.
*/
public function get_employment_type( $object, $field_name, $request, $object_type ) {
if ( $this->taxonomy_type !== $object_type ) {
return null;
}

if ( $this->rest_field_name !== $field_name ) {
return null;
}

$object_id = absint( $object['id'] );
$model = $this->get_model( $object_id );
return $model->to_dto();
}

/**
* Get a model if exists
*
* @param int $object_id Object ID.
* @return WP_Job_Manager_REST_Interfaces_Model
* @throws WP_Job_Manager_REST_Exception On Error.
*/
private function get_model( $object_id ) {
$data = array();
foreach ( $this->model_prototype->get_fields( WP_Job_Manager_REST_Field_Declaration::META ) as $field_declaration ) {
$field_name = $field_declaration->get_name();
if ( metadata_exists( 'term', $object_id, $field_name ) ) {
$meta = get_term_meta( $object_id, $field_name, true );
$data[ $field_name ] = $meta;
}
}

return $this->model_prototype->create( $data, array(
'deserialize' => true,
) );
}

/**
* Our Reader.
*
* @param mixed $data Data.
* @param object $object Object.
* @param string $field_name Field Name.
* @param WP_REST_Request $request Request.
* @param string $object_type Object Type.
*
* @return mixed|string
* @throws WP_Job_Manager_REST_Exception If type not there.
*/
public function update_employment_type( $data, $object, $field_name, $request, $object_type ) {
if ( $this->taxonomy_type !== $object_type ) {
return null;
}

if ( $this->rest_field_name !== $field_name ) {
return null;
}

if ( ! is_a( $object, 'WP_Term' ) ) {
return null;
}

$term_id = absint( $object->term_id );
if ( ! $term_id ) {
// No way to update this. Bail.
return new WP_Error( 'job-types-error-invalid-id', 'job-types-error-invalid-id', array(
'status' => 400,
) );
}
$existing_model = $this->get_model( $term_id );

$updated = $existing_model->update_from_array( $data );
if ( is_wp_error( $updated ) ) {
return $updated;
}

$maybe_validation_error = $updated->sanitize()->validate();
if ( is_wp_error( $maybe_validation_error ) ) {
return $maybe_validation_error;
}

$serialized_data = $updated->serialize( WP_Job_Manager_REST_Field_Declaration::META );

foreach ( $serialized_data as $field_name => $val ) {
if ( metadata_exists( 'term', $term_id, $field_name ) ) {
update_term_meta( $term_id, $field_name, $val );
} else {
add_term_meta( $term_id, $field_name, $val );
}
}

return true;
public function get_model_class_name() {
return 'WP_Job_Manager_Models_Job_Types_Custom_Fields';
}
}
Loading