Commit 4d1de030 authored by Rolf Forst's avatar Rolf Forst
Browse files

Add Plugin class

parent 00cb98c6
...@@ -6,12 +6,6 @@ defined('ABSPATH') || exit; ...@@ -6,12 +6,6 @@ defined('ABSPATH') || exit;
class Main class Main
{ {
/**
* [protected description]
* @var string
*/
protected $pluginFile;
/** /**
* [protected description] * [protected description]
* @var object * @var object
...@@ -24,16 +18,18 @@ class Main ...@@ -24,16 +18,18 @@ class Main
*/ */
protected $siteOptions; protected $siteOptions;
public function __construct($pluginFile) /**
* [__construct description]
*/
public function __construct()
{ {
$this->pluginFile = $pluginFile;
$this->options = Options::getOptions(); $this->options = Options::getOptions();
$this->siteOptions = Options::getSiteOptions(); $this->siteOptions = Options::getSiteOptions();
} }
public function onLoaded() public function onLoaded()
{ {
$settings = new Settings($this->pluginFile); $settings = new Settings();
$settings->onLoaded(); $settings->onLoaded();
add_action('init', [$this, 'add_publish_hooks'], 99); add_action('init', [$this, 'add_publish_hooks'], 99);
...@@ -74,32 +70,30 @@ class Main ...@@ -74,32 +70,30 @@ class Main
public function admin_enqueue_scripts($hook) public function admin_enqueue_scripts($hook)
{ {
if ($hook == 'settings_page_rrze-cache') { if ($hook == 'settings_page_rrze-cache') {
$plugin_data = get_plugin_data($this->pluginFile);
wp_enqueue_style( wp_enqueue_style(
'jquery-listfilterizer', 'jquery-listfilterizer',
plugins_url('assets/css/jquery.listfilterizer.min.css', $this->pluginFile), plugins_url('assets/css/jquery.listfilterizer.min.css', plugin()->getBasename()),
[], [],
'1.0' '1.0'
); );
wp_enqueue_script( wp_enqueue_script(
'jquery-listfilterizer', 'jquery-listfilterizer',
plugins_url('assets/js/jquery.listfilterizer.min.js', $this->pluginFile), plugins_url('assets/js/jquery.listfilterizer.min.js', plugin()->getBasename()),
['jquery'], ['jquery'],
'1.0', '1.0',
true true
); );
wp_enqueue_style( wp_enqueue_style(
'rrze-cache-settings', 'rrze-cache-settings',
plugins_url('assets/css/settings.min.css', $this->pluginFile), plugins_url('assets/css/settings.min.css', plugin()->getBasename()),
[], [],
$plugin_data['Version'] plugin()->getVersion()
); );
wp_enqueue_script( wp_enqueue_script(
'rrze-cache-settings', 'rrze-cache-settings',
plugins_url('assets/js/settings.min.js', $this->pluginFile), plugins_url('assets/js/settings.min.js', plugin()->getBasename()),
['jquery', 'jquery-listfilterizer'], ['jquery', 'jquery-listfilterizer'],
$plugin_data['Version'], plugin()->getVersion(),
true true
); );
wp_localize_script( wp_localize_script(
...@@ -112,19 +106,17 @@ class Main ...@@ -112,19 +106,17 @@ class Main
] ]
); );
} elseif ($hook === 'post.php') { } elseif ($hook === 'post.php') {
$plugin_data = get_plugin_data($this->pluginFile);
wp_enqueue_style( wp_enqueue_style(
'rrze-cache-post-edit', 'rrze-cache-post-edit',
plugins_url('assets/css/post-edit.min.css', $this->pluginFile), plugins_url('assets/css/post-edit.min.css', plugin()->getBasename()),
[], [],
$plugin_data['Version'] plugin()->getVersion()
); );
wp_enqueue_script( wp_enqueue_script(
'rrze-cache-post-edit', 'rrze-cache-post-edit',
plugins_url('assets/js/post-edit.min.js', $this->pluginFile), plugins_url('assets/js/post-edit.min.js', plugin()->getBasename()),
['jquery'], ['jquery'],
$plugin_data['Version'], plugin()->getVersion(),
true true
); );
} }
......
<?php
namespace RRZE\Cache;
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);
}
}
}
}
...@@ -6,12 +6,6 @@ defined('ABSPATH') || exit; ...@@ -6,12 +6,6 @@ defined('ABSPATH') || exit;
class Settings class Settings
{ {
/**
* [protected description]
* @var string
*/
protected $pluginFile;
/** /**
* [protected description] * [protected description]
* @var object * @var object
...@@ -32,11 +26,9 @@ class Settings ...@@ -32,11 +26,9 @@ class Settings
/** /**
* [__construct description] * [__construct description]
* @param string $pluginFile [description]
*/ */
public function __construct($pluginFile) public function __construct()
{ {
$this->pluginFile = $pluginFile;
$this->options = Options::getOptions(); $this->options = Options::getOptions();
$this->siteOptionName = Options::getSiteOptionName(); $this->siteOptionName = Options::getSiteOptionName();
$this->siteOptions = Options::getSiteOptions(); $this->siteOptions = Options::getSiteOptions();
...@@ -55,9 +47,9 @@ class Settings ...@@ -55,9 +47,9 @@ class Settings
if (is_multisite()) { if (is_multisite()) {
add_action('network_admin_menu', [$this, 'networkAdminMenu']); add_action('network_admin_menu', [$this, 'networkAdminMenu']);
add_action('admin_init', [$this, 'networkAdminSettings']); add_action('admin_init', [$this, 'networkAdminSettings']);
add_filter('network_admin_plugin_action_links_' . plugin_basename($this->pluginFile), [$this, 'networkAdminPluginActionLinks']); add_filter('network_admin_plugin_action_links_' . plugin()->getBasename(), [$this, 'networkAdminPluginActionLinks']);
} else { } else {
add_filter('plugin_action_links_' . plugin_basename($this->pluginFile), [$this, 'pluginActionLinks']); add_filter('plugin_action_links_' . plugin()->getBasename(), [$this, 'pluginActionLinks']);
} }
add_action('admin_bar_menu', [$this, 'adminBarFlushIcon'], 90); add_action('admin_bar_menu', [$this, 'adminBarFlushIcon'], 90);
...@@ -188,8 +180,7 @@ class Settings ...@@ -188,8 +180,7 @@ class Settings
*/ */
public function optionsPage() public function optionsPage()
{ {
?> ?><style>
<style>
#rrzecache_settings input[type="text"], #rrzecache_settings input[type="text"],
#rrzecache_settings input[type="number"] { #rrzecache_settings input[type="number"] {
height: 30px; height: 30px;
...@@ -257,7 +248,7 @@ class Settings ...@@ -257,7 +248,7 @@ class Settings
protected function enableCache() protected function enableCache()
{ {
if (!file_exists($wpCacheFile = WP_CONTENT_DIR . '/advanced-cache.php')) { if (!file_exists($wpCacheFile = WP_CONTENT_DIR . '/advanced-cache.php')) {
$rrzeCacheFile = dirname($this->pluginFile) . '/advanced-cache.php'; $rrzeCacheFile = plugin()->getPath() . 'advanced-cache.php';
if (function_exists('symlink')) { if (function_exists('symlink')) {
return symlink($rrzeCacheFile, $wpCacheFile); return symlink($rrzeCacheFile, $wpCacheFile);
} }
......
...@@ -52,22 +52,24 @@ add_action('plugins_loaded', __NAMESPACE__ . '\loaded'); ...@@ -52,22 +52,24 @@ add_action('plugins_loaded', __NAMESPACE__ . '\loaded');
*/ */
function loadTextdomain() function loadTextdomain()
{ {
load_plugin_textdomain('rrze-cache', false, sprintf('%s/languages/', dirname(plugin_basename(__FILE__)))); load_plugin_textdomain('rrze-multilang', false, sprintf('%s/languages/', dirname(plugin_basename(__FILE__))));
} }
/** /**
* [systemRequirements description] * [systemRequirements description]
* @return string [description] * @return string [description]
*/ */
function systemRequirements() function systemRequirements(): string
{ {
loadTextdomain();
$error = ''; $error = '';
if (version_compare(PHP_VERSION, RRZE_PHP_VERSION, '<')) { if (version_compare(PHP_VERSION, RRZE_PHP_VERSION, '<')) {
$error = sprintf(__('The server is running PHP version %1$s. The Plugin requires at least PHP version %2$s.', 'rrze-cache'), PHP_VERSION, RRZE_PHP_VERSION); $error = sprintf(__('The server is running PHP version %s. The Plugin requires at least PHP version %s.', 'rrze-multilang'), PHP_VERSION, RRZE_PHP_VERSION);
} elseif (version_compare($GLOBALS['wp_version'], RRZE_WP_VERSION, '<')) { } elseif (version_compare($GLOBALS['wp_version'], RRZE_WP_VERSION, '<')) {
$error = sprintf(__('The server is running WordPress version %1$s. The Plugin requires at least WordPress version %2$s.', 'rrze-cache'), $GLOBALS['wp_version'], RRZE_WP_VERSION); $error = sprintf(__('The server is running WordPress version %s. The Plugin requires at least WordPress version %s.', 'rrze-multilang'), $GLOBALS['wp_version'], RRZE_WP_VERSION);
} elseif (! defined('WP_CACHE') || ! WP_CACHE) { } elseif (!is_multisite()) {
$error = __('The constant WP_CACHE is not defined or is set to false.', 'rrze-cache'); $error = __('The plugin is compatible only with WordPress Multisite.', 'rrze-multilang');
} }
return $error; return $error;
} }
...@@ -77,11 +79,9 @@ function systemRequirements() ...@@ -77,11 +79,9 @@ function systemRequirements()
*/ */
function activation() function activation()
{ {
loadTextdomain();
if ($error = systemRequirements()) { if ($error = systemRequirements()) {
deactivate_plugins(plugin_basename(__FILE__)); deactivate_plugins(plugin_basename(__FILE__));
wp_die(sprintf(__('Plugins: %1$s: %2$s', 'rrze-cache'), plugin_basename(__FILE__), $error)); wp_die(sprintf(__('Plugins: %s: %s', 'rrze-log'), plugin_basename(__FILE__), $error));
} }
} }
...@@ -94,31 +94,46 @@ function deactivation() ...@@ -94,31 +94,46 @@ function deactivation()
delete_site_option(Options::getSiteOptionName()); delete_site_option(Options::getSiteOptionName());
} }
/**
* [plugin description]
* @return object
*/
function plugin(): object
{
static $instance;
if (null === $instance) {
$instance = new Plugin(__FILE__);
}
return $instance;
}
/** /**
* [loaded description] * [loaded description]
* @return void * @return void
*/ */
function loaded() function loaded()
{ {
loadTextDomain(); add_action('init', __NAMESPACE__ . '\loadTextdomain');
plugin()->onLoaded();
if ($error = systemRequirements()) {
deactivation(); if ($error = systemRequirements()) {
add_action('admin_init', function () use ($error) { add_action('admin_init', function () use ($error) {
$pluginData = get_plugin_data(__FILE__); if (current_user_can('activate_plugins')) {
$pluginName = $pluginData['Name']; $pluginData = get_plugin_data(plugin()->getFile());
$tag = is_plugin_active_for_network(plugin_basename(__FILE__)) ? 'network_admin_notices' : 'admin_notices'; $pluginName = $pluginData['Name'];
add_action($tag, function () use ($pluginName, $error) { $tag = is_plugin_active_for_network(plugin()->getBaseName()) ? 'network_admin_notices' : 'admin_notices';
printf( add_action($tag, function () use ($pluginName, $error) {
'<div class="notice notice-error"><p>' . __('Plugins: %1$s: %2$s', 'rrze-cache') . '</p></div>', printf(
esc_html($pluginName), '<div class="notice notice-error"><p>' . __('Plugins: %s: %s', 'rrze-multilang') . '</p></div>',
esc_html($error) esc_html($pluginName),
); esc_html($error)
}); );
}); });
return; }
} });
return;
$main = new Main(__FILE__); }
$main->onLoaded();
} $main = new Main;
$main->onLoaded();
}
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