Changeset 3423116
- Timestamp:
- 12/18/2025 04:52:43 PM (3 months ago)
- Location:
- alt-text-imagerr-ai
- Files:
-
- 8 edited
- 1 copied
-
tags/1.4.2 (copied) (copied from alt-text-imagerr-ai/trunk)
-
tags/1.4.2/assets/imagerr-attachment.js (modified) (1 diff)
-
tags/1.4.2/imagerr.php (modified) (3 diffs)
-
tags/1.4.2/readme.txt (modified) (2 diffs)
-
tags/1.4.2/src/Meta.php (modified) (5 diffs)
-
trunk/assets/imagerr-attachment.js (modified) (1 diff)
-
trunk/imagerr.php (modified) (3 diffs)
-
trunk/readme.txt (modified) (2 diffs)
-
trunk/src/Meta.php (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
alt-text-imagerr-ai/tags/1.4.2/assets/imagerr-attachment.js
r3371087 r3423116 70 70 var attachment_wrapper = $this.closest('.attachment-info'); 71 71 $('[data-setting="alt"] textarea', attachment_wrapper).val(response.alt_text); 72 $('[data-setting="title"] input[type="text"]', attachment_wrapper).val(response.title); 73 $('[data-setting="caption"] textarea', attachment_wrapper).val(response.caption); 74 $('[data-setting="description"] textarea', attachment_wrapper).val(response.description); 72 if (response.title !== undefined) { 73 $('[data-setting="title"] input[type="text"]', attachment_wrapper).val(response.title); 74 } 75 if (response.caption !== undefined) { 76 $('[data-setting="caption"] textarea', attachment_wrapper).val(response.caption); 77 } 78 if (response.description !== undefined) { 79 $('[data-setting="description"] textarea', attachment_wrapper).val(response.description); 80 } 75 81 } 76 82 -
alt-text-imagerr-ai/tags/1.4.2/imagerr.php
r3409077 r3423116 3 3 * Plugin Name: AI Image Alt Text Generator – Imagerr AI 4 4 * Description: Generate alt text, titles, descriptions, and captions for your images automatically with AI. 5 * Version: 1.4. 15 * Version: 1.4.2 6 6 * Text Domain: alt-text-imagerr-ai 7 7 * Domain Path: /languages … … 667 667 array( 668 668 'type' => 'string', 669 'sanitize_callback' => function ( $value ) { 670 return sanitize_text_field( $value ); 671 }, 669 'sanitize_callback' => array( $this, 'sanitize_language_setting' ), 672 670 'default' => get_locale(), 673 671 ) … … 707 705 public function sanitize_checkbox( $value ) { 708 706 return ( isset( $value ) && true === (bool) $value ) ? true : false; 707 } 708 709 /** 710 * Sanitize language setting to prevent injection attacks 711 * Only allow valid WordPress locale codes from available translations 712 * 713 * @param string $value The language value to sanitize. 714 * @return string The sanitized language code. 715 */ 716 public function sanitize_language_setting( $value ) { 717 // First sanitize the text field to remove any dangerous characters 718 $value = sanitize_text_field( $value ); 719 720 // If empty, default to site locale 721 if ( empty( $value ) ) { 722 return get_locale(); 723 } 724 725 // Get available WordPress translations 726 require_once ABSPATH . 'wp-admin/includes/translation-install.php'; 727 $languages = wp_get_available_translations(); 728 729 // Build whitelist of valid language codes 730 // Add English (US) as it's not always included in wp_get_available_translations() 731 $valid_languages = array( 'en_US' ); 732 foreach ( $languages as $lang ) { 733 if ( isset( $lang['language'] ) ) { 734 $valid_languages[] = $lang['language']; 735 } 736 } 737 738 // Only return the value if it's in the whitelist, otherwise default to site locale 739 if ( ! in_array( $value, $valid_languages, true ) ) { 740 return get_locale(); 741 } 742 743 return $value; 709 744 } 710 745 -
alt-text-imagerr-ai/tags/1.4.2/readme.txt
r3409077 r3423116 5 5 Requires PHP: 5.2 6 6 Requires at least: 4.6 7 Stable tag: 1.4. 17 Stable tag: 1.4.2 8 8 Tested up to: 6.9 9 9 License: GPLv2 or later … … 71 71 72 72 == Changelog == 73 = 1.4.2 = 74 * Fixed issue with image file paths 75 * Fixed issue when image description was overriding the description with a blank description 76 73 77 = 1.4.1 = 74 78 * Changed URLs to new faster API server -
alt-text-imagerr-ai/tags/1.4.2/src/Meta.php
r3409077 r3423116 56 56 $this->log( "> $image_id" ); 57 57 58 $ai_fields = $this->api_generate_meta( $image_ url );58 $ai_fields = $this->api_generate_meta( $image_id, $image_url ); 59 59 60 60 if ( is_wp_error( $ai_fields ) ) { … … 272 272 * API generate meta. 273 273 * 274 * @param string $image_url The image URL. 274 * @param int $image_id The image ID. 275 * @param string $image_url The image URL (used for IMAGERR_UPLOADS_URL replacement and logging). 275 276 * @return array|\WP_Error The AI fields or a WP_Error object. 276 277 */ 277 private function api_generate_meta( $image_url ) { 278 private function api_generate_meta( $image_id, $image_url ) { 279 // Get the actual file path using WordPress's built-in function 280 // This works reliably regardless of CDN, custom upload directories, or multisite setups 281 $image_path = get_attached_file( $image_id ); 282 283 // Check if file exists locally 284 if ( ! $image_path || ! file_exists( $image_path ) ) { 285 return new \WP_Error( 'imagerr_error', 'Image file not found for attachment ID: ' . $image_id ); 286 } 287 288 // If IMAGERR_UPLOADS_URL is defined, we need to adjust the URL for external access 278 289 if ( defined( 'IMAGERR_UPLOADS_URL' ) ) { 279 290 $wp_upload_dir = wp_upload_dir(); 280 291 $base_url = $wp_upload_dir['baseurl']; 281 292 $image_url = str_replace( $base_url, rtrim( IMAGERR_UPLOADS_URL, '/' ), $image_url ); 282 }283 284 // Get the image file path from URL285 $upload_dir = wp_upload_dir();286 $image_path = str_replace( $upload_dir['baseurl'], $upload_dir['basedir'], $image_url );287 288 // Check if file exists locally289 if ( ! file_exists( $image_path ) ) {290 return new \WP_Error( 'imagerr_error', 'Image file not found: ' . $image_path );291 293 } 292 294 … … 303 305 304 306 $site_url = get_site_url(); 305 $language = get_option( 'imagerr_generation_language', get_locale() );307 $language = $this->validate_language( get_option( 'imagerr_generation_language', get_locale() ) ); 306 308 307 309 // Prepare multipart form data … … 442 444 $credits = json_decode( wp_remote_retrieve_body( $response ), true ); 443 445 444 445 if ( isset( $credits['data']['credits'] ) && $credits['data']['credits'] > 0 ) { 446 $credits = $credits['data']['credits'] ?? $credits['credits'] ?? 0; 447 448 if ( $credits > 0 ) { 446 449 $dismissed_notice = get_option( 'imagerr_dismissed_notice' ); 447 450 if ( ! $dismissed_notice ) { … … 450 453 } 451 454 452 return $credits['data']['credits'] ?? 0; 455 return $credits; 456 } 457 458 /** 459 * Validate language code to prevent injection attacks 460 * Only allow valid WordPress locale codes from available translations 461 * 462 * @param string $language The language code to validate. 463 * @return string The validated language code. 464 */ 465 private function validate_language( $language ) { 466 // If empty, default to site locale 467 if ( empty( $language ) ) { 468 return get_locale(); 469 } 470 471 // Get available WordPress translations 472 require_once ABSPATH . 'wp-admin/includes/translation-install.php'; 473 $languages = wp_get_available_translations(); 474 475 // Build whitelist of valid language codes 476 // Add English (US) as it's not always included in wp_get_available_translations() 477 $valid_languages = array( 'en_US' ); 478 foreach ( $languages as $lang ) { 479 if ( isset( $lang['language'] ) ) { 480 $valid_languages[] = $lang['language']; 481 } 482 } 483 484 // Only return the value if it's in the whitelist, otherwise default to site locale 485 if ( ! in_array( $language, $valid_languages, true ) ) { 486 return get_locale(); 487 } 488 489 return $language; 453 490 } 454 491 -
alt-text-imagerr-ai/trunk/assets/imagerr-attachment.js
r3371087 r3423116 70 70 var attachment_wrapper = $this.closest('.attachment-info'); 71 71 $('[data-setting="alt"] textarea', attachment_wrapper).val(response.alt_text); 72 $('[data-setting="title"] input[type="text"]', attachment_wrapper).val(response.title); 73 $('[data-setting="caption"] textarea', attachment_wrapper).val(response.caption); 74 $('[data-setting="description"] textarea', attachment_wrapper).val(response.description); 72 if (response.title !== undefined) { 73 $('[data-setting="title"] input[type="text"]', attachment_wrapper).val(response.title); 74 } 75 if (response.caption !== undefined) { 76 $('[data-setting="caption"] textarea', attachment_wrapper).val(response.caption); 77 } 78 if (response.description !== undefined) { 79 $('[data-setting="description"] textarea', attachment_wrapper).val(response.description); 80 } 75 81 } 76 82 -
alt-text-imagerr-ai/trunk/imagerr.php
r3409077 r3423116 3 3 * Plugin Name: AI Image Alt Text Generator – Imagerr AI 4 4 * Description: Generate alt text, titles, descriptions, and captions for your images automatically with AI. 5 * Version: 1.4. 15 * Version: 1.4.2 6 6 * Text Domain: alt-text-imagerr-ai 7 7 * Domain Path: /languages … … 667 667 array( 668 668 'type' => 'string', 669 'sanitize_callback' => function ( $value ) { 670 return sanitize_text_field( $value ); 671 }, 669 'sanitize_callback' => array( $this, 'sanitize_language_setting' ), 672 670 'default' => get_locale(), 673 671 ) … … 707 705 public function sanitize_checkbox( $value ) { 708 706 return ( isset( $value ) && true === (bool) $value ) ? true : false; 707 } 708 709 /** 710 * Sanitize language setting to prevent injection attacks 711 * Only allow valid WordPress locale codes from available translations 712 * 713 * @param string $value The language value to sanitize. 714 * @return string The sanitized language code. 715 */ 716 public function sanitize_language_setting( $value ) { 717 // First sanitize the text field to remove any dangerous characters 718 $value = sanitize_text_field( $value ); 719 720 // If empty, default to site locale 721 if ( empty( $value ) ) { 722 return get_locale(); 723 } 724 725 // Get available WordPress translations 726 require_once ABSPATH . 'wp-admin/includes/translation-install.php'; 727 $languages = wp_get_available_translations(); 728 729 // Build whitelist of valid language codes 730 // Add English (US) as it's not always included in wp_get_available_translations() 731 $valid_languages = array( 'en_US' ); 732 foreach ( $languages as $lang ) { 733 if ( isset( $lang['language'] ) ) { 734 $valid_languages[] = $lang['language']; 735 } 736 } 737 738 // Only return the value if it's in the whitelist, otherwise default to site locale 739 if ( ! in_array( $value, $valid_languages, true ) ) { 740 return get_locale(); 741 } 742 743 return $value; 709 744 } 710 745 -
alt-text-imagerr-ai/trunk/readme.txt
r3409077 r3423116 5 5 Requires PHP: 5.2 6 6 Requires at least: 4.6 7 Stable tag: 1.4. 17 Stable tag: 1.4.2 8 8 Tested up to: 6.9 9 9 License: GPLv2 or later … … 71 71 72 72 == Changelog == 73 = 1.4.2 = 74 * Fixed issue with image file paths 75 * Fixed issue when image description was overriding the description with a blank description 76 73 77 = 1.4.1 = 74 78 * Changed URLs to new faster API server -
alt-text-imagerr-ai/trunk/src/Meta.php
r3409077 r3423116 56 56 $this->log( "> $image_id" ); 57 57 58 $ai_fields = $this->api_generate_meta( $image_ url );58 $ai_fields = $this->api_generate_meta( $image_id, $image_url ); 59 59 60 60 if ( is_wp_error( $ai_fields ) ) { … … 272 272 * API generate meta. 273 273 * 274 * @param string $image_url The image URL. 274 * @param int $image_id The image ID. 275 * @param string $image_url The image URL (used for IMAGERR_UPLOADS_URL replacement and logging). 275 276 * @return array|\WP_Error The AI fields or a WP_Error object. 276 277 */ 277 private function api_generate_meta( $image_url ) { 278 private function api_generate_meta( $image_id, $image_url ) { 279 // Get the actual file path using WordPress's built-in function 280 // This works reliably regardless of CDN, custom upload directories, or multisite setups 281 $image_path = get_attached_file( $image_id ); 282 283 // Check if file exists locally 284 if ( ! $image_path || ! file_exists( $image_path ) ) { 285 return new \WP_Error( 'imagerr_error', 'Image file not found for attachment ID: ' . $image_id ); 286 } 287 288 // If IMAGERR_UPLOADS_URL is defined, we need to adjust the URL for external access 278 289 if ( defined( 'IMAGERR_UPLOADS_URL' ) ) { 279 290 $wp_upload_dir = wp_upload_dir(); 280 291 $base_url = $wp_upload_dir['baseurl']; 281 292 $image_url = str_replace( $base_url, rtrim( IMAGERR_UPLOADS_URL, '/' ), $image_url ); 282 }283 284 // Get the image file path from URL285 $upload_dir = wp_upload_dir();286 $image_path = str_replace( $upload_dir['baseurl'], $upload_dir['basedir'], $image_url );287 288 // Check if file exists locally289 if ( ! file_exists( $image_path ) ) {290 return new \WP_Error( 'imagerr_error', 'Image file not found: ' . $image_path );291 293 } 292 294 … … 303 305 304 306 $site_url = get_site_url(); 305 $language = get_option( 'imagerr_generation_language', get_locale() );307 $language = $this->validate_language( get_option( 'imagerr_generation_language', get_locale() ) ); 306 308 307 309 // Prepare multipart form data … … 442 444 $credits = json_decode( wp_remote_retrieve_body( $response ), true ); 443 445 444 445 if ( isset( $credits['data']['credits'] ) && $credits['data']['credits'] > 0 ) { 446 $credits = $credits['data']['credits'] ?? $credits['credits'] ?? 0; 447 448 if ( $credits > 0 ) { 446 449 $dismissed_notice = get_option( 'imagerr_dismissed_notice' ); 447 450 if ( ! $dismissed_notice ) { … … 450 453 } 451 454 452 return $credits['data']['credits'] ?? 0; 455 return $credits; 456 } 457 458 /** 459 * Validate language code to prevent injection attacks 460 * Only allow valid WordPress locale codes from available translations 461 * 462 * @param string $language The language code to validate. 463 * @return string The validated language code. 464 */ 465 private function validate_language( $language ) { 466 // If empty, default to site locale 467 if ( empty( $language ) ) { 468 return get_locale(); 469 } 470 471 // Get available WordPress translations 472 require_once ABSPATH . 'wp-admin/includes/translation-install.php'; 473 $languages = wp_get_available_translations(); 474 475 // Build whitelist of valid language codes 476 // Add English (US) as it's not always included in wp_get_available_translations() 477 $valid_languages = array( 'en_US' ); 478 foreach ( $languages as $lang ) { 479 if ( isset( $lang['language'] ) ) { 480 $valid_languages[] = $lang['language']; 481 } 482 } 483 484 // Only return the value if it's in the whitelist, otherwise default to site locale 485 if ( ! in_array( $language, $valid_languages, true ) ) { 486 return get_locale(); 487 } 488 489 return $language; 453 490 } 454 491
Note: See TracChangeset
for help on using the changeset viewer.