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')) {
|
2021-08-20 19:36:46 +00:00
|
|
|
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')) {
|
2021-08-20 19:36:46 +00:00
|
|
|
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',
|
2021-08-20 19:36:46 +00:00
|
|
|
['publicBehaviorPostExpired', 'publicBeforeDocument']
|
|
|
|
);
|
2021-08-20 19:07:26 +00:00
|
|
|
}
|
2022-11-14 20:47:24 +00:00
|
|
|
dcCore::app()->addBehavior(
|
2021-08-20 19:36:46 +00:00
|
|
|
'coreBlogGetPosts',
|
|
|
|
['publicBehaviorPostExpired', 'coreBlogGetPosts']
|
2021-08-20 19:07:26 +00:00
|
|
|
);
|
2022-11-14 20:47:24 +00:00
|
|
|
dcCore::app()->tpl->addBlock(
|
2021-08-20 19:36:46 +00:00
|
|
|
'EntryExpiredIf',
|
|
|
|
['tplPostExpired', 'EntryExpiredIf']
|
2021-08-20 19:07:26 +00:00
|
|
|
);
|
2022-11-14 20:47:24 +00:00
|
|
|
dcCore::app()->tpl->addValue(
|
2021-08-20 19:36:46 +00:00
|
|
|
'EntryExpiredDate',
|
|
|
|
['tplPostExpired', 'EntryExpiredDate']
|
2021-08-20 19:07:26 +00:00
|
|
|
);
|
2022-11-14 20:47:24 +00:00
|
|
|
dcCore::app()->tpl->addValue(
|
2021-08-20 19:36:46 +00:00
|
|
|
'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
|
|
|
|
{
|
2021-08-20 19:36:46 +00:00
|
|
|
/**
|
|
|
|
* Check if there are expired dates
|
|
|
|
*/
|
2022-11-14 20:47:24 +00:00
|
|
|
public static function publicBeforeDocument()
|
2021-08-20 19:36:46 +00:00
|
|
|
{
|
|
|
|
# Get expired dates and post_id
|
2022-11-14 20:47:24 +00:00
|
|
|
$posts = dcCore::app()->con->select(
|
2021-08-20 19:36:46 +00:00
|
|
|
'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 ' .
|
2021-08-20 19:36:46 +00:00
|
|
|
'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) . "' " .
|
2021-08-20 19:36:46 +00:00
|
|
|
// Removed for quick compatibility with some plugins
|
2022-11-20 20:43:32 +00:00
|
|
|
//"AND P.post_type = 'post' " .
|
2021-08-20 19:36:46 +00:00
|
|
|
"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);
|
2021-08-20 19:36:46 +00:00
|
|
|
|
|
|
|
# Loop through marked posts
|
|
|
|
$updated = false;
|
2022-11-20 20:43:32 +00:00
|
|
|
while ($posts->fetch()) {
|
2021-08-20 19:36:46 +00:00
|
|
|
# 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);
|
2021-08-20 19:36:46 +00:00
|
|
|
$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'],
|
2021-08-20 19:36:46 +00:00
|
|
|
$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) {
|
2021-08-20 19:36:46 +00:00
|
|
|
if (empty($v)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
# values are prefixed by "!"
|
2022-11-20 20:43:32 +00:00
|
|
|
$v = (int) substr($v, 1);
|
2021-08-20 19:36:46 +00:00
|
|
|
|
|
|
|
# Put value in post cursor
|
2022-11-20 20:43:32 +00:00
|
|
|
switch($k) {
|
2021-08-20 19:36:46 +00:00
|
|
|
case 'status':
|
|
|
|
$post_cur->post_status = $v;
|
2022-11-20 20:43:32 +00:00
|
|
|
|
|
|
|
break;
|
2021-08-20 19:36:46 +00:00
|
|
|
|
|
|
|
case 'category':
|
|
|
|
$post_cur->cat_id = $v ? $v : null;
|
2022-11-20 20:43:32 +00:00
|
|
|
|
|
|
|
break;
|
2021-08-20 19:36:46 +00:00
|
|
|
|
|
|
|
case 'selected':
|
|
|
|
$post_cur->post_selected = $v;
|
2022-11-20 20:43:32 +00:00
|
|
|
|
|
|
|
break;
|
2021-08-20 19:36:46 +00:00
|
|
|
|
|
|
|
case 'comment':
|
|
|
|
$post_cur->post_open_comment = $v;
|
2022-11-20 20:43:32 +00:00
|
|
|
|
|
|
|
break;
|
2021-08-20 19:36:46 +00:00
|
|
|
|
|
|
|
case 'trackback':
|
|
|
|
$post_cur->post_open_tb = $v;
|
2022-11-20 20:43:32 +00:00
|
|
|
|
|
|
|
break;
|
2021-08-20 19:36:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# 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) . "' "
|
2021-08-20 19:36:46 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$updated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Say blog is updated
|
|
|
|
if ($updated) {
|
2022-11-14 20:47:24 +00:00
|
|
|
dcCore::app()->blog->triggerBlog();
|
2021-08-20 19:36:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2021-08-20 19:36:46 +00:00
|
|
|
*/
|
2022-11-14 20:47:24 +00:00
|
|
|
public static function coreBlogGetPosts(dcRecord $rs)
|
2021-08-20 19:36:46 +00:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
{
|
2021-08-20 19:36:46 +00:00
|
|
|
/**
|
|
|
|
* Retrieve expired date of a post
|
2022-11-20 20:43:32 +00:00
|
|
|
*
|
2021-08-20 19:36:46 +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)
|
2021-08-20 19:36:46 +00:00
|
|
|
{
|
|
|
|
if (!$rs->postexpired[$rs->post_id]) { //memory
|
2021-08-20 21:35:03 +00:00
|
|
|
$rs_date = $rs->core->meta->getMetadata([
|
2021-08-20 19:36:46 +00:00
|
|
|
'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
|
|
|
]);
|
2021-08-20 19:36:46 +00:00
|
|
|
|
|
|
|
if ($rs_date->isEmpty()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-11-20 20:43:32 +00:00
|
|
|
$v = decodePostExpired($rs_date->meta_id);
|
2021-08-20 19:36:46 +00:00
|
|
|
$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
|
|
|
|
{
|
2021-08-20 19:36:46 +00:00
|
|
|
/**
|
|
|
|
* Template condition to check if there is an expired date
|
2022-11-20 20:43:32 +00:00
|
|
|
*
|
2021-08-20 19:36:46 +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']) ?
|
2021-08-20 19:36:46 +00:00
|
|
|
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())';
|
2021-08-20 19:36:46 +00:00
|
|
|
} else {
|
2022-11-14 20:47:24 +00:00
|
|
|
$if[] = '(null !== dcCore::app()->ctx->posts->postExpiredDate())';
|
2021-08-20 19:36:46 +00:00
|
|
|
}
|
|
|
|
|
2022-11-20 20:43:32 +00:00
|
|
|
return
|
|
|
|
'<?php if(' . implode(' ' . $operator . ' ', $if) . ") : ?>\n" .
|
2021-08-20 19:36:46 +00:00
|
|
|
$content .
|
|
|
|
"<?php endif; ?>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Template for expired date
|
2022-11-20 20:43:32 +00:00
|
|
|
*
|
2021-08-20 19:36:46 +00:00
|
|
|
* @param array $attr Value attributes
|
|
|
|
*/
|
|
|
|
public static function EntryExpiredDate($attr)
|
|
|
|
{
|
2022-11-20 20:43:32 +00:00
|
|
|
$format = !empty($attr['format']) ?
|
2021-08-20 19:36:46 +00:00
|
|
|
addslashes($attr['format']) : '';
|
2022-11-14 20:47:24 +00:00
|
|
|
$f = dcCore::app()->tpl->getFilters($attr);
|
2021-08-20 19:36:46 +00:00
|
|
|
|
|
|
|
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)');
|
2021-08-20 19:36:46 +00:00
|
|
|
} 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)');
|
2021-08-20 19:36:46 +00:00
|
|
|
} elseif ($format) {
|
2022-11-14 20:47:24 +00:00
|
|
|
$res = sprintf($f, "dt::dt2str('" . $format . "',dcCore::app()->ctx->posts->postExpiredDate())");
|
2021-08-20 19:36:46 +00:00
|
|
|
} 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())');
|
2021-08-20 19:36:46 +00:00
|
|
|
}
|
|
|
|
|
2022-11-14 20:47:24 +00:00
|
|
|
return '<?php if (null !== dcCore::app()->ctx->posts->postExpiredDate()) { echo ' . $res . '; } ?>';
|
2021-08-20 19:36:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Template for expired time
|
2022-11-20 20:43:32 +00:00
|
|
|
*
|
2021-08-20 19:36:46 +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())'
|
2021-08-20 19:36:46 +00:00
|
|
|
) . '; } ?>';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse tempalte attributes oprerator
|
2022-11-20 20:43:32 +00:00
|
|
|
*
|
2021-08-20 19:36:46 +00:00
|
|
|
* @param string $op Operator
|
|
|
|
*/
|
|
|
|
protected static function getOperator($op)
|
|
|
|
{
|
2022-11-20 20:43:32 +00:00
|
|
|
switch (strtolower($op)) {
|
2021-08-20 19:36:46 +00:00
|
|
|
case 'or':
|
|
|
|
case '||':
|
|
|
|
return '||';
|
|
|
|
case 'and':
|
|
|
|
case '&&':
|
|
|
|
default:
|
|
|
|
return '&&';
|
|
|
|
}
|
|
|
|
}
|
2022-11-20 20:43:32 +00:00
|
|
|
}
|