ProductImport.php
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Models; |
| 4 | |
| 5 | use SureCart\Models\Concerns\ImportModel; |
| 6 | use SureCart\Models\Product; |
| 7 | |
| 8 | /** |
| 9 | * Product import model |
| 10 | */ |
| 11 | class ProductImport extends ImportModel { |
| 12 | /** |
| 13 | * Rest API endpoint. |
| 14 | * |
| 15 | * @var string |
| 16 | */ |
| 17 | protected $endpoint = 'imports/products'; |
| 18 | |
| 19 | /** |
| 20 | * Set the data attribute as an array of Product models. |
| 21 | * Accepts an iterable of attribute arrays/Products or a single Product instance. |
| 22 | * |
| 23 | * @param mixed $value Iterable of product attributes or Product instance. |
| 24 | * |
| 25 | * @return void |
| 26 | */ |
| 27 | public function setDataAttribute( $value ) { |
| 28 | $models = []; |
| 29 | if ( ! empty( $value ) && is_array( $value ) ) { |
| 30 | foreach ( $value as $attributes ) { |
| 31 | $models[] = is_a( $attributes, Product::class ) ? $attributes : new Product( $attributes ); |
| 32 | } |
| 33 | $value = $models; |
| 34 | } |
| 35 | $this->attributes['data'] = $value; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Create a new model. |
| 40 | * |
| 41 | * @param array $attributes Attributes to create. |
| 42 | * |
| 43 | * @return $this|false |
| 44 | */ |
| 45 | protected function create( $attributes = array() ) { |
| 46 | // Ensure data is set as Product models first. |
| 47 | $this->fill( $attributes ); |
| 48 | |
| 49 | // for each item in the data, add a pattern to the database. |
| 50 | foreach ( $this->attributes['data'] ?? [] as $product ) { |
| 51 | if ( empty( $product->content ) ) { |
| 52 | continue; |
| 53 | } |
| 54 | |
| 55 | $pattern_id = $this->addPattern( $product ); |
| 56 | if ( ! is_wp_error( $pattern_id ) && ! empty( $pattern_id ) ) { |
| 57 | $product->metadata->sc_initial_sync_pattern = $pattern_id; |
| 58 | } |
| 59 | |
| 60 | // Remove content from the data object. |
| 61 | unset( $product->content ); |
| 62 | } |
| 63 | |
| 64 | // Pass the modified attributes with processed data to parent. |
| 65 | return parent::create(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Add a pattern to the database. |
| 70 | * |
| 71 | * @param \SureCart\Models\Product $product The product. |
| 72 | * |
| 73 | * @return int|\WP_Error |
| 74 | */ |
| 75 | protected function addPattern( $product ) { |
| 76 | $pattern_id = wp_insert_post( |
| 77 | array( |
| 78 | // translators: %s is the product name. |
| 79 | 'post_title' => sprintf( __( '%s Content', 'surecart' ), $product->name ), |
| 80 | 'post_content' => $product->content, |
| 81 | 'post_status' => 'publish', |
| 82 | 'comment_status' => 'closed', |
| 83 | 'ping_status' => 'closed', |
| 84 | 'post_type' => 'wp_block', |
| 85 | 'meta_input' => [ |
| 86 | 'wp_pattern_sync_status' => 'unsynced', |
| 87 | ], |
| 88 | ) |
| 89 | ); |
| 90 | |
| 91 | if ( is_wp_error( $pattern_id ) ) { |
| 92 | error_log( 'Error adding pattern for sync ' . $product->name . ': ' . $pattern_id->get_error_message() ); |
| 93 | } |
| 94 | |
| 95 | return $pattern_id; |
| 96 | } |
| 97 | } |
| 98 |