'GET', 'callback' => [ $this, 'get_settings' ], 'permission_callback' => [ $this, 'check_permission' ], ], [ 'methods' => 'POST', 'callback' => [ $this, 'update_settings' ], 'permission_callback' => [ $this, 'check_permission' ], ], ] ); register_rest_route( self::NAMESPACE, '/settings/disconnect', [ 'methods' => 'POST', 'callback' => [ $this, 'disconnect' ], 'permission_callback' => [ $this, 'check_permission' ], ] ); } /** * GET /mintall/v1/settings */ public function get_settings() { $connection = Connection_Repository::get_current(); $api_key_row = $connection ? Api_Key_Repository::get_by_connection( $connection->id ) : null; $account_email = $api_key_row ? $api_key_row->user_email : ''; return new \WP_REST_Response( [ // Connected means the same thing here as it does everywhere // else: both rows exist and the key is readable. Reporting // connected on the connection row alone would show "Connected" // on a store whose every API call returns not_connected, with // no reconnect button anywhere. 'connected' => ! empty( $connection ) && ! empty( $api_key_row ), 'site_url' => $connection->site_url ?? '', 'platform_id' => $connection->platform_id ?? '', 'created_at' => $connection->created_at ?? '', 'account_email' => $account_email, 'auto_protect' => (bool) get_option( 'mintall_keep_auto_protect', false ), 'visibility_on_protect' => (bool) get_option( 'mintall_keep_visibility_on_protect', false ), 'page_view_capture' => Mintall_Pageview::is_enabled(), 'page_view_capture_decided' => Mintall_Pageview::is_decided(), 'reauth_required' => Connection_Manager::is_reauth_required(), ] ); } /** * POST /mintall/v1/settings */ public function update_settings( $request ) { $page_view_capture = $request->get_param( 'page_view_capture' ); if ( null !== $page_view_capture ) { if ( rest_sanitize_boolean( $page_view_capture ) ) { $result = Mintall_Pageview::enable(); if ( is_wp_error( $result ) ) { $data = $result->get_error_data(); $status = is_array( $data ) && isset( $data['status'] ) ? $data['status'] : 502; return $this->error_response( $result->get_error_message(), $status ); } } else { Mintall_Pageview::disable(); } } $auto_protect = $request->get_param( 'auto_protect' ); if ( null !== $auto_protect ) { $auto_protect = (bool) $auto_protect; $was_enabled = (bool) get_option( 'mintall_keep_auto_protect', false ); if ( $auto_protect !== $was_enabled ) { $api_key = Connection_Manager::get_api_key(); if ( ! $api_key ) { return $this->error_response( 'Not connected to Mintall.', 400 ); } // Mirror the toggle to the backend before persisting locally and // in lockstep with it. The protection queue rejects AUTO enqueues // with 409 unless the account has auto_protect_enabled set, so a // local-only toggle would silently queue nothing. On failure, // leave the local option unchanged so the two never diverge. $sync = Mintall_Accounts::set_auto_protect( $api_key, $auto_protect ); if ( is_wp_error( $sync ) ) { $data = $sync->get_error_data(); $status = is_array( $data ) && isset( $data['status'] ) ? $data['status'] : 502; return $this->error_response( $sync->get_error_message(), $status ); } update_option( 'mintall_keep_auto_protect', $auto_protect ); if ( $auto_protect ) { // Stamp when auto-protect was switched on — the summary banner // counts AUTO jobs since this moment — then backfill the // existing catalog so already-published products are protected // now, not just ones published/edited after this point. update_option( 'mintall_keep_auto_protect_enabled_at', gmdate( 'c' ) ); Product_Monitor::backfill_published_catalog( $api_key ); } } } $visibility_on_protect = $request->get_param( 'visibility_on_protect' ); if ( null !== $visibility_on_protect ) { update_option( 'mintall_keep_visibility_on_protect', (bool) $visibility_on_protect ); } return new \WP_REST_Response( [ 'success' => true, 'auto_protect' => (bool) get_option( 'mintall_keep_auto_protect', false ), 'visibility_on_protect' => (bool) get_option( 'mintall_keep_visibility_on_protect', false ), 'page_view_capture' => Mintall_Pageview::is_enabled(), 'page_view_capture_decided' => Mintall_Pageview::is_decided(), ] ); } /** * POST /mintall/v1/settings/disconnect */ public function disconnect() { $result = Connection_Manager::disconnect(); if ( is_wp_error( $result ) ) { return $this->error_response( 'Disconnect failed: ' . $result->get_error_message() . ' Your store is still connected — please try again.', 502 ); } return new \WP_REST_Response( [ 'success' => true, 'connected' => false, ] ); } }