plugin development - What is an alternative method to the WordPress private _doing_it_wrong() function

I've noticed plugins using a singleton pattern that will use WordPress's _doing_it_wrong() function in their c

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:

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: /

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:

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/reference/functions/_doing_it_wrong/

Share Improve this question asked Sep 9, 2016 at 5:27 XtremefaithXtremefaith 7882 gold badges5 silver badges21 bronze badges 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 Commented 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. – Xtremefaith Commented Sep 9, 2016 at 16:52
  • Singletons violate the SRP. – David Commented Sep 11, 2016 at 15:31
Add a comment  | 

2 Answers 2

Reset to default 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() {
  }
}

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742306110a4418973.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信