-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.php
executable file
·135 lines (128 loc) · 5.22 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
@include_once __DIR__ . '/vendor/autoload.php';
@include_once __DIR__ . '/src/KirbyShopify.php';
@include_once __DIR__ . '/src/models/shopify.products.php';
@include_once __DIR__ . '/src/models/shopify.product.php';
@include_once __DIR__ . '/src/models/shopify.collections.php';
@include_once __DIR__ . '/src/models/shopify.collection.php';
Kirby::plugin('tristanb/kirby-shopify', [
'options' => [
'cache.api' => true
],
'collections' => [
'kirby-shopify.productsPage' => function ($site) {
return $site->pages()->filterBy('intendedTemplate', 'shopify.products')->first();
},
'kirby-shopify.products' => function ($site) {
return collection('kirby-shopify.productsPage')->children();
},
'kirby-shopify.collectionsPage' => function ($site) {
return $site->pages()->filterBy('intendedTemplate', 'shopify.collections')->first();
},
'kirby-shopify.collections' => function ($site) {
return collection('kirby-shopify.collectionsPage')->children();
}
],
'pageModels' => [
'shopify.products' => 'ShopifyProductsPage',
'shopify.product' => 'ShopifyProductPage',
'shopify.collections' => 'ShopifyCollectionsPage',
'shopify.collection' => 'ShopifyCollectionPage',
],
'blueprints' => [
'pages/shopify.products' => __DIR__ . '/src/blueprints/shopify.products.yml',
'pages/shopify.product' => __DIR__ . '/src/blueprints/shopify.product.yml',
'pages/shopify.collections' => __DIR__ . '/src/blueprints/shopify.collections.yml',
'pages/shopify.collection' => __DIR__ . '/src/blueprints/shopify.collection.yml'
],
'snippets' => [
'kirby-shopify.product.structured-data' => __DIR__ . '/src/snippets/product.structured-data.php'
],
'fieldMethods' => [
'img_url' => function ($field, string $size = '') {
if (is_string($field)) {
$src = $field;
} elseif ($field->src()->isNotEmpty()) {
$src = $field->src();
}
return preg_replace('/\.(jpg|jpeg|png|bmp|gif)/', '_'.$size.'.$1', $src);
}
],
'pageMethods' => [
'inventory_quantity' => function () {
$inventory = 0;
foreach ($this->shopifyVariants()->toStructure() as $key => $variant) {
$inventory += $variant->inventory_quantity()->int();
}
return $inventory;
},
'isAvailable' => function () {
return $this->inventory_quantity() > 0;
},
'hasImages' => function () {
return $this->shopifyImages()->toStructure()->count() > 0;
},
'toProduct' => function () {
return collection('kirby-shopify.products')->findBy('shopifyHandle', $this->shopifyHandle()->value());
},
'toProducts' => function () {
$products = new Collection();
foreach ($this->children() as $key => $p) {
if ($p = $p->toProduct()) {
$products->add($p);
}
}
return $products;
}
],
'routes' => [
[
'pattern' => 'kirby-shopify/api/cache/clear',
'method' => 'POST',
'action' => function () {
$hmac_header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'];
$data = file_get_contents('php://input');
$verified = \KirbyShopify\App::verifyWebhook($data, $hmac_header);
if ($verified) {
\KirbyShopify\App::clearCache();
\KirbyShopify\App::clearKirbyCache();
return Response::json(["status" => "success", "code" => 200, "message" => "Cache cleared"]);
} else {
return Response::json(["status" => "success", "code" => 200, "message" => "Identication failed"]);
}
}
],
[
'pattern' => 'kirby-shopify/api/cache/products/clear',
'method' => 'POST',
'action' => function () {
$hmac_header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'];
$data = file_get_contents('php://input');
$verified = \KirbyShopify\App::verifyWebhook($data, $hmac_header);
if ($verified) {
\KirbyShopify\App::clearProductsCache();
\KirbyShopify\App::clearKirbyCache();
return Response::json(["status" => "success", "code" => 200, "message" => "Cache cleared"]);
} else {
return Response::json(["status" => "success", "code" => 200, "message" => "Identication failed"]);
}
}
],
[
'pattern' => 'kirby-shopify/api/cache/collections/clear',
'method' => 'POST',
'action' => function () {
$hmac_header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'];
$data = file_get_contents('php://input');
$verified = \KirbyShopify\App::verifyWebhook($data, $hmac_header);
if ($verified) {
\KirbyShopify\App::clearCollectionsCache();
\KirbyShopify\App::clearKirbyCache();
return Response::json(["status" => "success", "code" => 200, "message" => "Cache cleared"]);
} else {
return Response::json(["status" => "success", "code" => 200, "message" => "Identication failed"]);
}
}
]
]
]);