Skip to content
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
10 changes: 1 addition & 9 deletions owa-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1117,15 +1117,7 @@ function getSitesFromOwa( $options = [] ) {
// 2.x: a Property holds the website's name, a Profile only "Observation Profile 1"
$property = $this->getOwaVersion() === 2 ? (string) ( $site['property_name'] ?? '' ) : '';

// a Property named after its domain says nothing the domain does not
if ( $property !== '' && owaWp\network::hostOf( $property ) === owaWp\network::hostOf( $domain ) ) {

$property = '';
}

$label = $property !== ''
? sprintf( '%s – %s (%s)', $property, $domain, $site_id )
: sprintf( '%s (%s)', $site_id, $domain );
$label = owaWp\rest::profileLabel( (string) $site_id, [ 'domain' => $domain, 'property_name' => $property ] );

$list[ $site_id ] = [ 'label' => $label, 'siteId' => (string) $site_id ];
}
Expand Down
14 changes: 14 additions & 0 deletions src/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,23 @@ public static function register( $plugin ) {
add_action( 'network_admin_menu', fn() => self::addNetworkMenu() );
}

// a Settings link beside Deactivate on the Plugins screens
$basename = plugin_basename( OWA_WP_PLUGIN_PATH . 'owa-plugin.php' );
add_filter( 'plugin_action_links_' . $basename, fn( $links ) => self::addSettingsLink( $links, admin_url( 'admin.php?page=' . self::SLUG ) ) );
add_filter( 'network_admin_plugin_action_links_' . $basename, fn( $links ) => self::addSettingsLink( $links, network_admin_url( 'admin.php?page=' . network::MENU_SLUG ) ) );

add_action( 'admin_enqueue_scripts', [ self::class, 'enqueue' ] );
}

/**
* @param array<string, string> $links
* @return array<string, string>
*/
public static function addSettingsLink( $links, $url ) {

return [ 'settings' => sprintf( '<a href="%s">Settings</a>', esc_url( $url ) ) ] + (array) $links;
}

public static function addMenus( $plugin ) {

add_menu_page( 'OWA Settings', 'OWA', 'manage_options', self::SLUG, [ self::class, 'renderSettings' ], 'dashicons-chart-pie', 2 );
Expand Down
16 changes: 6 additions & 10 deletions src/rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,21 +315,17 @@ public static function sitesPayload( $plugin ) {
}

/**
* "Property – domain (site ID)", or "site ID (domain)" when the Property
* is unnamed or named after the domain, as the site's picker shows them.
* "site ID – domain (name)", or "site ID – domain" when it has no name, on
* the network page and in each site's picker.
*
* @param array{domain: string, property_name: string} $row
* @param array{domain: string, property_name?: string} $row
*/
public static function profileLabel( $id, array $row ) {

$property = (string) ( $row['property_name'] ?? '' );
$label = sprintf( '%s – %s', $id, $row['domain'] );
$name = (string) ( $row['property_name'] ?? '' );

if ( $property === '' || network::hostOf( $property ) === network::hostOf( $row['domain'] ) ) {

return sprintf( '%s (%s)', $id, $row['domain'] );
}

return sprintf( '%s – %s (%s)', $property, $row['domain'], $id );
return $name !== '' ? sprintf( '%s (%s)', $label, $name ) : $label;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/NetworkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ public function testTheMappingStartsOnTheExactMatchAndMarksWhatIsNew(): void {

$this->assertTrue( $profiles['OWA-www']['new'] );
$this->assertFalse( $profiles['OWA-main']['new'] );
$this->assertSame( 'PAA – https://www.peteradamsphoto.com (OWA-www)', $profiles['OWA-www']['label'] );
$this->assertSame( 'OWA-www – https://www.peteradamsphoto.com (PAA)', $profiles['OWA-www']['label'] );
}

public function testWithoutConfigurationTheTableShowsEachSitesOwnChoice(): void {
Expand Down
8 changes: 4 additions & 4 deletions tests/OwaV2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public function __construct( private object $client ) {}
public function createSites(): object { return $this->client; }
};

$this->assertSame( 'PhotoPress – https://photopress.io (OWA-1)', $plugin->getSitesFromOwa()['OWA-1']['label'] );
$this->assertSame( 'OWA-1 – https://photopress.io (PhotoPress)', $plugin->getSitesFromOwa()['OWA-1']['label'] );
}

public function testTheSettingsPageLinksToTheTrackingTagScreen(): void {
Expand All @@ -204,13 +204,13 @@ public function testTheSettingsPageLinksToTheTrackingTagScreen(): void {
$this->assertSame( '', \owaWp\rest::settingsPayload( $this->v2( [ 'owaVersion' => '1' ] ) )['tagSettingsUrl'], '1.x has no such screen.' );
}

public function testAPropertyNamedAfterItsDomainIsNotRepeated(): void {
public function testAnUnnamedProfileHasNoParentheses(): void {

$plugin = $this->v2( [ 'appPassword' => self::TOKEN, 'siteId' => '' ] );

$client = new class {
public function listSites(): array {
return [ 'OWA-1' => [ 'site_id' => 'OWA-1', 'domain' => 'https://wp.test', 'property_name' => 'https://wp.test' ] ];
return [ 'OWA-1' => [ 'site_id' => 'OWA-1', 'domain' => 'https://wp.test', 'property_name' => '' ] ];
}
public function getLastErrors(): array { return []; }
};
Expand All @@ -220,7 +220,7 @@ public function __construct( private object $client ) {}
public function createSites(): object { return $this->client; }
};

$this->assertSame( 'OWA-1 (https://wp.test)', $plugin->getSitesFromOwa()['OWA-1']['label'] );
$this->assertSame( 'OWA-1 – https://wp.test', $plugin->getSitesFromOwa()['OWA-1']['label'] );
}

public function testOnly2xFieldsAreShownIn2x(): void {
Expand Down
25 changes: 25 additions & 0 deletions tests/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace owaWp\Tests;

use Brain\Monkey\Actions;
use Brain\Monkey\Filters;
use Brain\Monkey\Functions;

class PluginTest extends TestCase {
Expand All @@ -22,6 +23,30 @@ public function testOtherRequestsLoadThePlugin(): void {
new \owaWp_plugin();
}

public function testThePluginsScreensLinkToTheSettings(): void {

Functions\when( 'is_multisite' )->justReturn( true );
Functions\when( 'plugin_basename' )->justReturn( 'open-web-analytics/owa-plugin.php' );
Functions\when( 'admin_url' )->alias( fn( $path ) => 'https://a.wp.test/wp-admin/' . $path );
Functions\when( 'network_admin_url' )->alias( fn( $path ) => 'https://wp.test/wp-admin/network/' . $path );

$links = [];

foreach ( [ 'site' => 'plugin_action_links_', 'network' => 'network_admin_plugin_action_links_' ] as $screen => $prefix ) {

Filters\expectAdded( $prefix . 'open-web-analytics/owa-plugin.php' )->once()->whenHappen( function ( $callback ) use ( &$links, $screen ) {

$links[ $screen ] = $callback( [ 'deactivate' => '<a href="#">Deactivate</a>' ] );
} );
}

\owaWp\admin::register( $this->makePlugin() );

$this->assertSame( [ 'settings', 'deactivate' ], array_keys( $links['site'] ) );
$this->assertSame( '<a href="https://a.wp.test/wp-admin/admin.php?page=owa-wordpress">Settings</a>', $links['site']['settings'] );
$this->assertSame( '<a href="https://wp.test/wp-admin/network/admin.php?page=owa-wordpress-network">Settings</a>', $links['network']['settings'] );
}

public function testAdminActionsAndFeedsAreNotTrackedEvenWithTheOldSettingsStored(): void {

Functions\when( 'get_option' )->justReturn( [
Expand Down
6 changes: 3 additions & 3 deletions tests/SitePickerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public function testReadsTheFlatRowsOwa114Returns(): void {
] );

$this->assertSame( [
'aaa' => [ 'label' => 'aaa (https://one.example.com)', 'siteId' => 'aaa' ],
'bbb' => [ 'label' => 'bbb (https://two.example.com)', 'siteId' => 'bbb' ],
'aaa' => [ 'label' => 'aaa – https://one.example.com', 'siteId' => 'aaa' ],
'bbb' => [ 'label' => 'bbb – https://two.example.com', 'siteId' => 'bbb' ],
], $plugin->getSitesFromOwa() );
}

Expand All @@ -56,7 +56,7 @@ public function testStillReadsTheNestedRowsOfServersBefore1101(): void {
] );

$this->assertSame(
[ 'aaa' => [ 'label' => 'aaa (https://one.example.com)', 'siteId' => 'aaa' ] ],
[ 'aaa' => [ 'label' => 'aaa – https://one.example.com', 'siteId' => 'aaa' ] ],
$plugin->getSitesFromOwa()
);
}
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ protected function setUp(): void {

Functions\stubs( [
'plugin_dir_path' => fn( $file ) => dirname( $file ) . '/',
'plugin_basename' => fn( $file ) => basename( dirname( $file ) ) . '/' . basename( $file ),
'wp_doing_cron' => false,
'esc_url_raw' => fn( $url ) => $url,
'wp_parse_url' => fn( $url, $component = -1 ) => parse_url( $url, $component ),
Expand Down
17 changes: 13 additions & 4 deletions tests/e2e/multisite.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test( 'the network configures each site\'s Profile', async ( { page, browser },
// b.wp.test has none, so it keeps its dropdown and has no switch
await expect( notice( page, 'Settings saved.' ) ).toBeVisible();
await expect( row( page, 'wp.test' ) ).toContainText( main );
await expect( row( page, 'a.wp.test' ) ).toContainText( `Site A – https://a.wp.test (${ siteA })` );
await expect( row( page, 'a.wp.test' ) ).toContainText( `${ siteA } – https://a.wp.test (Site A)` );
await expect( site( page, 'a.wp.test' ) ).toHaveCount( 0 );
await expect( edit( page, 'a.wp.test' ) ).toBeVisible();
await expect( tracking( page, 'a.wp.test' ) ).toBeVisible();
Expand Down Expand Up @@ -72,7 +72,7 @@ test( 'the network configures each site\'s Profile', async ( { page, browser },
await save.click();
await expect( notice( page, 'Settings saved.' ) ).toBeVisible();
// b.wp.test now has a saved Profile: text, Edit and its switch, on
await expect( row( page, 'b.wp.test' ) ).toContainText( `Site B – https://b.wp.test (${ siteB })` );
await expect( row( page, 'b.wp.test' ) ).toContainText( `${ siteB } – https://b.wp.test (Site B)` );
await expect( site( page, 'b.wp.test' ) ).toHaveCount( 0 );
await expect( tracking( page, 'b.wp.test' ) ).toBeChecked();
// c.wp.test was saved without a Profile: no longer New, still a dropdown and no switch
Expand All @@ -91,7 +91,7 @@ test( 'the network configures each site\'s Profile', async ( { page, browser },
await expect( notice( page, 'Settings saved.' ) ).toBeVisible();
await expect( tracking( page, 'a.wp.test' ) ).not.toBeChecked();
await expect( row( page, 'a.wp.test' ) ).toHaveClass( /is-disabled/ );
await expect( row( page, 'a.wp.test' ) ).toContainText( `Site A – https://a.wp.test (${ siteA })` );
await expect( row( page, 'a.wp.test' ) ).toContainText( `${ siteA } – https://a.wp.test (Site A)` );
await shot( page, testInfo, '05b-site-switched-off' );
await page.reload();
await expect( tracking( page, 'a.wp.test' ) ).not.toBeChecked();
Expand All @@ -102,7 +102,7 @@ test( 'the network configures each site\'s Profile', async ( { page, browser },
await expect( page.getByLabel( 'Observation Profile', { exact: true } ) ).toBeDisabled();
await expect( page.getByLabel( 'Observation Profile', { exact: true } ) ).toHaveValue( siteA );
// labelled from OWA's list, though the network has tracking off here
await expect( page.getByLabel( 'Observation Profile', { exact: true } ).locator( 'option:checked' ) ).toHaveText( /^Site A – https:\/\/a\.wp\.test/ );
await expect( page.getByLabel( 'Observation Profile', { exact: true } ).locator( 'option:checked' ) ).toHaveText( `${ siteA } – https://a.wp.test (Site A)` );
await expect( page.getByLabel( 'OWA Endpoint' ) ).toHaveCount( 0 );
await expect( page.getByLabel( 'Application Password' ) ).toHaveCount( 0 );
await expect( page.getByLabel( '2.x (beta)' ) ).toHaveCount( 0 );
Expand Down Expand Up @@ -146,3 +146,12 @@ test( 'each site picks from the Profiles sharing its domain', async ( { page },
await shot( page, testInfo, '08-site-choice' );
await docShot( page, 5 );
} );

test( 'the network Plugins screen links to the network settings', async ( { page } ) => {

await login( page );
await page.goto( 'http://wp.test/wp-admin/network/plugins.php' );
await page.locator( 'tr[data-plugin="owa-wordpress/owa-plugin.php"]' ).getByRole( 'link', { name: 'Settings', exact: true } ).click();
await expect( page ).toHaveURL( NETWORK_PAGE );
await expect( page.getByRole( 'heading', { name: 'OWA Network Settings' } ) ).toBeVisible();
} );
9 changes: 9 additions & 0 deletions tests/e2e/single.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,12 @@ test( 'switching to 2.x shows its fields, saves, and tags pages with the bundle'
expect( ( await bundle ).status() ).toBe( 200 );
expect( await visitor.content() ).not.toContain( 'owa.tracker.js' );
} );

test( 'the Plugins screen links to the settings', async ( { page } ) => {

await login( page );
await page.goto( '/wp-admin/plugins.php' );
await page.locator( 'tr[data-plugin="owa-wordpress/owa-plugin.php"]' ).getByRole( 'link', { name: 'Settings', exact: true } ).click();
await expect( page ).toHaveURL( /\/wp-admin\/admin\.php\?page=owa-wordpress$/ );
await expect( page.getByRole( 'heading', { name: 'OWA Settings' } ) ).toBeVisible();
} );
8 changes: 4 additions & 4 deletions tests/js/mapping.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { test, expect } from 'vitest';
import { hasSavedProfile, initialMapping, initialSwitches, isEditing, mappingPayload, switchesPayload, profileLabel, profilesFor, savedLabel } from '../../js/lib/mapping';

const PROFILES = [
{ id: 'OWA-io', label: 'PhotoPress – https://photopress.io (OWA-io)', domain: 'https://photopress.io', baseDomain: 'photopress.io', new: false },
{ id: 'OWA-arch', label: 'Archive – https://archive.peteradamsphoto.com (OWA-arch)', domain: 'https://archive.peteradamsphoto.com', baseDomain: 'peteradamsphoto.com', new: false },
{ id: 'OWA-www', label: 'PAA – https://www.peteradamsphoto.com (OWA-www)', domain: 'https://www.peteradamsphoto.com', baseDomain: 'peteradamsphoto.com', new: true },
{ id: 'OWA-io', label: 'OWA-io – https://photopress.io (PhotoPress)', domain: 'https://photopress.io', baseDomain: 'photopress.io', new: false },
{ id: 'OWA-arch', label: 'OWA-arch – https://archive.peteradamsphoto.com (Archive)', domain: 'https://archive.peteradamsphoto.com', baseDomain: 'peteradamsphoto.com', new: false },
{ id: 'OWA-www', label: 'OWA-www – https://www.peteradamsphoto.com (PAA)', domain: 'https://www.peteradamsphoto.com', baseDomain: 'peteradamsphoto.com', new: true },
];

test( 'a Profile from the last refresh is marked new', () => {
expect( profileLabel( PROFILES[ 2 ] ) ).toBe( 'PAA – https://www.peteradamsphoto.com (OWA-www) — new' );
expect( profileLabel( PROFILES[ 2 ] ) ).toBe( 'OWA-www – https://www.peteradamsphoto.com (PAA) — new' );
expect( profileLabel( PROFILES[ 0 ] ) ).toBe( PROFILES[ 0 ].label );
} );

Expand Down
Loading