Commit 317496dc authored by Rolf Forst's avatar Rolf Forst
Browse files

Update plugin version

parent 0d2e76a0
<?php
/*
Plugin Name: RRZE Post Expiration
Plugin URI: https://gitlab.rrze.fau.de/rrze-webteam/rrze-post-expiration
Version: 1.3.0
Description: Verfallsdatum für Beiträge und Seiten.
Author: RRZE-Webteam
License: GPLv2 or later
*/
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
/**
* Plugin Name: RRZE Post Expiration
* Plugin URI: https://gitlab.rrze.fau.de/rrze-webteam/rrze-post-expiration
* Description: Verfallsdatum für Beiträge und Seiten.
* Version: 1.4.0
* Author: RRZE-Webteam
* Author URI: https://blogs.fau.de/webworking/
* License: GNU General Public License v2
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Domain Path: /languages
* Text Domain: rrze-post-expiration
*/
load_plugin_textdomain('rrze-post-expiration', FALSE, dirname(plugin_basename(__FILE__)) . '/languages');
add_action('plugins_loaded', array('RRZE_Post_Expiration', 'instance'));
register_activation_hook(__FILE__, array('RRZE_Post_Expiration', 'activation'));
register_deactivation_hook(__FILE__, array('RRZE_Post_Expiration', 'deactivation'));
class RRZE_Post_Expiration {
const option_name = 'rrze_post_expiration';
const php_version = '5.5';
const wp_version = '4.6';
protected static $cron_hook;
protected static $allowed_post_types = array('post', 'page');
protected static $options;
private static $instance = NULL;
public static function instance() {
if (self::$instance == NULL) {
self::$instance = new self;
}
return self::$instance;
}
private function __construct() {
global $current_blog;
self::$options = self::get_options();
add_action('admin_menu', array(__CLASS__, 'add_options_page'));
add_action('admin_init', array(__CLASS__, 'settings_init'));
add_action('admin_enqueue_scripts', array(__CLASS__, 'enqueue_scripts'));
if (!empty(self::$options->post_types)) {
foreach (self::$options->post_types as $post_type) {
add_filter('manage_' . $post_type . '_posts_columns', array(__CLASS__, 'expiration_date_column'));
add_action('manage_' . $post_type . '_posts_custom_column', array(__CLASS__, 'expiration_date_custom_column'), 10, 2);
add_filter('manage_edit-' . $post_type . '_sortable_columns', array(__CLASS__, 'expiration_date_sortable_column'));
}
add_action('pre_get_posts', array(__CLASS__, 'expiration_date_orderby'));
add_action('post_submitbox_misc_actions', array(__CLASS__, 'exp_meta_box'));
add_action('admin_head', array(__CLASS__, 'expiration_js'));
add_action('save_post', array(__CLASS__, 'update_post_expiration_date'));
add_filter('cron_schedules', array(__CLASS__, 'add_cron_schedule'));
self::$cron_hook = sprintf('%s_%s', self::option_name, $current_blog->blog_id);
add_action(self::$cron_hook, array(__CLASS__, 'set_expired_posts'));
}
}
public static function activation() {
self::verify_system_requirements();
}
public static function deactivation() {
wp_clear_scheduled_hook(self::$cron_hook);
}
private static function verify_system_requirements() {
$error = '';
if (version_compare(PHP_VERSION, self::php_version, '<')) {
$error = sprintf(__('Ihre PHP-Version %s ist veraltet. Bitte aktualisieren Sie mindestens auf die PHP-Version %s.', 'rrze-post-expiration'), PHP_VERSION, self::php_version);
} elseif (version_compare($GLOBALS['wp_version'], self::wp_version, '<')) {
$error = sprintf(__('Ihre Wordpress-Version %s ist veraltet. Bitte aktualisieren Sie mindestens auf die Wordpress-Version %s.', 'rrze-post-expiration'), $GLOBALS['wp_version'], self::wp_version);
}
if (!empty($error)) {
deactivate_plugins(plugin_basename(__FILE__), FALSE, TRUE);
wp_die($error);
}
}
private static function default_options() {
$options = array(
'post_types' => array('post')
);
return $options;
}
private static function get_options() {
$defaults = self::default_options();
$options = (array) get_option(self::option_name);
$options = wp_parse_args($options, $defaults);
$options = array_intersect_key($options, $defaults);
return (object) $options;
}
public static function add_options_page() {
add_options_page(__('Verfallsdatum', 'rrze-post-expiration'), __('Verfallsdatum', 'rrze-post-expiration'), 'manage_options', 'options-post-expiration', array(__CLASS__, 'options_page'));
}
public static function options_page() {
?>
<div class="wrap">
<h2><?php echo esc_html(__('Verfallsdatum', 'rrze-post-expiration')); ?></h2>
<form method="post" action="options.php">
<?php
settings_fields('post_expiration_options');
do_settings_sections('post_expiration_options');
submit_button();
?>
</form>
</div>
<?php
}
public static function settings_init() {
register_setting('post_expiration_options', self::option_name, array(__CLASS__, 'options_validate'));
add_settings_section('options_general_section', FALSE, '__return_false', 'post_expiration_options');
add_settings_field('options_general', __('Freigabe', 'rrze-post-expiration'), array(__CLASS__, 'field_options_general'), 'post_expiration_options', 'options_general_section');
}
public static function field_options_general() {
?>
<ul>
<?php foreach (self::$allowed_post_types as $post_type): ?>
<li>
<label for="<?php echo $post_type; ?>"><input type="checkbox" <?php checked(in_array($post_type, self::$options->post_types), TRUE); ?> name="<?php printf('%s[post_types][]', self::option_name); ?>" value="<?php echo $post_type; ?>" id="<?php echo $post_type; ?>" /> <?php echo self::get_post_type_label($post_type); ?></label>
</li>
<?php endforeach; ?>
</ul>
<?php
}
public static function options_validate($input) {
if (!empty($input['post_types'])) {
foreach ($input['post_types'] as $key => $post_type) {
if (!in_array($post_type, self::$allowed_post_types)) {
unset($input['post_types'][$key]);
}
}
} else {
$input['post_types'] = array();
}
return $input;
}
private static function get_post_type_label($post_type = 'post') {
$post_types = get_post_types(array('show_in_menu' => true), 'objects');
$labels = wp_list_pluck($post_types, 'labels');
$labels = wp_list_pluck($labels, 'name');
return isset($labels[$post_type]) ? $labels[$post_type] : '';
}
public static function enqueue_scripts() {
wp_enqueue_style('post-expiration', plugins_url('css/post-expiration.css', __FILE__ ), 'all', NULL);
}
public static function expiration_date_column($columns) {
$columns['expiration_date'] = __('Verfallsdatum', 'rrze-post-expiration');
return $columns;
}
public static function expiration_date_custom_column($column, $post_id) {
if ($column != 'expiration_date') {
return;
}
$post = get_post($post_id);
$expiration_date = get_post_meta($post->ID, 'expiration_date', TRUE);
$expiration_date_gmt = get_post_meta($post->ID, 'expiration_date_gmt', TRUE);
if ($post->post_date == '0000-00-00 00:00:00' || !$expiration_date) {
echo '&#8212;';
} else {
$t_time = date(__('Y/m/d g:i:s A'), mysql2date('U', $expiration_date));
$m_time = $expiration_date;
$time = mysql2date('G', $expiration_date_gmt);
$time_diff = $time - time();
if ($time_diff > 0 && $time_diff < 24 * 60 * 60) {
$h_time = sprintf(__('in %s'), human_time_diff($time));
} elseif ($time_diff < 0 && abs($time_diff) < 24 * 60 * 60) {
$h_time = sprintf(__('%s ago'), human_time_diff($time));
} else {
$h_time = mysql2date(__('Y/m/d'), $m_time);
}
echo '<abbr title="' . $t_time . '">' . $h_time . '</abbr>';
if ($time_diff < 0) {
echo '<br/>', __('abgelaufen', 'rrze-post-expiration');
}
}
}
public static function expiration_date_sortable_column($columns) {
$columns['expiration_date'] = 'expiration_date';
return $columns;
}
public static function expiration_date_orderby($query) {
if (!is_admin()) {
return;
}
$orderby = $query->get('orderby');
if('expiration_date' == $orderby) {
$query->set('orderby', 'meta_value');
$query->set('meta_query', array(
'relation' => 'OR',
array(
'key' => 'expiration_date',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'expiration_date',
'compare' => 'EXISTS'
),
)
);
}
}
public static function exp_meta_box() {
global $post, $post_type, $post_type_object;
if (!in_array($post_type, self::$options->post_types)) {
return;
}
$can_publish = current_user_can($post_type_object->cap->publish_posts);
if ($can_publish) :
$cur_time = strtotime(current_time('mysql'));
$datef = __('j. M Y @ H:i', 'rrze-post-expiration');
$date = date_i18n($datef, $cur_time);
$stamp = __('Verfallsdatum <b>deaktiviert</b>', 'rrze-post-expiration');
if (0 != $post->ID) {
$expiration_date = get_post_meta($post->ID, 'expiration_date', TRUE);
if ($expiration_date) {
$exp_time = strtotime($expiration_date);
if ($exp_time > $cur_time) {
$stamp = __('Verfällt am: <b>%s</b>', 'rrze-post-expiration');
} else {
$stamp = __('Abgelaufen am: <b>%s</b>', 'rrze-post-expiration');
}
$enabled = ' checked="checked"';
$date = date_i18n($datef, strtotime($expiration_date));
}
}
?>
<div class="misc-pub-section curtime misc-pub-section-last">
<span id="timestamp" class="exp_timestamp">
<?php printf($stamp, $date); ?></span>
<a href="#edit_exp_timestamp" class="edit-exp-timestamp hide-if-no-js" tabindex="4"><?php _e('Bearbeiten', 'rrze-post-expiration') ?></a>
<div id="exp_timestampdiv" class="hide-if-js">
<?php self::expire_time(); ?>
</div>
</div>
<?php
endif;
}
private static function expire_time() {
global $wp_locale, $post;
$expiration_date = get_post_meta($post->ID, 'expiration_date', TRUE);
$expiration_date_gmt = get_post_meta($post->ID, 'expiration_date_gmt', TRUE);
$enabled = '';
if ($expiration_date) {
$enabled = ' checked="checked"';
}
$edit = !(!$expiration_date );
$time_adj = current_time('timestamp');
$jj = ($edit) ? mysql2date('d', $expiration_date, FALSE) : gmdate('d', $time_adj);
$mm = ($edit) ? mysql2date('m', $expiration_date, FALSE) : gmdate('m', $time_adj);
$aa = ($edit) ? mysql2date('Y', $expiration_date, FALSE) : gmdate('Y', $time_adj);
$hh = ($edit) ? mysql2date('H', $expiration_date, FALSE) : gmdate('H', $time_adj);
$mn = ($edit) ? mysql2date('i', $expiration_date, FALSE) : gmdate('i', $time_adj);
$ss = ($edit) ? mysql2date('s', $expiration_date, FALSE) : gmdate('s', $time_adj);
$cur_jj = gmdate('d', $time_adj);
$cur_mm = gmdate('m', $time_adj);
$cur_aa = gmdate('Y', $time_adj);
$cur_hh = gmdate('H', $time_adj);
$cur_mn = gmdate('i', $time_adj);
$month = "<select id=\"exp_mm\" name=\"exp_mm\" tabindex=\"4\" style=\"height:20px; line-height:14px; padding:0; vertical-align:top;\">\n";
for ($i = 1; $i < 13; $i = $i + 1) {
$month .= "\t\t\t" . '<option value="' . zeroise($i, 2) . '"';
if ($i == $mm)
$month .= ' selected="selected"';
$month .= '>' . $wp_locale->get_month_abbrev($wp_locale->get_month($i)) . "</option>\n";
}
$month .= '</select>';
namespace RRZE\PostExpiration;
$day = '<input type="text" id="exp_jj" name="exp_jj" value="' . $jj . '" size="2" maxlength="2" tabindex="4" autocomplete="off" style="font-size:12px; padding:1px; width:2em;" />';
$year = '<input type="text" id="exp_aa" name="exp_aa" value="' . $aa . '" size="4" maxlength="4" tabindex="4" autocomplete="off" style="font-size:12px; padding:1px; width:3.4em;" />';
$hour = '<input type="text" id="exp_hh" name="exp_hh" value="' . $hh . '" size="2" maxlength="2" tabindex="4" autocomplete="off" style="font-size:12px; padding:1px; width:2em;" />';
$minute = '<input type="text" id="exp_mn" name="exp_mn" value="' . $mn . '" size="2" maxlength="2" tabindex="4" autocomplete="off" style="font-size:12px; padding:1px; width:2em;" />';
defined('ABSPATH') || exit;
echo '<div class="exp-timestamp-wrap">';
echo '<p style="margin:0.5em 0;"><input type="checkbox" name="enable_expiration" id="enable_expiration" value="checked"' . $enabled . ' />';
echo '<label class="selectit" for="enable_expiration">' . __('Verfallsdatum aktivieren', 'rrze-post-expiration') . '</label></p>';
const RRZE_PHP_VERSION = '7.1';
const RRZE_WP_VERSION = '5.0';
/* 1: month input, 2: day input, 3: year input, 4: hour input, 5: minute input */
printf(__('%1$s%2$s, %3$s @ %4$s : %5$s'), $month, $day, $year, $hour, $minute);
require 'autoload.php';
echo '</div><input type="hidden" id="exp_ss" name="exp_ss" value="' . $ss . '" />';
echo "\n\n";
register_activation_hook(__FILE__, 'RRZE\PostExpiration\activation');
if ($enabled) {
echo '<input type="hidden" id="exp_enabled" name="exp_enabled" value="1" />';
} else {
echo '<input type="hidden" id="exp_enabled" name="exp_enabled" value="0" />';
}
echo "\n\n";
add_action('plugins_loaded', 'RRZE\PostExpiration\loaded');
foreach (array('mm', 'jj', 'aa', 'hh', 'mn') as $timeunit) {
echo '<input type="hidden" id="exp_hidden_' . $timeunit . '" name="hidden_' . $timeunit . '" value="' . $$timeunit . '" />' . "\n";
$cur_timeunit = 'cur_' . $timeunit;
echo '<input type="hidden" id="exp_' . $cur_timeunit . '" name="exp_' . $cur_timeunit . '" value="' . $$cur_timeunit . '" />' . "\n";
}
?>
<p>
<a href="#edit_exp_timestamp" class="save-exp-timestamp hide-if-no-js button"><?php _e('OK', 'rrze-post-expiration'); ?></a>
<a href="#edit_exp_timestamp" class="cancel-exp-timestamp hide-if-no-js"><?php _e('Abbrechen', 'rrze-post-expiration'); ?></a>
</p>
<?php
}
public static function expiration_js() {
?>
<script type="text/javascript">
//<![CDATA[
var $j = jQuery.noConflict();
$j(function () {
enabled = $j('#exp_enabled').val();
$j('#exp_timestampdiv').siblings('a.edit-exp-timestamp').click(function () {
if ($j('#exp_timestampdiv').is(":hidden")) {
$j('#exp_timestampdiv').slideDown("normal");
$j(this).hide();
}
return FALSE;
});
$j('.cancel-exp-timestamp', '#exp_timestampdiv').click(function () {
$j('#exp_timestampdiv').slideUp("normal");
$j('#exp_mm').val($j('#exp_hidden_mm').val());
$j('#exp_jj').val($j('#exp_hidden_jj').val());
$j('#exp_aa').val($j('#exp_hidden_aa').val());
$j('#exp_hh').val($j('#exp_hidden_hh').val());
$j('#exp_mn').val($j('#exp_hidden_mn').val());
$j('#exp_timestampdiv').siblings('a.edit-exp-timestamp').show();
updateExpiration();
return FALSE;
});
$j('.save-exp-timestamp', '#exp_timestampdiv').click(function () {
if (updateExpiration()) {
$j('#exp_timestampdiv').slideUp("normal");
$j('#exp_timestampdiv').siblings('a.edit-exp-timestamp').show();
}
return FALSE;
});
function updateExpiration() {
var stamp = $j('.exp_timestamp').html(), attemptedDate, postDate, currentDate,
postStatus = $j('#post_status'), optPublish = $j('option[value=publish]', postStatus), aa = $j('#exp_aa').val(),
mm = $j('#exp_mm').val(), jj = $j('#exp_jj').val(), hh = $j('#exp_hh').val(), mn = $j('#exp_mn').val(), txt = '';
attemptedDate = new Date(aa, mm - 1, jj, hh, mn);
postDate = new Date($j('#exp_hidden_aa').val(), $j('#exp_hidden_mm').val() - 1, $j('#exp_hidden_jj').val(), $j('#exp_hidden_hh').val(), $j('#exp_hidden_mn').val());
currentDate = new Date($j('#exp_cur_aa').val(), $j('#exp_cur_mm').val() - 1, $j('#exp_cur_jj').val(), $j('#exp_cur_hh').val(), $j('#exp_cur_mn').val());
if (attemptedDate.getFullYear() != aa || (1 + attemptedDate.getMonth()) != mm || attemptedDate.getDate() != jj || attemptedDate.getMinutes() != mn) {
$j('.exp-timestamp-wrap', '#exp_timestampdiv').addClass('form-invalid');
return FALSE;
} else {
$j('.exp-timestamp-wrap', '#exp_timestampdiv').removeClass('form-invalid');
}
if ($j('#enable_expiration').attr('checked') == 'checked') {
if (attemptedDate > currentDate)
txt = '<?php _e('Verfällt am:', 'rrze-post-expiration'); ?>';
else
txt = '<?php _e('Abgelaufen am:', 'rrze-post-expiration'); ?>';
if (postDate.toUTCString() == attemptedDate.toUTCString() && enabled == 1) {
$j('.exp_timestamp').html(stamp);
} else {
$j('.exp_timestamp').html(
txt + ' <b>' +
$j('option[value=' + $j('#exp_mm').val() + ']', '#exp_mm').text() + ' ' +
jj + ', ' +
aa + ' @ ' +
hh + ':' +
mn + '</b> '
);
}
} else {
enabled = 0;
$j('.exp_timestamp').html('<?php _e('Verfallsdatum <b>deaktiviert</b>', 'rrze-post-expiration'); ?>');
}
return TRUE;
}
});
//]]>
</script>
<?php
}
public static function update_post_expiration_date() {
global $current_blog, $post;
if (!$post || !in_array($post->post_type, self::$options->post_types)) {
return;
}
delete_post_meta($post->ID, 'expiration_date');
delete_post_meta($post->ID, 'expiration_date_gmt');
if (isset($_POST['enable_expiration'])) {
$aa = $_POST['exp_aa'];
$mm = $_POST['exp_mm'];
$jj = $_POST['exp_jj'];
$hh = $_POST['exp_hh'];
$mn = $_POST['exp_mn'];
$ss = $_POST['exp_ss'];
$aa = ($aa <= 0 ) ? date('Y') : $aa;
$mm = ($mm <= 0 ) ? date('n') : $mm;
$jj = ($jj > 31 ) ? 31 : $jj;
$jj = ($jj <= 0 ) ? date('j') : $jj;
$hh = ($hh > 23 ) ? $hh - 24 : $hh;
$mn = ($mn > 59 ) ? $mn - 60 : $mn;
$ss = ($ss > 59 ) ? $ss - 60 : $ss;
function load_textdomain() {
load_plugin_textdomain('rrze-post-expiration', false, sprintf('%s/languages/', dirname(plugin_basename(__FILE__))));
}
$ts = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss);
function activation() {
load_textdomain();
update_post_meta($post->ID, 'expiration_date', $ts, TRUE);
system_requirements();
}
$ts_gmt = get_gmt_from_date($ts);
update_post_meta($post->ID, 'expiration_date_gmt', $ts_gmt, TRUE);
function system_requirements() {
$error = '';
wp_clear_scheduled_hook(self::$cron_hook);
wp_schedule_event(time(), 'two-minutes', self::$cron_hook);
}
if (version_compare(PHP_VERSION, RRZE_PHP_VERSION, '<')) {
$error = sprintf(__('Your server is running PHP version %s. Please upgrade at least to PHP version %s.', 'rrze-post-expiration'), PHP_VERSION, RRZE_PHP_VERSION);
}
public static function add_cron_schedule($schedules) {
$schedules['two-minutes'] = array(
'interval' => 120,
'display' => __('Every Two Minutes')
);
return $schedules;
if (version_compare($GLOBALS['wp_version'], RRZE_WP_VERSION, '<')) {
$error = sprintf(__('Your Wordpress version is %s. Please upgrade at least to Wordpress version %s.', 'rrze-post-expiration'), $GLOBALS['wp_version'], RRZE_WP_VERSION);
}
public static function set_expired_posts() {
global $wpdb;
$cur_date = current_time('mysql');
$pt_sql = array();
foreach (self::$options->post_types as $post_type) {
$pt_sql[] = sprintf("posts.post_type = '%s'", $post_type);
}
$sql =
"SELECT post_id
FROM " . $wpdb->postmeta . " as postmeta
LEFT JOIN " . $wpdb->posts . " as posts ON postmeta.post_id = posts.ID
WHERE posts.post_status = 'publish' AND (" . implode(' OR ', $pt_sql) . ")
AND postmeta.meta_key = 'expiration_date'
AND postmeta.meta_value <= '" . $cur_date . "'";
$result = $wpdb->get_results($sql);
if (!empty($result)) {
foreach ($result as $row) {
self::flush_cache(get_permalink($row->post_id));
wp_update_post(array('ID' => $row->post_id, 'post_status' => 'draft'));
}
}
if (!empty($error)) {
deactivate_plugins(plugin_basename(__FILE__), false, true);
wp_die($error);
}
}
public static function flush_cache($url) {
if (empty($url)) {
return;
}
function loaded() {
load_textdomain();
// rrze-cache plugin
if (has_action('rrzecache_flush_cache_url')) {
do_action('rrzecache_flush_cache_url', $url);
}
}
return new Main(plugin_basename(__FILE__));
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment