Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split out plugin actions links logic #401

Merged
merged 1 commit into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/UI/class-plugins-actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Parsely class
*
* @package Parsely
* @since 2.6.0
*/

namespace Parsely\UI;

use Parsely;

/**
* User Interface changes for the plugins actions.
*
* @since 2.6.0
*/
class Plugins_Actions {

/**
* Register action and filter hook callbacks.
*/
public function run() {
add_filter( 'plugin_action_links_' . PARSELY_PLUGIN_BASENAME, array( $this, 'add_plugin_meta_links' ) );
}

/**
* Adds a 'Settings' action link to the Plugins screen in WP admin.
*
* @param array $actions An array of plugin action links. By default, this can include 'activate',
* 'deactivate', and 'delete'. With Multisite active this can also include
* 'network_active' and 'network_only' items.
*/
public function add_plugin_meta_links( $actions ) {
$settings_link = sprintf(
'<a href="%s">%s</a>',
esc_url( Parsely::get_settings_url() ),
esc_html__( 'Settings', 'wp-parsely' )
);

$actions['settings'] = $settings_link;

return $actions;
}
}
21 changes: 2 additions & 19 deletions src/class-parsely.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,6 @@ public function run() {
// display warning when plugin hasn't been configured.
add_action( 'admin_footer', array( $this, 'display_admin_warning' ) );

add_filter(
'plugin_action_links_' . PARSELY_PLUGIN_BASENAME,
array( $this, 'add_plugin_meta_links' )
);

// phpcs:ignore WordPress.WP.CronInterval.CronSchedulesInterval
add_filter( 'cron_schedules', array( $this, 'wpparsely_add_cron_interval' ) );

Expand Down Expand Up @@ -777,18 +772,6 @@ public function print_optional_settings() {
// need to now.
}

/**
* Adds a 'Settings' link to the Plugins screen in WP admin
*
* @category Function
* @package Parsely
* @param array $links The links to add.
*/
public function add_plugin_meta_links( $links ) {
array_unshift( $links, '<a href="' . esc_url( $this->get_settings_url() ) . '">' . __( 'Settings', 'wp-parsely' ) . '</a>' );
return $links;
}

/**
* Display the admin warning if needed
*
Expand All @@ -803,7 +786,7 @@ public function display_admin_warning() {
$message = sprintf(
/* translators: %s: Plugin settings page URL */
__( '<strong>The Parse.ly plugin is not active.</strong> You need to <a href="%s">provide your Parse.ly Dash Site ID</a> before things get cooking.', 'wp-parsely' ),
esc_url( $this->get_settings_url() )
esc_url( self::get_settings_url() )
);
?>
<div id="message" class="error"><p><?php echo wp_kses_post( $message ); ?></p></div>
Expand Down Expand Up @@ -1972,7 +1955,7 @@ public function get_clean_parsely_page_value( $val ) {
/**
* Get the URL of the plugin settings page
*/
private function get_settings_url() {
public static function get_settings_url() {
return admin_url( 'options-general.php?page=' . self::MENU_SLUG );
}

Expand Down
45 changes: 45 additions & 0 deletions tests/UI/PluginsActionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* UI Tests for the plugin actions
*
* @package Parsely\Tests\UI
*/

namespace Parsely\Tests\UI;

use Parsely\Tests\TestCase;
use Parsely\UI\Plugins_Actions;

/**
* UI Tests for the plugin screen.
*/
final class PluginsActionsTest extends TestCase {
/**
* Check that plugins screen will add a hook to change the plugin action links.
*
* @covers \Parsely\UI\Plugins_Actions::run
* @group ui
*/
public function test_plugins_screen_has_filter_to_add_a_settings_action_link() {
$plugins_screen = new Plugins_Actions();
$plugins_screen->run();

self::assertNotFalse( has_filter( 'plugin_action_links_' . PARSELY_PLUGIN_BASENAME, array( $plugins_screen, 'add_plugin_meta_links' ) ) );
}

/**
* Check that plugins screen will add a hook to change the plugin action links.
*
* @covers \Parsely\UI\Plugins_Actions::run
* @covers \Parsely\UI\Plugins_Actions::add_plugin_meta_links
* @uses \Parsely::get_settings_url
* @group ui
*/
public function test_plugins_screen_adds_a_settings_action_link() {
$actions = array();
$plugins_screen = new Plugins_Actions();
$actions = $plugins_screen->add_plugin_meta_links( $actions );

self::assertCount( 1, $actions );
}
}
10 changes: 10 additions & 0 deletions wp-parsely.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@
$GLOBALS['parsely'] = new Parsely();
$GLOBALS['parsely']->run();

// Until auto-loading happens, we need to include this file for tests as well.
require PARSELY_PLUGIN_DIR . 'src/UI/class-plugins-actions.php';
add_action(
'admin_init',
function() {
$GLOBALS['parsely_ui_plugins_actions'] = new Parsely\UI\Plugins_Actions();
$GLOBALS['parsely_ui_plugins_actions']->run();
}
);

require PARSELY_PLUGIN_DIR . 'src/class-parsely-recommended-widget.php';

add_action( 'widgets_init', 'parsely_recommended_widget_register' );
Expand Down