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

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

Get mapping by site ID
public static get_by_site ( integer | stdClass $site ) : Mapping | WP_Erro\WP_Error | null
$site integer | stdClass Site ID, or site object from {@see \get_blog_details}
Результат Mapping | WP_Erro\WP_Error | null Mapping on success, WP_Error if error occurred, or null if no mapping found
    public static function get_by_site($site)
    {
        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);
        // Check cache first
        $mappings = wp_cache_get('id:' . $site, 'domain_mapping');
        if (!empty($mappings)) {
            return static::to_instances($mappings);
        }
        // Cache missed, fetch from DB
        // Suppress errors in case the table doesn't exist
        $suppress = $wpdb->suppress_errors();
        $mappings = $wpdb->get_results($wpdb->prepare('SELECT * FROM ' . $wpdb->dmtable . ' WHERE blog_id = %d', $site));
        $wpdb->suppress_errors($suppress);
        if (!$mappings) {
            return null;
        }
        wp_cache_set('id:' . $site, $mappings, 'domain_mapping');
        return static::to_instances($mappings);
    }

Usage Example

Пример #1
0
 /**
  * ## 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);
 }
All Usage Examples Of Mercator\Mapping::get_by_site