Issues (446)

src/Recipe.php (1 issue)

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 Craft;
15
use craft\base\Plugin;
16
use craft\events\PluginEvent;
17
use craft\events\RegisterComponentTypesEvent;
18
use craft\feedme\events\RegisterFeedMeFieldsEvent;
19
use craft\feedme\services\Fields as FeedMeFields;
20
use craft\helpers\UrlHelper;
21
use craft\services\Fields;
22
use craft\services\Plugins;
23
use nystudio107\recipe\fields\Recipe as RecipeField;
24
use nystudio107\recipe\integrations\RecipeFeedMeField;
25
use nystudio107\recipe\models\Settings;
26
use nystudio107\recipe\services\ServicesTrait;
27
use yii\base\Event;
28
29
/**
30
 * Class Recipe
31
 *
32
 * @author    nystudio107
33
 * @package   Recipe
34
 * @since     1.0.0
35
 *
36
 * @property Settings $settings
37
 */
38
class Recipe extends Plugin
39
{
40
    // Traits
41
    // =========================================================================
42
43
    use ServicesTrait;
44
45
    // Static Properties
46
    // =========================================================================
47
48
    /**
49
     * @var ?Recipe
50
     */
51
    public static ?Recipe $plugin = null;
52
53
    // Public Properties
54
    // =========================================================================
55
56
    /**
57
     * @var string
58
     */
59
    public string $schemaVersion = '1.0.0';
60
61
    /**
62
     * @var bool
63
     */
64
    public bool $hasCpSection = false;
65
66
    /**
67
     * @var bool
68
     */
69
    public bool $hasCpSettings = true;
70
71
    // Public Methods
72
    // =========================================================================
73
74
    /**
75
     * @inheritdoc
76
     */
77
    public function init(): void
78
    {
79
        parent::init();
80
        self::$plugin = $this;
81
82
        // Register our Field
83
        Event::on(
84
            Fields::class,
85
            Fields::EVENT_REGISTER_FIELD_TYPES,
86
            static function(RegisterComponentTypesEvent $event): void {
87
                $event->types[] = RecipeField::class;
88
            }
89
        );
90
91
        // Show our "Welcome to Recipe" message
92
        Event::on(
93
            Plugins::class,
94
            Plugins::EVENT_AFTER_INSTALL_PLUGIN,
95
            function(PluginEvent $event): void {
96
                if (($event->plugin === $this) && !Craft::$app->getRequest()->getIsConsoleRequest()) {
0 ignored issues
show
The condition $event->plugin === $this is always false.
Loading history...
97
                    Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('recipe/welcome'))->send();
98
                }
99
            }
100
        );
101
102
        $feedMeInstalled = Craft::$app->getPlugins()->isPluginInstalled('feed-me') && Craft::$app->getPlugins()->isPluginEnabled('feed-me');
103
104
        if ($feedMeInstalled) {
105
            Event::on(FeedMeFields::class, FeedMeFields::EVENT_REGISTER_FEED_ME_FIELDS, function(RegisterFeedMeFieldsEvent $e) {
106
                $e->fields[] = RecipeFeedMeField::class;
107
            });
108
        }
109
110
        Craft::info(
111
            Craft::t(
112
                'recipe',
113
                '{name} plugin loaded',
114
                ['name' => $this->name]
115
            ),
116
            __METHOD__
117
        );
118
    }
119
120
    /**
121
     * @inheritdoc
122
     */
123
    protected function createSettingsModel(): Settings
124
    {
125
        return new Settings();
126
    }
127
128
    /**
129
     * @inheritdoc
130
     */
131
    protected function settingsHtml(): ?string
132
    {
133
        return Craft::$app->getView()->renderTemplate('recipe/settings', [
134
            'settings' => $this->settings,
135
        ]);
136
    }
137
}
138