• Hello,

    I have installed plugin Custom Permalinks in a project which is under development.

    The project has enabled the PHP errors and I have found two “bugs” in the file plugins/custom-permalinks/custom-permalinks.php of the plugin.

    1) Line 267

    function custom_permalinks_trailingslash($string, $type) {
      global $_CPRegisteredURL;
    
      $url = parse_url(get_bloginfo('url'));
      $request = ltrim(isset($url['path']) ? substr($string, strlen($url['path'])) : $string, '/');
    
      if ( !trim($request) ) return $string;
    
      if ( trim($_CPRegisteredURL,'/') == trim($request,'/') ) {
        return ($string{0} == '/' ? '/' : '') . trailingslashit($url['path']) . $_CPRegisteredURL;
      }
      return $string;
    }

    WordPress function get_bloginfo(‘url’) gets home url.

    In case the option home_url is the same with domain i.e. https://www.domain.com, the $url[‘path’] is not set.

    So, the expression

    return ($string{0} == '/' ? '/' : '') . trailingslashit($url['path']) . $_CPRegisteredURL;

    produces the warning
    Undefined index: path

    In case the home_url points to a subfolder of the domain i.e.
    https://www.domain.com/subfolder, the $url[‘path’] is set and the function is working properly.

    I updated the function like this:

    function custom_permalinks_trailingslash($string, $type) {
      global $_CPRegisteredURL;
    
      $url = parse_url(get_bloginfo('url'));
      $request = ltrim(isset($url['path']) ? substr($string, strlen($url['path'])) : $string, '/');
    
      if ( !trim($request) ) return $string;
    
      if ( trim($_CPRegisteredURL,'/') == trim($request,'/') && isset($url['path']) ) {
        return ($string{0} == '/' ? '/' : '') . trailingslashit($url['path']) . $_CPRegisteredURL;
      }
      return $string;
    }

    and it seems that is working for both cases.

    2) Line 292

    function custom_permalink_get_sample_permalink_html($html, $id, $new_title, $new_slug) {
        $permalink = get_post_meta( $id, 'custom_permalink', true );
      $post = &get_post($id);
    
      ob_start();
      ?>
      <?php custom_permalinks_form($permalink, ($post->post_type == "page" ? custom_permalinks_original_page_link($id) : custom_permalinks_original_post_link($id)), false); ?>
      <?php
      $content = ob_get_contents();
      ob_end_clean();
    
        if ( 'publish' == $post->post_status ) {
            $view_post = 'page' == $post->post_type ? __('View Page') : __('View Post');
      }
    
      if ( preg_match("@view-post-btn.*?href='([^']+)'@s", $html, $matches) ) {
          $permalink = $matches[1];
        } else {
            list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
            if ( false !== strpos($permalink, '%postname%') || false !== strpos($permalink, '%pagename%') ) {
                $permalink = str_replace(array('%pagename%','%postname%'), $post_name, $permalink);
            }
        }
    
      return '<strong>' . __('Permalink:') . "</strong>\n" . $content .
           ( isset($view_post) ? "<span id='view-post-btn'><a href='$permalink' class='button button-small' target='_blank'>$view_post</a></span>\n" : "" );
    }

    In the second line of the function, the result of the WP function get_post() assigned to a variable by reference.
    $post = &get_post($id);

    This produces the error:
    Only variables should be assigned by reference

    NOTE: I am using PHP 5.5

    I updated the above line to:
    $post = get_post($id);

    and again it seems the plugin is working properly.

    Can you please confirm that the two above changes are correct and they do not affect the functionality of the plugin?

    In case that the changes are correct and they needed, can you please update the plugin accordingly?

    Thank you for your time

    Best regards

    Mponiek

Viewing 3 replies - 1 through 3 (of 3 total)
  • Sean

    (@designicu_org)

    FYI: I implemented the first fix but it made my custom permalink pages return “failed to open page” errors. For example a page I had previously working at mydomain.com/listings/work/index.shtml would no longer load.

    Sean

    (@designicu_org)

    Further info that might help: I watched the URL in my browser as I tried to go to mydomain.com/listings/work/index.shtml and, in fact, it seemed to be stuck in a loop switching between…

    mydomain.com/listings/work/index.shtml

    …and…

    mydomain.com/listings/work/index.shtml/

    …that is, applying and stripping the trailing slash.

    Hope this helps.

    Nice Mponiek, this seems to fix the messages.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘PHP warnings’ is closed to new replies.