prefix . 'aieo_waitlist'; } public static function legacy_table() { global $wpdb; return $wpdb->prefix . 'om_ppe_waitlist'; } private static function exists( $table ) { global $wpdb; return (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table ) ) ) === $table; } public static function register_exporter( $exporters ) { $exporters = is_array( $exporters ) ? $exporters : array(); $exporters['aieo-waitlist'] = array( 'exporter_friendly_name' => __( 'Rooster One back-in-stock requests', 'ai-eshop-optimizer' ), 'callback' => array( __CLASS__, 'export' ), ); return $exporters; } public static function register_eraser( $erasers ) { $erasers = is_array( $erasers ) ? $erasers : array(); $erasers['aieo-waitlist'] = array( 'eraser_friendly_name' => __( 'Rooster One back-in-stock requests', 'ai-eshop-optimizer' ), 'callback' => array( __CLASS__, 'erase' ), ); return $erasers; } public static function export( $email, $page = 1 ) { global $wpdb; $email = sanitize_email( (string) $email ); if ( '' === $email || ! self::exists( self::table() ) ) { return array( 'data' => array(), 'done' => true ); } $per = 100; $page = max( 1, (int) $page ); $rows = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE email = %s ORDER BY id ASC LIMIT %d OFFSET %d', self::table(), $email, $per, ( $page - 1 ) * $per ) ); $states = array( 'subscribed' => __( 'Subscribed', 'ai-eshop-optimizer' ), 'notified' => __( 'Notified', 'ai-eshop-optimizer' ), 'suppressed' => __( 'Suppressed', 'ai-eshop-optimizer' ), ); $data = array(); foreach ( $rows as $r ) { $title = (string) get_the_title( (int) $r->product_id ); $fields = array( __( 'Product', 'ai-eshop-optimizer' ) => '' !== $title ? $title : '#' . (int) $r->product_id, __( 'Variation', 'ai-eshop-optimizer' ) => (int) $r->variation_id > 0 ? (string) get_the_title( (int) $r->variation_id ) : '', __( 'Email', 'ai-eshop-optimizer' ) => (string) $r->email, __( 'Date', 'ai-eshop-optimizer' ) => (string) $r->created_at . ' UTC', __( 'Status', 'ai-eshop-optimizer' ) => isset( $states[ (string) $r->status ] ) ? $states[ (string) $r->status ] : (string) $r->status, __( 'Notified', 'ai-eshop-optimizer' ) => null !== $r->notified_at ? (string) $r->notified_at . ' UTC' : '', ); $items = array(); foreach ( $fields as $label => $value ) { if ( '' !== trim( $value ) ) { $items[] = array( 'name' => $label, 'value' => $value ); } } $data[] = array( 'group_id' => 'aieo-waitlist', 'group_label' => __( 'Back-in-stock requests', 'ai-eshop-optimizer' ), 'item_id' => 'aieo-waitlist-' . (int) $r->id, 'data' => $items, ); } return array( 'data' => $data, 'done' => count( $rows ) < $per ); } public static function erase( $email, $page = 1 ) { global $wpdb; $email = sanitize_email( (string) $email ); $none = array( 'items_removed' => false, 'items_retained' => false, 'messages' => array(), 'done' => true ); if ( '' === $email ) { return $none; } $per = 100; $n = 0; $removed = 0; $rows = array(); if ( self::exists( self::table() ) ) { $rows = (array) $wpdb->get_col( $wpdb->prepare( 'SELECT id FROM %i WHERE email = %s ORDER BY id ASC LIMIT %d', self::table(), $email, $per ) ); foreach ( $rows as $id ) { $ok = $wpdb->query( $wpdb->prepare( "UPDATE %i SET email = %s, status = IF(status = 'subscribed', 'suppressed', status) WHERE id = %d", self::table(), 'erased-' . (int) $id, (int) $id ) ); if ( false !== $ok ) { $n++; } } } if ( 1 === (int) $page && self::exists( self::legacy_table() ) ) { $removed = (int) $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE email = %s', self::legacy_table(), $email ) ); } return array( 'items_removed' => $n > 0 || $removed > 0, 'items_retained' => $n > 0, 'messages' => $n > 0 ? array( __( 'Back-in-stock requests were anonymised: the products, the dates and the states are kept for the shop\'s waitlist counts, and no alert will be sent.', 'ai-eshop-optimizer' ) ) : array(), 'done' => count( $rows ) < $per, ); } } }