postExpired/_public.php

295 lines
8.6 KiB
PHP
Raw Normal View History

2021-08-20 19:07:26 +00:00
<?php
2021-09-02 20:43:05 +00:00
/**
* @brief postExpired, a plugin for Dotclear 2
2022-11-20 20:43:32 +00:00
*
2021-09-02 20:43:05 +00:00
* @package Dotclear
* @subpackage Plugin
2022-11-20 20:43:32 +00:00
*
2021-09-02 20:43:05 +00:00
* @author Jean-Christian Denis and Contributors
2022-11-20 20:43:32 +00:00
*
2021-09-02 20:43:05 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2021-08-20 19:07:26 +00:00
if (!defined('DC_RC_PATH')) {
return null;
2021-08-20 19:07:26 +00:00
}
2022-11-14 20:47:24 +00:00
if (dcCore::app()->getVersion('postExpired') != dcCore::app()->plugins->moduleInfo('postExpired', 'version')) {
return null;
2021-08-20 19:07:26 +00:00
}
__('Expired on');
__('This entry has no expiration date');
# launch update only on public home page and feed
2022-11-20 20:43:32 +00:00
if (in_array(dcCore::app()->url->type, ['default', 'feed'])) {
2022-11-14 20:47:24 +00:00
dcCore::app()->addBehavior(
'publicBeforeDocumentV2',
['publicBehaviorPostExpired', 'publicBeforeDocument']
);
2021-08-20 19:07:26 +00:00
}
2022-11-14 20:47:24 +00:00
dcCore::app()->addBehavior(
'coreBlogGetPosts',
['publicBehaviorPostExpired', 'coreBlogGetPosts']
2021-08-20 19:07:26 +00:00
);
2022-11-14 20:47:24 +00:00
dcCore::app()->tpl->addBlock(
'EntryExpiredIf',
['tplPostExpired', 'EntryExpiredIf']
2021-08-20 19:07:26 +00:00
);
2022-11-14 20:47:24 +00:00
dcCore::app()->tpl->addValue(
'EntryExpiredDate',
['tplPostExpired', 'EntryExpiredDate']
2021-08-20 19:07:26 +00:00
);
2022-11-14 20:47:24 +00:00
dcCore::app()->tpl->addValue(
'EntryExpiredTime',
['tplPostExpired', 'EntryExpiredTime']
2021-08-20 19:07:26 +00:00
);
/**
* @ingroup DC_PLUGIN_POSTEXPIRED
* @brief Scheduled post change - public methods.
* @since 2.6
*/
class publicBehaviorPostExpired
{
/**
* Check if there are expired dates
*/
2022-11-14 20:47:24 +00:00
public static function publicBeforeDocument()
{
# Get expired dates and post_id
2022-11-14 20:47:24 +00:00
$posts = dcCore::app()->con->select(
'SELECT P.post_id, P.post_tz, META.meta_id ' .
2022-12-03 15:45:34 +00:00
'FROM ' . dcCore::app()->prefix . dcBlog::POST_TABLE_NAME . ' P ' .
'INNER JOIN ' . dcCore::app()->prefix . dcMeta::META_TABLE_NAME . ' META ' .
'ON META.post_id = P.post_id ' .
2022-11-14 20:47:24 +00:00
"WHERE blog_id = '" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' " .
// Removed for quick compatibility with some plugins
2022-11-20 20:43:32 +00:00
//"AND P.post_type = 'post' " .
"AND META.meta_type = 'post_expired' "
);
# No expired date
if ($posts->isEmpty()) {
return null;
}
# Get curent timestamp
$now = dt::toUTC(time());
# Prepared post cursor
2022-12-03 15:45:34 +00:00
$post_cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcBlog::POST_TABLE_NAME);
# Loop through marked posts
$updated = false;
2022-11-20 20:43:32 +00:00
while ($posts->fetch()) {
# Decode meta record
$post_expired = decodePostExpired($posts->meta_id);
# Check if post is outdated
2022-11-20 20:43:32 +00:00
$now_tz = $now + dt::getTimeOffset($posts->post_tz, $now);
$meta_tz = strtotime($post_expired['date']);
if ($now_tz > $meta_tz) {
# Delete meta for expired date
2022-11-14 20:47:24 +00:00
dcCore::app()->auth->sudo(
2022-11-20 20:43:32 +00:00
[dcCore::app()->meta, 'delPostMeta'],
$posts->post_id,
'post_expired'
);
# Prepare post cursor
$post_cur->clean();
$post_cur->post_upddt = date('Y-m-d H:i:s', $now_tz);
# Loop through actions
2022-11-20 20:43:32 +00:00
foreach ($post_expired as $k => $v) {
if (empty($v)) {
continue;
}
# values are prefixed by "!"
2022-11-20 20:43:32 +00:00
$v = (int) substr($v, 1);
# Put value in post cursor
2022-11-20 20:43:32 +00:00
switch($k) {
case 'status':
$post_cur->post_status = $v;
2022-11-20 20:43:32 +00:00
break;
case 'category':
$post_cur->cat_id = $v ? $v : null;
2022-11-20 20:43:32 +00:00
break;
case 'selected':
$post_cur->post_selected = $v;
2022-11-20 20:43:32 +00:00
break;
case 'comment':
$post_cur->post_open_comment = $v;
2022-11-20 20:43:32 +00:00
break;
case 'trackback':
$post_cur->post_open_tb = $v;
2022-11-20 20:43:32 +00:00
break;
}
}
# Update post
$post_cur->update(
'WHERE post_id = ' . $posts->post_id . ' ' .
2022-11-14 20:47:24 +00:00
"AND blog_id = '" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' "
);
$updated = true;
}
}
# Say blog is updated
if ($updated) {
2022-11-14 20:47:24 +00:00
dcCore::app()->blog->triggerBlog();
}
}
/**
* Extends posts record with expired date
2022-11-20 20:43:32 +00:00
*
2022-11-14 20:47:24 +00:00
* @param dcRecord $rs Post recordset
*/
2022-11-14 20:47:24 +00:00
public static function coreBlogGetPosts(dcRecord $rs)
{
$rs->extend('rsExtPostExpiredPublic');
}
2021-08-20 19:07:26 +00:00
}
/**
* @ingroup DC_PLUGIN_POSTEXPIRED
* @brief Scheduled post change - extends recordset.
* @since 2.6
*/
class rsExtPostExpiredPublic extends rsExtPost
{
/**
* Retrieve expired date of a post
2022-11-20 20:43:32 +00:00
*
* @param record $rs Post recordset
* @return string Expired date or null
*/
2022-11-14 20:47:24 +00:00
public static function postExpiredDate(dcRecord $rs)
{
if (!$rs->postexpired[$rs->post_id]) { //memory
2021-08-20 21:35:03 +00:00
$rs_date = $rs->core->meta->getMetadata([
'meta_type' => 'post_expired',
2022-11-20 20:43:32 +00:00
'post_id' => $rs->post_id,
'limit' => 1,
2021-08-20 21:35:03 +00:00
]);
if ($rs_date->isEmpty()) {
return null;
}
2022-11-20 20:43:32 +00:00
$v = decodePostExpired($rs_date->meta_id);
$rs->postexpired[$rs->post_id] = $v['date'];
}
return $rs->postexpired[$rs->post_id];
}
2021-08-20 19:07:26 +00:00
}
/**
* @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
2022-11-20 20:43:32 +00:00
*
* @param array $attr Block attributes
* @param string $content Block content
*/
public static function EntryExpiredIf($attr, $content)
{
2022-11-20 20:43:32 +00:00
$if = [];
$operator = isset($attr['operator']) ?
self::getOperator($attr['operator']) : '&&';
if (isset($attr['has_date'])) {
2022-11-20 20:43:32 +00:00
$sign = (bool) $attr['has_date'] ? '!' : '=';
2022-11-14 20:47:24 +00:00
$if[] = '(null ' . $sign . '== dcCore::app()->ctx->posts->postExpiredDate())';
} else {
2022-11-14 20:47:24 +00:00
$if[] = '(null !== dcCore::app()->ctx->posts->postExpiredDate())';
}
2022-11-20 20:43:32 +00:00
return
'<?php if(' . implode(' ' . $operator . ' ', $if) . ") : ?>\n" .
$content .
"<?php endif; ?>\n";
}
/**
* Template for expired date
2022-11-20 20:43:32 +00:00
*
* @param array $attr Value attributes
*/
public static function EntryExpiredDate($attr)
{
2022-11-20 20:43:32 +00:00
$format = !empty($attr['format']) ?
addslashes($attr['format']) : '';
2022-11-14 20:47:24 +00:00
$f = dcCore::app()->tpl->getFilters($attr);
if (!empty($attr['rfc822'])) {
2022-11-20 20:43:32 +00:00
$res = sprintf($f, 'dt::rfc822(strtotime(dcCore::app()->ctx->posts->postExpiredDate()),dcCore::app()->ctx->posts->post_tz)');
} elseif (!empty($attr['iso8601'])) {
2022-11-20 20:43:32 +00:00
$res = sprintf($f, 'dt::iso8601(strtotime(dcCore::app()->ctx->posts->postExpiredDate(),dcCore::app()->ctx->posts->post_tz)');
} elseif ($format) {
2022-11-14 20:47:24 +00:00
$res = sprintf($f, "dt::dt2str('" . $format . "',dcCore::app()->ctx->posts->postExpiredDate())");
} else {
2022-11-20 20:43:32 +00:00
$res = sprintf($f, 'dt::dt2str(dcCore::app()->blog->settings->system->date_format,dcCore::app()->ctx->posts->postExpiredDate())');
}
2022-11-14 20:47:24 +00:00
return '<?php if (null !== dcCore::app()->ctx->posts->postExpiredDate()) { echo ' . $res . '; } ?>';
}
/**
* Template for expired time
2022-11-20 20:43:32 +00:00
*
* @param array $attr Value attributes
*/
public static function EntryExpiredTime($attr)
{
2022-11-20 20:43:32 +00:00
return
2022-11-14 20:47:24 +00:00
'<?php if (null !== dcCore::app()->ctx->posts->postExpiredDate()) { echo ' . sprintf(
2022-11-20 20:43:32 +00:00
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
2022-11-20 20:43:32 +00:00
*
* @param string $op Operator
*/
protected static function getOperator($op)
{
2022-11-20 20:43:32 +00:00
switch (strtolower($op)) {
case 'or':
case '||':
return '||';
case 'and':
case '&&':
default:
return '&&';
}
}
2022-11-20 20:43:32 +00:00
}