Skip to content

Instantly share code, notes, and snippets.

@jeremyfelt
Last active February 9, 2017 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyfelt/6f1a5b0e684668b9a389cdb1b6a9a563 to your computer and use it in GitHub Desktop.
Save jeremyfelt/6f1a5b0e684668b9a389cdb1b6a9a563 to your computer and use it in GitHub Desktop.

Error

PHP Warning:  explode() expects parameter 2 to be string, array given in /var/www/wp-content/plugins/events-calendar-pro/src/Tribe/Recurrence/Meta.php on line 1403"
PHP Warning:  array_map(): Argument #2 should be an array in /var/www/wp-content/plugins/events-calendar-pro/src/Tribe/Recurrence/Meta.php on line 1403"

Code introduced in The Events Calendar Pro 4.4.1

// Select saves on a single field, and we have to explode it into an array for manipulation
if ( isset( $rule['custom']['year']['month'] ) ) {
  $rule['custom']['year']['month'] = array_map( 'intval', explode( ',', $rule['custom']['year']['month'] ) );
}

To properly handle the old data structure that may still exist on events, the code should defensively check for an existing array. I'm not sure if this is the best place for the approach, but it's what I've used to hotfix the issue in our production environment.

// Select saves on a single field, and we have to explode it into an array for manipulation
if ( isset( $rule['custom']['year']['month'] ) ) {
  if ( ! is_array( $rule['custom']['year']['month'] ) ) {
    $rule['custom']['year']['month'] = explode( ',', $rule['custom']['year']['month'] );
  }
  $rule['custom']['year']['month'] = array_map( 'intval', $rule['custom']['year']['month'] );
}

Newer style data structure

On a newer event, the data in $rule['custom']['year']['month'] is stored as a string.

wp> $rules = get_post_meta( 1823, '_EventRecurrence', true );
=> array(3) {
  ["rules"]=>
  array(1) {
    [0]=>
    array(6) {
      ["type"]=>
      string(6) "Custom"
      ["custom"]=>
      array(4) {
        ["interval"]=>
        string(1) "1"
        ["year"]=>
        array(2) {
          ["month"]=>
          string(1) "1"
          ["filter"]=>
          string(1) "0"
        }
        ["same-time"]=>
        string(3) "yes"
        ["type"]=>
        string(6) "Yearly"
      }
      ["end-type"]=>
      string(2) "On"
      ["end"]=>
      string(10) "2021-01-01"
      ["EventStartDate"]=>
      string(19) "2018-01-01 00:00:00"
      ["EventEndDate"]=>
      string(19) "2018-01-01 23:59:59"
    }
  }
  ["exclusions"]=>
  array(0) {
  }
  ["description"]=>
  NULL
}

Older style data structure

On an older event, the data in $rule['custom']['year']['month'] is stored as an array.

wp> $rules_tribe = Tribe__Events__Pro__Recurrence__Meta::getRecurrenceMeta( 272 );
=> array(2) {
  ["rules"]=>
  array(1) {
    [0]=>
    array(7) {
      ["EventStartDate"]=>
      string(19) "2015-09-07 00:00:00"
      ["EventEndDate"]=>
      string(19) "2015-09-07 23:59:59"
      ["type"]=>
      string(6) "Custom"
      ["end-type"]=>
      string(5) "Never"
      ["end"]=>
      NULL
      ["end-count"]=>
      string(1) "1"
      ["custom"]=>
      array(7) {
        ["type"]=>
        string(6) "Yearly"
        ["interval"]=>
        string(1) "1"
        ["same-time"]=>
        string(3) "yes"
        ["day"]=>
        array(1) {
          ["same-time"]=>
          string(3) "yes"
        }
        ["week"]=>
        array(2) {
          ["day"]=>
          NULL
          ["same-time"]=>
          string(3) "yes"
        }
        ["month"]=>
        array(3) {
          ["number"]=>
          string(5) "First"
          ["day"]=>
          string(1) "1"
          ["same-time"]=>
          string(3) "yes"
        }
        ["year"]=>
        array(5) {
          ["month"]=>
          array(1) {
            [0]=>
            string(1) "9"
          }
          ["filter"]=>
          string(1) "1"
          ["number"]=>
          string(5) "First"
          ["day"]=>
          string(1) "1"
          ["same-time"]=>
          string(3) "yes"
        }
      }
    }
  }
  ["exclusions"]=>
  array(0) {
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment