Skip to content

Commit b4e942e

Browse files
authored
Merge pull request #1 from PolyPlugins/endpoints/taxonomy/brand
Added brand endpoints
2 parents 5930a5f + 44a422a commit b4e942e

File tree

3 files changed

+317
-2
lines changed

3 files changed

+317
-2
lines changed

README.MD

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

6666
## Changelog
6767

68+
### 1.0.2
69+
70+
* Added: [Get Brand](https://www.polyplugins.com/docs/content-api/api/get-brand/) endpoint
71+
* Added: [Update Brand](https://www.polyplugins.com/docs/content-api/api/update-brand/) endpoint
72+
* Added: [Create Brand](https://www.polyplugins.com/docs/content-api/api/create-brand/) endpoint
73+
6874
### 1.0.1
6975

7076
* Added: [Get Product Brands](https://www.polyplugins.com/docs/content-api/api/get-product-brands/) endpoint

content-api.php

Lines changed: 305 additions & 1 deletion
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.1
7+
* Version: 1.0.2
88
* Requires at least: 6.5
99
* Requires PHP: 7.4
1010
* Author: Poly Plugins
@@ -110,6 +110,24 @@ public function register_endpoints() {
110110
'permission_callback' => array($this, 'has_permission'), // Add your permission callback for security
111111
));
112112

113+
register_rest_route('content-api/v1', '/taxonomy/brand/', array(
114+
'methods' => 'GET',
115+
'callback' => array($this, 'get_brand'),
116+
'permission_callback' => array($this, 'has_permission'), // Add your permission callback for security
117+
));
118+
119+
register_rest_route('content-api/v1', '/taxonomy/brand/', array(
120+
'methods' => 'PATCH',
121+
'callback' => array($this, 'update_brand'),
122+
'permission_callback' => array($this, 'has_permission'), // Add your permission callback for security
123+
));
124+
125+
register_rest_route('content-api/v1', '/taxonomy/brand/', array(
126+
'methods' => 'POST',
127+
'callback' => array($this, 'create_brand'),
128+
'permission_callback' => array($this, 'has_permission'), // Add your permission callback for security
129+
));
130+
113131
register_rest_route('content-api/v1', '/product/brands/', array(
114132
'methods' => 'GET',
115133
'callback' => array($this, 'get_product_brands'),
@@ -804,6 +822,292 @@ public function update_product_categories(WP_REST_Request $request) {
804822
), 200);
805823
}
806824

825+
/**
826+
* Get product brands
827+
*
828+
* @param WP_REST_Request $request
829+
* @return WP_REST_Response
830+
*/
831+
public function get_brand(WP_REST_Request $request) {
832+
$params = $request->get_params();
833+
$taxonomy = isset($params['taxonomy']) ? sanitize_text_field($params['taxonomy']) : 'product_brand';
834+
$id = isset($params['id']) ? absint($params['id']) : 0;
835+
$slug = isset($params['slug']) ? sanitize_title($params['slug']) : '';
836+
$name = isset($params['name']) ? sanitize_text_field($params['name']) : '';
837+
838+
if ($id) {
839+
$term = get_term($id, $taxonomy);
840+
} elseif ($slug) {
841+
$term = get_term_by('slug', $slug, $taxonomy);
842+
} elseif ($name) {
843+
$term = get_term_by('name', $name, $taxonomy);
844+
}
845+
846+
if (empty($term) || is_wp_error($term)) {
847+
return new WP_Error('no_brand_found', "No brand found under the " . $taxonomy . " taxonomy.", array('status' => 400));
848+
}
849+
850+
$term_id = $term->term_id;
851+
$brand_data = (array) $term;
852+
853+
// Get thumbnail ID and URL
854+
$thumbnail_id = get_term_meta($term_id, 'thumbnail_id', true);
855+
$thumbnail_url = $thumbnail_id ? wp_get_attachment_url($thumbnail_id) : '';
856+
857+
if ($thumbnail_url) {
858+
$brand_data['thumbnail'] = $thumbnail_url;
859+
}
860+
861+
// Get Yoast SEO meta fields
862+
$yoast_option = get_option('wpseo_taxonomy_meta');
863+
$yoast_data = [];
864+
865+
if (isset($yoast_option[$taxonomy]) && isset($yoast_option[$taxonomy][$term_id])) {
866+
$yoast_raw = $yoast_option[$taxonomy][$term_id];
867+
$yoast_data = array(
868+
'title' => $yoast_raw['wpseo_title'] ?? '',
869+
'description' => $yoast_raw['wpseo_desc'] ?? '',
870+
'premium' => array(
871+
'social_appearance' => array(
872+
'title' => $yoast_raw['wpseo_opengraph-title'] ?? '',
873+
'description' => $yoast_raw['wpseo_opengraph-description'] ?? '',
874+
'image' => $yoast_raw['wpseo_opengraph-image'] ?? '',
875+
),
876+
'x' => array(
877+
'title' => $yoast_raw['wpseo_twitter-title'] ?? '',
878+
'description' => $yoast_raw['wpseo_twitter-description'] ?? '',
879+
'image' => $yoast_raw['wpseo_twitter-image'] ?? '',
880+
)
881+
)
882+
);
883+
}
884+
885+
$brand_data['yoast'] = $yoast_data;
886+
887+
return new WP_REST_Response(array(
888+
'success' => true,
889+
'data' => $brand_data,
890+
), 201);
891+
}
892+
893+
/**
894+
* Get product brands
895+
*
896+
* @param WP_REST_Request $request
897+
* @return WP_REST_Response
898+
*/
899+
public function update_brand(WP_REST_Request $request) {
900+
$fields = $request->get_json_params();
901+
$id = isset($fields['id']) ? absint($fields['id']) : 0;
902+
$name = isset($fields['name']) ? sanitize_text_field($fields['name']) : '';
903+
$description = isset($fields['description']) ? wp_kses_post($fields['description']) : '';
904+
$slug = isset($fields['slug']) ? sanitize_title($fields['slug']) : '';
905+
$parent_id = isset($fields['parent']) ? absint($fields['parent']) : '';
906+
$taxonomy = isset($fields['taxonomy']) ? sanitize_text_field($fields['taxonomy']) : 'product_brand';
907+
908+
// Basic input validation
909+
if (!$id) {
910+
return new WP_Error('missing_identifier', 'Brand ID is required', array('status' => 400));
911+
}
912+
913+
$term = get_term($id, $taxonomy);
914+
915+
if (!$term || is_wp_error($term)) {
916+
return new WP_Error('brand_not_found', 'Brand not found', array('status' => 404));
917+
}
918+
919+
$term_id = $term->term_id;
920+
921+
$term_args = [];
922+
923+
if ($name) {
924+
$term_args['name'] = $name;
925+
}
926+
927+
if ($description) {
928+
$term_args['description'] = $description;
929+
}
930+
931+
if ($slug) {
932+
$term_args['slug'] = $slug;
933+
}
934+
935+
if ($parent_id > 0) {
936+
$parent_term = get_term($parent_id, $taxonomy);
937+
938+
if ($parent_term && !is_wp_error($parent_term)) {
939+
$term_args['parent'] = $parent_id;
940+
} else {
941+
return new WP_Error('invalid_parent', 'The specified parent brand does not exist.', array('status' => 400));
942+
}
943+
} elseif ($parent_id === 0) {
944+
$term_args['parent'] = 0; // Remove parent
945+
}
946+
947+
if (!empty($term_args)) {
948+
$result = wp_update_term($term_id, $taxonomy, $term_args);
949+
950+
if (is_wp_error($result)) {
951+
return new WP_Error('term_update_failed', 'Failed to update brand term', array('status' => 500));
952+
}
953+
}
954+
955+
// Update Yoast fields
956+
if (isset($fields['yoast']) && is_array($fields['yoast'])) {
957+
$yoast_option = get_option('wpseo_taxonomy_meta');
958+
959+
if (!isset($yoast_option[$taxonomy])) {
960+
$yoast_option[$taxonomy] = [];
961+
}
962+
963+
if (!isset($yoast_option[$taxonomy][$term_id])) {
964+
$yoast_option[$taxonomy][$term_id] = [];
965+
}
966+
967+
$yoast_input = $fields['yoast'];
968+
969+
if (isset($yoast_input['title']) && $yoast_input['title']) {
970+
$yoast_option[$taxonomy][$term_id]['wpseo_title'] = $yoast_input['title'];
971+
}
972+
973+
if (isset($yoast_input['description']) && $yoast_input['description']) {
974+
$yoast_option[$taxonomy][$term_id]['wpseo_desc'] = $yoast_input['description'];
975+
}
976+
977+
if (isset($yoast_input['premium']['social_appearance']['title']) && $yoast_input['premium']['social_appearance']['title']) {
978+
$yoast_option[$taxonomy][$term_id]['wpseo_opengraph-title'] = $yoast_input['premium']['social_appearance']['title'];
979+
}
980+
981+
if (isset($yoast_input['premium']['social_appearance']['description']) && $yoast_input['premium']['social_appearance']['description']) {
982+
$yoast_option[$taxonomy][$term_id]['wpseo_opengraph-description'] = $yoast_input['premium']['social_appearance']['description'];
983+
}
984+
985+
if (isset($yoast_input['premium']['social_appearance']['image']) && $yoast_input['premium']['social_appearance']['image']) {
986+
$yoast_option[$taxonomy][$term_id]['wpseo_opengraph-image'] = $yoast_input['premium']['social_appearance']['image'];
987+
}
988+
989+
if (isset($yoast_input['premium']['x']['title']) && $yoast_input['premium']['x']['title']) {
990+
$yoast_option[$taxonomy][$term_id]['wpseo_twitter-title'] = $yoast_input['premium']['x']['title'];
991+
}
992+
993+
if (isset($yoast_input['premium']['x']['description']) && $yoast_input['premium']['x']['description']) {
994+
$yoast_option[$taxonomy][$term_id]['wpseo_twitter-description'] = $yoast_input['premium']['x']['description'];
995+
}
996+
997+
if (isset($yoast_input['premium']['x']['image']) && $yoast_input['premium']['x']['image']) {
998+
$yoast_option[$taxonomy][$term_id]['wpseo_twitter-image'] = $yoast_input['premium']['x']['image'];
999+
}
1000+
1001+
update_option('wpseo_taxonomy_meta', $yoast_option);
1002+
}
1003+
1004+
return new WP_REST_Response(array(
1005+
'success' => true,
1006+
'brand_id' => $term_id,
1007+
'message' => 'Brand updated successfully.'
1008+
), 200);
1009+
}
1010+
1011+
/**
1012+
* Create a new product brand
1013+
*
1014+
* @param WP_REST_Request $request
1015+
* @return WP_REST_Response
1016+
*/
1017+
public function create_brand(WP_REST_Request $request) {
1018+
$fields = $request->get_json_params();
1019+
$name = isset($fields['name']) ? sanitize_text_field($fields['name']) : '';
1020+
$description = isset($fields['description']) ? wp_kses_post($fields['description']) : '';
1021+
$slug = isset($fields['slug']) ? sanitize_title($fields['slug']) : '';
1022+
$parent_id = isset($fields['parent']) ? absint($fields['parent']) : 0;
1023+
$taxonomy = isset($fields['taxonomy']) ? sanitize_text_field($fields['taxonomy']) : 'product_brand';
1024+
1025+
// Validate name
1026+
if (empty($name)) {
1027+
return new WP_Error('missing_name', 'Brand name is required.', array('status' => 400));
1028+
}
1029+
1030+
$term_args = array(
1031+
'description' => $description,
1032+
'slug' => $slug,
1033+
'parent' => 0
1034+
);
1035+
1036+
if ($parent_id > 0) {
1037+
$parent_term = get_term($parent_id, $taxonomy);
1038+
1039+
if ($parent_term && !is_wp_error($parent_term)) {
1040+
$term_args['parent'] = $parent_id;
1041+
} else {
1042+
return new WP_Error('invalid_parent', 'The specified parent brand does not exist.', array('status' => 400));
1043+
}
1044+
}
1045+
1046+
// Create the term
1047+
$result = wp_insert_term($name, $taxonomy, $term_args);
1048+
1049+
if (is_wp_error($result)) {
1050+
return new WP_Error('term_creation_failed', $result->get_error_message(), array('status' => 500));
1051+
}
1052+
1053+
$term_id = $result['term_id'];
1054+
1055+
// Update Yoast fields
1056+
if (isset($fields['yoast']) && is_array($fields['yoast'])) {
1057+
$yoast_option = get_option('wpseo_taxonomy_meta');
1058+
1059+
if (!isset($yoast_option[$taxonomy])) {
1060+
$yoast_option[$taxonomy] = array();
1061+
}
1062+
1063+
if (!isset($yoast_option[$taxonomy][$term_id])) {
1064+
$yoast_option[$taxonomy][$term_id] = array();
1065+
}
1066+
1067+
$yoast_input = $fields['yoast'];
1068+
1069+
if (!empty($yoast_input['title'])) {
1070+
$yoast_option[$taxonomy][$term_id]['wpseo_title'] = $yoast_input['title'];
1071+
}
1072+
1073+
if (!empty($yoast_input['description'])) {
1074+
$yoast_option[$taxonomy][$term_id]['wpseo_desc'] = $yoast_input['description'];
1075+
}
1076+
1077+
if (!empty($yoast_input['premium']['social_appearance']['title'])) {
1078+
$yoast_option[$taxonomy][$term_id]['wpseo_opengraph-title'] = $yoast_input['premium']['social_appearance']['title'];
1079+
}
1080+
1081+
if (!empty($yoast_input['premium']['social_appearance']['description'])) {
1082+
$yoast_option[$taxonomy][$term_id]['wpseo_opengraph-description'] = $yoast_input['premium']['social_appearance']['description'];
1083+
}
1084+
1085+
if (!empty($yoast_input['premium']['social_appearance']['image'])) {
1086+
$yoast_option[$taxonomy][$term_id]['wpseo_opengraph-image'] = $yoast_input['premium']['social_appearance']['image'];
1087+
}
1088+
1089+
if (!empty($yoast_input['premium']['x']['title'])) {
1090+
$yoast_option[$taxonomy][$term_id]['wpseo_twitter-title'] = $yoast_input['premium']['x']['title'];
1091+
}
1092+
1093+
if (!empty($yoast_input['premium']['x']['description'])) {
1094+
$yoast_option[$taxonomy][$term_id]['wpseo_twitter-description'] = $yoast_input['premium']['x']['description'];
1095+
}
1096+
1097+
if (!empty($yoast_input['premium']['x']['image'])) {
1098+
$yoast_option[$taxonomy][$term_id]['wpseo_twitter-image'] = $yoast_input['premium']['x']['image'];
1099+
}
1100+
1101+
update_option('wpseo_taxonomy_meta', $yoast_option);
1102+
}
1103+
1104+
return new WP_REST_Response(array(
1105+
'success' => true,
1106+
'brand_id' => $term_id,
1107+
'message' => 'Brand created successfully.'
1108+
), 201);
1109+
}
1110+
8071111
/**
8081112
* Get terms
8091113
*

readme.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Contributors: polyplugins
33
Tags: content api, rest api, content, creative, api
44
Tested up to: 6.8
5-
Stable tag: 1.0.1
5+
Stable tag: 1.0.2
66
Requires PHP: 7.4
77
License: GPLv3
88
License URI: https://www.gnu.org/licenses/gpl-3.0.html
@@ -84,6 +84,11 @@ Yes, the plugin supports uploading images to the WordPress media library via the
8484

8585
== Changelog ==
8686

87+
= 1.0.2 =
88+
* Added: [Get Brand](https://www.polyplugins.com/docs/content-api/api/get-brand/) endpoint
89+
* Added: [Update Brand](https://www.polyplugins.com/docs/content-api/api/update-brand/) endpoint
90+
* Added: [Create Brand](https://www.polyplugins.com/docs/content-api/api/create-brand/) endpoint
91+
8792
= 1.0.1 =
8893
* Added: [Get Product Brands](https://www.polyplugins.com/docs/content-api/api/get-product-brands/) endpoint
8994
* Added: [Update Product Brands](https://www.polyplugins.com/docs/content-api/api/update-product-brands/) endpoint

0 commit comments

Comments
 (0)