true, self::CAPABILITY => true, self::CONTENT_CAPABILITY => true, 'edit_posts' => true, ) ); } } /** * The role => capabilities the plugin grants, additively. Full access * for Administrator/SEO-менеджер; content-only access for Editor and * Author (their own posts, scoped by edit_post at the REST layer). * * @return array Role slug => capabilities to add. */ public static function capability_map(): array { return array( 'administrator' => array( self::CAPABILITY, self::CONTENT_CAPABILITY ), 'seo_manager' => array( self::CAPABILITY, self::CONTENT_CAPABILITY ), 'editor' => array( self::CONTENT_CAPABILITY ), 'author' => array( self::CONTENT_CAPABILITY ), ); } /** * Additively grants the plugin's capabilities to their roles. Uses * WP_Role::add_cap, which is idempotent and never removes an existing * capability — so re-running never revokes access, and a site owner * who has hand-tuned role assignments keeps them. Runs on activation * and, via maybe_upgrade_caps(), once per CAPS_VERSION bump on sites * that predate a change. */ public static function assign_capabilities() { foreach ( self::capability_map() as $role_slug => $capabilities ) { $role = get_role( $role_slug ); if ( ! $role ) { continue; } foreach ( $capabilities as $capability ) { if ( ! $role->has_cap( $capability ) ) { $role->add_cap( $capability ); } } } update_option( self::CAPS_VERSION_OPTION, self::CAPS_VERSION ); } /** * Re-applies the additive capability grants once per CAPS_VERSION on * already-active sites (activation only fires once, and existing sites * never re-run activate()). Hooked to plugins_loaded. Safe to call on * every request — it no-ops when the stored revision is current. */ public static function maybe_upgrade_caps() { if ( get_option( self::CAPS_VERSION_OPTION ) === self::CAPS_VERSION ) { return; } self::assign_capabilities(); } /** * Seeds the plugin's default settings option if it doesn't already exist. */ protected static function set_default_options() { add_option( 'abds_seo_settings', array( 'migration_completed' => false, 'ai_provider' => '', 'ai_api_key_encrypted' => '', 'keep_data_on_uninstall' => true, ) ); } }