abstract-wc-order-data-store-cpt.php
6 years ago
abstract-wc-order-item-type-data-store.php
6 years ago
class-wc-coupon-data-store-cpt.php
6 years ago
class-wc-customer-data-store-session.php
7 years ago
class-wc-customer-data-store.php
6 years ago
class-wc-customer-download-data-store.php
6 years ago
class-wc-customer-download-log-data-store.php
6 years ago
class-wc-data-store-wp.php
6 years ago
class-wc-order-data-store-cpt.php
6 years ago
class-wc-order-item-coupon-data-store.php
8 years ago
class-wc-order-item-data-store.php
6 years ago
class-wc-order-item-fee-data-store.php
8 years ago
class-wc-order-item-product-data-store.php
8 years ago
class-wc-order-item-shipping-data-store.php
8 years ago
class-wc-order-item-tax-data-store.php
6 years ago
class-wc-order-refund-data-store-cpt.php
6 years ago
class-wc-payment-token-data-store.php
6 years ago
class-wc-product-data-store-cpt.php
6 years ago
class-wc-product-grouped-data-store-cpt.php
7 years ago
class-wc-product-variable-data-store-cpt.php
6 years ago
class-wc-product-variation-data-store-cpt.php
6 years ago
class-wc-shipping-zone-data-store.php
6 years ago
class-wc-webhook-data-store.php
6 years ago
class-wc-customer-download-log-data-store.php
240 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class WC_Customer_Download_Log_Data_Store file. |
| 4 | * |
| 5 | * @version 3.3.0 |
| 6 | * @package WooCommerce\Classes |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * WC_Customer_Download_Log_Data_Store class. |
| 13 | */ |
| 14 | class WC_Customer_Download_Log_Data_Store implements WC_Customer_Download_Log_Data_Store_Interface { |
| 15 | |
| 16 | // Table name for download logs. |
| 17 | const WC_DOWNLOAD_LOG_TABLE = 'wc_download_log'; |
| 18 | |
| 19 | /** |
| 20 | * Get the table name for download logs. |
| 21 | * |
| 22 | * @return string |
| 23 | */ |
| 24 | public static function get_table_name() { |
| 25 | return self::WC_DOWNLOAD_LOG_TABLE; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Create download log entry. |
| 30 | * |
| 31 | * @param WC_Customer_Download_Log $download_log Customer download log object. |
| 32 | */ |
| 33 | public function create( WC_Customer_Download_Log &$download_log ) { |
| 34 | global $wpdb; |
| 35 | |
| 36 | // Always set a timestamp. |
| 37 | if ( is_null( $download_log->get_timestamp( 'edit' ) ) ) { |
| 38 | $download_log->set_timestamp( time() ); |
| 39 | } |
| 40 | |
| 41 | $data = array( |
| 42 | 'timestamp' => date( 'Y-m-d H:i:s', $download_log->get_timestamp( 'edit' )->getTimestamp() ), |
| 43 | 'permission_id' => $download_log->get_permission_id( 'edit' ), |
| 44 | 'user_id' => $download_log->get_user_id( 'edit' ), |
| 45 | 'user_ip_address' => $download_log->get_user_ip_address( 'edit' ), |
| 46 | ); |
| 47 | |
| 48 | $format = array( |
| 49 | '%s', |
| 50 | '%s', |
| 51 | '%s', |
| 52 | '%s', |
| 53 | ); |
| 54 | |
| 55 | $result = $wpdb->insert( |
| 56 | $wpdb->prefix . self::get_table_name(), |
| 57 | apply_filters( 'woocommerce_downloadable_product_download_log_insert_data', $data ), |
| 58 | apply_filters( 'woocommerce_downloadable_product_download_log_insert_format', $format, $data ) |
| 59 | ); |
| 60 | |
| 61 | do_action( 'woocommerce_downloadable_product_download_log_insert', $data ); |
| 62 | |
| 63 | if ( $result ) { |
| 64 | $download_log->set_id( $wpdb->insert_id ); |
| 65 | $download_log->apply_changes(); |
| 66 | } else { |
| 67 | wp_die( esc_html__( 'Unable to insert download log entry in database.', 'woocommerce' ) ); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Method to read a download log from the database. |
| 73 | * |
| 74 | * @param WC_Customer_Download_Log $download_log Download log object. |
| 75 | * @throws Exception Exception when read is not possible. |
| 76 | */ |
| 77 | public function read( &$download_log ) { |
| 78 | global $wpdb; |
| 79 | |
| 80 | $download_log->set_defaults(); |
| 81 | |
| 82 | // Ensure we have an id to pull from the DB. |
| 83 | if ( ! $download_log->get_id() ) { |
| 84 | throw new Exception( __( 'Invalid download log: no ID.', 'woocommerce' ) ); |
| 85 | } |
| 86 | |
| 87 | $table = $wpdb->prefix . self::get_table_name(); |
| 88 | |
| 89 | // Query the DB for the download log. |
| 90 | $raw_download_log = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE download_log_id = %d", $download_log->get_id() ) ); // WPCS: unprepared SQL ok. |
| 91 | |
| 92 | if ( ! $raw_download_log ) { |
| 93 | throw new Exception( __( 'Invalid download log: not found.', 'woocommerce' ) ); |
| 94 | } |
| 95 | |
| 96 | $download_log->set_props( |
| 97 | array( |
| 98 | 'timestamp' => strtotime( $raw_download_log->timestamp ), |
| 99 | 'permission_id' => $raw_download_log->permission_id, |
| 100 | 'user_id' => $raw_download_log->user_id, |
| 101 | 'user_ip_address' => $raw_download_log->user_ip_address, |
| 102 | ) |
| 103 | ); |
| 104 | |
| 105 | $download_log->set_object_read( true ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Method to update a download log in the database. |
| 110 | * |
| 111 | * @param WC_Customer_Download_Log $download_log Download log object. |
| 112 | */ |
| 113 | public function update( &$download_log ) { |
| 114 | global $wpdb; |
| 115 | |
| 116 | $data = array( |
| 117 | 'timestamp' => date( 'Y-m-d H:i:s', $download_log->get_timestamp( 'edit' )->getTimestamp() ), |
| 118 | 'permission_id' => $download_log->get_permission_id( 'edit' ), |
| 119 | 'user_id' => $download_log->get_user_id( 'edit' ), |
| 120 | 'user_ip_address' => $download_log->get_user_ip_address( 'edit' ), |
| 121 | ); |
| 122 | |
| 123 | $format = array( |
| 124 | '%s', |
| 125 | '%s', |
| 126 | '%s', |
| 127 | '%s', |
| 128 | ); |
| 129 | |
| 130 | $wpdb->update( |
| 131 | $wpdb->prefix . self::get_table_name(), |
| 132 | $data, |
| 133 | array( |
| 134 | 'download_log_id' => $download_log->get_id(), |
| 135 | ), |
| 136 | $format |
| 137 | ); |
| 138 | $download_log->apply_changes(); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Get a download log object. |
| 143 | * |
| 144 | * @param array $data From the DB. |
| 145 | * @return WC_Customer_Download_Log |
| 146 | */ |
| 147 | private function get_download_log( $data ) { |
| 148 | return new WC_Customer_Download_Log( $data ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Get array of download log ids by specified args. |
| 153 | * |
| 154 | * @param array $args Arguments to define download logs to retrieve. |
| 155 | * @return array |
| 156 | */ |
| 157 | public function get_download_logs( $args = array() ) { |
| 158 | global $wpdb; |
| 159 | |
| 160 | $args = wp_parse_args( |
| 161 | $args, |
| 162 | array( |
| 163 | 'permission_id' => '', |
| 164 | 'user_id' => '', |
| 165 | 'user_ip_address' => '', |
| 166 | 'orderby' => 'download_log_id', |
| 167 | 'order' => 'ASC', |
| 168 | 'limit' => -1, |
| 169 | 'page' => 1, |
| 170 | 'return' => 'objects', |
| 171 | ) |
| 172 | ); |
| 173 | |
| 174 | $query = array(); |
| 175 | $table = $wpdb->prefix . self::get_table_name(); |
| 176 | $query[] = "SELECT * FROM {$table} WHERE 1=1"; |
| 177 | |
| 178 | if ( $args['permission_id'] ) { |
| 179 | $query[] = $wpdb->prepare( 'AND permission_id = %d', $args['permission_id'] ); |
| 180 | } |
| 181 | |
| 182 | if ( $args['user_id'] ) { |
| 183 | $query[] = $wpdb->prepare( 'AND user_id = %d', $args['user_id'] ); |
| 184 | } |
| 185 | |
| 186 | if ( $args['user_ip_address'] ) { |
| 187 | $query[] = $wpdb->prepare( 'AND user_ip_address = %s', $args['user_ip_address'] ); |
| 188 | } |
| 189 | |
| 190 | $allowed_orders = array( 'download_log_id', 'timestamp', 'permission_id', 'user_id' ); |
| 191 | $orderby = in_array( $args['orderby'], $allowed_orders, true ) ? $args['orderby'] : 'download_log_id'; |
| 192 | $order = 'DESC' === strtoupper( $args['order'] ) ? 'DESC' : 'ASC'; |
| 193 | $orderby_sql = sanitize_sql_orderby( "{$orderby} {$order}" ); |
| 194 | $query[] = "ORDER BY {$orderby_sql}"; |
| 195 | |
| 196 | if ( 0 < $args['limit'] ) { |
| 197 | $query[] = $wpdb->prepare( 'LIMIT %d, %d', absint( $args['limit'] ) * absint( $args['page'] - 1 ), absint( $args['limit'] ) ); |
| 198 | } |
| 199 | |
| 200 | $raw_download_logs = $wpdb->get_results( implode( ' ', $query ) ); // WPCS: unprepared SQL ok. |
| 201 | |
| 202 | switch ( $args['return'] ) { |
| 203 | case 'ids': |
| 204 | return wp_list_pluck( $raw_download_logs, 'download_log_id' ); |
| 205 | default: |
| 206 | return array_map( array( $this, 'get_download_log' ), $raw_download_logs ); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Get download logs for a given download permission. |
| 212 | * |
| 213 | * @param int $permission_id Permission to get logs for. |
| 214 | * @return array |
| 215 | */ |
| 216 | public function get_download_logs_for_permission( $permission_id ) { |
| 217 | // If no permission_id is passed, return an empty array. |
| 218 | if ( empty( $permission_id ) ) { |
| 219 | return array(); |
| 220 | } |
| 221 | |
| 222 | return $this->get_download_logs( |
| 223 | array( |
| 224 | 'permission_id' => $permission_id, |
| 225 | ) |
| 226 | ); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Method to delete download logs for a given permission ID. |
| 231 | * |
| 232 | * @since 3.4.0 |
| 233 | * @param int $id download_id of the downloads that will be deleted. |
| 234 | */ |
| 235 | public function delete_by_permission_id( $id ) { |
| 236 | global $wpdb; |
| 237 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE permission_id = %d", $id ) ); |
| 238 | } |
| 239 | } |
| 240 |