sudar /
bulk-delete
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace BulkWP\BulkDelete\Core; |
||||
| 4 | |||||
| 5 | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
||||
| 6 | |||||
| 7 | /** |
||||
| 8 | * Bulk Delete Controller. |
||||
| 9 | * |
||||
| 10 | * Handle all requests and automatically perform nonce checks. |
||||
| 11 | * |
||||
| 12 | * @since 5.5.4 |
||||
| 13 | * @since 6.0.0 Added namespace. |
||||
| 14 | */ |
||||
| 15 | class Controller { |
||||
| 16 | /** |
||||
| 17 | * Load the controller and setup hooks and actions. |
||||
| 18 | * |
||||
| 19 | * @since 6.0.0 |
||||
| 20 | */ |
||||
| 21 | public function load() { |
||||
| 22 | add_action( 'admin_init', array( $this, 'request_handler' ) ); |
||||
| 23 | |||||
| 24 | add_action( 'bd_pre_bulk_action', array( $this, 'increase_timeout' ), 9 ); |
||||
| 25 | add_action( 'bd_before_scheduler', array( $this, 'increase_timeout' ), 9 ); |
||||
| 26 | |||||
| 27 | add_filter( 'bd_get_action_nonce_check', array( $this, 'verify_get_request_nonce' ), 10, 2 ); |
||||
| 28 | |||||
| 29 | add_action( 'wp_ajax_bd_load_taxonomy_term', array( $this, 'load_taxonomy_term' ) ); |
||||
| 30 | |||||
| 31 | add_filter( 'bd_help_tooltip', 'bd_generate_help_tooltip', 10, 2 ); |
||||
| 32 | add_filter( 'plugin_action_links', array( $this, 'filter_plugin_action_links' ), 10, 2 ); |
||||
| 33 | |||||
| 34 | if ( defined( 'BD_DEBUG' ) && BD_DEBUG ) { |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 35 | add_action( 'bd_after_query', array( $this, 'log_sql_query' ) ); |
||||
| 36 | } |
||||
| 37 | } |
||||
| 38 | |||||
| 39 | /** |
||||
| 40 | * Handle both POST and GET requests. |
||||
| 41 | * This method automatically triggers all the actions after checking the nonce. |
||||
| 42 | */ |
||||
| 43 | public function request_handler() { |
||||
| 44 | if ( isset( $_POST['bd_action'] ) ) { |
||||
| 45 | $bd_action = sanitize_text_field( $_POST['bd_action'] ); |
||||
| 46 | $nonce_valid = false; |
||||
| 47 | |||||
| 48 | if ( 'delete_jetpack_messages' === $bd_action && wp_verify_nonce( $_POST['sm-bulk-delete-misc-nonce'], 'sm-bulk-delete-misc' ) ) { |
||||
| 49 | $nonce_valid = true; |
||||
| 50 | } |
||||
| 51 | |||||
| 52 | /** |
||||
| 53 | * Perform nonce check. |
||||
| 54 | * |
||||
| 55 | * @since 5.5 |
||||
| 56 | */ |
||||
| 57 | if ( ! apply_filters( 'bd_action_nonce_check', $nonce_valid, $bd_action ) ) { |
||||
| 58 | return; |
||||
| 59 | } |
||||
| 60 | |||||
| 61 | /** |
||||
| 62 | * Before performing a bulk action. |
||||
| 63 | * This hook is for doing actions just before performing any bulk operation. |
||||
| 64 | * |
||||
| 65 | * @since 5.4 |
||||
| 66 | */ |
||||
| 67 | do_action( 'bd_pre_bulk_action', $bd_action ); |
||||
| 68 | |||||
| 69 | /** |
||||
| 70 | * Perform the bulk operation. |
||||
| 71 | * This hook is for doing the bulk operation. Nonce check has already happened by this point. |
||||
| 72 | * |
||||
| 73 | * @since 5.4 |
||||
| 74 | */ |
||||
| 75 | do_action( 'bd_' . $bd_action, $_POST ); |
||||
| 76 | } |
||||
| 77 | |||||
| 78 | if ( isset( $_GET['bd_action'] ) ) { |
||||
| 79 | $bd_action = sanitize_text_field( $_GET['bd_action'] ); |
||||
| 80 | $nonce_valid = false; |
||||
| 81 | |||||
| 82 | /** |
||||
| 83 | * Perform nonce check. |
||||
| 84 | * |
||||
| 85 | * @since 5.5.4 |
||||
| 86 | */ |
||||
| 87 | if ( ! apply_filters( 'bd_get_action_nonce_check', $nonce_valid, $bd_action ) ) { |
||||
| 88 | return; |
||||
| 89 | } |
||||
| 90 | |||||
| 91 | /** |
||||
| 92 | * Perform the bulk operation. |
||||
| 93 | * This hook is for doing the bulk operation. Nonce check has already happened by this point. |
||||
| 94 | * |
||||
| 95 | * @since 5.5.4 |
||||
| 96 | */ |
||||
| 97 | do_action( 'bd_' . $bd_action, $_GET ); |
||||
| 98 | } |
||||
| 99 | } |
||||
| 100 | |||||
| 101 | /** |
||||
| 102 | * Increase PHP timeout. |
||||
| 103 | * |
||||
| 104 | * This is to prevent bulk operations from timing out |
||||
| 105 | * |
||||
| 106 | * @since 5.5.4 |
||||
| 107 | */ |
||||
| 108 | public function increase_timeout() { |
||||
| 109 | // phpcs:ignore PHPCompatibility.PHP.DeprecatedIniDirectives.safe_modeDeprecatedRemoved |
||||
| 110 | if ( ! ini_get( 'safe_mode' ) ) { |
||||
| 111 | // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged |
||||
| 112 | @set_time_limit( 0 ); |
||||
| 113 | } |
||||
| 114 | } |
||||
| 115 | |||||
| 116 | /** |
||||
| 117 | * Verify if GET request has a valid nonce. |
||||
| 118 | * |
||||
| 119 | * @since 5.5.4 |
||||
| 120 | * |
||||
| 121 | * @param bool $result Whether nonce is valid. |
||||
| 122 | * @param string $action Action name. |
||||
| 123 | * |
||||
| 124 | * @return bool True if nonce is valid, otherwise return $result. |
||||
| 125 | */ |
||||
| 126 | public function verify_get_request_nonce( $result, $action ) { |
||||
| 127 | if ( check_admin_referer( "bd-{$action}", "bd-{$action}-nonce" ) ) { |
||||
| 128 | return true; |
||||
| 129 | } |
||||
| 130 | |||||
| 131 | return $result; |
||||
| 132 | } |
||||
| 133 | |||||
| 134 | /** |
||||
| 135 | * Ajax call back function for getting taxonomies to load select2 options. |
||||
| 136 | * |
||||
| 137 | * @since 6.0.0 |
||||
| 138 | */ |
||||
| 139 | public function load_taxonomy_term() { |
||||
| 140 | $response = array(); |
||||
| 141 | |||||
| 142 | $taxonomy = sanitize_text_field( $_GET['taxonomy'] ); |
||||
| 143 | |||||
| 144 | $terms = get_terms( |
||||
| 145 | array( |
||||
| 146 | 'taxonomy' => $taxonomy, |
||||
| 147 | 'hide_empty' => false, |
||||
| 148 | 'search' => sanitize_text_field( $_GET['q'] ), |
||||
| 149 | ) |
||||
| 150 | ); |
||||
| 151 | |||||
| 152 | if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { |
||||
| 153 | foreach ( $terms as $term ) { |
||||
| 154 | $response[] = array( |
||||
| 155 | absint( $term->term_id ), |
||||
| 156 | $term->name . ' (' . $term->count . __( ' Posts', 'bulk-delete' ) . ')', |
||||
| 157 | ); |
||||
| 158 | } |
||||
| 159 | } |
||||
| 160 | |||||
| 161 | echo wp_json_encode( $response ); |
||||
|
0 ignored issues
–
show
Are you sure
wp_json_encode($response) of type false|string can be used in echo?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 162 | die; |
||||
| 163 | } |
||||
| 164 | |||||
| 165 | /** |
||||
| 166 | * Adds the settings link in the Plugin page. |
||||
| 167 | * |
||||
| 168 | * Based on http://striderweb.com/nerdaphernalia/2008/06/wp-use-action-links/. |
||||
| 169 | * |
||||
| 170 | * @since 6.0.0 Moved into Controller class. |
||||
| 171 | * |
||||
| 172 | * @staticvar string $this_plugin |
||||
| 173 | * |
||||
| 174 | * @param array $action_links Action Links. |
||||
| 175 | * @param string $file Plugin file name. |
||||
| 176 | * |
||||
| 177 | * @return array Modified links. |
||||
| 178 | */ |
||||
| 179 | public function filter_plugin_action_links( $action_links, $file ) { |
||||
| 180 | static $this_plugin; |
||||
| 181 | |||||
| 182 | if ( ! $this_plugin ) { |
||||
| 183 | $this_plugin = plugin_basename( $this->get_plugin_file() ); |
||||
| 184 | } |
||||
| 185 | |||||
| 186 | if ( $file === $this_plugin ) { |
||||
| 187 | /** |
||||
| 188 | * Filter plugin action links added by Bulk Move. |
||||
| 189 | * |
||||
| 190 | * @since 6.0.0 |
||||
| 191 | * |
||||
| 192 | * @param array Plugin Links. |
||||
| 193 | */ |
||||
| 194 | $bm_action_links = apply_filters( 'bd_plugin_action_links', array() ); |
||||
| 195 | |||||
| 196 | if ( ! empty( $bm_action_links ) ) { |
||||
| 197 | $action_links = array_merge( $bm_action_links, $action_links ); |
||||
| 198 | } |
||||
| 199 | } |
||||
| 200 | |||||
| 201 | return $action_links; |
||||
| 202 | } |
||||
| 203 | |||||
| 204 | /** |
||||
| 205 | * Log SQL query used by Bulk Delete. |
||||
| 206 | * |
||||
| 207 | * Query is logged only when `BD_DEBUG` is set. |
||||
| 208 | * |
||||
| 209 | * @since 5.6 |
||||
| 210 | * @since 6.0.0 Moved into Controller class. |
||||
| 211 | * |
||||
| 212 | * @param \WP_Query $wp_query WP Query object. |
||||
| 213 | */ |
||||
| 214 | public function log_sql_query( $wp_query ) { |
||||
| 215 | $query = $wp_query->request; |
||||
| 216 | |||||
| 217 | /** |
||||
| 218 | * Bulk Delete query is getting logged. |
||||
| 219 | * |
||||
| 220 | * @since 5.6 |
||||
| 221 | * |
||||
| 222 | * @param string $query Bulk Delete SQL Query. |
||||
| 223 | */ |
||||
| 224 | do_action( 'bd_log_sql_query', $query ); |
||||
| 225 | |||||
| 226 | error_log( 'Bulk Delete Query: ' . $query ); |
||||
| 227 | } |
||||
| 228 | |||||
| 229 | /** |
||||
| 230 | * Temporary fix to get plugin file. |
||||
| 231 | * |
||||
| 232 | * TODO: Remove this method from this class. |
||||
| 233 | * |
||||
| 234 | * @since 6.0.0 |
||||
| 235 | * |
||||
| 236 | * @return string |
||||
| 237 | */ |
||||
| 238 | private function get_plugin_file() { |
||||
| 239 | $bd = BULK_DELETE(); |
||||
| 240 | |||||
| 241 | return $bd->get_plugin_file(); |
||||
| 242 | } |
||||
| 243 | } |
||||
| 244 |