Changeset 3130894
- Timestamp:
- 08/05/2024 08:43:50 AM (20 months ago)
- Location:
- artist-image-generator/trunk
- Files:
-
- 6 edited
-
README.txt (modified) (3 diffs)
-
artist-image-generator.php (modified) (2 diffs)
-
includes/class-artist-image-generator-constant.php (modified) (1 diff)
-
public/class-artist-image-generator-credits-balance-manager.php (modified) (2 diffs)
-
public/class-artist-image-generator-public.php (modified) (6 diffs)
-
public/js/artist-image-generator-public.js (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
artist-image-generator/trunk/README.txt
r3130568 r3130894 5 5 Tested up to: 6.6 6 6 Requires PHP: 7.4 7 Stable tag: v1.1.8 7 Stable tag: v1.1.8.1 8 8 License: GPLv3 9 9 License URI: https://www.gnu.org/licenses/gpl-3.0.html … … 28 28 29 29 Setting up a credit balance is simple. You just need to add a WooCommerce product (e.g., credit packs: 10 - 50 - 150). This works without any other WooCommerce extensions. 30 31 https://www.youtube.com/watch?v=aFjRa8dpM0A32 30 33 31 When a user purchases a number of credits, they have a credit balance on their account to use the generators. When they run out of credits, a link to your product is suggested so they can purchase more. … … 258 256 259 257 == Changelog == 258 1.1.8.1 - 2024-08-04 259 - Improvment of limitation to treats parallels queries 260 - If credits are < balance, serve n images = maximum of balance 261 260 262 1.1.8 - 2024-08-04 261 263 - Credit Balance feature -
artist-image-generator/trunk/artist-image-generator.php
r3130506 r3130894 17 17 * Plugin URI: https://artist-image-generator.com/ 18 18 * Description: Illustrate posts with Ai images (DALL·E). Text-to-Image, variation, editing. Public image generator & Ai avatars by topics. 19 * Version: 1.1.8 19 * Version: 1.1.8.1 20 20 * Author: Pierre Viéville 21 21 * Author URI: https://www.pierrevieville.fr … … 36 36 * Rename this for your plugin and update it as you release new versions. 37 37 */ 38 define( 'ARTIST_IMAGE_GENERATOR_VERSION', '1.1.8 ' );38 define( 'ARTIST_IMAGE_GENERATOR_VERSION', '1.1.8.1' ); 39 39 40 40 /** -
artist-image-generator/trunk/includes/class-artist-image-generator-constant.php
r3130506 r3130894 61 61 const REFILL_WC_PRODUCT_META_KEY = 'credits'; 62 62 const REFILL_USER_META_KEY = '_aig_balance'; 63 const REFILL_USER_META_KEY_VERSION = '_aig_balance_version'; 63 64 const REFILL_WC_ORDER_RECHARGED = '_aig_credits_recharged'; 64 65 } -
artist-image-generator/trunk/public/class-artist-image-generator-credits-balance-manager.php
r3130506 r3130894 75 75 } 76 76 77 public static function update_user_credits($user_id, $credits) { 77 public static function update_user_credits($user_id, $credits) { 78 $current_version = get_user_meta($user_id, Constants::REFILL_USER_META_KEY_VERSION, true); 79 80 if ($current_version === '') { 81 // The version number doesn't exist yet for the user, so create it 82 $current_version = 0; 83 add_user_meta($user_id, Constants::REFILL_USER_META_KEY_VERSION, $current_version); 84 } 85 78 86 $user_credits = self::get_user_balance($user_id); 79 87 $new_balance = $user_credits + $credits; 80 81 update_user_meta($user_id, Constants::REFILL_USER_META_KEY, $new_balance, $user_credits); 82 88 $new_version = $current_version + 1; 89 90 $updated = update_user_meta($user_id, Constants::REFILL_USER_META_KEY, $new_balance, $user_credits); 91 $version_updated = update_user_meta($user_id, Constants::REFILL_USER_META_KEY_VERSION, $new_version, $current_version); 92 83 93 return self::get_user_balance($user_id); 84 94 } … … 86 96 public static function get_user_balance($user_id) { 87 97 $user_credits = get_user_meta($user_id, Constants::REFILL_USER_META_KEY, true); 88 if (!$user_credits ) {98 if (!$user_credits || $user_credits < 0) { 89 99 $user_credits = 0; 90 100 } 101 91 102 return $user_credits; 92 103 } -
artist-image-generator/trunk/public/class-artist-image-generator-public.php
r3130506 r3130894 73 73 } 74 74 75 private function get_credits_to_remove($post_data) 76 { 75 private function get_request_number($post_data) 76 { 77 // DALLE 3 model has a fixed number of requests of 1 (parrallel requests) 78 if ($post_data['model'] === Constants::DALL_E_MODEL_3) { 79 return 1; 80 } 81 82 // DALLE 2 model needs to get the number of requests from the post data 77 83 return (int)$post_data['n']; 78 84 } 79 85 80 private function check_and_update_user_limit( $post_data)86 private function check_and_update_user_limit(&$post_data) 81 87 { 82 88 // If Constants::REFILL_PRODUCT_ID is set, handle the limit with the refill system … … 84 90 $user_id = get_current_user_id(); 85 91 $current_balance = $this->credits_balance_manager::get_user_balance($user_id); 86 $n_credits = $this->get_credits_to_remove($post_data); 87 $balance = $current_balance - $n_credits; 88 89 if ($balance < 1) { 92 93 $requests = $this->get_request_number($post_data); 94 $balance = $current_balance - $requests; 95 96 if ($balance < 0) { 97 $requests = $current_balance; 98 $balance = $current_balance - $requests; 99 $post_data['n'] = $requests; 100 } 101 102 if ($balance < 0 || $requests <= 0) { 90 103 $refill_product_id = $this->options[Constants::REFILL_PRODUCT_ID]; 91 104 $product_url = get_permalink($refill_product_id); … … 94 107 wp_send_json(array( 95 108 'error' => array( 96 'type' => self::ERROR_TYPE_LIMIT_EXCEEDED, 109 'type' => self::ERROR_TYPE_LIMIT_EXCEEDED, 97 110 'message' => $error_message, 98 'product_url' => $product_url 111 'product_url' => $product_url, 112 'user_balance' => $current_balance 99 113 ) 100 114 )); … … 109 123 $user_ip = $_SERVER['REMOTE_ADDR']; 110 124 $user_identifier = $user_id ? 'artist_image_generator_user_' . $form_id . '_' . $user_id : 'artist_image_generator_ip_' . $form_id . '_' . $user_ip; 111 $ requests= get_transient($user_identifier);125 $current_balance = get_transient($user_identifier); 112 126 $duration = isset($post_data['user_limit_duration']) && $post_data['user_limit_duration'] > 0 ? (int)$post_data['user_limit_duration'] : 0; 113 127 $expiration = get_option('_transient_timeout_' . $user_identifier); 114 128 115 if ($ requests=== false || ($duration > 0 && time() > $expiration)) {116 $ requests= 0;117 set_transient($user_identifier, $ requests, $duration);129 if ($current_balance === false || ($duration > 0 && time() > $expiration)) { 130 $current_balance = 0; 131 set_transient($user_identifier, $current_balance, $duration); 118 132 } 119 133 120 $requests++; 121 122 if ((int)$post_data['user_limit'] < $requests) { 134 $requests = $this->get_request_number($post_data); 135 $balance = $current_balance + $requests; 136 137 if ((int)$post_data['user_limit'] < $balance) { 138 $requests = (int)$post_data['user_limit'] - $current_balance; 139 $balance = $current_balance + $requests; 140 $post_data['n'] = $requests; 141 } 142 143 if ((int)$post_data['user_limit'] < $balance || $requests <= 0) { 123 144 $duration_msg = $duration > 0 ? sprintf(__(' Please try again in %d seconds.', 'artist-image-generator'), $expiration - time()) : ''; 124 145 $error_message = esc_html__('You have reached the limit of requests.', 'artist-image-generator') . $duration_msg; 125 146 wp_send_json(array( 126 147 'error' => array( 127 'type' => self::ERROR_TYPE_LIMIT_EXCEEDED, 148 'type' => self::ERROR_TYPE_LIMIT_EXCEEDED, 128 149 'message' => $error_message 129 150 ) … … 132 153 } 133 154 134 set_transient($user_identifier, $requests, $duration); 135 } 136 } 137 } 155 set_transient($user_identifier, $balance, $duration); 156 } 157 } 158 159 // Ensure that $post_data['n'] is not less than 1 160 if ($post_data['n'] < 1) { 161 $post_data['n'] = 1; 162 } 163 } 164 138 165 139 166 public function generate_image() … … 167 194 $data = $dalle->prepare_data($images ?? [], $error ?? [], $post_data); 168 195 196 $n_credits = $this->get_request_number($post_data); 197 198 /*$dummyImages = []; 199 200 for ($i = 0; $i < $n_credits; $i++) { 201 $dummyImages[] = [ 202 'url' => 'https://artist-image-generator.com/wp-content/uploads/img-rck1GT0eGIYLu4oAXFEMqsPT.png' 203 ]; 204 } 205 206 $data = [ 207 'error' => [], 208 'images' => $dummyImages, 209 'model_input' => $post_data['model'], 210 'prompt_input' => 'Painting of a bird, including following criterias:', 211 'size_input' => '1024x1024', 212 'n_input' => $n_credits, 213 'quality_input' => '', 214 'style_input' => '' 215 ];*/ 216 169 217 if (!empty($this->options[Constants::REFILL_PRODUCT_ID]) && is_user_logged_in()) { 170 218 $user_id = get_current_user_id(); 171 $n_credits = $this->get_credits_to_remove($post_data);172 219 $newBalance = $this->credits_balance_manager::update_user_credits($user_id, -$n_credits); 220 $data['user_credits_used'] = $n_credits; 173 221 $data['user_balance'] = $newBalance; 174 222 } -
artist-image-generator/trunk/public/js/artist-image-generator-public.js
r3130506 r3130894 92 92 let requests = []; 93 93 if (data.model === 'dall-e-3' && data.n > 1) { 94 requests = Array.from({ length: data.n }, () => { 95 return fetch(ajaxurl, { 96 method: "POST", 97 body: new URLSearchParams(data), 98 headers: { 99 "Content-Type": "application/x-www-form-urlencoded", 100 "Cache-Control": "no-cache", 101 }, 102 }).then((response) => response.json()); 103 }); 94 requests = Array.from({ length: data.n }, () => data); 104 95 } else { 105 requests.push(fetch(ajaxurl, { 106 method: "POST", 107 body: new URLSearchParams(data), 108 headers: { 109 "Content-Type": "application/x-www-form-urlencoded", 110 "Cache-Control": "no-cache", 111 }, 112 }).then((response) => response.json())); 113 } 96 requests.push(data); 97 } 98 99 let responses = []; 100 for (let i = 0; i < requests.length; i++) { 101 const requestData = requests[i]; 102 const response = await fetch(ajaxurl, { 103 method: "POST", 104 body: new URLSearchParams(requestData), 105 headers: { 106 "Content-Type": "application/x-www-form-urlencoded", 107 "Cache-Control": "no-cache", 108 }, 109 }); 110 111 const json = await response.json(); 112 responses.push(json); 113 114 // Add a timeout between each request 115 if (i < requests.length - 1) { 116 await new Promise(resolve => setTimeout(resolve, 200)); 117 } 118 } 119 114 120 115 121 try { 116 const responses = await Promise.all(requests);117 122 // Merge all responses 118 123 const mergedResponse = responses.reduce((acc, response) => { … … 135 140 acc.images = acc.images.concat(response.images); 136 141 } 137 if (response.user_balance ) {138 acc.user_balances.push( response.user_balance);142 if (response.user_balance !== undefined) { 143 acc.user_balances.push(String(response.user_balance)); 139 144 } 140 145 return acc; … … 207 212 if (mergedResponse.errors && mergedResponse.errors.length > 0) { 208 213 const errorContainer = form.querySelector(".aig-errors"); 209 errorContainer.innerHTML = mergedResponse.errors.join('<br>'); 210 } 211 212 const userBalanceValue = form.querySelector('.aig-credits-balance-value'); 213 if (userBalanceValue) { 214 const uniqueErrors = [...new Set(mergedResponse.errors)]; 215 errorContainer.innerHTML = uniqueErrors.join('<br>'); 216 } 217 218 const userBalanceValueElements = document.querySelectorAll('.aig-credits-balance-value'); 219 if (userBalanceValueElements.length > 0) { 220 //console.log(mergedResponse.user_balances); 214 221 if (Array.isArray(mergedResponse.user_balances) && mergedResponse.user_balances.length > 0) { 215 222 const numericBalances = mergedResponse.user_balances.map(Number); 223 216 224 const minBalance = Math.min(...numericBalances); 217 userBalanceValue.innerHTML = minBalance; 225 userBalanceValueElements.forEach(element => { 226 element.innerHTML = minBalance; 227 }); 218 228 } else { 219 userBalanceValue.innerHTML = 0; 229 userBalanceValueElements.forEach(element => { 230 element.innerHTML = 0; 231 }); 220 232 } 221 233 }
Note: See TracChangeset
for help on using the changeset viewer.