class-wpsweep-api.php
3 years ago
class-wpsweep-command.php
3 years ago
class-wpsweep.php
3 years ago
class-wpsweep-api.php
195 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WP-Sweep WP-API |
| 4 | * |
| 5 | * @package wp-sweep |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Class WPSweep_Api |
| 10 | */ |
| 11 | class WPSweep_Api { |
| 12 | /** |
| 13 | * WP-Sweep WP Rest API namespace |
| 14 | * |
| 15 | * @var string |
| 16 | */ |
| 17 | private $namespace = 'sweep/v1'; |
| 18 | |
| 19 | /** |
| 20 | * List of sweeps |
| 21 | * |
| 22 | * @var array |
| 23 | */ |
| 24 | private $sweeps = array( |
| 25 | 'revisions', |
| 26 | 'auto_drafts', |
| 27 | 'deleted_posts', |
| 28 | 'unapproved_comments', |
| 29 | 'spam_comments', |
| 30 | 'deleted_comments', |
| 31 | 'transient_options', |
| 32 | 'orphan_postmeta', |
| 33 | 'orphan_commentmeta', |
| 34 | 'orphan_usermeta', |
| 35 | 'orphan_termmeta', |
| 36 | 'orphan_term_relationships', |
| 37 | 'unused_terms', |
| 38 | 'duplicated_postmeta', |
| 39 | 'duplicated_commentmeta', |
| 40 | 'duplicated_usermeta', |
| 41 | 'duplicated_termmeta', |
| 42 | 'optimize_database', |
| 43 | 'oembed_postmeta', |
| 44 | ); |
| 45 | |
| 46 | /** |
| 47 | * Register WP-Sweep API Routes |
| 48 | * |
| 49 | * @since 1.1.0 |
| 50 | * |
| 51 | * @access public |
| 52 | * @return void |
| 53 | */ |
| 54 | public function __construct() { |
| 55 | add_action( |
| 56 | 'rest_api_init', function() { |
| 57 | register_rest_route( |
| 58 | $this->namespace, 'count/(?P<name>\w+)', array( |
| 59 | 'methods' => WP_REST_Server::READABLE, |
| 60 | 'callback' => array( $this, 'count' ), |
| 61 | 'permission_callback' => array( $this, 'permission_check' ), |
| 62 | 'args' => array( |
| 63 | 'name' => array( |
| 64 | 'required' => true, |
| 65 | 'validate_callback' => array( $this, 'is_sweep_name_valid' ), |
| 66 | ), |
| 67 | ), |
| 68 | ) |
| 69 | ); |
| 70 | register_rest_route( |
| 71 | $this->namespace, 'details/(?P<name>\w+)', array( |
| 72 | 'methods' => WP_REST_Server::READABLE, |
| 73 | 'callback' => array( $this, 'details' ), |
| 74 | 'permission_callback' => array( $this, 'permission_check' ), |
| 75 | 'args' => array( |
| 76 | 'name' => array( |
| 77 | 'required' => true, |
| 78 | 'validate_callback' => array( $this, 'is_sweep_name_valid' ), |
| 79 | ), |
| 80 | ), |
| 81 | ) |
| 82 | ); |
| 83 | register_rest_route( |
| 84 | $this->namespace, 'sweep/(?P<name>\w+)', array( |
| 85 | 'methods' => WP_REST_Server::DELETABLE, |
| 86 | 'callback' => array( $this, 'sweep' ), |
| 87 | 'permission_callback' => array( $this, 'permission_check' ), |
| 88 | 'args' => array( |
| 89 | 'name' => array( |
| 90 | 'required' => true, |
| 91 | 'validate_callback' => array( $this, 'is_sweep_name_valid' ), |
| 92 | ), |
| 93 | ), |
| 94 | ) |
| 95 | ); |
| 96 | } |
| 97 | ); |
| 98 | } |
| 99 | /** |
| 100 | * Sweep item count |
| 101 | * |
| 102 | * @since 1.1.0 |
| 103 | * |
| 104 | * @access public |
| 105 | * @param WP_REST_Request $request Request. |
| 106 | * @return WP_REST_Response |
| 107 | */ |
| 108 | public function count( $request ) { |
| 109 | $params = $request->get_params(); |
| 110 | |
| 111 | $sweep = WPSweep::get_instance(); |
| 112 | $count = (int) $sweep->count( $params['name'] ); |
| 113 | |
| 114 | return new WP_REST_Response( |
| 115 | array( |
| 116 | 'name' => $params['name'], |
| 117 | 'count' => $count, |
| 118 | ) |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Sweep details |
| 124 | * |
| 125 | * @since 1.1.0 |
| 126 | * |
| 127 | * @access public |
| 128 | * @param WP_REST_Request $request Request. |
| 129 | * @return WP_REST_Response |
| 130 | */ |
| 131 | public function details( $request ) { |
| 132 | $params = $request->get_params(); |
| 133 | |
| 134 | $sweep = WPSweep::get_instance(); |
| 135 | $details = $sweep->details( $params['name'] ); |
| 136 | |
| 137 | return new WP_REST_Response( |
| 138 | array( |
| 139 | 'name' => $params['name'], |
| 140 | 'count' => count( $details ), |
| 141 | 'data' => $details, |
| 142 | ) |
| 143 | ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Lets do the sweeping |
| 148 | * |
| 149 | * @since 1.1.0 |
| 150 | * |
| 151 | * @access public |
| 152 | * @param WP_REST_Request $request Request. |
| 153 | * @return WP_REST_Response |
| 154 | */ |
| 155 | public function sweep( $request ) { |
| 156 | $params = $request->get_params(); |
| 157 | |
| 158 | $sweep = WPSweep::get_instance(); |
| 159 | $results = $sweep->sweep( $params['name'] ); |
| 160 | |
| 161 | return new WP_REST_Response( |
| 162 | array( |
| 163 | 'success' => ! empty( $results ), |
| 164 | 'name' => $params['name'], |
| 165 | 'message' => empty( $results ) ? __( 'No items left to sweep.', 'wp-sweep' ) : $results, |
| 166 | ) |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Check whether a sweep is valid |
| 172 | * |
| 173 | * @since 1.1.0 |
| 174 | * |
| 175 | * @access public |
| 176 | * @param string $name Sweep name. |
| 177 | * @return bool Is the sweep name valid? |
| 178 | */ |
| 179 | public function is_sweep_name_valid( $name ) { |
| 180 | return in_array( $name, $this->sweeps, true ); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Check whether the function is allowed to be run. Must have either capabilities to enact action, or a valid nonce. |
| 185 | * |
| 186 | * @since 1.1.0 |
| 187 | * |
| 188 | * @access public |
| 189 | * @return bool Does the user has access to sweep? |
| 190 | */ |
| 191 | public function permission_check() { |
| 192 | return current_user_can( 'activate_plugins' ); |
| 193 | } |
| 194 | } |
| 195 |