diff --git a/includes/wc-functions.php b/includes/wc-functions.php index e244eddd8c..ac2f46ae3a 100755 --- a/includes/wc-functions.php +++ b/includes/wc-functions.php @@ -5,17 +5,18 @@ * * @access public * - * @param int $post_id + * @param int $post_id * @param array $data * * @return void + * @throws WC_Data_Exception */ -function dokan_process_product_meta( $post_id, $data = [] ) { +function dokan_process_product_meta( int $post_id, array $data = [] ) { if ( ! $post_id || ! $data ) { return; } - global $wpdb, $woocommerce, $woocommerce_errors; + global $woocommerce_errors; $product_type = empty( $data['product_type'] ) ? 'simple' : sanitize_text_field( $data['product_type'] ); @@ -37,11 +38,11 @@ function dokan_process_product_meta( $post_id, $data = [] ) { update_post_meta( $post_id, '_product_image_gallery', implode( ',', $attachment_ids ) ); } - // Check product visibility and purchaces note + // Check product visibility and purchases note $data['_visibility'] = isset( $data['_visibility'] ) ? sanitize_text_field( $data['_visibility'] ) : ''; $data['_purchase_note'] = isset( $data['_purchase_note'] ) ? sanitize_textarea_field( $data['_purchase_note'] ) : ''; - // Set visibiliy for WC 3.0.0+ + // Set visibility for WC 3.0.0+ $terms = []; switch ( $data['_visibility'] ) { @@ -65,7 +66,7 @@ function dokan_process_product_meta( $post_id, $data = [] ) { $terms[] = 'featured'; } - wp_set_post_terms( $post_id, $terms, 'product_visibility', false ); + wp_set_post_terms( $post_id, $terms, 'product_visibility' ); update_post_meta( $post_id, '_visibility', $data['_visibility'] ); // Update post meta @@ -118,7 +119,7 @@ function ( $value ) { $attribute_position = array_map( 'absint', $data['attribute_position'] ); $attribute_names_max_key = max( array_keys( $attribute_names ) ); - for ( $i = 0; $i <= $attribute_names_max_key; $i ++ ) { + for ( $i = 0; $i <= $attribute_names_max_key; $i++ ) { if ( empty( $attribute_names[ $i ] ) ) { continue; } @@ -351,10 +352,10 @@ function ( $value ) { $files = []; $file_names = isset( $data['_wc_file_names'] ) ? array_map( 'wc_clean', $data['_wc_file_names'] ) : []; - $file_urls = isset( $data['_wc_file_urls'] ) ? array_map( 'esc_url_raw', array_map( 'trim', $data['_wc_file_urls'] ) ) : []; + $file_urls = array_map( 'esc_url_raw', array_map( 'trim', $data['_wc_file_urls'] ) ); $file_url_size = count( $file_urls ); - for ( $i = 0; $i < $file_url_size; $i ++ ) { + for ( $i = 0; $i < $file_url_size; $i++ ) { if ( ! empty( $file_urls[ $i ] ) ) { $files[ md5( $file_urls[ $i ] ) ] = [ 'name' => $file_names[ $i ], @@ -395,7 +396,7 @@ function ( $value ) { $sku = trim( wp_unslash( $data['_sku'] ) ) !== '' ? sanitize_text_field( wp_unslash( $data['_sku'] ) ) : ''; try { $product->set_sku( $sku ); - } catch ( \WC_Data_Exception $e ) { + } catch ( WC_Data_Exception $e ) { $product->set_sku( $old_sku ); $woocommerce_errors[] = __( 'Product SKU must be unique', 'dokan-lite' ); } @@ -420,7 +421,7 @@ function ( $value ) { * * @return void */ -function dokan_process_product_file_download_paths( $product_id, $variation_id, $downloadable_files ) { +function dokan_process_product_file_download_paths( int $product_id, int $variation_id, array $downloadable_files ) { global $wpdb; if ( $variation_id ) { @@ -466,13 +467,13 @@ function dokan_process_product_file_download_paths( $product_id, $variation_id, } /** - * Get discount coupon total from a order + * Get discount coupon total from an order * * @param int $order_id * * @return int */ -function dokan_sub_order_get_total_coupon( $order_id ) { +function dokan_sub_order_get_total_coupon( int $order_id ): int { global $wpdb; $result = $wpdb->get_var( @@ -516,7 +517,7 @@ function dokan_seller_displayname( $display_name ) { * * @param int $per_page * - * @return \WP_Query + * @return WP_Query */ function dokan_get_featured_products( $per_page = 9, $seller_id = '', $page = 1 ) { $args = [ @@ -543,7 +544,7 @@ function dokan_get_featured_products( $per_page = 9, $seller_id = '', $page = 1 * * @param int $per_page * - * @return \WP_Query + * @return WP_Query */ function dokan_get_latest_products( $per_page = 9, $seller_id = '', $page = 1 ) { $args = [ @@ -568,7 +569,7 @@ function dokan_get_latest_products( $per_page = 9, $seller_id = '', $page = 1 ) * * @param int $per_page * - * @return \WP_Query + * @return WP_Query */ function dokan_get_best_selling_products( $per_page = 8, $seller_id = '', $page = 1, $hide_outofstock = false ) { $args = [ @@ -617,13 +618,13 @@ function check_more_seller_product_tab() { } /** - * Get top rated products + * Get top-rated products * * Shown on homepage * * @param int $per_page * - * @return \WP_Query + * @return WP_Query */ function dokan_get_top_rated_products( $per_page = 8, $seller_id = '', $page = 1 ) { $args = [ @@ -650,9 +651,9 @@ function dokan_get_top_rated_products( $per_page = 8, $seller_id = '', $page = 1 * @param int $paged * @param int $seller_id * - * @return \WP_Query + * @return WP_Query */ -function dokan_get_on_sale_products( $per_page = 10, $paged = 1, $seller_id = 0 ) { +function dokan_get_on_sale_products( int $per_page = 10, int $paged = 1, int $seller_id = 0 ): WP_Query { // Get products on sale $product_ids_on_sale = wc_get_product_ids_on_sale(); @@ -754,7 +755,7 @@ function dokan_get_readable_seller_rating( $seller_id ) { * A hacky and dirty way to do this from this action. Because there is no easy * way to do this by removing action hooks from WooCommerce. It would be easier * if they were from functions. Because they are added from classes, we can't - * remove those action hooks. Thats why we are doing this from the phpmailer_init action + * remove those action hooks. That's why we are doing this from the phpmailer_init action * by returning a fake phpmailer class. * * @param array $attr @@ -1064,16 +1065,15 @@ function dokan_set_more_from_seller_tab( $tabs ) { add_action( 'woocommerce_product_tabs', 'dokan_set_more_from_seller_tab', 10 ); /** - * Show more products from current seller + * Show more products from current seller * * @since 2.5 * @since DOKAN_LITE_SINCE added filter 'dokan_get_more_products_per_page' * - * @param int $seller_id - * @param int $posts_per_page + * @param int|string $seller_id + * @param int|string $posts_per_page * - * @global object $product - * @global object $post + * @return void */ function dokan_get_more_products_from_seller( $seller_id = 0, $posts_per_page = 6 ) { global $product, $post; @@ -1265,3 +1265,45 @@ function dokan_modify_vendor_order_counts( $counts ) { } add_filter( 'wp_count_posts', 'dokan_modify_vendor_order_counts', 10, 1 ); + + +/** + * @since DOKAN_SINCE + * + * @param boolean $is_purchasable + * @param object $product + * + * @return boolean + */ +function dokan_vendor_own_product_purchase_restriction( bool $is_purchasable, $product ): bool { + if ( false === $is_purchasable || dokan_is_product_author( $product->get_id() ) ) { + $is_purchasable = false; + } + return $is_purchasable; +} + +add_filter( 'woocommerce_is_purchasable', 'dokan_vendor_own_product_purchase_restriction', 10, 2 ); + +/** + * Restricts vendor from reviewing own product + * + * @since DOKAN_SINCE + * + * @param array $data + * @return array + */ +function dokan_vendor_product_review_restriction( array $data ): array { + global $product; + if ( ! is_user_logged_in() ) { + return $data; + } + if ( dokan_is_product_author( $product->get_id() ) ) { + $data['title_reply'] = __( 'Reviews cannot be posted for products that you own.', 'dokan-lite' ); + $data['comment_field'] = ''; + $data['fields'] = []; + $data['submit_field'] = ''; + $data['submit_button'] = ''; + } + return $data; +} +add_filter( 'woocommerce_product_review_comment_form_args', 'dokan_vendor_product_review_restriction' );