Mercator\Mapping::create PHP Метод

create() публичный статический Метод

Create a new domain mapping
public static create ( string $site, string $domain, boolean $active = false ) : Mapping | WP_Error
$site string Site ID, or site object from {@see \get_blog_details}
$domain string
$active boolean
Результат Mapping | WP_Error
    public static function create($site, $domain, $active = false)
    {
        global $wpdb;
        // Allow passing a site object in
        if (is_object($site) && isset($site->blog_id)) {
            $site = $site->blog_id;
        }
        if (!is_numeric($site)) {
            return new \WP_Error('mercator.mapping.invalid_id');
        }
        $site = absint($site);
        $active = (bool) $active;
        // Did we get a full URL?
        if (strpos($domain, '://') !== false) {
            // Parse just the domain out
            $domain = parse_url($domain, PHP_URL_HOST);
        }
        // Does this domain exist already?
        $existing = static::get_by_domain($domain);
        if (is_wp_error($existing)) {
            return $existing;
        }
        if (!empty($existing)) {
            // Domain exists already...
            if ($site !== $existing->get_site_id()) {
                // ...and points to another site
                return new \WP_Error('mercator.mapping.domain_exists');
            }
            // ...and points to this site, so nothing to do
            return $existing;
        }
        // Create the mapping!
        $prev_errors = !empty($GLOBALS['EZSQL_ERROR']) ? $GLOBALS['EZSQL_ERROR'] : array();
        $suppress = $wpdb->suppress_errors(true);
        $result = $wpdb->insert($wpdb->dmtable, array('blog_id' => $site, 'domain' => $domain, 'active' => $active), array('%d', '%s', '%d'));
        $wpdb->suppress_errors($suppress);
        if (empty($result)) {
            // Check that the table exists...
            if (check_table() === 'created') {
                // Table created, try again
                return static::create($site, $domain);
            }
            // Other error. We suppressed errors before, so we need to make sure
            // we handle that now.
            $recent_errors = array_diff_key($GLOBALS['EZSQL_ERROR'], $prev_errors);
            while (count($recent_errors) > 0) {
                $error = array_shift($recent_errors);
                $wpdb->print_error($error['error_str']);
            }
            return new \WP_Error('mercator.mapping.insert_failed');
        }
        // Ensure the cache is flushed
        wp_cache_delete('id:' . $site, 'domain_mapping');
        wp_cache_delete('domain:' . $domain, 'domain_mapping');
        $mapping = static::get($wpdb->insert_id);
        /**
         * Fires after a mapping has been created.
         *
         * @param Mercator\Mapping $mapping         The mapping object.
         */
        do_action('mercator.mapping.created', $mapping);
        return $mapping;
    }

Usage Example

Пример #1
0
/**
 * Handle submission of the add page
 *
 * @return array|null List of errors. Issues a redirect and exits on success.
 */
function handle_edit_page_submit($id, $mapping)
{
    $messages = array();
    if (empty($mapping)) {
        $did_action = 'add';
        check_admin_referer('mercator-add-' . $id);
    } else {
        $did_action = 'edit';
        check_admin_referer('mercator-edit-' . $mapping->get_id());
    }
    // Check that the parameters are correct first
    $params = validate_alias_parameters(wp_unslash($_POST));
    if (is_wp_error($params)) {
        $messages[] = $params->get_error_message();
        if ($params->get_error_code() === 'mercator.params.domain_invalid_chars') {
            $messages[] = __('<strong>Note</strong>: for internationalized domain names, use the ASCII form (e.g, <code>xn--bcher-kva.example</code>)', 'mercator');
        }
        return $messages;
    }
    if (empty($mapping)) {
        // Create the actual mapping
        $result = $mapping = Mapping::create($params['site'], $params['domain'], $params['active']);
    } else {
        // Update our existing
        $result = $mapping->update($params);
    }
    if (is_wp_error($result)) {
        $messages[] = $result->get_error_message();
        return $messages;
    }
    // Success, redirect to alias page
    $location = add_query_arg(array('action' => 'mercator-aliases', 'id' => $id, 'did_action' => $did_action, 'mappings' => $mapping->get_id(), 'processed' => 1, '_wpnonce' => wp_create_nonce('mercator-alias-added-' . $mapping->get_id())), network_admin_url('admin.php'));
    wp_safe_redirect($location);
    exit;
}