Commit cf27006c authored by Rolf Forst's avatar Rolf Forst
Browse files

Add options (allowed post types) and settings.

parent 09a3ec98
......@@ -4,4 +4,6 @@ RRZE Post Expiration
Wordpress-Plugin
----------------
Verfallsdatum für Beiträge.
Verfallsdatum für Beiträge und Seiten.
Einstellungsmenü: Einstellungen › Verfallsdatum
......@@ -2,7 +2,7 @@ msgid ""
msgstr ""
"Project-Id-Version: RRZE Post Expiration\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2016-06-01 15:08+0200\n"
"POT-Creation-Date: 2016-06-15 09:22+0200\n"
"PO-Revision-Date: \n"
"Last-Translator: \n"
"Language-Team: RRZE-Webteam <webmaster@rrze.fau.de>\n"
......@@ -17,7 +17,7 @@ msgstr ""
"X-Generator: Poedit 1.8.8\n"
"X-Poedit-SearchPath-0: ..\n"
#: ../rrze-post-expiration.php:82
#: ../rrze-post-expiration.php:98
#, php-format
msgid ""
"Ihre PHP-Version %s ist veraltet. Bitte aktualisieren Sie mindestens auf die "
......@@ -26,7 +26,7 @@ msgstr ""
"Your server is running PHP version %s. Please upgrade at least to PHP "
"version %s."
#: ../rrze-post-expiration.php:84
#: ../rrze-post-expiration.php:100
#, php-format
msgid ""
"Ihre Wordpress-Version %s ist veraltet. Bitte aktualisieren Sie mindestens "
......@@ -35,83 +35,91 @@ msgstr ""
"Your Wordpress version is %s. Please upgrade at least to Wordpress version "
"%s."
#: ../rrze-post-expiration.php:98
#: ../rrze-post-expiration.php:129 ../rrze-post-expiration.php:135
#: ../rrze-post-expiration.php:193
msgid "Verfallsdatum"
msgstr "Expiration"
#: ../rrze-post-expiration.php:116
#: ../rrze-post-expiration.php:150
msgid "Freigabe"
msgstr "Allowed Post Types"
#: ../rrze-post-expiration.php:210
msgid "Y/m/d g:i:s A"
msgstr "Y/m/d g:i:s A"
#: ../rrze-post-expiration.php:124
#: ../rrze-post-expiration.php:218
#, php-format
msgid "in %s"
msgstr "in %s"
#: ../rrze-post-expiration.php:127
#: ../rrze-post-expiration.php:221
#, php-format
msgid "%s ago"
msgstr "%s ago"
#: ../rrze-post-expiration.php:129
#: ../rrze-post-expiration.php:223
msgid "Y/m/d"
msgstr "Y/m/d"
#: ../rrze-post-expiration.php:133
#: ../rrze-post-expiration.php:229
msgid "abgelaufen"
msgstr "expired"
#: ../rrze-post-expiration.php:177
#: ../rrze-post-expiration.php:273
msgid "j. M Y @ H:i"
msgstr "M j, Y @ H:i"
#: ../rrze-post-expiration.php:179 ../rrze-post-expiration.php:350
#: ../rrze-post-expiration.php:275 ../rrze-post-expiration.php:446
msgid "Verfallsdatum <b>deaktiviert</b>"
msgstr "Expiration <b>disabled</b>"
#: ../rrze-post-expiration.php:185
#: ../rrze-post-expiration.php:281
#, php-format
msgid "Verfällt am: <b>%s</b>"
msgstr "Expires on: <b>%s</b>"
#: ../rrze-post-expiration.php:187
#: ../rrze-post-expiration.php:283
#, php-format
msgid "Abgelaufen am: <b>%s</b>"
msgstr "Expired on: <b>%s</b>"
#: ../rrze-post-expiration.php:197
#: ../rrze-post-expiration.php:293
msgid "Bearbeiten"
msgstr "Edit"
#: ../rrze-post-expiration.php:250
#: ../rrze-post-expiration.php:346
msgid "Verfallsdatum aktivieren"
msgstr "Enable expiration"
#: ../rrze-post-expiration.php:253
#: ../rrze-post-expiration.php:349
#, php-format
msgid "%1$s%2$s, %3$s @ %4$s : %5$s"
msgstr "%1$s%2$s, %3$s @ %4$s : %5$s"
#: ../rrze-post-expiration.php:273
#: ../rrze-post-expiration.php:369
msgid "OK"
msgstr "OK"
#: ../rrze-post-expiration.php:274
#: ../rrze-post-expiration.php:370
msgid "Abbrechen"
msgstr "Cancel"
#: ../rrze-post-expiration.php:331
#: ../rrze-post-expiration.php:427
msgid "Verfällt am:"
msgstr "Expires on:"
#: ../rrze-post-expiration.php:333
#: ../rrze-post-expiration.php:429
msgid "Abgelaufen am:"
msgstr "Expired on:"
#: ../rrze-post-expiration.php:403
#: ../rrze-post-expiration.php:498
msgid "Every Two Minutes"
msgstr "Every Two Minutes"
#~ msgid "Einstellungen &rsaquo; Verfallsdatum"
#~ msgstr "Settings &rsaquo; Expiration"
#~ msgid "Keine"
#~ msgstr "None"
......
......@@ -3,7 +3,7 @@ msgid ""
msgstr ""
"Project-Id-Version: RRZE Post Expiration\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2016-06-01 15:08+0200\n"
"POT-Creation-Date: 2016-06-15 09:22+0200\n"
"PO-Revision-Date: \n"
"Last-Translator: \n"
"Language-Team: RRZE-Webteam <webmaster@rrze.fau.de>\n"
......@@ -18,93 +18,98 @@ msgstr ""
"X-Generator: Poedit 1.8.8\n"
"X-Poedit-SearchPath-0: ..\n"
#: ../rrze-post-expiration.php:82
#: ../rrze-post-expiration.php:98
#, php-format
msgid ""
"Ihre PHP-Version %s ist veraltet. Bitte aktualisieren Sie mindestens auf die "
"PHP-Version %s."
msgstr ""
#: ../rrze-post-expiration.php:84
#: ../rrze-post-expiration.php:100
#, php-format
msgid ""
"Ihre Wordpress-Version %s ist veraltet. Bitte aktualisieren Sie mindestens "
"auf die Wordpress-Version %s."
msgstr ""
#: ../rrze-post-expiration.php:98
#: ../rrze-post-expiration.php:129 ../rrze-post-expiration.php:135
#: ../rrze-post-expiration.php:193
msgid "Verfallsdatum"
msgstr ""
#: ../rrze-post-expiration.php:116
#: ../rrze-post-expiration.php:150
msgid "Freigabe"
msgstr ""
#: ../rrze-post-expiration.php:210
msgid "Y/m/d g:i:s A"
msgstr ""
#: ../rrze-post-expiration.php:124
#: ../rrze-post-expiration.php:218
#, php-format
msgid "in %s"
msgstr ""
#: ../rrze-post-expiration.php:127
#: ../rrze-post-expiration.php:221
#, php-format
msgid "%s ago"
msgstr ""
#: ../rrze-post-expiration.php:129
#: ../rrze-post-expiration.php:223
msgid "Y/m/d"
msgstr ""
#: ../rrze-post-expiration.php:133
#: ../rrze-post-expiration.php:229
msgid "abgelaufen"
msgstr ""
#: ../rrze-post-expiration.php:177
#: ../rrze-post-expiration.php:273
msgid "j. M Y @ H:i"
msgstr ""
#: ../rrze-post-expiration.php:179 ../rrze-post-expiration.php:350
#: ../rrze-post-expiration.php:275 ../rrze-post-expiration.php:446
msgid "Verfallsdatum <b>deaktiviert</b>"
msgstr ""
#: ../rrze-post-expiration.php:185
#: ../rrze-post-expiration.php:281
#, php-format
msgid "Verfällt am: <b>%s</b>"
msgstr ""
#: ../rrze-post-expiration.php:187
#: ../rrze-post-expiration.php:283
#, php-format
msgid "Abgelaufen am: <b>%s</b>"
msgstr ""
#: ../rrze-post-expiration.php:197
#: ../rrze-post-expiration.php:293
msgid "Bearbeiten"
msgstr ""
#: ../rrze-post-expiration.php:250
#: ../rrze-post-expiration.php:346
msgid "Verfallsdatum aktivieren"
msgstr ""
#: ../rrze-post-expiration.php:253
#: ../rrze-post-expiration.php:349
#, php-format
msgid "%1$s%2$s, %3$s @ %4$s : %5$s"
msgstr ""
#: ../rrze-post-expiration.php:273
#: ../rrze-post-expiration.php:369
msgid "OK"
msgstr ""
#: ../rrze-post-expiration.php:274
#: ../rrze-post-expiration.php:370
msgid "Abbrechen"
msgstr ""
#: ../rrze-post-expiration.php:331
#: ../rrze-post-expiration.php:427
msgid "Verfällt am:"
msgstr ""
#: ../rrze-post-expiration.php:333
#: ../rrze-post-expiration.php:429
msgid "Abgelaufen am:"
msgstr ""
#: ../rrze-post-expiration.php:403
#: ../rrze-post-expiration.php:498
msgid "Every Two Minutes"
msgstr ""
......@@ -2,8 +2,8 @@
/*
Plugin Name: RRZE Post Expiration
Plugin URI: https://gitlab.rrze.fau.de/rrze-webteam/rrze-post-expiration
Version: 1.1.2
Description: Verfallsdatum für Beiträge.
Version: 1.2.0
Description: Verfallsdatum für Beiträge und Seiten.
Author: RRZE-Webteam
License: GPLv2 or later
*/
......@@ -32,11 +32,17 @@ register_activation_hook(__FILE__, array('RRZE_Post_Expiration', 'activation'));
class RRZE_Post_Expiration {
const option_name = '_rrze_post_expiration';
const option_name = 'rrze_post_expiration';
const php_version = '5.4';
const wp_version = '4.5';
protected static $cron_hook;
protected static $allowed_post_types = array('post', 'page');
protected static $options;
private static $instance = NULL;
public static function instance() {
......@@ -51,24 +57,34 @@ class RRZE_Post_Expiration {
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'));
add_filter('manage_post_posts_columns', array(__CLASS__, 'expiration_date_column'));
add_action('manage_post_posts_custom_column', array(__CLASS__, 'expiration_date_custom_column'), 10, 2);
add_filter('manage_edit-post_sortable_columns', array(__CLASS__, 'expiration_date_sortable_column'));
add_action('pre_get_posts', array(__CLASS__, 'expiration_date_orderby'));
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('post_submitbox_misc_actions', array(__CLASS__, 'exp_meta_box'));
add_action('pre_get_posts', array(__CLASS__, 'expiration_date_orderby'));
add_action('admin_head', array(__CLASS__, 'expiration_js'));
add_action('post_submitbox_misc_actions', array(__CLASS__, 'exp_meta_box'));
add_action('save_post', array(__CLASS__, 'update_post_expiration_date'));
add_action('admin_head', array(__CLASS__, 'expiration_js'));
add_filter('cron_schedules', array(__CLASS__, 'add_cron_schedule'));
add_action(self::option_name, array(__CLASS__, 'set_expired_posts'));
add_action('save_post', array(__CLASS__, 'update_post_expiration_date'));
$cron_hook = sprintf('%s_%s', self::option_name, $current_blog->blog_id);
add_action($cron_hook, array(__CLASS__, 'set_expired_posts'));
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() {
......@@ -85,11 +101,90 @@ class RRZE_Post_Expiration {
}
if (!empty($error)) {
deactivate_plugins(plugin_basename(__FILE__), false, true);
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);
}
......@@ -106,13 +201,12 @@ class RRZE_Post_Expiration {
$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);
$expiration_date = get_post_meta($post->ID, 'expiration_date', TRUE);
$expiration_date_gmt = get_post_meta($post->ID, 'expiration_date_gmt', TRUE);
if (( '0000-00-00 00:00:00' == $post->post_date ) || !$expiration_date)
if ($post->post_date == '0000-00-00 00:00:00' || !$expiration_date) {
echo '&#8212;';
else {
} else {
$t_time = date(__('Y/m/d g:i:s A'), mysql2date('U', $expiration_date));
$m_time = $expiration_date;
......@@ -128,7 +222,9 @@ class RRZE_Post_Expiration {
} 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');
}
......@@ -167,7 +263,7 @@ class RRZE_Post_Expiration {
public static function exp_meta_box() {
global $post, $post_type, $post_type_object;
if ($post_type != 'post') {
if (!in_array($post_type, self::$options->post_types)) {
return;
}
......@@ -178,7 +274,7 @@ class 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);
$expiration_date = get_post_meta($post->ID, 'expiration_date', TRUE);
if ($expiration_date) {
$exp_time = strtotime($expiration_date);
if ($exp_time > $cur_time) {
......@@ -206,8 +302,8 @@ class RRZE_Post_Expiration {
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);
$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) {
......@@ -218,12 +314,12 @@ class RRZE_Post_Expiration {
$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);
$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);
......@@ -289,7 +385,7 @@ class RRZE_Post_Expiration {
$j('#exp_timestampdiv').slideDown("normal");
$j(this).hide();
}
return false;
return FALSE;
});
$j('.cancel-exp-timestamp', '#exp_timestampdiv').click(function () {
$j('#exp_timestampdiv').slideUp("normal");
......@@ -300,14 +396,14 @@ class RRZE_Post_Expiration {
$j('#exp_mn').val($j('#exp_hidden_mn').val());
$j('#exp_timestampdiv').siblings('a.edit-exp-timestamp').show();
updateExpiration();
return false;
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;
return FALSE;
});
function updateExpiration() {
......@@ -321,7 +417,7 @@ class RRZE_Post_Expiration {
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;
return FALSE;
} else {
$j('.exp-timestamp-wrap', '#exp_timestampdiv').removeClass('form-invalid');
}
......@@ -350,7 +446,7 @@ class RRZE_Post_Expiration {
$j('.exp_timestamp').html('<?php _e('Verfallsdatum <b>deaktiviert</b>', 'rrze-post-expiration'); ?>');
}
return true;
return TRUE;
}
});
......@@ -362,7 +458,7 @@ class RRZE_Post_Expiration {
public static function update_post_expiration_date() {
global $current_blog, $post;
if (!$post || $post->post_type != 'post') {
if (!$post || !in_array($post->post_type, self::$options->post_types)) {
return;
}
......@@ -386,14 +482,13 @@ class RRZE_Post_Expiration {
$ts = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss);
update_post_meta($post->ID, 'expiration_date', $ts, true);
update_post_meta($post->ID, 'expiration_date', $ts, TRUE);
$ts_gmt = get_gmt_from_date($ts);
update_post_meta($post->ID, 'expiration_date_gmt', $ts_gmt, true);
update_post_meta($post->ID, 'expiration_date_gmt', $ts_gmt, TRUE);
$cron_hook = sprintf('%s_%s', self::option_name, $current_blog->blog_id);
wp_clear_scheduled_hook($cron_hook);
wp_schedule_event(time(), 'two-minutes', $cron_hook);
wp_clear_scheduled_hook(self::$cron_hook);
wp_schedule_event(time(), 'two-minutes', self::$cron_hook);
}
}
......@@ -409,14 +504,21 @@ class RRZE_Post_Expiration {
global $wpdb;
$cur_date = current_time('mysql');
$result = $wpdb->get_results(
$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 posts.post_type = 'post'
AND postmeta.meta_key = 'expiration_date'
AND postmeta.meta_value <= '" . $cur_date . "'");
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) {
......
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