Support » Developing with WordPress » unexpected behavior in wp_link_query_args

  • I’ve got some code that updates the functionality of the Insert Hyperlink button in TinyMCE (WP5.9.x) so that I can create links to “Draft” and “Pending” posts, in addition to posts that are already published.

    The code (in my functions.php) follows:

    function adventuretacoplugin_wp_link_query_args( $query ) {
      //Abort early if the user will never see TinyMCE
      if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') && get_user_option('rich_editing') == 'true')
        return;
    
      $post_status_array[] = $query['post_status'];
      $query['post_status'] = array_push($post_status_array, 'draft', 'pending', 'auto-draft');
    
      return $query;
    }
    
    function update_drafts( $result) {
      $post = get_post( $result['ID'] );
      if (in_array($post->post_status, array('draft', 'pending', 'auto-draft', 'future'))) {
        $my_post = clone $post;
        $my_post->post_status = 'publish';
        $my_post->post_name = sanitize_title($my_post->post_name ? $my_post->post_name : $my_post->post_title, $my_post->ID);
        $result['permalink'] = get_permalink($my_post);
        $result['info'] = '(' . $post->post_status . ') ' . $result['info'];
    
      } else {
        // just pass through the $result
      }
      return $result;
    }
    
    function adventuretacoplugin_wp_link_query( $results ) {
      foreach ( $results as &$result ) {
        $result = update_drafts ( $result );
      }
      return $results;
    }
    add_filter( 'wp_link_query_args', 'adventuretacoplugin_wp_link_query_args' );
    add_filter( 'wp_link_query', 'adventuretacoplugin_wp_link_query' );
    

    This has been working splendidly for the last couple of years while my WP has been running in a Windows IIS-based, PHP7.x environment.

    The modifications above returned what I consider to be “linkable” items of various post_types (post, pages, custom post types).

    Recently, I migrated to a Linux-based, PHP8.x environment and now I’m seeing strange behavior. I’m getting a bunch of “duplicate” items returned in the Insert Hyperlink dropdown – essentially, all the the post_type = post items have a second entry in the list, and say “Media” on the right side (where usually the publish date of the post would be shown).

    Since this all happens as part of an ajax call, I’m not sure of the best way to debug this. Any help would be appreciated, or, if there is an obvious problem with my code due to some PHP7/8 issue that I don’t know about, I’d love to know that as well.

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Unless there have been recent developments I’ve not heard of yet, WP is still not 100% 8.x compatible yet, so strange problems like this is entirely possible.

    I’m not sure of the best way to debug this

    Build yourself a simple testing page, either a custom page template or admin settings page. From there call your Ajax handler directly, supplying its expected data by setting them directly in $_REQUEST or similar. Thus it’s just like any other procedural WP function. Debug as you normally would.

    Thread Starter turbodb

    (@turbodb)

    Thanks; the debugging idea is a good one.

    For the time being, I’ve removed the attachment post_type from the query, which has at least resulted in the correct behavior in the authoring experience.

    Interesting about PHP8; I was under the impression that 7.x was going out of support in November, and though 8 would be supported by now.

    At any rate, thanks!

    Moderator bcworkz

    (@bcworkz)

    You’re likely correct about PHP. Everything seems to lag behind whatever PHP is doing. Add to that the fact that WP is nearly all volunteer driven. Some things just take a while. Don’t get me wrong, WP is very nearly there, my understanding is there are just a few niggling details that still need to be resolved. Disclaimer: I don’t follow core development very closely. Also, this supposed compatibility issue could lie within themes or plugins, which are in wildly varying of states of compatibility.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.