n] */ public function store_nav_settings( $nav_settngs = array(), $settings = array() ) { $nav_settngs['inject_alternative_items'] = ! empty( $settings['inject_alternative_items'] ) ? $settings['inject_alternative_items'] : ''; $nav_settngs['injection_items'] = ! empty( $settings['injection_items'] ) ? $settings['injection_items'] : array(); return $nav_settngs; } /** * Reset injected counter * * @return [type] [description] */ public function reset_injected_counter() { if ( ! empty( $this->injected_counter ) ) { $this->parent_injected_counter = $this->injected_counter; $this->parent_injected_indexes = $this->injected_indexes; } $this->injected_counter = array(); $this->injected_indexes = array(); $this->is_last_static_hooked = false; } public function set_parent_injected_counter() { if ( empty( $this->parent_injected_counter ) ) { return; } $this->injected_counter = $this->parent_injected_counter; $this->injected_indexes = $this->parent_injected_indexes; $this->parent_injected_counter = array(); $this->parent_injected_indexes = array(); } /** * Maybe inject new listing item */ public function maybe_inject_item( $content = false, $post = null, $i = 0, $widget = false, $query = null ) { $settings = $widget->get_settings(); $injected_item = $this->get_injected_item( $settings, $post, $i, $widget, count( $query ) ); if ( ! $injected_item ) { return $content; } else { return $this->get_injected_item_content( $injected_item, $post ); } } /** * Returns injected item ID * * @return [type] [description] */ public function get_injected_item_content( $item_id, $post ) { jet_engine()->admin_bar->register_post_item( $item_id ); jet_engine()->frontend->set_listing( $item_id ); ob_start(); $listing_item = jet_engine()->frontend->get_listing_item( $post ); $inline_css = ob_get_clean(); return $inline_css . $listing_item; } /** * Maybe add columns colspan on appropriate indexes * * @param [type] $classes [description] * @param [type] $post [description] * @param [type] $i [description] * @param [type] $widget [description] * * @return [type] [description] */ public function maybe_add_colspan( $classes = array(), $post = null, $i = null, $widget = null, $is_static = false ) { if ( empty( $this->injected_indexes[ $i ] ) ) { return $classes; } $item = $this->injected_indexes[ $i ]; if ( ! empty( $item['static_item'] ) && ! $is_static ) { return $classes; } if ( empty( $item['item_colspan'] ) ) { return $classes; } $colspan = absint( $item['item_colspan'] ); if ( 1 < $colspan ) { $columns = $widget->get_settings( 'columns' ); if ( ! $columns ) { $columns = 3; } $columns = absint( $columns ); if ( $colspan > $columns ) { $final_colspan = '1'; } elseif ( $columns === $colspan ) { $final_colspan = '1'; } else { $final_colspan = $colspan . '-' . $columns; } $classes[] = 'colspan-' . $final_colspan; } return $classes; } public function update_static_item_classes( $classes = array(), $post = null, $i = null, $widget = null, $is_static = false ) { $static_post_id = $this->get_static_item_post_id( $i, $is_static ); if ( ! $static_post_id ) { return $classes; } $initial_id = jet_engine()->listings->data->get_current_object_id( $post ); if ( $initial_id == $static_post_id ) { return $classes; } $classes = array_filter( $classes, function ( $class ) { return false === strpos( $class, 'jet-listing-dynamic-post-' ); } ); $classes[] = 'jet-listing-dynamic-post-' . $static_post_id; return $classes; } public function update_static_item_post_id( $id = null, $post = null, $i = null, $widget = null, $is_static = false ) { $static_post_id = $this->get_static_item_post_id( $i, $is_static ); if ( ! $static_post_id ) { return $id; } return $static_post_id; } public function get_static_item_post_id( $i, $is_static ) { if ( ! $is_static ) { return false; } if ( empty( $this->injected_indexes[ $i ] ) ) { return false; } $item = $this->injected_indexes[ $i ]; $injected_hash = $this->get_injected_hash( $item ); if ( empty( $this->static_items_post_ids[ $injected_hash ] ) ) { return false; } if ( empty( $this->static_items_post_ids[ $injected_hash ][ $i ] ) ) { return false; } return $this->static_items_post_ids[ $injected_hash ][ $i ]; } /** * Check if current iterator is matched with required number * * @param [type] $i [description] * @param [type] $number [description] * @param [type] $from_first [description] * * @return boolean [description] */ public function is_matched_num( $i = 1, $number = 2, $from_first = false, $once = false, $total = 1, $listing_id = false ) { if ( empty( $number ) ) { return false; } if ( 0 > $number && $listing_id && ! empty( $this->static_injections[ $listing_id ] ) ) { $total = $total + $this->static_injections[ $listing_id ]; } if ( empty( $once ) ) { if ( $from_first ) { if ( 0 <= $number ) { return ( 1 === $i || 0 === ( $i - 1 ) % $number ); } else { return ( $total === $i || 0 === ( $total - $i ) % absint( $number ) ); } } else { if ( 0 <= $number ) { return ( 0 === $i % $number ); } else { return ( 0 === ( $total - ( $i - 1 ) ) % absint( $number ) ); } } } else { if ( $from_first ) { if ( 0 <= $number ) { return ( 1 === $i ); } else { return ( $total === $i ); } } else { if ( 0 <= $number ) { return ( absint( $number ) === $i ); } else { return ( absint( $number ) === ( $total - ( $i - 1 ) ) ); } } } } /** * Check if we need to inject item on this moment * * @return [type] [description] */ public function get_injected_item( $settings, $post, $i, $widget, $total ) { $inject = ! empty( $settings['inject_alternative_items'] ) ? $settings['inject_alternative_items'] : false; $inject = filter_var( $inject, FILTER_VALIDATE_BOOLEAN ); if ( ! $inject ) { return false; } $items = ! empty( $settings['injection_items'] ) ? $settings['injection_items'] : array(); if ( empty( $items ) || ! is_array( $items ) ) { return false; } $i = absint( $i ); $items = $this->sort_items( $items ); $listing_id = ! empty( $settings['lisitng_id'] ) ? absint( $settings['lisitng_id'] ) : false; if ( $listing_id !== jet_engine()->listings->data->get_listing()->get_main_id() ) { return false; } foreach ( array_unique( $items, SORT_REGULAR ) as $item ) { $result = false; if ( empty( $item['item'] ) ) { continue; } $type = ! empty( $item['item_condition_type'] ) ? $item['item_condition_type'] : 'on_item'; $once = ! empty( $item['inject_once'] ) ? $item['inject_once'] : false; switch ( $type ) { case 'on_item': $num = ! empty( $item['item_num'] ) ? intval( $item['item_num'] ) : 2; $from_first = ! empty( $item['start_from_first'] ) ? true : false; if ( $this->is_matched_num( $i, $num, $from_first, $once, $total, $listing_id ) ) { $this->increase_count( $item['item'], $i, $item ); $result = $item['item']; } break; case 'item_meta': $meta_key = ! empty( $item['meta_key'] ) ? $item['meta_key'] : false; $meta_compare = ! empty( $item['meta_key_compare'] ) ? $item['meta_key_compare'] : '='; $compare_val = ! empty( $item['meta_key_val'] ) ? $item['meta_key_val'] : false; if ( $meta_key ) { $class = get_class( $post ); switch ( $class ) { case 'WP_Post': $meta_val = get_post_meta( $post->ID, $meta_key ); break; case 'WP_User': $meta_val = get_user_meta( $post->ID, $meta_key ); break; case 'WP_Term': $meta_val = get_term_meta( $post->term_id, $meta_key ); break; default: $meta_val = apply_filters( 'jet-engine/listing-injections/item-meta-value', false, $post, $meta_key ); } $exists = ! empty( $meta_val ) ? true : false; $meta_val = $exists ? $meta_val[0] : false; $matched = false; $compare_val = do_shortcode( jet_engine()->listings->macros->do_macros( $compare_val ) ); switch ( $meta_compare ) { case '=': if ( $meta_val == $compare_val ) { $matched = true; } break; case '!=': if ( $meta_val != $compare_val ) { $matched = true; } break; case '>': if ( $meta_val > $compare_val ) { $matched = true; } break; case '<': if ( $meta_val < $compare_val ) { $matched = true; } break; case '>=': if ( $meta_val >= $compare_val ) { $matched = true; } break; case '<=': if ( $meta_val <= $compare_val ) { $matched = true; } break; case 'LIKE': if ( false !== strpos( $compare_val, $meta_val ) ) { $matched = true; } break; case 'NOT LIKE': if ( false === strpos( $compare_val, $meta_val ) ) { $matched = true; } break; case 'IN': $compare_val = explode( ',', $compare_val ); $compare_val = array_map( 'trim', $compare_val ); if ( in_array( $meta_val, $compare_val ) ) { $matched = true; } break; case 'NOT IN': $compare_val = explode( ',', $compare_val ); $compare_val = array_map( 'trim', $compare_val ); if ( ! in_array( $meta_val, $compare_val ) ) { $matched = true; } break; case 'BETWEEN': $compare_val = explode( ',', $compare_val ); $compare_val = array_map( 'trim', $compare_val ); $from = isset( $compare_val[0] ) ? floatval( $compare_val[0] ) : 0; $to = isset( $compare_val[1] ) ? floatval( $compare_val[1] ) : 0; $meta_val = floatval( $meta_val ); if ( ( $from <= $meta_val ) && ( $meta_val <= $to ) ) { $matched = true; } break; case 'NOT BETWEEN': $compare_val = explode( ',', $compare_val ); $compare_val = array_map( 'trim', $compare_val ); $from = isset( $compare_val[0] ) ? floatval( $compare_val[0] ) : 0; $to = isset( $compare_val[1] ) ? floatval( $compare_val[1] ) : 0; $meta_val = floatval( $meta_val ); if ( ( $meta_val < $from ) || ( $to < $meta_val ) ) { $matched = true; } break; } if ( $matched ) { if ( $once ) { if ( ! isset( $this->injected_counter[ $item['item'] ] ) ) { $this->increase_count( $item['item'], $i, $item ); $result = $item['item']; } } else { $this->increase_count( $item['item'], $i, $item ); $result = $item['item']; } } } break; case 'has_terms': $tax = ! empty( $item['tax'] ) ? $item['tax'] : 'category'; $terms = ! empty( $item['terms'] ) ? explode( ',', $item['terms'] ) : array(); $terms = array_map( 'trim', $terms ); if ( ! empty( $terms ) && has_term( $terms, $tax, $post ) ) { if ( $once ) { if ( ! isset( $this->injected_counter[ $item['item'] ] ) ) { $this->increase_count( $item['item'], $i, $item ); $result = $item['item']; } } else { $this->increase_count( $item['item'], $i, $item ); $result = $item['item']; } } break; case 'post_type': $post_type = ! empty( $item['post_type'] ) ? $item['post_type'] : 'post'; if ( ! empty( $post_type ) && isset( $post->post_type ) && $post_type === $post->post_type ) { if ( $once ) { if ( ! isset( $this->injected_counter[ $item['item'] ] ) ) { $this->increase_count( $item['item'], $i, $item ); $result = $item['item']; } } else { $this->increase_count( $item['item'], $i, $item ); $result = $item['item']; } } break; case 'term_tax': $tax = ! empty( $item['tax'] ) ? $item['tax'] : 'category'; $term = $post; if ( isset( $term->taxonomy ) && $tax === $term->taxonomy ) { if ( $once ) { if ( ! isset( $this->injected_counter[ $item['item'] ] ) ) { $this->increase_count( $item['item'], $i, $item ); $result = $item['item']; } } else { $this->increase_count( $item['item'], $i, $item ); $result = $item['item']; } } break; } if ( $result ) { if ( ! empty( $item['static_item'] ) ) { if ( ! empty( $item['static_item_context'] ) && 'default_object' !== $item['static_item_context'] ) { $post = jet_engine()->listings->data->get_object_by_context( $item['static_item_context'] ); } $post = apply_filters( 'jet-engine/listing-injections/static-item-post', $post, $item, $settings, $widget ); if ( $post ) { // increase inserted static items counter if ( $listing_id ) { $this->static_injections[ $listing_id ] = isset( $this->static_injections[ $listing_id ] ) ? $this->static_injections[ $listing_id ] ++ : 1; } $injected_hash = $this->get_injected_hash( $item ); if ( empty( $this->static_items_post_ids[ $injected_hash ] ) ) { $this->static_items_post_ids[ $injected_hash ] = array(); } $this->static_items_post_ids[ $injected_hash ][ $i ] = jet_engine()->listings->data->get_current_object_id( $post ); $this->print_static_result( $result, $post, $item, $i, $listing_id ); } //return false; } else { return $result; } } } return false; } public function get_injected_hash( $item ) { return md5( json_encode( $item ) ); } public function print_static_result( $result, $post, $item, $i, $listing_id = false ) { $type = ! empty( $item['item_condition_type'] ) ? $item['item_condition_type'] : 'on_item'; $num = ! empty( $item['item_num'] ) ? intval( $item['item_num'] ) : 2; if ( 'on_item' === $type && 0 > $num ) { if ( ! $this->is_last_static_hooked ) { $this->is_last_static_hooked = true; add_action( 'jet-engine/listing/after-grid-item', function ( $post, $widget, $i ) use ( $listing_id ) { if ( ! $listing_id || $listing_id === jet_engine()->listings->data->get_listing()->get_main_id() ) { $this->print_static_result_cb( $post, $widget, $i ); } }, 10, 3 ); } if ( empty( $this->static_items_to_print[ $i ] ) ) { $this->static_items_to_print[ $i ] = array(); } $this->static_items_to_print[ $i ][] = array( $result, $post ); } else { echo $this->get_injected_item_content( $result, $post ); } } public function print_static_result_cb( $post, $widget, $i ) { if ( ! empty( $this->static_items_to_print[ $i ] ) ) { foreach ( $this->static_items_to_print[ $i ] as $item ) { $classes = array( 'jet-listing-grid__item', 'jet-listing-dynamic-post-' . $post->ID ); $equal_cols = $widget->get_settings( 'equal_columns_height' ); if ( ! empty( $equal_cols ) ) { $classes[] = 'jet-equal-columns'; } $static_classes = apply_filters( 'jet-engine/listing/item-classes', $classes, $post, $i, $widget, true ); printf( '