_key ] = array( 'title' => sprintf( __( 'Child %s', 'jet-engine' ), $label_2 ), 'type' => 'select', 'name' => $meta_key, ); $this->_relations_for_post_types[ $post_type_2 ][ $meta_key ] = array( 'title' => sprintf( __( 'Parent %s', 'jet-engine' ), $label_1 ), 'type' => 'select', 'name' => $meta_key, ); } /** * Get values for relations meta fields early * * @param [type] $result [description] * @param [type] $post [description] * @param [type] $key [description] * @param [type] $default [description] * @param [type] $field [description] * @return [type] [description] */ public function get_meta( $result, $post, $key, $default, $field ) { $relation = isset( $this->_active_relations[ $key ] ) ? $this->_active_relations[ $key ] : false; if ( ! $relation ) { return $result; } $meta = get_post_meta( $post->ID, $key, false ); $meta = array_filter( $meta ); $meta = array_values( $meta ); if ( ! isset( $field['multiple'] ) || ! $field['multiple'] ) { $meta = isset( $meta[0] ) ? $meta[0] : null; } if ( ! empty( $meta ) ) { return $meta; } else { return null; } } /** * Check if is relation meta key * * @param [type] $meta_key [description] * @return boolean [description] */ public function is_relation_key( $meta_key = null ) { return isset( $this->_active_relations[ $meta_key ] ); } /** * Returns active relations list * * @return [type] [description] */ public function get_active_relations( $rel_id = false ) { return $this->_active_relations; } /** * Synchronize related meta on post save * * @return void */ public function process_meta( $result = null, $post_id = null, $meta_key = '', $related_posts = array() ) { $relation = isset( $this->_active_relations[ $meta_key ] ) ? $this->_active_relations[ $meta_key ] : false; if ( ! $relation ) { return $result; } if ( empty( $related_posts ) ) { $related_posts = ! empty( $_POST[ $meta_key ] ) ? $_POST[ $meta_key ] : false; } if ( empty( $related_posts ) ) { jet_engine()->relations->data->delete_all_related_meta( $meta_key, $post_id ); } if ( ! is_array( $related_posts ) ) { $related_posts = array( $related_posts ); } $prev_related = get_post_meta( $post_id, $meta_key ); $saved_post_type = get_post_type( $post_id ); $type = explode( '_to_', $relation['type'] ); if ( $relation['post_type_1'] === $saved_post_type ) { $current_type = $type[0]; $related_type = $type[1]; } elseif ( $relation['post_type_2'] === $saved_post_type ) { $current_type = $type[1]; $related_type = $type[0]; } else { return $result; } $result = true; $to_delete = array_diff( $prev_related, $related_posts ); $to_delete = array_filter( $to_delete ); if ( ! empty( $to_delete ) ) { foreach ( $to_delete as $delete_post_id ) { delete_post_meta( $delete_post_id, $meta_key, $post_id ); delete_post_meta( $post_id, $meta_key, $delete_post_id ); } } foreach ( $related_posts as $related_post_id ) { if ( $related_post_id && ! in_array( $related_post_id, $prev_related ) ) { add_post_meta( $post_id, $meta_key, $related_post_id, false ); } $stored = get_post_meta( $related_post_id, $meta_key, false ); if ( ! is_array( $stored ) ) { $stored = array( $stored ); $stored = array_filter( $stored ); } if ( 'one' === $current_type ) { foreach ( $stored as $stored_id ) { if ( absint( $stored_id ) !== $post_id ) { delete_post_meta( $related_post_id, $meta_key ); delete_post_meta( $stored_id, $meta_key, $related_post_id ); } } } if ( $post_id && ! in_array( $post_id, $stored ) ) { add_post_meta( $related_post_id, $meta_key, $post_id, false ); } } return $result; } /** * Returns relation meta keys for passed post type * * @param string $post_type Post type name * @return array */ public function get_relation_fields_for_post_type( $post_type = null ) { $default = array(); if ( ! $post_type ) { return $default; } if ( empty( $this->_relations_for_post_types ) ) { return $default; } if ( empty( $this->_relations_for_post_types[ $post_type ] ) ) { return $default; } return $this->_relations_for_post_types[ $post_type ]; } /** * Returns info about relationby hash */ public function get_relation_info( $key ) { return isset( $this->_active_relations[ $key ] ) ? $this->_active_relations[ $key ] : false; } /** * Returns related posts * * @return void */ public function get_related_posts( $args = array() ) { $post_type_1 = isset( $args['post_type_1'] ) ? $args['post_type_1'] : false; $post_type_2 = isset( $args['post_type_2'] ) ? $args['post_type_2'] : false; $hash = isset( $args['hash'] ) ? $args['hash'] : false; if ( $hash ) { $meta_key = $hash; $relation = isset( $this->_active_relations[ $meta_key ] ) ? $this->_active_relations[ $meta_key ] : false; if ( ! $relation ) { return false; } $post_type_1 = $relation['post_type_1']; $post_type_2 = $relation['post_type_2']; $current = ! empty( $args['current'] ) ? $args['current'] : false; if ( $current ) { $args['from'] = ( $post_type_1 === $current ) ? $post_type_2 : $post_type_1; } } else { if ( ! $post_type_1 || ! $post_type_2 ) { return false; } $meta_key = $this->get_relation_hash( $post_type_1, $post_type_2 ); $relation = isset( $this->_active_relations[ $meta_key ] ) ? $this->_active_relations[ $meta_key ] : false; } if ( ! $relation ) { return false; } $post_id = isset( $args['post_id'] ) ? $args['post_id'] : get_the_ID(); $type = explode( '_to_', $relation['type'] ); $from = isset( $args['from'] ) ? $args['from'] : $post_type_1; if ( $post_type_1 === $from ) { $type = $type[0]; } else { $type = $type[1]; } if ( 'one' === $type ) { $single = true; } else { $single = false; } $meta = get_post_meta( $post_id, $meta_key, $single ); $meta = apply_filters( 'jet-engine/relations/get_related_posts', $meta ); return $meta; } /** * Clear relations meta on delete a relation post. * * @param $post_id */ public function clear_relations_meta_on_delete_post( $post_id ) { $post_type = get_post_type( $post_id ); $relation_fields = $this->get_relation_fields_for_post_type( $post_type ); if ( ! empty( $relation_fields ) ) { foreach ( $relation_fields as $field => $args ) { jet_engine()->relations->data->delete_all_related_meta( $field, $post_id ); } } } }