ProductDataObject.php
105 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Integrations\ThriveAutomator\DataObjects; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | exit; // Silence is golden! |
| 7 | } |
| 8 | |
| 9 | use SureCart\Integrations\ThriveAutomator\DataFields\PreviousProductDataField; |
| 10 | use SureCart\Integrations\ThriveAutomator\DataFields\ProductDataField; |
| 11 | use SureCart\Integrations\ThriveAutomator\DataFields\ProductIDDataField; |
| 12 | use SureCart\Integrations\ThriveAutomator\DataFields\ProductNameDataField; |
| 13 | use Thrive\Automator\Items\Data_Object; |
| 14 | use SureCart\Models\Product; |
| 15 | |
| 16 | /** |
| 17 | * Class ProductDataObject |
| 18 | */ |
| 19 | class ProductDataObject extends Data_Object { |
| 20 | /** |
| 21 | * Get the data-object identifier |
| 22 | * |
| 23 | * @return string |
| 24 | */ |
| 25 | public static function get_id() { |
| 26 | return 'surecart_product_data'; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Nice name for the data-object. |
| 31 | * |
| 32 | * @return string |
| 33 | */ |
| 34 | public static function get_nice_name() { |
| 35 | return __( 'SureCart product object', 'surecart' ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Array of field object keys that are contained by this data-object |
| 40 | * |
| 41 | * @return array |
| 42 | */ |
| 43 | public static function get_fields() { |
| 44 | return [ |
| 45 | ProductNameDataField::get_id(), |
| 46 | ProductIDDataField::get_id(), |
| 47 | ProductDataField::get_id(), |
| 48 | ]; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Create the object from the given product. |
| 53 | * |
| 54 | * @param string|\SureCart\Models\Product $param Product model or id. |
| 55 | * |
| 56 | * @throws \Exception If no parameter is provided. |
| 57 | * |
| 58 | * @return array |
| 59 | */ |
| 60 | public static function create_object( $param ) { |
| 61 | if ( empty( $param ) ) { |
| 62 | throw new \Exception( 'No parameter provided for SureCart ProductData object' ); |
| 63 | } |
| 64 | |
| 65 | $product = null; |
| 66 | if ( is_a( $param, Product::class ) ) { |
| 67 | $product = $param; |
| 68 | } else { |
| 69 | $product = Product::find( $param ); |
| 70 | } |
| 71 | |
| 72 | if ( $product ) { |
| 73 | return [ |
| 74 | ProductNameDataField::get_id() => $product->id, |
| 75 | ProductIDDataField::get_id() => $product->id, |
| 76 | ProductDataField::get_id() => $product->id, |
| 77 | PreviousProductDataField::get_id() => $product->id, |
| 78 | ]; |
| 79 | } |
| 80 | |
| 81 | return $product; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get the options. |
| 86 | * |
| 87 | * @return array |
| 88 | */ |
| 89 | public static function get_data_object_options() { |
| 90 | $options = []; |
| 91 | |
| 92 | foreach ( Product::get() as $product ) { |
| 93 | $name = $product->name; |
| 94 | $id = $product->id; |
| 95 | $options[ $id ] = [ |
| 96 | 'id' => $id, |
| 97 | 'label' => $name, |
| 98 | ]; |
| 99 | } |
| 100 | |
| 101 | return $options; |
| 102 | } |
| 103 | |
| 104 | } |
| 105 |