Skip to content

Commit

Permalink
Beta release
Browse files Browse the repository at this point in the history
  • Loading branch information
hissy committed Oct 23, 2016
1 parent eec8ac1 commit 6528803
Show file tree
Hide file tree
Showing 16 changed files with 493 additions and 108 deletions.
4 changes: 4 additions & 0 deletions blocks/content/templates/amp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
defined('C5_EXECUTE') or die("Access Denied.");

echo \Concrete\Package\Amp\Amp\Converter::content($controller);
22 changes: 22 additions & 0 deletions blocks/image/templates/amp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php defined('C5_EXECUTE') or die("Access Denied.");

if (is_object($f)) {
$tag = new \Concrete\Package\Amp\Html\Object\AmpImg($f);
if ($altText) {
$tag->alt(h($altText));
} else {
$tag->alt('');
}
if ($title) {
$tag->title(h($title));
}
if ($linkURL) {
print '<a href="' . $linkURL . '">';
}

print $tag;

if ($linkURL) {
print '</a>';
}
}
2 changes: 1 addition & 1 deletion blocks/video/templates/amp.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?
<?php
defined('C5_EXECUTE') or die("Access Denied.");

$c = Page::getCurrentPage();
Expand Down
9 changes: 9 additions & 0 deletions blocks/youtube/templates/amp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php defined('C5_EXECUTE') or die("Access Denied.");
$tag = \HtmlObject\Element::create('amp-youtube');
$tag->setAttribute('data-videoid', h($videoID));
$tag->setAttribute('layout', 'responsive');
$width = ($vWidth) ? $vWidth : '480';
$height = ($vHeight) ? $vHeight : '270';
$tag->setAttribute('width', h($width));
$tag->setAttribute('height', h($height));
echo $tag;
20 changes: 20 additions & 0 deletions config/install.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<concrete5-cif version="1.0">
<themes>
<theme handle="amp" package="amp"/>
</themes>
<singlepages>
<page name="AMP"
path="/dashboard/system/seo/amp"
filename="/dashboard/system/seo/amp.php"
pagetype=""
description=""
package="amp">
<attributes>
<attributekey handle="meta_keywords">
<value>Accelerated Mobile Pages, AMP, Analytics</value>
</attributekey>
</attributes>
</page>
</singlepages>
</concrete5-cif>
23 changes: 19 additions & 4 deletions controller.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Concrete\Package\Amp;

use Concrete\Core\Backup\ContentImporter;
use PageTheme;
use Core;

Expand All @@ -10,26 +11,40 @@ class Controller extends \Concrete\Core\Package\Package
protected $appVersionRequired = '5.7.5';
protected $pkgVersion = '0.1';
protected $pkgAutoloaderMapCoreExtensions = true;
protected $pkgAutoloaderRegistries = array(
'src/NotnilCreative' => 'NotnilCreative',
);

/**
* Returns the translated package name.
*
* @return string
*/
public function getPackageName()
{
return t('Accelerated Mobile Pages (AMP)');
}

/**
* Returns the translated package description.
*
* @return string
*/
public function getPackageDescription()
{
return t('Make your site ready for Accelerated Mobile Pages (AMP).');
}

/**
* Install process of the package.
*/
public function install()
{
$pkg = parent::install();
PageTheme::add('amp', $pkg);
$ci = new ContentImporter();
$ci->importContentFile($pkg->getPackagePath() . '/config/install.xml');
}

/**
* Startup process of the package.
*/
public function on_start()
{
Core::bind('\PageController', 'Concrete\Package\Amp\Page\Controller\PageController');
Expand Down
56 changes: 56 additions & 0 deletions controllers/single_page/dashboard/system/seo/amp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
namespace Concrete\Package\Amp\Controller\SinglePage\Dashboard\System\Seo;

use Core;
use Package;
use PageType;

class Amp extends \Concrete\Core\Page\Controller\DashboardPageController
{
public function view()
{
/** @var \Concrete\Core\Package\Package $pkg */
$pkg = Package::getByHandle('amp');
$account = $pkg->getFileConfig()->get('analytics.googleanalytics.account');
$this->set('account', $account);

$selectedTypes = $pkg->getFileConfig()->get('types');
$this->set('types', $selectedTypes);

$ptIDs = array();
$pagetypes = PageType::getList();
/** @var PageType $pt */
foreach ($pagetypes as $pt) {
$ptIDs[$pt->getPageTypeID()] = $pt->getPageTypeDisplayName();
}
$this->set('pagetypes', $ptIDs);

$this->requireAsset('select2');

$this->set('pageTitle', t('Accelerated Mobile Pages (AMP)'));
}

public function settings_saved()
{
$this->set('message', t('Settings saved.'));
$this->view();
}

public function update_settings()
{
if ($this->token->validate("update_settings")) {
if ($this->isPost()) {
/** @var \Concrete\Core\Package\Package $pkg */
$pkg = Package::getByHandle('amp');
$pkg->getFileConfig()->save('analytics.googleanalytics.account', $this->post('account'));
if (is_array($this->post('types'))) {
$pkg->getFileConfig()->save('types', $this->post('types'));
}
$this->redirect('/dashboard/system/seo/amp', 'settings_saved');
}
} else {
$this->error->add($this->token->getErrorMessage());
}
$this->view();
}
}
52 changes: 37 additions & 15 deletions elements/header_required.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
defined('C5_EXECUTE') or die("Access Denied.");

/** @var \Concrete\Core\Page\Page $c */
$c = Page::getCurrentPage();
if (is_object($c)) {
$cp = new Permissions($c);
Expand Down Expand Up @@ -46,6 +47,13 @@

$cID = $c->getCollectionID();

if (!isset($image)) {
$thumbnail = $c->getAttribute('thumbnail');
if ($thumbnail instanceof File && !$thumbnail->isError()) {
$image = $thumbnail->getURL();
}
}

} else {
$cID = 1;
if (!isset($pageTitle)) {
Expand All @@ -55,22 +63,36 @@
?>
<meta charset="utf-8">
<title><?=h($pageTitle)?></title>
<meta name="description" content="<?=h($pageDescription)?>" />
<?php
$akk = $c->getCollectionAttributeValue('meta_keywords');
if ($akk) { ?>
<meta name="keywords" content="<?=h($akk)?>" />
<?php }
$canonical_link = $c->getCollectionLink(true);
echo new \Concrete\Core\Html\Object\HeadLink($canonical_link, 'canonical');
?>
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<script type="application/ld+json">
<?php
/** @var \Concrete\Core\Localization\Service\Date $date */
$date = Core::make('helper/date');

if($c->getCollectionAttributeValue('exclude_search_index')) {
echo '<meta name="robots" content="noindex" />';
}
$jsonLD = array(
"@context" => "http://schema.org",
"@type" => "NewsArticle",
"mainEntityOfPage" => $canonical_link,
"headline" => $pageDescription,
"datePublished" => $date->formatCustom('c', $c->getCollectionDatePublic()),
"dateModified" => $date->formatCustom('c', $c->getCollectionDateLastModified()),
);

if (Config::get('concrete.misc.app_version_display_in_header')) {
echo '<meta name="generator" content="concrete5 - ' . APP_VERSION . '" />';
} else {
echo '<meta name="generator" content="concrete5" />';
}
if (isset($image)) {
$jsonLD['image'] = array($image);
}

$canonical_link = $c->getCollectionLink(true);
echo new \Concrete\Core\Html\Object\HeadLink($canonical_link, 'canonical');
if (is_array($headers)) {
$jsonLD = $headers + $jsonLD;
}
echo json_encode($jsonLD);
?>
</script>
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<?php
$v = View::getInstance();
$v->markHeaderAssetPosition();
31 changes: 31 additions & 0 deletions single_pages/dashboard/system/seo/amp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php defined('C5_EXECUTE') or die("Access Denied.");?>

<form action="<?php echo $this->action('update_settings')?>" method="post">
<?php echo $this->controller->token->output('update_settings')?>
<fieldset>
<legend><?php echo t('Basic'); ?></legend>
<div class="form-group">
<?php echo $form->label('types', t('Which Page Types should serve AMP Page?')); ?>
<div style="width: 100%">
<?php echo $form->selectMultiple('types', $pagetypes, $types, array('style' => 'width: 100%')); ?>
</div>
</div>
</fieldset>
<fieldset>
<legend><?php echo t('Google Analytics'); ?></legend>
<div class="form-group">
<?php echo $form->label('account', t('Property ID')); ?>
<?php echo $form->text('account', $account, array('placeholder' => 'UA-XXXXX-Y')); ?>
</div>
</fieldset>
<div class="ccm-dashboard-form-actions-wrapper">
<div class="ccm-dashboard-form-actions">
<button class="pull-right btn btn-success" type="submit" ><?php echo t('Save')?></button>
</div>
</div>
</form>
<script type="text/javascript">
$(function() {
$('#types').removeClass('form-control').select2();
});
</script>
33 changes: 33 additions & 0 deletions src/Concrete/Amp/Converter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace Concrete\Package\Amp\Amp;

use Concrete\Block\Content\Controller as ContentBlockController;
use Concrete\Core\Editor\LinkAbstractor;
use Concrete\Package\Amp\Html\Object\AmpImg;
use File;
use Sunra\PhpSimple\HtmlDomParser;

class Converter
{
public static function content(ContentBlockController $controller)
{
$content = $controller->getSearchableContent();

$dom = new HtmlDomParser();
$r = $dom->str_get_html($content, true, true, DEFAULT_TARGET_CHARSET, false);
if (is_object($r)) {
foreach ($r->find('concrete-picture') as $picture) {
$fID = $picture->fid;
$fo = File::getByID($fID);
if (is_object($fo)) {
$tag = new AmpImg($fo);
$tag->alt($picture->alt);
$picture->outertext = (string) $tag;
}
}
$content = (string) $r->restore_noise($r);
}

return LinkAbstractor::translateFrom($content);
}
}
46 changes: 46 additions & 0 deletions src/Concrete/Area/AmpArea.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
namespace Concrete\Package\Amp\Area;

use Concrete\Core\Area\Area;
use Concrete\Core\Block\Block;
use Concrete\Package\Amp\View\AmpBlockView;
use Page;
use Permissions;
use View;

class AmpArea extends Area
{
private $ampSafeBlockTypes = array('image','content','video','youtube');

public function display($c = false, $alternateBlockArray = null)
{
if (!$c) {
$c = Page::getCurrentPage();
}
$v = View::getRequestInstance();

if (!is_object($c) || $c->isError()) {
return false;
}

$this->load($c);
$ap = new Permissions($this);
if (!$ap->canViewArea()) {
return false;
}

$blocksToDisplay = ($alternateBlockArray) ? $alternateBlockArray : $this->getAreaBlocksArray();

/** @var Block $b */
foreach ($blocksToDisplay as $b) {
if (in_array($b->getBlockTypeHandle(), $this->ampSafeBlockTypes)) {
$bv = new AmpBlockView($b);
$bv->setAreaObject($this);
$p = new Permissions($b);
if ($p->canViewBlock()) {
echo $bv->render('view');
}
}
}
}
}
Loading

0 comments on commit 6528803

Please sign in to comment.