/** * Returns an indexed array of objects the addresses per the defined options. * * @param $atts { * @type string $fields The fields to return. * Default: all * Accepts: all, ids, locality, regions, postal-code, country * @type int $id The entry ID in which to retrieve the addresses for. * @type bool $preferred Whether or not to return only the preferred address. * Default: false * @type array|string $type The address types to return. * Default: array() which will return all registered address types. * Accepts: home, work, school, other and any other registered types. * @type array|string $city Return address in the defined cities. * @type array|string $state Return address in the defined states. * @type array|string $country Return address in the defined countries. * @type array $coordinates { * Return the addresses at the specific coordinates. * @type float $latitude * @type float $longitude * } * } * * @return array */ public function addresses($atts = array()) { /** @var wpdb $wpdb */ global $wpdb; $where = array('WHERE 1=1'); /* * // START -- Set the default attributes array. \\ */ $defaults = array('fields' => 'all', 'id' => NULL, 'preferred' => FALSE, 'type' => array(), 'city' => array(), 'state' => array(), 'zipcode' => array(), 'country' => array(), 'coordinates' => array(), 'limit' => NULL); $atts = cnSanitize::args($atts, $defaults); /* * // END -- Set the default attributes array if not supplied. \\ */ /** * @var int $id * @var bool $preferred * @var array|string $type * @var array|string $city * @var array|string $state * @var array|string $zipcode * @var array|string $country * @var array $coordinates * @var null|int $limit */ extract($atts); /* * Convert these to values to an array if they were supplied as a comma delimited string */ /** @var array $type */ cnFunction::parseStringList($type); /** @var array $city */ cnFunction::parseStringList($city); /** @var array $state */ cnFunction::parseStringList($state); /** @var array $zipcode */ cnFunction::parseStringList($zipcode); /** @var array $country */ cnFunction::parseStringList($country); switch ($atts['fields']) { case 'ids': $select = array('a.id', 'a.entry_id'); break; case 'locality': $select = array('a.city'); break; case 'region': $select = array('a.state'); break; case 'postal-code': $select = array('a.zipcode'); break; case 'country': $select = array('a.country'); break; default: $select = array('a.*'); } if (!empty($id)) { $where[] = $wpdb->prepare('AND `entry_id` = %d', $id); } if (!empty($preferred)) { $where[] = $wpdb->prepare('AND `preferred` = %d', (bool) $preferred); } if (!empty($type)) { $where[] = $wpdb->prepare('AND `type` IN (' . cnFormatting::prepareINPlaceholders($type) . ')', $type); } if (!empty($city)) { $where[] = $wpdb->prepare('AND `city` IN (' . cnFormatting::prepareINPlaceholders($city) . ')', $city); } if (!empty($state)) { $where[] = $wpdb->prepare('AND `state` IN (' . cnFormatting::prepareINPlaceholders($state) . ')', $state); } if (!empty($zipcode)) { $where[] = $wpdb->prepare('AND `zipcode` IN (' . cnFormatting::prepareINPlaceholders($zipcode) . ')', $zipcode); } if (!empty($country)) { $where[] = $wpdb->prepare('AND `country` IN (' . cnFormatting::prepareINPlaceholders($country) . ')', $country); } if (!empty($coordinates)) { if (!empty($coordinates['latitude']) && !empty($coordinates['longitude'])) { $where[] = $wpdb->prepare('AND `latitude` = %f', $coordinates['latitude']); $where[] = $wpdb->prepare('AND `longitude` = %f', $coordinates['longitude']); } } // Limit the characters that are queried based on if the current user can view public, private or unlisted entries. $where = self::setQueryVisibility($where, array('table' => 'a')); $limit = is_null($atts['limit']) ? '' : sprintf(' LIMIT %d', $atts['limit']); $sql = sprintf('SELECT %1$s FROM %2$s AS a %3$s ORDER BY `order`%4$s', implode(', ', $select), CN_ENTRY_ADDRESS_TABLE, implode(' ', $where), $limit); $results = $wpdb->get_results($sql); return $results; }
/** * Returns as an array of objects containing the dates per the defined options. * * @param array $atts { * Optional. An array of arguments. * * @type int $id The entry ID in which to retrieve the dates for. * @type bool $preferred Whether or not to return only the preferred dates. * Default: false * @type array|string $type The types to return. * Default: array() which will return all registered types. * Accepts: Any other registered types. * @type int $limit The number to limit the results to. * } * @param bool $saving Set as TRUE if adding a new entry or updating an existing entry. * * @return array */ public static function dates($atts = array(), $saving = FALSE) { /** @var wpdb $wpdb */ global $wpdb; $where = array('WHERE 1=1'); $defaults = array('fields' => 'all', 'id' => NULL, 'preferred' => FALSE, 'type' => array(), 'limit' => NULL); $atts = cnSanitize::args($atts, $defaults); /** * @var string $fields * @var int $id * @var bool $preferred * @var array|string $type * @var null|int $limit */ extract($atts); /* * Convert these to values to an array if they were supplied as a comma delimited string */ cnFunction::parseStringList($type); switch ($atts['fields']) { case 'ids': $select = array('d.id', 'd.entry_id'); break; case 'date': $select = array('d.date'); break; default: $select = array('d.*'); } if (is_numeric($id) && !empty($id)) { $where[] = $wpdb->prepare('AND `entry_id` = "%d"', $id); } if ($preferred) { $where[] = $wpdb->prepare('AND `preferred` = %d', (bool) $preferred); } if (!empty($type)) { $where[] = $wpdb->prepare('AND `type` IN (' . cnFormatting::prepareINPlaceholders($type) . ')', $type); } if (!$saving) { $where = self::setQueryVisibility($where, array('table' => 'd')); } $limit = is_null($atts['limit']) ? '' : sprintf(' LIMIT %d', $atts['limit']); $sql = sprintf('SELECT %1$s FROM %2$s AS d %3$s ORDER BY `order`%4$s', implode(', ', $select), CN_ENTRY_DATE_TABLE, implode(' ', $where), $limit); $results = $wpdb->get_results($sql); return $results; }