1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Recipe plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* A comprehensive recipe FieldType for Craft CMS that includes metric/imperial |
6
|
|
|
* conversion, portion calculation, and JSON-LD microdata support |
7
|
|
|
* |
8
|
|
|
* @link https://nystudio107.com |
|
|
|
|
9
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace nystudio107\recipe; |
13
|
|
|
|
14
|
|
|
use nystudio107\recipe\fields\Recipe as RecipeField; |
15
|
|
|
use nystudio107\recipe\integrations\RecipeFeedMeField; |
16
|
|
|
|
17
|
|
|
use Craft; |
18
|
|
|
use craft\base\Plugin; |
19
|
|
|
use craft\services\Fields; |
20
|
|
|
use craft\services\Plugins; |
21
|
|
|
use craft\events\RegisterComponentTypesEvent; |
22
|
|
|
use craft\events\PluginEvent; |
23
|
|
|
use craft\helpers\UrlHelper; |
24
|
|
|
use nystudio107\recipe\models\Settings; |
25
|
|
|
use nystudio107\recipe\services\NutritionApi; |
26
|
|
|
use yii\base\Event; |
27
|
|
|
|
28
|
|
|
use craft\feedme\events\RegisterFeedMeFieldsEvent; |
|
|
|
|
29
|
|
|
use craft\feedme\services\Fields as FeedMeFields; |
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class Recipe |
33
|
|
|
* |
34
|
|
|
* @author nystudio107 |
|
|
|
|
35
|
|
|
* @package Recipe |
|
|
|
|
36
|
|
|
* @since 1.0.0 |
|
|
|
|
37
|
|
|
* |
38
|
|
|
* @property NutritionApi $nutritionApi |
39
|
|
|
* @property Settings $settings |
40
|
|
|
*/ |
|
|
|
|
41
|
|
|
class Recipe extends Plugin |
42
|
|
|
{ |
43
|
|
|
// Static Properties |
44
|
|
|
// ========================================================================= |
45
|
|
|
|
46
|
|
|
/** |
|
|
|
|
47
|
|
|
* @var Recipe |
48
|
|
|
*/ |
49
|
|
|
public static $plugin; |
50
|
|
|
|
51
|
|
|
// Public Methods |
52
|
|
|
// ========================================================================= |
53
|
|
|
|
54
|
|
|
/** |
|
|
|
|
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
|
|
|
|
57
|
|
|
public function init() |
58
|
|
|
{ |
59
|
|
|
parent::init(); |
60
|
|
|
self::$plugin = $this; |
61
|
|
|
|
62
|
|
|
// Register services |
63
|
|
|
$this->setComponents([ |
|
|
|
|
64
|
|
|
'nutritionApi' => NutritionApi::class, |
65
|
|
|
]); |
|
|
|
|
66
|
|
|
|
67
|
|
|
// Register our Field |
68
|
|
|
Event::on( |
69
|
|
|
Fields::class, |
70
|
|
|
Fields::EVENT_REGISTER_FIELD_TYPES, |
71
|
|
|
function (RegisterComponentTypesEvent $event) { |
72
|
|
|
$event->types[] = RecipeField::class; |
73
|
|
|
} |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
// Show our "Welcome to Recipe" message |
77
|
|
|
Event::on( |
78
|
|
|
Plugins::class, |
79
|
|
|
Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
80
|
|
|
function (PluginEvent $event) { |
81
|
|
|
if (!Craft::$app->getRequest()->getIsConsoleRequest() |
82
|
|
|
&& ($event->plugin === $this)) { |
|
|
|
|
83
|
|
|
Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('recipe/welcome'))->send(); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$feedMeInstalled = Craft::$app->getPlugins()->isPluginInstalled('feed-me') && Craft::$app->getPlugins()->isPluginEnabled('feed-me'); |
89
|
|
|
|
90
|
|
|
if ($feedMeInstalled) { |
91
|
|
|
Event::on(FeedMeFields::class, FeedMeFields::EVENT_REGISTER_FEED_ME_FIELDS, function(RegisterFeedMeFieldsEvent $e) { |
|
|
|
|
92
|
|
|
$e->fields[] = RecipeFeedMeField::class; |
93
|
|
|
}); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
Craft::info( |
97
|
|
|
Craft::t( |
98
|
|
|
'recipe', |
99
|
|
|
'{name} plugin loaded', |
100
|
|
|
['name' => $this->name] |
101
|
|
|
), |
102
|
|
|
__METHOD__ |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
|
|
|
|
107
|
|
|
* @inheritdoc |
108
|
|
|
*/ |
|
|
|
|
109
|
|
|
protected function createSettingsModel(): Settings |
110
|
|
|
{ |
111
|
|
|
return new Settings(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
|
|
|
|
115
|
|
|
* @inheritdoc |
116
|
|
|
*/ |
|
|
|
|
117
|
|
|
protected function settingsHtml() |
118
|
|
|
{ |
119
|
|
|
return Craft::$app->getView()->renderTemplate('recipe/settings', [ |
|
|
|
|
120
|
|
|
'settings' => $this->settings |
121
|
|
|
]); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|