class-adbc-automation-endpoints.php
3 months ago
class-adbc-common-endpoints.php
4 months ago
class-adbc-cron-jobs-endpoints.php
3 months ago
class-adbc-general-cleanup-endpoints.php
3 months ago
class-adbc-info-endpoints.php
3 months ago
class-adbc-logs-endpoints.php
3 months ago
class-adbc-options-endpoints.php
3 months ago
class-adbc-post-types-endpoints.php
3 months ago
class-adbc-posts-meta-endpoints.php
3 months ago
class-adbc-settings-endpoints.php
3 months ago
class-adbc-tables-endpoints.php
3 months ago
class-adbc-transients-endpoints.php
3 months ago
class-adbc-users-meta-endpoints.php
3 months ago
class-adbc-common-endpoints.php
221 lines
| 1 | <?php |
| 2 | |
| 3 | // Exit if accessed directly |
| 4 | if ( ! defined( 'ABSPATH' ) ) |
| 5 | exit; |
| 6 | |
| 7 | /** |
| 8 | * ADBC Common Endpoints. |
| 9 | * |
| 10 | * This class provides the endpoints (controllers) for the common routes. |
| 11 | */ |
| 12 | class ADBC_Common_Endpoints { |
| 13 | |
| 14 | private const MAX_VALUE_TO_DEEP_DECODE = 100 * 1024 * 1024; // 100 MB |
| 15 | |
| 16 | /** |
| 17 | * Get the value of a column from a table. |
| 18 | * |
| 19 | * @return WP_REST_Response The value of the column with its type and info. |
| 20 | */ |
| 21 | public static function get_column_value_from_table( WP_REST_Request $request_data ) { |
| 22 | |
| 23 | try { |
| 24 | |
| 25 | // Get params |
| 26 | $items_type = $request_data->get_param( 'itemsType' ); |
| 27 | $site_id = $request_data->get_param( 'siteId' ); |
| 28 | $row_id = $request_data->get_param( 'rowId' ); |
| 29 | $transient_found_in = $request_data->get_param( 'transientFoundIn' ); |
| 30 | |
| 31 | $answer = ADBC_Common_Validator::validate_get_column_value_endpoint_data( $items_type, $site_id, $row_id, $transient_found_in ); |
| 32 | |
| 33 | if ( $answer['success'] === false ) |
| 34 | return ADBC_Rest::error( $answer['message'], ADBC_Rest::BAD_REQUEST ); |
| 35 | |
| 36 | $table = '`' . esc_sql( $answer['data']['table_name'] ) . '`'; |
| 37 | $col = '`' . esc_sql( $answer['data']['column_name'] ) . '`'; |
| 38 | $pk = '`' . esc_sql( $answer['data']['column_id'] ) . '`'; |
| 39 | |
| 40 | global $wpdb; |
| 41 | $sql = $wpdb->prepare( |
| 42 | "SELECT {$col} AS `value` |
| 43 | FROM {$table} |
| 44 | WHERE {$pk} = %d |
| 45 | LIMIT 1 |
| 46 | ", |
| 47 | $row_id |
| 48 | ); |
| 49 | $value = $wpdb->get_var( $sql ); |
| 50 | $value_type = ADBC_Common_Utils::get_value_type( $value ); |
| 51 | $pretty_json = ""; |
| 52 | |
| 53 | // Prepare a pretty JSON representation of the value if it is not too big. |
| 54 | $too_big = ( $value !== null && strlen( $value ) > self::MAX_VALUE_TO_DEEP_DECODE ); |
| 55 | |
| 56 | if ( ! $too_big ) { |
| 57 | |
| 58 | // We take into account only serialized_array for unserialize(). We de not unserialize serialized_object for security reasons. |
| 59 | if ( $value_type === 'serialized_data' ) { |
| 60 | $decoded = ADBC_Common_Utils::safe_unserialize_array( $value ); |
| 61 | } elseif ( $value_type === 'json_array' || $value_type === 'json_object' ) { |
| 62 | $decoded = json_decode( $value, true ); |
| 63 | } |
| 64 | |
| 65 | if ( isset( $decoded ) && $decoded !== false ) { |
| 66 | $decoded = ADBC_Common_Utils::deep_decode( $decoded ); // Recursively decode the structure to handle nested arrays/objects. |
| 67 | $pretty_json = json_encode( |
| 68 | $decoded, |
| 69 | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE |
| 70 | ); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return ADBC_Rest::success( "", [ "value" => $value, "type" => $value_type, "pretty_json" => $pretty_json ] ); |
| 75 | |
| 76 | } catch (Exception $e) { |
| 77 | |
| 78 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 79 | |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Dismiss a notification. |
| 85 | * |
| 86 | * @return WP_REST_Response The response indicating success or failure. |
| 87 | */ |
| 88 | public static function dismiss_notification( WP_REST_Request $request_data ) { |
| 89 | |
| 90 | try { |
| 91 | |
| 92 | // Get params |
| 93 | $notification_key = sanitize_key( $request_data->get_param( 'notificationKey' ) ); |
| 94 | |
| 95 | // Check if the notification is already dismissed |
| 96 | if ( ADBC_Notifications::instance()->is_notification_dismissed( $notification_key ) ) { |
| 97 | return ADBC_Rest::success( "" ); |
| 98 | } |
| 99 | |
| 100 | // Dismiss the notification |
| 101 | if ( ! ADBC_Notifications::instance()->dismiss_notification( $notification_key ) ) { |
| 102 | return ADBC_Rest::error( __( 'Failed to dismiss the notification.', 'advanced-database-cleaner' ), ADBC_Rest::BAD_REQUEST ); |
| 103 | } |
| 104 | |
| 105 | // Return success response |
| 106 | return ADBC_Rest::success( "" ); |
| 107 | |
| 108 | } catch (Exception $e) { |
| 109 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 110 | } |
| 111 | |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Delay the rating notice. |
| 116 | * |
| 117 | * @return WP_REST_Response The response indicating success or failure. |
| 118 | */ |
| 119 | public static function delay_rating_notice( WP_REST_Request $request_data ) { |
| 120 | |
| 121 | try { |
| 122 | |
| 123 | // get params |
| 124 | $notification_key = sanitize_key( $request_data->get_param( 'notificationKey' ) ); |
| 125 | |
| 126 | // Check if the notification key is valid |
| 127 | if ( $notification_key !== 'rating_notice' ) { |
| 128 | return ADBC_Rest::error( 'Invalid notification key.', ADBC_Rest::BAD_REQUEST ); |
| 129 | } |
| 130 | |
| 131 | // Delay the rating notice |
| 132 | if ( ! ADBC_Notifications::instance()->delay_rating_notice() ) { |
| 133 | return ADBC_Rest::error( 'Failed to delay the rating notice.', ADBC_Rest::BAD_REQUEST ); |
| 134 | } |
| 135 | |
| 136 | // Return success response |
| 137 | return ADBC_Rest::success( "" ); |
| 138 | } catch (Exception $e) { |
| 139 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 140 | } |
| 141 | |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Delay the LTD migration notice by 7 days (remind me in a week). |
| 146 | * |
| 147 | * @return WP_REST_Response The response indicating success or failure. |
| 148 | */ |
| 149 | public static function delay_ltd_migration_notice( WP_REST_Request $request_data ) { |
| 150 | |
| 151 | try { |
| 152 | |
| 153 | // get params |
| 154 | $notification_key = sanitize_key( $request_data->get_param( 'notificationKey' ) ); |
| 155 | |
| 156 | // Check if the notification key is valid |
| 157 | if ( $notification_key !== 'ltd_migration_notice' ) { |
| 158 | return ADBC_Rest::error( 'Invalid notification key.', ADBC_Rest::BAD_REQUEST ); |
| 159 | } |
| 160 | |
| 161 | // Delay the LTD migration notice |
| 162 | if ( ! ADBC_Notifications::instance()->delay_ltd_migration_notice() ) { |
| 163 | return ADBC_Rest::error( 'Failed to delay the LTD migration notice.', ADBC_Rest::BAD_REQUEST ); |
| 164 | } |
| 165 | |
| 166 | // Return success response |
| 167 | return ADBC_Rest::success( '' ); |
| 168 | } catch (Exception $e) { |
| 169 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 170 | } |
| 171 | |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Get all schedule frequencies. |
| 176 | * |
| 177 | * @return WP_REST_Response The response. |
| 178 | */ |
| 179 | public static function get_all_schedule_frequencies() { |
| 180 | |
| 181 | try { |
| 182 | |
| 183 | return ADBC_Rest::success( "", wp_get_schedules() ); |
| 184 | |
| 185 | } catch (Exception $e) { |
| 186 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 187 | } |
| 188 | |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Get last week database size for free version. |
| 193 | * It only returns the current database size for each day of the last week. |
| 194 | * |
| 195 | * @return WP_REST_Response The response with the database size for the last week. |
| 196 | */ |
| 197 | public static function get_last_week_database_size_for_free_version() { |
| 198 | |
| 199 | try { |
| 200 | |
| 201 | // Get the current database size |
| 202 | $current_database_size = ADBC_Database::get_database_size_sql( false ); |
| 203 | |
| 204 | // Create an array with all days of the last week initialized to 0 |
| 205 | $result = []; |
| 206 | for ( $i = 6; $i >= 0; $i-- ) { |
| 207 | $date = date( 'Y-m-d', strtotime( "-$i days" ) ); |
| 208 | $result[ $date ] = $current_database_size; |
| 209 | } |
| 210 | |
| 211 | return ADBC_Rest::success( '', $result ); |
| 212 | |
| 213 | } catch (Throwable $e) { |
| 214 | |
| 215 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 216 | |
| 217 | } |
| 218 | |
| 219 | } |
| 220 | |
| 221 | } |