10up / classifai

Supercharge WordPress Content Workflows and Engagement with Artificial Intelligence.
https://classifaiplugin.com
GNU General Public License v2.0
578 stars 53 forks source link

Computer Vision API throws notice for non-image attachement types. #422

Closed peterwilsoncc closed 1 year ago

peterwilsoncc commented 1 year ago

Describe the bug

Classifai\Providers\Azure\ComputerVision::maybe_rescan_image() throws a notice on PDF and other attachment types that do not include sizes meta data.

The cause of the notice is in this block which assumes the presence of the sizes array on all media rather than just images:

https://github.com/10up/classifai/blob/f890b4d5ea0ec61277d4e9e3578b5847bf435963/includes/Classifai/Providers/Azure/ComputerVision.php#L404-L411

Steps to Reproduce

  1. Install WP with Classifai and Query Monitor
  2. Enable Computer Vision
  3. Upload a PDF on the media screen
  4. Click the PDF's icon to edit it
  5. Modify the description and tab to another field.
  6. The Query Monitor admin bar button will turn yellow and show an AJAX Notice alert
  7. Observe the notice in the browser console:
   Component: "Plugin: classifai"
​   file: "content/plugins/classifai/includes/Classifai/Providers/Azure/ComputerVision.php"
​   key: "5fb902ded0df0b68cb9efaa046dfedfa"
​   line: 408
​   message: "Undefined index: sizes"
​   ​   stack: Array(7) [ "Classifai\\P\\A\\ComputerVision->maybe_rescan_image()", "do_action('edit_attachment')", "wp_insert_post()", … ]
​​​   ​   0: "Classifai\\P\\A\\ComputerVision->maybe_rescan_image()"
​​​   ​   1: "do_action('edit_attachment')"
​​​   ​   2: "wp_insert_post()"
​​​   ​   3: "wp_insert_attachment()"
​​​   ​   4: "wp_update_post()"
​​​   ​   5: "wp_ajax_save_attachment()"
​​​   ​   6: "do_action('wp_ajax_save-attachment')"
​   ​   length: 7
​   ​​type: "notice"

Screenshots, screen recording, code snippet

No response

Environment information

WordPress information

Site Health Info ``` ### wp-core ### version: 6.2 site_language: en_US user_language: en_US timezone: +00:00 permalink: /%year%/%monthnum%/%postname%/ https_status: false multisite: false user_registration: 0 blog_public: 1 default_comment_status: open environment_type: local user_count: 1 dotorg_communication: true ### wp-paths-sizes ### wordpress_path: /vagrant/wp wordpress_size: loading... uploads_path: /vagrant/content/uploads uploads_size: loading... themes_path: /vagrant/content/themes themes_size: loading... plugins_path: /vagrant/content/plugins plugins_size: loading... database_size: loading... total_size: loading... ### wp-dropins (1) ### db.php: true ### wp-active-theme ### name: Twenty Twenty-Three (twentytwentythree) version: 1.1 author: the WordPress team author_website: https://wordpress.org parent_theme: none theme_features: core-block-patterns, post-thumbnails, responsive-embeds, editor-styles, html5, automatic-feed-links, block-templates, widgets-block-editor theme_path: /vagrant/wp/wp-content/themes/twentytwentythree ### wp-themes-inactive (12) ### Twenty Eleven: version: 4.3, author: the WordPress team Twenty Fifteen: version: 3.4, author: the WordPress team Twenty Fourteen: version: 3.6, author: the WordPress team Twenty Nineteen: version: 2.5, author: the WordPress team Twenty Seventeen: version: 3.2, author: the WordPress team Twenty Sixteen: version: 2.9, author: the WordPress team Twenty Ten: version: 3.8, author: the WordPress team Twenty Thirteen: version: 3.8, author: the WordPress team Twenty Twelve: version: 3.9, author: the WordPress team Twenty Twenty: version: 2.2, author: the WordPress team Twenty Twenty-One: version: 1.8, author: the WordPress team Twenty Twenty-Two: version: 1.4, author: the WordPress team ### wp-plugins-active (3) ### ClassifAI: version: 1.9.0-dev, author: 10up Query Monitor: version: 3.12.1, author: John Blackbourn User Switching: version: 1.7.0, author: John Blackbourn & contributors ### wp-plugins-inactive (1) ### Classic Editor: version: 1.6.3, author: WordPress Contributors ### wp-media ### image_editor: WP_Image_Editor_GD imagick_module_version: Not available imagemagick_version: Not available imagick_version: Not available file_uploads: File uploads is turned off post_max_size: 1024M upload_max_filesize: 1024M max_effective_size: 1 GB max_file_uploads: 20 gd_version: 2.3.3 gd_formats: GIF, JPEG, PNG, WebP, BMP, XPM ghostscript_version: not available ### wp-server ### server_architecture: Linux 5.15.0-30-generic x86_64 httpd_software: nginx/1.18.0 php_version: 7.4.30 64bit php_sapi: fpm-fcgi max_input_variables: 1000 time_limit: 900 memory_limit: 128M admin_memory_limit: 256M max_input_time: 900 upload_max_filesize: 1024M php_post_max_size: 1024M curl_version: 7.81.0 OpenSSL/3.0.2 suhosin: false imagick_availability: false pretty_permalinks: true ### wp-database ### extension: mysqli server_version: 8.0.30-0ubuntu0.22.04.1 client_version: mysqlnd 7.4.30 max_allowed_packet: 16777216 max_connections: 151 ### wp-constants ### WP_HOME: http://xu-classifai.local WP_SITEURL: http://xu-classifai.local/wp WP_CONTENT_DIR: /vagrant/content WP_PLUGIN_DIR: /vagrant/content/plugins WP_MEMORY_LIMIT: 40M WP_MAX_MEMORY_LIMIT: 256M WP_DEBUG: true WP_DEBUG_DISPLAY: true WP_DEBUG_LOG: true SCRIPT_DEBUG: false WP_CACHE: false CONCATENATE_SCRIPTS: undefined COMPRESS_SCRIPTS: undefined COMPRESS_CSS: undefined WP_ENVIRONMENT_TYPE: local DB_CHARSET: utf8 DB_COLLATE: undefined ### wp-filesystem ### wordpress: writable wp-content: writable uploads: writable plugins: writable themes: writable ### classifai ### 0: 1.9.0-dev 1: yes 2: peter.wilson@10up.com 3: Configured: no API URL: API username: apikey Post types: post, page Features: {"category":1, "category_threshold":70, "category_taxonomy":"category", "keyword":1, "keyword_threshold":70, "keyword_taxonomy":"category", "entity":1, "entity_threshold":70, "entity_taxonomy":"category", "concept":1, "concept_threshold":70, "concept_taxonomy":"category"} Latest response: N/A 4: Authenticated: no Generate excerpt: no Allowed roles: Excerpt length: 55 Latest response: N/A 5: Authenticated: yes API URL: https://eastus.api.cognitive.microsoft.com/ Caption threshold: 75 Latest response - Image Scan: {"tags":[{"name":"text", "confidence":0.9912763237953186},{"name":"screenshot", "confidence":0.97503066062927246},{"name":"design", "confidence":0.95708608627319336},{"name":"graphic", "confidence":0.80960321426391602},{"name":"logo", "confidence":0.79690659046173096},{"name":"typography", "confidence":0.75953376293182373},{"name":"illustration", "confidence":0.64977622032165527},{"name":"font", "confidence":0.64477717876434326},{"name":"graphics", "confidence":0.59695339202880859}], "description":{"tags":["logo"], "captions":[{"text":"logo", "confidence":0.87399297952651978}]}, "requestId":"a853cbd1-8992-45e0-a96d-36538878c715", "metadata":{"height":648, "width":1232, "format":"Png"}} Latest response - Smart Cropping: N/A Latest response - OCR: {"language":"en", "textAngle":0, "orientation":"Up", "regions":[{"boundingBox":"436,378,695,132", "lines":[{"boundingBox":"436,378,695,132", "words":[{"boundingBox":"436,378,695,132", "text":"800x450"}]}]}], "modelVersion":"2021-04-01"} 6: Authenticated: no Generate images: no Allowed roles: Number of images: 1 Image size: 1024x1024 Latest response: N/A 7: Authenticated: Yes API URL: https://classifai-personalizer.cognitiveservices.azure.com/ Service Status: {"service":"personalizationfrontend", "apiStatus":"Valid", "apiStatusMessage":"Api Key is valid, no action needed."} ```

Code of Conduct

av3nger commented 1 year ago

Added PR to fix the issue