Plugin preg_match_all Error after Updating from very oldto PHP 7.4

236 Views Asked by At

Our website with error turned on (plugin activated):

Example URLs with error: https://www.live-karikaturen.ch/downloads/1-august-fahnen-schwingen-nationalfeiertag-schweiz/ https://www.live-karikaturen.ch/webshop-gratisbilder-free-cartoon-comic-images/ https://www.live-karikaturen.ch/downloads/auto-suv-autofan-abgase-umweltverschmutzung/

UPDATE INCOMPATIBILITY: I updated from a very old PHP version to 7.4

I use this EXTREMLY important plugin, which is horrible outdated (4 years last Update): https://wordpress.org/support/plugin/creative-commons-configurator-1/

Now I get this error and I think it's maybe very easy to solve, but I know almost nothing about PHP, but I try to learn it a bit, so thank you very much in advance.

ERROR: *

Warning: preg_match_all(): Compilation failed: escape sequence is invalid in character class at offset 18 in /home/clients/f9abb707fe4ac1215fe0f0a9c0b0ae21/www.live-karikaturen-ch/wp-content/plugins/creative-commons-configurator-1/creative-commons-configurator-1.php on line 821

I copy the code from the .php file here

CODE:This is line 821:

if ( ! preg_match_all( $pattern_images_no_caption, $post_content, $matches ) ) {
return $post_content;

}

CODE From line 803 - 828 (don't know who to copy the line numbers):

function bccl_separate_licensing_on_images_without_caption( $post_content ) {

    // Plugin options
    $options = get_option('cc_settings');
    if ( $options === false || $options['cc_enable_individual_media_licensing'] == '0' ) {
        return $post_content;
    }

    $post = get_queried_object();

    if ( apply_filters('bccl_exclude_license', false, $post) ) {
        return $post_content;
    }

    // Pattern that matches images without caption
    //$pattern_images_no_caption = '#<p>[\s\R]*(<img [^>]+>)#';
    $pattern_images_no_caption = '#<(?:p|h[\d]+)>[\s\R]*(<img [^>]+>)#';
    $pattern_images_no_caption = apply_filters('bccl_pattern_match_images_without_caption' , $pattern_images_no_caption);
    **if ( ! preg_match_all( $pattern_images_no_caption, $post_content, $matches ) ) {
        return $post_content;
    }**
    //var_dump($matches[1]);

    // Iterate over the found images and add licensing metadata

    foreach ( $matches[1] as $image_html ) {

Is there a simple way how to fix this error?

THANK YOU VERY MUCH FOR YOUR ANSWER & HELP!

1

There are 1 best solutions below

2
On

THAAAANK you very much, you are AMAZING! :) I tried it out and it works, at least there is no error anymore.

FIRST I am just not sure which version of the corrected code I should take. I guess Nr. 1 is ok, without []. That mean, as Toto wrote above I replaced [\s\R] with a simple \s.

Nr. 1.) without []

//$pattern_images_no_caption = '#

\s*(<img [^>]+>)#'; $pattern_images_no_caption = '#<(?:p|h[\d]+)>\s*(<img [^>]+>)#';

Nr. 2.) with []

//$pattern_images_no_caption = '#<p>[\s]*(<img [^>]+>)#';
    $pattern_images_no_caption = '#<(?:p|h[\d]+)>[\s]*(<img [^>]+>)#';

SECOND I searched the whole creative-commons-configurator.php plugin code for \R and found further in line 979 this:

    $parts = preg_split('#\R#u', $attach`enter code here`ment_data);

All lines together of this part are this:

// Attachment ID
    // Unfortunately, WP does not provide enough information in order to determine the attachment ID
    // So, first collect all the audio/video media file URLs in $attachments_urls
    $attachments_data = get_post_meta( $post->ID, 'enclosure', false );
    //var_dump($attachments_data);
    $attachments_urls = array();
    foreach ( $attachments_data as $attachment_data ) {
        $parts = preg_split('#\R#u', $attachment_data);
        $attachments_urls[] = $parts[0];
    }
    //var_dump($attachments_urls);
    // Then check which media file URL exists in the $atts array so as to
    // determine which attachment we are processing currently.
    $atts_values = array_values($atts);
    //var_dump($atts_values);
    // Find the URL of the attachment we are processing
    $current_attachment_url = '';
    foreach ( $attachments_urls as $attachment_url) {
        if ( in_array($attachment_url, $atts_values) ) {
            $current_attachment_url = $attachment_url;
            break;
        }
    }

QUESTION: In the moment I don't get any error, but I wonder if this will produce one and I have to delete or replace the "evil" \R?