From 4ffafb781d0183b71d0a8d422ebc6bc6d95df644 Mon Sep 17 00:00:00 2001 From: Brandon Shipley Date: Sun, 2 Nov 2025 01:13:21 -0700 Subject: [PATCH] handle internal id vs integer id category in product photos --- src/Controller/ProductPhotosController.php | 22 ++++++++++++++++--- .../ProductPhotosControllerTest.php | 1 + 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Controller/ProductPhotosController.php b/src/Controller/ProductPhotosController.php index c792c46..deac217 100644 --- a/src/Controller/ProductPhotosController.php +++ b/src/Controller/ProductPhotosController.php @@ -81,6 +81,9 @@ class ProductPhotosController extends AppController ]) ->first(); $path = $productSku ? $productSku->product_id . DS . 'skus' . DS . $productSku->id : $path; + + $postData['product_id'] = $productSku->product->id ?? null; + $postData['product_category_id'] = $productSku->product->product_category->internal_id ?? null; } else if ($this->request->getData('product_id')) { $product = $productPhotosTable->Products ->find() @@ -90,9 +93,13 @@ class ProductPhotosController extends AppController ]) ->first(); $path = $product ? $product->id : $path; + $postData['product_category_id'] = $product->product_category->internal_id ?? null; + } else if ($this->request->getData('product_category_id')) { $categoryId = $this->request->getData('product_category_id'); - $field = is_integer($categoryId) ? 'ProductCategories.id' : 'ProductCategories.internal_id'; + // @link https://developer.wordpress.org/reference/functions/wp_is_uuid/ + $regex = '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/'; + $field = preg_match($regex, $categoryId) ? 'ProductCategories.internal_id' : 'ProductCategories.id'; $productCategoryPosted = $productPhotosTable->ProductCategories ->find() ->where([ @@ -106,12 +113,18 @@ class ProductPhotosController extends AppController * @var UploadedFileInterface $photoObject */ $photoObject = $this->request->getData('photo'); + $ext = substr(strtolower($photoObject->getClientFilename()), -4); + $ext = str_starts_with($ext, '.') ? substr($ext, 1) : $ext; + $allowedFileTypes = ['png', 'jpeg', 'jpg']; + if (!in_array($ext, $allowedFileTypes)) { + throw new ForbiddenException('Invalid file type. Only PNG and JPG types are allowed.'); + } $fullPath = $baseDir . $path; if (!file_exists($fullPath)) { if (!mkdir($fullPath, 0777, true)) { throw new ForbiddenException('Failed to create the required folders. Please check the folder permissions and try again.'); - } + } } $destination = $fullPath . DS . $uuid; @@ -122,13 +135,16 @@ class ProductPhotosController extends AppController } $postData['photo_dir'] = $path; - $postData['photo_filename'] = $uuid; + $postData['photo_filename'] = $uuid . '.' . $ext; +// dd($postData); + $productPhoto = $productPhotosTable->patchEntity($productPhoto, $postData); if ($productPhotosTable->save($productPhoto)) { $this->Flash->success(__('The product photo has been saved.')); return $this->redirect(['action' => 'index']); } + dd($productPhoto->getErrors()); $this->Flash->error(__('The product photo could not be saved. Please, try again.')); } $productCategory = $productPhoto->product_category_id ? $productPhotosTable->ProductCategories->find()->where(['internal_id' => $productPhoto->product_category_id ?? '-1'])->first() : null; diff --git a/tests/TestCase/Controller/ProductPhotosControllerTest.php b/tests/TestCase/Controller/ProductPhotosControllerTest.php index 73a064e..c0aae77 100644 --- a/tests/TestCase/Controller/ProductPhotosControllerTest.php +++ b/tests/TestCase/Controller/ProductPhotosControllerTest.php @@ -277,6 +277,7 @@ class ProductPhotosControllerTest extends BaseControllerTest 'enabled' => 1, ]; $this->post($url, $data); +// dd($this->_response); $this->assertResponseCode(302); $this->assertRedirectContains('product-photos');