class-gsc-bulk-action.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Google_Search_Console
  6. */
  7. /**
  8. * Class WPSEO_GSC_Bulk_Action
  9. */
  10. class WPSEO_GSC_Bulk_Action {
  11. /**
  12. * Setting the listener on the bulk action post
  13. */
  14. public function __construct() {
  15. if ( wp_verify_nonce( filter_input( INPUT_POST, 'wpseo_gsc_nonce' ), 'wpseo_gsc_nonce' ) ) {
  16. $this->handle_bulk_action();
  17. }
  18. }
  19. /**
  20. * Handles the bulk action when there is an action posted
  21. */
  22. private function handle_bulk_action() {
  23. $bulk_action = $this->determine_bulk_action();
  24. if ( $bulk_action !== false ) {
  25. $this->run_bulk_action( $bulk_action, $this->posted_issues() );
  26. wp_redirect( filter_input( INPUT_POST, '_wp_http_referer' ) );
  27. exit;
  28. }
  29. }
  30. /**
  31. * Determine which bulk action is selected and return that value
  32. *
  33. * @return string|bool
  34. */
  35. private function determine_bulk_action() {
  36. $action_inputs = array(
  37. 'action', // Bulk action select above the table.
  38. 'action2', // Bulk action select below the table.
  39. );
  40. foreach ( $action_inputs as $action_name ) {
  41. $action = filter_input( INPUT_POST, $action_name );
  42. if ( ! empty( $action ) && $action !== '-1' ) {
  43. return $action;
  44. }
  45. }
  46. return false;
  47. }
  48. /**
  49. * Get the posted issues and return them
  50. *
  51. * @return array
  52. */
  53. private function posted_issues() {
  54. $issues = filter_input( INPUT_POST, 'wpseo_crawl_issues', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
  55. if ( ! empty( $issues ) ) {
  56. return $issues;
  57. }
  58. // Fallback if issues are empty.
  59. return array();
  60. }
  61. /**
  62. * Runs the bulk action
  63. *
  64. * @param string $bulk_action Action type.
  65. * @param array $issues Set of issues to apply to.
  66. */
  67. private function run_bulk_action( $bulk_action, $issues ) {
  68. switch ( $bulk_action ) {
  69. case 'mark_as_fixed':
  70. array_map( array( $this, 'action_mark_as_fixed' ), $issues );
  71. break;
  72. }
  73. }
  74. /**
  75. * Marks the issue as fixed
  76. *
  77. * @param string $issue Issue URL.
  78. *
  79. * @return string
  80. */
  81. private function action_mark_as_fixed( $issue ) {
  82. new WPSEO_GSC_Marker( $issue );
  83. return $issue;
  84. }
  85. }