Skip to content
Open
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
22 changes: 17 additions & 5 deletions assets/js/components/UsersTable/RowActions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useDispatch } from '@wordpress/data';
/**
* Internal dependencies
*/
import { tempAccessHooks } from '../../data/utils';
import { STORE_NAME, UI_STORE_NAME } from '../../datastores/constants';

export default function RowActions({ user }) {
Expand Down Expand Up @@ -44,26 +45,37 @@ export default function RowActions({ user }) {
setUserForDeletion(user);
};

return (
<div className="tempuser-actions">
const actions = tempAccessHooks.applyFilters(
'tempAccess.rowActions',
[
<Button
icon="edit"
title={__('Edit', 'passwordless-temporary-login')}
isPressed={false}
onClick={onEdit}
/>
key={'row-action-edit'}
/>,
<Button
icon="trash"
title={__('Delete', 'passwordless-temporary-login')}
isPressed={false}
onClick={onDelete}
/>
key={'row-action-delete'}
/>,
<Button
icon="admin-links"
title={__('Copy link', 'passwordless-temporary-login')}
isPressed={false}
onClick={() => onCopyLink(user)}
/>
key={'row-action-copy-link'}
/>,
],
user
);

return (
<div className="tempuser-actions">
{actions.map((action) => ({ ...action }))}
</div>
);
}
7 changes: 7 additions & 0 deletions assets/js/data/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
*/
import invariant from 'invariant';

/**
* WordPress dependencies
*/
import { createHooks } from '@wordpress/hooks';

export const tempAccessHooks = createHooks();

/**
* Collects and combines multiple objects of similar shape.
*
Expand Down
13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"@wordpress/date": "^5.5.0",
"@wordpress/dom-ready": "^4.3.0",
"@wordpress/element": "^6.3.0",
"@wordpress/hooks": "^4.7.0",
"@wordpress/i18n": "^5.3.0",
"@wordpress/url": "^4.3.0",
"classnames": "^2.5.1",
Expand Down
25 changes: 24 additions & 1 deletion src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
namespace Ankit\TemporaryAccess;

use Ankit\TemporaryAccess\Interfaces\Container as ContainerInterface;
use Ankit\TemporaryAccess\REST\Manager;
use Ankit\TemporaryAccess\UI\Settings;
use Ankit\TemporaryAccess\UI\UsersTable;
use InvalidArgumentException;
use Pimple\Container as PimpleContainer;
use Ankit\TemporaryAccess\REST\TempUser;
use Ankit\TemporaryAccess\REST\Settings as SettingsEndpoint;

/**
* Class Container
Expand All @@ -25,7 +27,6 @@
*/
class Container implements ContainerInterface {


/**
* Pimple container.
*
Expand Down Expand Up @@ -103,6 +104,28 @@ public function define_services(): void {
return new TempUser( $c['user_manager'] );
};

/**
* Settings endpoint.
*
* @param PimpleContainer $c Pimple container object.
*
* @return TempUser
*/
$this->container['settings_endpoint'] = function ( PimpleContainer $c ) {
return new SettingsEndpoint();
};

/**
* REST API manager.
*
* @param PimpleContainer $c Pimple container object.
*
* @return Manager
*/
$this->container['rest_manager'] = function ( PimpleContainer $c ) {
return new Manager();
};

/**
* Settings service.
*
Expand Down
22 changes: 12 additions & 10 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use Ankit\TemporaryAccess\Interfaces\Container as ContainerInterface;
use Ankit\TemporaryAccess\Interfaces\UserManagement;
use Ankit\TemporaryAccess\REST\TempUser;
use Ankit\TemporaryAccess\REST\Manager;

/**
* Class Plugin
Expand Down Expand Up @@ -53,16 +53,16 @@ class Plugin {
/**
* User manager.
*
* @var UserManagement
* @var Manager
*/
private $user_manager;
private $rest_manager;

/**
* Endpoint service.
*
* @var TempUser
* User manager.
*
* @var UserManagement
*/
private $user_endpoint;
private $user_manager;

/**
* Authentication service.
Expand Down Expand Up @@ -103,17 +103,19 @@ public function run(): void {

$settings = $this->container->get( 'settings' );
$this->user_manager = $this->container->get( 'user_manager' );
$this->user_endpoint = $this->container->get( 'temp_user_endpoint' );
$this->rest_manager = $this->container->get( 'rest_manager' );
$this->authenticator = $this->container->get( 'authenticator' );
$users_table = $this->container->get( 'users_table' );

$this->authenticator->init();
$settings->init();
$users_table->init();

add_action( 'init', array( $this->user_manager, 'init' ) );
add_action( 'rest_api_init', array( $this->user_endpoint, 'register' ) );
add_action( 'rest_api_init', array( $this->rest_manager, 'register' ) );

add_action( 'tempaccess.user_authenticated', array( $this->user_manager, 'post_login_actions' ) );
add_action( 'init', array( $this->user_manager, 'init' ) );

add_action( 'init', array( $this, 'load_translations' ) );
}

Expand Down
97 changes: 97 additions & 0 deletions src/REST/Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* Base class for REST endpoints.
*/

declare(strict_types=1);

namespace Ankit\TemporaryAccess\REST;

use WP_REST_Server;

/**
* Class Base
*
* @package Ankit\TemporaryAccess\REST
*/

abstract class Base {

/**
* Namespace for the controller.
*/
const NAMESPACE = 'tempaccess/v1';

/**
* Register the routes for the objects of the controller.
*
* @return void
*/
abstract public function register(): void;

/**
* Register the routes for the objects of the controller.
*
* @param string $route Route.
* @param array $args Args.
*/
protected function register_route( string $route, array $args ) {
$default = array(
'permission_callback' => $this->get_permission_callback(),
'args' => $this->get_args(),
);

$route_args = wp_parse_args( $args, $default );

register_rest_route(
self::NAMESPACE,
$route,
$route_args
);
}

/**
* Get the namespace for the controller.
*
* @return string
*/
public function get_namespace(): string {
return self::NAMESPACE;
}

/**
* Get the version for the controller.
*
* @return string
*/
public function get_version(): string {
return '1';
}

/**
* Get the method for the controller.
*
* @return string
*/
public function get_method(): string {
return WP_REST_Server::READABLE;
}

/**
* Get the permission callback for the controller.
*
* @return string
*/
public function get_permission_callback(): bool {
return current_user_can( 'edit_users' );
}

/**
* Get the args for the controller.
*
* @return array
*/
public function get_args(): array {
return array();
}
}
56 changes: 56 additions & 0 deletions src/REST/Manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Manager class for REST endpoints.
*/

declare(strict_types=1);

namespace Ankit\TemporaryAccess\REST;

use Ankit\TemporaryAccess\Container;
use Ankit\TemporaryAccess\REST\Base;
use function Ankit\TemporaryAccess\plugin;

/**
* Class Manager
*
* @package Ankit\TemporaryAccess\REST
*/
class Manager {

/**
* Container instance.
*
* @var Container
*/
private $container;

/**
* List of REST endpoints.
*
* @var Base[]
*/
private $endpoints;

/**
* Manager constructor.
*
* @param array $endpoints List of REST endpoints.
*/
public function __construct() {
$this->container = plugin()->container();
$this->endpoints = [
'tempuser' => $this->container->get( 'temp_user_endpoint' ),
'settings' => $this->container->get( 'settings_endpoint' ),
];
}

/**
* Register the REST endpoints.
*/
public function register() {
foreach ( $this->endpoints as $endpoint ) {
$endpoint->register();
}
}
}
Loading