Changeset 2979509
- Timestamp:
- 10/16/2023 10:59:41 AM (2 years ago)
- Location:
- setary
- Files:
-
- 14 edited
- 1 copied
-
tags/1.10.0 (copied) (copied from setary/trunk)
-
tags/1.10.0/README.md (modified) (2 diffs)
-
tags/1.10.0/inc/class-batch.php (modified) (1 diff)
-
tags/1.10.0/inc/class-meta-attributes.php (modified) (2 diffs)
-
tags/1.10.0/inc/class-product-controller.php (modified) (4 diffs)
-
tags/1.10.0/inc/class-products-with-variations.php (modified) (4 diffs)
-
tags/1.10.0/inc/class-utils.php (modified) (1 diff)
-
tags/1.10.0/setary.php (modified) (2 diffs)
-
trunk/README.md (modified) (2 diffs)
-
trunk/inc/class-batch.php (modified) (1 diff)
-
trunk/inc/class-meta-attributes.php (modified) (2 diffs)
-
trunk/inc/class-product-controller.php (modified) (4 diffs)
-
trunk/inc/class-products-with-variations.php (modified) (4 diffs)
-
trunk/inc/class-utils.php (modified) (1 diff)
-
trunk/setary.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
setary/tags/1.10.0/README.md
r2955128 r2979509 5 5 Tested up to: 6.2.2 6 6 Requires PHP: 7.1 7 Stable tag: 1. 9.17 Stable tag: 1.10.0 8 8 License: MIT 9 9 License URI: https://opensource.org/licenses/MIT … … 91 91 == Changelog == 92 92 93 = v1.10.0 (2023-10-16) = 94 * [new] Enable product type editing 95 93 96 = v1.9.1 (2023-08-17) = 94 97 * [update] Minor update which allows us to add new core fields via the app without plugin updates -
setary/tags/1.10.0/inc/class-batch.php
r2903444 r2979509 70 70 foreach($request->get_json_params() as $action) { 71 71 $r = clone $request; 72 72 73 $r->set_route('/wc/' . $action['endpoint']); 73 74 $r->set_method(strtoupper($action['method'])); -
setary/tags/1.10.0/inc/class-meta-attributes.php
r2951032 r2979509 96 96 'meta' => $data_store->filter_raw_meta_keys( $results ), 97 97 'attributes' => $this->get_all_woocommerce_attribute_names_and_labels(), 98 'types' => $this->get_product_types(), 98 99 ] ); 99 100 … … 198 199 return $all_attributes; 199 200 } 201 202 /** 203 * Retrieves a unique array of all available product types for WooCommerce products. 204 * 205 * @return array An array of unique product types. 206 */ 207 function get_product_types() { 208 $types = wc_get_product_types(); 209 210 $types['variation'] = __( 'Variation', 'setary' ); 211 212 // Loop all types and remove " product" or " Product" from the end. 213 foreach ( $types as $key => $type ) { 214 $types[ $key ] = preg_replace( '/\s+product$/i', '', $type ); 215 } 216 217 return $types; 218 } 200 219 } -
setary/tags/1.10.0/inc/class-product-controller.php
r2951032 r2979509 141 141 * Filter product before insert to database. 142 142 * 143 * @param WC_Product $product Object object.144 * @param WP_REST_Request $request Request object.143 * @param \WC_Product $product Object object. 144 * @param \WP_REST_Request $request Request object. 145 145 * @param bool $creating If is creating a new object. 146 146 * 147 * @return WC_Product|WP_Error147 * @return \WC_Product|\WP_Error 148 148 */ 149 149 public function pre_insert_product_object( $product, $request, $creating ) { 150 // Change variation to product, or product to variation. 151 if ( ! empty( $request['product_type'] ) ) { 152 $product = $this->change_type( $product, $request['product_type'] ); 153 } 154 150 155 $format_ids = array( 151 156 'formatted_upsell_ids', … … 229 234 230 235 // Check if `type` is being saved 231 if ( isset( $request[' type'] ) ) {236 if ( isset( $request['product_type'] ) ) { 232 237 // Ensure the new type exists 233 if ( ! in_array( $request['type'], array_keys( wc_get_product_types() ) ) ) {238 if ( 'variation' !== $request['product_type'] && ! in_array( $request['product_type'], array_keys( wc_get_product_types() ) ) ) { 234 239 return new \WP_Error( 'invalid_product_type', __( 'The product type does not exist.', 'setary' ), array( 'status' => 400 ) ); 235 240 } … … 253 258 'height', 254 259 'length', 260 'parent_id', 255 261 ]; 256 262 … … 354 360 return $term_id; 355 361 } 362 363 /** 364 * Change variation to product, or product to variation. 365 * 366 * @param \WC_Product_Variation|\WC_Product $product Product. 367 * 368 * @return int|false 369 */ 370 public function transition_product_variation( $product ) { 371 global $wpdb; 372 373 $product_id = $product->get_id(); 374 $new_type = 'variation' === $product->get_type() ? 'product' : 'product_variation'; 375 376 $args = array( 377 'post_type' => $new_type, 378 ); 379 380 $format = array( '%s' ); 381 382 if ( 'product' === $new_type ) { 383 $args['post_parent'] = 0; 384 $format[] = '%d'; 385 } 386 387 // Update the post type 388 $update = $wpdb->update( 389 $wpdb->posts, 390 $args, 391 array( 'ID' => $product_id ), 392 $format, 393 array( '%d' ) 394 ); 395 396 wp_cache_delete( $product_id, 'posts' ); 397 398 if ( is_wp_error( $update ) ) { 399 return false; 400 } 401 402 return $update; 403 } 404 405 /** 406 * Change product type. 407 * 408 * @param WC_Product $product Product. 409 * @param string $new_type product type. 410 * 411 * @return mixed 412 */ 413 public function change_type( $product, $new_type = 'simple' ) { 414 $current_type = $product->get_type(); 415 $current_type_is_variation = 'variation' === $current_type; 416 $new_type_is_variation = 'variation' === $new_type; 417 418 $should_transition = ( $current_type_is_variation && ! $new_type_is_variation ) || ( ! $current_type_is_variation && $new_type_is_variation ); 419 420 if ( $should_transition ) { 421 $this->transition_product_variation( $product ); 422 } 423 424 // If current type was not a variation, remove the type. Variations have no product type. 425 if ( ! $current_type_is_variation ) { 426 wp_remove_object_terms( $product->get_id(), $current_type, 'product_type' ); 427 } 428 429 // If new type is not a variation, add it as a term. Variations have no product type. 430 if ( ! $new_type_is_variation ) { 431 wp_set_object_terms( $product->get_id(), $new_type, 'product_type' ); 432 } 433 434 $classname = \WC_Product_Factory::get_classname_from_product_type( $new_type ); 435 436 if ( ! $classname || ! class_exists( $classname ) ) { 437 return $product; 438 } 439 440 return new $classname( $product->get_id() ); 441 } 356 442 } -
setary/tags/1.10.0/inc/class-products-with-variations.php
r2955128 r2979509 296 296 } 297 297 298 $required_fields = [ 'id', 'parent_id', 'name' ];298 $required_fields = [ 'id', 'parent_id', 'name', 'product_type' ]; 299 299 300 300 // Merge the required fields with the requested fields. … … 321 321 'formatted_tags' => 'tags', 322 322 'attribute_*' => 'attributes', 323 'product_type' => 'type', 323 324 ]; 324 325 … … 424 425 425 426 $item['tax_class'] = empty( $item['tax_class'] ) ? 'standard' : $item['tax_class']; 427 $item['product_type'] = Utils::get_product_type( $item['id'] ); 428 $item['type'] = $item['product_type']; 426 429 427 430 if ( isset( $item['manage_stock'] ) ) { 428 431 // If manage stock is "parent", then really it means "No". 429 432 $item['manage_stock'] = $item['manage_stock'] && 'parent' !== $item['manage_stock']; 430 }431 432 if ( isset( $item['type'] ) ) {433 $item['type'] = Utils::get_product_type( $item['id'] );434 433 } 435 434 … … 604 603 $field = $numeric_post_fields[ $key ]; 605 604 add_filter( 'posts_where', [ new FilterByBetween(" AND {$wpdb->prefix}posts.{$field} BETWEEN %d AND %d", array( floatval($filter['from']), floatval($filter['to']) )), 'filter' ]); 606 } else if( ' type' === $key ) {605 } else if( 'product_type' === $key ) { 607 606 if( in_array( 'variation', $filter['query'], true ) ) { 608 607 $query_args['post_type'] = [ 'product_variation' ]; -
setary/tags/1.10.0/inc/class-utils.php
r2940762 r2979509 20 20 */ 21 21 public static function get_product_type( $product_id ) { 22 // Default new products with zero id. 23 if ( ! $product_id ) { 24 return 'simple'; 25 } 26 22 27 $post_type = get_post_type( $product_id ); 23 28 -
setary/tags/1.10.0/setary.php
r2955128 r2979509 11 11 * Author URI: https://setary.com/ 12 12 * 13 * Version: 1. 9.113 * Version: 1.10.0 14 14 * WC requires at least: 6.0.0 15 15 * WC tested up to: 8.0.1 … … 23 23 define( 'SETARY_PATH', \plugin_dir_path( __FILE__ ) ); 24 24 define( 'SETARY_URL', \plugins_url( '/', __FILE__ ) ); 25 define( 'SETARY_VERSION', '1. 9.1' );25 define( 'SETARY_VERSION', '1.10.0' ); 26 26 define( 'SETARY_SITE_URL', 'https://setary.com/' ); 27 27 define( 'SETARY_APP_URL', 'https://setary.com/app/' ); -
setary/trunk/README.md
r2955128 r2979509 5 5 Tested up to: 6.2.2 6 6 Requires PHP: 7.1 7 Stable tag: 1. 9.17 Stable tag: 1.10.0 8 8 License: MIT 9 9 License URI: https://opensource.org/licenses/MIT … … 91 91 == Changelog == 92 92 93 = v1.10.0 (2023-10-16) = 94 * [new] Enable product type editing 95 93 96 = v1.9.1 (2023-08-17) = 94 97 * [update] Minor update which allows us to add new core fields via the app without plugin updates -
setary/trunk/inc/class-batch.php
r2903444 r2979509 70 70 foreach($request->get_json_params() as $action) { 71 71 $r = clone $request; 72 72 73 $r->set_route('/wc/' . $action['endpoint']); 73 74 $r->set_method(strtoupper($action['method'])); -
setary/trunk/inc/class-meta-attributes.php
r2951032 r2979509 96 96 'meta' => $data_store->filter_raw_meta_keys( $results ), 97 97 'attributes' => $this->get_all_woocommerce_attribute_names_and_labels(), 98 'types' => $this->get_product_types(), 98 99 ] ); 99 100 … … 198 199 return $all_attributes; 199 200 } 201 202 /** 203 * Retrieves a unique array of all available product types for WooCommerce products. 204 * 205 * @return array An array of unique product types. 206 */ 207 function get_product_types() { 208 $types = wc_get_product_types(); 209 210 $types['variation'] = __( 'Variation', 'setary' ); 211 212 // Loop all types and remove " product" or " Product" from the end. 213 foreach ( $types as $key => $type ) { 214 $types[ $key ] = preg_replace( '/\s+product$/i', '', $type ); 215 } 216 217 return $types; 218 } 200 219 } -
setary/trunk/inc/class-product-controller.php
r2951032 r2979509 141 141 * Filter product before insert to database. 142 142 * 143 * @param WC_Product $product Object object.144 * @param WP_REST_Request $request Request object.143 * @param \WC_Product $product Object object. 144 * @param \WP_REST_Request $request Request object. 145 145 * @param bool $creating If is creating a new object. 146 146 * 147 * @return WC_Product|WP_Error147 * @return \WC_Product|\WP_Error 148 148 */ 149 149 public function pre_insert_product_object( $product, $request, $creating ) { 150 // Change variation to product, or product to variation. 151 if ( ! empty( $request['product_type'] ) ) { 152 $product = $this->change_type( $product, $request['product_type'] ); 153 } 154 150 155 $format_ids = array( 151 156 'formatted_upsell_ids', … … 229 234 230 235 // Check if `type` is being saved 231 if ( isset( $request[' type'] ) ) {236 if ( isset( $request['product_type'] ) ) { 232 237 // Ensure the new type exists 233 if ( ! in_array( $request['type'], array_keys( wc_get_product_types() ) ) ) {238 if ( 'variation' !== $request['product_type'] && ! in_array( $request['product_type'], array_keys( wc_get_product_types() ) ) ) { 234 239 return new \WP_Error( 'invalid_product_type', __( 'The product type does not exist.', 'setary' ), array( 'status' => 400 ) ); 235 240 } … … 253 258 'height', 254 259 'length', 260 'parent_id', 255 261 ]; 256 262 … … 354 360 return $term_id; 355 361 } 362 363 /** 364 * Change variation to product, or product to variation. 365 * 366 * @param \WC_Product_Variation|\WC_Product $product Product. 367 * 368 * @return int|false 369 */ 370 public function transition_product_variation( $product ) { 371 global $wpdb; 372 373 $product_id = $product->get_id(); 374 $new_type = 'variation' === $product->get_type() ? 'product' : 'product_variation'; 375 376 $args = array( 377 'post_type' => $new_type, 378 ); 379 380 $format = array( '%s' ); 381 382 if ( 'product' === $new_type ) { 383 $args['post_parent'] = 0; 384 $format[] = '%d'; 385 } 386 387 // Update the post type 388 $update = $wpdb->update( 389 $wpdb->posts, 390 $args, 391 array( 'ID' => $product_id ), 392 $format, 393 array( '%d' ) 394 ); 395 396 wp_cache_delete( $product_id, 'posts' ); 397 398 if ( is_wp_error( $update ) ) { 399 return false; 400 } 401 402 return $update; 403 } 404 405 /** 406 * Change product type. 407 * 408 * @param WC_Product $product Product. 409 * @param string $new_type product type. 410 * 411 * @return mixed 412 */ 413 public function change_type( $product, $new_type = 'simple' ) { 414 $current_type = $product->get_type(); 415 $current_type_is_variation = 'variation' === $current_type; 416 $new_type_is_variation = 'variation' === $new_type; 417 418 $should_transition = ( $current_type_is_variation && ! $new_type_is_variation ) || ( ! $current_type_is_variation && $new_type_is_variation ); 419 420 if ( $should_transition ) { 421 $this->transition_product_variation( $product ); 422 } 423 424 // If current type was not a variation, remove the type. Variations have no product type. 425 if ( ! $current_type_is_variation ) { 426 wp_remove_object_terms( $product->get_id(), $current_type, 'product_type' ); 427 } 428 429 // If new type is not a variation, add it as a term. Variations have no product type. 430 if ( ! $new_type_is_variation ) { 431 wp_set_object_terms( $product->get_id(), $new_type, 'product_type' ); 432 } 433 434 $classname = \WC_Product_Factory::get_classname_from_product_type( $new_type ); 435 436 if ( ! $classname || ! class_exists( $classname ) ) { 437 return $product; 438 } 439 440 return new $classname( $product->get_id() ); 441 } 356 442 } -
setary/trunk/inc/class-products-with-variations.php
r2955128 r2979509 296 296 } 297 297 298 $required_fields = [ 'id', 'parent_id', 'name' ];298 $required_fields = [ 'id', 'parent_id', 'name', 'product_type' ]; 299 299 300 300 // Merge the required fields with the requested fields. … … 321 321 'formatted_tags' => 'tags', 322 322 'attribute_*' => 'attributes', 323 'product_type' => 'type', 323 324 ]; 324 325 … … 424 425 425 426 $item['tax_class'] = empty( $item['tax_class'] ) ? 'standard' : $item['tax_class']; 427 $item['product_type'] = Utils::get_product_type( $item['id'] ); 428 $item['type'] = $item['product_type']; 426 429 427 430 if ( isset( $item['manage_stock'] ) ) { 428 431 // If manage stock is "parent", then really it means "No". 429 432 $item['manage_stock'] = $item['manage_stock'] && 'parent' !== $item['manage_stock']; 430 }431 432 if ( isset( $item['type'] ) ) {433 $item['type'] = Utils::get_product_type( $item['id'] );434 433 } 435 434 … … 604 603 $field = $numeric_post_fields[ $key ]; 605 604 add_filter( 'posts_where', [ new FilterByBetween(" AND {$wpdb->prefix}posts.{$field} BETWEEN %d AND %d", array( floatval($filter['from']), floatval($filter['to']) )), 'filter' ]); 606 } else if( ' type' === $key ) {605 } else if( 'product_type' === $key ) { 607 606 if( in_array( 'variation', $filter['query'], true ) ) { 608 607 $query_args['post_type'] = [ 'product_variation' ]; -
setary/trunk/inc/class-utils.php
r2940762 r2979509 20 20 */ 21 21 public static function get_product_type( $product_id ) { 22 // Default new products with zero id. 23 if ( ! $product_id ) { 24 return 'simple'; 25 } 26 22 27 $post_type = get_post_type( $product_id ); 23 28 -
setary/trunk/setary.php
r2955128 r2979509 11 11 * Author URI: https://setary.com/ 12 12 * 13 * Version: 1. 9.113 * Version: 1.10.0 14 14 * WC requires at least: 6.0.0 15 15 * WC tested up to: 8.0.1 … … 23 23 define( 'SETARY_PATH', \plugin_dir_path( __FILE__ ) ); 24 24 define( 'SETARY_URL', \plugins_url( '/', __FILE__ ) ); 25 define( 'SETARY_VERSION', '1. 9.1' );25 define( 'SETARY_VERSION', '1.10.0' ); 26 26 define( 'SETARY_SITE_URL', 'https://setary.com/' ); 27 27 define( 'SETARY_APP_URL', 'https://setary.com/app/' );
Note: See TracChangeset
for help on using the changeset viewer.