PluginProbe
Block User Account / trunk
Block User Account vtrunk
2.0.0 trunk 1.3.1 1.4
block-user-account / block-user-account.php

block-user-account.php in Block User Account trunk, at block-user-account.php

372 lines 10.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Plugin Name: Block User Account
5 * Plugin URI: https://dangoweb.ir/product/buacc-wordpress-user-block-plugin/
6 * Description: Advanced user account management - Block users temporarily or permanently with custom messages, email notifications, activity logs and bulk actions
7 * Version: 2.0.1
8 * Author: DangoWeb
9 * Author URI: https://dangoweb.ir
10 * Text Domain: block-user-account
11 * Domain Path: /languages
12 * Requires at least: 5.0
13 * Requires PHP: 7.2
14 * License: GPL v2 or later
15 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
16 */
17
18 defined('ABSPATH') || exit;
19
20 /**
21 * Define plugin constants
22 */
23 define('BUA_VERSION', '2.0.1');
24 define('BUA_PLUGIN_FILE', __FILE__);
25 define('BUA_PLUGIN_DIR', plugin_dir_path(__FILE__));
26 define('BUA_PLUGIN_URL', plugin_dir_url(__FILE__));
27 define('BUA_ASSETS_URL', BUA_PLUGIN_URL . 'assets/');
28 define('BUA_CSS_URL', BUA_ASSETS_URL . 'css');
29 define('BUA_JS_URL', BUA_ASSETS_URL . 'js');
30 define('BUA_LANG_DIR', dirname(plugin_basename(__FILE__)) . '/languages/');
31
32 /**
33 * Load required files
34 */
35 require_once BUA_PLUGIN_DIR . 'includes/class-logger.php';
36 require_once BUA_PLUGIN_DIR . 'includes/class-email-notifications.php';
37 require_once BUA_PLUGIN_DIR . 'includes/class-cron-jobs.php';
38 require_once BUA_PLUGIN_DIR . 'includes/class-authentication.php';
39 require_once BUA_PLUGIN_DIR . 'includes/class-block-manager.php';
40 require_once BUA_PLUGIN_DIR . 'admin/class-user-profile.php';
41 require_once BUA_PLUGIN_DIR . 'admin/class-user-list.php';
42 require_once BUA_PLUGIN_DIR . 'admin/class-bulk-actions.php';
43 require_once BUA_PLUGIN_DIR . 'admin/class-admin-menu.php';
44
45 /**
46 * Main plugin class
47 */
48 final class Block_User_Account
49 {
50
51 /**
52 * Single instance
53 *
54 * @var Block_User_Account
55 */
56 private static $instance = null;
57
58 /**
59 * Plugin components
60 *
61 * @var array
62 */
63 private $components = array();
64
65 /**
66 * Get instance
67 *
68 * @return Block_User_Account
69 */
70 public static function instance()
71 {
72 if (is_null(self::$instance)) {
73 self::$instance = new self();
74 }
75 return self::$instance;
76 }
77
78 /**
79 * Constructor
80 */
81 private function __construct()
82 {
83 $this->define_hooks();
84 }
85
86 /**
87 * Define hooks
88 */
89 private function define_hooks()
90 {
91 add_action('init', array($this, 'load_textdomain'));
92 add_action('plugins_loaded', array($this, 'init_components'));
93
94 register_activation_hook(__FILE__, array($this, 'activate'));
95 register_deactivation_hook(__FILE__, array($this, 'deactivate'));
96 register_uninstall_hook(__FILE__, array('Block_User_Account', 'uninstall'));
97
98 add_action('wp_dashboard_setup', array($this, 'add_dashboard_widgets'));
99 add_action('admin_bar_menu', array($this, 'add_admin_bar_menu'), 100);
100
101 add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'add_plugin_action_links'));
102 add_filter('plugin_row_meta', array($this, 'add_plugin_row_meta'), 10, 2);
103 }
104
105 /**
106 * Load plugin textdomain
107 */
108 public function load_textdomain()
109 {
110 $locale = determine_locale();
111 $locale = apply_filters('plugin_locale', $locale, 'block-user-account');
112
113 unload_textdomain('block-user-account');
114
115 $loaded = load_textdomain(
116 'block-user-account',
117 BUA_PLUGIN_DIR . 'languages/block-user-account-' . $locale . '.mo'
118 );
119 }
120
121 /**
122 * Initialize plugin components
123 */
124 public function init_components()
125 {
126 $this->components['logger'] = new BUA_Logger();
127 $this->components['email_notifications'] = new BUA_Email_Notifications();
128 $this->components['cron_jobs'] = new BUA_Cron_Jobs();
129 $this->components['authentication'] = new BUA_Authentication();
130 $this->components['block_manager'] = new BUA_Block_Manager();
131 $this->components['user_profile'] = new BUA_User_Profile();
132 $this->components['user_list'] = new BUA_User_List();
133 $this->components['bulk_actions'] = new BUA_Bulk_Actions();
134
135 if (is_admin()) {
136 $this->components['admin_menu'] = new BUA_Admin_Menu();
137 }
138 }
139
140 /**
141 * Add dashboard widgets
142 */
143 public function add_dashboard_widgets()
144 {
145 BUA_User_Profile::add_dashboard_widget();
146 }
147
148 /**
149 * Add admin bar menu
150 *
151 * @param WP_Admin_Bar $wp_admin_bar Admin bar object
152 */
153 public function add_admin_bar_menu($wp_admin_bar)
154 {
155 if (!current_user_can('edit_users')) {
156 return;
157 }
158
159 $blocked_count = BUA_Logger::get_blocked_users_count();
160
161 $wp_admin_bar->add_node(array(
162 'id' => 'bua-admin-bar',
163 'title' => sprintf(
164 '<span class="ab-icon dashicons dashicons-shield" style="top:2px;"></span>' .
165 '<span class="ab-label">%s</span>',
166 sprintf(__('Blocked (%d)', 'block-user-account'), $blocked_count)
167 ),
168 'href' => admin_url('users.php?bua_filter=blocked'),
169 'parent' => null
170 ));
171
172 if ($blocked_count > 0) {
173 $wp_admin_bar->add_node(array(
174 'id' => 'bua-view-blocked',
175 'title' => __('View Blocked Users', 'block-user-account'),
176 'href' => admin_url('users.php?bua_filter=blocked'),
177 'parent' => 'bua-admin-bar'
178 ));
179 }
180
181 $wp_admin_bar->add_node(array(
182 'id' => 'bua-settings',
183 'title' => __('Block Settings', 'block-user-account'),
184 'href' => admin_url('options-general.php?page=block-user-account-settings'),
185 'parent' => 'bua-admin-bar'
186 ));
187 }
188
189 /**
190 * Add plugin action links
191 *
192 * @param array $links Existing links
193 * @return array Modified links
194 */
195 public function add_plugin_action_links($links)
196 {
197 $settings_link = sprintf(
198 '<a href="%s">%s</a>',
199 admin_url('options-general.php?page=block-user-account-settings'),
200 __('Settings', 'block-user-account')
201 );
202 array_unshift($links, $settings_link);
203 return $links;
204 }
205
206 /**
207 * Add plugin row meta
208 *
209 * @param array $links Existing links
210 * @param string $file Plugin file
211 * @return array Modified links
212 */
213 public function add_plugin_row_meta($links, $file)
214 {
215 if (plugin_basename(__FILE__) !== $file) {
216 return $links;
217 }
218
219 $links[] = sprintf(
220 '<a href="%s" target="_blank">%s</a>',
221 'https://dangoweb.ir/product/buacc-wordpress-user-block-plugin/',
222 __('Documentation', 'block-user-account')
223 );
224
225 $links[] = sprintf(
226 '<a href="%s" target="_blank">%s</a>',
227 'https://wordpress.org/support/plugin/block-user-account/reviews/#new-post',
228 __('Rate Us', 'block-user-account')
229 );
230
231 return $links;
232 }
233
234 /**
235 * Plugin activation
236 */
237 public function activate()
238 {
239 BUA_Logger::create_table();
240
241 add_option('bua_default_message', __('Your account has been temporarily disabled. Please contact the administrator.', 'block-user-account'));
242 add_option('bua_email_notifications', 'yes');
243 add_option('bua_admin_notifications', 'yes');
244 add_option('bua_log_activities', 'yes');
245 add_option('bua_daily_report', 'no');
246 add_option('bua_weekly_summary', 'no');
247 add_option('bua_log_retention_days', 90);
248 add_option('bua_auto_unblock', 'yes');
249 add_option('bua_db_version', BUA_VERSION);
250
251 set_transient('bua_activation_notice', true, 30);
252
253 flush_rewrite_rules();
254 }
255
256 /**
257 * Plugin deactivation
258 */
259 public function deactivate()
260 {
261 BUA_Cron_Jobs::clear_scheduled_events();
262 flush_rewrite_rules();
263 }
264
265 /**
266 * Plugin uninstall
267 */
268 public static function uninstall()
269 {
270 if (!current_user_can('activate_plugins')) {
271 return;
272 }
273
274 $users = get_users(array(
275 'meta_key' => 'user_status',
276 'meta_value' => 'deactive',
277 'fields' => 'ID'
278 ));
279
280 foreach ($users as $user_id) {
281 delete_user_meta($user_id, 'user_status');
282 delete_user_meta($user_id, 'user_status_message');
283 delete_user_meta($user_id, 'block_expiry_date');
284 delete_user_meta($user_id, 'blocked_by');
285 delete_user_meta($user_id, 'blocked_date');
286 }
287
288 BUA_Logger::drop_table();
289
290 delete_option('bua_default_message');
291 delete_option('bua_email_notifications');
292 delete_option('bua_admin_notifications');
293 delete_option('bua_log_activities');
294 delete_option('bua_daily_report');
295 delete_option('bua_weekly_summary');
296 delete_option('bua_log_retention_days');
297 delete_option('bua_auto_unblock');
298 delete_option('bua_db_version');
299 }
300
301 /**
302 * Get component instance
303 *
304 * @param string $name Component name
305 * @return object|null Component instance
306 */
307 public function get_component($name)
308 {
309 return isset($this->components[$name]) ? $this->components[$name] : null;
310 }
311
312 /**
313 * Prevent cloning
314 */
315 private function __clone() {}
316
317 /**
318 * Prevent unserializing
319 */
320 public function __wakeup()
321 {
322 wp_die(__('Cannot unserialize singleton', 'block-user-account'));
323 }
324 }
325
326 /**
327 * Initialize plugin
328 *
329 * @return Block_User_Account
330 */
331 function BUA()
332 {
333 return Block_User_Account::instance();
334 }
335
336 /**
337 * Handle CSV export before any output
338 */
339 add_action('admin_init', function () {
340 if (!isset($_GET['page']) || $_GET['page'] !== 'block-user-account-settings') {
341 return;
342 }
343
344 if (!isset($_GET['bua_export']) || $_GET['bua_export'] !== '1') {
345 return;
346 }
347
348 if (!wp_verify_nonce($_GET['bua_export_nonce'], 'bua_export_logs')) {
349 return;
350 }
351
352 if (!current_user_can('manage_options')) {
353 return;
354 }
355
356 $csv = BUA_Logger::export_logs();
357
358 // Clear all buffers
359 while (ob_get_level()) {
360 ob_end_clean();
361 }
362
363 nocache_headers();
364 header('Content-Type: text/csv; charset=utf-8');
365 header('Content-Disposition: attachment; filename="bua-logs-' . date('Y-m-d') . '.csv"');
366
367 echo $csv;
368 exit;
369 });
370
371 BUA();
372