Fix refresh procedure #2
69
inc/class-refresh.php
Normal file
69
inc/class-refresh.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
/**
|
||||
* @package ogre-sort
|
||||
* @author cleverogre
|
||||
* @copyright 2025 CleverOgre
|
||||
* @license GLP-3.0-or-later
|
||||
* @version 1.1.0
|
||||
* @since 1.1.0
|
||||
*/
|
||||
|
||||
namespace Ogre\Sort;
|
||||
|
||||
use Ogre\Singleton;
|
||||
use Ogre\Sort as Plugin;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
final class Refresh {
|
||||
use Singleton;
|
||||
|
||||
const CAPABILITY = 'manage_options';
|
||||
const ACTION = 'ogre-sort-refresh';
|
||||
|
||||
protected function __construct() {
|
||||
add_filter('plugin_action_links_' . Plugin::get_basename(), [$this, 'action_links'], 5);
|
||||
add_action('admin_post_' . self::ACTION, [$this, 'handle_action']);
|
||||
add_action('admin_notices', [$this, 'handle_notices']);
|
||||
}
|
||||
|
||||
public function action_links($links) {
|
||||
if (current_user_can(self::CAPABILITY)) {
|
||||
array_unshift($links, sprintf(
|
||||
'<a href="%s">%s</a>',
|
||||
esc_url(add_query_arg([
|
||||
'_wpnonce' => wp_create_nonce(self::ACTION),
|
||||
], self::get_url())),
|
||||
esc_html(Plugin::__('Refresh'))
|
||||
));
|
||||
}
|
||||
return $links;
|
||||
}
|
||||
|
||||
public function handle_action():void {
|
||||
if (!current_user_can(self::CAPABILITY)) wp_die(Plugin::__('Invalid request.'));
|
||||
check_admin_referer(self::ACTION);
|
||||
|
||||
Sort::instance()->refresh_all();
|
||||
|
||||
wp_redirect(add_query_arg([
|
||||
self::ACTION => '1',
|
||||
], Settings::get_url()));
|
||||
exit;
|
||||
}
|
||||
|
||||
public function handle_notices():void {
|
||||
if (!current_user_can(self::CAPABILITY) || $_GET['page'] != Plugin::get_id() || !isset($_GET[self::ACTION])) return;
|
||||
printf(
|
||||
'<div class="notice notice-success is-dismissable"><p>%s</p></div>',
|
||||
Plugin::esc_html__('All sort relationships successfully refreshed!')
|
||||
);
|
||||
}
|
||||
|
||||
public static function get_url():string {
|
||||
return admin_url('admin-post.php?action=' . self::ACTION);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Refresh::instance();
|
||||
@@ -41,6 +41,7 @@ final class Sort extends Plugin {
|
||||
$this->add_files([
|
||||
'inc/class-settings.php',
|
||||
'inc/class-sort.php',
|
||||
'inc/class-refresh.php',
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user