7

I've noticed plugins using a singleton pattern that will use WordPress's _doing_it_wrong() function in their clone() methods, like so:

<?php
public function __clone() {
    _doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'divlibrary' ), $this->version );
}
?>

But I also noticed this warning/notice on WordPress's official documentation: enter image description here

It really doesn't matter when, how a developer is using it, WordPress is advocating that it shouldn't be used, so I'm wondering what is the correct way to do this within custom plugins? The method is used for deprecating code but in this instance the developer is just wanting to send a warning/alert out.

Reference: https://developer.wordpress.org/reference/functions/_doing_it_wrong/

3
  • The best thing would be to not write public code that is intended to be used internally only. However, if you want to handle a logical error in your public API throw a LogicException as this error is caused by wrong design of the executing code. If you don't want to use exceptions, you can still always use trigger_error(). Also: Don't use singletons!
    – David
    Sep 9, 2016 at 7:44
  • @David Thanks for the trigger_error() tip. As for "Don't use singletons" directive, there is a time and place to use them, so to declare that as a commandment is just as naive as the reasons most people use them. Sep 9, 2016 at 16:52
  • Singletons violate the SRP.
    – David
    Sep 11, 2016 at 15:31

2 Answers 2

2

What is an alternative method to the WordPress private _doing_it_wrong() function?

There's no way that WordPress is ever going to get rid of the _doing_it_wrong() function, so it's perfectly safe to use it. But if for some reason you don't want to use it because it's marked private, then you could create a plugin that has a function called doing_it_wrong() that is copy and pasted from _doing_it_wrong().

Another way would be to not copy code and instead use a class that handles deprecated code. Here's some sample code that basically does the same thing as _doing_it_wrong().

class deprecated {
  protected $method;
  protected $message;
  protected $version;

  public function method( $method ) {
    $this->method = $method;
    return $this;
  }

  public function message( $message ) {
    $this->message = $message;
    return $this;
  }

  public function version( $version ) {
    $this->version = sprintf( 
      __( 'This message was added in version %1$s.' ), 
      $version
    );
    return $this;
  }

  public function trigger_error() {
    do_action( 'doing_it_wrong_run', $this->method, $this->message, $this->version );
    if ( WP_DEBUG && apply_filters( 'doing_it_wrong_trigger_error', true ) ) {
      trigger_error( sprintf( 
        __( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s' ),
        isset( $this->method ) ? $this->method : '', 
        isset( $this->message ) ? $this->message : '', 
        isset( $this->version ) ? $this->version : ''
      ) );
    }
  }
}

Usage

class wpse_238672 {
  public function some_deprecated_method() {
    ( new deprecated() )
      ->method( __METHOD__ )
      ->message( __( 
        'Deprecated Method: Use non_deprecated_method() instead.', 'wpse-238672'
       ) )
      ->version( '2.3.4' )
      ->trigger_error();

    $this->non_deprecated_method();
  }

  public function non_deprecated_method() {
  }
}
1

As of WordPress 5.4 _doing_it_wrong() is no longer marked private, so it looks like we can just use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.