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.3
7+ * Version: 1.0.4
88 * Requires at least: 6.5
99 * Requires PHP: 7.4
1010 * Author: Poly Plugins
1616namespace PolyPlugins ;
1717
1818use WC_Product_Attribute ;
19+ use WC_Product_Simple ;
1920use WP_Error ;
2021use WP_REST_Request ;
2122use WP_REST_Response ;
@@ -62,6 +63,12 @@ public function register_endpoints() {
6263 'permission_callback ' => array ($ this , 'has_permission ' ), // Add your permission callback for security
6364 ));
6465
66+ register_rest_route ('content-api/v1 ' , '/product/ ' , array (
67+ 'methods ' => 'POST ' ,
68+ 'callback ' => array ($ this , 'create_product ' ),
69+ 'permission_callback ' => array ($ this , 'has_permission ' ), // Add your permission callback for security
70+ ));
71+
6572 register_rest_route ('content-api/v1 ' , '/product-category/ ' , array (
6673 'methods ' => 'GET ' ,
6774 'callback ' => array ($ this , 'get_product_category ' ),
@@ -413,11 +420,30 @@ public function get_product(WP_REST_Request $request) {
413420 $ short_description = $ product ->get_short_description ();
414421 $ regular_price = $ product ->get_regular_price ();
415422 $ sale_price = $ product ->get_sale_price ();
423+ $ map_price = $ product ->get_meta ('_map_price ' );
424+ $ cost = $ product ->get_meta ('_cost ' );
416425 $ sku = $ product ->get_sku ();
417426 $ stock_status = $ product ->get_stock_status ();
418427 $ stock_quantity = $ product ->get_stock_quantity ();
419428 $ tags = wp_get_post_terms ($ product ->get_id (), 'product_tag ' , array ('fields ' => 'names ' ));
420- $ images = array_map (function ($ image ) {return $ image ['src ' ];}, $ product ->get_gallery_image_ids () ? wp_get_attachment_image_src ($ product ->get_image_id (), 'full ' ) : array ());
429+ $ categories = wp_get_post_terms ($ product ->get_id (), 'product_cat ' , array ('fields ' => 'names ' ));
430+
431+ $ images = array ();
432+ $ gallery_ids = $ product ->get_gallery_image_ids ();
433+
434+ foreach ($ gallery_ids as $ id ) {
435+ $ image = wp_get_attachment_image_src ($ id , 'full ' );
436+ if ($ image ) {
437+ $ images [] = $ image [0 ];
438+ }
439+ }
440+
441+ $ featured_image = wp_get_attachment_image_src ($ product ->get_image_id (), 'full ' );
442+
443+ if ($ featured_image ) {
444+ array_unshift ($ images , $ featured_image [0 ]);
445+ }
446+
421447 $ featured_image = wp_get_attachment_url ($ product ->get_image_id ());
422448
423449 $ response_data = array (
@@ -428,10 +454,13 @@ public function get_product(WP_REST_Request $request) {
428454 'short_description ' => $ short_description ? wp_kses_post ($ short_description ) : '' ,
429455 'price ' => $ regular_price ? floatval ($ regular_price ) : '' ,
430456 'sale_price ' => $ sale_price ? floatval ($ sale_price ) : '' ,
457+ 'map_price ' => $ map_price ? floatval ($ map_price ) : '' ,
458+ 'cost ' => $ cost ? floatval ($ cost ) : '' ,
431459 'sku ' => $ sku ? sanitize_text_field ($ sku ) : '' ,
432460 'stock_status ' => $ stock_status ? sanitize_text_field ($ product ->get_stock_status ()) : '' ,
433461 'stock_quantity ' => $ stock_quantity ? absint ($ product ->get_stock_quantity ()) : 0 ,
434462 'tags ' => $ tags ? array_map ('sanitize_text_field ' , $ tags ) : array (),
463+ 'categories ' => $ categories ? array_map ('sanitize_text_field ' , $ categories ) : array (),
435464 'images ' => $ images ? array_map ('esc_url_raw ' , $ images ) : array (),
436465 'featured_image ' => $ featured_image ? esc_url_raw ($ featured_image ) : '' ,
437466 );
@@ -456,10 +485,13 @@ public function update_product(WP_REST_Request $request) {
456485 $ short_description = isset ($ fields ['short_description ' ]) ? wp_kses_post ($ fields ['short_description ' ]) : '' ;
457486 $ price = isset ($ fields ['price ' ]) ? floatval ($ fields ['price ' ]) : '' ;
458487 $ sale_price = isset ($ fields ['sale_price ' ]) ? floatval ($ fields ['sale_price ' ]) : '' ;
488+ $ map_price = isset ($ fields ['map_price ' ]) ? floatval ($ fields ['map_price ' ]) : '' ;
489+ $ cost = isset ($ fields ['cost ' ]) ? floatval ($ fields ['cost ' ]) : '' ;
459490 $ sku = isset ($ fields ['sku ' ]) ? sanitize_text_field ($ fields ['sku ' ]) : '' ;
460491 $ stock_status = isset ($ fields ['stock_status ' ]) ? sanitize_text_field ($ fields ['stock_status ' ]) : '' ;
461492 $ stock_quantity = isset ($ fields ['stock_quantity ' ]) ? intval ($ fields ['stock_quantity ' ]) : '' ;
462493 $ tags = isset ($ fields ['tags ' ]) && is_array ($ fields ['tags ' ]) ? array_map ('sanitize_text_field ' , $ fields ['tags ' ]) : array ();
494+ $ categories = isset ($ fields ['categories ' ]) && is_array ($ fields ['categories ' ]) ? array_map ('sanitize_text_field ' , $ fields ['categories ' ]) : array ();
463495 $ featured_image = isset ($ fields ['featured_image ' ]) ? sanitize_url ($ fields ['featured_image ' ]) : '' ;
464496 $ images = isset ($ fields ['images ' ]) && is_array ($ fields ['images ' ]) ? array_map ('sanitize_url ' , $ fields ['images ' ]) : array ();
465497 $ yoast = isset ($ fields ['yoast ' ]) ? $ fields ['yoast ' ] : '' ;
@@ -539,6 +571,14 @@ public function update_product(WP_REST_Request $request) {
539571 $ product ->set_sale_price ($ sale_price );
540572 }
541573
574+ if ($ map_price ) {
575+ $ product ->update_meta_data ('_map_price ' , $ map_price );
576+ }
577+
578+ if ($ cost ) {
579+ $ product ->update_meta_data ('_cost ' , $ cost );
580+ }
581+
542582 if ($ sku ) {
543583 $ product ->set_sku ($ sku );
544584 }
@@ -548,13 +588,18 @@ public function update_product(WP_REST_Request $request) {
548588 }
549589
550590 if ($ stock_quantity ) {
591+ $ product ->set_manage_stock (true );
551592 $ product ->set_stock_quantity ($ stock_quantity );
552593 }
553594
554595 if ($ tags ) {
555596 wp_set_object_terms ($ product_id , $ tags , 'product_tag ' );
556597 }
557598
599+ if ($ categories ) {
600+ wp_set_object_terms ($ product_id , $ categories , 'product_cat ' );
601+ }
602+
558603 // Handle featured image
559604 if ($ featured_image ) {
560605 $ image_id = $ this ->upload_image_to_media_library ($ featured_image , $ product_id );
@@ -632,6 +677,193 @@ public function update_product(WP_REST_Request $request) {
632677 ), 200 );
633678 }
634679
680+ /**
681+ * Create product
682+ *
683+ * @param mixed $request
684+ * @return void
685+ */
686+ public function create_product (WP_REST_Request $ request ) {
687+ $ this ->options = get_option ('content_api_options_polyplugins ' );
688+
689+ $ fields = $ request ->get_json_params ();
690+ $ name = isset ($ fields ['name ' ]) ? sanitize_text_field ($ fields ['name ' ]) : '' ;
691+ $ status = isset ($ fields ['status ' ]) ? sanitize_text_field ($ fields ['status ' ]) : 'draft ' ;
692+ $ slug = isset ($ fields ['slug ' ]) ? sanitize_title ($ fields ['slug ' ]) : '' ;
693+ $ description = isset ($ fields ['description ' ]) ? wp_kses_post ($ fields ['description ' ]) : '' ;
694+ $ short_description = isset ($ fields ['short_description ' ]) ? wp_kses_post ($ fields ['short_description ' ]) : '' ;
695+ $ price = isset ($ fields ['price ' ]) ? floatval ($ fields ['price ' ]) : '' ;
696+ $ sale_price = isset ($ fields ['sale_price ' ]) ? floatval ($ fields ['sale_price ' ]) : '' ;
697+ $ map_price = isset ($ fields ['map_price ' ]) ? floatval ($ fields ['map_price ' ]) : '' ;
698+ $ cost = isset ($ fields ['cost ' ]) ? floatval ($ fields ['cost ' ]) : '' ;
699+ $ sku = isset ($ fields ['sku ' ]) ? sanitize_text_field ($ fields ['sku ' ]) : '' ;
700+ $ stock_status = isset ($ fields ['stock_status ' ]) ? sanitize_text_field ($ fields ['stock_status ' ]) : '' ;
701+ $ stock_quantity = isset ($ fields ['stock_quantity ' ]) ? intval ($ fields ['stock_quantity ' ]) : '' ;
702+ $ tags = isset ($ fields ['tags ' ]) && is_array ($ fields ['tags ' ]) ? array_map ('sanitize_text_field ' , $ fields ['tags ' ]) : array ();
703+ $ categories = isset ($ fields ['categories ' ]) && is_array ($ fields ['categories ' ]) ? array_map ('sanitize_text_field ' , $ fields ['categories ' ]) : array ();
704+ $ featured_image = isset ($ fields ['featured_image ' ]) ? sanitize_url ($ fields ['featured_image ' ]) : '' ;
705+ $ images = isset ($ fields ['images ' ]) && is_array ($ fields ['images ' ]) ? array_map ('sanitize_url ' , $ fields ['images ' ]) : array ();
706+ $ yoast = isset ($ fields ['yoast ' ]) ? $ fields ['yoast ' ] : '' ;
707+
708+ if ($ sku ) {
709+ if ($ sku !== sanitize_text_field ($ sku )) {
710+ return new WP_Error ('sku_invalid ' , 'SKU is invalid ' , array ('status ' => 400 ));
711+ }
712+ }
713+
714+ if ($ sku ) {
715+ $ product_id_by_sku = wc_get_product_id_by_sku ($ sku );
716+
717+ if ($ product_id_by_sku ) {
718+ return new WP_Error ('product_sku_exists ' , 'Product with provided SKU already exists ' , array ('status ' => 404 ));
719+ }
720+ }
721+
722+ if ($ slug ) {
723+ // Check if slug already exists for a different product
724+ if (wc_get_product_id_by_sku ($ sku )) {
725+ return new WP_Error ('slug_exists ' , 'Slug already in use by another product ' , array ('status ' => 400 ));
726+ }
727+ }
728+
729+ $ product = new WC_Product_Simple ();
730+
731+ if ($ name ) {
732+ $ product ->set_name ($ name );
733+ }
734+
735+ if ($ status ) {
736+ $ product ->set_status ($ status );
737+ }
738+
739+ if ($ slug ) {
740+ $ product ->set_slug ($ slug );
741+ }
742+
743+ if ($ description ) {
744+ $ product ->set_description ($ description );
745+ }
746+
747+ if ($ short_description ) {
748+ $ product ->set_short_description ($ short_description );
749+ }
750+
751+ if ($ price ) {
752+ $ product ->set_regular_price ($ price );
753+ }
754+
755+ if ($ sale_price ) {
756+ $ product ->set_sale_price ($ sale_price );
757+ }
758+
759+ if ($ map_price ) {
760+ $ product ->update_meta_data ('_map_price ' , $ map_price );
761+ }
762+
763+ if ($ cost ) {
764+ $ product ->update_meta_data ('_cost ' , $ cost );
765+ }
766+
767+ if ($ sku ) {
768+ $ product ->set_sku ($ sku );
769+ }
770+
771+ if ($ stock_status ) {
772+ $ product ->set_stock_status ($ stock_status );
773+ }
774+
775+ if ($ stock_quantity ) {
776+ $ product ->set_manage_stock (true );
777+ $ product ->set_stock_quantity ($ stock_quantity );
778+ }
779+
780+ // Assign Yoast Meta Title
781+ if (isset ($ yoast ['title ' ])) {
782+ $ product ->update_meta_data ('_yoast_wpseo_title ' , sanitize_text_field ($ yoast ['title ' ]));
783+ }
784+
785+ // Assign Yoast Meta Description
786+ if (isset ($ yoast ['description ' ])) {
787+ $ product ->update_meta_data ('_yoast_wpseo_metadesc ' , sanitize_text_field ($ yoast ['description ' ]));
788+ }
789+
790+ // Assign Yoast Facebook Open Graph Title
791+ if (isset ($ yoast ['premium ' ]['social_appearance ' ]['title ' ])) {
792+ $ product ->update_meta_data ('_yoast_wpseo_opengraph-title ' , sanitize_text_field ($ yoast ['premium ' ]['social_appearance ' ]['title ' ]));
793+ }
794+
795+ // Assign Yoast Facebook Open Graph Description
796+ if (isset ($ yoast ['premium ' ]['social_appearance ' ]['description ' ])) {
797+ $ product ->update_meta_data ('_yoast_wpseo_opengraph-description ' , sanitize_text_field ($ yoast ['premium ' ]['social_appearance ' ]['description ' ]));
798+ }
799+
800+ // Assign Yoast Facebook Open Graph Image
801+ if (isset ($ yoast ['premium ' ]['social_appearance ' ]['image ' ])) {
802+ $ product ->update_meta_data ('_yoast_wpseo_opengraph-image ' , sanitize_url ($ yoast ['premium ' ]['social_appearance ' ]['image ' ]));
803+ }
804+
805+ // Assign Yoast Facebook Open Graph Title
806+ if (isset ($ yoast ['premium ' ]['x ' ]['title ' ])) {
807+ $ product ->update_meta_data ('_yoast_wpseo_twitter-title ' , sanitize_text_field ($ yoast ['premium ' ]['x ' ]['title ' ]));
808+ }
809+
810+ // Assign Yoast Facebook Open Graph Description
811+ if (isset ($ yoast ['premium ' ]['x ' ]['description ' ])) {
812+ $ product ->update_meta_data ('_yoast_wpseo_twitter-description ' , sanitize_text_field ($ yoast ['premium ' ]['x ' ]['description ' ]));
813+ }
814+
815+ // Assign Yoast Facebook Open Graph Image
816+ if (isset ($ yoast ['premium ' ]['x ' ]['image ' ])) {
817+ $ product ->update_meta_data ('_yoast_wpseo_twitter-image ' , sanitize_url ($ yoast ['premium ' ]['x ' ]['image ' ]));
818+ }
819+
820+ // Save the product
821+ $ product_id = $ product ->save ();
822+
823+ if ($ tags ) {
824+ wp_set_object_terms ($ product_id , $ tags , 'product_tag ' );
825+ }
826+
827+ if ($ categories ) {
828+ wp_set_object_terms ($ product_id , $ categories , 'product_cat ' );
829+ }
830+
831+ // Handle featured image
832+ if ($ featured_image ) {
833+ $ image_id = $ this ->upload_image_to_media_library ($ featured_image , $ product_id );
834+
835+ if ($ image_id && !is_wp_error ($ image_id )) {
836+ $ product ->set_image_id ($ image_id );
837+ }
838+ }
839+
840+ // Handle gallery images
841+ if ($ images ) {
842+ $ image_ids = array ();
843+ foreach ($ images as $ image_url ) {
844+ $ image_id = $ this ->upload_image_to_media_library ($ image_url , $ product_id );
845+ if ($ image_id && !is_wp_error ($ image_id )) {
846+ $ image_ids [] = $ image_id ;
847+ }
848+ }
849+
850+ $ product ->set_gallery_image_ids ($ image_ids );
851+ }
852+
853+ $ product ->save ();
854+
855+ if (!$ product_id ) {
856+ return new WP_Error ('creation_failed ' , 'Failed to create product ' , array ('status ' => 500 ));
857+ }
858+
859+ // Return success response
860+ return new WP_REST_Response (array (
861+ 'success ' => true ,
862+ 'product_id ' => $ product_id ,
863+ 'message ' => 'Product created successfully '
864+ ), 200 );
865+ }
866+
635867 /**
636868 * Get all product categories
637869 *
0 commit comments