$check source, method, min_age, result; order_id (optional). * @return int The new row's id, 0 when the check was refused or not written. */ public static function record( array $check ) { global $wpdb; $row = self::clean( $check ); if ( null === $row ) { return 0; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- this plugin's own table; one row per check. $ok = $wpdb->insert( Schema::table( 'checks' ), $row, array( '%d', '%s', '%s', '%s', '%d', '%s', '%s', '%s' ) ); if ( ! $ok ) { return 0; } $id = (int) $wpdb->insert_id; if ( $row['order_id'] ) { self::stamp_order( (int) $row['order_id'], $row ); } return $id; } /** * A check as the table keeps it, or null when anything is outside its lists. * The order id is kept only when WooCommerce knows that order; a reference only on a * remembered customer's pass, and only the time of a check (B1). * * @param array $check Raw check: source, method, min_age, result; order_id, ref (optional). * @return array|null */ public static function clean( array $check ) { $source = isset( $check['source'] ) ? $check['source'] : ''; $method = isset( $check['method'] ) ? $check['method'] : ''; $result = isset( $check['result'] ) ? $check['result'] : ''; $age = isset( $check['min_age'] ) ? $check['min_age'] : 0; if ( ! in_array( $source, self::SOURCES, true ) || ! in_array( $method, self::METHODS, true ) || ! in_array( $result, self::RESULTS, true ) || ! Settings::is_age( $age ) ) { return null; } $order_id = isset( $check['order_id'] ) ? $check['order_id'] : 0; $order_id = Settings::int_between( $order_id, 1, PHP_INT_MAX ) && self::order_exists( (int) $order_id ) ? (int) $order_id : null; $ref = isset( $check['ref'] ) ? $check['ref'] : null; if ( 'remembered' === $source && ( 'pass' !== $result || ! Remember::is_time( $ref ) ) ) { return null; } return array( 'order_id' => $order_id, 'checked_at' => gmdate( 'Y-m-d H:i:s' ), 'source' => $source, 'method' => $method, 'min_age' => (int) $age, 'result' => $result, 'version' => AGEVOW_VERSION, 'ref' => 'remembered' === $source ? (string) $ref : null, ); } /** * Put the check on the order, through WooCommerce's own order API. * * @param int $order_id The order. * @param array $row The row record() wrote. * @return bool */ public static function stamp_order( $order_id, array $row ) { $order = function_exists( 'wc_get_order' ) ? wc_get_order( $order_id ) : false; if ( ! $order ) { return false; } $meta = array( 'method' => $row['method'], 'min_age' => (int) $row['min_age'], 'result' => $row['result'], 'time' => $row['checked_at'], 'version' => $row['version'], ); // A remembered customer's order: the time of the check it rests on (B1). if ( isset( $row['source'], $row['ref'] ) && 'remembered' === $row['source'] ) { $meta['remembered'] = (string) $row['ref']; } $order->update_meta_data( self::ORDER_META, $meta ); $order->save(); return true; } /** * The record an order carries, or null. * * @param int $order_id The order. * @return array|null */ public static function for_order( $order_id ) { $order = function_exists( 'wc_get_order' ) ? wc_get_order( (int) $order_id ) : false; if ( ! $order ) { return null; } $meta = $order->get_meta( self::ORDER_META, true ); return is_array( $meta ) ? $meta : null; } /** * Whether an order already carries a passed check at this minimum age or higher * (the pay-for-order page then asks nothing, and a second try at paying the same * order writes no second record). * * @param int $order_id The order. * @param int $age The minimum age asked now. * @return bool */ public static function passed( $order_id, $age ) { $rec = self::for_order( $order_id ); return null !== $rec && isset( $rec['result'], $rec['min_age'] ) && 'pass' === $rec['result'] && (int) $rec['min_age'] >= (int) $age; } /** * Whether WooCommerce knows an order of this id (any status, the trash too — * a check stays linked to the order it was made for). * * @param int $order_id The order. * @return bool */ public static function order_exists( $order_id ) { return function_exists( 'wc_get_order' ) && $order_id > 0 && false !== wc_get_order( $order_id ); } /** * A page of checks, newest first. * * @param array $args from, to (the site's days), result, method, source, order_id, page, per_page. * @return array{items: array>, total: int} */ public static function query( array $args ) { global $wpdb; $page = max( 1, isset( $args['page'] ) ? (int) $args['page'] : 1 ); $per_page = min( 100, max( 1, isset( $args['per_page'] ) ? (int) $args['per_page'] : 20 ) ); $result = isset( $args['result'] ) && in_array( $args['result'], self::RESULTS, true ) ? $args['result'] : ''; $method = isset( $args['method'] ) && in_array( $args['method'], self::METHODS, true ) ? $args['method'] : ''; $source = isset( $args['source'] ) && in_array( $args['source'], self::SOURCES, true ) ? $args['source'] : ''; $order_id = isset( $args['order_id'] ) && Settings::int_between( $args['order_id'], 1, PHP_INT_MAX ) ? (int) $args['order_id'] : 0; list( $from, $to ) = Dates::utc_range( isset( $args['from'] ) ? $args['from'] : '', isset( $args['to'] ) ? $args['to'] : '' ); $table = Schema::table( 'checks' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- this plugin's own table; a list that changes with every order. $total = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE checked_at >= %s AND checked_at <= %s AND ( %s = '' OR result = %s ) AND ( %s = '' OR method = %s ) AND ( %s = '' OR source = %s ) AND ( %d = 0 OR order_id = %d )", $table, $from, $to, $result, $result, $method, $method, $source, $source, $order_id, $order_id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- this plugin's own table; a list that changes with every order. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT id, order_id, checked_at, source, method, min_age, result, ref, version FROM %i WHERE checked_at >= %s AND checked_at <= %s AND ( %s = '' OR result = %s ) AND ( %s = '' OR method = %s ) AND ( %s = '' OR source = %s ) AND ( %d = 0 OR order_id = %d ) ORDER BY id DESC LIMIT %d OFFSET %d", $table, $from, $to, $result, $result, $method, $method, $source, $source, $order_id, $order_id, $per_page, ( $page - 1 ) * $per_page ), ARRAY_A ); return array( 'items' => array_map( array( self::class, 'decode' ), (array) $rows ), 'total' => $total, ); } /** * Every check of one order, oldest first (the site's personal-data export). * * @param int $order_id The order. * @return array> Checks::decode() rows. */ public static function of_order( $order_id ) { global $wpdb; if ( (int) $order_id < 1 ) { return array(); } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- this plugin's own table, read once for a personal-data export. $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT id, order_id, checked_at, source, method, min_age, result, ref, version FROM %i WHERE order_id = %d ORDER BY id ASC LIMIT 100', Schema::table( 'checks' ), (int) $order_id ), ARRAY_A ); return array_map( array( self::class, 'decode' ), (array) $rows ); } /** * Take an order's checks off the order (the site's personal-data eraser): the rows * stay as anonymous counts — where, how, at what age, when — with no order, and the * order loses its own record of the check. The note on the order is the shop's and * is not touched. * * @param int $order_id The order. * @return int How many rows and order records were removed. */ public static function unlink_order( $order_id ) { global $wpdb; $order_id = (int) $order_id; if ( $order_id < 1 ) { return 0; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- this plugin's own table; one statement for every check of the order. $n = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET order_id = NULL WHERE order_id = %d', Schema::table( 'checks' ), $order_id ) ); $n = is_int( $n ) ? $n : 0; $order = function_exists( 'wc_get_order' ) ? wc_get_order( $order_id ) : false; if ( $order && '' !== $order->get_meta( self::ORDER_META, true ) ) { $order->delete_meta_data( self::ORDER_META ); $order->save(); ++$n; } return $n; } /** * A stored row as the screens show it. * * @param array $row A row. * @return array */ public static function decode( array $row ) { return array( 'id' => (int) $row['id'], 'order_id' => null === $row['order_id'] ? null : (int) $row['order_id'], 'checked_at' => (string) $row['checked_at'], 'source' => (string) $row['source'], 'method' => (string) $row['method'], 'min_age' => (int) $row['min_age'], 'result' => (string) $row['result'], 'ref' => null === $row['ref'] ? null : (string) $row['ref'], 'version' => (string) $row['version'], ); } }