Ejemplo n.º 1
0
 /**
  * Populate fields of the model
  *
  * @since  1.0.0
  *
  * @param  MS_Model $model
  * @param  array $settings
  * @param  bool $postmeta
  */
 public static function populate_model(&$model, $settings, $postmeta = false)
 {
     $fields = $model->get_object_vars();
     $class = get_class($model);
     $vars = get_class_vars($class);
     $saved_data = array();
     $ignore = isset($vars['ignore_fields']) ? $vars['ignore_fields'] : array();
     $ignore[] = 'instance';
     // Don't deserialize the double-serialized model!
     $ignore[] = 'actions';
     $ignore[] = 'filters';
     $ignore[] = 'ignore_fields';
     foreach ($fields as $field => $val) {
         if ('_' === $field[0] || in_array($field, $ignore)) {
             continue;
         }
         $value = null;
         if (false === $postmeta) {
             if (isset($settings[$field])) {
                 $value = $settings[$field];
             } elseif (isset($settings['_' . $field])) {
                 $value = $settings['_' . $field];
             }
         } elseif (true === $postmeta) {
             if (isset($settings[$field][0])) {
                 $value = $settings[$field][0];
             } elseif (isset($settings['_' . $field][0])) {
                 $value = $settings['_' . $field][0];
             }
         } elseif (is_string($postmeta)) {
             if (isset($settings[$postmeta . $field][0])) {
                 $value = $settings[$postmeta . $field][0];
             } elseif (isset($settings['_' . $postmeta . $field][0])) {
                 $value = $settings['_' . $postmeta . $field][0];
             }
         }
         if ($value) {
             $value = maybe_unserialize($value);
         }
         $saved_data[$field] = $value;
         if (null !== $value) {
             $model->set_field($field, $value);
         }
     }
     $model->_saved_data = $saved_data;
     /**
      * Filter the serialized data collection before it is returned.
      *
      * Typically it is written to database right after this function call,
      * so this hook allows us to modify data before it's written to the DB.
      *
      * @var object $model The completely populated object.
      * @var string $class Class name of the object.
      * @var array $settings The source data (serialized array).
      * @var bool|string $postmeta The post-meta flag defines how the
      *      $settings array is formatted.
      */
     $model = apply_filters('ms_factory_populate', $model, $class, $settings, $postmeta);
     $model = apply_filters('ms_factory_populate-' . strtolower($class), $model, $settings, $postmeta);
 }