get_classname_from_product_type() публичный статический Метод

Create a WC coding standards compliant class name e.g. WC_Product_Type_Class instead of WC_Product_type-class.
public static get_classname_from_product_type ( string $product_type ) : string | false
$product_type string
Результат string | false
 /**
  * Create a new product
  *
  * @since 2.2
  * @param array $data posted data
  * @return array
  */
 public function create_product($data)
 {
     $id = 0;
     try {
         if (!isset($data['product'])) {
             throw new WC_API_Exception('woocommerce_api_missing_product_data', sprintf(__('No %1$s data specified to create %1$s', 'woocommerce'), 'product'), 400);
         }
         $data = $data['product'];
         // Check permissions
         if (!current_user_can('publish_products')) {
             throw new WC_API_Exception('woocommerce_api_user_cannot_create_product', __('You do not have permission to create products', 'woocommerce'), 401);
         }
         $data = apply_filters('woocommerce_api_create_product_data', $data, $this);
         // Check if product title is specified
         if (!isset($data['title'])) {
             throw new WC_API_Exception('woocommerce_api_missing_product_title', sprintf(__('Missing parameter %s', 'woocommerce'), 'title'), 400);
         }
         // Check product type
         if (!isset($data['type'])) {
             $data['type'] = 'simple';
         }
         // Set visible visibility when not sent
         if (!isset($data['catalog_visibility'])) {
             $data['catalog_visibility'] = 'visible';
         }
         // Validate the product type
         if (!in_array(wc_clean($data['type']), array_keys(wc_get_product_types()))) {
             throw new WC_API_Exception('woocommerce_api_invalid_product_type', sprintf(__('Invalid product type - the product type must be any of these: %s', 'woocommerce'), implode(', ', array_keys(wc_get_product_types()))), 400);
         }
         // Enable description html tags.
         $post_content = isset($data['description']) ? wc_clean($data['description']) : '';
         if ($post_content && isset($data['enable_html_description']) && true === $data['enable_html_description']) {
             $post_content = $data['description'];
         }
         // Enable short description html tags.
         $post_excerpt = isset($data['short_description']) ? wc_clean($data['short_description']) : '';
         if ($post_excerpt && isset($data['enable_html_short_description']) && true === $data['enable_html_short_description']) {
             $post_excerpt = $data['short_description'];
         }
         $classname = WC_Product_Factory::get_classname_from_product_type($data['type']);
         if (!class_exists($classname)) {
             $classname = 'WC_Product_Simple';
         }
         $product = new $classname();
         $product->set_name(wc_clean($data['title']));
         $product->set_status(isset($data['status']) ? wc_clean($data['status']) : 'publish');
         $product->set_short_description(isset($data['short_description']) ? $post_excerpt : '');
         $product->set_description(isset($data['description']) ? $post_content : '');
         // Attempts to create the new product.
         $product->create();
         $id = $product->get_id();
         // Checks for an error in the product creation
         if (0 >= $id) {
             throw new WC_API_Exception('woocommerce_api_cannot_create_product', $id->get_error_message(), 400);
         }
         // Check for featured/gallery images, upload it and set it
         if (isset($data['images'])) {
             $product = $this->save_product_images($product, $data['images']);
         }
         // Save product meta fields
         $product = $this->save_product_meta($product, $data);
         $product->save();
         // Save variations
         if (isset($data['type']) && 'variable' == $data['type'] && isset($data['variations']) && is_array($data['variations'])) {
             $this->save_variations($product, $data);
         }
         do_action('woocommerce_api_create_product', $id, $data);
         // Clear cache/transients
         wc_delete_product_transients($id);
         $this->server->send_status(201);
         return $this->get_product($id);
     } catch (WC_Data_Exception $e) {
         $this->clear_product($id);
         return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode()));
     } catch (WC_API_Exception $e) {
         $this->clear_product($id);
         return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode()));
     }
 }
 /**
  * Prepare a single product for create or update.
  *
  * @param WP_REST_Request $request Request object.
  * @return WP_Error|stdClass $data Post object.
  */
 protected function prepare_item_for_database($request)
 {
     if (isset($request['id'])) {
         $product = wc_get_product(absint($request['id']));
     } else {
         $classname = WC_Product_Factory::get_classname_from_product_type($request['type']);
         if (!class_exists($classname)) {
             $classname = 'WC_Product_Simple';
         }
         $product = new $classname();
     }
     // Post title.
     if (isset($request['name'])) {
         $product->set_name(wp_filter_post_kses($request['name']));
     }
     // Post content.
     if (isset($request['description'])) {
         $product->set_description(wp_filter_post_kses($request['description']));
     }
     // Post excerpt.
     if (isset($request['short_description'])) {
         $product->set_short_description(wp_filter_post_kses($request['short_description']));
     }
     // Post status.
     if (isset($request['status'])) {
         $product->set_status(get_post_status_object($request['status']) ? $request['status'] : 'draft');
     }
     // Post slug.
     if (isset($request['slug'])) {
         $product->set_slug($request['slug']);
     }
     // Menu order.
     if (isset($request['menu_order'])) {
         $product->set_menu_order($request['menu_order']);
     }
     // Comment status.
     if (!empty($request['reviews_allowed'])) {
         $product->set_reviews_allowed($request['reviews_allowed']);
     }
     /**
      * Filter the query_vars used in `get_items` for the constructed query.
      *
      * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
      * prepared for insertion.
      *
      * @param WC_Product       $product An object representing a single item prepared
      *                                       for inserting or updating the database.
      * @param WP_REST_Request $request       Request object.
      */
     return apply_filters("woocommerce_rest_pre_insert_{$this->post_type}", $product, $request);
 }
 /**
  * Save meta box data.
  */
 public static function save($post_id, $post)
 {
     // Process product type first so we have the correct class to run setters.
     $product_type = empty($_POST['product-type']) ? 'simple' : sanitize_title(stripslashes($_POST['product-type']));
     $classname = WC_Product_Factory::get_classname_from_product_type($product_type);
     if (!class_exists($classname)) {
         $classname = 'WC_Product_Simple';
     }
     $product = new $classname($post_id);
     $attributes = self::prepare_attributes();
     $errors = $product->set_props(array('sku' => isset($_POST['_sku']) ? wc_clean($_POST['_sku']) : null, 'purchase_note' => wp_kses_post(stripslashes($_POST['_purchase_note'])), 'downloadable' => isset($_POST['_downloadable']), 'virtual' => isset($_POST['_virtual']), 'featured' => isset($_POST['_featured']), 'catalog_visibility' => wc_clean($_POST['_visibility']), 'tax_status' => wc_clean($_POST['_tax_status']), 'tax_class' => wc_clean($_POST['_tax_class']), 'weight' => wc_clean($_POST['_weight']), 'length' => wc_clean($_POST['_length']), 'width' => wc_clean($_POST['_width']), 'height' => wc_clean($_POST['_height']), 'shipping_class_id' => absint($_POST['product_shipping_class']), 'sold_individually' => !empty($_POST['_sold_individually']), 'upsell_ids' => array_map('intval', explode(',', $_POST['upsell_ids'])), 'cross_sell_ids' => array_map('intval', explode(',', $_POST['crosssell_ids'])), 'regular_price' => wc_clean($_POST['_regular_price']), 'sale_price' => wc_clean($_POST['_sale_price']), 'date_on_sale_from' => wc_clean($_POST['_sale_price_dates_from']), 'date_on_sale_to' => wc_clean($_POST['_sale_price_dates_to']), 'manage_stock' => !empty($_POST['_manage_stock']), 'backorders' => wc_clean($_POST['_backorders']), 'stock_status' => wc_clean($_POST['_stock_status']), 'stock_quantity' => wc_stock_amount($_POST['_stock']), 'download_limit' => '' === $_POST['_download_limit'] ? '' : absint($_POST['_download_limit']), 'download_expiry' => '' === $_POST['_download_expiry'] ? '' : absint($_POST['_download_expiry']), 'downloads' => self::prepare_downloads(isset($_POST['_wc_file_names']) ? $_POST['_wc_file_names'] : array(), isset($_POST['_wc_file_urls']) ? $_POST['_wc_file_urls'] : array(), isset($_POST['_wc_file_hashes']) ? $_POST['_wc_file_hashes'] : array()), 'product_url' => esc_url_raw($_POST['_product_url']), 'button_text' => wc_clean($_POST['_button_text']), 'children' => 'grouped' === $product_type ? self::prepare_children() : null, 'reviews_allowed' => !empty($_POST['_reviews_allowed']), 'attributes' => $attributes, 'default_attributes' => self::prepare_set_attributes($attributes, 'default_attribute_')));
     if (is_wp_error($errors)) {
         WC_Admin_Meta_Boxes::add_error($errors->get_error_message());
     }
     $product->save();
     do_action('woocommerce_process_product_meta_' . $product_type, $post_id);
 }