request = $request; if ( ! current_user_can( 'install_plugins' ) || ! current_user_can( 'install_themes' ) ) { return new WP_Error( 'rest_forbidden', __( 'Sorry, you are not allowed to fetch pack import info.', 'templately' ), [ 'status' => rest_authorization_required_code() ] ); } return parent::permission_check( $request ); } public function register_routes() { $this->get( 'import/info', [ $this, 'import_info' ], [ 'id' => [ 'required' => true, ], 'isAi' => [ 'required' => false, ], ] ); } public function import_info() { $id = $this->get_param( 'id', 0, 'absint' ); $isAi = Helper::sanitize( $this->get_param( 'isAi', false, null ), 'boolean' ); if ( empty( $id ) ) { return $this->error( 'invalid_id', __( 'A valid pack id is required.', 'templately' ), 'import/info', 400 ); } $data = ( new PackInfoFetcher() )->fetch( $id, $isAi ); if ( is_wp_error( $data ) ) { return $this->error( 'api_error', $data->get_error_message(), 'import/info', 500 ); } // Post-fetch enrichment — identical to Ajax\PackInfoController::import_info(). $business_niches = get_option( 'templately_ai_business_niches', [] ); $data['data']['business_niches'] = $business_niches; if ( isset( $data['data']['manifest'] ) ) { $data['data']['manifest'] = json_decode( $data['data']['manifest'], true ); } if ( isset( $data['data']['settings'] ) ) { $data['data']['settings'] = json_decode( $data['data']['settings'], true ); } if ( $isAi ) { // Get the latest AI process for the current API key. $last_ai_process = AIUtils::get_latest_ai_process_by_api_key( $id ); if ( $last_ai_process ) { $data['data']['ai_process'] = $last_ai_process; } if ( $last_ai_process && $id == ( $last_ai_process['pack_id'] ?? null ) ) { // Read AI preview content directly from files using the common function. $session_id = $last_ai_process['session_id'] ?? null; $ai_page_ids = $last_ai_process['ai_page_ids'] ?? []; $dir_path = null; // Get session data to retrieve dir_path. if ( $session_id ) { $session_data = SessionData::get_data( $session_id ); $dir_path = $session_data['dir_path'] ?? null; } // Use the common function to read AI template data if we have the required data. if ( $session_id && $ai_page_ids && $dir_path ) { $data['data']['ai_preview_content'] = AIUtils::read_ai_template_data( $session_id, $ai_page_ids, $dir_path ); } else { $data['data']['ai_preview_content'] = []; } } } // The ajax handler returned the payload raw via `wp_send_json($data)` (no // success wrapper) — `success($data)` emits the same body verbatim (200 + $data). return $this->success( $data ); } }