New hooks for plugins and Sitemap

This commit is contained in:
dignajar 2016-01-07 20:43:09 -03:00
parent ad1b85efdf
commit 01dc40f442
22 changed files with 264 additions and 140 deletions

0
content/sitemap.xml Normal file
View File

View File

@ -61,7 +61,7 @@ if(!defined('JSON_PRETTY_PRINT')) {
define('JSON', function_exists('json_encode')); define('JSON', function_exists('json_encode'));
// Database format date // Database format date
define('DB_DATE_FORMAT', 'Y-m-d H:i'); define('DB_DATE_FORMAT', 'Y-m-d H:i:s');
// Charset, default UTF-8. // Charset, default UTF-8.
define('CHARSET', 'UTF-8'); define('CHARSET', 'UTF-8');

View File

@ -1,5 +1,9 @@
<?php defined('BLUDIT') or die('Bludit CMS.'); <?php defined('BLUDIT') or die('Bludit CMS.');
// ============================================================================
// Check role
// ============================================================================
// ============================================================================ // ============================================================================
// Functions // Functions
// ============================================================================ // ============================================================================
@ -13,11 +17,17 @@ function editPage($args)
$args['parent'] = NO_PARENT_CHAR; $args['parent'] = NO_PARENT_CHAR;
} }
// Edit the page. // Add the page, if the $key is FALSE the creation of the post failure.
if( $dbPages->edit($args) ) $key = $dbPages->edit($args);
if($key)
{ {
$dbPages->regenerateCli(); $dbPages->regenerateCli();
// Call the plugins after page created.
Theme::plugins('afterPageModify');
// Alert the user
Alert::set($Language->g('The changes have been saved')); Alert::set($Language->g('The changes have been saved'));
Redirect::page('admin', 'edit-page/'.$args['slug']); Redirect::page('admin', 'edit-page/'.$args['slug']);
} }
@ -34,6 +44,9 @@ function deletePage($key)
if( $dbPages->delete($key) ) if( $dbPages->delete($key) )
{ {
// Call the plugins after post created.
Theme::plugins('afterPageDelete');
Alert::set($Language->g('The page has been deleted successfully')); Alert::set($Language->g('The page has been deleted successfully'));
Redirect::page('admin', 'manage-pages'); Redirect::page('admin', 'manage-pages');
} }

View File

@ -13,12 +13,18 @@ function editPost($args)
global $dbPosts; global $dbPosts;
global $Language; global $Language;
// Edit the post. // Add the page, if the $key is FALSE the creation of the post failure.
if( $dbPosts->edit($args) ) $key = $dbPosts->edit($args);
if($key)
{ {
// Reindex tags, this function is in 70.posts.php // Reindex tags, this function is in 70.posts.php
reIndexTagsPosts(); reIndexTagsPosts();
// Call the plugins after post created.
Theme::plugins('afterPostModify');
// Alert the user
Alert::set($Language->g('The changes have been saved')); Alert::set($Language->g('The changes have been saved'));
Redirect::page('admin', 'edit-post/'.$args['slug']); Redirect::page('admin', 'edit-post/'.$args['slug']);
} }
@ -40,6 +46,9 @@ function deletePost($key)
// Reindex tags, this function is in 70.posts.php // Reindex tags, this function is in 70.posts.php
reIndexTagsPosts(); reIndexTagsPosts();
// Call the plugins after post created.
Theme::plugins('afterPostDelete');
Alert::set($Language->g('The post has been deleted successfully')); Alert::set($Language->g('The post has been deleted successfully'));
Redirect::page('admin', 'manage-posts'); Redirect::page('admin', 'manage-posts');
} }

View File

@ -13,9 +13,15 @@ function addPage($args)
global $dbPages; global $dbPages;
global $Language; global $Language;
// Add the page. // Add the page, if the $key is FALSE the creation of the post failure.
if( $dbPages->add($args) ) $key = $dbPages->add($args);
if($key)
{ {
// Call the plugins after page created.
Theme::plugins('afterPageCreate');
// Alert the user
Alert::set($Language->g('Page added successfully')); Alert::set($Language->g('Page added successfully'));
Redirect::page('admin', 'manage-pages'); Redirect::page('admin', 'manage-pages');
} }

View File

@ -13,12 +13,18 @@ function addPost($args)
global $dbPosts; global $dbPosts;
global $Language; global $Language;
// Add the page. // Add the page, if the $key is FALSE the creation of the post failure.
if( $dbPosts->add($args) ) $key = $dbPosts->add($args);
if($key)
{ {
// Reindex tags, this function is in 70.posts.php // Reindex tags, this function is in 70.posts.php
reIndexTagsPosts(); reIndexTagsPosts();
// Call the plugins after post created.
Theme::plugins('afterPostCreate');
// Alert for the user
Alert::set($Language->g('Post added successfully')); Alert::set($Language->g('Post added successfully'));
Redirect::page('admin', 'manage-posts'); Redirect::page('admin', 'manage-posts');
} }

View File

@ -40,9 +40,9 @@ if( $layout['slug']==='ajax' )
else else
{ {
// Boot rules // Boot rules
include(PATH_RULES.'60.plugins.php');
include(PATH_RULES.'70.posts.php'); include(PATH_RULES.'70.posts.php');
include(PATH_RULES.'70.pages.php'); include(PATH_RULES.'70.pages.php');
include(PATH_RULES.'80.plugins.php');
include(PATH_RULES.'99.header.php'); include(PATH_RULES.'99.header.php');
include(PATH_RULES.'99.paginator.php'); include(PATH_RULES.'99.paginator.php');
include(PATH_RULES.'99.themes.php'); include(PATH_RULES.'99.themes.php');

View File

@ -61,7 +61,6 @@ define('ALERT_STATUS_FAIL', 1);
// Salt length // Salt length
define('THUMBNAILS_WIDTH', 400); define('THUMBNAILS_WIDTH', 400);
define('THUMBNAILS_HEIGHT', 400); define('THUMBNAILS_HEIGHT', 400);
define('THUMBNAILS_AMOUNT', 6); define('THUMBNAILS_AMOUNT', 6);
// Salt length // Salt length
@ -82,9 +81,12 @@ define('JSON', function_exists('json_encode'));
// TRUE if new posts hand-made set published, or FALSE for draft. // TRUE if new posts hand-made set published, or FALSE for draft.
define('CLI_STATUS', 'published'); define('CLI_STATUS', 'published');
// Database format date // Database date format
define('DB_DATE_FORMAT', 'Y-m-d H:i:s'); define('DB_DATE_FORMAT', 'Y-m-d H:i:s');
// Sitemap date format
define('SITEMAP_DATE_FORMAT', 'Y-m-d');
// Date format for Dashboard schedule posts // Date format for Dashboard schedule posts
define('SCHEDULED_DATE_FORMAT', 'd M - h:i a'); define('SCHEDULED_DATE_FORMAT', 'd M - h:i a');

View File

@ -24,6 +24,15 @@ $plugins = array(
'beforeAdminLoad'=>array(), 'beforeAdminLoad'=>array(),
'afterAdminLoad'=>array(), 'afterAdminLoad'=>array(),
'beforeRulesLoad'=>array(),
'afterPostCreate'=>array(),
'afterPostModify'=>array(),
'afterPostDelete'=>array(),
'afterPageCreate'=>array(),
'afterPageModify'=>array(),
'afterPageDelete'=>array(),
'loginHead'=>array(), 'loginHead'=>array(),
'loginBodyBegin'=>array(), 'loginBodyBegin'=>array(),
'loginBodyEnd'=>array(), 'loginBodyEnd'=>array(),

View File

@ -23,7 +23,7 @@ function sortPages($a, $b)
return ($a->position() < $b->position()) ? -1 : 1; return ($a->position() < $b->position()) ? -1 : 1;
} }
function build_page($key) function buildPage($key)
{ {
global $dbPages; global $dbPages;
global $dbUsers; global $dbUsers;
@ -38,7 +38,7 @@ function build_page($key)
} }
// Page database, content from DATABASE JSON. // Page database, content from DATABASE JSON.
$db = $dbPages->getDb($key); $db = $dbPages->getPageDB($key);
if( !$db ) { if( !$db ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the page from database with key: '.$key); Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the page from database with key: '.$key);
return false; return false;
@ -73,19 +73,22 @@ function build_page($key)
return $Page; return $Page;
} }
function build_all_pages() function buildAllPages()
{ {
global $pages; global $pages;
global $pagesParents; global $pagesParents;
global $dbPages; global $dbPages;
$list = $dbPages->getAll(); $list = $dbPages->getDB();
// Clean pages array.
$pages = array();
unset($list['error']); unset($list['error']);
foreach($list as $key=>$db) foreach($list as $key=>$db)
{ {
$Page = build_page($key); $Page = buildPage($key);
if($Page!==false) if($Page!==false)
{ {
@ -144,7 +147,7 @@ if( $Site->cliMode() ) {
// Filter by page, then build it // Filter by page, then build it
if( ($Url->whereAmI()==='page') && ($Url->notFound()===false) ) if( ($Url->whereAmI()==='page') && ($Url->notFound()===false) )
{ {
$Page = build_page( $Url->slug() ); $Page = buildPage( $Url->slug() );
if($Page===false) if($Page===false)
{ {
@ -165,7 +168,7 @@ if($Url->notFound()===false)
{ {
$Url->setWhereAmI('page'); $Url->setWhereAmI('page');
$Page = build_page( $Site->homepage() ); $Page = buildPage( $Site->homepage() );
if($Page===false) { if($Page===false) {
$Url->setWhereAmI('home'); $Url->setWhereAmI('home');
@ -180,4 +183,4 @@ if($Url->notFound())
} }
// Build all pages // Build all pages
build_all_pages(); buildAllPages();

View File

@ -42,7 +42,7 @@ function buildPost($key)
} }
// Post database, content from DATABASE JSON. // Post database, content from DATABASE JSON.
$db = $dbPosts->getDb($key); $db = $dbPosts->getPostDB($key);
if( !$db ) { if( !$db ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the post from database with key: '.$key); Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the post from database with key: '.$key);
return false; return false;

View File

@ -1,9 +1,14 @@
<?php defined('BLUDIT') or die('Bludit CMS.'); <?php defined('BLUDIT') or die('Bludit CMS.');
// Boot rules // Load plugins rules
include(PATH_RULES.'60.plugins.php');
// Plugins before rules loaded, except plugins rules
Theme::plugins('beforeRulesLoad');
// Load rules
include(PATH_RULES.'70.posts.php'); include(PATH_RULES.'70.posts.php');
include(PATH_RULES.'70.pages.php'); include(PATH_RULES.'70.pages.php');
include(PATH_RULES.'80.plugins.php');
include(PATH_RULES.'99.header.php'); include(PATH_RULES.'99.header.php');
include(PATH_RULES.'99.paginator.php'); include(PATH_RULES.'99.paginator.php');
include(PATH_RULES.'99.themes.php'); include(PATH_RULES.'99.themes.php');

View File

@ -95,7 +95,7 @@ class dbPages extends dbJSON
return false; return false;
} }
return true; return $key;
} }
public function edit($args) public function edit($args)
@ -183,7 +183,7 @@ class dbPages extends dbJSON
return false; return false;
} }
return true; return $newKey;
} }
public function delete($key) public function delete($key)
@ -215,7 +215,7 @@ class dbPages extends dbJSON
} }
// Return an array with the database for a page, FALSE otherwise. // Return an array with the database for a page, FALSE otherwise.
public function getDb($key) public function getPageDB($key)
{ {
if($this->pageExists($key)) { if($this->pageExists($key)) {
return $this->db[$key]; return $this->db[$key];
@ -288,8 +288,8 @@ class dbPages extends dbJSON
return $newKey; return $newKey;
} }
// Return an array with all databases. // Returns the database
public function getAll() public function getDB()
{ {
return $this->db; return $this->db;
} }

View File

@ -35,8 +35,14 @@ class dbPosts extends dbJSON
return $this->numberPosts['published']; return $this->numberPosts['published'];
} }
// Returns the database
public function getDB()
{
return $this->db;
}
// Return an array with the post's database, FALSE otherwise. // Return an array with the post's database, FALSE otherwise.
public function getDb($key) public function getPostDB($key)
{ {
if($this->postExists($key)) { if($this->postExists($key)) {
return $this->db[$key]; return $this->db[$key];
@ -45,7 +51,7 @@ class dbPosts extends dbJSON
return false; return false;
} }
public function setDb($key, $field, $value) public function setPostDb($key, $field, $value)
{ {
if($this->postExists($key)) { if($this->postExists($key)) {
$this->db[$key][$field] = $value; $this->db[$key][$field] = $value;
@ -176,7 +182,7 @@ class dbPosts extends dbJSON
return false; return false;
} }
return true; return $key;
} }
public function edit($args) public function edit($args)

View File

@ -131,103 +131,6 @@ class Theme {
return $tmp; return $tmp;
} }
// OLD
public static function url($relative = true)
{
if($relative)
return HTML_PATH_ROOT;
else
return BLOG_URL;
}
public static function name()
{
global $settings;
return $settings['name'];
}
public static function slogan()
{
global $settings;
return $settings['slogan'];
}
public static function footer()
{
global $settings;
return $settings['footer'];
}
public static function language()
{
global $settings;
$lang = explode("_",$settings['locale']);
return $lang[0];
}
public static function locale()
{
global $settings;
return $settings['locale'];
}
public static function meta_tags()
{
global $layout;
global $seo;
// The validator W3C doesn't support???
//$meta = '<meta charset="UTF-8">'.PHP_EOL;
$meta = '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'.PHP_EOL;
if(!empty($layout['title']))
$meta .= '<title>'.$layout['title'].'</title>'.PHP_EOL;
if(!empty($layout['description']))
$meta .= '<meta name="description" content="'.$layout['description'].'">'.PHP_EOL;
if(!empty($layout['generator']))
$meta .= '<meta name="generator" content="'.$layout['generator'].'">'.PHP_EOL;
if(!empty($layout['keywords']))
$meta .= '<meta name="keywords" content="'.$layout['keywords'].'">'.PHP_EOL;
if(!empty($layout['author']))
{
if(filter_var($layout['author'], FILTER_VALIDATE_URL))
$meta .= '<link rel="author" href="'.$layout['author'].'">'.PHP_EOL;
else
$meta .= '<meta name="author" content="'.$layout['author'].'">'.PHP_EOL;
}
if(!empty($layout['canonical']))
$meta .= '<link rel="canonical" href="'.$layout['canonical'].'">'.PHP_EOL;
if(!empty($layout['robots']))
$meta .= '<meta name="robots" content="'.$layout['robots'].'">'.PHP_EOL;
if(!empty($seo['google_code']))
$meta .= '<meta name="google-site-verification" content="'.$seo['google_code'].'">'.PHP_EOL;
if(!empty($seo['bing_code']))
$meta .= '<meta name="msvalidate.01" content="'.$seo['bing_code'].'">'.PHP_EOL;
$meta .= '<link rel="alternate" type="application/atom+xml" title="ATOM Feed" href="'.$layout['feed'].'">'.PHP_EOL;
return $meta;
}
} }
?> ?>

View File

@ -131,7 +131,7 @@ class Post extends fileContent
} }
// Returns the post date according to locale settings and format settings. // Returns the post date according to locale settings and format settings.
public function date($format=false) public function date()
{ {
return $this->getField('date'); return $this->getField('date');
} }

View File

@ -0,0 +1,12 @@
{
"plugin-data":
{
"name": "Sitemap",
"description": "This plugin generate a file sitemap.xml where you can list the web pages of your site to tell to search engines about the organization of your site content.",
"author": "Bludit",
"email": "",
"website": "https://github.com/dignajar/bludit-plugins",
"version": "1.0",
"releaseDate": "2016-01-07"
}
}

150
plugins/sitemap/plugin.php Normal file
View File

@ -0,0 +1,150 @@
<?php
class pluginSitemap extends Plugin {
public function init()
{
$this->dbFields = array(
'label'=>'Tags'
);
}
private function createXML()
{
global $Site;
global $dbPages;
global $dbPosts;
global $Url;
$doc = new DOMDocument('1.0', 'UTF-8');
// Friendly XML code
$doc->formatOutput = true;
// Create urlset element
$urlset = $doc->createElement('urlset');
$attribute = $doc->createAttribute('xmlns');
$attribute->value = 'http://www.sitemaps.org/schemas/sitemap/0.9';
$urlset->appendChild($attribute);
// --- Base URL ---
// Create url, loc and lastmod elements
$url = $doc->createElement('url');
$loc = $doc->createElement('loc', $Site->url());
$lastmod = $doc->createElement('lastmod', '');
// Append loc and lastmod -> url
$url->appendChild($loc);
$url->appendChild($lastmod);
// Append url -> urlset
$urlset->appendChild($url);
// --- Pages and Posts ---
$all = array();
$url = trim($Site->url(),'/');
// --- Pages ---
$filter = trim($Url->filters('page'),'/');
$pages = $dbPages->getDB();
foreach($pages as $key=>$db)
{
$permalink = empty($filter) ? $url.'/'.$key : $url.'/'.$filter.'/'.$key;
$date = Date::format($db['date'], DB_DATE_FORMAT, SITEMAP_DATE_FORMAT);
array_push($all, array('permalink'=>$permalink, 'date'=>$date));
}
// --- Posts ---
$filter = rtrim($Url->filters('post'),'/');
$posts = $dbPosts->getDB();
foreach($posts as $key=>$db)
{
$permalink = empty($filter) ? $url.'/'.$key : $url.'/'.$filter.'/'.$key;
$date = Date::format($db['date'], DB_DATE_FORMAT, SITEMAP_DATE_FORMAT);
array_push($all, array('permalink'=>$permalink, 'date'=>$date));
}
// Generate the XML for posts and pages
foreach($all as $db)
{
// Create url, loc and lastmod elements
$url = $doc->createElement('url');
$loc = $doc->createElement('loc', $db['permalink']);
$lastmod = $doc->createElement('lastmod', $db['date']);
// Append loc and lastmod -> url
$url->appendChild($loc);
$url->appendChild($lastmod);
// Append url -> urlset
$urlset->appendChild($url);
}
// Append urlset -> XML
$doc->appendChild($urlset);
$doc->save(PATH_PLUGINS_DATABASES.$this->directoryName.DS.'sitemap.xml');
}
public function install($position = 0)
{
parent::install($position);
$this->createXML();
}
public function afterPostCreate()
{
$this->createXML();
}
public function afterPageCreate()
{
$this->createXML();
}
public function afterPostModify()
{
$this->createXML();
}
public function afterPageModify()
{
$this->createXML();
}
public function afterPostDelete()
{
$this->createXML();
}
public function afterPageDelete()
{
$this->createXML();
}
public function beforeRulesLoad()
{
global $Url;
if( $Url->uri() === HTML_PATH_ROOT.'sitemap.xml' )
{
// Send XML header
header('Content-type: text/xml');
// New DOM document
$doc = new DOMDocument();
// Load XML
$doc->load(PATH_PLUGINS_DATABASES.$this->directoryName.DS.'sitemap.xml');
// Print the XML
echo $doc->saveXML();
// Stop Bludit running
exit;
}
}
}

View File

@ -51,4 +51,4 @@
<?php Theme::plugins('siteBodyEnd') ?> <?php Theme::plugins('siteBodyEnd') ?>
</body> </body>
</html> </html>

View File

@ -1,4 +1,4 @@
<h1 class="subhead"><?php echo $Language->get('Recent posts') ?></h1> <div class="subhead"><?php echo $Language->get('Recent posts') ?></div>
<?php foreach ($posts as $Post): ?> <?php foreach ($posts as $Post): ?>
@ -11,9 +11,9 @@
<header class="post-header"> <header class="post-header">
<!-- Post title --> <!-- Post title -->
<h2 class="post-title"> <h1 class="post-title">
<a href="<?php echo $Post->permalink() ?>"><?php echo $Post->title() ?></a> <a href="<?php echo $Post->permalink() ?>"><?php echo $Post->title() ?></a>
</h2> </h1>
<!-- Post date and author --> <!-- Post date and author -->
<div class="post-meta"> <div class="post-meta">

View File

@ -1,4 +1,4 @@
<h1 class="subhead"><?php echo $Language->get('Page') ?></h1> <div class="subhead"><?php echo $Language->get('Page') ?></div>
<section class="page"> <section class="page">
@ -9,9 +9,9 @@
<header class="page-header"> <header class="page-header">
<!-- page title --> <!-- page title -->
<h2 class="page-title"> <h1 class="page-title">
<a href="<?php echo $Page->permalink() ?>"><?php echo $Page->title() ?></a> <a href="<?php echo $Page->permalink() ?>"><?php echo $Page->title() ?></a>
</h2> </h1>
</header> </header>

View File

@ -1,4 +1,4 @@
<h1 class="subhead"><?php echo $Language->get('Post') ?></h1> <div class="subhead"><?php echo $Language->get('Post') ?></div>
<section class="post"> <section class="post">
@ -9,9 +9,9 @@
<header class="post-header"> <header class="post-header">
<!-- Post title --> <!-- Post title -->
<h2 class="post-title"> <h1 class="post-title">
<a href="<?php echo $Post->permalink() ?>"><?php echo $Post->title() ?></a> <a href="<?php echo $Post->permalink() ?>"><?php echo $Post->title() ?></a>
</h2> </h1>
<!-- Post date and author --> <!-- Post date and author -->
<div class="post-meta"> <div class="post-meta">