Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function glsr_create_carrier_term(): array {
- $dotNum = filter_input(INPUT_GET, 'dotNumber', FILTER_VALIDATE_INT);
- if (empty($dotNum)) {
- return []; // don't assign category if a dotNumber isn't requested
- }
- $termSlug = 'carrier_'.$dotNum;
- if ($term = term_exists($termSlug, glsr()->taxonomy)) {
- return $term;
- }
- $termName = glsr_get_carrier_name($dotNum);
- if (empty($termName)) {
- return glsr_create_fallack_carrier_term();
- }
- $term = wp_insert_term($termName, glsr()->taxonomy, ['slug' => $termSlug]);
- if (is_wp_error($term)) {
- glsr_log()->error($term->get_error_message());
- return glsr_create_fallack_carrier_term();
- }
- return $term;
- }
- function glsr_create_fallack_carrier_term(): array {
- $termName = 'Unknown Carrier';
- $termSlug = 'unknown_carrier';
- if ($term = term_exists($termSlug, glsr()->taxonomy)) {
- return $term;
- }
- return wp_insert_term($termName, glsr()->taxonomy, ['slug' => $termSlug]);
- }
- function glsr_get_carrier_name(int $dotNum): string {
- $apiUrl = 'https://mobile.fmcsa.dot.gov/qc/services/carriers/';
- $apiKey = 'e9d6c0cb17ee98addddfc81f8af610f035949f50';
- $url = sprintf('%s/%s/', untrailingslashit($apiUrl), $dotNum);
- $url = add_query_arg('webKey', $apiKey, $url);
- $response = wp_remote_get($url);
- if (is_wp_error($response)) {
- glsr_log()->error($response->get_error_message());
- return '';
- }
- if ('200' != ($statusCode = wp_remote_retrieve_response_code($response))) {
- glsr_log()->error(sprintf('Unable to connect to %s [%s]', $url, $statusCode));
- return '';
- }
- $body = json_decode(wp_remote_retrieve_body($response), true);
- return glsr_get($body, 'content.carrier.dbaName');
- }
- function glsr_set_carrier_assigned_terms(array $args): array {
- $term = glsr_create_carrier_term();
- if (!empty($term['term_id'])) {
- $args['assigned_terms'] = $term['term_id'];
- }
- return $args;
- }
- add_action('site-reviews/defaults/site-review', 'glsr_set_carrier_assigned_terms');
- add_action('site-reviews/defaults/site-reviews', 'glsr_set_carrier_assigned_terms');
- add_action('site-reviews/defaults/site-reviews-form', 'glsr_set_carrier_assigned_terms');
- add_action('site-reviews/defaults/site-reviews-summary', 'glsr_set_carrier_assigned_terms');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement