Skip to content

Commit 301993e

Browse files
authored
Merge pull request #2 from PolyPlugins/feature/brand-and-cats
Endpoint additions
2 parents b4e942e + da9199b commit 301993e

File tree

3 files changed

+344
-5
lines changed

3 files changed

+344
-5
lines changed

README.MD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ Yes, the plugin supports uploading images to the WordPress media library via the
6565

6666
## Changelog
6767

68+
### 1.0.3
69+
70+
* Added: [Get Product Category](https://www.polyplugins.com/docs/content-api/api/get-product-category/) endpoint
71+
* Added: [Update Product Category](https://www.polyplugins.com/docs/content-api/api/update-product-category/) endpoint
72+
* Added: [Create Product Category](https://www.polyplugins.com/docs/content-api/api/create-product-category/) endpoint
73+
* Updated: [Get Brand](https://www.polyplugins.com/docs/content-api/api/get-brand/) endpoint (Previous endpoint is an alias)
74+
* Updated: [Update Brand](https://www.polyplugins.com/docs/content-api/api/update-brand/) endpoint (Previous endpoint is an alias)
75+
* Updated: [Create Brand](https://www.polyplugins.com/docs/content-api/api/create-brand/) endpoint (Previous endpoint is an alias)
76+
6877
### 1.0.2
6978

7079
* Added: [Get Brand](https://www.polyplugins.com/docs/content-api/api/get-brand/) endpoint

content-api.php

Lines changed: 326 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Plugin Name: Content API
55
* Plugin URI: https://www.polyplugins.com/contact/
66
* Description: Adds various endpoints to create content
7-
* Version: 1.0.2
7+
* Version: 1.0.3
88
* Requires at least: 6.5
99
* Requires PHP: 7.4
1010
* Author: Poly Plugins
@@ -62,6 +62,24 @@ public function register_endpoints() {
6262
'permission_callback' => array($this, 'has_permission'), // Add your permission callback for security
6363
));
6464

65+
register_rest_route('content-api/v1', '/product-category/', array(
66+
'methods' => 'GET',
67+
'callback' => array($this, 'get_product_category'),
68+
'permission_callback' => array($this, 'has_permission'), // Add your permission callback for security
69+
));
70+
71+
register_rest_route('content-api/v1', '/product-category/', array(
72+
'methods' => 'PATCH',
73+
'callback' => array($this, 'update_product_category'),
74+
'permission_callback' => array($this, 'has_permission'), // Add your permission callback for security
75+
));
76+
77+
register_rest_route('content-api/v1', '/product-category/', array(
78+
'methods' => 'POST',
79+
'callback' => array($this, 'create_product_category'),
80+
'permission_callback' => array($this, 'has_permission'), // Add your permission callback for security
81+
));
82+
6583
register_rest_route('content-api/v1', '/product-categories/', array(
6684
'methods' => 'GET',
6785
'callback' => array($this, 'get_all_product_categories'),
@@ -110,6 +128,24 @@ public function register_endpoints() {
110128
'permission_callback' => array($this, 'has_permission'), // Add your permission callback for security
111129
));
112130

131+
register_rest_route('content-api/v1', '/product-brand/', array(
132+
'methods' => 'GET',
133+
'callback' => array($this, 'get_brand'),
134+
'permission_callback' => array($this, 'has_permission'), // Add your permission callback for security
135+
));
136+
137+
register_rest_route('content-api/v1', '/product-brand/', array(
138+
'methods' => 'PATCH',
139+
'callback' => array($this, 'update_brand'),
140+
'permission_callback' => array($this, 'has_permission'), // Add your permission callback for security
141+
));
142+
143+
register_rest_route('content-api/v1', '/product-brand/', array(
144+
'methods' => 'POST',
145+
'callback' => array($this, 'create_brand'),
146+
'permission_callback' => array($this, 'has_permission'), // Add your permission callback for security
147+
));
148+
113149
register_rest_route('content-api/v1', '/taxonomy/brand/', array(
114150
'methods' => 'GET',
115151
'callback' => array($this, 'get_brand'),
@@ -654,6 +690,292 @@ public function get_all_product_categories(WP_REST_Request $request) {
654690
), 200);
655691
}
656692

693+
/**
694+
* Get product category
695+
*
696+
* @param mixed $request
697+
* @return void
698+
*/
699+
public function get_product_category(WP_REST_Request $request) {
700+
$params = $request->get_params();
701+
$taxonomy = isset($params['taxonomy']) ? sanitize_text_field($params['taxonomy']) : 'product_cat';
702+
$id = isset($params['id']) ? absint($params['id']) : 0;
703+
$slug = isset($params['slug']) ? sanitize_title($params['slug']) : '';
704+
$name = isset($params['name']) ? sanitize_text_field($params['name']) : '';
705+
706+
if ($id) {
707+
$term = get_term($id, $taxonomy);
708+
} elseif ($slug) {
709+
$term = get_term_by('slug', $slug, $taxonomy);
710+
} elseif ($name) {
711+
$term = get_term_by('name', $name, $taxonomy);
712+
}
713+
714+
if (empty($term) || is_wp_error($term)) {
715+
return new WP_Error('no_product_category_found', "No category found under the " . $taxonomy . " taxonomy.", array('status' => 400));
716+
}
717+
718+
$term_id = $term->term_id;
719+
$product_category_data = (array) $term;
720+
721+
// Get thumbnail ID and URL
722+
$thumbnail_id = get_term_meta($term_id, 'thumbnail_id', true);
723+
$thumbnail_url = $thumbnail_id ? wp_get_attachment_url($thumbnail_id) : '';
724+
725+
if ($thumbnail_url) {
726+
$product_category_data['thumbnail'] = $thumbnail_url;
727+
}
728+
729+
// Get Yoast SEO meta fields
730+
$yoast_option = get_option('wpseo_taxonomy_meta');
731+
$yoast_data = [];
732+
733+
if (isset($yoast_option[$taxonomy]) && isset($yoast_option[$taxonomy][$term_id])) {
734+
$yoast_raw = $yoast_option[$taxonomy][$term_id];
735+
$yoast_data = array(
736+
'title' => $yoast_raw['wpseo_title'] ?? '',
737+
'description' => $yoast_raw['wpseo_desc'] ?? '',
738+
'premium' => array(
739+
'social_appearance' => array(
740+
'title' => $yoast_raw['wpseo_opengraph-title'] ?? '',
741+
'description' => $yoast_raw['wpseo_opengraph-description'] ?? '',
742+
'image' => $yoast_raw['wpseo_opengraph-image'] ?? '',
743+
),
744+
'x' => array(
745+
'title' => $yoast_raw['wpseo_twitter-title'] ?? '',
746+
'description' => $yoast_raw['wpseo_twitter-description'] ?? '',
747+
'image' => $yoast_raw['wpseo_twitter-image'] ?? '',
748+
)
749+
)
750+
);
751+
}
752+
753+
$product_category_data['yoast'] = $yoast_data;
754+
755+
return new WP_REST_Response(array(
756+
'success' => true,
757+
'data' => $product_category_data,
758+
), 201);
759+
}
760+
761+
/**
762+
* Update product category
763+
*
764+
* @param mixed $request
765+
* @return void
766+
*/
767+
public function update_product_category(WP_REST_Request $request) {
768+
$fields = $request->get_json_params();
769+
$id = isset($fields['id']) ? absint($fields['id']) : 0;
770+
$name = isset($fields['name']) ? sanitize_text_field($fields['name']) : '';
771+
$description = isset($fields['description']) ? wp_kses_post($fields['description']) : '';
772+
$slug = isset($fields['slug']) ? sanitize_title($fields['slug']) : '';
773+
$parent_id = isset($fields['parent']) ? absint($fields['parent']) : '';
774+
$taxonomy = isset($fields['taxonomy']) ? sanitize_text_field($fields['taxonomy']) : 'product_cat';
775+
776+
// Basic input validation
777+
if (!$id) {
778+
return new WP_Error('missing_identifier', 'Product category ID is required', array('status' => 400));
779+
}
780+
781+
$term = get_term($id, $taxonomy);
782+
783+
if (!$term || is_wp_error($term)) {
784+
return new WP_Error('category_not_found', 'Product category not found', array('status' => 404));
785+
}
786+
787+
$term_id = $term->term_id;
788+
789+
$term_args = [];
790+
791+
if ($name) {
792+
$term_args['name'] = $name;
793+
}
794+
795+
if ($description) {
796+
$term_args['description'] = $description;
797+
}
798+
799+
if ($slug) {
800+
$term_args['slug'] = $slug;
801+
}
802+
803+
if ($parent_id > 0) {
804+
$parent_term = get_term($parent_id, $taxonomy);
805+
806+
if ($parent_term && !is_wp_error($parent_term)) {
807+
$term_args['parent'] = $parent_id;
808+
} else {
809+
return new WP_Error('invalid_parent', 'The specified parent product category does not exist.', array('status' => 400));
810+
}
811+
} elseif ($parent_id === 0) {
812+
$term_args['parent'] = 0; // Remove parent
813+
}
814+
815+
if (!empty($term_args)) {
816+
$result = wp_update_term($term_id, $taxonomy, $term_args);
817+
818+
if (is_wp_error($result)) {
819+
return new WP_Error('term_update_failed', 'Failed to update product category term', array('status' => 500));
820+
}
821+
}
822+
823+
// Update Yoast fields
824+
if (isset($fields['yoast']) && is_array($fields['yoast'])) {
825+
$yoast_option = get_option('wpseo_taxonomy_meta');
826+
827+
if (!isset($yoast_option[$taxonomy])) {
828+
$yoast_option[$taxonomy] = [];
829+
}
830+
831+
if (!isset($yoast_option[$taxonomy][$term_id])) {
832+
$yoast_option[$taxonomy][$term_id] = [];
833+
}
834+
835+
$yoast_input = $fields['yoast'];
836+
837+
if (isset($yoast_input['title']) && $yoast_input['title']) {
838+
$yoast_option[$taxonomy][$term_id]['wpseo_title'] = $yoast_input['title'];
839+
}
840+
841+
if (isset($yoast_input['description']) && $yoast_input['description']) {
842+
$yoast_option[$taxonomy][$term_id]['wpseo_desc'] = $yoast_input['description'];
843+
}
844+
845+
if (isset($yoast_input['premium']['social_appearance']['title']) && $yoast_input['premium']['social_appearance']['title']) {
846+
$yoast_option[$taxonomy][$term_id]['wpseo_opengraph-title'] = $yoast_input['premium']['social_appearance']['title'];
847+
}
848+
849+
if (isset($yoast_input['premium']['social_appearance']['description']) && $yoast_input['premium']['social_appearance']['description']) {
850+
$yoast_option[$taxonomy][$term_id]['wpseo_opengraph-description'] = $yoast_input['premium']['social_appearance']['description'];
851+
}
852+
853+
if (isset($yoast_input['premium']['social_appearance']['image']) && $yoast_input['premium']['social_appearance']['image']) {
854+
$yoast_option[$taxonomy][$term_id]['wpseo_opengraph-image'] = $yoast_input['premium']['social_appearance']['image'];
855+
}
856+
857+
if (isset($yoast_input['premium']['x']['title']) && $yoast_input['premium']['x']['title']) {
858+
$yoast_option[$taxonomy][$term_id]['wpseo_twitter-title'] = $yoast_input['premium']['x']['title'];
859+
}
860+
861+
if (isset($yoast_input['premium']['x']['description']) && $yoast_input['premium']['x']['description']) {
862+
$yoast_option[$taxonomy][$term_id]['wpseo_twitter-description'] = $yoast_input['premium']['x']['description'];
863+
}
864+
865+
if (isset($yoast_input['premium']['x']['image']) && $yoast_input['premium']['x']['image']) {
866+
$yoast_option[$taxonomy][$term_id]['wpseo_twitter-image'] = $yoast_input['premium']['x']['image'];
867+
}
868+
869+
update_option('wpseo_taxonomy_meta', $yoast_option);
870+
}
871+
872+
return new WP_REST_Response(array(
873+
'success' => true,
874+
'product_category_id' => $term_id,
875+
'message' => 'Product category updated successfully.'
876+
), 200);
877+
}
878+
879+
/**
880+
* Create product category
881+
*
882+
* @param mixed $request
883+
* @return void
884+
*/
885+
public function create_product_category(WP_REST_Request $request) {
886+
$fields = $request->get_json_params();
887+
$name = isset($fields['name']) ? sanitize_text_field($fields['name']) : '';
888+
$description = isset($fields['description']) ? wp_kses_post($fields['description']) : '';
889+
$slug = isset($fields['slug']) ? sanitize_title($fields['slug']) : '';
890+
$parent_id = isset($fields['parent']) ? absint($fields['parent']) : 0;
891+
$taxonomy = isset($fields['taxonomy']) ? sanitize_text_field($fields['taxonomy']) : 'product_cat';
892+
893+
// Validate name
894+
if (empty($name)) {
895+
return new WP_Error('missing_name', 'Product category name is required.', array('status' => 400));
896+
}
897+
898+
$term_args = array(
899+
'description' => $description,
900+
'slug' => $slug,
901+
'parent' => 0
902+
);
903+
904+
if ($parent_id > 0) {
905+
$parent_term = get_term($parent_id, $taxonomy);
906+
907+
if ($parent_term && !is_wp_error($parent_term)) {
908+
$term_args['parent'] = $parent_id;
909+
} else {
910+
return new WP_Error('invalid_parent', 'The specified parent product category does not exist.', array('status' => 400));
911+
}
912+
}
913+
914+
// Create the term
915+
$result = wp_insert_term($name, $taxonomy, $term_args);
916+
917+
if (is_wp_error($result)) {
918+
return new WP_Error('term_creation_failed', $result->get_error_message(), array('status' => 500));
919+
}
920+
921+
$term_id = $result['term_id'];
922+
923+
// Update Yoast fields
924+
if (isset($fields['yoast']) && is_array($fields['yoast'])) {
925+
$yoast_option = get_option('wpseo_taxonomy_meta');
926+
927+
if (!isset($yoast_option[$taxonomy])) {
928+
$yoast_option[$taxonomy] = array();
929+
}
930+
931+
if (!isset($yoast_option[$taxonomy][$term_id])) {
932+
$yoast_option[$taxonomy][$term_id] = array();
933+
}
934+
935+
$yoast_input = $fields['yoast'];
936+
937+
if (!empty($yoast_input['title'])) {
938+
$yoast_option[$taxonomy][$term_id]['wpseo_title'] = $yoast_input['title'];
939+
}
940+
941+
if (!empty($yoast_input['description'])) {
942+
$yoast_option[$taxonomy][$term_id]['wpseo_desc'] = $yoast_input['description'];
943+
}
944+
945+
if (!empty($yoast_input['premium']['social_appearance']['title'])) {
946+
$yoast_option[$taxonomy][$term_id]['wpseo_opengraph-title'] = $yoast_input['premium']['social_appearance']['title'];
947+
}
948+
949+
if (!empty($yoast_input['premium']['social_appearance']['description'])) {
950+
$yoast_option[$taxonomy][$term_id]['wpseo_opengraph-description'] = $yoast_input['premium']['social_appearance']['description'];
951+
}
952+
953+
if (!empty($yoast_input['premium']['social_appearance']['image'])) {
954+
$yoast_option[$taxonomy][$term_id]['wpseo_opengraph-image'] = $yoast_input['premium']['social_appearance']['image'];
955+
}
956+
957+
if (!empty($yoast_input['premium']['x']['title'])) {
958+
$yoast_option[$taxonomy][$term_id]['wpseo_twitter-title'] = $yoast_input['premium']['x']['title'];
959+
}
960+
961+
if (!empty($yoast_input['premium']['x']['description'])) {
962+
$yoast_option[$taxonomy][$term_id]['wpseo_twitter-description'] = $yoast_input['premium']['x']['description'];
963+
}
964+
965+
if (!empty($yoast_input['premium']['x']['image'])) {
966+
$yoast_option[$taxonomy][$term_id]['wpseo_twitter-image'] = $yoast_input['premium']['x']['image'];
967+
}
968+
969+
update_option('wpseo_taxonomy_meta', $yoast_option);
970+
}
971+
972+
return new WP_REST_Response(array(
973+
'success' => true,
974+
'product_category_id' => $term_id,
975+
'message' => 'Product category created successfully.'
976+
), 201);
977+
}
978+
657979
/**
658980
* Get product categories
659981
*
@@ -663,9 +985,9 @@ public function get_all_product_categories(WP_REST_Request $request) {
663985
public function get_product_categories(WP_REST_Request $request) {
664986
$this->options = get_option('content_api_options_polyplugins');
665987

666-
$fields = $request->get_json_params();
667-
$product_id = isset($fields['product_id']) ? absint($fields['product_id']) : 0;
668-
$sku = isset($fields['sku']) ? sanitize_text_field($fields['sku']) : '';
988+
$fields = $request->get_json_params();
989+
$product_id = isset($fields['product_id']) ? absint($fields['product_id']) : 0;
990+
$sku = isset($fields['sku']) ? sanitize_text_field($fields['sku']) : '';
669991

670992
if (!$product_id && !$sku) {
671993
return new WP_Error('missing_identifier', 'Product ID or SKU is required', array('status' => 400));

0 commit comments

Comments
 (0)