ProvisionalAccount.php
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Models; |
| 4 | |
| 5 | /** |
| 6 | * Provisional Account model |
| 7 | */ |
| 8 | class ProvisionalAccount extends Model { |
| 9 | /** |
| 10 | * Rest API endpoint |
| 11 | * |
| 12 | * @var string |
| 13 | */ |
| 14 | protected $endpoint = 'public/provisional_accounts'; |
| 15 | |
| 16 | /** |
| 17 | * Object name |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | protected $object_name = 'provisional_account'; |
| 22 | |
| 23 | /** |
| 24 | * Make the API request. |
| 25 | * |
| 26 | * @param array $args Array of arguments. |
| 27 | * @param string $endpoint Optional endpoint override. |
| 28 | * |
| 29 | * @return Model |
| 30 | */ |
| 31 | protected function makeRequest( $args = [], $endpoint = '' ) { |
| 32 | return \SureCart::unAuthorizedRequest( ...$this->prepareRequest( $args, $endpoint ) ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Check if the API token is set. |
| 37 | * |
| 38 | * @return bool |
| 39 | */ |
| 40 | protected function hasApiToken() { |
| 41 | return ! empty( ApiToken::get() ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Check if the E2E testing is enabled. |
| 46 | * |
| 47 | * @return bool |
| 48 | */ |
| 49 | protected function isTesting() { |
| 50 | return defined( 'SC_E2E_TESTING' ) ? (bool) SC_E2E_TESTING : false; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Create a new model |
| 55 | * |
| 56 | * @param array $attributes Attributes to create. |
| 57 | * |
| 58 | * @return $this|false |
| 59 | */ |
| 60 | protected function create( $attributes = [] ) { |
| 61 | // we only allow this if the setup is not complete. |
| 62 | if ( $this->hasApiToken() && ! $this->isTesting() ) { |
| 63 | return new \WP_Error( 'setup_complete', __( 'You have already set up your store.', 'surecart' ) ); |
| 64 | } |
| 65 | |
| 66 | // set account name as the site name if nothing is provided. |
| 67 | if ( empty( $attributes['account_name'] ) ) { |
| 68 | $attributes['account_name'] = get_bloginfo( 'name' ) ? get_bloginfo( 'name' ) : get_bloginfo( 'url' ); |
| 69 | } |
| 70 | |
| 71 | // Prevent minimum 3 character requirement error for account name. |
| 72 | if ( strlen( $attributes['account_name'] ) < 3 ) { |
| 73 | return new \WP_Error( 'invalid_account_name', __( 'Store name must be at least 3 characters long. Please choose a different name for your store.', 'surecart' ) ); |
| 74 | } |
| 75 | |
| 76 | // set the account url from the blog url. |
| 77 | if ( empty( $attributes['account_url'] ) ) { |
| 78 | $attributes['account_url'] = get_bloginfo( 'url' ); |
| 79 | } |
| 80 | |
| 81 | // set source with fallback to the option. |
| 82 | $attributes['source'] = isset( $attributes['source'] ) ? sanitize_text_field( wp_unslash( $attributes['source'] ) ) : sanitize_text_field( get_option( 'surecart_source', 'surecart_wp' ) ); |
| 83 | |
| 84 | $created = parent::create( $attributes ); |
| 85 | if ( is_wp_error( $created ) ) { |
| 86 | return $created; |
| 87 | } |
| 88 | |
| 89 | // bulk product creation action. |
| 90 | if ( isset( $attributes['products'] ) ) { |
| 91 | $seed = $this->seed( $attributes['products'] ); |
| 92 | if ( is_wp_error( $seed ) ) { |
| 93 | return $seed; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // clear account cache. |
| 98 | \SureCart::account()->clearCache(); |
| 99 | |
| 100 | // create the products. |
| 101 | return $created; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Seed the account with products. |
| 106 | * |
| 107 | * @param array $products The products to seed. |
| 108 | * |
| 109 | * @return \SureCart\Models\Import|\WP_Error |
| 110 | */ |
| 111 | protected function seed( $products = [] ) { |
| 112 | return \SureCart::account()->seed( $products ); |
| 113 | } |
| 114 | } |
| 115 |