Plugin Directory

Changeset 1801403


Ignore:
Timestamp:
01/11/2018 10:07:36 PM (8 years ago)
Author:
thundersnow
Message:

updated error checking

Location:
knowledge-tags-from-yext/trunk
Files:
2 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • knowledge-tags-from-yext/trunk/settings.php

    r1730703 r1801403  
    9696   if (gettype($location_error)=="string") {
    9797  ?>
    98       <p class="error"><?php esc_html_e("ERROR: ". $location_error, 'KTFY-yext-schema'); ?></p>
     98      <pre class="error"><?php esc_html_e("ERROR: ". $location_error, 'KTFY-yext-schema'); ?></pre>
    9999  <?php
    100100   }
    101101  if (gettype($schema_error)=="string") {
    102102  ?>
    103     <p class="error"><?php esc_html_e("ERROR: ". $schema_error, 'KTFY-yext-schema'); ?></p>
     103    <pre class="error"><?php esc_html_e("ERROR: ". $schema_error, 'KTFY-yext-schema'); ?></pre>
    104104  <?php
    105105  }
     
    133133 <p id="<?php echo esc_attr($args['id']); ?>"><?php esc_html_e('[yext-data yext-location-id=1001 yext-field=locationName]', 'KTFY_yext_schema_shortcodes'); ?></p>
    134134 <?php
     135 $host = KTFY_get_yext_host_name();
     136 $supported_field_url = sprintf("%s/supportedfields", $host);
    135137 $account_options = get_option('KTFY_yext_sub_account_options');
    136     $fields = array(
    137    "locationName"        =>array("locationName", "Name of your yext location", "Business Name"),
    138    "logo.url"            =>array("logo.url", "Image corresponding to your Yext Location", "Logo"),
    139    "id"                  =>array("id", "Store ID", "Store ID"),
    140    "description"         =>array("description", "Business Description", "Business Description"),
    141    "email"               =>array("email", "Email Address", "Contact Email"),
    142    "address1"            =>array("address1", "Street Address", "Business Address"),
    143    "address2"            =>array("address2", "Floor, Suite, Unit", "Business Floor, Suite, Unit"),
    144    "city"                =>array("city", "City/Locality", "Business City"),
    145    "state"               =>array("state", "Region", "Business State"),
    146    "zip"                 =>array("zip", "Postal Code", "Business Postal Code"),
    147    "countryCode"         =>array("countryCode", "Country", "Business Country"),
    148    "phone"               =>array("phone", "Phone number for location", "Business Phone"),
    149    "paymentOptions"      =>array("paymentOptions", "The payment options for the location", "Payment Methods, comma-separated"),
    150    "hours"               =>array("hours", "Hours of Operation, for all days", "Business Hours"),
    151    "hours-monday"        =>array("hours-monday", "Hours of operation for Monday", ""),
    152    "hours-tuesday"       =>array("hours-tuesday", "Hours of operation for Tuesday", ""),
    153    "hours-wednesday"     =>array("hours-wednesday", "Hours of operation for Wednesday", ""),
    154    "hours-thursday"      =>array("hours-thursday", "Hours of operation for Thursday", ""),
    155    "hours-friday"        =>array("hours-friday", "Hours of operation for Friday", ""),
    156    "hours-saturday"      =>array("hours-saturday", "Hours of operation for Saturday", ""),
    157    "hours-sunday"        =>array("hours-sunday", "Hours of operation for Sunday", ""),
    158    "get-directions-link" =>array("get-directions-link", "The link for a given location", "Get Directions"),
    159  ); ?>
     138 $supported_fields = @file_get_contents($supported_field_url);
     139 $KTFY_knowlege_tag_supported_fields = json_decode($supported_fields);
     140 $fields = array();
     141 foreach ($KTFY_knowlege_tag_supported_fields as $key => $value) {
     142    $fields[$key] = $value;
     143 }
     144 ?>
     145
    160146 <p class="">
    161147   <?php esc_html_e('Accepted Shortcode Fields', 'KTFY_yext_schema_shortcodes'); ?>
  • knowledge-tags-from-yext/trunk/yext-schema.php

    r1730703 r1801403  
    1212*/
    1313include "settings.php";
    14 include "format.php";
    1514global $KTFY_page_location_Ids;
    1615$KTFY_page_location_Ids=array();
    17 
    18 
    19 // fomatting location data to accepted knowledge tags
    20 function KTFY_schema_formater($locId, $loc_data)
    21 {
    22     global $KTFY_knowlege_tag_data;
    23     $loc_data = $loc_data->{"response"};
    24     $KTFY_knowlege_tag_data = new StdClass;
    25     $KTFY_knowlege_tag_data->{$locId} = new StdClass;
    26     $hours = $loc_data->{"hours"};
    27     $KTFY_knowlege_tag_data->{$locId}->{"locationName"} = $loc_data->{"locationName"};
    28     $KTFY_knowlege_tag_data->{$locId}->{"id"} = $loc_data->{"id"};
    29     $KTFY_knowlege_tag_data->{$locId}->{"description"} = $loc_data->{"description"};
    30     $KTFY_knowlege_tag_data->{$locId}->{"email"} = KTFY_get_email_format($loc_data->{"emails"});
    31     $KTFY_knowlege_tag_data->{$locId}->{"address1"} = $loc_data->{"address"};
    32     $KTFY_knowlege_tag_data->{$locId}->{"address2"} = $loc_data->{"address2"};
    33     $KTFY_knowlege_tag_data->{$locId}->{"city"} = $loc_data->{"city"};
    34     $KTFY_knowlege_tag_data->{$locId}->{"state"} = $loc_data->{"state"};
    35     $KTFY_knowlege_tag_data->{$locId}->{"zip"} = $loc_data->{"zip"};
    36     $KTFY_knowlege_tag_data->{$locId}->{"countryCode"} = $loc_data->{"countryCode"};
    37     $KTFY_knowlege_tag_data->{$locId}->{"phone"} = $loc_data->{"phone"};
    38     $KTFY_knowlege_tag_data->{$locId}->{"hours"} = KTFY_get_hours_format($hours);
    39     $KTFY_knowlege_tag_data->{$locId}->{"hours-monday"} = KTFY_get_hours_weekday_format(2, $hours);
    40     $KTFY_knowlege_tag_data->{$locId}->{"hours-tuesday"} = KTFY_get_hours_weekday_format(3, $hours);
    41     $KTFY_knowlege_tag_data->{$locId}->{"hours-wednesday"} = KTFY_get_hours_weekday_format(4, $hours);
    42     $KTFY_knowlege_tag_data->{$locId}->{"hours-thursday"} = KTFY_get_hours_weekday_format(5, $hours);
    43     $KTFY_knowlege_tag_data->{$locId}->{"hours-friday"} = KTFY_get_hours_weekday_format(6, $hours);
    44     $KTFY_knowlege_tag_data->{$locId}->{"hours-saturday"} = KTFY_get_hours_weekday_format(7, $hours);
    45     $KTFY_knowlege_tag_data->{$locId}->{"hours-sunday"} = KTFY_get_hours_weekday_format(1, $hours);
    46     $KTFY_knowlege_tag_data->{$locId}->{"paymentOptions"} = KTFY_get_payment_options_format($loc_data->{"paymentOptions"});
    47     $KTFY_knowlege_tag_data->{$locId}->{"logo.url"} = KTFY_get_logo_url_format($loc_data->{"logo"});
    48     $KTFY_knowlege_tag_data->{$locId}->{"get-directions-link"} = KTFY_get_get_directions_link_format($loc_data);
    49 }
    5016
    5117/**
     
    6430    $KTFY_page_location_Ids = $locationIds;
    6531    $KTFY_yext_schema_data = new StdClass;
     32    $KTFY_knowlege_tag_data = new StdClass;
     33    delete_option('KTFY_yext_location_error_option');
     34
    6635    if (is_null($account_Id) || $account_Id == ""){
    6736      $account_Id = "me";
     
    7241            if (!is_null($locationId) && !is_null($apikey) && !is_null($account_Id)) {
    7342                $locationId = trim($locationId);
    74                 // get schema data
    75                 $schema_data= KTFY_get_schema_data($apikey, $account_Id, $locationId);
    76                 if ($schema_data) {
    77                     $KTFY_yext_schema_data->{$locationId} = $schema_data;
    78                 }
    79                 //get location data
    80                 $loc_data = KTFY_get_location_data($apikey, $account_Id, $locationId);
    81                 if ($loc_data) {
    82                     KTFY_schema_formater($locationId, $loc_data);
    83                     $loc_data->{$locationId} = $loc_data;
     43                $KTFY_knowlege_tag_data->{$locationId} = new StdClass;
     44                $data = KTFY_get_yext_data($apikey, $account_Id, $locationId);
     45                if ($data) {
     46                    $KTFY_yext_schema_data->{$locationId} = $data->{"locations"}[0]->{"schema"};
     47                    $KTFY_knowlege_tag_data->{$locationId} = $data->{"locations"}[0]->{"location"};
    8448                }
    8549            }
     
    8852}
    8953
    90 
    91 // querying for schema data
    92 function KTFY_get_schema_data($apiKey, $account_Id, $locationId)
     54// querying for data
     55function KTFY_get_yext_data($apiKey, $account_Id, $locationId)
    9356{
    94     $date = date("Ymd");
    95     $schema_url = sprintf("%s/v2/accounts/%s/locations/%s/schema?api_key=%s&v=%s", KTFY_get_yext_api_host(), $account_Id, $locationId, $apiKey, $date);
     57    $host = KTFY_get_yext_host_name();
     58    $schema_url = sprintf("%s/jsondata?account_id=%s&location_id=%s&key=%s", $host, urlencode($account_Id), urlencode($locationId), urlencode($apiKey));
    9659    $schema_data = @file_get_contents($schema_url);
    9760    if (!$schema_data){
     
    10366}
    10467
    105 // querying for location data
    106 function KTFY_get_location_data($apiKey, $account_Id, $locationId)
    107 {
    108     $date = date("Ymd");
    109     $location_url = sprintf("%s/v2/accounts/%s/locations/%s?api_key=%s&v=%s", KTFY_get_yext_api_host(), $account_Id, $locationId, $apiKey, $date);
    110     $KTFY_knowlege_tag_data = @file_get_contents($location_url);
    111     if (!$KTFY_knowlege_tag_data){
    112       //display error here
    113       echo "<h3 style='color:red;'> ERROR: Unable to retrieve Location Data - Please check your plugin configuration and your server's network configuration. If this issue persists please contact the plugin authors and/or your network admin.</h3>";
    114 
    115     }
    116     $KTFY_knowlege_tag_data = json_decode($KTFY_knowlege_tag_data);
    117     return $KTFY_knowlege_tag_data;
    118 }
    119 
    120 function KTFY_get_yext_api_host()
    121 {
    122     return "https://liveapi.yext.com";
     68function KTFY_get_yext_host_name(){
     69  if ( $_SERVER["HTTP_HOST"] === '192.168.99.100:8080' ){
     70    // testing url
     71    return "http://10.254.254.254:9090";
     72  }
     73  // live url
     74  return "https://knowledgetags.yextpages.net";
    12375}
    12476
     
    13385    if (is_null($KTFY_knowlege_tag_data)) {
    13486      add_option('KTFY_yext_location_error_option', 'Location Data is Empty');
    135     } else{
    136       delete_option('KTFY_yext_location_error_option');
    13787    }
    138     $response = @$KTFY_knowlege_tag_data->{$locationId}->{$field_name};
     88    if (array_key_exists($field_name, $KTFY_knowlege_tag_data->{$locationId})){
     89      $response = @$KTFY_knowlege_tag_data->{$locationId}->{$field_name};
     90    } else {
     91      $option_value = get_option('KTFY_yext_location_error_option');
     92      update_option('KTFY_yext_location_error_option', $option_value."\n". "\"".$field_name .  '" is not present in this location, please double check if this field is still supported.');
     93    }
     94
    13995    return $response;
    14096}
     
    149105        echo '<script type="application/ld+json">'."\r\n";
    150106        foreach ($KTFY_page_location_Ids as $locationId) {
    151             $ld_json = @json_encode($KTFY_yext_schema_data->{$locationId}->{"response"}, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
     107            $ld_json = @json_encode($KTFY_yext_schema_data->{$locationId}, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
    152108            // only write to html if we have data
    153109            if (!is_null($ld_json) && $ld_json !="null") {
     
    157113        echo "\r\n".'</script>';
    158114        delete_option('KTFY_yext_schema_error_option');
    159     } else {
    160         add_option('KTFY_yext_schema_error_option', 'Schema Data is Empty');
     115    } elseif (is_null($KTFY_yext_schema_data) && !is_null($KTFY_page_location_Ids)) {
     116        add_option('KTFY_yext_schema_error_option', 'Schema Data is Empty for '+ get_the_title());
    161117    }
    162118}
Note: See TracChangeset for help on using the changeset viewer.