use namespace
This commit is contained in:
parent
5de059b1c2
commit
596569cbb2
8 changed files with 407 additions and 310 deletions
34
_install.php
34
_install.php
|
@ -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
56
src/Backend.php
Normal 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;
|
||||
}
|
||||
}
|
|
@ -10,52 +10,35 @@
|
|||
* @copyright Jean-Christian Denis
|
||||
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
|
||||
*/
|
||||
if (!defined('DC_CONTEXT_ADMIN')) {
|
||||
return null;
|
||||
}
|
||||
declare(strict_types=1);
|
||||
|
||||
# Check plugin version
|
||||
if (dcCore::app()->getVersion(basename(__DIR__)) != dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version')) {
|
||||
return null;
|
||||
}
|
||||
namespace Dotclear\Plugin\postExpired;
|
||||
|
||||
# Check user right
|
||||
if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([
|
||||
dcAuth::PERMISSION_CONTENT_ADMIN,
|
||||
]), dcCore::app()->blog->id)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
# Admin behaviors
|
||||
dcCore::app()->addBehaviors([
|
||||
'adminPostsActions' => ['adminBehaviorPostExpired', 'adminPostsActions'],
|
||||
'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'],
|
||||
]);
|
||||
use ArrayObject;
|
||||
use cursor;
|
||||
use dcCore;
|
||||
use dcPostsActions;
|
||||
use dcPage;
|
||||
use dcRecord;
|
||||
use Exception;
|
||||
use dt;
|
||||
use html;
|
||||
use form;
|
||||
use formSelectOption;
|
||||
|
||||
/**
|
||||
* @ingroup DC_PLUGIN_POSTEXPIRED
|
||||
* @brief Scheduled post change - admin methods.
|
||||
* @since 2.6
|
||||
*/
|
||||
class adminBehaviorPostExpired
|
||||
class BackendBehaviors
|
||||
{
|
||||
/**
|
||||
* 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(
|
||||
[
|
||||
|
@ -63,7 +46,7 @@ class adminBehaviorPostExpired
|
|||
__('Add expired date') => 'post_expired_add',
|
||||
],
|
||||
],
|
||||
['adminBehaviorPostExpired', 'callbackAdd']
|
||||
[self::class, 'callbackAdd']
|
||||
);
|
||||
|
||||
$pa->addAction(
|
||||
|
@ -72,7 +55,7 @@ class adminBehaviorPostExpired
|
|||
__('Remove expired date') => 'post_expired_remove',
|
||||
],
|
||||
],
|
||||
['adminBehaviorPostExpired', 'callbackRemove']
|
||||
[self::class, 'callbackRemove']
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -81,9 +64,9 @@ class adminBehaviorPostExpired
|
|||
*
|
||||
* @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 $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) {
|
||||
return null;
|
||||
return;
|
||||
}
|
||||
|
||||
$sidebar_items['post_expired'] = [
|
||||
'title' => __('Expired date'),
|
||||
'items' => self::fieldsPostExpired(
|
||||
$post->post_type,
|
||||
$post->post_id
|
||||
$post->f('post_type'),
|
||||
(int) $post->f('post_id')
|
||||
),
|
||||
];
|
||||
}
|
||||
|
@ -113,7 +96,7 @@ class adminBehaviorPostExpired
|
|||
*
|
||||
* @param integer $post_id Post id
|
||||
*/
|
||||
public static function adminBeforePostDelete($post_id)
|
||||
public static function adminBeforePostDelete(int $post_id): void
|
||||
{
|
||||
self::delPostExpired($post_id);
|
||||
}
|
||||
|
@ -124,7 +107,7 @@ class adminBehaviorPostExpired
|
|||
* @param cursor $cur Current post cursor
|
||||
* @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);
|
||||
|
||||
|
@ -134,7 +117,7 @@ class adminBehaviorPostExpired
|
|||
|| !empty($_POST['post_expired_selected'])
|
||||
|| !empty($_POST['post_expired_comment'])
|
||||
|| !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 ArrayObject $post _POST actions
|
||||
*/
|
||||
public static function callbackAdd(dcPostsActions $pa, ArrayObject $post)
|
||||
public static function callbackAdd(dcPostsActions $pa, ArrayObject $post): void
|
||||
{
|
||||
# No entry
|
||||
$posts_ids = $pa->getIDs();
|
||||
|
@ -164,7 +147,7 @@ class adminBehaviorPostExpired
|
|||
self::setPostExpired($post_id, $post);
|
||||
}
|
||||
|
||||
dcAdminNotices::addSuccessNotice(__('Expired date added.'));
|
||||
dcPage::addSuccessNotice(__('Expired date added.'));
|
||||
$pa->redirect(true);
|
||||
|
||||
# Display form
|
||||
|
@ -186,7 +169,7 @@ class adminBehaviorPostExpired
|
|||
'<form action="' . $pa->getURI() . '" method="post">' .
|
||||
$pa->getCheckboxes() .
|
||||
|
||||
implode('', self::fieldsPostExpired($posts->post_type)) .
|
||||
implode('', self::fieldsPostExpired($posts->f('post_type'))) .
|
||||
|
||||
dcCore::app()->formNonce() .
|
||||
$pa->getHiddenFields() .
|
||||
|
@ -204,7 +187,7 @@ class adminBehaviorPostExpired
|
|||
* @param dcPostsActions $pa dcPostsActions instance
|
||||
* @param ArrayObject $post _POST actions
|
||||
*/
|
||||
public static function callbackRemove(dcPostsActions $pa, ArrayObject $post)
|
||||
public static function callbackRemove(dcPostsActions $pa, ArrayObject $post): void
|
||||
{
|
||||
# No entry
|
||||
$posts_ids = $pa->getIDs();
|
||||
|
@ -217,7 +200,7 @@ class adminBehaviorPostExpired
|
|||
self::delPostExpired($post_id);
|
||||
}
|
||||
|
||||
dcAdminNotices::addSuccessNotice(__('Expired date deleted.'));
|
||||
dcPage::addSuccessNotice(__('Expired date deleted.'));
|
||||
$pa->redirect(true);
|
||||
}
|
||||
|
||||
|
@ -226,18 +209,18 @@ class adminBehaviorPostExpired
|
|||
*
|
||||
* @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
|
||||
*
|
||||
* @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 = [
|
||||
'status' => '',
|
||||
|
@ -247,7 +230,7 @@ class adminBehaviorPostExpired
|
|||
'trackback' => '',
|
||||
'date' => date(
|
||||
'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(
|
||||
$post_id,
|
||||
'post_expired',
|
||||
encodePostExpired($post_expired)
|
||||
My::META_TYPE,
|
||||
My::encode($post_expired)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -280,26 +263,26 @@ class adminBehaviorPostExpired
|
|||
* @param string $post_type Posts type
|
||||
* @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 = [];
|
||||
|
||||
if ($post_id) {
|
||||
$rs = dcCore::app()->meta->getMetadata([
|
||||
'meta_type' => 'post_expired',
|
||||
'meta_type' => My::META_TYPE,
|
||||
'post_id' => $post_id,
|
||||
'limit' => 1,
|
||||
]);
|
||||
|
||||
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">' .
|
||||
__('Date:') . '</label>' .
|
||||
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' : ''),
|
||||
])
|
||||
. '</p>';
|
||||
|
@ -365,7 +348,7 @@ class adminBehaviorPostExpired
|
|||
* @param dcRecord $categories Categories recordset
|
||||
* @return array Categorires combo
|
||||
*/
|
||||
protected static function categoriesCombo(dcRecord $categories)
|
||||
protected static function categoriesCombo(dcRecord $categories): array
|
||||
{
|
||||
# Getting categories
|
||||
$categories_combo = [
|
||||
|
@ -379,8 +362,8 @@ class adminBehaviorPostExpired
|
|||
);
|
||||
while ($categories->fetch()) {
|
||||
$categories_combo[] = new formSelectOption(
|
||||
str_repeat(' ', $categories->level - 1) . '• ' . html::escapeHTML($categories->cat_title),
|
||||
'!' . $categories->cat_id
|
||||
str_repeat(' ', ((int) $categories->f('level')) - 1) . '• ' . html::escapeHTML($categories->f('cat_title')),
|
||||
'!' . $categories->f('cat_id')
|
||||
);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
|
@ -395,7 +378,7 @@ class adminBehaviorPostExpired
|
|||
*
|
||||
* @return array Status combo
|
||||
*/
|
||||
protected static function statusCombo()
|
||||
protected static function statusCombo(): array
|
||||
{
|
||||
return [
|
||||
__('Not changed') => '',
|
||||
|
@ -410,7 +393,7 @@ class adminBehaviorPostExpired
|
|||
*
|
||||
* @return array Selection combo
|
||||
*/
|
||||
protected static function selectedCombo()
|
||||
protected static function selectedCombo(): array
|
||||
{
|
||||
return [
|
||||
__('Not changed') => '',
|
||||
|
@ -424,7 +407,7 @@ class adminBehaviorPostExpired
|
|||
*
|
||||
* @return array Comment status combo
|
||||
*/
|
||||
protected static function commentCombo()
|
||||
protected static function commentCombo(): array
|
||||
{
|
||||
return [
|
||||
__('Not changed') => '',
|
||||
|
@ -438,7 +421,7 @@ class adminBehaviorPostExpired
|
|||
*
|
||||
* @return array Trackback status combo
|
||||
*/
|
||||
protected static function trackbackCombo()
|
||||
protected static function trackbackCombo(): array
|
||||
{
|
||||
return [
|
||||
__('Not changed') => '',
|
||||
|
|
65
src/Frontend.php
Normal file
65
src/Frontend.php
Normal 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;
|
||||
}
|
||||
}
|
|
@ -10,52 +10,27 @@
|
|||
* @copyright Jean-Christian Denis
|
||||
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
|
||||
*/
|
||||
if (!defined('DC_RC_PATH')) {
|
||||
return null;
|
||||
}
|
||||
declare(strict_types=1);
|
||||
|
||||
if (dcCore::app()->getVersion(basename(__DIR__)) != dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version')) {
|
||||
return null;
|
||||
}
|
||||
namespace Dotclear\Plugin\postExpired;
|
||||
|
||||
__('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',
|
||||
['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']
|
||||
);
|
||||
use dcBlog;
|
||||
use dcCore;
|
||||
use dcMeta;
|
||||
use dcRecord;
|
||||
use dt;
|
||||
|
||||
/**
|
||||
* @ingroup DC_PLUGIN_POSTEXPIRED
|
||||
* @brief Scheduled post change - public methods.
|
||||
* @since 2.6
|
||||
*/
|
||||
class publicBehaviorPostExpired
|
||||
class FrontendBehaviors
|
||||
{
|
||||
/**
|
||||
* Check if there are expired dates
|
||||
*/
|
||||
public static function publicBeforeDocument()
|
||||
public static function publicBeforeDocument(): void
|
||||
{
|
||||
# Get expired dates and post_id
|
||||
$posts = dcCore::app()->con->select(
|
||||
|
@ -66,12 +41,12 @@ class publicBehaviorPostExpired
|
|||
"WHERE blog_id = '" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' " .
|
||||
// Removed for quick compatibility with some plugins
|
||||
//"AND P.post_type = 'post' " .
|
||||
"AND META.meta_type = 'post_expired' "
|
||||
"AND META.meta_type = '" . My::META_TYPE . "' "
|
||||
);
|
||||
|
||||
# No expired date
|
||||
if ($posts->isEmpty()) {
|
||||
return null;
|
||||
return;
|
||||
}
|
||||
|
||||
# Get curent timestamp
|
||||
|
@ -84,22 +59,22 @@ class publicBehaviorPostExpired
|
|||
$updated = false;
|
||||
while ($posts->fetch()) {
|
||||
# Decode meta record
|
||||
$post_expired = decodePostExpired($posts->meta_id);
|
||||
$post_expired = My::decode($posts->f('meta_id'));
|
||||
|
||||
# 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']);
|
||||
if ($now_tz > $meta_tz) {
|
||||
# Delete meta for expired date
|
||||
dcCore::app()->auth->sudo(
|
||||
[dcCore::app()->meta, 'delPostMeta'],
|
||||
$posts->post_id,
|
||||
'post_expired'
|
||||
$posts->f('post_id'),
|
||||
My::META_TYPE
|
||||
);
|
||||
|
||||
# Prepare post cursor
|
||||
$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
|
||||
foreach ($post_expired as $k => $v) {
|
||||
|
@ -113,27 +88,27 @@ class publicBehaviorPostExpired
|
|||
# Put value in post cursor
|
||||
switch($k) {
|
||||
case 'status':
|
||||
$post_cur->post_status = $v;
|
||||
$post_cur->setField('post_status', $v);
|
||||
|
||||
break;
|
||||
|
||||
case 'category':
|
||||
$post_cur->cat_id = $v ? $v : null;
|
||||
$post_cur->setField('cat_id', $v ? $v : null);
|
||||
|
||||
break;
|
||||
|
||||
case 'selected':
|
||||
$post_cur->post_selected = $v;
|
||||
$post_cur->setField('post_selected', $v);
|
||||
|
||||
break;
|
||||
|
||||
case 'comment':
|
||||
$post_cur->post_open_comment = $v;
|
||||
$post_cur->setField('post_open_comment', $v);
|
||||
|
||||
break;
|
||||
|
||||
case 'trackback':
|
||||
$post_cur->post_open_tb = $v;
|
||||
$post_cur->setField('post_open_tb', $v);
|
||||
|
||||
break;
|
||||
}
|
||||
|
@ -141,7 +116,7 @@ class publicBehaviorPostExpired
|
|||
|
||||
# Update post
|
||||
$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) . "' "
|
||||
);
|
||||
|
||||
|
@ -160,135 +135,8 @@ class publicBehaviorPostExpired
|
|||
*
|
||||
* @param dcRecord $rs Post recordset
|
||||
*/
|
||||
public static function coreBlogGetPosts(dcRecord $rs)
|
||||
public static function coreBlogGetPosts(dcRecord $rs): void
|
||||
{
|
||||
$rs->extend('rsExtPostExpiredPublic');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 '&&';
|
||||
}
|
||||
$rs->extend('rsExtPostExpired');
|
||||
}
|
||||
}
|
||||
|
|
100
src/FrontendTemplate.php
Normal file
100
src/FrontendTemplate.php
Normal 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())'
|
||||
) . '; } ?>';
|
||||
}
|
||||
}
|
88
src/My.php
88
src/My.php
|
@ -10,42 +10,66 @@
|
|||
* @copyright Jean-Christian Denis
|
||||
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
|
||||
*/
|
||||
if (!defined('DC_RC_PATH')) {
|
||||
return null;
|
||||
}
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Encode Expired Date settings
|
||||
*
|
||||
* This is saved into post_meta as meta_id value,
|
||||
* so this must be less than 255 caracters.
|
||||
*
|
||||
* @param array $in Array of options
|
||||
* @return string "Serialized" options
|
||||
*/
|
||||
function encodePostExpired($in)
|
||||
namespace Dotclear\Plugin\postExpired;
|
||||
|
||||
use dcCore;
|
||||
|
||||
class My
|
||||
{
|
||||
$out = [];
|
||||
foreach ($in as $k => $v) {
|
||||
$out[] = $k . '|' . $v;
|
||||
/** @var string This plugin meta type */
|
||||
public const META_TYPE = 'post_expired';
|
||||
|
||||
/**
|
||||
* This module id
|
||||
*/
|
||||
public static function id(): string
|
||||
{
|
||||
return basename(dirname(__DIR__));
|
||||
}
|
||||
|
||||
return implode(';', $out);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode Expired Date settings
|
||||
*
|
||||
* @param string $in "Serialized" options
|
||||
* @return array Array of options
|
||||
*/
|
||||
function decodePostExpired($in)
|
||||
{
|
||||
$out = [];
|
||||
foreach (explode(';', $in) as $v) {
|
||||
$v = explode('|', $v);
|
||||
$out[$v[0]] = $v[1];
|
||||
/**
|
||||
* This module name
|
||||
*/
|
||||
public static function name(): string
|
||||
{
|
||||
return __((string) dcCore::app()->plugins->moduleInfo(self::id(), 'name'));
|
||||
}
|
||||
|
||||
return $out;
|
||||
/**
|
||||
* Encode Expired Date settings
|
||||
*
|
||||
* This is saved into post_meta as meta_id value,
|
||||
* so this must be less than 255 caracters.
|
||||
*
|
||||
* @param array $in Array of options
|
||||
* @return string "Serialized" options
|
||||
*/
|
||||
public static function encode(array $in): string
|
||||
{
|
||||
$out = [];
|
||||
foreach ($in as $k => $v) {
|
||||
$out[] = $k . '|' . $v;
|
||||
}
|
||||
|
||||
return implode(';', $out);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode Expired Date settings
|
||||
*
|
||||
* @param string $in "Serialized" options
|
||||
* @return array Array of options
|
||||
*/
|
||||
public static function decode(string $in): array
|
||||
{
|
||||
$out = [];
|
||||
foreach (explode(';', $in) as $v) {
|
||||
$v = explode('|', $v);
|
||||
$out[$v[0]] = $v[1];
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
|
|
55
src/RsExtPostExpired.php
Normal file
55
src/RsExtPostExpired.php
Normal 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')];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue