/**
  * Delete a single mapping
  *
  * ## OPTIONS
  *
  * <id>
  * : Mapping ID
  */
 public function delete($args)
 {
     $mapping = Mapping::get($args[0]);
     if (empty($mapping)) {
         $mapping = new WP_Error('mercator.cli.mapping_not_found', __('Invalid mapping ID', 'mercator'));
     }
     if (is_wp_error($mapping)) {
         return WP_CLI::error($mapping->get_error_message());
     }
     $result = $mapping->delete();
     if (empty($result) || is_wp_error($result)) {
         return WP_CLI::error(__('Could not delete mapping', 'mercator'));
     }
 }
Example #2
0
/**
 * Register filters for URLs, if we've mapped
 */
function register_mapped_filters()
{
    $current_site = $GLOBALS['current_blog'];
    $real_domain = $current_site->domain;
    $domain = $_SERVER['HTTP_HOST'];
    if ($domain === $real_domain) {
        // Domain hasn't been mapped
        return;
    }
    // Grab both WWW and no-WWW
    if (strpos($domain, 'www.') === 0) {
        $www = $domain;
        $nowww = substr($domain, 4);
    } else {
        $nowww = $domain;
        $www = 'www.' . $domain;
    }
    $mapping = Mapping::get_by_domain(array($www, $nowww));
    if (empty($mapping) || is_wp_error($mapping)) {
        return;
    }
    $GLOBALS['mercator_current_mapping'] = $mapping;
    add_filter('site_url', __NAMESPACE__ . '\\mangle_url', -10, 4);
    add_filter('home_url', __NAMESPACE__ . '\\mangle_url', -10, 4);
    // If on network site, also filter network urls
    if (is_main_site()) {
        add_filter('network_site_url', __NAMESPACE__ . '\\mangle_url', -10, 3);
        add_filter('network_home_url', __NAMESPACE__ . '\\mangle_url', -10, 3);
    }
}
Example #3
0
/**
 * 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;
}
 /**
  * Get cell value for the active column
  *
  * @param Mapping $mapping Current mapping item
  * @return string HTML for the cell
  */
 protected function column_active($mapping)
 {
     $active = $mapping->is_active();
     if ($active) {
         return esc_html__('Active', 'mercator');
     }
     return esc_html__('Inactive', 'mercator');
 }
Example #5
0
/**
 * Output alias editing page
 */
function output_edit_page()
{
    $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
    if (!$id) {
        wp_die(__('Invalid site ID.'));
    }
    $id = absint($id);
    $details = get_blog_details($id);
    if (!can_edit_network($details->site_id) || (int) $details->blog_id !== $id) {
        wp_die(__('You do not have permission to access this page.'));
    }
    // Are we editing?
    $mapping = null;
    $form_action = network_admin_url('admin.php?action=mercator-add');
    if (!empty($_REQUEST['mapping'])) {
        $mapping_id = absint($_REQUEST['mapping']);
        $mapping = Mapping::get($mapping_id);
        if (is_wp_error($mapping) || empty($mapping)) {
            wp_die(__('Invalid alias ID.', 'mercator'));
        }
        $form_action = network_admin_url('admin.php?action=mercator-edit');
    }
    // Handle form submission
    $messages = array();
    if (!empty($_POST['submit'])) {
        $messages = handle_edit_page_submit($id, $mapping);
    }
    output_page_header($id, $messages);
    if (empty($mapping) || !empty($_POST['_wpnonce'])) {
        $domain = empty($_POST['domain']) ? '' : wp_unslash($_POST['domain']);
        $active = !empty($_POST['active']);
    } else {
        $domain = $mapping->get_domain();
        $active = $mapping->is_active();
    }
    ?>
	<form method="post" action="<?php 
    echo esc_url($form_action);
    ?>
">
		<table class="form-table">
			<tr>
				<th scope="row">
					<label for="mercator-domain"><?php 
    echo esc_html_x('Domain Name', 'field name', 'mercator');
    ?>
</label>
				</th>
				<td>
					<input type="text" class="regular-text code"
						name="domain" id="mercator-domain"
						value="<?php 
    echo esc_attr($domain);
    ?>
" />
				</td>
			</tr>
			<tr>
				<th scope="row">
					<?php 
    echo esc_html_x('Active', 'field name', 'mercator');
    ?>
				</th>
				<td>
					<label>
						<input type="checkbox"
							name="active" <?php 
    checked($active);
    ?>
 />

						<?php 
    esc_html_e('Mark alias as active', 'mercator');
    ?>
					</label>
				</td>
			</tr>
		</table>

		<input type="hidden" name="id" value="<?php 
    echo esc_attr($id);
    ?>
" />
		<?php 
    if (empty($mapping)) {
        wp_nonce_field('mercator-add-' . $id);
        submit_button(__('Add Alias', 'mercator'));
    } else {
        echo '<input type="hidden" name="mapping" value="' . esc_attr($mapping->get_id()) . '" />';
        wp_nonce_field('mercator-edit-' . $mapping->get_id());
        submit_button(__('Save Alias', 'mercator'));
    }
    ?>
	</form>
<?php 
    output_page_footer();
}