batch, 'reviews', $index ); $plan = Plan::review( $random ); $orders = $ctx->pool( 'orders' ); $product = 0; $user_id = 0; $name = ''; $email = ''; $after = $ctx->from; $ok = false; if ( $orders ) { $order = wc_get_order( $orders[ $random->int( 0, count( $orders ) - 1 ) ] ); if ( $order instanceof \WC_Order ) { $items = array_values( $order->get_items() ); $item = $items ? $items[ $random->int( 0, count( $items ) - 1 ) ] : null; if ( $item instanceof \WC_Order_Item_Product && $item->get_product_id() ) { $product = (int) $item->get_product_id(); $user_id = (int) $order->get_customer_id(); $name = trim( $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() ); $email = (string) $order->get_billing_email(); $created = $order->get_date_created(); $after = $created ? $created->getTimestamp() : $after; $ok = $order->has_status( array( 'processing', 'completed' ) ); } } } if ( ! $product ) { $products = $ctx->pool( 'products' ); if ( ! $products ) { return array( 'id' => 0, 'product' => 0, ); } $product = (int) $products[ $random->int( 0, count( $products ) - 1 ) ]; $customers = $ctx->pool( 'customers' ); $user = $customers ? get_userdata( (int) $customers[ $random->int( 0, count( $customers ) - 1 ) ] ) : false; if ( $user ) { $user_id = (int) $user->ID; $name = (string) $user->display_name; $email = (string) $user->user_email; } else { $first = (string) $random->pick( $ctx->words['first_names'] ); $name = $first . ' ' . substr( (string) $random->pick( $ctx->words['last_names'] ), 0, 1 ) . '.'; $email = 'reviewer.' . Words::ascii( $first ) . '.' . $random->int( 1000, 9999 ) . '@' . $ctx->domain; } $post = get_post( $product ); $after = $post ? max( $after, (int) get_post_time( 'U', true, $post ) ) : $after; } $plan['product'] = $product; $plan['verified'] = $ok; $plan['content'] = (string) $random->pick( $ctx->words['reviews'] ); /** This filter is documented in src/Modules/TestData/Make/Products.php */ $plan = (array) apply_filters( 'apwc_test_data_item', $plan, 'review', $index, $ctx->batch ); $product = (int) ( $plan['product'] ?? $product ); // Only test products get test reviews, whatever a filter did. if ( ! $product || 'product' !== get_post_type( $product ) || '' === (string) get_post_meta( $product, Marker::META, true ) ) { return array( 'id' => 0, 'product' => 0, ); } $date = Plan::date_in( (float) ( $plan['date_roll'] ?? 0.5 ), min( $after, $ctx->to ), $ctx->to ); $rating = max( 1, min( 5, (int) ( $plan['rating'] ?? 5 ) ) ); $id = wp_insert_comment( array( 'comment_post_ID' => $product, 'comment_author' => mb_substr( sanitize_text_field( '' === $name ? __( 'Test reviewer', 'admin-powerups-for-woocommerce' ) : $name ), 0, 245 ), 'comment_author_email' => sanitize_email( $email ), 'comment_author_url' => '', 'comment_author_IP' => Words::ip( $random ), 'comment_agent' => 'Admin Powerups test data', 'comment_content' => wp_kses_post( (string) ( $plan['content'] ?? '' ) ), 'comment_type' => 'review', 'comment_parent' => 0, 'comment_approved' => empty( $plan['approved'] ) ? 0 : 1, 'user_id' => $user_id, 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $date ), 'comment_date' => get_date_from_gmt( gmdate( 'Y-m-d H:i:s', $date ) ), 'comment_meta' => array( Marker::META => $ctx->batch, 'rating' => $rating, 'verified' => empty( $plan['verified'] ) ? 0 : 1, ), ) ); if ( ! $id ) { return array( 'id' => 0, 'product' => 0, ); } Marker::sign( 'comment', (int) $id, $ctx->batch ); $ctx->created( 'review', (int) $id ); return array( 'id' => (int) $id, 'product' => $product, ); } }