• I’m trying to fix a broken integration with a job site’s API that I use on my site. I recently changed to the Sage theme and am using PHP 5.6.2. Before I migrated everything worked, and now it does not.

    In my extras.php I’ve declared a few functions:

    Function 1: slugify the job titles:

    function slugify($text) {
      // replace non letter or digits by -
      $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
      // trim
      $text = trim($text, '-');
      // transliterate
      $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
      // lowercase
      $text = strtolower($text);
      // remove unwanted characters
      $text = preg_replace('~[^-\w]+~', '', $text);
    
      if (empty($text)) {
        return 'n-a';
      }
      return $text;
    }
    add_filter('slugify',  __NAMESPACE__ . '\\slugify');

    Function 2: get the list of jobs:

    function get_jobs() {
      $transient = 'lever_jobs';
      $json = get_transient($transient);
    
      if(false == $json) {
        $json = file_get_contents(LEVER_API_URL . '?mode=json');
        $json = json_decode($json);
        if(sizeof($json) > 0) {
          for ($i = 0; $i < count($json); $i++) {
            $json[$i]->slug = slugify($json[$i]->text);
          }
          set_transient($transient, $json, HOUR_IN_SECONDS);
        }
      }
      return $json;
    }
    add_filter('get_jobs',  __NAMESPACE__ . '\\get_jobs');

    Function 3: get job form errors:

    function get_job_form_errors($postdata, $files) {
      $output = array();
    
      if(strlen($postdata['fullname']) == 0) {
        $output['fullname'] = true;
      }
      if(!is_email($postdata['email'])) {
        $output['email'] = true;
      }
      if (empty($_FILES["resume"]['name'])) {
        $output['resume'] = true;
      }
      return $output;
    }
    add_filter('get_job_form_errors',  __NAMESPACE__ . '\\get_job_form_errors');

    Function 4: submit the job to Lever:

    function submit_to_lever($jobid, $postdata, $files) {
      $ch = curl_init(LEVER_API_URL . $jobid . '?key=' . LEVER_API_KEY);
    
      $f = $_FILES['resume']['tmp_name'];
      if(is_uploaded_file($f)) {
        $postdata['resume'] = "@$f;filename=".$_FILES['resume']['name'];
      }
    
      // curl doesnt support multi-dimensional arrays
      if(is_array($postdata['urls'])) {
        foreach ($postdata['urls'] as $key => $value) {
          $postdata["urls['$key']"] = $value;
        }
        unset($postdata['urls']);
      }
    
      $postdata['name'] = $postdata['fullname'];
      unset($postdata['fullname']);
    
      curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_exec($ch);
      curl_close($ch);
    }
    add_filter('submit_to_lever',  __NAMESPACE__ . '\\submit_to_lever');

    And then, in my template file, I call these functions at the top:

    <?php
      $jobs = Roots\Sage\Extras\get_jobs();
      $is_post = False;
      $form_submitted = False;
      $errors = [];
    
      if($_SERVER['REQUEST_METHOD'] == 'POST') {
        $is_post = True;
        $postdata = $_POST;
        var_dump($postdata);
        $files = $_FILES;
        parse_str($_SERVER['QUERY_STRING']);
    
        $errors = Roots\Sage\Extras\get_job_form_errors($postdata, $files);
    
        if (sizeof($errors) == 0) {
          Roots\Sage\Extras\submit_to_lever($jobid, $postdata, $files);
          // var_dump($jobid);
          $form_submitted = True;
        }
      }
      }
    ?>

    I’m able to get my job postings, and am able to echo what I need to in most parts of the template, except for this part:

    <?php
      $cur_job = null;
    
      foreach($jobs as $one) {
        if(isset($postdata['jobid'])) {
          if($one->id == $postdata['jobid']) {
            $cur_job = $one;
            break;
          }
        }
      }
    ?>

    For example, when I try outputting some of these values in an input, like this:

    <input type="hidden" value="<?php echo isset($cur_job->id); ?>" name="jobid" />

    I get index errors…

    Since everything was working properly prior to the PHP update, I’m convinced this is a syntax issue. I’ve researched what needed to be updated to the best of my ability, but there’s still clearly something wrong. What am I missing?

  • The topic ‘PHP 5.6 syntax issues? Broken API integration’ is closed to new replies.