Skip to content

Commit

Permalink
Add logs/fix issue with logs
Browse files Browse the repository at this point in the history
  • Loading branch information
rbairwell committed Feb 8, 2019
1 parent bb6df59 commit a27dc0c
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 123 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 1.0.1 - 2019-02-08
## Fixed
- Additional logs

## 1.0.0 - 2019-01-24
### Added
- Initial release
158 changes: 78 additions & 80 deletions src/CraftSilktide.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
use yii\base\Event;

/**
* Craft plugins are very much like little applications in and of themselves. We’ve made
* it as simple as we can, but the training wheels are off. A little prior knowledge is
* going to be required to write a plugin.
* Craft plugins are very much like little applications in and of themselves.
* We’ve made it as simple as we can, but the training wheels are off. A little
* prior knowledge is going to be required to write a plugin.
*
* For the purposes of the plugin docs, we’re going to assume that you know PHP and SQL,
* as well as some semi-advanced concepts like object-oriented programming and PHP namespaces.
* For the purposes of the plugin docs, we’re going to assume that you know PHP
* and SQL, as well as some semi-advanced concepts like object-oriented
* programming and PHP namespaces.
*
* https://craftcms.com/docs/plugins/introduction
*
Expand All @@ -36,12 +37,13 @@
*/
class CraftSilktide extends Plugin
{

// Static Properties
// =========================================================================

/**
* Static property that is an instance of this plugin class so that it can be accessed via
* CraftSilktide::$plugin
* Static property that is an instance of this plugin class so that it can
* be accessed via CraftSilktide::$plugin
*
* @var craftSilktide
*/
Expand All @@ -51,24 +53,25 @@ class CraftSilktide extends Plugin
// =========================================================================

/**
* To execute your plugin’s migrations, you’ll need to increase its schema version.
* To execute your plugin’s migrations, you’ll need to increase its schema
* version.
*
* @var string
*/
public $schemaVersion = '1.0.0';
public $schemaVersion = '1.0.1';

// Public Methods
// =========================================================================

/**
* Set our $plugin static property to this class so that it can be accessed via
* craftsilktide::$plugin
* Set our $plugin static property to this class so that it can be accessed
* via craftsilktide::$plugin
*
* Called after the plugin class is instantiated; do any one-time initialization
* here such as hooks and events.
* Called after the plugin class is instantiated; do any one-time
* initialization here such as hooks and events.
*
* If you have a '/vendor/autoload.php' file, it will be loaded for you automatically;
* you do not need to load it in your init() method.
* If you have a '/vendor/autoload.php' file, it will be loaded for you
* automatically; you do not need to load it in your init() method.
*
*/
public function init()
Expand All @@ -87,84 +90,79 @@ public function init()
$apiKey = $this->getSettings()->apiKey;
if ($apiKey === '') {
Craft::info(
Craft::t(
'craft-silktide',
'{name} plugin loaded, but not configured',
['name' => $this->name]
),
__METHOD__
Craft::t(
'craft-silktide',
'{name} plugin loaded, but not configured',
['name' => $this->name]
),
__METHOD__
);
return;
}

$this->initSaveEntry($apiKey);

$this->initMetaTag($apiKey);

Craft::info(
Craft::t(
'craft-silktide',
'{name} plugin loaded',
['name' => $this->name]
),
__METHOD__
);
}

// Protected Methods
// =========================================================================

/**
* Setup our save event.
*
* @param string $apiKey
*/
protected function initSaveEntry(string $apiKey)
{
Event::on(Entry::class, Entry::EVENT_AFTER_SAVE, function (Event $e) use ($apiKey) {
/* @var Entry $sender */
$sender = $e->sender;
if ($sender->status === Entry::STATUS_LIVE) {
/**
* If we have an entry which has been saved and is 'live', send notification to silktide.
*/
Craft::$app->getQueue()->push(new CraftSilktideJob([
Event::on(Entry::class, Entry::EVENT_AFTER_SAVE,
function (Event $e) use ($apiKey) {
/* @var Entry $sender */
$sender = $e->sender;
if ($sender->status === Entry::STATUS_LIVE) {
/**
* If we have an entry which has been saved and is 'live', send notification to silktide.
*/
Craft::$app->getQueue()->push(new CraftSilktideJob([
'apiKey' => $apiKey,
'urls' => $sender->url,
'urls' => [$sender->url],
'userAgent' => 'SilktideCraft/' .
$this->getVersion() .
' (compatible; CraftCMS ' .
Craft::$app->getVersion() .
')'
]));
}
});
$this->getVersion() .
' (compatible; CraftCMS ' .
Craft::$app->getVersion() .
')',
]));
}
});
}

/**
* Setup the meta tag event.
*
* @param string $apiKey
*/
protected function initMetaTag(string $apiKey)
{
Event::on(View::class, View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE, function (TemplateEvent $e) use ($apiKey) {
// make sure we are public view only
if (Craft::$app->request->getIsSiteRequest() &&
Event::on(View::class, View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE,
function (TemplateEvent $e) use ($apiKey) {
// make sure we are public view only
if (Craft::$app->request->getIsSiteRequest() &&
!Craft::$app->request->getIsCpRequest() &&
!Craft::$app->request->getIsConsoleRequest() &&
!(Craft::$app->request->hasMethod('getIsAjax') && Craft::$app->request->getIsAjax()) &&
!(Craft::$app->request->hasMethod('getIsLivePreview') && Craft::$app->request->getIsLivePreview())
) {
// get current page element
$element = Craft::$app->urlManager->getMatchedElement();
if (!empty($element)) {
$editLink = $element->getCpEditUrl();
if ($editLink !== null) {
$this->createMetaTag($apiKey, $editLink);
}
}
}

});
) {
// get current page element
$element = Craft::$app->urlManager->getMatchedElement();
if (!empty($element)) {
$editLink = $element->getCpEditUrl();
if ($editLink !== null) {
$this->createMetaTag($apiKey, $editLink);
}
}
}

});
}

/**
Expand All @@ -181,24 +179,24 @@ protected function createMetaTag(string $apiKey, string $editLink)
return;
}
$ciphertext_raw = openssl_encrypt(
json_encode(
[
'editorUrl' => $editLink
]
),
'AES-256-CBC',
$apiKey,
OPENSSL_RAW_DATA,
$iv
json_encode(
[
'editorUrl' => $editLink,
]
),
'AES-256-CBC',
$apiKey,
OPENSSL_RAW_DATA,
$iv
);

$hmac = hash_hmac('sha256', $ciphertext_raw, $apiKey, true);
$ciphertext = base64_encode($iv . $hmac . $ciphertext_raw);
Craft::$app->getView()->registerMetaTag(
[
'name' => 'silktide-cms',
'content' => htmlspecialchars($ciphertext, ENT_QUOTES)
]
[
'name' => 'silktide-cms',
'content' => htmlspecialchars($ciphertext, ENT_QUOTES),
]
);
}

Expand All @@ -213,8 +211,8 @@ protected function createSettingsModel(): Settings
}

/**
* Returns the rendered settings HTML, which will be inserted into the content
* block on the settings page.
* Returns the rendered settings HTML, which will be inserted into the
* content block on the settings page.
*
* @return string The rendered settings HTML
*
Expand All @@ -224,10 +222,10 @@ protected function createSettingsModel(): Settings
protected function settingsHtml(): string
{
return Craft::$app->view->renderTemplate(
'craft-silktide/_settings',
[
'settings' => $this->getSettings()
]
'craft-silktide/_settings',
[
'settings' => $this->getSettings(),
]
);
}

Expand Down
Loading

0 comments on commit a27dc0c

Please sign in to comment.