PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.4.1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.4.1
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / Models / ProvisionalAccount.php
ProvisionalAccount.php
125 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 first (before potential early return).
98 \SureCart::account()->clearCache();
99
100 if ( ! empty( $attributes['import_woocommerce_products'] ) ) {
101 $service = \SureCart::sync()->woocommerce_products();
102 if ( ! $service->isRunning() ) {
103 $import = $service->dispatch();
104 if ( is_wp_error( $import ) ) {
105 error_log( 'SureCart: WooCommerce import dispatch failed during onboarding - ' . $import->get_error_message() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
106 }
107 }
108 }
109
110 // create the products.
111 return $created;
112 }
113
114 /**
115 * Seed the account with products.
116 *
117 * @param array $products The products to seed.
118 *
119 * @return \SureCart\Models\Import|\WP_Error
120 */
121 protected function seed( $products = [] ) {
122 return \SureCart::account()->seed( $products );
123 }
124 }
125