Plugin Directory

Changeset 2979509


Ignore:
Timestamp:
10/16/2023 10:59:41 AM (2 years ago)
Author:
setaryapp
Message:

Update to version 1.10.0 from GitHub

Location:
setary
Files:
14 edited
1 copied

Legend:

Unmodified
Added
Removed
  • setary/tags/1.10.0/README.md

    r2955128 r2979509  
    55Tested up to: 6.2.2
    66Requires PHP: 7.1
    7 Stable tag: 1.9.1
     7Stable tag: 1.10.0
    88License: MIT
    99License URI: https://opensource.org/licenses/MIT
     
    9191== Changelog ==
    9292
     93= v1.10.0 (2023-10-16) =
     94* [new] Enable product type editing
     95
    9396= v1.9.1 (2023-08-17) =
    9497* [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  
    7070        foreach($request->get_json_params() as $action) {
    7171            $r = clone $request;
     72
    7273            $r->set_route('/wc/' . $action['endpoint']);
    7374            $r->set_method(strtoupper($action['method']));
  • setary/tags/1.10.0/inc/class-meta-attributes.php

    r2951032 r2979509  
    9696            'meta'       => $data_store->filter_raw_meta_keys( $results ),
    9797            'attributes' => $this->get_all_woocommerce_attribute_names_and_labels(),
     98            'types'      => $this->get_product_types(),
    9899        ] );
    99100
     
    198199        return $all_attributes;
    199200    }
     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    }
    200219}
  • setary/tags/1.10.0/inc/class-product-controller.php

    r2951032 r2979509  
    141141     * Filter product before insert to database.
    142142     *
    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.
    145145     * @param bool            $creating If is creating a new object.
    146146     *
    147      * @return WC_Product|WP_Error
     147     * @return \WC_Product|\WP_Error
    148148     */
    149149    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
    150155        $format_ids = array(
    151156            'formatted_upsell_ids',
     
    229234
    230235        // Check if `type` is being saved
    231         if ( isset( $request['type'] ) ) {
     236        if ( isset( $request['product_type'] ) ) {
    232237            // 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() ) ) ) {
    234239                return new \WP_Error( 'invalid_product_type', __( 'The product type does not exist.', 'setary' ), array( 'status' => 400 ) );
    235240            }
     
    253258            'height',
    254259            'length',
     260            'parent_id',
    255261        ];
    256262
     
    354360        return $term_id;
    355361    }
     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    }
    356442}
  • setary/tags/1.10.0/inc/class-products-with-variations.php

    r2955128 r2979509  
    296296        }
    297297
    298         $required_fields = [ 'id', 'parent_id', 'name' ];
     298        $required_fields = [ 'id', 'parent_id', 'name', 'product_type' ];
    299299
    300300        // Merge the required fields with the requested fields.
     
    321321            'formatted_tags' => 'tags',
    322322            'attribute_*' => 'attributes',
     323            'product_type' => 'type',
    323324        ];
    324325
     
    424425
    425426        $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'];
    426429
    427430        if ( isset( $item['manage_stock'] ) ) {
    428431            // If manage stock is "parent", then really it means "No".
    429432            $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'] );
    434433        }
    435434
     
    604603                $field = $numeric_post_fields[ $key ];
    605604                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 ) {
    607606                if( in_array( 'variation', $filter['query'], true ) ) {
    608607                    $query_args['post_type'] = [ 'product_variation' ];
  • setary/tags/1.10.0/inc/class-utils.php

    r2940762 r2979509  
    2020     */
    2121    public static function get_product_type( $product_id ) {
     22        // Default new products with zero id.
     23        if ( ! $product_id ) {
     24            return 'simple';
     25        }
     26
    2227        $post_type = get_post_type( $product_id );
    2328
  • setary/tags/1.10.0/setary.php

    r2955128 r2979509  
    1111 * Author URI:           https://setary.com/
    1212 *
    13  * Version:              1.9.1
     13 * Version:              1.10.0
    1414 * WC requires at least: 6.0.0
    1515 * WC tested up to:      8.0.1
     
    2323define( 'SETARY_PATH', \plugin_dir_path( __FILE__ ) );
    2424define( 'SETARY_URL', \plugins_url( '/', __FILE__ ) );
    25 define( 'SETARY_VERSION', '1.9.1' );
     25define( 'SETARY_VERSION', '1.10.0' );
    2626define( 'SETARY_SITE_URL', 'https://setary.com/' );
    2727define( 'SETARY_APP_URL', 'https://setary.com/app/' );
  • setary/trunk/README.md

    r2955128 r2979509  
    55Tested up to: 6.2.2
    66Requires PHP: 7.1
    7 Stable tag: 1.9.1
     7Stable tag: 1.10.0
    88License: MIT
    99License URI: https://opensource.org/licenses/MIT
     
    9191== Changelog ==
    9292
     93= v1.10.0 (2023-10-16) =
     94* [new] Enable product type editing
     95
    9396= v1.9.1 (2023-08-17) =
    9497* [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  
    7070        foreach($request->get_json_params() as $action) {
    7171            $r = clone $request;
     72
    7273            $r->set_route('/wc/' . $action['endpoint']);
    7374            $r->set_method(strtoupper($action['method']));
  • setary/trunk/inc/class-meta-attributes.php

    r2951032 r2979509  
    9696            'meta'       => $data_store->filter_raw_meta_keys( $results ),
    9797            'attributes' => $this->get_all_woocommerce_attribute_names_and_labels(),
     98            'types'      => $this->get_product_types(),
    9899        ] );
    99100
     
    198199        return $all_attributes;
    199200    }
     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    }
    200219}
  • setary/trunk/inc/class-product-controller.php

    r2951032 r2979509  
    141141     * Filter product before insert to database.
    142142     *
    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.
    145145     * @param bool            $creating If is creating a new object.
    146146     *
    147      * @return WC_Product|WP_Error
     147     * @return \WC_Product|\WP_Error
    148148     */
    149149    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
    150155        $format_ids = array(
    151156            'formatted_upsell_ids',
     
    229234
    230235        // Check if `type` is being saved
    231         if ( isset( $request['type'] ) ) {
     236        if ( isset( $request['product_type'] ) ) {
    232237            // 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() ) ) ) {
    234239                return new \WP_Error( 'invalid_product_type', __( 'The product type does not exist.', 'setary' ), array( 'status' => 400 ) );
    235240            }
     
    253258            'height',
    254259            'length',
     260            'parent_id',
    255261        ];
    256262
     
    354360        return $term_id;
    355361    }
     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    }
    356442}
  • setary/trunk/inc/class-products-with-variations.php

    r2955128 r2979509  
    296296        }
    297297
    298         $required_fields = [ 'id', 'parent_id', 'name' ];
     298        $required_fields = [ 'id', 'parent_id', 'name', 'product_type' ];
    299299
    300300        // Merge the required fields with the requested fields.
     
    321321            'formatted_tags' => 'tags',
    322322            'attribute_*' => 'attributes',
     323            'product_type' => 'type',
    323324        ];
    324325
     
    424425
    425426        $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'];
    426429
    427430        if ( isset( $item['manage_stock'] ) ) {
    428431            // If manage stock is "parent", then really it means "No".
    429432            $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'] );
    434433        }
    435434
     
    604603                $field = $numeric_post_fields[ $key ];
    605604                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 ) {
    607606                if( in_array( 'variation', $filter['query'], true ) ) {
    608607                    $query_args['post_type'] = [ 'product_variation' ];
  • setary/trunk/inc/class-utils.php

    r2940762 r2979509  
    2020     */
    2121    public static function get_product_type( $product_id ) {
     22        // Default new products with zero id.
     23        if ( ! $product_id ) {
     24            return 'simple';
     25        }
     26
    2227        $post_type = get_post_type( $product_id );
    2328
  • setary/trunk/setary.php

    r2955128 r2979509  
    1111 * Author URI:           https://setary.com/
    1212 *
    13  * Version:              1.9.1
     13 * Version:              1.10.0
    1414 * WC requires at least: 6.0.0
    1515 * WC tested up to:      8.0.1
     
    2323define( 'SETARY_PATH', \plugin_dir_path( __FILE__ ) );
    2424define( 'SETARY_URL', \plugins_url( '/', __FILE__ ) );
    25 define( 'SETARY_VERSION', '1.9.1' );
     25define( 'SETARY_VERSION', '1.10.0' );
    2626define( 'SETARY_SITE_URL', 'https://setary.com/' );
    2727define( 'SETARY_APP_URL', 'https://setary.com/app/' );
Note: See TracChangeset for help on using the changeset viewer.