media-cleaner
Last commit date
common
7 years ago
parsers
7 years ago
scripts
7 years ago
views
7 years ago
admin.php
7 years ago
api.php
7 years ago
core.php
7 years ago
engine.php
7 years ago
media-cleaner.php
7 years ago
parsers.php
7 years ago
readme.txt
7 years ago
ui.php
7 years ago
api.php
234 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_WPMC_API { |
| 4 | |
| 5 | function __construct( $core, $admin, $engine ) { |
| 6 | $this->core = $core; |
| 7 | $this->engine = $engine; |
| 8 | $this->admin = $admin; |
| 9 | add_action( 'wp_ajax_wpmc_prepare_do', array( $this, 'wp_ajax_wpmc_prepare_do' ) ); |
| 10 | add_action( 'wp_ajax_wpmc_scan', array( $this, 'wp_ajax_wpmc_scan' ) ); |
| 11 | add_action( 'wp_ajax_wpmc_scan_do', array( $this, 'wp_ajax_wpmc_scan_do' ) ); |
| 12 | add_action( 'wp_ajax_wpmc_get_all_issues', array( $this, 'wp_ajax_wpmc_get_all_issues' ) ); |
| 13 | add_action( 'wp_ajax_wpmc_get_all_deleted', array( $this, 'wp_ajax_wpmc_get_all_deleted' ) ); |
| 14 | add_action( 'wp_ajax_wpmc_delete_do', array( $this, 'wp_ajax_wpmc_delete_do' ) ); |
| 15 | add_action( 'wp_ajax_wpmc_ignore_do', array( $this, 'wp_ajax_wpmc_ignore_do' ) ); |
| 16 | add_action( 'wp_ajax_wpmc_recover_do', array( $this, 'wp_ajax_wpmc_recover_do' ) ); |
| 17 | add_action( 'wp_ajax_wpmc_validate_option', array( $this, 'wp_ajax_wpmc_validate_option' ) ); |
| 18 | } |
| 19 | |
| 20 | /******************************************************************************* |
| 21 | * ASYNCHRONOUS AJAX FUNCTIONS |
| 22 | ******************************************************************************/ |
| 23 | |
| 24 | function wp_ajax_wpmc_prepare_do() { |
| 25 | $limit = isset( $_POST['limit'] ) ? $_POST['limit'] : 0; |
| 26 | $limitsize = get_option( 'wpmc_posts_buffer', 5 ); |
| 27 | $finished = $this->engine->parse( $limit, $limitsize, $message ); // $message is set by run() |
| 28 | echo json_encode( |
| 29 | array( |
| 30 | 'success' => true, |
| 31 | 'finished' => $finished, |
| 32 | 'limit' => $limit + $limitsize, |
| 33 | 'message' => $message ) |
| 34 | ); |
| 35 | die(); |
| 36 | } |
| 37 | |
| 38 | function wp_ajax_wpmc_scan() { |
| 39 | global $wpdb; |
| 40 | |
| 41 | $method = get_option( 'wpmc_method', 'media' ); |
| 42 | if ( !$this->admin->is_registered() ) |
| 43 | $method = 'media'; |
| 44 | |
| 45 | if ( $method == 'files' ) { |
| 46 | $output = null; |
| 47 | $path = isset( $_POST['path'] ) ? $_POST['path'] : null; |
| 48 | $files = $this->engine->get_files( $path ); |
| 49 | |
| 50 | if ( $files === null ) { |
| 51 | $output = array( |
| 52 | 'results' => array(), |
| 53 | 'success' => true, |
| 54 | 'message' => __( "No files for this path ($path).", 'media-cleaner' ) |
| 55 | ); |
| 56 | } |
| 57 | else { |
| 58 | $output = array( |
| 59 | 'results' => $files, |
| 60 | 'success' => true, |
| 61 | 'message' => __( "Files retrieved.", 'media-cleaner' ) |
| 62 | ); |
| 63 | } |
| 64 | echo json_encode( $output ); |
| 65 | die(); |
| 66 | } |
| 67 | |
| 68 | if ( $method == 'media' ) { |
| 69 | $limit = isset( $_POST['limit'] ) ? $_POST['limit'] : 0; |
| 70 | $limitsize = get_option( 'wpmc_medias_buffer', 100 ); |
| 71 | $results = $this->engine->get_media_entries( $limit, $limitsize ); |
| 72 | $finished = count( $results ) < $limitsize; |
| 73 | echo json_encode( |
| 74 | array( |
| 75 | 'results' => $results, |
| 76 | 'success' => true, |
| 77 | 'finished' => $finished, |
| 78 | 'limit' => $limit + $limitsize, |
| 79 | 'message' => __( "Medias retrieved.", 'media-cleaner' ) ) |
| 80 | ); |
| 81 | die(); |
| 82 | } |
| 83 | |
| 84 | // No task. |
| 85 | echo json_encode( array( 'success' => false, 'message' => __( "No task.", 'media-cleaner' ) ) ); |
| 86 | die(); |
| 87 | } |
| 88 | |
| 89 | function wp_ajax_wpmc_scan_do() { |
| 90 | // For debug, to pretend there is a timeout |
| 91 | //$this->core->deepsleep(10); |
| 92 | //header("HTTP/1.0 408 Request Timeout"); |
| 93 | //exit; |
| 94 | |
| 95 | ob_start(); |
| 96 | $type = $_POST['type']; |
| 97 | $data = $_POST['data']; |
| 98 | $this->core->timeout_check_start( count( $data ) ); |
| 99 | $success = 0; |
| 100 | if ( $type == 'file' ) { |
| 101 | do_action( 'wpmc_check_file_init' ); // Build_CroppedFile_Cache() in pro core.php |
| 102 | } |
| 103 | foreach ( $data as $piece ) { |
| 104 | $this->core->timeout_check(); |
| 105 | if ( $type == 'file' ) { |
| 106 | $this->core->log( "Check File: {$piece}" ); |
| 107 | |
| 108 | $result = ( $this->engine->check_file( $piece ) ? 1 : 0 ); |
| 109 | |
| 110 | if ( $result ) |
| 111 | $success += $result; |
| 112 | } |
| 113 | else if ( $type == 'media' ) { |
| 114 | $this->core->log( "Checking Media #{$piece}" ); |
| 115 | $result = ( $this->engine->check_media( $piece ) ? 1 : 0 ); |
| 116 | if ( $result ) |
| 117 | $success += $result; |
| 118 | } |
| 119 | $this->core->log(); |
| 120 | $this->core->timeout_check_additem(); |
| 121 | } |
| 122 | ob_end_clean(); |
| 123 | echo json_encode( |
| 124 | array( |
| 125 | 'success' => true, |
| 126 | 'result' => array( 'type' => $type, 'data' => $data, 'success' => $success ), |
| 127 | 'message' => __( "Items checked.", 'media-cleaner' ) |
| 128 | ) |
| 129 | ); |
| 130 | die(); |
| 131 | } |
| 132 | |
| 133 | function wp_ajax_wpmc_get_all_issues() { |
| 134 | global $wpdb; |
| 135 | $isTrash = ( isset( $_POST['isTrash'] ) && $_POST['isTrash'] == 1 ) ? true : false; |
| 136 | $table_name = $wpdb->prefix . "mclean_scan"; |
| 137 | $q = "SELECT id FROM $table_name WHERE ignored = 0 AND deleted = " . ( $isTrash ? 1 : 0 ); |
| 138 | if ( $search = ( isset( $_POST['s'] ) && $_POST['s'] ) ? sanitize_text_field( $_POST['s'] ) : '' ) |
| 139 | $q = $wpdb->prepare( $q . ' AND path LIKE %s', '%' . $wpdb->esc_like( $search ) . '%' ); |
| 140 | $ids = $wpdb->get_col( $q ); |
| 141 | |
| 142 | echo json_encode( |
| 143 | array( |
| 144 | 'results' => array( 'ids' => $ids ), |
| 145 | 'success' => true, |
| 146 | 'message' => __( "List generated.", 'media-cleaner' ) |
| 147 | ) |
| 148 | ); |
| 149 | die; |
| 150 | } |
| 151 | |
| 152 | function wp_ajax_wpmc_get_all_deleted() { |
| 153 | global $wpdb; |
| 154 | $table_name = $wpdb->prefix . "mclean_scan"; |
| 155 | $ids = $wpdb->get_col( "SELECT id FROM $table_name WHERE ignored = 0 AND deleted = 1" ); |
| 156 | echo json_encode( |
| 157 | array( |
| 158 | 'results' => array( 'ids' => $ids ), |
| 159 | 'success' => true, |
| 160 | 'message' => __( "List generated.", 'media-cleaner' ) |
| 161 | ) |
| 162 | ); |
| 163 | die; |
| 164 | } |
| 165 | |
| 166 | function wp_ajax_wpmc_delete_do() { |
| 167 | ob_start(); |
| 168 | $data = $_POST['data']; |
| 169 | $success = 0; |
| 170 | foreach ( $data as $piece ) { |
| 171 | $success += ( $this->core->delete( $piece ) ? 1 : 0 ); |
| 172 | } |
| 173 | ob_end_clean(); |
| 174 | echo json_encode( |
| 175 | array( |
| 176 | 'success' => true, |
| 177 | 'result' => array( 'data' => $data, 'success' => $success ), |
| 178 | 'message' => __( "Status unknown.", 'media-cleaner' ) |
| 179 | ) |
| 180 | ); |
| 181 | die(); |
| 182 | } |
| 183 | |
| 184 | function wp_ajax_wpmc_ignore_do() { |
| 185 | ob_start(); |
| 186 | $data = $_POST['data']; |
| 187 | $success = 0; |
| 188 | foreach ( $data as $piece ) { |
| 189 | $success += ( $this->core->ignore( $piece ) ? 1 : 0 ); |
| 190 | } |
| 191 | ob_end_clean(); |
| 192 | echo json_encode( |
| 193 | array( |
| 194 | 'success' => true, |
| 195 | 'result' => array( 'data' => $data, 'success' => $success ), |
| 196 | 'message' => __( "Status unknown.", 'media-cleaner' ) |
| 197 | ) |
| 198 | ); |
| 199 | die(); |
| 200 | } |
| 201 | |
| 202 | function wp_ajax_wpmc_recover_do() { |
| 203 | ob_start(); |
| 204 | $data = $_POST['data']; |
| 205 | $success = 0; |
| 206 | foreach ( $data as $piece ) { |
| 207 | $success += ( $this->core->recover( $piece ) ? 1 : 0 ); |
| 208 | } |
| 209 | ob_end_clean(); |
| 210 | echo json_encode( |
| 211 | array( |
| 212 | 'success' => true, |
| 213 | 'result' => array( 'data' => $data, 'success' => $success ), |
| 214 | 'message' => __( "Status unknown.", 'media-cleaner' ) |
| 215 | ) |
| 216 | ); |
| 217 | die(); |
| 218 | } |
| 219 | |
| 220 | function wp_ajax_wpmc_validate_option() { |
| 221 | $name = $_POST['name']; // Option Name |
| 222 | $value = $_POST['value']; // Option Value |
| 223 | $value = wp_unslash( $value ); // Unescape backslashes |
| 224 | $validated = $this->admin->validate_option( $name, $value ); |
| 225 | if ( $validated instanceof WP_Error ) { // Invalid value |
| 226 | $error = array ( |
| 227 | 'code' => $validated->get_error_code() ?: 'invalid_option', |
| 228 | 'message' => $validated->get_error_message() ?: __( "Invalid Option Value", 'media-cleaner' ) |
| 229 | ); |
| 230 | wp_send_json_error( $error ); |
| 231 | } |
| 232 | wp_send_json_success(); |
| 233 | } |
| 234 | } |