values apart into an // array and iterate over them individually. if ( $operator == 'in' ) { $values = preg_split( "/'?\s?(,)\s?'?/i", $value ); } elseif ( $operator == 'between' ) { $values = preg_split( "/'?\s?(AND)\s?'?/i", $value ); } // If there's a single value, treat it as an array so that we can still iterate. if ( empty( $values ) ) { $values = [ $value ]; } foreach ( $values as $index => $value ) { $value = preg_replace( "/^(\()?'/", '', $value ); $value = preg_replace( "/'(\))?$/", '', $value ); $values[ $index ] = $value; } if ( count( $values ) > 1 ) { $value = $values; } // Return the WP Query meta query parameters. $retval = [ 'column' => $column, 'value' => $value, 'compare' => strtoupper( $operator ), 'type' => $numeric ? 'numeric' : 'string', ]; return $retval; } public function strip_slashes( $stdObject_or_array_or_string ) { /** * Some objects have properties that are recursive objects. To avoid this we have to keep track of what objects * we've already processed when we're running this method recursively. */ static $level = 0; static $processed_objects = []; ++$level; $processed_objects[] = $stdObject_or_array_or_string; if ( is_string( $stdObject_or_array_or_string ) ) { $stdObject_or_array_or_string = str_replace( "\\'", "'", str_replace( '\"', '"', str_replace( '\\\\', '\\', $stdObject_or_array_or_string ) ) ); } elseif ( is_object( $stdObject_or_array_or_string ) && ! in_array( $stdObject_or_array_or_string, $processed_objects ) ) { foreach ( get_object_vars( $stdObject_or_array_or_string ) as $key => $val ) { if ( $val != $stdObject_or_array_or_string && $key != '_mapper' ) { $stdObject_or_array_or_string->$key = $this->strip_slashes( $val ); } } $processed_objects[] = $stdObject_or_array_or_string; } elseif ( is_array( $stdObject_or_array_or_string ) ) { foreach ( $stdObject_or_array_or_string as $key => $val ) { if ( $key != '_mixins' ) { $stdObject_or_array_or_string[ $key ] = $this->strip_slashes( $val ); } } } --$level; if ( $level == 0 ) { $processed_objects = []; } return $stdObject_or_array_or_string; } /** * Converts a stdObject entity to a model * * @param object $stdObject * @param string|bool $context (optional) * @return object */ public function convert_to_model( $stdObject, $context = false ) { try { $this->_convert_to_entity( $stdObject ); } catch ( \Exception $ex ) { throw new \E_InvalidEntityException( $ex ); } return $this->create( $stdObject ); } /** * Determines whether an object is actually a model * * @param mixed $obj * @return bool */ public function is_model( $obj ) { return is_subclass_of( $obj, '\Imagely\NGG\DataMapper\Model' ); } /** * If a field has no value, then use the default value. * * @param \stdClass|Model $object */ public function set_default_value( $object ) { $array = null; $field = null; $default_value = null; // The first argument MUST be an object. if ( ! is_object( $object ) ) { throw new \E_InvalidEntityException(); } // This method has two signatures: // 1) _set_default_value($object, $field, $default_value) // 2) _set_default_value($object, $array_field, $field, $default_value). // Handle #1. $args = func_get_args(); if ( count( $args ) == 4 ) { list($object, $array, $field, $default_value) = $args; if ( ! isset( $object->{$array} ) ) { $object->{$array} = []; $object->{$array}[ $field ] = null; } else { $arr = &$object->{$array}; if ( ! isset( $arr[ $field ] ) ) { $arr[ $field ] = null; } } $array = &$object->{$array}; $value = &$array[ $field ]; if ( $value === '' or is_null( $value ) ) { $value = $default_value; } } // Handle #2. else { list($object, $field, $default_value) = $args; if ( ! isset( $object->$field ) ) { $object->$field = null; } $value = $object->$field; if ( $value === '' or is_null( $value ) ) { $object->$field = $default_value; } } } public function has_defined_column( $name ) { $columns = $this->_columns; return isset( $columns[ $name ] ); } public function cast_columns( $entity ) { foreach ( $this->_columns as $key => $properties ) { $value = property_exists( $entity, $key ) ? $entity->$key : null; $default_value = $properties['default_value']; if ( ! is_null( $value ) && $value !== $default_value ) { $column_type = $this->_columns[ $key ]['type']; if ( preg_match( '/varchar|text/i', $column_type ) ) { if ( ! is_array( $value ) && ! is_object( $value ) ) { $entity->$key = strval( $value ); } } elseif ( preg_match( '/decimal|numeric|double|float/i', $column_type ) ) { $entity->$key = floatval( $value ); } elseif ( preg_match( '/int/i', $column_type ) ) { $entity->$key = intval( $value ); } elseif ( preg_match( '/bool/i', $column_type ) ) { $entity->$key = ( $value ? true : false ); } } else { // Add property and default value. $entity->$key = $default_value; } } return $entity; } /** * Finds a particular entry by id * * @param int|\stdClass|Model $entity * @return null|object|Model */ public function find( $entity ) { $retval = null; // Get primary key of the entity. $pkey = $this->get_primary_key_column(); if ( ! is_numeric( $entity ) ) { $entity = isset( $entity->$pkey ) ? intval( $entity->$pkey ) : false; } // If we have an entity ID, then get the record. if ( $entity ) { $results = $this->select()->where_and( [ "{$pkey} = %d", $entity ] )->limit( 1, 0 )->run_query(); if ( $results ) { $retval = $this->convert_to_model( $results[0] ); } } return $retval; } /** * Converts a stdObject to an Entity * * @param \stdClass $entity * @return object */ public function _convert_to_entity( $entity ) { // Add extra columns to entity. if ( isset( $entity->extras ) ) { $extras = $entity->extras; unset( $entity->extras ); foreach ( explode( ',', $extras ) as $extra ) { if ( $extra ) { $parts = explode( '@@', $extra ); if ( count( $parts ) === 2 ) { // Ensure we have exactly two parts. list( $key, $value ) = $parts; if ( $this->has_defined_column( $key ) && ! isset( $entity->$key ) && $key !== 'extras_post_id' ) { $entity->$key = $value; } } } } } // Cast custom_post_id as integer. if ( isset( $entity->extras_post_id ) ) { $entity->extras_post_id = intval( $entity->extras_post_id ); } else { $entity->extras_post_id = 0; } // Add name of the id_field to the entity, and convert the ID to an integer. $entity->id_field = $this->get_primary_key_column(); // Cast columns to their appropriate data type. $this->cast_columns( $entity ); // Strip slashes. $this->strip_slashes( $entity ); // Deserialize columns. $this->deserialize_columns( $entity ); return $entity; } /** * Creates a new model * * @param array $properties (optional) * @return object */ public function create( $properties = [] ) { $entity = new \stdClass(); foreach ( $properties as $key => $value ) { $entity->$key = $value; } return new $this->model_class( $entity ); } /** * Saves an entity * * @param \stdClass|Model $entity * @return bool|int Resulting ID or false upon failure */ public function save( $entity ) { $retval = false; $model = $entity; $this->flush_query_cache(); if ( is_array( $entity ) ) { throw new \E_InvalidEntityException(); } elseif ( ! $this->is_model( $entity ) ) { // We can work with what we have. But we need to ensure that we've got a model. $model = $this->convert_to_model( $entity ); } if ( ! is_array( $model->validation() ) ) { $retval = $this->save_entity( $model ); } $this->flush_query_cache(); return $retval; } /** * Gets validation errors for the entity * * @param \stdClass|Model $entity * @return array */ public function get_errors( $entity ) { $model = $entity; if ( ! $this->is_model( $entity ) ) { $model = $this->convert_to_model( $entity ); } return $model->validation(); } /** * @param object $entity */ public function set_defaults( $entity ) {} /** * @param string $name * @param string $type * @param string|number $default_value * @param bool $extra * @return void */ public function define_column( $name, $type, $default_value = null, $extra = false ) { $this->_columns[ $name ] = [ 'type' => $type, 'default_value' => $default_value, 'extra' => $extra, ]; } }