> */ private static $store = array(); /** * Register $name with core and, only when that call actually succeeds, record the resulting * object as this plugin's own. The store never holds anything but wp_register_ability()'s own * return value - never a caller-supplied object. * * @param string $name Ability name. * @param array $args wp_register_ability() args. * @return WP_Ability|null Whatever wp_register_ability() returns. */ public static function register( string $name, array $args ): ?WP_Ability { // R12-1: force our own trusted ability class, discarding whatever $args carried. This is // the only place that actually calls wp_register_ability(), so overriding here closes the // class-substitution route for every caller of this method - see the class docblock. $args['ability_class'] = AAFM_Rate_Limited_Ability::class; $registered = wp_register_ability( $name, $args ); if ( $registered instanceof WP_Ability ) { self::$store[ $name ] = WeakReference::create( $registered ); } return $registered; } /** * Read-only: the object this plugin's own registration produced for $name, or null if this * plugin never registered it OR that registration has since been unregistered and collected. * Self-invalidating: this dereferences whatever is CURRENTLY stored, so a later registration * that replaces $store[$name] - or core forgetting $name entirely - makes any earlier answer * stale on its own, with nothing else to reset. * * @param string $name Ability name. * @return WP_Ability|null */ public static function owned( string $name ): ?WP_Ability { $ref = self::$store[ $name ] ?? null; return $ref instanceof WeakReference ? $ref->get() : null; } }