Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
58 / 58 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| VideoPress_Abilities | |
100.00% |
58 / 58 |
|
100.00% |
6 / 6 |
9 | |
100.00% |
1 / 1 |
| init | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| get_category_slug | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_category_definition | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| get_abilities | |
100.00% |
42 / 42 |
|
100.00% |
1 / 1 |
2 | |||
| can_manage_settings | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_settings | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * VideoPress abilities registration. |
| 4 | * |
| 5 | * @package automattic/jetpack-videopress |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\VideoPress\Abilities; |
| 9 | |
| 10 | use Automattic\Jetpack\VideoPress\Data; |
| 11 | use Automattic\Jetpack\WP_Abilities\Registrar; |
| 12 | |
| 13 | /** |
| 14 | * Registers VideoPress management abilities. |
| 15 | */ |
| 16 | class VideoPress_Abilities extends Registrar { |
| 17 | /** |
| 18 | * Initializes registration for sites in the VideoPress pilot. |
| 19 | * |
| 20 | * @return void |
| 21 | */ |
| 22 | public static function init() { |
| 23 | if ( get_option( 'videopress_abilities_enabled', false ) ) { |
| 24 | parent::init(); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * {@inheritDoc} |
| 30 | */ |
| 31 | public static function get_category_slug(): string { |
| 32 | return 'videopress'; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * {@inheritDoc} |
| 37 | */ |
| 38 | public static function get_category_definition(): array { |
| 39 | return array( |
| 40 | 'label' => 'VideoPress', |
| 41 | 'description' => __( 'Abilities provided by VideoPress.', 'jetpack-videopress-pkg' ), |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * {@inheritDoc} |
| 47 | */ |
| 48 | public static function get_abilities(): array { |
| 49 | $properties = array(); |
| 50 | foreach ( array( |
| 51 | 'videopress_videos_private_for_site', |
| 52 | 'videopress_auto_subtitles_disabled', |
| 53 | 'videopress_player_preload_disabled', |
| 54 | 'videopress_inline_player_enabled', |
| 55 | 'videopress_share_menu_disabled', |
| 56 | 'site_is_private', |
| 57 | ) as $setting ) { |
| 58 | $properties[ $setting ] = array( 'type' => 'boolean' ); |
| 59 | } |
| 60 | $properties['site_type'] = array( |
| 61 | 'type' => 'string', |
| 62 | 'enum' => array( 'jetpack', 'atomic', 'simple' ), |
| 63 | ); |
| 64 | |
| 65 | return array( |
| 66 | 'videopress/get-settings' => array( |
| 67 | 'label' => __( 'Get VideoPress settings', 'jetpack-videopress-pkg' ), |
| 68 | 'description' => __( 'Return effective VideoPress site settings and host type. This does not change settings or require an active VideoPress module.', 'jetpack-videopress-pkg' ), |
| 69 | 'input_schema' => array( |
| 70 | 'type' => 'object', |
| 71 | 'default' => array(), |
| 72 | 'additionalProperties' => false, |
| 73 | ), |
| 74 | 'output_schema' => array( |
| 75 | 'type' => 'object', |
| 76 | 'properties' => $properties, |
| 77 | 'required' => array_keys( $properties ), |
| 78 | 'additionalProperties' => false, |
| 79 | ), |
| 80 | 'permission_callback' => array( __CLASS__, 'can_manage_settings' ), |
| 81 | 'execute_callback' => array( __CLASS__, 'get_settings' ), |
| 82 | 'meta' => array( |
| 83 | 'show_in_rest' => true, |
| 84 | 'mcp' => array( 'public' => true ), |
| 85 | 'annotations' => array( |
| 86 | 'readonly' => true, |
| 87 | 'destructive' => false, |
| 88 | 'idempotent' => true, |
| 89 | ), |
| 90 | ), |
| 91 | ), |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Checks the same capability as the settings REST endpoints. |
| 97 | * |
| 98 | * @return bool Whether the user can inspect settings. |
| 99 | */ |
| 100 | public static function can_manage_settings(): bool { |
| 101 | return current_user_can( 'manage_options' ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns effective settings without exposing connection credentials. |
| 106 | * |
| 107 | * @param array|null $input Unused zero-argument input. |
| 108 | * @return array|\WP_Error Settings or a permission error. |
| 109 | */ |
| 110 | public static function get_settings( $input = null ) { |
| 111 | unset( $input ); |
| 112 | if ( ! self::can_manage_settings() ) { |
| 113 | return new \WP_Error( |
| 114 | 'videopress_forbidden', |
| 115 | __( 'You are not allowed to view VideoPress settings.', 'jetpack-videopress-pkg' ), |
| 116 | array( 'status' => 403 ) |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | return Data::get_videopress_settings(); |
| 121 | } |
| 122 | } |