PHP code example of automattic / jetpack-wp-abilities

1. Go to this page and download the library: Download automattic/jetpack-wp-abilities library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

automattic / jetpack-wp-abilities example snippets


use Automattic\Jetpack\WP_Abilities\Registrar;

class My_Plugin_Abilities extends Registrar {

	public static function get_category_slug(): string {
		return 'my-plugin';
	}

	public static function get_category_definition(): array {
		return array(
			'label'       => 'My Plugin',
			'description' => __( 'Abilities for My Plugin.', 'my-plugin' ),
		);
	}

	public static function get_abilities(): array {
		return array(
			'my-plugin/get-things' => array(
				'label'               => __( 'List things', 'my-plugin' ),
				'description'         => __( 'Return the list of things the user can access.', 'my-plugin' ),
				'input_schema'        => array(
					'type'                 => 'object',
					'default'              => array(),
					'properties'           => array(),
					'additionalProperties' => false,
				),
				'execute_callback'    => array( __CLASS__, 'get_things' ),
				'permission_callback' => array( __CLASS__, 'can_read_things' ),
				'meta'                => array(
					'annotations'  => array(
						'readonly'    => true,
						'destructive' => false,
						'idempotent'  => true,
					),
					'show_in_rest' => true,
				),
			),
		);
	}

	public static function can_read_things(): bool {
		return current_user_can( 'read' );
	}

	public static function get_things( $input = null ) {
		return array( 'things' => array() );
	}
}

My_Plugin_Abilities::init();

// Enable Jetpack abilities registration on sites that have opted in.
add_filter(
	'jetpack_wp_abilities_enabled',
	static function ( bool $enabled ): bool {
		return (bool) get_option( 'my_abilities_rollout_enabled', false );
	}
);

// Globally enabled, but hold back a single ability until it's GA.
add_filter(
	'jetpack_wp_abilities_should_register',
	static function ( bool $enabled, string $type, string $slug ): bool {
		if ( 'ability' === $type && 'my-plugin/experimental-write' === $slug ) {
			return current_user_can( 'manage_options' );
		}
		return $enabled;
	},
	10,
	3
);