• I try to use sublanguage with toolset, but on certain post types i get the error:
    Notice: Undefined offset: 0 in /wp-content/plugins/types/vendor/toolset/toolset-common/toolset-forms/classes/class.types.php on line 426

    Having this error also causes
    Warning: Cannot modify header information – headers already sent by (output started at /wp-content/plugins/types/vendor/toolset/toolset-common/toolset-forms/classes/class.types.php:426) in /wp-admin/admin-header.php on line 9

    Which causes editing that post become impossible.

    Here is the function from toolset with that error:

    public static function getConditionalValues($post_id, $meta_type = 'postmeta') {
        $cond_values = array();
        if ( !empty( $post_id ) ) {
            $cond_values = $meta_type == 'usermeta' ? get_user_meta( $post_id ) : get_post_custom( $post_id );
        }
    
        // Unserialize [values] and do not allow array (take first value from array
        if ( is_array( $cond_values ) ) {
            foreach ( $cond_values as $k => &$v ) {
                $v = maybe_unserialize( $v[0] ); //THIS CAUSES THE ERROR
                $v = self::getStringFromArray($v);
            }
        }
    
        return $cond_values;
    }

    the highlighted line is the problematic one.
    After investigating the problems it seems that get_post_custom() while editing a post in the second language in my case returns the data in the wrong format.
    From my observation get_post_custom returns an associative array with meta_keys as keys and array of values as the value.
    With sublanguage the value may be an empty array (and that crashes toolset)
    here is an example of var_dump(get_post_custom()) (i’ve cut out irrelevant lines, both fields are translatable, but only the text-lower does have a translation)

    array(25) {
      ["wpcf-main-text-upper"]=>
      array(0) {
      }
      ["wpcf-main-text-lower"]=>
      array(1) {
        [0]=>
        string(15) "translated text"
      }
      ["_en_wpcf-main-text-lower"]=>
      array(1) {
        [0]=>
        string(15) "translated text"
      }
    }

    There is a workaround for toolset – just silencing the error with @, but it may cause problems in other places…

Viewing 1 replies (of 1 total)
  • Plugin Author maximeschoeni

    (@maximeschoeni)

    Thanks for submitting this issue and for the investigation!

    I think I got the problem, but I’m hesitating to fix it in Sublanguage, because I’m not sure if the fix wouldn’t break other things…

    If Toolset author is okay to bring changes, I think it would be safer to fix there:

    
    public static function getConditionalValues($post_id, $meta_type = 'postmeta') {
      // [...]
      foreach ( $cond_values as $k => &$v ) {
        if (isset($v[0])) { // <- add this test in case other plugins mess around with get_post_custom() returned value.
          $v = maybe_unserialize( $v[0] ); //THIS CAUSES THE ERROR
          $v = self::getStringFromArray($v);
        }
      }
      // [...]
    }
    

    In other case, you may try to filter out empty arrays by using get_post_metadata filter (get_{meta_type}_metadata/):

    
    add_filter('get_post_metadata', function($value, $object_id, $meta_key, $single, $meta_type) {
    
      if (is_array($value) && !$single && !$meta_key && is_admin()) {
    
        $value = array_filter(
          $value,
          function ($val) {
            return !is_array($val) || !empty($val);
          }
        );
    
      }
    
      return $value;
    
    }, 99, 5);
    

    But I didn’t test it, not sure if it actually works.

Viewing 1 replies (of 1 total)
  • The topic ‘problem with get_post_custom()’ is closed to new replies.