Issues (29)

src/Plugin.php (1 issue)

Severity
1
<?php
2
/**
3
 * @link      https://dukt.net/facebook/
4
 * @copyright Copyright (c) Dukt
5
 * @license   https://github.com/dukt/facebook/blob/master/LICENSE.md
6
 */
7
8
namespace dukt\facebook;
9
10
use Craft;
11
use craft\helpers\UrlHelper;
12
use craft\web\UrlManager;
13
use craft\events\RegisterUrlRulesEvent;
14
use craft\services\Dashboard;
15
use craft\events\RegisterComponentTypesEvent;
16
use dukt\facebook\base\PluginTrait;
17
use dukt\facebook\models\Settings;
18
use dukt\facebook\widgets\InsightsWidget;
19
use yii\base\Event;
20
21
/**
22
 * Facebook plugin.
23
 *
24
 * @author Dukt <[email protected]>
25
 * @since  2.0
26
 */
27
class Plugin extends \craft\base\Plugin
28
{
29
    // Traits
30
    // =========================================================================
31
32
    use PluginTrait;
0 ignored issues
show
The trait dukt\facebook\base\PluginTrait requires some properties which are not provided by dukt\facebook\Plugin: $oauthClientId, $oauthClientSecret
Loading history...
33
34
    // Properties
35
    // =========================================================================
36
37
    /**
38
     * @var bool
39
     */
40
    public $hasCpSettings = true;
41
42
    /**
43
     * @var \dukt\facebook\Plugin The plugin instance.
44
     */
45
    public static $plugin;
46
47
    // Public Methods
48
    // =========================================================================
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function init()
54
    {
55
        parent::init();
56
        self::$plugin = $this;
57
58
        $this->setComponents([
59
            'accounts' => \dukt\facebook\services\Accounts::class,
60
            'api' => \dukt\facebook\services\Api::class,
61
            'cache' => \dukt\facebook\services\Cache::class,
62
            'oauth' => \dukt\facebook\services\Oauth::class,
63
            'reports' => \dukt\facebook\services\Reports::class,
64
        ]);
65
66
        Event::on(Dashboard::class, Dashboard::EVENT_REGISTER_WIDGET_TYPES, function(RegisterComponentTypesEvent $event): void {
67
            $event->types[] = InsightsWidget::class;
68
        });
69
70
        Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event): void {
71
            $rules = [
72
                'facebook/settings' => 'facebook/settings/index',
73
                'facebook/settings/oauth' => 'facebook/settings/oauth',
74
            ];
75
76
            $event->rules = array_merge($event->rules, $rules);
77
        });
78
    }
79
80
    // Protected Methods
81
    // =========================================================================
82
83
    /**
84
     * Creates and returns the model used to store the plugin’s settings.
85
     *
86
     * @return \craft\base\Model|null
87
     */
88
    protected function createSettingsModel()
89
    {
90
        return new Settings();
91
    }
92
93
    /**
94
     * Returns the rendered settings HTML, which will be inserted into the content
95
     * block on the settings page.
96
     *
97
     * @return string The rendered settings HTML
98
     */
99
    public function getSettingsResponse()
100
    {
101
        $url = UrlHelper::cpUrl('facebook/settings');
102
103
        Craft::$app->controller->redirect($url);
104
105
        return '';
106
    }
107
}
108