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

Merge branch 'dev' into 'master'

Dev

See merge request rrze-webteam/rrze-post-expiration!3
parents 9f84c180 239e7e8b
<?php
namespace RRZE\PostExpiration;
defined('ABSPATH') || exit;
spl_autoload_register(function ($class) {
$prefix = 'RRZE\PostExpiration\\';
$base_dir = __DIR__ . '/RRZE/PostExpiration/';
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}
$relative_class = substr($class, $len);
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
if (file_exists($file)) {
require $file;
}
});
......@@ -10,8 +10,6 @@ class Expiration
protected $options;
protected $cron_hook;
protected $expiration_post_status = 'expired';
protected $expiration_date_key = 'expiration_date';
......@@ -20,9 +18,15 @@ class Expiration
public function __construct()
{
$opt = new Options();
$this->option_name = $opt->get_option_name();
$this->options = $opt->get_options();
$this->option_name = Options::getOptionName();
$this->options = Options::getOptions();
}
public function onLoaded()
{
add_action('wp', [$this, 'activateScheduledEvents']);
add_filter('cron_schedules', [$this, 'customCronSchedules']);
add_action('rrze_post_expiration_every2minutes_event', [$this, 'every2MinutesEvent']);
if (empty($this->options->post_types)) {
return;
......@@ -43,11 +47,6 @@ class Expiration
add_action('post_submitbox_misc_actions', array($this, 'exp_meta_box'));
add_action('save_post', array($this, 'update_post_expiration_date'));
add_filter('cron_schedules', array($this, 'add_cron_schedule'));
$this->cron_hook = sprintf('%s_%s', $this->option_name, get_current_blog_id());
add_action($this->cron_hook, array($this, 'set_expired_posts'));
}
public function expiration_date_column($columns)
......@@ -294,7 +293,7 @@ class Expiration
public function update_post_expiration_date()
{
global $current_blog, $post;
global $post;
if (!$post || !in_array($post->post_type, $this->options->post_types)) {
return;
......@@ -325,21 +324,43 @@ class Expiration
$ts_gmt = get_gmt_from_date($ts);
update_post_meta($post->ID, $this->expiration_date_gmt_key, $ts_gmt, true);
wp_clear_scheduled_hook($this->cron_hook);
wp_schedule_event(time(), 'two-minutes', $this->cron_hook);
$this->clearSchedule();
}
}
public function add_cron_schedule($schedules)
/**
* customCronSchedules
* Add custom cron schedules.
* @param array $schedules Available cron schedules
* @return array New cron schedules
*/
public function customCronSchedules(array $schedules): array
{
$schedules['two-minutes'] = array(
'interval' => 120,
'display' => __('Every Two Minutes')
);
$schedules['rrze_post_expiration_every2minutes'] = [
'interval' => 2 * MINUTE_IN_SECONDS,
'display' => __('Every two minutes', 'rrze-post-expiration')
];
return $schedules;
}
public function set_expired_posts()
/**
* activateScheduledEvents
* Activate all scheduled events.
* @return void
*/
public function activateScheduledEvents()
{
if (!wp_next_scheduled('rrze_post_expiration_every2minutes_event')) {
wp_schedule_event(time(), 'rrze_post_expiration_every2minutes', 'rrze_post_expiration_every2minutes_event');
}
}
/**
* every2MinutesEvent
* Run the event every 2 minutes.
* @return void
*/
public function every2MinutesEvent()
{
global $wpdb;
......@@ -364,14 +385,17 @@ class Expiration
);
if (!empty($result)) {
$plugins = new Plugins();
foreach ($result as $row) {
wp_update_post(['ID' => $row->post_id, 'post_status' => $this->expiration_post_status]);
// rrze cache plugin support
$plugins->flush_rrze_cache(get_permalink($row->post_id));
Functions::flush_cache_url(get_permalink($row->post_id));
}
}
}
protected function clearSchedule()
{
wp_clear_scheduled_hook('rrze_post_expiration_every2minutes_event');
}
}
......@@ -4,22 +4,17 @@ namespace RRZE\PostExpiration;
defined('ABSPATH') || exit;
class Plugins
class Functions
{
/**
* Flush the RRZE Cache
* Flush the Cache for an Url.
* RRZE Cache plugin support.
* @param string $url Post permalink
* @return void
*/
public function flush_rrze_cache($url)
public static function flush_cache_url(string $url)
{
if (empty($url)) {
return;
}
if (has_action('rrzecache_flush_cache_url')) {
if (!empty($url) && has_action('rrzecache_flush_cache_url')) {
do_action('rrzecache_flush_cache_url', $url);
}
}
......
......@@ -6,33 +6,45 @@ defined('ABSPATH') || exit;
class Main
{
protected $plugin_basename;
public function __construct()
{
//
}
public function __construct($plugin_basename)
public function onLoaded()
{
$this->plugin_basename = $plugin_basename;
$settings = new Settings;
$settings->onLoaded();
$expiration = new Expiration;
$expiration->onLoaded();
$update = new Update();
add_action('init', [$update, 'update_version']);
add_action('admin_enqueue_scripts', [$this, 'admin_enqueue_scripts']);
$settings = new Settings();
$expiration = new Expiration();
}
public function admin_enqueue_scripts($hook)
{
wp_register_style('rrze-post-expiration', plugins_url('css/post-expiration.min.css', $this->plugin_basename));
if ($hook == 'post.php') {
wp_register_script('rrze-post-expiration', plugins_url('js/post-hook-suffix.min.js', $this->plugin_basename), ['jquery']);
wp_enqueue_style(
'rrze-post-expiration',
plugins_url('assets/css/post-expiration.min.css', plugin()->getBasename()),
[],
plugin()->getVersion()
);
wp_enqueue_script(
'rrze-post-expiration',
plugins_url('assets/js/post-hook-suffix.min.js', plugin()->getBasename()),
['jquery'],
plugin()->getVersion()
);
wp_localize_script('rrze-post-expiration', 'expiration', [
'expiresOn' => __('Expires on', 'rrze-post-expiration'),
'expiredOn' => __('Expired on:', 'rrze-post-expiration'),
'expirationDate' => __('Expiration <b>disabled</b>', 'rrze-post-expiration')
]);
wp_enqueue_script('rrze-post-expiration');
}
}
}
......@@ -6,9 +6,17 @@ defined('ABSPATH') || exit;
class Options
{
protected $option_name = 'rrze_post_expiration';
protected function default_options()
/**
* Option name
* @var string
*/
protected static $optionName = 'rrze_post_expiration';
/**
* Default options
* @return array
*/
protected static function defaultOptions(): array
{
$options = [
'post_types' => ['post']
......@@ -17,19 +25,28 @@ class Options
return $options;
}
public function get_options()
/**
* Returns the options.
* @return object
*/
public static function getOptions(): object
{
$defaults = $this->default_options();
$defaults = self::defaultOptions();
$options = (array) get_option($this->option_name);
$options = (array) get_option(self::$optionName);
$options = wp_parse_args($options, $defaults);
$options = array_intersect_key($options, $defaults);
return (object) $options;
}
public function get_option_name()
/**
* Returns the name of the option.
* @return string
*/
public static function getOptionName(): string
{
return $this->option_name;
return self::$optionName;
}
}
<?php
namespace RRZE\PostExpiration;
defined('ABSPATH') || exit;
class Plugin
{
/**
* The full path and filename of the plugin.
* @var string
*/
protected $pluginFile;
/**
* The basename of the plugin.
* @var string
*/
protected $basename;
/**
* The filesystem directory path (with trailing slash) for the plugin.
* @var string
*/
protected $directory;
/**
* The URL directory path (with trailing slash) for the plugin.
* @var string
*/
protected $url;
/**
* The version of the plugin.
* @var string
*/
protected $version;
/**
* __construct method
* @param string $pluginFile The full path and filename of the plugin.
*/
public function __construct(string $pluginFile)
{
$this->pluginFile = $pluginFile;
}
/**
* onLoaded method
*/
public function onLoaded()
{
$this->setBasename()
->setDirectory()
->setUrl()
->setVersion();
}
/**
* getFile method
* Get the full path and filename of the plugin.
* @return string The full path and filename.
*/
public function getFile(): string
{
return $this->pluginFile;
}
/**
* getBasename method
* Get the basename of the plugin.
* @return string The basename.
*/
public function getBasename(): string
{
return $this->basename;
}
/**
* setBasename method
* Set the basename of the plugin.
* @return object This Plugin object.
*/
public function setBasename(): object
{
$this->basename = plugin_basename($this->pluginFile);
return $this;
}
/**
* getDirectory method
* Get the filesystem directory path (with trailing slash) for the plugin.
* @return string The filesystem directory path.
*/
public function getDirectory(): string
{
return $this->directory;
}
/**
* setDirectory method
* Set the filesystem directory path (with trailing slash) for the plugin.
* @return object This Plugin object.
*/
public function setDirectory(): object
{
$this->directory = rtrim(plugin_dir_path($this->pluginFile), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
return $this;
}
/**
* getPath method
* Get the filesystem directory path (with trailing slash) for the plugin.
* @param string $path The path name.
* @return string The filesystem directory path.
*/
public function getPath(string $path = ''): string
{
return $this->directory . ltrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
}
/**
* getUrl method
* Get the URL directory path (with trailing slash) for the plugin.
* @param string $path The path name.
* @return string The URL directory path.
*/
public function getUrl(string $path = ''): string
{
return $this->url . ltrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
}
/**
* setUrl method
* Set the URL directory path (with trailing slash) for the plugin.
* @return object This Plugin object.
*/
public function setUrl(): object
{
$this->url = rtrim(plugin_dir_url($this->pluginFile), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
return $this;
}
/**
* getSlug method
* Get the slug of the plugin.
* @return string The slug.
*/
public function getSlug(): string
{
return sanitize_title(dirname($this->basename));
}
/**
* getVersion method
* Get the version of the plugin.
* @return string The version.
*/
public function getVersion(): string
{
if (defined('WP_DEBUG') && WP_DEBUG) {
return bin2hex(random_bytes(4));
}
return $this->version;
}
/**
* getVersion method
* Set the version of the plugin.
* @return object This Plugin object.
*/
public function setVersion(): object
{
$headers = ['Version' => 'Version'];
$fileData = get_file_data($this->pluginFile, $headers, 'plugin');
if (isset($fileData['Version'])) {
$this->version = $fileData['Version'];
};
return $this;
}
/**
* __call method
* Method overloading.
*/
public function __call(string $name, array $arguments)
{
if (!method_exists($this, $name)) {
$message = sprintf(__('Call to undefined method %1$s::%2$s', 'rrze-rsvp'), __CLASS__, $name);
do_action(
'rrze.log.error',
$message,
[
'class' => __CLASS__,
'method' => $name,
'arguments' => $arguments
]
);
if (defined('WP_DEBUG') && WP_DEBUG) {
throw new \Exception($message);
}
}
}
}
......@@ -21,13 +21,15 @@ class Settings
public function __construct()
{
$opt = new Options();
$this->option_name = $opt->get_option_name();
$this->options = $opt->get_options();
$this->option_name = Options::getOptionName();
$this->options = Options::getOptions();;
}
public function onLoaded()
{
add_action('admin_menu', [$this, 'admin_settings_page']);
add_action('admin_init', [$this, 'admin_settings']);
}
}
public function admin_settings_page()
{
......
......@@ -36,12 +36,10 @@ class Update
);
if (!empty($result)) {
$plugins = new Plugins();
foreach ($result as $row) {
wp_update_post(['ID' => $row->post_id, 'post_status' => 'expired']);
// rrze cache plugin support
$plugins->flush_rrze_cache(get_permalink($row->post_id));
Functions::flush_cache_url(get_permalink($row->post_id));
}
}
}
......
<?php
/**
* 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.1
* 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
*/
/*
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.5.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
*/
namespace RRZE\PostExpiration;
defined('ABSPATH') || exit;
const RRZE_PHP_VERSION = '7.1';
const RRZE_WP_VERSION = '5.0';
const RRZE_PHP_VERSION = '7.4';
const RRZE_WP_VERSION = '5.5';
require 'autoload.php';
// Autoloader (PSR-4)
spl_autoload_register(function ($class) {
$prefix = __NAMESPACE__;
$base_dir = __DIR__ . '/includes/';
register_activation_hook(__FILE__, 'RRZE\PostExpiration\activation');
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}
add_action('plugins_loaded', 'RRZE\PostExpiration\loaded');
$relativeClass = substr($class, $len);
$file = $base_dir . str_replace('\\', '/', $relativeClass) . '.php';
function load_textdomain() {
load_plugin_textdomain('rrze-post-expiration', false, sprintf('%s/languages/', dirname(plugin_basename(__FILE__))));
}
if (file_exists($file)) {
require $file;
}
});
register_activation_hook(__FILE__, __NAMESPACE__ . '\activation');
register_deactivation_hook(__FILE__, __NAMESPACE__ . '\deactivation');
function activation() {
load_textdomain();
add_action('plugins_loaded', __NAMESPACE__ . '\loaded');
system_requirements();
/**
* [loadTextdomain description]
*/
function loadTextdomain()
{
load_plugin_textdomain('rrze-post-expiration', false, sprintf('%s/languages/', dirname(plugin_basename(__FILE__))));
}
function system_requirements() {
$error = '';
/**
* [systemRequirements description]
* @return string [description]
*/
function systemRequirements(): string
{
loadTextdomain();
$error = '';
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);
$error = sprintf(__('The server is running PHP version %s. The Plugin requires at least PHP version %s.', 'rrze-post-expiration'), PHP_VERSION, RRZE_PHP_VERSION);
} elseif (version_compare($GLOBALS['wp_version'], RRZE_WP_VERSION, '<')) {
$error = sprintf(__('The server is running WordPress version %s. The Plugin requires at least WordPress version %s.', 'rrze-post-expiration'), $GLOBALS['wp_version'], RRZE_WP_VERSION);
}
return $error;
}
if (version_compare($GLOBALS['wp_version'], RRZE_WP_VERSION, '<')) {