Skip to content

Commit

Permalink
First release
Browse files Browse the repository at this point in the history
  • Loading branch information
rbairwell committed Jan 24, 2019
0 parents commit bb6df59
Show file tree
Hide file tree
Showing 11 changed files with 674 additions and 0 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Craft Silktide Changelog

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.0 - 2019-01-24
### Added
- Initial release
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2019 Silktide Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Silktide Craft plugin for Craft CMS 3.x

Integrate [Silktide](https://www.silktide.com) with Craft CMS 3.

![Screenshot](src/icon.svg)

## Requirements

* Craft 3.0 RC1+
* PHP 7.0+
* An account with [Silktide](https://www.silktide.com)

## Installation

To install the plugin, follow these instructions.

1. Open your terminal and go to your Craft project:

cd /path/to/project

2. Then tell Composer to load the plugin:

composer require silktide/craft-silktide

3. In the Control Panel, go to Settings → Plugins and click the “Settings” button for Silktide.

4. Enter the API key as provided by Silktide.

## Silktide Craft Overview

Enables Silktide to be notified of new and updated pages on your Craft CMS 3 powered website and for you to easily edit pages within Craft from Silktide.

## Configuring Silktide Craft

1. Go to the Craft CMS admin dashboard

2. Click 'Settings' and under the 'Plugins' heading, select the 'Silktide' plugin

3. Provide your Silktide API key. This can be found in the 'Settings -> CMS' section of your Silktide account.

Brought to you by [Silktide](https://www.silktide.com)
45 changes: 45 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "silktide/craft-silktide",
"description": "Integrate Silktide with Craft",
"type": "craft-plugin",
"version": "1.0.0",
"keywords": [
"craft",
"cms",
"craftcms",
"craft-plugin",
"silktide craft",
"silktide"
],
"support": {
"docs": "https://github.com/silktide/cms-plugin-craft3/blob/master/README.md",
"issues": "https://github.com/silktide/cms-plugin-craft3/issues"
},
"license": "MIT",
"authors": [
{
"name": "silktide",
"homepage": "https://www.silktide.com"
}
],
"require": {
"php": "^7.0",
"ext-openssl": "*",
"ext-json": "*",
"craftcms/cms": "^3.0.0-RC1"
},
"autoload": {
"psr-4": {
"silktide\\craftsilktide\\": "src/"
}
},
"extra": {
"name": "Silktide",
"handle": "craft-silktide",
"hasCpSettings": true,
"hasSettings" : false,
"hasCpSection": false,
"changelogUrl": "https://raw.githubusercontent.com/silktide/cms-plugin-craft3/master/CHANGELOG.md",
"class": "silktide\\craftsilktide\\CraftSilktide"
}
}
235 changes: 235 additions & 0 deletions src/CraftSilktide.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
<?php
declare(strict_types=1);
/**
* Silktide Craft plugin for Craft CMS 3.x
*
* Integrate Silktide with Craft
*
* @link https://www.silktide.com
* @copyright Copyright (c) 2019 silktide
*/

namespace silktide\craftsilktide;

use Craft;
use craft\base\Plugin;
use craft\elements\Entry;
use craft\events\TemplateEvent;
use craft\web\View;
use silktide\craftsilktide\models\Settings;
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.
*
* 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
*
* @author silktide
* @package craftsilktide
* @since 1.0.0
*
*/
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
*
* @var craftSilktide
*/
public static $plugin;

// Public Properties
// =========================================================================

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

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

/**
* 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.
*
* 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()
{
parent::init();
self::$plugin = $this;

if (!$this->isInstalled) {
return;
}

if (!$this->isInstalled) {
return;
}
// check if we've been configured, and if we haven't then return.
$apiKey = $this->getSettings()->apiKey;
if ($apiKey === '') {
Craft::info(
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([
'apiKey' => $apiKey,
'urls' => $sender->url,
'userAgent' => 'SilktideCraft/' .
$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() &&
!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);
}
}
}

});
}

/**
* Created the encrypted meta tag.
*
* @param string $apiKey
* @param string $editLink
*/
protected function createMetaTag(string $apiKey, string $editLink)
{
$ivlen = openssl_cipher_iv_length('AES-256-CBC');
$iv = openssl_random_pseudo_bytes($ivlen);
if ($iv === false) {
return;
}
$ciphertext_raw = openssl_encrypt(
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)
]
);
}

/**
* Creates and returns the model used to store the plugin’s settings.
*
* @return Settings
*/
protected function createSettingsModel(): Settings
{
return new Settings();
}

/**
* Returns the rendered settings HTML, which will be inserted into the content
* block on the settings page.
*
* @return string The rendered settings HTML
*
* @throws \Twig_Error_Loader
* @throws \yii\base\Exception
*/
protected function settingsHtml(): string
{
return Craft::$app->view->renderTemplate(
'craft-silktide/_settings',
[
'settings' => $this->getSettings()
]
);
}


}
Loading

0 comments on commit bb6df59

Please sign in to comment.