Get mapping by site ID
public static get_by_site ( integer | stdClass $site ) : |
||
$site | integer | stdClass | Site ID, or site object from {@see \get_blog_details} |
return | Mapping on success, WP_Error if error occurred, or null if no mapping found |
/** * ## OPTIONS * * [<site>] * : Site ID (defaults to current site, use `--url=...`) * * [--format=<format>] * : Format to display as (table, json, csv, count) * * @subcommand list */ public function list_($args, $assoc_args) { $id = empty($args[0]) ? get_current_blog_id() : absint($args[0]); $mappings = Mapping::get_by_site($id); if (empty($mappings)) { return; } $this->display($mappings, $assoc_args); }
/** * Output the site list column * * @param string $column Column ID * @param int $site_id Site ID */ function output_site_list_column($column, $site_id) { switch ($column) { case 'mercator_aliases': $mappings = Mapping::get_by_site($site_id); if (!empty($mappings)) { foreach ($mappings as $mapping) { // Kinda horrible formatting, but matches the existing echo esc_html($mapping->get_domain()) . '<br />'; } } break; } }
/** * Prepare items for the list table */ public function prepare_items() { $this->items = array(); if (empty($this->_args['site_id'])) { return; } $id = $this->_args['site_id']; $mappings = Mapping::get_by_site($id); if (is_wp_error($mappings)) { \Mercator\warn_with_message(__('Could not fetch aliases for the site. This may indicate a database error.', 'mercator')); } if (!empty($mappings)) { $this->items = $mappings; } }
/** * Clear mappings for a site when it's deleted * * @param int $site_id Site being deleted */ function clear_mappings_on_delete($site_id) { $mappings = Mapping::get_by_site($site_id); if (empty($mappings)) { return; } foreach ($mappings as $mapping) { $error = $mapping->delete(); if (is_wp_error($error)) { $message = sprintf(__('Unable to delete mapping %d for site %d', 'mercator'), $mapping->get_id(), $site_id); trigger_error($message, E_USER_WARNING); } } }
/** * Get primary domain from site_id * Note that the first active alias is assumed to be primary * @param $site_id * @param bool|true $with_protocol * @return string */ function get_primary_domain($site_id, $with_protocol = true) { // Add object caching to remove need to lookup primary domain multiple times $primary_domain = wp_cache_get('mercator_get_primary_domain_' . $site_id); if (false === $primary_domain) { // Settings primary domain to http host for easy fallback $primary_domain = $_SERVER['HTTP_HOST']; $mappings = Mapping::get_by_site($site_id); // If no mapping are found, return the current domain if (!$mappings) { return $primary_domain; } // Check for the first active alias and assume its primary foreach ($mappings as $mapping) { if ($mapping->is_active()) { $primary_domain = $mapping->get_domain(); wp_cache_set('mercator_get_primary_domain', $primary_domain . $site_id); break; } } } // Always allow protocal modifier to change primary domain url returned (do not cache) if ($with_protocol) { if (is_ssl()) { $primary_domain = 'https://' . $primary_domain; } else { $primary_domain = 'http://' . $primary_domain; } } return $primary_domain; }