dukt /
twitter
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * @link https://dukt.net/twitter/ |
||||
| 4 | * @copyright Copyright (c) Dukt |
||||
| 5 | * @license https://github.com/dukt/twitter/blob/master/LICENSE.md |
||||
| 6 | */ |
||||
| 7 | |||||
| 8 | namespace dukt\twitter; |
||||
| 9 | |||||
| 10 | use Craft; |
||||
| 11 | use craft\events\RegisterCacheOptionsEvent; |
||||
| 12 | use craft\events\RegisterComponentTypesEvent; |
||||
| 13 | use craft\events\RegisterUrlRulesEvent; |
||||
| 14 | use craft\helpers\UrlHelper; |
||||
| 15 | use craft\services\Dashboard; |
||||
| 16 | use craft\services\Fields; |
||||
| 17 | use craft\web\twig\variables\CraftVariable; |
||||
| 18 | use craft\web\UrlManager; |
||||
| 19 | use craft\utilities\ClearCaches; |
||||
| 20 | use dukt\twitter\base\PluginTrait; |
||||
| 21 | use dukt\twitter\fields\Tweet as TweetField; |
||||
| 22 | use dukt\twitter\models\Settings; |
||||
| 23 | use dukt\twitter\widgets\SearchWidget; |
||||
| 24 | use dukt\twitter\web\twig\Extension; |
||||
| 25 | use dukt\twitter\web\twig\variables\TwitterVariable; |
||||
| 26 | use yii\base\Event; |
||||
| 27 | |||||
| 28 | /** |
||||
| 29 | * Twitter Plugin |
||||
| 30 | * |
||||
| 31 | * @author Dukt <[email protected]> |
||||
| 32 | * @since 3.0 |
||||
| 33 | */ |
||||
| 34 | class Plugin extends \craft\base\Plugin |
||||
| 35 | { |
||||
| 36 | // Traits |
||||
| 37 | // ========================================================================= |
||||
| 38 | |||||
| 39 | use PluginTrait; |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 40 | |||||
| 41 | // Properties |
||||
| 42 | // ========================================================================= |
||||
| 43 | |||||
| 44 | /** |
||||
| 45 | * @var bool |
||||
| 46 | */ |
||||
| 47 | public $hasCpSettings = true; |
||||
| 48 | |||||
| 49 | /** |
||||
| 50 | * @var \dukt\twitter\Plugin The plugin instance. |
||||
| 51 | */ |
||||
| 52 | public static $plugin; |
||||
| 53 | |||||
| 54 | // Public Methods |
||||
| 55 | // ========================================================================= |
||||
| 56 | |||||
| 57 | /** |
||||
| 58 | * @inheritdoc |
||||
| 59 | */ |
||||
| 60 | public function init() |
||||
| 61 | { |
||||
| 62 | parent::init(); |
||||
| 63 | self::$plugin = $this; |
||||
| 64 | |||||
| 65 | // Components |
||||
| 66 | |||||
| 67 | $this->setComponents([ |
||||
| 68 | 'twitter' => \dukt\twitter\services\Twitter::class, |
||||
| 69 | 'accounts' => \dukt\twitter\services\Accounts::class, |
||||
| 70 | 'api' => \dukt\twitter\services\Api::class, |
||||
| 71 | 'cache' => \dukt\twitter\services\Cache::class, |
||||
| 72 | 'oauth' => \dukt\twitter\services\Oauth::class, |
||||
| 73 | 'publish' => \dukt\twitter\services\Publish::class, |
||||
| 74 | ]); |
||||
| 75 | |||||
| 76 | |||||
| 77 | // Events |
||||
| 78 | |||||
| 79 | Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event) { |
||||
| 80 | $rules = [ |
||||
| 81 | 'twitter/settings' => 'twitter/settings/index', |
||||
| 82 | 'twitter/settings/oauth' => 'twitter/settings/oauth', |
||||
| 83 | ]; |
||||
| 84 | |||||
| 85 | $event->rules = array_merge($event->rules, $rules); |
||||
| 86 | }); |
||||
| 87 | |||||
| 88 | Event::on(Dashboard::class, Dashboard::EVENT_REGISTER_WIDGET_TYPES, function(RegisterComponentTypesEvent $event) { |
||||
| 89 | $event->types[] = SearchWidget::class; |
||||
| 90 | }); |
||||
| 91 | |||||
| 92 | Event::on(Fields::class, Fields::EVENT_REGISTER_FIELD_TYPES, function(RegisterComponentTypesEvent $event) { |
||||
| 93 | $event->types[] = TweetField::class; |
||||
| 94 | }); |
||||
| 95 | |||||
| 96 | Event::on(ClearCaches::class, ClearCaches::EVENT_REGISTER_CACHE_OPTIONS, function(RegisterCacheOptionsEvent $event) { |
||||
| 97 | $event->options[] = [ |
||||
| 98 | 'key' => 'twitter-caches', |
||||
| 99 | 'label' => Craft::t('twitter', 'Twitter caches'), |
||||
| 100 | 'action' => Craft::$app->path->getRuntimePath().'/twitter' |
||||
|
0 ignored issues
–
show
The method
getRuntimePath() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 101 | ]; |
||||
| 102 | }); |
||||
| 103 | |||||
| 104 | Event::on(CraftVariable::class, CraftVariable::EVENT_INIT, function(Event $event) { |
||||
| 105 | /** @var CraftVariable $variable */ |
||||
| 106 | $variable = $event->sender; |
||||
| 107 | $variable->set('twitter', TwitterVariable::class); |
||||
| 108 | }); |
||||
| 109 | |||||
| 110 | |||||
| 111 | // Twig extension |
||||
| 112 | |||||
| 113 | Craft::$app->view->registerTwigExtension(new Extension()); |
||||
| 114 | } |
||||
| 115 | |||||
| 116 | /** |
||||
| 117 | * @inheritdoc |
||||
| 118 | */ |
||||
| 119 | public function getSettings() |
||||
| 120 | { |
||||
| 121 | $settings = parent::getSettings(); |
||||
| 122 | $configFile = Craft::$app->getConfig()->getConfigFromFile('twitter'); |
||||
| 123 | |||||
| 124 | if($settings) { |
||||
| 125 | $defaultSettingsModel = new Settings(); |
||||
| 126 | |||||
| 127 | if(!isset($configFile['cacheDuration'])) { |
||||
| 128 | $settings->cacheDuration = $defaultSettingsModel->cacheDuration; |
||||
| 129 | } |
||||
| 130 | |||||
| 131 | if(!isset($configFile['enableCache'])) { |
||||
| 132 | $settings->enableCache = $defaultSettingsModel->enableCache; |
||||
| 133 | } |
||||
| 134 | if(!isset($configFile['searchWidgetExtraQuery'])) { |
||||
| 135 | $settings->searchWidgetExtraQuery = $defaultSettingsModel->searchWidgetExtraQuery; |
||||
| 136 | } |
||||
| 137 | } |
||||
| 138 | |||||
| 139 | return $settings; |
||||
| 140 | } |
||||
| 141 | |||||
| 142 | // Protected Methods |
||||
| 143 | // ========================================================================= |
||||
| 144 | |||||
| 145 | /** |
||||
| 146 | * Creates and returns the model used to store the plugin’s settings. |
||||
| 147 | * |
||||
| 148 | * @return \craft\base\Model|null |
||||
| 149 | */ |
||||
| 150 | protected function createSettingsModel() |
||||
| 151 | { |
||||
| 152 | return new Settings(); |
||||
| 153 | } |
||||
| 154 | |||||
| 155 | /** |
||||
| 156 | * Returns the rendered settings HTML, which will be inserted into the content |
||||
| 157 | * block on the settings page. |
||||
| 158 | * |
||||
| 159 | * @return string The rendered settings HTML |
||||
| 160 | */ |
||||
| 161 | public function getSettingsResponse() |
||||
| 162 | { |
||||
| 163 | $url = UrlHelper::cpUrl('twitter/settings'); |
||||
| 164 | |||||
| 165 | Craft::$app->controller->redirect($url); |
||||
| 166 | |||||
| 167 | return ''; |
||||
| 168 | } |
||||
| 169 | } |
||||
| 170 |