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

WIP: 3.0.0 - new DomainQuery (in the style of WP_Query) #114

Draft
wants to merge 74 commits into
base: feature/3.0.0/reworked-ui
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
0818967
Added a message when there are no domains for a site.
cameronterry Jan 14, 2024
6d6a860
Got the beginnings of a Custom Table query class.
cameronterry Jan 14, 2024
43da406
Fixed a missing method comment.
cameronterry Jan 14, 2024
8813a1b
Removed the site references and meta.
cameronterry Jan 14, 2024
6f4209a
Updated more typos.
cameronterry Jan 14, 2024
c525965
Implemented a non-cached version of get_records() - doesn't work yet.
cameronterry Jan 14, 2024
4f7d6ef
Renamed and rearranged the abstract methods.
cameronterry Jan 14, 2024
73f4f64
Got the first version of the Custom Table created.
cameronterry Jan 21, 2024
63323a1
Fixed a handful of issues with CustomTable and renamed the class.
cameronterry Jan 21, 2024
9b8242e
Implementing the domain mapping table with the new CustomTable class.
cameronterry Jan 21, 2024
20fe4c3
Moved Restricted Domain custom table to the new class.
cameronterry Jan 21, 2024
ba38e68
Changed the installer to use the new CustomTable implementation.
cameronterry Jan 21, 2024
cebecab
Renamed the Custom Query class.
cameronterry Jan 28, 2024
7d89d3d
Changed the accessibility of get tablename.
cameronterry Jan 28, 2024
34d6747
Changed the access for the get_columns() method.
cameronterry Jan 28, 2024
1967c9c
Got a basic version of CustomQuery working.
cameronterry Jan 28, 2024
9ea9406
Changed the method accessors for DomainMapping/RestricteDomain.
cameronterry Jan 28, 2024
81ed766
Changed primary key method to be public.
cameronterry Jan 28, 2024
1726dd6
Got a version of CustomTableQuery working.
cameronterry Jan 28, 2024
e1614b7
Added support for __in and __not_in fields.
cameronterry Jan 28, 2024
84c0b29
Defined the domain mapping fields which are queryable.
cameronterry Jan 28, 2024
9d30461
Fixed a notice.
cameronterry Jan 28, 2024
05fa55c
Fixed some dynamic properties.
cameronterry May 10, 2024
08806f5
Reworked the installer so the table classes can be defined without th…
cameronterry May 11, 2024
b3566b3
Revert "Fixed some dynamic properties."
cameronterry May 11, 2024
c6164d8
Reworked the CustomTableQuery to accept query args array.
cameronterry May 11, 2024
70e8043
Changed records_per_page to number.
cameronterry May 11, 2024
47a369c
Added the map for the custom table records.
cameronterry May 11, 2024
d574a08
Added the start of the DomainQuery object.
cameronterry May 11, 2024
efa6ffd
Replaced the raw wpdb queries with the new DomainQuery object.
cameronterry May 11, 2024
04cca22
Added a couple of check methods.
cameronterry May 11, 2024
295bdbb
Added a get_by_domain() method.
cameronterry May 11, 2024
559b54d
Refactored args check into a separate method.
cameronterry May 11, 2024
414ea36
Beefed up the sanitization of the args/data for custom table.
cameronterry May 11, 2024
8751eb5
Implemented an update method.
cameronterry May 11, 2024
151784e
Fixed a few issues with checks and handling of domain before changes.
cameronterry May 11, 2024
9aeb4b7
Added a root update method for the CustomTable.
cameronterry May 11, 2024
07f7b50
Check for a sanitizer property before seeing if it is callable.
cameronterry May 11, 2024
78b48f5
Updated the Primary Domain set to use the new APIs.
cameronterry May 11, 2024
5780074
Refactored the set() / unset() methods to use a single helper method.
cameronterry May 12, 2024
89d491b
Added the actions for primary set/unset.
cameronterry May 12, 2024
9fff97f
Implemented the domain delete.
cameronterry May 12, 2024
0a6171f
Implemented the custom table delete.
cameronterry May 12, 2024
e74e27d
Added the primary key check.
cameronterry May 12, 2024
b160720
Implemented the add() method to the Domain Mapping.
cameronterry May 12, 2024
3c6f8f6
Forgot to rename the check method calls to check_fqdn().
cameronterry May 12, 2024
8710503
Changed to return a WP_Error() on delete when the domain cannot be fo…
cameronterry May 12, 2024
a810967
Added the ability to disable the check on the primary key - for examp…
cameronterry May 12, 2024
5226e2b
Fixed an issue finding a domain if it was disable/not active.
cameronterry May 12, 2024
bc8f5f6
Reworked the Domains CLI to use the new DomainMapping/DomainQuery setup.
cameronterry May 12, 2024
65a4fc4
Updated Sunrise to use the new DomainQuery.
cameronterry May 12, 2024
341745a
Switched the REST API to use DomainMapping/DomainQuery.
cameronterry May 12, 2024
af2948b
Added a helper method to get the primary domain for a site.
cameronterry May 12, 2024
e01b225
Updated the mapping to use the new DomainQuery logic.
cameronterry May 12, 2024
2435e5b
Switched Media and Redirect to use the new DomainQuery.
cameronterry May 12, 2024
2b2947f
Added a class stub for Restricted Domains query.
cameronterry May 27, 2024
d12a980
Implemented the get record.
cameronterry May 27, 2024
8cd2281
Replaced the restrict domain check with new restricted domain query.
cameronterry May 27, 2024
ddf7923
Moved the check domain into the Helper class to be more reusable.
cameronterry May 27, 2024
3fe5033
Implemented the add method for the RestrictedDomain data class.
cameronterry May 27, 2024
888af70
Fixed an issue with the add restricted domain.
cameronterry May 27, 2024
6fb71af
Updated the CLI for restricted domains for add and list.
cameronterry May 27, 2024
aa03ce0
Added a helper method to get a restricted domain ID by searching for …
cameronterry May 27, 2024
d1e65e5
Implemented the restricted domain remove CLI.
cameronterry May 27, 2024
921eb9e
Added a check to prevent duplicate restricted domains.
cameronterry May 27, 2024
a11f17b
Updated the REST API to use the new data RestrictedDomain and Restric…
cameronterry May 27, 2024
42d2b61
Remove the unused class.
cameronterry May 27, 2024
c3b6fb3
Added caching to the CustomQuery and CustomTable classes.
cameronterry May 27, 2024
b69e41f
Added the prime caching functionality.
cameronterry May 27, 2024
1111d3b
Updated the NPM dependencies.
cameronterry Oct 22, 2024
4498029
Added a large number count for Domains.
cameronterry Oct 22, 2024
ddad55a
Moved the prepare tables into the Sunrise class, rather than the drop…
cameronterry Oct 22, 2024
bd6d413
Fixed a formatting issue.
cameronterry Oct 22, 2024
428d8ba
Added support for Script Modules.
cameronterry Oct 22, 2024
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
4 changes: 4 additions & 0 deletions assets/css/DomainMapping.css
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,9 @@
}
}
}

& .dmp__domain-table-empty {
text-align: center;
}
}
}
7 changes: 7 additions & 0 deletions assets/js/components/domain/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ class Table extends Component {
</tr>
);
} ) }
{ 0 === domains.length && (
<tr>
<td colSpan={ displayFields.length }>
<p className="dmp__domain-table-empty">{ __( 'No domains have been added for this site.', 'darkmatterplugin' ) }</p>
</td>
</tr>
) }
</tbody>
);
}
Expand Down
109 changes: 48 additions & 61 deletions includes/classes/DomainMapping/CLI/Domains.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

namespace DarkMatter\DomainMapping\CLI;

use DarkMatter\DomainMapping\Manager;
use DarkMatter\DomainMapping\Data\Domain;
use DarkMatter\DomainMapping\Data\DomainMapping;
use DarkMatter\DomainMapping\Data\DomainQuery;
use WP_CLI;
use WP_CLI_Command;

// phpcs:disable PHPCompatibility.Keywords.ForbiddenNames.listFound -- Changing CLI for list would introduce backward compatibility (2.x.x) problems for pre-existing users.

/**
* Class Domains
*
Expand Down Expand Up @@ -72,7 +72,7 @@ public function add( $args, $assoc_args ) {

$fqdn = $args[0];

$opts = wp_parse_args(
$assoc_args = wp_parse_args(
$assoc_args,
[
'disable' => false,
Expand All @@ -83,14 +83,12 @@ public function add( $args, $assoc_args ) {
]
);

$type = $this->check_type_opt( $opts['type'] );
$assoc_args['blog_id'] = get_current_blog_id();
$assoc_args['domain'] = $fqdn;

/**
* Add the domain.
*/
$db = Manager\Domain::instance();
$result = $db->add( $fqdn, $opts['primary'], $opts['https'], $opts['force'], ! $opts['disable'], $type );
$data = new DomainMapping();

$result = $data->add( $assoc_args, $assoc_args['force'] );
if ( is_wp_error( $result ) ) {
$error_msg = $result->get_error_message();

Expand All @@ -104,30 +102,6 @@ public function add( $args, $assoc_args ) {
WP_CLI::success( $fqdn . __( ': was added.', 'dark-matter' ) );
}

/**
* Checks to ensure the value of type is valid and useable.
*
* @param string $type Type value to be checked.
* @return integer Domain type.
*
* @since 2.2.0
*/
private function check_type_opt( $type = '' ) {
/**
* Handle the Media flag.
*/
$domain_types = [
'main' => DM_DOMAIN_TYPE_MAIN,
'media' => DM_DOMAIN_TYPE_MEDIA,
];

if ( array_key_exists( strtolower( $type ), $domain_types ) ) {
return $domain_types[ $type ];
}

return DM_DOMAIN_TYPE_MAIN;
}

/**
* Include this CLI amongst the others.
*
Expand Down Expand Up @@ -165,12 +139,14 @@ public static function define() {
*
* wp darkmatter domain list
*
* @subcommand list
*
* @since 2.0.0
*
* @param array $args CLI args.
* @param array $assoc_args CLI args maintaining the flag names from the terminal.
*/
public function list( $args, $assoc_args ) {
public function _list( $args, $assoc_args ) {
/**
* Handle and validate the format flag if provided.
*/
Expand All @@ -187,8 +163,9 @@ public function list( $args, $assoc_args ) {
}

if ( $opts['primary'] ) {
$db = Manager\Primary::instance();
$domains = $db->get_all();
$query_args = [
'is_primary' => true,
];
} else {
/**
* Retrieve the current Blog ID. However this will be set to null if
Expand All @@ -200,10 +177,14 @@ public function list( $args, $assoc_args ) {
$site_id = null;
}

$db = Manager\Domain::instance();
$domains = $db->get_domains( $site_id );
$query_args = [
'blog_site' => $site_id,
'number' => 500, // Large number.
];
}

$query = new DomainQuery( $query_args );

/**
* Filter out and format the columns and values appropriately.
*/
Expand Down Expand Up @@ -234,7 +215,7 @@ function ( $domain ) {

return $columns;
},
$domains
$query->records
);

/**
Expand Down Expand Up @@ -304,13 +285,15 @@ public function remove( $args, $assoc_args ) {
]
);

$db = Manager\Domain::instance();
$query = new DomainQuery();
$domain = $query->get_by_domain( $fqdn );
if ( ! $domain instanceof Domain ) {
WP_CLI::error( __( 'Domain cannot be found.', 'dark-matter' ) );
}

/**
* Remove the domain.
*/
$result = $db->delete( $fqdn, $opts['force'] );
$data = new DomainMapping();

$result = $data->delete( $domain->id, $opts['force'] );
if ( is_wp_error( $result ) ) {
$error_msg = $result->get_error_message();

Expand Down Expand Up @@ -387,8 +370,12 @@ public function set( $args, $assoc_args ) {

$fqdn = $args[0];

$db = Manager\Domain::instance();
$domain_before = $db->get( $fqdn );
$query = new DomainQuery();

$domain_before = $query->get_by_domain( $fqdn );
if ( ! $domain_before instanceof Domain ) {
WP_CLI::error( __( 'Domain cannot be found.', 'dark-matter' ) );
}

$opts = wp_parse_args(
$assoc_args,
Expand All @@ -400,6 +387,7 @@ public function set( $args, $assoc_args ) {
'use-https' => true,
'primary' => null,
'secondary' => null,
'type' => null,
]
);

Expand Down Expand Up @@ -449,22 +437,21 @@ public function set( $args, $assoc_args ) {
$active = false;
}

/**
* If the type is specified, then validate it to ensure it is correct.
*/
$type = null;
if ( ! empty( $opts['type'] ) ) {
$type = $this->check_type_opt( $opts['type'] );
}
$type = $opts['type'];

/**
* Update the records.
*/
$result = $db->update( $fqdn, $is_primary, $is_https, $opts['force'], $active, $type );
$data = new DomainMapping();

/**
* Handle the output for errors and success.
*/
$result = $data->update(
[
'id' => $domain_before->id,
'active' => $active,
'domain' => $fqdn,
'is_primary' => $is_primary,
'is_https' => $is_https,
'type' => $type,
],
$opts['force']
);
if ( is_wp_error( $result ) ) {
$error_msg = $result->get_error_message();

Expand Down
56 changes: 41 additions & 15 deletions includes/classes/DomainMapping/CLI/Restricted.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

namespace DarkMatter\DomainMapping\CLI;

use \DarkMatter\DomainMapping\Manager;
use DarkMatter\DomainMapping\Data\RestrictedDomain;
use DarkMatter\DomainMapping\Data\RestrictedDomainQuery;
use WP_CLI;
use WP_CLI_Command;

Expand Down Expand Up @@ -46,14 +47,20 @@ public function add( $args, $assoc_args ) {

$fqdn = $args[0];

$restricted = Manager\Restricted::instance();
$result = $restricted->add( $fqdn );

$data = new RestrictedDomain();
$result = $data->add(
[
'domain' => $fqdn,
]
);
if ( is_wp_error( $result ) ) {
WP_CLI::error( $result->get_error_message() );
} elseif ( $result ) {
WP_CLI::success( $fqdn . __( ': is now restricted.', 'dark-matter' ) );
return;
}

WP_CLI::success( $fqdn . __( ': is now restricted.', 'dark-matter' ) );
WP_CLI::error( __( 'An unknown error has occurred.', 'dark-matter' ) );
}

/**
Expand Down Expand Up @@ -88,12 +95,14 @@ public static function define() {
*
* wp darkmatter restrict list --format=ids
*
* @subcommand list
*
* @since 2.0.0
*
* @param array $args CLI args.
* @param array $assoc_args CLI args maintaining the flag names from the terminal.
*/
public function list( $args, $assoc_args ) {
public function _list( $args, $assoc_args ) {
/**
* Handle and validate the format flag if provided.
*/
Expand All @@ -108,21 +117,23 @@ public function list( $args, $assoc_args ) {
$opts['format'] = 'table';
}

$db = Manager\Restricted::instance();

$restricted = $db->get();
$query = new RestrictedDomainQuery(
[
'number' => 100,
]
);

/**
* Only format the return array if "ids" is not specified.
*/
if ( 'ids' !== $opts['format'] ) {
$restricted = array_map(
function ( $domain ) {
function ( $restricted_domain ) {
return array(
'F.Q.D.N.' => $domain,
'F.Q.D.N.' => $restricted_domain,
);
},
$restricted
$query->records
);
}

Expand Down Expand Up @@ -160,13 +171,28 @@ public function remove( $args, $assoc_args ) {

$fqdn = $args[0];

$restricted = Manager\Restricted::instance();
$result = $restricted->delete( $fqdn );
$query = new RestrictedDomainQuery();
$domain_id = $query->get_id_by_domain( $fqdn );
if ( empty( $domain_id ) ) {
WP_CLI::error(
sprintf(
/* translators: %s: restricted domain that cannot be found. */
__( 'Cannot find domain: %s', 'dark-matter' ),
$fqdn
)
);
}

$data = new RestrictedDomain();
$result = $data->delete( $domain_id );

if ( is_wp_error( $result ) ) {
WP_CLI::error( $result->get_error_message() );
} elseif ( $result ) {
WP_CLI::success( $fqdn . __( ': is no longer restricted.', 'dark-matter' ) );
return;
}

WP_CLI::success( $fqdn . __( ': is no longer restricted.', 'dark-matter' ) );
WP_CLI::error( __( 'An unknown error has occurred.', 'dark-matter' ) );
}
}
Loading
Loading