Skip to content

Commit

Permalink
Add "type" support to forms, tables, and other interface elements.
Browse files Browse the repository at this point in the history
  • Loading branch information
JJJ committed Dec 7, 2016
1 parent ac1df9c commit d8c66ae
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class WP_Site_Alias_Query {
* @type int $offset Number of aliases to offset the query. Used to build LIMIT clause.
* Default 0.
* @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
* @type string|array $orderby Site status or array of statuses. Accepts 'id', 'domain', 'status',
* @type string|array $orderby Site status or array of statuses. Accepts 'id', 'domain', 'status', 'type',
* 'created', 'domain_length', 'path_length', or 'site__in'. Also accepts false,
* an empty array, or 'none' to disable `ORDER BY` clause.
* Default 'id'.
Expand All @@ -155,7 +155,7 @@ class WP_Site_Alias_Query {
* @type array $type__in Array of types to include affiliated aliases for. Default empty.
* @type array $type__not_in Array of types to exclude affiliated aliases for. Default empty.
* @type string $search Search term(s) to retrieve matching aliases for. Default empty.
* @type array $search_columns Array of column names to be searched. Accepts 'domain' and 'status'.
* @type array $search_columns Array of column names to be searched. Accepts 'domain', 'status', 'type'.
* Default empty array.
*
* @type bool $update_site_alias_cache Whether to prime the cache for found aliases. Default false.
Expand Down Expand Up @@ -495,11 +495,11 @@ protected function get_alias_ids() {
$search_columns = array();

if ( $this->query_vars['search_columns'] ) {
$search_columns = array_intersect( $this->query_vars['search_columns'], array( 'domain', 'status' ) );
$search_columns = array_intersect( $this->query_vars['search_columns'], array( 'domain', 'status', 'type' ) );
}

if ( ! $search_columns ) {
$search_columns = array( 'domain', 'status' );
$search_columns = array( 'domain', 'status', 'type' );
}

/**
Expand Down
37 changes: 34 additions & 3 deletions wp-site-aliases/includes/classes/class-wp-site-alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ final class WP_Site_Alias {
*/
public $status = '';

/**
* Type of alias.
*
* @since 3.0.0
* @access public
* @var string
*/
public $type = '';

/**
* Creates a new WP_Site_Alias object.
*
Expand Down Expand Up @@ -180,6 +189,21 @@ public function set_status( $status = 'active' ) {
) );
}

/**
* Set the type for the alias
*
* @since 3.0.0
*
* @param bool $type Status should be 'active' or 'inactive'
*
* @return bool|WP_Error True if we updated, false if we didn't need to, or WP_Error if an error occurred
*/
public function set_type( $type = 'active' ) {
return $this->update( array(
'type' => $type,
) );
}

/**
* Set the domain for the alias
*
Expand All @@ -198,7 +222,7 @@ public function set_domain( $domain ) {
/**
* Update the alias
*
* See also, {@see set_domain} and {@see set_status} as convenience methods.
* See also {@see set_domain} {@see set_status} {@see set_type} as convenience methods.
*
* @since 1.0.0
*
Expand Down Expand Up @@ -238,6 +262,12 @@ public function update( $data = array() ) {
$formats[] = '%s';
}

// Were we given a type (and is it not the current one?)
if ( ! empty( $data['type'] ) && ( $this->type !== $data['type'] ) ) {
$fields['type'] = sanitize_key( $data['type'] );
$formats[] = '%s';
}

// Were we given a site ID (and is it not the current one?)
if ( ! empty( $data['site_id'] ) && ( $this->site_id !== $data['site_id'] ) ) {
$fields['blog_id'] = (int) $data['site_id'];
Expand Down Expand Up @@ -448,11 +478,12 @@ public static function get_by_domain( $domains = array() ) {
*
* @param mixed $site Site ID, or site object from {@see get_blog_details}
* @param string $domain Domain
* @param status $status Status of alias
* @param string $status Status of alias
* @param string $type Type of alias
*
* @return WP_Site_Alias|WP_Error
*/
public static function create( $site = 0, $domain = '', $status = 'active' ) {
public static function create( $site = 0, $domain = '', $status = 'active', $type = 'mask' ) {
global $wpdb;

// Allow passing a site object in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function get_columns() {
'cb' => '<input type="checkbox" />',
'domain' => _x( 'Domain', 'site aliases', 'wp-site-aliases' ),
'status' => _x( 'Status', 'site aliases', 'wp-site-aliases' ),
'type' => _x( 'Type', 'site aliases', 'wp-site-aliases' ),
'created' => _x( 'Created', 'site aliases', 'wp-site-aliases' )
);

Expand Down Expand Up @@ -183,7 +184,7 @@ public function current_action() {
}

/**
* Get cell value for the checkbox column
* Get cell value for the check-box column
*
* @since 1.0.0
* @access protected
Expand Down Expand Up @@ -288,6 +289,21 @@ protected function column_status( $alias ) {
: esc_html_x( 'Inactive', 'site aliases', 'wp-site-aliases' );
}

/**
* Get value for the type column
*
* @since 3.0.0
* @access protected
*
* @param WP_Site_Alias $alias Current alias item
* @return string HTML for the cell
*/
protected function column_type( $alias ) {
return ( 'mask' === $alias->type )
? esc_html_x( 'Mask', 'site aliases', 'wp-site-aliases' )
: esc_html_x( 'Redirect', 'site aliases', 'wp-site-aliases' );
}

/**
* Get value for the status column
*
Expand Down
51 changes: 47 additions & 4 deletions wp-site-aliases/includes/functions/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,8 @@ function wp_site_aliases_handle_site_actions() {
$alias = WP_Site_Alias::create(
$params['site_id'],
$params['domain'],
$params['status']
$params['status'],
$params['type']
);

// Bail if an error occurred
Expand Down Expand Up @@ -593,16 +594,39 @@ function wp_site_aliases_output_edit_page() {
<td>
<select name="status" id="status"><?php

// Get statuses
$statuses = wp_site_aliases_get_statuses();

// Loop throug sites
// Loop through statuses
foreach ( $statuses as $status ) :

// Loop through sites
// Output status
?><option value="<?php echo esc_attr( $status->id ); ?>" <?php selected( $status->id, $alias->status ); ?>><?php echo esc_html( $status->name ); ?></option><?php

endforeach;

?></select>
</td>
</tr>

<tr>
<th scope="row">
<?php echo esc_html_x( 'Type', 'field name', 'wp-site-aliases' ); ?>
</th>
<td>
<select name="type" id="type"><?php

// Get types
$types = wp_site_aliases_get_types();

// Loop through types
foreach ( $types as $type ) :

// Output type
?><option value="<?php echo esc_attr( $type->id ); ?>" <?php selected( $type->id, $alias->status ); ?>><?php echo esc_html( $type->name ); ?></option><?php

endforeach;

?></select>
</td>
</tr><?php
Expand Down Expand Up @@ -770,6 +794,24 @@ function wp_site_aliases_output_list_page() {
<p><?php esc_html_e( 'Whether this domain is ready to accept incoming requests.', 'wp-site-aliases' ); ?></p>
</div>

<div class="form-field form-required type-wrap">
<label for="type"><?php echo esc_html_x( 'Type', 'field name', 'wp-site-aliases' ); ?></label>
<select name="type" id="type"><?php

$types = wp_site_aliases_get_types();

// Loop throug sites
foreach ( $types as $type ) :

// Loop through sites
?><option value="<?php echo esc_attr( $type->id ); ?>"><?php echo esc_html( $type->name ); ?></option><?php

endforeach;

?></select>
<p><?php esc_html_e( 'Whether to mask the original domain, or redirect to it.', 'wp-site-aliases' ); ?></p>
</div>

<input type="hidden" name="action" value="add"><?php

//
Expand Down Expand Up @@ -830,7 +872,8 @@ function wp_site_aliases_output_admin_notices() {
'wp_site_aliases_domain_empty' => _x( 'Alias missing domain.', 'site aliases', 'wp-site-aliases' ),
'wp_site_aliases_domain_requires_tld' => _x( 'Alias missing a top-level domain.', 'site aliases', 'wp-site-aliases' ),
'wp_site_aliases_domain_invalid_chars' => _x( 'Alias contains invalid characters.', 'site aliases', 'wp-site-aliases' ),
'wp_site_aliases_domain_invalid_status' => _x( 'Status must be active or inactive', 'site aliases', 'wp-site-aliases' )
'wp_site_aliases_domain_invalid_status' => _x( 'Status must be active or inactive', 'site aliases', 'wp-site-aliases' ),
'wp_site_aliases_domain_invalid_type' => _x( 'Type must be mask or redirect', 'site aliases', 'wp-site-aliases' )
);

// Insert the placeholder
Expand Down
20 changes: 20 additions & 0 deletions wp-site-aliases/includes/functions/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,26 @@ function wp_site_aliases_get_statuses() {
) );
}

/**
* Get all available site alias types
*
* @since 1.0.0
*
* @return array
*/
function wp_site_aliases_get_types() {
return apply_filters( 'wp_site_aliases_get_types', array(
(object) array(
'id' => 'mask',
'name' => _x( 'Mask', 'site aliases', 'wp-site-aliases' )
),
(object) array(
'id' => 'redirect',
'name' => _x( 'Redirect', 'site aliases', 'wp-site-aliases' )
),
) );
}

/**
* Sanitize requested alias ID values
*
Expand Down

0 comments on commit d8c66ae

Please sign in to comment.