AI
1 year ago
RateLimits
4 weeks ago
Reports
4 weeks ago
Templates
3 years ago
views
4 weeks ago
AnalyticsImports.php
5 months ago
Coupons.php
4 years ago
CustomAttributeTraits.php
4 years ago
Customers.php
4 years ago
Data.php
4 years ago
DataCountries.php
4 years ago
DataDownloadIPs.php
4 years ago
Experiments.php
4 years ago
Features.php
4 years ago
Init.php
4 weeks ago
LaunchYourStore.php
1 year ago
Leaderboards.php
1 year ago
Marketing.php
2 months ago
MarketingCampaignTypes.php
3 years ago
MarketingCampaigns.php
2 years ago
MarketingChannels.php
3 years ago
MarketingOverview.php
2 months ago
MarketingRecommendations.php
2 years ago
MobileAppMagicLink.php
3 years ago
MobileAppQRLogin.php
4 weeks ago
NoteActions.php
4 weeks ago
Notes.php
4 weeks ago
Notice.php
1 year ago
OnboardingFreeExtensions.php
1 year ago
OnboardingPlugins.php
4 weeks ago
OnboardingProductTypes.php
4 years ago
OnboardingProducts.php
2 years ago
OnboardingProfile.php
1 year ago
OnboardingTasks.php
9 months ago
OnboardingThemes.php
9 months ago
Options.php
4 weeks ago
Orders.php
1 year ago
PaymentGatewaySuggestions.php
2 months ago
Plugins.php
2 months ago
ProductAttributeTerms.php
4 years ago
ProductAttributes.php
4 years ago
ProductCategories.php
4 years ago
ProductForm.php
3 years ago
ProductReviews.php
4 years ago
ProductVariations.php
1 year ago
Products.php
1 year ago
ProductsLowInStock.php
4 months ago
SettingOptions.php
3 years ago
ShippingPartnerSuggestions.php
1 month ago
Taxes.php
4 years ago
Themes.php
2 years ago
LaunchYourStore.php
223 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API Launch Your Store Controller |
| 4 | * |
| 5 | * Handles requests to /launch-your-store/* |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin\API; |
| 9 | |
| 10 | use Automattic\WooCommerce\Admin\WCAdminHelper; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | /** |
| 15 | * Launch Your Store controller. |
| 16 | * |
| 17 | * @internal |
| 18 | */ |
| 19 | class LaunchYourStore { |
| 20 | |
| 21 | /** |
| 22 | * Endpoint namespace. |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | protected $namespace = 'wc-admin'; |
| 27 | |
| 28 | /** |
| 29 | * Route base. |
| 30 | * |
| 31 | * @var string |
| 32 | */ |
| 33 | protected $rest_base = 'launch-your-store'; |
| 34 | |
| 35 | /** |
| 36 | * Register routes. |
| 37 | */ |
| 38 | public function register_routes() { |
| 39 | register_rest_route( |
| 40 | $this->namespace, |
| 41 | '/' . $this->rest_base . '/initialize-coming-soon', |
| 42 | array( |
| 43 | array( |
| 44 | 'methods' => 'POST', |
| 45 | 'callback' => array( $this, 'initialize_coming_soon' ), |
| 46 | 'permission_callback' => array( $this, 'must_be_shop_manager_or_admin' ), |
| 47 | ), |
| 48 | ) |
| 49 | ); |
| 50 | |
| 51 | register_rest_route( |
| 52 | $this->namespace, |
| 53 | '/' . $this->rest_base . '/update-survey-status', |
| 54 | array( |
| 55 | array( |
| 56 | 'methods' => 'POST', |
| 57 | 'callback' => array( $this, 'update_survey_status' ), |
| 58 | 'permission_callback' => array( $this, 'must_be_shop_manager_or_admin' ), |
| 59 | 'args' => array( |
| 60 | 'status' => array( |
| 61 | 'type' => 'string', |
| 62 | 'enum' => array( 'yes', 'no' ), |
| 63 | ), |
| 64 | ), |
| 65 | ), |
| 66 | ) |
| 67 | ); |
| 68 | |
| 69 | register_rest_route( |
| 70 | $this->namespace, |
| 71 | '/' . $this->rest_base . '/survey-completed', |
| 72 | array( |
| 73 | array( |
| 74 | 'methods' => 'GET', |
| 75 | 'callback' => array( $this, 'has_survey_completed' ), |
| 76 | 'permission_callback' => array( $this, 'must_be_shop_manager_or_admin' ), |
| 77 | ), |
| 78 | ) |
| 79 | ); |
| 80 | |
| 81 | register_rest_route( |
| 82 | $this->namespace, |
| 83 | '/' . $this->rest_base . '/woopayments/test-orders/count', |
| 84 | array( |
| 85 | array( |
| 86 | 'methods' => 'GET', |
| 87 | 'callback' => array( $this, 'get_woopay_test_orders_count' ), |
| 88 | 'permission_callback' => array( $this, 'must_be_shop_manager_or_admin' ), |
| 89 | ), |
| 90 | ) |
| 91 | ); |
| 92 | |
| 93 | register_rest_route( |
| 94 | $this->namespace, |
| 95 | '/' . $this->rest_base . '/woopayments/test-orders', |
| 96 | array( |
| 97 | array( |
| 98 | 'methods' => 'DELETE', |
| 99 | 'callback' => array( $this, 'delete_woopay_test_orders' ), |
| 100 | 'permission_callback' => array( $this, 'must_be_shop_manager_or_admin' ), |
| 101 | ), |
| 102 | ) |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * User must be either shop_manager or administrator. |
| 108 | * |
| 109 | * @return bool |
| 110 | */ |
| 111 | public function must_be_shop_manager_or_admin() { |
| 112 | // phpcs:ignore |
| 113 | if ( ! current_user_can( 'manage_woocommerce' ) && ! current_user_can( 'administrator' ) ) { |
| 114 | return false; |
| 115 | } |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Initializes options for coming soon. Overwrites existing coming soon status but keeps the private link and share key. |
| 121 | * |
| 122 | * @return bool|void |
| 123 | */ |
| 124 | public function initialize_coming_soon() { |
| 125 | $current_user_id = get_current_user_id(); |
| 126 | // Abort if we don't have a user id for some reason. |
| 127 | if ( ! $current_user_id ) { |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | $coming_soon = 'yes'; |
| 132 | $store_pages_only = WCAdminHelper::is_site_fresh() ? 'no' : 'yes'; |
| 133 | $private_link = 'no'; |
| 134 | $share_key = wp_generate_password( 32, false ); |
| 135 | |
| 136 | update_option( 'woocommerce_coming_soon', $coming_soon ); |
| 137 | update_option( 'woocommerce_store_pages_only', $store_pages_only ); |
| 138 | add_option( 'woocommerce_private_link', $private_link ); |
| 139 | add_option( 'woocommerce_share_key', $share_key ); |
| 140 | |
| 141 | wc_admin_record_tracks_event( |
| 142 | 'launch_your_store_initialize_coming_soon', |
| 143 | array( |
| 144 | 'coming_soon' => $coming_soon, |
| 145 | 'store_pages_only' => $store_pages_only, |
| 146 | 'private_link' => $private_link, |
| 147 | ) |
| 148 | ); |
| 149 | |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Count the test orders created during Woo Payments test mode. |
| 155 | * |
| 156 | * @return \WP_REST_Response |
| 157 | */ |
| 158 | public function get_woopay_test_orders_count() { |
| 159 | $return = function ( $count ) { |
| 160 | return new \WP_REST_Response( array( 'count' => $count ) ); |
| 161 | }; |
| 162 | |
| 163 | $orders = wc_get_orders( |
| 164 | array( |
| 165 | // phpcs:ignore |
| 166 | 'meta_key' => '_wcpay_mode', |
| 167 | // phpcs:ignore |
| 168 | 'meta_value' => 'test', |
| 169 | 'return' => 'ids', |
| 170 | ) |
| 171 | ); |
| 172 | |
| 173 | return $return( count( $orders ) ); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Delete WooPayments test orders. |
| 178 | * |
| 179 | * @return \WP_REST_Response |
| 180 | */ |
| 181 | public function delete_woopay_test_orders() { |
| 182 | $return = function ( $status = 204 ) { |
| 183 | return new \WP_REST_Response( null, $status ); |
| 184 | }; |
| 185 | |
| 186 | $orders = wc_get_orders( |
| 187 | array( |
| 188 | // phpcs:ignore |
| 189 | 'meta_key' => '_wcpay_mode', |
| 190 | // phpcs:ignore |
| 191 | 'meta_value' => 'test', |
| 192 | ) |
| 193 | ); |
| 194 | |
| 195 | foreach ( $orders as $order ) { |
| 196 | $order->delete(); |
| 197 | } |
| 198 | |
| 199 | return $return(); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Update woocommerce_admin_launch_your_store_survey_completed to yes or no |
| 204 | * |
| 205 | * @param \WP_REST_Request $request WP_REST_Request object. |
| 206 | * |
| 207 | * @return \WP_REST_Response |
| 208 | */ |
| 209 | public function update_survey_status( \WP_REST_Request $request ) { |
| 210 | update_option( 'woocommerce_admin_launch_your_store_survey_completed', $request->get_param( 'status' ) ); |
| 211 | return new \WP_REST_Response(); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Return woocommerce_admin_launch_your_store_survey_completed option. |
| 216 | * |
| 217 | * @return \WP_REST_Response |
| 218 | */ |
| 219 | public function has_survey_completed() { |
| 220 | return new \WP_REST_Response( get_option( 'woocommerce_admin_launch_your_store_survey_completed', 'no' ) ); |
| 221 | } |
| 222 | } |
| 223 |