Plugin Directory

Changeset 3265951


Ignore:
Timestamp:
04/03/2025 12:08:30 AM (12 months ago)
Author:
etruel
Message:

2.8.2 Abr 2, 2025

Location:
wpematico
Files:
158 added
7 edited

Legend:

Unmodified
Added
Removed
  • wpematico/trunk/app/campaign_fetch.php

    r3261841 r3265951  
    839839                $itemUrl = $this->current_item['permalink'];
    840840                $imagen_src = $this->current_item['featured_image'];
    841                 //**** ecesaria para la featured ?  $imagen_src = apply_filters('wpematico_imagen_src', $imagen_src ); // allow strip parts
     841               
    842842                $imagen_src_real = $this->getRelativeUrl($itemUrl, $imagen_src);
    843                 // Strip all white space on images URLs.   
    844                 $imagen_src_real = str_replace(' ', '%20', $imagen_src_real);
    845                 // Fix images URLs with entities like & to get it with correct name and remain the original in images array.
    846                 $imagen_src_real = html_entity_decode($imagen_src_real);
    847                 $imagen_src_real = apply_filters('wpematico_img_src_url', $imagen_src_real);
     843               
     844                // Parse the original name on url to get the file correctly
     845                $imagen_src_real = $this->parse_src_image($imagen_src_real);
     846
    848847                $allowed = (isset($this->cfg['images_allowed_ext']) && !empty($this->cfg['images_allowed_ext']) ) ? $this->cfg['images_allowed_ext'] : 'jpg,gif,png,tif,bmp,jpeg';
    849848                $allowed = apply_filters('wpematico_allowext', $allowed);
    850                 //Fetch and Store the Image
    851                 ///////////////***************************************************************************************////////////////////////
    852                 $newimgname = apply_filters('wpematico_newimgname', sanitize_file_name(urlencode(basename($imagen_src_real))), $this->current_item, $this->campaign, $item);  // new name here
    853                 // Primero intento con mi funcion mas rapida
     849               
     850                // Parse the destiny filename to store the file correctly on WP media or system directory
     851                $newimgname = $this->parse_dst_image($imagen_src_real, $this->current_item, $this->campaign, $item);
     852               
     853                // Proceed with the Fetch and Store the Image
    854854                $upload_dir = wp_upload_dir();
    855855                $imagen_dst = trailingslashit($upload_dir['path']) . $newimgname;
    856856                $imagen_dst_url = trailingslashit($upload_dir['url']) . $newimgname;
    857857                $img_new_url = "";
    858                 if (in_array(str_replace('.', '', strrchr(strtolower($imagen_dst), '.')), explode(',', $allowed))) {   // -------- Controlo extensiones permitidas
     858               
     859                // Check for allowed extensions
     860                if (in_array(str_replace('.', '', strrchr(strtolower($imagen_dst), '.')), explode(',', $allowed))) {
    859861                    trigger_error('Uploading media=' . $imagen_src . ' <b>to</b> imagen_dst=' . $imagen_dst . '', E_USER_NOTICE);
     862                   
     863                    // First I try my fastest function
    860864                    $newfile = ($options_images['customupload']) ? WPeMatico::save_file_from_url($imagen_src_real, $imagen_dst) : false;
    861                     if ($newfile) { //subió
     865                    if ($newfile) { // uploaded
    862866                        trigger_error('Uploaded media=' . $newfile, E_USER_NOTICE);
    863867                        $imagen_dst = $newfile;
    864868                        $imagen_dst_url = trailingslashit($upload_dir['url']) . basename($newfile);
    865869                        $img_new_url = $imagen_dst_url;
    866                     } else { // falló -> intento con otros
    867                         $bits = WPeMatico::wpematico_get_contents($imagen_src_real);
    868                         $mirror = wp_upload_bits($newimgname, NULL, $bits);
    869                         if (!$mirror['error']) {
    870                             trigger_error($mirror['url'], E_USER_NOTICE);
    871                             $img_new_url = $mirror['url'];
    872                         }
     870                       
     871                    } else { // Upload failed
     872                        // try other methods
     873                        $bits = WPeMatico::wpematico_get_contents($imagen_src_real); // Read the file
     874                       
     875                        if (!$bits) { //Reading errors
     876                            // Actions if give nothing or getting errors
     877                            trigger_error(__('Failed to obtain file:', 'wpematico') . $imagen_src_real, E_USER_WARNING);
     878                        }else{
     879                            // Obtained -> Try to upload
     880                            $mirror = wp_upload_bits($newimgname, NULL, $bits);
     881                            if (!$mirror['error']) { // Upload well
     882                                trigger_error($mirror['url'], E_USER_NOTICE);
     883                                $img_new_url = $mirror['url'];
     884                            } else { // Uploading errors
     885                                trigger_error(__('Save Featured image file failed:', 'wpematico') . $imagen_dst, E_USER_WARNING);
     886                            }
     887                        }
    873888                    }
    874889                }
    875             } else {
     890            } else { // The image[0] was already uploaded with the images in the content, do not upload it again
    876891                $img_new_url = $this->current_item['featured_image'];
    877892            }
     
    881896                $attachid = false;
    882897                if (!$options_images['imgattach']) {
    883                     //get previously uploaded attach IDs, false if not exist.  (Just attach once/first time)
    884                     //  $attachid = $this->get_attach_id_from_url($this->current_item['featured_image']);
     898                    // Get previously uploaded attach IDs, false if not exist.  (Just attach once/first time)
     899                   
     900                    // $attachid = $this->get_attach_id_from_url($this->current_item['featured_image']);
    885901                    $attachid = attachment_url_to_postid($this->current_item['featured_image']);
    886902                }
  • wpematico/trunk/app/campaign_fetch_functions.php

    r3261841 r3265951  
    447447            $itemUrl = $this->current_item['permalink'];
    448448
    449             if (sizeof($current_item['images'])) { // Si hay alguna imagen en el contenido
     449            if (sizeof($current_item['images'])) { // If there is at least one image in the content
    450450                trigger_error('<b>' . __('Looking for images in content.', 'wpematico') . '</b>', E_USER_NOTICE);
    451451                //trigger_error(print_r($current_item['images'],true),E_USER_NOTICE);
     
    458458                    $imagen_src      = apply_filters('wpematico_imagen_src', $imagen_src); // allow strip parts
    459459                    trigger_error(__('Uploading media...', 'wpematico') . $imagen_src, E_USER_NOTICE);
     460                   
    460461                    $imagen_src_real = $this->getRelativeUrl($itemUrl, $imagen_src);
    461                     // Strip all white space on images URLs.   
    462                     $imagen_src_real = str_replace(' ', '%20', $imagen_src_real);
    463                     // Fix images URLs with entities like &amp; to get it with correct name and remain the original in images array.
    464                     $imagen_src_real = html_entity_decode($imagen_src_real);
    465                     $imagen_src_real = apply_filters('wpematico_img_src_url', $imagen_src_real); // original source
     462                   
     463                    // Parse the original name on url to get the file correctly
     464                    $imagen_src_real = $this->parse_src_image($imagen_src_real);
     465                   
    466466                    $allowed         = (isset($this->cfg['images_allowed_ext']) && !empty($this->cfg['images_allowed_ext']) ) ? $this->cfg['images_allowed_ext'] : 'jpg,gif,png,tif,bmp,jpeg';
    467467                    $allowed         = apply_filters('wpematico_allowext', $allowed);
    468                     //Fetch and Store the Image
    469                     ///////////////***************************************************************************************////////////////////////
    470                     $newimgname      = apply_filters('wpematico_newimgname', sanitize_file_name(urlencode(basename($imagen_src_real))), $current_item, $campaign, $item);  // new name here
    471468                   
    472                     // We need to cut the number of characters in the name to avoid upload errors by OS limits.
    473                     $newimgname      = mb_substr($newimgname , 0, 245);
     469                    // Parse the destiny filename to store the file correctly on WP media or system directory
     470                    $newimgname = $this->parse_dst_image($imagen_src_real, $current_item, $campaign, $item);
    474471                   
    475                     // First I try my fastest function
     472                    // Proceed with the Fetch and Store the Image
    476473                    $upload_dir      = wp_upload_dir();
    477474                    $imagen_dst      = trailingslashit($upload_dir['path']) . $newimgname;
    478475                    $imagen_dst_url  = trailingslashit($upload_dir['url']) . $newimgname;
    479476                    trigger_error('Filtering image extensions:' . $allowed, E_USER_NOTICE);
    480                     if (in_array(str_replace('.', '', strrchr(strtolower($imagen_dst), '.')), explode(',', $allowed))) {   // ----- check allowed extensions
     477                   
     478                    // Check for allowed extensions
     479                    if (in_array(str_replace('.', '', strrchr(strtolower($imagen_dst), '.')), explode(',', $allowed))) {
    481480                        trigger_error('Uploading media=' . $imagen_src . ' <b>to</b> image=' . $imagen_dst . '', E_USER_NOTICE);
    482                         // Check if try custom functions to upload files.
     481                       
     482                        // First I try my fastest function
    483483                        $newfile = ($options_images['customupload']) ? WPeMatico::save_file_from_url($imagen_src_real, $imagen_dst) : false;
    484                         if ($newfile) { //If <> false was uploaded
     484                        if ($newfile) { // uploaded
    485485                            trigger_error('Uploaded media=' . $newfile, E_USER_NOTICE);
    486486                            $imagen_dst              = $newfile;
     
    489489                            do_action('wpematico_new_image_url_uploaded', $imagen_dst_url, $imagen_src, $current_item, $campaign);
    490490                            $img_new_url[]           = $imagen_dst_url;
    491                         } else { // Upload fail -> try with others
     491                           
     492                        } else { // Upload fail
     493                            // try other methods
    492494                            $bits = WPeMatico::wpematico_get_contents($imagen_src_real); // Read the file
    493                             if (!$bits) {
    494                                 // Remove the image if its upload fail.
    495                                 trigger_error(__('Upload file failed:', 'wpematico') . $imagen_dst, E_USER_WARNING);
     495                           
     496                            if (!$bits) { //Reading errors
     497                                // Actions if give nothing or getting errors
     498                                trigger_error(__('Failed to obtain file:', 'wpematico') . $imagen_src_real, E_USER_WARNING);
     499                                // Remove the image from content if its upload fail and no link to source.
    496500                                if ($options_images['gralnolinkimg']) {
    497                                     //  trigger_error( __('Deleted media img.', 'wpematico' ),E_USER_WARNING);
     501                                    //  trigger_error( __('Remove img tag from content.', 'wpematico' ),E_USER_NOTICE);
    498502                                    $current_item['content'] = self::strip_Image_by_src($imagen_src, $current_item['content']);
    499503                                }
    500504                            } else {
    501                                 //here is the error
    502 
    503                                 $mirror = wp_upload_bits($newimgname, NULL, $bits);
    504 
    505 
    506                                 if (!$mirror['error']) {
     505                                // Obtained -> Try to upload
     506                                $mirror = wp_upload_bits($newimgname, NULL, $bits);                             
     507                                if (!$mirror['error']) { // Upload well
    507508                                    trigger_error($mirror['url'], E_USER_NOTICE);
     509                                   
     510                                    // Replace the image src from content if its upload well.
    508511                                    $current_item['content'] = str_replace($imagen_src, $mirror['url'], $current_item['content']);
    509512                                    do_action('wpematico_new_image_url_uploaded', $mirror['url'], $imagen_src, $current_item, $campaign);
    510                                     $img_new_url[]           = $mirror['url'];
    511                                 } else {
    512                                     trigger_error('wp_upload_bits error:' . print_r($mirror, true) . '.', E_USER_WARNING);
    513                                     // Si no quiere linkar las img al server borro el link de la imagen
    514                                     trigger_error(__('Upload file failed:', 'wpematico') . $imagen_dst, E_USER_WARNING);
     513                                   
     514                                    $img_new_url[] = $mirror['url'];
     515                                } else { // Uploading errors
     516                                    //trigger_error('wp_upload_bits error:' . print_r($mirror, true) . '.', E_USER_WARNING);
     517                                    trigger_error(__('Save image file failed:', 'wpematico') . $imagen_dst, E_USER_WARNING);
     518                                   
     519                                    // Remove the image from content if its upload fail and no link to source.
    515520                                    if ($options_images['gralnolinkimg']) {
    516                                         //  trigger_error( __('Deleted media img.', 'wpematico' ),E_USER_WARNING);
     521                                        //  trigger_error( __('Remove img tag from content.', 'wpematico' ),E_USER_NOTICE);
    517522                                        $current_item['content'] = self::strip_Image_by_src($imagen_src, $current_item['content']);
    518523                                    }
     
    522527                    } else {
    523528                        trigger_error(__('Extension not allowed: ', 'wpematico') . urldecode($imagen_dst_url), E_USER_WARNING);
    524                         if ($options_images['gralnolinkimg']) { // Si no quiere linkar las img al server borro el link de la imagen
     529                        // Remove the image from content if it is not allowed and no link to source.
     530                        if ($options_images['gralnolinkimg']) {
    525531                            trigger_error(__('Stripped src.', 'wpematico'), E_USER_WARNING);
    526532                            $current_item['content'] = self::strip_Image_by_src($imagen_src, $current_item['content']);
     
    538544                    }
    539545                }
    540             }  // // Si hay alguna imagen en el contenido
    541         } else {
     546            }  // END If there is at least one image in the content
     547        } else {  // If no cache images (not upload to current WordPress
    542548            if (isset($current_item['images']) && sizeof($current_item['images'])) {
    543549                trigger_error('<b>' . __('Using remotely linked images in content. No changes.', 'wpematico') . '</b>', E_USER_NOTICE);
     
    550556// item images
    551557
    552     /**
    553      *  // retrieves the attachment ID from the file URL
    554      *  @return integer The attach ID of the image. If not exists return false.
    555      */
    556     function get_attach_id_from_url($image_url) {
    557         global $wpdb;
    558         $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url));
    559         return ($attachment[0] > 0) ? $attachment[0] : FALSE;
    560     }
     558// Deprecated custom function in favor of WP native attachment_url_to_postid Apr 2, 2025
     559//  /**
     560//   *  // retrieves the attachment ID from the file URL
     561//   *  @return integer The attach ID of the image. If not exists return false.
     562//   */
     563//  function get_attach_id_from_url($image_url) {
     564//      global $wpdb;
     565//      $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url));
     566//      return ($attachment[0] > 0) ? $attachment[0] : FALSE;
     567//  }
    561568
    562569    /**
     
    678685    }
    679686
     687    public function parse_src_image($imagen_src_real) {
     688        // Decode URL encoding to properly handle spaces and special characters
     689        $imagen_src_real = rawurldecode($imagen_src_real);
     690
     691        // Replace spaces with %20 to keep URLs valid
     692        $imagen_src_real = str_replace(' ', '%20', $imagen_src_real);
     693
     694        // Apply WordPress sanitization
     695        return apply_filters('wpematico_img_src_url', esc_url_raw($imagen_src_real));
     696    }
     697
     698    public function parse_dst_image($imagen_src_real, $current_item, $campaign, $item) {
     699        // Decode URL to handle special characters correctly
     700        $basename = rawurldecode(basename($imagen_src_real));
     701
     702        // Extract extension and filename
     703        $ext = pathinfo($basename, PATHINFO_EXTENSION);
     704        $filename = pathinfo($basename, PATHINFO_FILENAME);
     705
     706        // Allow Chinese characters, letters, numbers, underscores, and dashes
     707        $filename = preg_replace('/[^\p{L}\p{N}_-]/u', '', $filename);
     708
     709        // We need to trim the number of characters in the filename to avoid upload errors by OS limits, while ensuring UTF-8 encoding.
     710        // Trim the filename to 200 (because windows?)
     711        $filename = mb_substr($filename, 0, apply_filters('wpematico_trim_img_name', 200), 'UTF-8');
     712
     713        // Ensure the extension remains intact
     714        $newimgname = !empty($ext) ? "{$filename}.{$ext}" : $basename;
     715
     716        // Apply WordPress sanitization and return the new filename
     717        return apply_filters('wpematico_newimgname', sanitize_file_name($newimgname), $current_item, $campaign, $item);
     718    }
     719
     720   
    680721    /**
    681722     * Filters images, upload and replace on text item content
  • wpematico/trunk/app/plugin_functions.php

    r3261841 r3265951  
    198198    $subject = 'Plugin Deactivation Feedback';
    199199    $message = "A user deactivated the plugin for the following reason: " . $reason;
    200     $message .= "\n\nThe user wrote the following $explicit_reason";
     200    if($explicit_reason != '')
     201        $message .= "\n\nThe user wrote: $explicit_reason";
     202   
    201203    $headers = ['From: ' . get_bloginfo('name') . ' <wordpress@' . parse_url(home_url(), PHP_URL_HOST) . '>'];
    202204
  • wpematico/trunk/app/wpematico_functions.php

    r3249023 r3265951  
    2121   
    2222    class WPeMatico_functions {
     23
    2324        public static $current_feed = ''; // The current feed that is running.
    2425
  • wpematico/trunk/readme.txt

    r3261841 r3265951  
    243243
    244244> See all detailed changelog at [WPeMatico Releases](https://wpematico.com/releases/)
     245
     246= 2.8.2 Abr 2, 2025 =
     247
     248* Enhanced image upload handling and processing for improved reliability.
     249* Refined feedback email content when the plugin is deactivated.
     250* Replaced custom function with WordPress native `attachment_url_to_postid` for better compatibility and performance.
     251* NOTE: **Enlarges the version required for the [WPeMatico Professional](https://etruel.com/downloads/wpematico-professional/) addon to 3.2** 
    245252
    246253= 2.8.1 Mar 25, 2025 =
     
    430437== Upgrade Notice ==
    431438
    432 Fixes few bugs about images incorporated in the last version.
     439Fixes few bugs and few improvements with images handling.
  • wpematico/trunk/wpematico.php

    r3261841 r3265951  
    44 * Plugin URI: https://www.wpematico.com
    55 * Description: Create posts automatically from RSS/Atom feeds organized into campaigns with multiples filters.  If you like it, please rate it 5 stars.
    6  * Version: 2.8.1
     6 * Version: 2.8.2
    77 * Author: Etruel Developments LLC
    88 * Author URI: https://etruel.com/wpematico/
     
    2828        private function setup_constants() {
    2929            if (!defined('WPEMATICO_VERSION'))
    30                 define('WPEMATICO_VERSION', '2.8.1');
     30                define('WPEMATICO_VERSION', '2.8.2');
    3131            if (!defined('WPEMATICO_BASENAME'))
    3232                define('WPEMATICO_BASENAME', plugin_basename(__FILE__));
  • wpematico/trunk/wpematico_class.php

    r3249023 r3265951  
    1212
    1313        const TEXTDOMAIN     = 'wpematico';
    14         const PROREQUIRED    = '3.1';
     14        const PROREQUIRED    = '3.2';
    1515        const OPTION_KEY     = 'WPeMatico_Options';
    1616
Note: See TracChangeset for help on using the changeset viewer.