| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Bulk Action Manager Class |
| 5 |
* |
| 6 |
* Handles global bulk action registration and the separator logic. |
| 7 |
* |
| 8 |
* @package ThinkRank\Admin |
| 9 |
* @since 1.1.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
declare(strict_types=1); |
| 13 |
|
| 14 |
namespace ThinkRank\Admin; |
| 15 |
|
| 16 |
// Prevent direct access |
| 17 |
if (!defined('ABSPATH')) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Bulk Action Manager Class |
| 23 |
* |
| 24 |
* Manages the "ThinkRank" group in bulk actions dropdown. |
| 25 |
* |
| 26 |
* @since 1.1.0 |
| 27 |
*/ |
| 28 |
class Bulk_Action_Manager { |
| 29 |
|
| 30 |
/** |
| 31 |
* Initialize the component |
| 32 |
* |
| 33 |
* @since 1.1.0 |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function init(): void { |
| 37 |
add_action('admin_init', [$this, 'register_hooks']); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Register hooks |
| 42 |
* |
| 43 |
* @since 1.1.0 |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public function register_hooks(): void { |
| 47 |
// We need to target all supported post types. |
| 48 |
// A generic approach is to get all public post types. |
| 49 |
$post_types = get_post_types(['public' => true], 'names'); |
| 50 |
|
| 51 |
foreach ($post_types as $post_type) { |
| 52 |
add_filter("bulk_actions-edit-{$post_type}", function ($bulk_actions) use ($post_type) { |
| 53 |
return $this->inject_bulk_actions($bulk_actions, $post_type); |
| 54 |
}); |
| 55 |
} |
| 56 |
|
| 57 |
add_action('admin_enqueue_scripts', [$this, 'bulk_action_footer_script']); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Inject ThinkRank bulk actions |
| 62 |
* |
| 63 |
* @since 1.1.0 |
| 64 |
* @param array $bulk_actions Existing bulk actions. |
| 65 |
* @param string $post_type Current post type. |
| 66 |
* @return array |
| 67 |
*/ |
| 68 |
public function inject_bulk_actions(array $bulk_actions, string $post_type): array { |
| 69 |
$new_actions = []; |
| 70 |
|
| 71 |
// Allow modules to add their actions |
| 72 |
// Filter: thinkrank_bulk_actions |
| 73 |
// Args: $new_actions (empty array), $post_type |
| 74 |
$thinkrank_actions = apply_filters('thinkrank_bulk_actions', [], $post_type); |
| 75 |
|
| 76 |
// If no actions were added, return original array |
| 77 |
if (empty($thinkrank_actions)) { |
| 78 |
return $bulk_actions; |
| 79 |
} |
| 80 |
|
| 81 |
// Add separator at the top of our group |
| 82 |
$new_actions['thinkrank_separator'] = '— ' . __('ThinkRank', 'thinkrank') . ' —'; |
| 83 |
|
| 84 |
// Merge module actions |
| 85 |
$new_actions = array_merge($new_actions, $thinkrank_actions); |
| 86 |
|
| 87 |
// Merge our group into the main list |
| 88 |
// We append to the end or merge. |
| 89 |
// array_merge might reindex numeric keys but bulk actions use string keys. |
| 90 |
return array_merge($bulk_actions, $new_actions); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Disable the "separator" bulk action option on post-list screens. |
| 95 |
* |
| 96 |
* Registered on admin_enqueue_scripts and attached as an inline script to the |
| 97 |
* core jQuery handle (rather than echoing a raw <script> in admin_footer) so it |
| 98 |
* passes Plugin Check's inline-script rule. |
| 99 |
* |
| 100 |
* @since 1.1.0 |
| 101 |
* @return void |
| 102 |
*/ |
| 103 |
public function bulk_action_footer_script(): void { |
| 104 |
// Only run on edit.php screens |
| 105 |
$screen = get_current_screen(); |
| 106 |
if (!$screen || $screen->base !== 'edit') { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
wp_enqueue_script('jquery'); |
| 111 |
wp_add_inline_script( |
| 112 |
'jquery', |
| 113 |
'jQuery(function($){ $(\'option[value="thinkrank_separator"]\').prop(\'disabled\', true); });' |
| 114 |
); |
| 115 |
} |
| 116 |
} |
| 117 |
|