Skip to content

Commit a3c672d

Browse files
committed
Abilities: Add _deprecated_ability function
1 parent 4b4e950 commit a3c672d

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

src/wp-includes/abilities-api.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,3 +612,116 @@ function wp_get_ability_categories(): array {
612612

613613
return $registry->get_all_registered();
614614
}
615+
616+
/**
617+
* Marks an ability as deprecated and informs when it has been used.
618+
*
619+
* This function should be called from within a deprecated ability's execute callback
620+
* to notify developers that they are using an obsolete ability. The function logs
621+
* the deprecation and triggers a notice when WP_DEBUG is enabled.
622+
*
623+
* Example:
624+
*
625+
* function my_plugin_old_ability_callback( $input ) {
626+
* _deprecated_ability( 'my-plugin/old-ability', '1.5.0', 'my-plugin/new-ability' );
627+
* // Legacy implementation...
628+
* }
629+
*
630+
* @since 7.0.0
631+
*
632+
* @param string $ability_name The name of the ability that is deprecated.
633+
* @param string $version Optional. The version in which the ability was deprecated.
634+
* Default empty string.
635+
* @param string $replacement Optional. The name of the ability that should be used instead.
636+
* Default empty string.
637+
*/
638+
function _deprecated_ability( string $ability_name, string $version = '', string $replacement = '' ): void {
639+
640+
/**
641+
* Fires when a deprecated ability is called.
642+
*
643+
* @since 7.0.0
644+
*
645+
* @param string $ability_name The ability that was called.
646+
* @param string $replacement The ability that should have been called.
647+
* @param string $version The version in which the ability was deprecated.
648+
*/
649+
do_action( 'deprecated_ability_run', $ability_name, $replacement, $version );
650+
651+
/**
652+
* Filters whether to trigger an error for deprecated abilities.
653+
*
654+
* @since 7.0.0
655+
*
656+
* @param bool $trigger Whether to trigger the error for deprecated abilities. Default true.
657+
*/
658+
if ( WP_DEBUG && apply_filters( 'deprecated_ability_trigger_error', true ) ) {
659+
if ( function_exists( '__' ) ) {
660+
if ( $replacement ) {
661+
if ( $version ) {
662+
$message = sprintf(
663+
/* translators: 1: Ability name, 2: Version number, 3: Alternative ability name. */
664+
__( 'Ability %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ),
665+
$ability_name,
666+
$version,
667+
$replacement
668+
);
669+
} else {
670+
$message = sprintf(
671+
/* translators: 1: Ability name, 2: Alternative ability name. */
672+
__( 'Ability %1$s is <strong>deprecated</strong>! Use %2$s instead.' ),
673+
$ability_name,
674+
$replacement
675+
);
676+
}
677+
} else {
678+
if ( $version ) {
679+
$message = sprintf(
680+
/* translators: 1: Ability name, 2: Version number. */
681+
__( 'Ability %1$s is <strong>deprecated</strong> since version %2$s with no alternative available.' ),
682+
$ability_name,
683+
$version
684+
);
685+
} else {
686+
$message = sprintf(
687+
/* translators: %s: Ability name. */
688+
__( 'Ability %s is <strong>deprecated</strong> with no alternative available.' ),
689+
$ability_name
690+
);
691+
}
692+
}
693+
} else {
694+
if ( $replacement ) {
695+
if ( $version ) {
696+
$message = sprintf(
697+
'Ability %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.',
698+
$ability_name,
699+
$version,
700+
$replacement
701+
);
702+
} else {
703+
$message = sprintf(
704+
'Ability %1$s is <strong>deprecated</strong>! Use %2$s instead.',
705+
$ability_name,
706+
$replacement
707+
);
708+
}
709+
} else {
710+
if ( $version ) {
711+
$message = sprintf(
712+
'Ability %1$s is <strong>deprecated</strong> since version %2$s with no alternative available.',
713+
$ability_name,
714+
$version
715+
);
716+
} else {
717+
$message = sprintf(
718+
'Ability %s is <strong>deprecated</strong> with no alternative available.',
719+
$ability_name
720+
);
721+
}
722+
}
723+
}
724+
725+
wp_trigger_error( '', $message, E_USER_DEPRECATED );
726+
}
727+
}

0 commit comments

Comments
 (0)