use namespace

This commit is contained in:
Jean-Christian Paul Denis 2023-03-21 23:51:43 +01:00
parent 5de059b1c2
commit 596569cbb2
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
8 changed files with 407 additions and 310 deletions

View File

@ -1,34 +0,0 @@
<?php
/**
* @brief postExpired, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and Contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_ADMIN')) {
return null;
}
# -- Nothing to change below --
try {
# Check module version
if (!dcCore::app()->newVersion(
basename(__DIR__),
dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version')
)) {
return null;
}
//...
return true;
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
return false;
}

56
src/Backend.php Normal file
View File

@ -0,0 +1,56 @@
<?php
/**
* @brief postExpired, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and Contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\postExpired;
use dcAuth;
use dcCore;
use dcNsProcess;
class Backend extends dcNsProcess
{
public static function init(): bool
{
static::$init = defined('DC_CONTEXT_ADMIN') && dcCore::app()->auth->check(
dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_CONTENT_ADMIN]),
dcCore::app()->blog->id
);
return static::$init;
}
public static function process(): bool
{
if (!static::$init) {
return false;
}
dcCore::app()->addBehaviors([
'adminPostsActions' => [BackendBehaviors::class, 'adminPostsActions'],
'adminPagesActions' => [BackendBehaviors::class, 'adminPostsActions'],
'adminPostHeaders' => [BackendBehaviors::class, 'adminPostHeaders'],
'adminPageHeaders' => [BackendBehaviors::class, 'adminPostHeaders'],
'adminPostFormItems' => [BackendBehaviors::class, 'adminPostFormItems'],
'adminPageFormItems' => [BackendBehaviors::class, 'adminPostFormItems'],
'adminBeforePostDelete' => [BackendBehaviors::class, 'adminBeforePostDelete'],
'adminBeforePageDelete' => [BackendBehaviors::class, 'adminBeforePostDelete'],
'adminAfterPostUpdate' => [BackendBehaviors::class, 'adminAfterPostSave'],
'adminAfterPageUpdate' => [BackendBehaviors::class, 'adminAfterPostSave'],
'adminAfterPostCreate' => [BackendBehaviors::class, 'adminAfterPostSave'],
'adminAfterPageCreate' => [BackendBehaviors::class, 'adminAfterPostSave'],
]);
return true;
}
}

View File

@ -10,52 +10,35 @@
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { declare(strict_types=1);
return null;
}
# Check plugin version namespace Dotclear\Plugin\postExpired;
if (dcCore::app()->getVersion(basename(__DIR__)) != dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version')) {
return null;
}
# Check user right use ArrayObject;
if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([ use cursor;
dcAuth::PERMISSION_CONTENT_ADMIN, use dcCore;
]), dcCore::app()->blog->id) use dcPostsActions;
) { use dcPage;
return null; use dcRecord;
} use Exception;
use dt;
# Admin behaviors use html;
dcCore::app()->addBehaviors([ use form;
'adminPostsActions' => ['adminBehaviorPostExpired', 'adminPostsActions'], use formSelectOption;
'adminPagesActions' => ['adminBehaviorPostExpired', 'adminPostsActions'],
'adminPostHeaders' => ['adminBehaviorPostExpired', 'adminPostHeaders'],
'adminPageHeaders' => ['adminBehaviorPostExpired', 'adminPostHeaders'],
'adminPostFormItems' => ['adminBehaviorPostExpired', 'adminPostFormItems'],
'adminPageFormItems' => ['adminBehaviorPostExpired', 'adminPostFormItems'],
'adminBeforePostDelete' => ['adminBehaviorPostExpired', 'adminBeforePostDelete'],
'adminBeforePageDelete' => ['adminBehaviorPostExpired', 'adminBeforePostDelete'],
'adminAfterPostUpdate' => ['adminBehaviorPostExpired', 'adminAfterPostSave'],
'adminAfterPageUpdate' => ['adminBehaviorPostExpired', 'adminAfterPostSave'],
'adminAfterPostCreate' => ['adminBehaviorPostExpired', 'adminAfterPostSave'],
'adminAfterPageCreate' => ['adminBehaviorPostExpired', 'adminAfterPostSave'],
]);
/** /**
* @ingroup DC_PLUGIN_POSTEXPIRED * @ingroup DC_PLUGIN_POSTEXPIRED
* @brief Scheduled post change - admin methods. * @brief Scheduled post change - admin methods.
* @since 2.6 * @since 2.6
*/ */
class adminBehaviorPostExpired class BackendBehaviors
{ {
/** /**
* Add actions to posts page combo * Add actions to posts page combo
* *
* @param dcPostsActionsPage $ap dcPostsActionsPage instance * @param dcPostsActions $pa dcPostsActions instance
*/ */
public static function adminPostsActions(dcPostsActions $pa) public static function adminPostsActions(dcPostsActions $pa): void
{ {
$pa->addAction( $pa->addAction(
[ [
@ -63,7 +46,7 @@ class adminBehaviorPostExpired
__('Add expired date') => 'post_expired_add', __('Add expired date') => 'post_expired_add',
], ],
], ],
['adminBehaviorPostExpired', 'callbackAdd'] [self::class, 'callbackAdd']
); );
$pa->addAction( $pa->addAction(
@ -72,7 +55,7 @@ class adminBehaviorPostExpired
__('Remove expired date') => 'post_expired_remove', __('Remove expired date') => 'post_expired_remove',
], ],
], ],
['adminBehaviorPostExpired', 'callbackRemove'] [self::class, 'callbackRemove']
); );
} }
@ -81,9 +64,9 @@ class adminBehaviorPostExpired
* *
* @return string HTML head * @return string HTML head
*/ */
public static function adminPostHeaders() public static function adminPostHeaders(): string
{ {
return dcPage::jsModuleLoad(basename(__DIR__) . '/js/postexpired.js'); return dcPage::jsModuleLoad(My::id() . '/js/backend.js');
} }
/** /**
@ -91,19 +74,19 @@ class adminBehaviorPostExpired
* *
* @param ArrayObject $main_items Main items * @param ArrayObject $main_items Main items
* @param ArrayObject $sidebar_items Sidebar items * @param ArrayObject $sidebar_items Sidebar items
* @param record $post Post record or null * @param ?dcRecord $post Post record or null
*/ */
public static function adminPostFormItems(ArrayObject $main_items, ArrayObject $sidebar_items, $post) public static function adminPostFormItems(ArrayObject $main_items, ArrayObject $sidebar_items, ?dcRecord $post): void
{ {
if ($post === null) { if ($post === null) {
return null; return;
} }
$sidebar_items['post_expired'] = [ $sidebar_items['post_expired'] = [
'title' => __('Expired date'), 'title' => __('Expired date'),
'items' => self::fieldsPostExpired( 'items' => self::fieldsPostExpired(
$post->post_type, $post->f('post_type'),
$post->post_id (int) $post->f('post_id')
), ),
]; ];
} }
@ -113,7 +96,7 @@ class adminBehaviorPostExpired
* *
* @param integer $post_id Post id * @param integer $post_id Post id
*/ */
public static function adminBeforePostDelete($post_id) public static function adminBeforePostDelete(int $post_id): void
{ {
self::delPostExpired($post_id); self::delPostExpired($post_id);
} }
@ -124,7 +107,7 @@ class adminBehaviorPostExpired
* @param cursor $cur Current post cursor * @param cursor $cur Current post cursor
* @param integer $post_id Post id * @param integer $post_id Post id
*/ */
public static function adminAfterPostSave(cursor $cur, $post_id) public static function adminAfterPostSave(cursor $cur, int $post_id): void
{ {
self::delPostExpired($post_id); self::delPostExpired($post_id);
@ -134,7 +117,7 @@ class adminBehaviorPostExpired
|| !empty($_POST['post_expired_selected']) || !empty($_POST['post_expired_selected'])
|| !empty($_POST['post_expired_comment']) || !empty($_POST['post_expired_comment'])
|| !empty($_POST['post_expired_trackback']))) { || !empty($_POST['post_expired_trackback']))) {
self::setPostExpired($post_id, $_POST); self::setPostExpired($post_id, new ArrayObject($_POST));
} }
} }
@ -144,7 +127,7 @@ class adminBehaviorPostExpired
* @param dcPostsActions $pa dcPostsActions instance * @param dcPostsActions $pa dcPostsActions instance
* @param ArrayObject $post _POST actions * @param ArrayObject $post _POST actions
*/ */
public static function callbackAdd(dcPostsActions $pa, ArrayObject $post) public static function callbackAdd(dcPostsActions $pa, ArrayObject $post): void
{ {
# No entry # No entry
$posts_ids = $pa->getIDs(); $posts_ids = $pa->getIDs();
@ -164,7 +147,7 @@ class adminBehaviorPostExpired
self::setPostExpired($post_id, $post); self::setPostExpired($post_id, $post);
} }
dcAdminNotices::addSuccessNotice(__('Expired date added.')); dcPage::addSuccessNotice(__('Expired date added.'));
$pa->redirect(true); $pa->redirect(true);
# Display form # Display form
@ -186,7 +169,7 @@ class adminBehaviorPostExpired
'<form action="' . $pa->getURI() . '" method="post">' . '<form action="' . $pa->getURI() . '" method="post">' .
$pa->getCheckboxes() . $pa->getCheckboxes() .
implode('', self::fieldsPostExpired($posts->post_type)) . implode('', self::fieldsPostExpired($posts->f('post_type'))) .
dcCore::app()->formNonce() . dcCore::app()->formNonce() .
$pa->getHiddenFields() . $pa->getHiddenFields() .
@ -204,7 +187,7 @@ class adminBehaviorPostExpired
* @param dcPostsActions $pa dcPostsActions instance * @param dcPostsActions $pa dcPostsActions instance
* @param ArrayObject $post _POST actions * @param ArrayObject $post _POST actions
*/ */
public static function callbackRemove(dcPostsActions $pa, ArrayObject $post) public static function callbackRemove(dcPostsActions $pa, ArrayObject $post): void
{ {
# No entry # No entry
$posts_ids = $pa->getIDs(); $posts_ids = $pa->getIDs();
@ -217,7 +200,7 @@ class adminBehaviorPostExpired
self::delPostExpired($post_id); self::delPostExpired($post_id);
} }
dcAdminNotices::addSuccessNotice(__('Expired date deleted.')); dcPage::addSuccessNotice(__('Expired date deleted.'));
$pa->redirect(true); $pa->redirect(true);
} }
@ -226,18 +209,18 @@ class adminBehaviorPostExpired
* *
* @param integer $post_id Post id * @param integer $post_id Post id
*/ */
protected static function delPostExpired($post_id) protected static function delPostExpired(int $post_id): void
{ {
dcCore::app()->meta->delPostMeta($post_id, 'post_expired'); dcCore::app()->meta->delPostMeta($post_id, My::META_TYPE);
} }
/** /**
* Save expired date * Save expired date
* *
* @param integer $post_id Post id * @param integer $post_id Post id
* @param array $post _POST fields * @param ArrayObject $post _POST fields
*/ */
protected static function setPostExpired($post_id, $post) protected static function setPostExpired(int $post_id, ArrayObject $post): void
{ {
$post_expired = [ $post_expired = [
'status' => '', 'status' => '',
@ -247,7 +230,7 @@ class adminBehaviorPostExpired
'trackback' => '', 'trackback' => '',
'date' => date( 'date' => date(
'Y-m-d H:i:00', 'Y-m-d H:i:00',
strtotime($post['post_expired_date']) strtotime((string) $post['post_expired_date'])
), ),
]; ];
@ -269,8 +252,8 @@ class adminBehaviorPostExpired
dcCore::app()->meta->setPostMeta( dcCore::app()->meta->setPostMeta(
$post_id, $post_id,
'post_expired', My::META_TYPE,
encodePostExpired($post_expired) My::encode($post_expired)
); );
} }
@ -280,26 +263,26 @@ class adminBehaviorPostExpired
* @param string $post_type Posts type * @param string $post_type Posts type
* @return array Array of HTML form fields * @return array Array of HTML form fields
*/ */
protected static function fieldsPostExpired($post_type, $post_id = null) protected static function fieldsPostExpired(string $post_type, ?int $post_id = null): array
{ {
$fields = $post_expired = []; $fields = $post_expired = [];
if ($post_id) { if ($post_id) {
$rs = dcCore::app()->meta->getMetadata([ $rs = dcCore::app()->meta->getMetadata([
'meta_type' => 'post_expired', 'meta_type' => My::META_TYPE,
'post_id' => $post_id, 'post_id' => $post_id,
'limit' => 1, 'limit' => 1,
]); ]);
if (!$rs->isEmpty()) { if (!$rs->isEmpty()) {
$post_expired = decodePostExpired($rs->meta_id); $post_expired = My::decode($rs->f('meta_id'));
} }
} }
$fields['post_expired_date'] = '<p><label for="post_expired_date">' . $fields['post_expired_date'] = '<p><label for="post_expired_date">' .
__('Date:') . '</label>' . __('Date:') . '</label>' .
form::datetime('post_expired_date', [ form::datetime('post_expired_date', [
'default' => html::escapeHTML(dt::str('%Y-%m-%dT%H:%M', strtotime($post_expired['date'] ?? 0))), 'default' => html::escapeHTML(dt::str('%Y-%m-%dT%H:%M', (int) strtotime((string) ($post_expired['date'] ?? 'now')))),
'class' => (empty($post_expired['date']) ? 'invalid' : ''), 'class' => (empty($post_expired['date']) ? 'invalid' : ''),
]) ])
. '</p>'; . '</p>';
@ -365,7 +348,7 @@ class adminBehaviorPostExpired
* @param dcRecord $categories Categories recordset * @param dcRecord $categories Categories recordset
* @return array Categorires combo * @return array Categorires combo
*/ */
protected static function categoriesCombo(dcRecord $categories) protected static function categoriesCombo(dcRecord $categories): array
{ {
# Getting categories # Getting categories
$categories_combo = [ $categories_combo = [
@ -379,8 +362,8 @@ class adminBehaviorPostExpired
); );
while ($categories->fetch()) { while ($categories->fetch()) {
$categories_combo[] = new formSelectOption( $categories_combo[] = new formSelectOption(
str_repeat('&nbsp;&nbsp;', $categories->level - 1) . '&bull; ' . html::escapeHTML($categories->cat_title), str_repeat('&nbsp;&nbsp;', ((int) $categories->f('level')) - 1) . '&bull; ' . html::escapeHTML($categories->f('cat_title')),
'!' . $categories->cat_id '!' . $categories->f('cat_id')
); );
} }
} catch (Exception $e) { } catch (Exception $e) {
@ -395,7 +378,7 @@ class adminBehaviorPostExpired
* *
* @return array Status combo * @return array Status combo
*/ */
protected static function statusCombo() protected static function statusCombo(): array
{ {
return [ return [
__('Not changed') => '', __('Not changed') => '',
@ -410,7 +393,7 @@ class adminBehaviorPostExpired
* *
* @return array Selection combo * @return array Selection combo
*/ */
protected static function selectedCombo() protected static function selectedCombo(): array
{ {
return [ return [
__('Not changed') => '', __('Not changed') => '',
@ -424,7 +407,7 @@ class adminBehaviorPostExpired
* *
* @return array Comment status combo * @return array Comment status combo
*/ */
protected static function commentCombo() protected static function commentCombo(): array
{ {
return [ return [
__('Not changed') => '', __('Not changed') => '',
@ -438,7 +421,7 @@ class adminBehaviorPostExpired
* *
* @return array Trackback status combo * @return array Trackback status combo
*/ */
protected static function trackbackCombo() protected static function trackbackCombo(): array
{ {
return [ return [
__('Not changed') => '', __('Not changed') => '',

65
src/Frontend.php Normal file
View File

@ -0,0 +1,65 @@
<?php
/**
* @brief postExpired, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and Contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\postExpired;
use dcCore;
use dcNsProcess;
class Frontend extends dcNsProcess
{
public static function init(): bool
{
static::$init = true;
return static::$init;
}
public static function process(): bool
{
if (!static::$init) {
return false;
}
// l10n
__('Expired on');
__('This entry has no expiration date');
# launch update only on public home page and feed
if (in_array(dcCore::app()->url->type, ['default', 'feed'])) {
dcCore::app()->addBehavior(
'publicBeforeDocumentV2',
[FrontendBehaviors::class, 'publicBeforeDocument']
);
}
dcCore::app()->addBehavior(
'coreBlogGetPosts',
[FrontendBehaviors::class, 'coreBlogGetPosts']
);
dcCore::app()->tpl->addBlock(
'EntryExpiredIf',
[FrontendTemplate::class, 'EntryExpiredIf']
);
dcCore::app()->tpl->addValue(
'EntryExpiredDate',
[FrontendTemplate::class, 'EntryExpiredDate']
);
dcCore::app()->tpl->addValue(
'EntryExpiredTime',
[FrontendTemplate::class, 'EntryExpiredTime']
);
return true;
}
}

View File

@ -10,52 +10,27 @@
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_RC_PATH')) { declare(strict_types=1);
return null;
}
if (dcCore::app()->getVersion(basename(__DIR__)) != dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version')) { namespace Dotclear\Plugin\postExpired;
return null;
}
__('Expired on'); use dcBlog;
__('This entry has no expiration date'); use dcCore;
use dcMeta;
# launch update only on public home page and feed use dcRecord;
if (in_array(dcCore::app()->url->type, ['default', 'feed'])) { use dt;
dcCore::app()->addBehavior(
'publicBeforeDocumentV2',
['publicBehaviorPostExpired', 'publicBeforeDocument']
);
}
dcCore::app()->addBehavior(
'coreBlogGetPosts',
['publicBehaviorPostExpired', 'coreBlogGetPosts']
);
dcCore::app()->tpl->addBlock(
'EntryExpiredIf',
['tplPostExpired', 'EntryExpiredIf']
);
dcCore::app()->tpl->addValue(
'EntryExpiredDate',
['tplPostExpired', 'EntryExpiredDate']
);
dcCore::app()->tpl->addValue(
'EntryExpiredTime',
['tplPostExpired', 'EntryExpiredTime']
);
/** /**
* @ingroup DC_PLUGIN_POSTEXPIRED * @ingroup DC_PLUGIN_POSTEXPIRED
* @brief Scheduled post change - public methods. * @brief Scheduled post change - public methods.
* @since 2.6 * @since 2.6
*/ */
class publicBehaviorPostExpired class FrontendBehaviors
{ {
/** /**
* Check if there are expired dates * Check if there are expired dates
*/ */
public static function publicBeforeDocument() public static function publicBeforeDocument(): void
{ {
# Get expired dates and post_id # Get expired dates and post_id
$posts = dcCore::app()->con->select( $posts = dcCore::app()->con->select(
@ -66,12 +41,12 @@ class publicBehaviorPostExpired
"WHERE blog_id = '" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' " . "WHERE blog_id = '" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' " .
// Removed for quick compatibility with some plugins // Removed for quick compatibility with some plugins
//"AND P.post_type = 'post' " . //"AND P.post_type = 'post' " .
"AND META.meta_type = 'post_expired' " "AND META.meta_type = '" . My::META_TYPE . "' "
); );
# No expired date # No expired date
if ($posts->isEmpty()) { if ($posts->isEmpty()) {
return null; return;
} }
# Get curent timestamp # Get curent timestamp
@ -84,22 +59,22 @@ class publicBehaviorPostExpired
$updated = false; $updated = false;
while ($posts->fetch()) { while ($posts->fetch()) {
# Decode meta record # Decode meta record
$post_expired = decodePostExpired($posts->meta_id); $post_expired = My::decode($posts->f('meta_id'));
# Check if post is outdated # Check if post is outdated
$now_tz = $now + dt::getTimeOffset($posts->post_tz, $now); $now_tz = $now + dt::getTimeOffset($posts->f('post_tz'), $now);
$meta_tz = strtotime($post_expired['date']); $meta_tz = strtotime($post_expired['date']);
if ($now_tz > $meta_tz) { if ($now_tz > $meta_tz) {
# Delete meta for expired date # Delete meta for expired date
dcCore::app()->auth->sudo( dcCore::app()->auth->sudo(
[dcCore::app()->meta, 'delPostMeta'], [dcCore::app()->meta, 'delPostMeta'],
$posts->post_id, $posts->f('post_id'),
'post_expired' My::META_TYPE
); );
# Prepare post cursor # Prepare post cursor
$post_cur->clean(); $post_cur->clean();
$post_cur->post_upddt = date('Y-m-d H:i:s', $now_tz); $post_cur->setField('post_upddt', date('Y-m-d H:i:s', $now_tz));
# Loop through actions # Loop through actions
foreach ($post_expired as $k => $v) { foreach ($post_expired as $k => $v) {
@ -113,27 +88,27 @@ class publicBehaviorPostExpired
# Put value in post cursor # Put value in post cursor
switch($k) { switch($k) {
case 'status': case 'status':
$post_cur->post_status = $v; $post_cur->setField('post_status', $v);
break; break;
case 'category': case 'category':
$post_cur->cat_id = $v ? $v : null; $post_cur->setField('cat_id', $v ? $v : null);
break; break;
case 'selected': case 'selected':
$post_cur->post_selected = $v; $post_cur->setField('post_selected', $v);
break; break;
case 'comment': case 'comment':
$post_cur->post_open_comment = $v; $post_cur->setField('post_open_comment', $v);
break; break;
case 'trackback': case 'trackback':
$post_cur->post_open_tb = $v; $post_cur->setField('post_open_tb', $v);
break; break;
} }
@ -141,7 +116,7 @@ class publicBehaviorPostExpired
# Update post # Update post
$post_cur->update( $post_cur->update(
'WHERE post_id = ' . $posts->post_id . ' ' . 'WHERE post_id = ' . $posts->f('post_id') . ' ' .
"AND blog_id = '" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' " "AND blog_id = '" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' "
); );
@ -160,135 +135,8 @@ class publicBehaviorPostExpired
* *
* @param dcRecord $rs Post recordset * @param dcRecord $rs Post recordset
*/ */
public static function coreBlogGetPosts(dcRecord $rs) public static function coreBlogGetPosts(dcRecord $rs): void
{ {
$rs->extend('rsExtPostExpiredPublic'); $rs->extend('rsExtPostExpired');
}
}
/**
* @ingroup DC_PLUGIN_POSTEXPIRED
* @brief Scheduled post change - extends recordset.
* @since 2.6
*/
class rsExtPostExpiredPublic extends rsExtPost
{
/**
* Retrieve expired date of a post
*
* @param record $rs Post recordset
* @return string Expired date or null
*/
public static function postExpiredDate(dcRecord $rs)
{
if (!$rs->postexpired[$rs->post_id]) { //memory
$rs_date = $rs->core->meta->getMetadata([
'meta_type' => 'post_expired',
'post_id' => $rs->post_id,
'limit' => 1,
]);
if ($rs_date->isEmpty()) {
return null;
}
$v = decodePostExpired($rs_date->meta_id);
$rs->postexpired[$rs->post_id] = $v['date'];
}
return $rs->postexpired[$rs->post_id];
}
}
/**
* @ingroup DC_PLUGIN_POSTEXPIRED
* @brief Scheduled post change - template methods.
* @since 2.6
*/
class tplPostExpired
{
/**
* Template condition to check if there is an expired date
*
* @param array $attr Block attributes
* @param string $content Block content
*/
public static function EntryExpiredIf($attr, $content)
{
$if = [];
$operator = isset($attr['operator']) ?
self::getOperator($attr['operator']) : '&&';
if (isset($attr['has_date'])) {
$sign = (bool) $attr['has_date'] ? '!' : '=';
$if[] = '(null ' . $sign . '== dcCore::app()->ctx->posts->postExpiredDate())';
} else {
$if[] = '(null !== dcCore::app()->ctx->posts->postExpiredDate())';
}
return
'<?php if(' . implode(' ' . $operator . ' ', $if) . ") : ?>\n" .
$content .
"<?php endif; ?>\n";
}
/**
* Template for expired date
*
* @param array $attr Value attributes
*/
public static function EntryExpiredDate($attr)
{
$format = !empty($attr['format']) ?
addslashes($attr['format']) : '';
$f = dcCore::app()->tpl->getFilters($attr);
if (!empty($attr['rfc822'])) {
$res = sprintf($f, 'dt::rfc822(strtotime(dcCore::app()->ctx->posts->postExpiredDate()),dcCore::app()->ctx->posts->post_tz)');
} elseif (!empty($attr['iso8601'])) {
$res = sprintf($f, 'dt::iso8601(strtotime(dcCore::app()->ctx->posts->postExpiredDate(),dcCore::app()->ctx->posts->post_tz)');
} elseif ($format) {
$res = sprintf($f, "dt::dt2str('" . $format . "',dcCore::app()->ctx->posts->postExpiredDate())");
} else {
$res = sprintf($f, 'dt::dt2str(dcCore::app()->blog->settings->system->date_format,dcCore::app()->ctx->posts->postExpiredDate())');
}
return '<?php if (null !== dcCore::app()->ctx->posts->postExpiredDate()) { echo ' . $res . '; } ?>';
}
/**
* Template for expired time
*
* @param array $attr Value attributes
*/
public static function EntryExpiredTime($attr)
{
return
'<?php if (null !== dcCore::app()->ctx->posts->postExpiredDate()) { echo ' . sprintf(
dcCore::app()->tpl->getFilters($attr),
'dt::dt2str(' .
(
!empty($attr['format']) ?
"'" . addslashes($attr['format']) . "'" : 'dcCore::app()->blog->settings->system->time_format'
) . ',dcCore::app()->ctx->posts->postExpiredDate())'
) . '; } ?>';
}
/**
* Parse tempalte attributes oprerator
*
* @param string $op Operator
*/
protected static function getOperator($op)
{
switch (strtolower($op)) {
case 'or':
case '||':
return '||';
case 'and':
case '&&':
default:
return '&&';
}
} }
} }

100
src/FrontendTemplate.php Normal file
View File

@ -0,0 +1,100 @@
<?php
/**
* @brief postExpired, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and Contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\postExpired;
use ArrayObject;
use dcCore;
use dcTemplate;
/**
* @ingroup DC_PLUGIN_POSTEXPIRED
* @brief Scheduled post change - template methods.
* @since 2.6
*/
class FrontendTemplate
{
/**
* Template condition to check if there is an expired date
*
* @param ArrayObject $attr Block attributes
* @param string $content Block content
*
* @return string
*/
public static function EntryExpiredIf(ArrayObject $attr, string $content): string
{
$if = [];
$operator = isset($attr['operator']) ?
dcTemplate::getOperator($attr['operator']) : '&&';
if (isset($attr['has_date'])) {
$sign = (bool) $attr['has_date'] ? '!' : '=';
$if[] = '(null ' . $sign . '== dcCore::app()->ctx->posts->postExpiredDate())';
} else {
$if[] = '(null !== dcCore::app()->ctx->posts->postExpiredDate())';
}
return
'<?php if(' . implode(' ' . $operator . ' ', $if) . ") : ?>\n" .
$content .
"<?php endif; ?>\n";
}
/**
* Template for expired date
*
* @param ArrayObject $attr Value attributes
*
* @return string
*/
public static function EntryExpiredDate(ArrayObject $attr): string
{
$format = !empty($attr['format']) ?
addslashes($attr['format']) : '';
$f = dcCore::app()->tpl->getFilters($attr);
if (!empty($attr['rfc822'])) {
$res = sprintf($f, 'dt::rfc822(strtotime(dcCore::app()->ctx->posts->postExpiredDate()),dcCore::app()->ctx->posts->post_tz)');
} elseif (!empty($attr['iso8601'])) {
$res = sprintf($f, 'dt::iso8601(strtotime(dcCore::app()->ctx->posts->postExpiredDate(),dcCore::app()->ctx->posts->post_tz)');
} elseif ($format) {
$res = sprintf($f, "dt::dt2str('" . $format . "',dcCore::app()->ctx->posts->postExpiredDate())");
} else {
$res = sprintf($f, 'dt::dt2str(dcCore::app()->blog->settings->system->date_format,dcCore::app()->ctx->posts->postExpiredDate())');
}
return '<?php if (null !== dcCore::app()->ctx->posts->postExpiredDate()) { echo ' . $res . '; } ?>';
}
/**
* Template for expired time
*
* @param ArrayObject $attr Value attributes
*
* @return string
*/
public static function EntryExpiredTime(ArrayObject $attr): string
{
return
'<?php if (null !== dcCore::app()->ctx->posts->postExpiredDate()) { echo ' . sprintf(
dcCore::app()->tpl->getFilters($attr),
'dt::dt2str(' .
(
!empty($attr['format']) ?
"'" . addslashes($attr['format']) . "'" : 'dcCore::app()->blog->settings->system->time_format'
) . ',dcCore::app()->ctx->posts->postExpiredDate())'
) . '; } ?>';
}
}

View File

@ -10,11 +10,34 @@
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_RC_PATH')) { declare(strict_types=1);
return null;
}
/** namespace Dotclear\Plugin\postExpired;
use dcCore;
class My
{
/** @var string This plugin meta type */
public const META_TYPE = 'post_expired';
/**
* This module id
*/
public static function id(): string
{
return basename(dirname(__DIR__));
}
/**
* This module name
*/
public static function name(): string
{
return __((string) dcCore::app()->plugins->moduleInfo(self::id(), 'name'));
}
/**
* Encode Expired Date settings * Encode Expired Date settings
* *
* This is saved into post_meta as meta_id value, * This is saved into post_meta as meta_id value,
@ -23,24 +46,24 @@ if (!defined('DC_RC_PATH')) {
* @param array $in Array of options * @param array $in Array of options
* @return string "Serialized" options * @return string "Serialized" options
*/ */
function encodePostExpired($in) public static function encode(array $in): string
{ {
$out = []; $out = [];
foreach ($in as $k => $v) { foreach ($in as $k => $v) {
$out[] = $k . '|' . $v; $out[] = $k . '|' . $v;
} }
return implode(';', $out); return implode(';', $out);
} }
/** /**
* Decode Expired Date settings * Decode Expired Date settings
* *
* @param string $in "Serialized" options * @param string $in "Serialized" options
* @return array Array of options * @return array Array of options
*/ */
function decodePostExpired($in) public static function decode(string $in): array
{ {
$out = []; $out = [];
foreach (explode(';', $in) as $v) { foreach (explode(';', $in) as $v) {
$v = explode('|', $v); $v = explode('|', $v);
@ -48,4 +71,5 @@ function decodePostExpired($in)
} }
return $out; return $out;
}
} }

55
src/RsExtPostExpired.php Normal file
View File

@ -0,0 +1,55 @@
<?php
/**
* @brief postExpired, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and Contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\postExpired;
use dcCore;
use dcRecord;
use rsExtPost;
/**
* @ingroup DC_PLUGIN_POSTEXPIRED
* @brief Scheduled post change - extends recordset.
* @since 2.6
*/
class rsExtPostExpired extends rsExtPost
{
protected static array $memory = [];
/**
* Retrieve expired date of a post
*
* @param dcRecord $rs Post recordset
* @return string Expired date or null
*/
public static function postExpiredDate(dcRecord $rs): string
{
if (!self::$memory[$rs->f('post_id')]) { //memory
$rs_date = dcCore::app()->meta->getMetadata([
'meta_type' => My::META_TYPE,
'post_id' => $rs->f('post_id'),
'limit' => 1,
]);
if ($rs_date->isEmpty()) {
return '';
}
$v = My::decode($rs_date->f('meta_id'));
self::$memory[$rs->f('post_id')] = $v['date'];
}
return self::$memory[$rs->f('post_id')];
}
}