Changeset 3306098
- Timestamp:
- 06/04/2025 02:03:14 AM (9 months ago)
- Location:
- content-api
- Files:
-
- 4 edited
- 1 copied
-
tags/1.0.9 (copied) (copied from content-api/trunk)
-
tags/1.0.9/content-api.php (modified) (17 diffs)
-
tags/1.0.9/readme.txt (modified) (2 diffs)
-
trunk/content-api.php (modified) (17 diffs)
-
trunk/readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
content-api/tags/1.0.9/content-api.php
r3305371 r3306098 5 5 * Plugin URI: https://www.polyplugins.com/contact/ 6 6 * Description: Adds various endpoints to create content 7 * Version: 1.0. 87 * Version: 1.0.9 8 8 * Requires at least: 6.5 9 9 * Requires PHP: 7.4 … … 86 86 'methods' => 'POST', 87 87 'callback' => array($this, 'create_product_category'), 88 'permission_callback' => array($this, 'has_permission'), // Add your permission callback for security 89 )); 90 91 register_rest_route('content-api/v1', '/product-ids/', array( 92 'methods' => 'GET', 93 'callback' => array($this, 'get_all_product_ids'), 88 94 'permission_callback' => array($this, 'has_permission'), // Add your permission callback for security 89 95 )); … … 346 352 public function get_product(WP_REST_Request $request) { 347 353 $this->options = get_option('content_api_options_polyplugins'); 354 348 355 349 $product_id = $request->get_param('product_id'); 350 $sku = $request->get_param('sku'); 351 $missing_description = $request->get_param('missing_description') ? true : false; 352 $missing_description_limit = $request->get_param('missing_description_limit') ? absint($request->get_param('missing_description_limit')) : 100; 356 $fields = $request->get_params(); 357 $product_id = isset($fields['product_id']) && is_numeric($fields['product_id']) ? absint($fields['product_id']) : ''; 358 $sku = isset($fields['sku']) ? sanitize_text_field($fields['sku']) : ''; 359 $missing_description = isset($fields['missing_description']) ? true : false; 360 $missing_description_limit = isset($fields['missing_description_limit']) ? absint($fields['missing_description_limit']) : 100; 353 361 354 362 if ($missing_description) { … … 368 376 $products[] = array( 369 377 'product_id' => $product_id, 370 'sku' => $sku378 'sku' => sanitize_text_field($sku) 371 379 ); 372 380 } … … 405 413 return new WP_Error('product_not_found', 'Product not found with provided SKU', array('status' => 404)); 406 414 } 407 } 408 elseif ($product_id) { 415 } elseif ($product_id) { 409 416 $product = wc_get_product($product_id); 410 417 } … … 417 424 $product_id = $product->get_id(); 418 425 $name = $product->get_name(); 426 $status = $product->get_status(); 419 427 $slug = $product->get_slug(); 420 428 $description = $product->get_description(); … … 428 436 $weight = $product->get_weight(); 429 437 $stock_status = $product->get_stock_status(); 438 $manage_stock = $product->get_manage_stock(); 430 439 $stock_quantity = $product->get_stock_quantity(); 431 440 $tags = wp_get_post_terms($product->get_id(), 'product_tag', array('fields' => 'names')); 432 441 $categories = wp_get_post_terms($product->get_id(), 'product_cat', array('fields' => 'names')); 433 434 $images = array(); 435 $gallery_ids = $product->get_gallery_image_ids(); 442 $attributes = array(); 443 $raw_attributes = $product->get_attributes(); 444 $images = array(); 445 $gallery_ids = $product->get_gallery_image_ids(); 446 $yoast = array( 447 "title" => sanitize_text_field($product->get_meta('_yoast_wpseo_title')), 448 "description" => sanitize_text_field($product->get_meta('_yoast_wpseo_metadesc')), 449 "premium" => array( 450 "social_appearance" => array( 451 "title" => sanitize_text_field($product->get_meta('_yoast_wpseo_opengraph-title')), 452 "description" => sanitize_text_field($product->get_meta('_yoast_wpseo_opengraph-description')), 453 "image" => sanitize_url($product->get_meta('_yoast_wpseo_opengraph-image')), 454 ), 455 "x" => array( 456 "title" => sanitize_text_field($product->get_meta('_yoast_wpseo_twitter-title')), 457 "description" => sanitize_text_field($product->get_meta('_yoast_wpseo_twitter-description')), 458 "image" => sanitize_url($product->get_meta('_yoast_wpseo_twitter-image')), 459 ) 460 ) 461 ); 462 463 foreach ($raw_attributes as $raw_attribute) { 464 if ($raw_attribute->is_taxonomy()) { 465 $terms = wp_get_post_terms($product->get_id(), $raw_attribute->get_name(), array('fields' => 'names')); 466 467 $attributes[] = array( 468 'name' => $raw_attribute->get_name(), 469 'value' => $terms, 470 ); 471 } else { 472 $attributes[] = array( 473 'name' => $raw_attribute->get_name(), 474 'value' => $raw_attribute->get_options(), 475 ); 476 } 477 } 436 478 437 479 foreach ($gallery_ids as $id) { … … 453 495 'product_id' => $product_id ? absint($product_id) : 0, 454 496 'name' => $name ? sanitize_text_field($name) : '', 497 'status' => $status ? sanitize_text_field($status) : '', 455 498 'slug' => $slug ? sanitize_title($slug) : '', 456 499 'description' => $description ? wp_kses_post($description) : '', … … 463 506 'upc' => $upc ? sanitize_text_field($upc) : '', 464 507 'weight' => $weight ? floatval($weight) : '', 465 'stock_status' => $stock_status ? sanitize_text_field($product->get_stock_status()) : '', 508 'stock_status' => $stock_status ? sanitize_text_field($stock_status) : '', 509 'manage_stock' => $manage_stock ? sanitize_text_field($manage_stock) : '', 466 510 'stock_quantity' => $stock_quantity ? absint($product->get_stock_quantity()) : 0, 467 511 'tags' => $tags ? array_map('sanitize_text_field', $tags) : array(), 468 512 'categories' => $categories ? array_map('sanitize_text_field', $categories) : array(), 513 'attributes' => $attributes, 469 514 'images' => $images ? array_map('esc_url_raw', $images) : array(), 470 515 'featured_image' => $featured_image ? esc_url_raw($featured_image) : '', 516 'yoast' => $yoast, 471 517 ); 472 518 … … 498 544 $weight = isset($fields['weight']) ? floatval($fields['weight']) : ''; 499 545 $stock_status = isset($fields['stock_status']) ? sanitize_text_field($fields['stock_status']) : ''; 500 $ stock_quantity = isset($fields['stock_quantity']) ? intval($fields['stock_quantity']) : '';546 $manage_stock = isset($fields['manage_stock']) ? sanitize_text_field($fields['manage_stock']) : ''; 501 547 $tags = isset($fields['tags']) && is_array($fields['tags']) ? array_map('sanitize_text_field', $fields['tags']) : array(); 502 548 $categories = isset($fields['categories']) && is_array($fields['categories']) ? array_map('sanitize_text_field', $fields['categories']) : array(); … … 537 583 return new WP_Error('product_not_found', 'Product not found', array('status' => 404)); 538 584 } 585 586 $stock_quantity = isset($fields['stock_quantity']) ? intval($fields['stock_quantity']) : false; 539 587 540 588 if ($slug) { … … 608 656 } 609 657 610 if ($stock_quantity >= 0) { 658 if ($manage_stock == 'true') { 659 $product->set_manage_stock(true); 660 } elseif ($manage_stock == 'false') { 661 $product->set_manage_stock(false); 662 } 663 664 if ($stock_quantity >= 0 && $stock_quantity !== false) { 611 665 $product->set_manage_stock(true); 612 666 $product->set_stock_quantity($stock_quantity); … … 721 775 $weight = isset($fields['weight']) ? floatval($fields['weight']) : ''; 722 776 $stock_status = isset($fields['stock_status']) ? sanitize_text_field($fields['stock_status']) : ''; 777 $manage_stock = isset($fields['manage_stock']) ? sanitize_text_field($fields['manage_stock']) : ''; 723 778 $stock_quantity = isset($fields['stock_quantity']) ? intval($fields['stock_quantity']) : ''; 724 779 $tags = isset($fields['tags']) && is_array($fields['tags']) ? array_map('sanitize_text_field', $fields['tags']) : array(); … … 807 862 } 808 863 809 if ($stock_quantity >= 0) { 864 if ($manage_stock == 'true') { 865 $product->set_manage_stock(true); 866 } elseif ($manage_stock == 'false') { 867 $product->set_manage_stock(false); 868 } 869 870 if ($stock_quantity >= 0 && $stock_quantity !== false) { 810 871 $product->set_manage_stock(true); 811 872 $product->set_stock_quantity($stock_quantity); … … 896 957 'product_id' => $product_id, 897 958 'message' => 'Product created successfully' 959 ), 200); 960 } 961 962 /** 963 * Get all product categories 964 * 965 * @param mixed $request 966 * @return void 967 */ 968 public function get_all_product_ids(WP_REST_Request $request) { 969 $this->options = get_option('content_api_options_polyplugins'); 970 971 $fields = $request->get_params(); 972 $limit = isset($fields['limit']) && is_numeric($fields['limit']) ? intval($fields['limit']) : -1; 973 $status = isset($fields['status']) ? sanitize_text_field($fields['status']) : ''; 974 975 $args = array( 976 'limit' => $limit, 977 'return' => 'ids' 978 ); 979 980 if ($status) { 981 $args['status'] = $status; 982 } 983 984 $products_ids = wc_get_products($args); 985 986 return new WP_REST_Response(array( 987 'success' => true, 988 'product_ids' => $products_ids 898 989 ), 200); 899 990 } … … 1859 1950 1860 1951 $formatted_attributes = array(); 1952 1861 1953 foreach ($attributes as $attribute) { 1862 $formatted_attributes[] = array( 1863 'name' => $attribute->get_name(), 1864 'value' => $attribute->get_options(), 1865 ); 1954 if ($attribute->is_taxonomy()) { 1955 $terms = wp_get_post_terms($product_id, $attribute->get_name(), array('fields' => 'names')); 1956 1957 $formatted_attributes[] = array( 1958 'name' => $attribute->get_name(), 1959 'value' => $terms, 1960 ); 1961 } else { 1962 $formatted_attributes[] = array( 1963 'name' => $attribute->get_name(), 1964 'value' => $attribute->get_options(), 1965 ); 1966 } 1866 1967 } 1867 1968 … … 1892 1993 $this->options = get_option('content_api_options_polyplugins'); 1893 1994 1894 $product_id = $request->get_param('product_id'); 1895 $sku = $request->get_param('sku'); 1995 $fields = $request->get_params(); 1996 $product_id = isset($fields['product_id']) && is_numeric($fields['product_id']) ? absint($fields['product_id']) : ''; 1997 $sku = isset($fields['sku']) ? sanitize_text_field($fields['sku']) : ''; 1896 1998 1897 1999 if (!$product_id && !$sku) { -
content-api/tags/1.0.9/readme.txt
r3305371 r3306098 3 3 Tags: content api, rest api, content, creative, api 4 4 Tested up to: 6.8 5 Stable tag: 1.0. 85 Stable tag: 1.0.9 6 6 Requires PHP: 7.4 7 7 License: GPLv3 … … 85 85 == Changelog == 86 86 87 = 1.0.9 = 88 * Added: [Get All Product IDs](https://www.polyplugins.com/docs/content-api/api/get-all-product-ids/) endpoint 89 * Added: Attribute manage_stock to results of [Get Product](https://www.polyplugins.com/docs/content-api/api/get-product/) endpoint 90 * Added: Attribute attributes to results of [Get Product](https://www.polyplugins.com/docs/content-api/api/get-product/) endpoint 91 * Added: Attribute yoast to results of [Get Product](https://www.polyplugins.com/docs/content-api/api/get-product/) endpoint 92 * Added: Attribute manage_stock to [Update Product](https://www.polyplugins.com/docs/content-api/api/update-product/) endpoint 93 * Added: Attribute manage_stock to [Create Product](https://www.polyplugins.com/docs/content-api/api/create-product/) endpoint 94 * Added: Security enhancements 95 * Updated: [Update Product](https://www.polyplugins.com/docs/content-api/api/update-product/) endpoint to not require quantity 96 * Bugfix: Taxonomy handling for [Get Attributes](https://www.polyplugins.com/docs/content-api/api/get-attributes/) endpoint 97 87 98 = 1.0.8 = 88 99 * Updated: [Update Product](https://www.polyplugins.com/docs/content-api/api/update-product/) endpoint to handle updating sku when product id is provided. -
content-api/trunk/content-api.php
r3305371 r3306098 5 5 * Plugin URI: https://www.polyplugins.com/contact/ 6 6 * Description: Adds various endpoints to create content 7 * Version: 1.0. 87 * Version: 1.0.9 8 8 * Requires at least: 6.5 9 9 * Requires PHP: 7.4 … … 86 86 'methods' => 'POST', 87 87 'callback' => array($this, 'create_product_category'), 88 'permission_callback' => array($this, 'has_permission'), // Add your permission callback for security 89 )); 90 91 register_rest_route('content-api/v1', '/product-ids/', array( 92 'methods' => 'GET', 93 'callback' => array($this, 'get_all_product_ids'), 88 94 'permission_callback' => array($this, 'has_permission'), // Add your permission callback for security 89 95 )); … … 346 352 public function get_product(WP_REST_Request $request) { 347 353 $this->options = get_option('content_api_options_polyplugins'); 354 348 355 349 $product_id = $request->get_param('product_id'); 350 $sku = $request->get_param('sku'); 351 $missing_description = $request->get_param('missing_description') ? true : false; 352 $missing_description_limit = $request->get_param('missing_description_limit') ? absint($request->get_param('missing_description_limit')) : 100; 356 $fields = $request->get_params(); 357 $product_id = isset($fields['product_id']) && is_numeric($fields['product_id']) ? absint($fields['product_id']) : ''; 358 $sku = isset($fields['sku']) ? sanitize_text_field($fields['sku']) : ''; 359 $missing_description = isset($fields['missing_description']) ? true : false; 360 $missing_description_limit = isset($fields['missing_description_limit']) ? absint($fields['missing_description_limit']) : 100; 353 361 354 362 if ($missing_description) { … … 368 376 $products[] = array( 369 377 'product_id' => $product_id, 370 'sku' => $sku378 'sku' => sanitize_text_field($sku) 371 379 ); 372 380 } … … 405 413 return new WP_Error('product_not_found', 'Product not found with provided SKU', array('status' => 404)); 406 414 } 407 } 408 elseif ($product_id) { 415 } elseif ($product_id) { 409 416 $product = wc_get_product($product_id); 410 417 } … … 417 424 $product_id = $product->get_id(); 418 425 $name = $product->get_name(); 426 $status = $product->get_status(); 419 427 $slug = $product->get_slug(); 420 428 $description = $product->get_description(); … … 428 436 $weight = $product->get_weight(); 429 437 $stock_status = $product->get_stock_status(); 438 $manage_stock = $product->get_manage_stock(); 430 439 $stock_quantity = $product->get_stock_quantity(); 431 440 $tags = wp_get_post_terms($product->get_id(), 'product_tag', array('fields' => 'names')); 432 441 $categories = wp_get_post_terms($product->get_id(), 'product_cat', array('fields' => 'names')); 433 434 $images = array(); 435 $gallery_ids = $product->get_gallery_image_ids(); 442 $attributes = array(); 443 $raw_attributes = $product->get_attributes(); 444 $images = array(); 445 $gallery_ids = $product->get_gallery_image_ids(); 446 $yoast = array( 447 "title" => sanitize_text_field($product->get_meta('_yoast_wpseo_title')), 448 "description" => sanitize_text_field($product->get_meta('_yoast_wpseo_metadesc')), 449 "premium" => array( 450 "social_appearance" => array( 451 "title" => sanitize_text_field($product->get_meta('_yoast_wpseo_opengraph-title')), 452 "description" => sanitize_text_field($product->get_meta('_yoast_wpseo_opengraph-description')), 453 "image" => sanitize_url($product->get_meta('_yoast_wpseo_opengraph-image')), 454 ), 455 "x" => array( 456 "title" => sanitize_text_field($product->get_meta('_yoast_wpseo_twitter-title')), 457 "description" => sanitize_text_field($product->get_meta('_yoast_wpseo_twitter-description')), 458 "image" => sanitize_url($product->get_meta('_yoast_wpseo_twitter-image')), 459 ) 460 ) 461 ); 462 463 foreach ($raw_attributes as $raw_attribute) { 464 if ($raw_attribute->is_taxonomy()) { 465 $terms = wp_get_post_terms($product->get_id(), $raw_attribute->get_name(), array('fields' => 'names')); 466 467 $attributes[] = array( 468 'name' => $raw_attribute->get_name(), 469 'value' => $terms, 470 ); 471 } else { 472 $attributes[] = array( 473 'name' => $raw_attribute->get_name(), 474 'value' => $raw_attribute->get_options(), 475 ); 476 } 477 } 436 478 437 479 foreach ($gallery_ids as $id) { … … 453 495 'product_id' => $product_id ? absint($product_id) : 0, 454 496 'name' => $name ? sanitize_text_field($name) : '', 497 'status' => $status ? sanitize_text_field($status) : '', 455 498 'slug' => $slug ? sanitize_title($slug) : '', 456 499 'description' => $description ? wp_kses_post($description) : '', … … 463 506 'upc' => $upc ? sanitize_text_field($upc) : '', 464 507 'weight' => $weight ? floatval($weight) : '', 465 'stock_status' => $stock_status ? sanitize_text_field($product->get_stock_status()) : '', 508 'stock_status' => $stock_status ? sanitize_text_field($stock_status) : '', 509 'manage_stock' => $manage_stock ? sanitize_text_field($manage_stock) : '', 466 510 'stock_quantity' => $stock_quantity ? absint($product->get_stock_quantity()) : 0, 467 511 'tags' => $tags ? array_map('sanitize_text_field', $tags) : array(), 468 512 'categories' => $categories ? array_map('sanitize_text_field', $categories) : array(), 513 'attributes' => $attributes, 469 514 'images' => $images ? array_map('esc_url_raw', $images) : array(), 470 515 'featured_image' => $featured_image ? esc_url_raw($featured_image) : '', 516 'yoast' => $yoast, 471 517 ); 472 518 … … 498 544 $weight = isset($fields['weight']) ? floatval($fields['weight']) : ''; 499 545 $stock_status = isset($fields['stock_status']) ? sanitize_text_field($fields['stock_status']) : ''; 500 $ stock_quantity = isset($fields['stock_quantity']) ? intval($fields['stock_quantity']) : '';546 $manage_stock = isset($fields['manage_stock']) ? sanitize_text_field($fields['manage_stock']) : ''; 501 547 $tags = isset($fields['tags']) && is_array($fields['tags']) ? array_map('sanitize_text_field', $fields['tags']) : array(); 502 548 $categories = isset($fields['categories']) && is_array($fields['categories']) ? array_map('sanitize_text_field', $fields['categories']) : array(); … … 537 583 return new WP_Error('product_not_found', 'Product not found', array('status' => 404)); 538 584 } 585 586 $stock_quantity = isset($fields['stock_quantity']) ? intval($fields['stock_quantity']) : false; 539 587 540 588 if ($slug) { … … 608 656 } 609 657 610 if ($stock_quantity >= 0) { 658 if ($manage_stock == 'true') { 659 $product->set_manage_stock(true); 660 } elseif ($manage_stock == 'false') { 661 $product->set_manage_stock(false); 662 } 663 664 if ($stock_quantity >= 0 && $stock_quantity !== false) { 611 665 $product->set_manage_stock(true); 612 666 $product->set_stock_quantity($stock_quantity); … … 721 775 $weight = isset($fields['weight']) ? floatval($fields['weight']) : ''; 722 776 $stock_status = isset($fields['stock_status']) ? sanitize_text_field($fields['stock_status']) : ''; 777 $manage_stock = isset($fields['manage_stock']) ? sanitize_text_field($fields['manage_stock']) : ''; 723 778 $stock_quantity = isset($fields['stock_quantity']) ? intval($fields['stock_quantity']) : ''; 724 779 $tags = isset($fields['tags']) && is_array($fields['tags']) ? array_map('sanitize_text_field', $fields['tags']) : array(); … … 807 862 } 808 863 809 if ($stock_quantity >= 0) { 864 if ($manage_stock == 'true') { 865 $product->set_manage_stock(true); 866 } elseif ($manage_stock == 'false') { 867 $product->set_manage_stock(false); 868 } 869 870 if ($stock_quantity >= 0 && $stock_quantity !== false) { 810 871 $product->set_manage_stock(true); 811 872 $product->set_stock_quantity($stock_quantity); … … 896 957 'product_id' => $product_id, 897 958 'message' => 'Product created successfully' 959 ), 200); 960 } 961 962 /** 963 * Get all product categories 964 * 965 * @param mixed $request 966 * @return void 967 */ 968 public function get_all_product_ids(WP_REST_Request $request) { 969 $this->options = get_option('content_api_options_polyplugins'); 970 971 $fields = $request->get_params(); 972 $limit = isset($fields['limit']) && is_numeric($fields['limit']) ? intval($fields['limit']) : -1; 973 $status = isset($fields['status']) ? sanitize_text_field($fields['status']) : ''; 974 975 $args = array( 976 'limit' => $limit, 977 'return' => 'ids' 978 ); 979 980 if ($status) { 981 $args['status'] = $status; 982 } 983 984 $products_ids = wc_get_products($args); 985 986 return new WP_REST_Response(array( 987 'success' => true, 988 'product_ids' => $products_ids 898 989 ), 200); 899 990 } … … 1859 1950 1860 1951 $formatted_attributes = array(); 1952 1861 1953 foreach ($attributes as $attribute) { 1862 $formatted_attributes[] = array( 1863 'name' => $attribute->get_name(), 1864 'value' => $attribute->get_options(), 1865 ); 1954 if ($attribute->is_taxonomy()) { 1955 $terms = wp_get_post_terms($product_id, $attribute->get_name(), array('fields' => 'names')); 1956 1957 $formatted_attributes[] = array( 1958 'name' => $attribute->get_name(), 1959 'value' => $terms, 1960 ); 1961 } else { 1962 $formatted_attributes[] = array( 1963 'name' => $attribute->get_name(), 1964 'value' => $attribute->get_options(), 1965 ); 1966 } 1866 1967 } 1867 1968 … … 1892 1993 $this->options = get_option('content_api_options_polyplugins'); 1893 1994 1894 $product_id = $request->get_param('product_id'); 1895 $sku = $request->get_param('sku'); 1995 $fields = $request->get_params(); 1996 $product_id = isset($fields['product_id']) && is_numeric($fields['product_id']) ? absint($fields['product_id']) : ''; 1997 $sku = isset($fields['sku']) ? sanitize_text_field($fields['sku']) : ''; 1896 1998 1897 1999 if (!$product_id && !$sku) { -
content-api/trunk/readme.txt
r3305371 r3306098 3 3 Tags: content api, rest api, content, creative, api 4 4 Tested up to: 6.8 5 Stable tag: 1.0. 85 Stable tag: 1.0.9 6 6 Requires PHP: 7.4 7 7 License: GPLv3 … … 85 85 == Changelog == 86 86 87 = 1.0.9 = 88 * Added: [Get All Product IDs](https://www.polyplugins.com/docs/content-api/api/get-all-product-ids/) endpoint 89 * Added: Attribute manage_stock to results of [Get Product](https://www.polyplugins.com/docs/content-api/api/get-product/) endpoint 90 * Added: Attribute attributes to results of [Get Product](https://www.polyplugins.com/docs/content-api/api/get-product/) endpoint 91 * Added: Attribute yoast to results of [Get Product](https://www.polyplugins.com/docs/content-api/api/get-product/) endpoint 92 * Added: Attribute manage_stock to [Update Product](https://www.polyplugins.com/docs/content-api/api/update-product/) endpoint 93 * Added: Attribute manage_stock to [Create Product](https://www.polyplugins.com/docs/content-api/api/create-product/) endpoint 94 * Added: Security enhancements 95 * Updated: [Update Product](https://www.polyplugins.com/docs/content-api/api/update-product/) endpoint to not require quantity 96 * Bugfix: Taxonomy handling for [Get Attributes](https://www.polyplugins.com/docs/content-api/api/get-attributes/) endpoint 97 87 98 = 1.0.8 = 88 99 * Updated: [Update Product](https://www.polyplugins.com/docs/content-api/api/update-product/) endpoint to handle updating sku when product id is provided.
Note: See TracChangeset
for help on using the changeset viewer.