Mercator\Mapping::get PHP Method

get() public static method

Get mapping by mapping ID
public static get ( integer | Mapping $mapping ) : Mapping | WP_Erro\WP_Error | null
$mapping integer | Mapping Mapping ID or instance
return Mapping | WP_Erro\WP_Error | null Mapping on success, WP_Error if error occurred, or null if no mapping found
    public static function get($mapping)
    {
        global $wpdb;
        // Allow passing a site object in
        if ($mapping instanceof Mapping) {
            return $mapping;
        }
        if (!is_numeric($mapping)) {
            return new \WP_Error('mercator.mapping.invalid_id');
        }
        $mapping = absint($mapping);
        // Suppress errors in case the table doesn't exist
        $suppress = $wpdb->suppress_errors();
        $mapping = $wpdb->get_row($wpdb->prepare('SELECT * FROM ' . $wpdb->dmtable . ' WHERE id = %d', $mapping));
        $wpdb->suppress_errors($suppress);
        if (!$mapping) {
            return null;
        }
        return new static($mapping);
    }

Usage Example

 /**
  * 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'));
     }
 }
All Usage Examples Of Mercator\Mapping::get