Skip to content

Add Settings link to plugin row actions#12

Merged
felixarntz merged 3 commits intofelixarntz:mainfrom
westonruter:add/plugin-action-settings-link
Nov 4, 2024
Merged

Add Settings link to plugin row actions#12
felixarntz merged 3 commits intofelixarntz:mainfrom
westonruter:add/plugin-action-settings-link

Conversation

@westonruter
Copy link
Copy Markdown
Contributor

This adds a Settings link to the plugin row actions as is common for plugins to do to avoid having to locate the Settings in the admin menu.

Before After
image image

public function __construct( Script_Registry $script_registry, Style_Registry $style_registry ) {
parent::__construct();

add_filter( 'plugin_action_links_' . AI_SERVICES_MAIN_FILE, array( $this, 'add_settings_action_link' ) );
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize you probably don't want to add the plugin_action_links_* filter in the constructor, but it doesn't seem like the Abstract_Admin_Page class has the required interface for adding plugin action links.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes most sense to implement this in its own small class, e.g. Plugin_Action_Link, because it's not part of the settings page. Also, this way:

  • It can take the Settings_Page instance as constructor parameter and read the data from there.
  • It can also take the Plugin_Env instance as constructor parameter and thus call the main_file() method instead of introducing a constant for it.


$settings_link = sprintf(
'<a href="%1$s">%2$s</a>',
esc_url( add_query_arg( 'page', $this->get_slug(), admin_url( 'options-general.php' ) ) ),
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better if options-general.php was not duplicated here with:

$this->container['admin_settings_menu'] = static function () {
return new Admin_Menu( 'options-general.php' );
};

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now we may need to go with the duplicate string. Though it would make sense to add a public get_slug() method to the Admin_Menu class (which comes from https://github.com/felixarntz/wp-oop-plugin-lib). This way we could read it from there. That change would need to be made upstream first though.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Owner

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @westonruter for the PR! Some feedback below, I think this will need a minor refactoring in its own small class.

public function __construct( Script_Registry $script_registry, Style_Registry $style_registry ) {
parent::__construct();

add_filter( 'plugin_action_links_' . AI_SERVICES_MAIN_FILE, array( $this, 'add_settings_action_link' ) );
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes most sense to implement this in its own small class, e.g. Plugin_Action_Link, because it's not part of the settings page. Also, this way:

  • It can take the Settings_Page instance as constructor parameter and read the data from there.
  • It can also take the Plugin_Env instance as constructor parameter and thus call the main_file() method instead of introducing a constant for it.


$settings_link = sprintf(
'<a href="%1$s">%2$s</a>',
esc_url( add_query_arg( 'page', $this->get_slug(), admin_url( 'options-general.php' ) ) ),
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now we may need to go with the duplicate string. Though it would make sense to add a public get_slug() method to the Admin_Menu class (which comes from https://github.com/felixarntz/wp-oop-plugin-lib). This way we could read it from there. That change would need to be made upstream first though.

return $links;
}

$settings_link = sprintf(
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before unconditionally returning this, this needs a capability check for ais_manage_services. Based on my above comment, this capability could also be read from the Settings_Page instance.

@felixarntz felixarntz added the enhancement Enhancement to an existing feature label Oct 25, 2024
…ter/ai-services into westonruter-add/plugin-action-settings-link
…ant classes for the link URL and capability check.
@felixarntz
Copy link
Copy Markdown
Owner

@westonruter I have updated the implementation locally to use a new Plugin_Action_Link class, as mentioned in my review. That integrates the logic you wrote a bit better into the plugin's architecture.

If you're okay with that, could you give me permission to push to your PR branch so that I can update this one?

Copy link
Copy Markdown
Owner

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@westonruter I've moved the logic to its own class in 975ba04. Thanks again!

@felixarntz felixarntz merged commit 87a149f into felixarntz:main Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Enhancement to an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants