sudar /
bulk-delete
| 1 | <?php |
||
| 2 | namespace BulkWP\BulkDelete\Core\Base; |
||
| 3 | |||
| 4 | 1 | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
|
| 5 | |||
| 6 | /** |
||
| 7 | * Encapsulates the Bulk Delete Meta box Module Logic. |
||
| 8 | * |
||
| 9 | * All Bulk Delete Meta box Modules should extend this class. |
||
| 10 | * |
||
| 11 | * @since 5.7.0 |
||
| 12 | */ |
||
| 13 | abstract class BaseMetabox { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Item Type. Possible values 'posts', 'pages', 'users' etc. |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $item_type = 'posts'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The hook suffix of the screen where this meta box would be shown. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $page_hook_suffix; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Slug of the page where this module will be shown. |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $page_slug; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Slug for the form fields. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $field_slug; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Slug of the meta box. |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $meta_box_slug; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Action in which the delete operation should be performed. |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $action; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Hook for scheduler. |
||
| 59 | * |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | protected $cron_hook; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Url of the scheduler addon. |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | protected $scheduler_url; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Messages shown to the user. |
||
| 73 | * |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected $messages = array( |
||
| 77 | 'box_label' => '', |
||
| 78 | ); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Initialize and setup variables. |
||
| 82 | * |
||
| 83 | * @return void |
||
| 84 | */ |
||
| 85 | abstract protected function initialize(); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Render the Metabox. |
||
| 89 | * |
||
| 90 | * @return void |
||
| 91 | */ |
||
| 92 | abstract public function render(); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Process user input and create metabox options. |
||
| 96 | * |
||
| 97 | * @param array $request Request array. |
||
| 98 | * |
||
| 99 | * @return array User options. |
||
| 100 | */ |
||
| 101 | abstract protected function convert_user_input_to_options( $request ); |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Perform the deletion. |
||
| 105 | * |
||
| 106 | * @param array $delete_options Array of Delete options. |
||
| 107 | * |
||
| 108 | * @return int Number of items that were deleted. |
||
| 109 | */ |
||
| 110 | abstract public function delete( $delete_options ); |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Get Success Message. |
||
| 114 | * |
||
| 115 | * @param int $items_deleted Number of items that were deleted. |
||
| 116 | * |
||
| 117 | * @return string Success message. |
||
| 118 | */ |
||
| 119 | abstract protected function get_success_message( $items_deleted ); |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Create new instances of Metabox. |
||
| 123 | */ |
||
| 124 | public function __construct() { |
||
| 125 | $this->initialize(); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Register. |
||
| 130 | * |
||
| 131 | * @param string $hook_suffix Page Hook Suffix. |
||
| 132 | * @param string $page_slug Page slug. |
||
| 133 | */ |
||
| 134 | public function register( $hook_suffix, $page_slug ) { |
||
| 135 | $this->page_hook_suffix = $hook_suffix; |
||
| 136 | $this->page_slug = $page_slug; |
||
| 137 | |||
| 138 | add_action( "add_meta_boxes_{$this->page_hook_suffix}", array( $this, 'setup_metabox' ) ); |
||
| 139 | |||
| 140 | add_action( 'bd_' . $this->action, array( $this, 'process' ) ); |
||
| 141 | add_filter( 'bd_javascript_array', array( $this, 'filter_js_array' ) ); |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Setup the meta box. |
||
| 146 | */ |
||
| 147 | public function setup_metabox() { |
||
| 148 | add_meta_box( |
||
| 149 | $this->meta_box_slug, |
||
| 150 | $this->messages['box_label'], |
||
| 151 | array( $this, 'render_box' ), |
||
| 152 | $this->page_hook_suffix, |
||
| 153 | 'advanced' |
||
| 154 | ); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Render the meta box. |
||
| 159 | */ |
||
| 160 | public function render_box() { |
||
| 161 | if ( $this->is_hidden() ) { |
||
| 162 | printf( |
||
| 163 | /* translators: 1 module url */ |
||
| 164 | __( 'This section just got enabled. Kindly <a href = "%1$s">refresh</a> the page to fully enable it.', 'bulk-delete' ), |
||
| 165 | 'admin.php?page=' . $this->page_slug |
||
| 166 | ); |
||
| 167 | |||
| 168 | return; |
||
| 169 | } |
||
| 170 | |||
| 171 | $this->render(); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Is the current meta box hidden by user. |
||
| 176 | * |
||
| 177 | * @return bool True, if hidden. False, otherwise. |
||
| 178 | */ |
||
| 179 | protected function is_hidden() { |
||
| 180 | $current_user = wp_get_current_user(); |
||
| 181 | $user_meta_field = $this->get_hidden_box_user_meta_field(); |
||
| 182 | $hidden_boxes = get_user_meta( $current_user->ID, $user_meta_field, true ); |
||
| 183 | |||
| 184 | return is_array( $hidden_boxes ) && in_array( $this->meta_box_slug, $hidden_boxes, true ); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Get the user meta field that stores the status of the hidden meta boxes. |
||
| 189 | * |
||
| 190 | * @return string Name of the User Meta field. |
||
| 191 | */ |
||
| 192 | protected function get_hidden_box_user_meta_field() { |
||
| 193 | if ( 'posts' === $this->item_type ) { |
||
| 194 | return 'metaboxhidden_toplevel_page_bulk-delete-posts'; |
||
| 195 | } else { |
||
| 196 | return 'metaboxhidden_bulk-wp_page_' . $this->page_slug; |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Filter the js array. |
||
| 202 | * |
||
| 203 | * This function will be overridden by the child classes. |
||
| 204 | * |
||
| 205 | * @param array $js_array JavaScript Array. |
||
| 206 | * |
||
| 207 | * @return array Modified JavaScript Array |
||
| 208 | */ |
||
| 209 | public function filter_js_array( $js_array ) { |
||
| 210 | return $js_array; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Render filtering table header. |
||
| 215 | */ |
||
| 216 | protected function render_filtering_table_header() { |
||
| 217 | bd_render_filtering_table_header(); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Render restrict settings. |
||
| 222 | */ |
||
| 223 | protected function render_restrict_settings() { |
||
| 224 | bd_render_restrict_settings( $this->field_slug, $this->item_type ); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Render delete settings. |
||
| 229 | */ |
||
| 230 | protected function render_delete_settings() { |
||
| 231 | bd_render_delete_settings( $this->field_slug ); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Render limit settings. |
||
| 236 | */ |
||
| 237 | protected function render_limit_settings() { |
||
| 238 | bd_render_limit_settings( $this->field_slug, $this->item_type ); |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Render cron settings. |
||
| 243 | */ |
||
| 244 | protected function render_cron_settings() { |
||
| 245 | bd_render_cron_settings( $this->field_slug, $this->scheduler_url ); |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Render submit button. |
||
| 250 | */ |
||
| 251 | protected function render_submit_button() { |
||
| 252 | bd_render_submit_button( $this->action ); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Helper function for processing deletion. |
||
| 257 | * Setups up cron and invokes the actual delete method. |
||
| 258 | * |
||
| 259 | * @param array $request Request array. |
||
| 260 | */ |
||
| 261 | public function process( $request ) { |
||
| 262 | $options = $this->convert_user_input_to_options( $request ); |
||
| 263 | |||
| 264 | if ( $this->is_scheduled( $request ) ) { |
||
| 265 | $msg = $this->schedule_deletion( $request, $options ); |
||
| 266 | } else { |
||
| 267 | $items_deleted = $this->delete( $options ); |
||
| 268 | $msg = sprintf( $this->get_success_message( $items_deleted ), $items_deleted ); |
||
| 269 | } |
||
| 270 | |||
| 271 | add_settings_error( |
||
| 272 | $this->page_slug, |
||
| 273 | $this->action, |
||
| 274 | $msg, |
||
| 275 | 'updated' |
||
| 276 | ); |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Getter for cron_hook. |
||
| 281 | * |
||
| 282 | * @return string Cron Hook name. |
||
| 283 | */ |
||
| 284 | public function get_cron_hook() { |
||
| 285 | return $this->cron_hook; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Getter for field slug. |
||
| 290 | * |
||
| 291 | * @return string Field Slug. |
||
| 292 | */ |
||
| 293 | public function get_field_slug() { |
||
| 294 | return $this->field_slug; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Getter for action. |
||
| 299 | * |
||
| 300 | * @return string Metabox action. |
||
| 301 | */ |
||
| 302 | public function get_action() { |
||
| 303 | return $this->action; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Is the current deletion request a scheduled request? |
||
| 308 | * |
||
| 309 | * @param array $request Request object. |
||
| 310 | * |
||
| 311 | * @return bool True if it is a scheduled request, False otherwise. |
||
| 312 | */ |
||
| 313 | protected function is_scheduled( $request ) { |
||
| 314 | return bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_cron', false ); |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Schedule Deletion of items. |
||
| 319 | * |
||
| 320 | * @param array $request Request array. |
||
| 321 | * @param array $options Deletion option. |
||
| 322 | * |
||
| 323 | * @return string Message. |
||
| 324 | */ |
||
| 325 | protected function schedule_deletion( $request, $options ) { |
||
| 326 | $freq = $request[ 'smbd_' . $this->field_slug . '_cron_freq' ]; |
||
| 327 | $time = strtotime( $request[ 'smbd_' . $this->field_slug . '_cron_start' ] ) - ( get_option( 'gmt_offset' ) * 60 * 60 ); |
||
| 328 | |||
| 329 | if ( -1 === $freq ) { |
||
| 330 | wp_schedule_single_event( $time, $this->cron_hook, array( $options ) ); |
||
| 331 | } else { |
||
| 332 | wp_schedule_event( $time, $freq, $this->cron_hook, array( $options ) ); |
||
| 333 | } |
||
| 334 | |||
| 335 | return $this->messages['scheduled'] . ' ' . $this->get_task_list_link(); |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Get the link to the page that lists all the scheduled tasks. |
||
| 340 | * |
||
| 341 | * @return string Link to scheduled tasks page. |
||
| 342 | */ |
||
| 343 | protected function get_task_list_link() { |
||
| 344 | return sprintf( |
||
| 345 | __( 'See the full list of <a href = "%s">scheduled tasks</a>', 'bulk-delete' ), |
||
| 346 | get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . Bulk_Delete::CRON_PAGE_SLUG |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 347 | ); |
||
| 348 | } |
||
| 349 | } |
||
| 350 |