/** * wpv_get_filter_taxonomy_term_summary_txt * * Returns the taxonomy term filter summary for a View * * @param $view_settings * * @return array (string) $summary * * @since unknown */ function wpv_get_filter_taxonomy_term_summary_txt( $view_settings ) { if ( !isset( $view_settings['taxonomy_type'] ) ) { return; } elseif ( is_array( $view_settings['taxonomy_type'] ) ) { $view_settings['taxonomy_type'] = $view_settings['taxonomy_type'][0]; if ( ! taxonomy_exists( $view_settings['taxonomy_type'] ) ) { return; } } if ( ! isset( $view_settings['taxonomy_terms_mode'] ) ) { return; } if ( ! isset( $view_settings['taxonomy_terms'] ) ) { $view_settings['taxonomy_terms'] = array(); } if ( ! empty( $view_settings['taxonomy_terms'] ) ) { $adjusted_term_ids = array(); foreach ( $view_settings['taxonomy_terms'] as $candidate_term_id ) { // WordPress 4.2 compatibility - split terms $candidate_term_id_splitted = wpv_compat_get_split_term( $candidate_term_id, $view_settings['taxonomy_type'] ); if ( $candidate_term_id_splitted ) { $candidate_term_id = $candidate_term_id_splitted; } // WPML support $candidate_term_id = apply_filters( 'translate_object_id', $candidate_term_id, $view_settings['taxonomy_type'], true, null ); $adjusted_term_ids[] = $candidate_term_id; } $view_settings['taxonomy_terms'] = $adjusted_term_ids; } ob_start(); switch ( $view_settings['taxonomy_terms_mode'] ) { case 'THESE': $cat_text = ''; $category_selected = $view_settings['taxonomy_terms']; $taxonomy = $view_settings['taxonomy_type']; foreach ( $category_selected as $cat ) { $term_check = term_exists( (int) $cat, $taxonomy ); if ( $term_check !== 0 && $term_check !== null ) { $term = get_term( $cat, $taxonomy ); if ( $cat_text != '' ) { $cat_text .= ', '; } $cat_text .= esc_html( $term->name ); } } echo sprintf( __( 'Taxonomy is <strong>One</strong> of these: <strong>%s</strong>', 'wpv-views' ), $cat_text ); break; case 'CURRENT_PAGE': echo __( 'Taxonomy is set by the current page', 'wpv-views' ); break; case 'framework': global $WP_Views_fapi; if ( $WP_Views_fapi->framework_valid ) { if ( isset( $view_settings['taxonomy_terms_framework'] ) && '' != $view_settings['taxonomy_terms_framework'] ) { $taxonomy_terms_framework = esc_html( $view_settings['taxonomy_terms_framework'] ); } else { $taxonomy_terms_framework = __( 'None', 'wpv-views' ); } echo sprintf( __( 'Taxonomy is set by the Framework option <strong>"%s"</strong>', 'wpv-views' ), $taxonomy_terms_framework ); } else { $WP_Views_fapi->framework_missing_message_for_filters(); } break; default: _e( 'Oops! It seems there is a filter by taxonomy terms that is missing some options', 'wpv-views' ); break; } $data = ob_get_clean(); return $data; }
/** * wpv_get_list_item_ui_taxonomy_parent * * Render taxonomy parent filter item content in the filters list * * @param $view_settings * * @since unknown */ static function wpv_get_list_item_ui_taxonomy_parent( $view_settings = array() ) { if ( isset( $view_settings['taxonomy_type'] ) && is_array( $view_settings['taxonomy_type'] ) && sizeof( $view_settings['taxonomy_type'] ) > 0 ) { $view_settings['taxonomy_type'] = $view_settings['taxonomy_type'][0]; if ( ! taxonomy_exists( $view_settings['taxonomy_type'] ) ) { return '<p class="toolset-alert">' . __( 'This View has a filter for a taxonomy that no longer exists. Please select one taxonomy and update the Content Selection section.', 'wpv-views' ) . '</p>'; } } if ( isset( $view_settings['taxonomy_parent_mode'] ) && is_array( $view_settings['taxonomy_parent_mode'] ) ) { $view_settings['taxonomy_parent_mode'] = $view_settings['taxonomy_parent_mode'][0]; } if ( isset( $view_settings['taxonomy_type'] ) && isset( $view_settings['taxonomy_parent_id'] ) && ! empty( $view_settings['taxonomy_parent_id'] ) ) { // WordPress 4.2 compatibility - split terms $candidate_term_id_splitted = wpv_compat_get_split_term( $view_settings['taxonomy_parent_id'], $view_settings['taxonomy_type'] ); if ( $candidate_term_id_splitted ) { $view_settings['taxonomy_parent_id'] = $candidate_term_id_splitted; } // Adjust for WPML support $view_settings['taxonomy_parent_id'] = apply_filters( 'translate_object_id', $view_settings['taxonomy_parent_id'], $view_settings['taxonomy_type'], true, null ); } ob_start(); ?> <p class='wpv-filter-taxonomy-parent-edit-summary js-wpv-filter-summary js-wpv-filter-taxonomy-parent-summary'> <?php echo wpv_get_filter_taxonomy_parent_summary_txt( $view_settings ); ?> </p> <?php WPV_Filter_Item::simple_filter_list_item_buttons( 'taxonomy-parent', 'wpv_filter_taxonomy_parent_update', wp_create_nonce( 'wpv_view_filter_taxonomy_parent_nonce' ), 'wpv_filter_taxonomy_parent_delete', wp_create_nonce( 'wpv_view_filter_taxonomy_parent_delete_nonce' ) ); ?> <span class="wpv-filter-title-notice js-wpv-filter-taxonomy-parent-notice hidden"> <i class="icon-bookmark icon-rotate-270 icon-large" title="<?php echo esc_attr( __( 'This filters needs some action', 'wpv-views' ) ); ?>"></i> </span> <div id="wpv-filter-taxonomy-parent-edit" class="wpv-filter-edit js-wpv-filter-edit"> <div id="wpv-filter-taxonomy-parent" class="js-wpv-filter-options js-wpv-filter-taxonomy-parent-options"> <?php WPV_Parent_Filter::wpv_render_taxonomy_parent_options( $view_settings ); ?> </div> <div class="js-wpv-filter-toolset-messages"></div> </div> <?php $res = ob_get_clean(); return $res; }
/** * wpv_add_filter_taxonomy_list_item * * Render taxonomy filter item in the filters list * * @param $view_settings * * @since unknown */ static function wpv_add_filter_taxonomy_list_item( $view_settings ) { if ( ! isset( $view_settings['taxonomy_relationship'] ) ) { $view_settings['taxonomy_relationship'] = 'AND'; } $summary = ''; $td = ''; $taxonomies = get_taxonomies( '', 'objects' ); foreach ( $taxonomies as $category_slug => $category ) { $save_name = ( $category->name == 'category' ) ? 'post_category' : 'tax_input_' . $category->name; $relationship_name = ( $category->name == 'category' ) ? 'tax_category_relationship' : 'tax_' . $category->name . '_relationship'; if ( isset( $view_settings[$relationship_name] ) ) { if ( ! isset( $view_settings[$save_name] ) ) { $view_settings[$save_name] = array(); } if ( ! empty( $view_settings[$save_name] ) ) { $adjusted_term_ids = array(); foreach ( $view_settings[$save_name] as $candidate_term_id ) { // WordPress 4.2 compatibility - split terms $candidate_term_id_splitted = wpv_compat_get_split_term( $candidate_term_id, $category->name ); if ( $candidate_term_id_splitted ) { $candidate_term_id = $candidate_term_id_splitted; } // WPML support $candidate_term_id = apply_filters( 'translate_object_id', $candidate_term_id, $category->name, true, null ); $adjusted_term_ids[] = $candidate_term_id; } $view_settings[$save_name] = $adjusted_term_ids; } $name = ( $category->name == 'category' ) ? 'post_category' : 'tax_input[' . $category->name . ']'; $td .= WPV_Taxonomy_Filter::wpv_get_list_item_ui_post_taxonomy( $category, $view_settings[$save_name], $view_settings ); if ( $summary != '' ) { if ( $view_settings['taxonomy_relationship'] == 'OR') { $summary .= __( ' OR ', 'wpv-views' ); } else { $summary .= __( ' AND ', 'wpv-views' ); } } $summary .= wpv_get_taxonomy_summary( $name, $view_settings, $view_settings[$save_name] ); } } if ( $td != '' ) { ob_start(); WPV_Filter_Item::filter_list_item_buttons( 'taxonomy', 'wpv_filter_taxonomy_update', wp_create_nonce( 'wpv_view_filter_taxonomy_nonce' ), 'wpv_filter_taxonomy_delete', wp_create_nonce( 'wpv_view_filter_taxonomy_delete_nonce' ) ); ?> <?php if ( $summary != '' ) { ?> <p class='wpv-filter-taxonomy-edit-summary js-wpv-filter-summary js-wpv-filter-taxonomy-summary'> <?php _e('Select posts with taxonomy: ', 'wpv-views'); echo $summary; ?> </p> <?php } ?> <div id="wpv-filter-taxonomy-edit" class="wpv-filter-edit js-wpv-filter-edit js-wpv-filter-taxonomy-edit js-wpv-filter-options" style="padding-bottom:28px;"> <?php echo $td;?> <div class="wpv-filter-taxonomy-relationship wpv-filter-multiple-element js-wpv-filter-taxonomy-relationship"> <h4><?php _e('Taxonomy relationship:', 'wpv-views') ?></h4> <div class="wpv-filter-multiple-element-options"> <?php _e('Relationship to use when querying with multiple taxonomies:', 'wpv-views'); ?> <select name="taxonomy_relationship"> <option value="AND" <?php selected( $view_settings['taxonomy_relationship'], 'AND' ); ?>><?php _e( 'AND', 'wpv-views' ); ?> </option> <option value="OR" <?php selected( $view_settings['taxonomy_relationship'], 'OR' ); ?>><?php _e( 'OR', 'wpv-views' ); ?></option> </select> </div> </div> <div class="js-wpv-filter-multiple-toolset-messages"></div> <span class="filter-doc-help"> <?php echo sprintf( __( '%sLearn about filtering by taxonomy%s', 'wpv-views' ), '<a class="wpv-help-link" href="' . WPV_FILTER_BY_TAXONOMY_LINK . '" target="_blank">', ' »</a>' ); ?> </span> </div> <?php $li_content = ob_get_clean(); WPV_Filter_Item::multiple_filter_list_item( 'taxonomy', 'posts', __( 'Taxonomy filter', 'wpv-views' ), $li_content ); } }
/** * get_taxonomy_query * * Main function to get the results of a View that lists taxonomy terms * * @param $view_settings array * * @since unknown */ function get_taxonomy_query( $view_settings ) { global $WP_Views, $wpdb, $WPVDebug; $taxonomies = get_taxonomies( '', 'objects' ); $view_id = $WP_Views->get_current_view(); $WPVDebug->add_log( 'info', apply_filters( 'wpv-view-get-content-summary', '', $WP_Views->current_view, $view_settings ), 'short_query' ); $tax_query_settings = array( 'hide_empty' => $view_settings['taxonomy_hide_empty'], 'hierarchical' => $view_settings['taxonomy_include_non_empty_decendants'], 'pad_counts' => $view_settings['taxonomy_pad_counts'] ); $WPVDebug->add_log( 'info', "Basic query arguments\n". print_r( $tax_query_settings, true ) , 'query_args' ); /** * Filter wpv_filter_taxonomy_query * * This is where all the filters coming from the View settings to modify the query are (or should be) hooked * * @param $tax_query_settings the relevant elements of the View settings in an array to be used as arguments in a get_terms() call * @param $view_settings the View settings * @param $view_id the ID of the View being displayed * * @return $tax_query_settings * * @since unknown */ $tax_query_settings = apply_filters( 'wpv_filter_taxonomy_query', $tax_query_settings, $view_settings, $view_id ); $WPVDebug->add_log( 'filters', "wpv_filter_taxonomy_query\n". print_r( $tax_query_settings, true ), 'filters', 'Filter arguments before the query using <strong>wpv_filter_taxonomy_query</strong>' ); if ( isset( $taxonomies[$view_settings['taxonomy_type'][0]] ) ) { $items = get_terms( $taxonomies[$view_settings['taxonomy_type'][0]]->name, $tax_query_settings ); } else { // taxonomy no longer exists. $items = array(); } // get_terms doesn't sort by count when child count is included. // we need to do it manually. if ( $view_settings['taxonomy_orderby'] == 'count' ) { if ( $view_settings['taxonomy_order'] == 'ASC' ) { usort( $items, '_wpv_taxonomy_sort_asc' ); } else { usort( $items, '_wpv_taxonomy_sort_dec' ); } } // Filter by parent if required. // Note: We could use the 'parent' siggin in the tax_query_settings but // this doesn't return the correct post count. $parent_id = null; if ( isset( $view_settings['taxonomy_parent_mode'] ) && isset( $view_settings['taxonomy_parent_mode'][0] ) ) { switch ( $view_settings['taxonomy_parent_mode'][0] ) { case 'current_view': $parent_id = $WP_Views->get_parent_view_taxonomy(); break; case 'current_archive_loop': if ( is_category() || is_tag() || is_tax() ) { $queried_object = get_queried_object(); $parent_id = $queried_object->term_id; } break; case 'this_parent': $parent_id = $view_settings['taxonomy_parent_id']; if ( isset( $view_settings['taxonomy_type'][0] ) && ! empty( $parent_id ) ) { // WordPress 4.2 compatibility - split terms $candidate_term_id_splitted = wpv_compat_get_split_term( $parent_id, $view_settings['taxonomy_type'][0] ); if ( $candidate_term_id_splitted ) { $parent_id = $candidate_term_id_splitted; } // Adjust for WPML support $parent_id = apply_filters( 'translate_object_id', $parent_id, $view_settings['taxonomy_type'][0], true, null ); } break; } } if ( $parent_id !== null ) { foreach( $items as $index => $item ) { if ( $item->parent != $parent_id ) { unset( $items[$index] ); } } $WPVDebug->add_log( 'filters', "Filter by parent with ID {$parent_id}\n". print_r( $items, true ), 'filters', 'Filter by parent term' ); } if ( isset( $view_settings['taxonomy_terms_mode'] ) ) { switch ( $view_settings['taxonomy_terms_mode'] ) { case 'CURRENT_PAGE': if ( isset( $taxonomies[$view_settings['taxonomy_type'][0]] ) ) { global $post; if ( isset( $post ) ) { $terms = get_the_terms( $post->ID, $view_settings['taxonomy_type'][0] ); } else { $terms = array(); } if ( ! is_array( $terms ) ) { $terms = array(); } $filtered_terms = array(); $terms_info = array(); foreach ( $items as $item ) { foreach( $terms as $term ) { if ( $item->term_id == $term->term_id ) { // only add the terms in the 'taxonomy_terms' array. $filtered_terms[] = $item; $terms_info[] = $term->name . ' (id=' . $term->term_id . ')'; } } } $items = $filtered_terms; $WPVDebug->add_log( 'filters', "Filter by terms from the current page " . implode( ', ' , $terms_info ) . "\n" . print_r( $items, true ), 'filters', 'Filter by terms from the current page' ); } else { $items = array(); $WPVDebug->add_log( 'filters', "Filter by terms from the current page but for a taxonomy that no longer exists \n" . print_r( $items, true ), 'filters', 'Filter by terms from the current page' ); } break; case 'THESE': if ( isset( $view_settings['taxonomy_terms'] ) && sizeof( $view_settings['taxonomy_terms'] ) ) { $filtered_terms = array(); if ( isset( $view_settings['taxonomy_type'][0] ) && ! empty( $view_settings['taxonomy_terms'] ) ) { $adjusted_term_ids = array(); foreach ( $view_settings['taxonomy_terms'] as $candidate_term_id ) { // WordPress 4.2 compatibility - split terms $candidate_term_id_splitted = wpv_compat_get_split_term( $candidate_term_id, $view_settings['taxonomy_type'][0] ); if ( $candidate_term_id_splitted ) { $candidate_term_id = $candidate_term_id_splitted; } // WPML support $candidate_term_id = apply_filters( 'translate_object_id', $candidate_term_id, $view_settings['taxonomy_type'][0], true, null ); $adjusted_term_ids[] = $candidate_term_id; } $view_settings['taxonomy_terms'] = $adjusted_term_ids; } foreach ( $items as $item ) { if ( in_array( $item->term_id, $view_settings['taxonomy_terms'] ) ) { // only add the terms in the 'taxonomy_terms' array. $filtered_terms[] = $item; } } $items = $filtered_terms; $WPVDebug->add_log( 'filters', "Filter by specific terms " . implode( ', ' , $view_settings['taxonomy_terms'] ) . "\n". print_r( $items, true ), 'filters', 'Filter by specific terms' ); } break; case 'framework': global $WP_Views_fapi; if ( $WP_Views_fapi->framework_valid ) { if ( isset( $view_settings['taxonomy_terms_framework'] ) && '' != $view_settings['taxonomy_terms_framework'] ) { $taxonomy_terms_framework = $view_settings['taxonomy_terms_framework']; $taxonomy_terms_candidates = $WP_Views_fapi->get_framework_value( $taxonomy_terms_framework, array() ); if ( ! is_array( $taxonomy_terms_candidates ) ) { $taxonomy_terms_candidates = explode( ',', $taxonomy_terms_candidates ); } $taxonomy_terms_candidates = array_map( 'esc_attr', $taxonomy_terms_candidates ); $taxonomy_terms_candidates = array_map( 'trim', $taxonomy_terms_candidates ); // is_numeric does sanitization $taxonomy_terms_candidates = array_filter( $taxonomy_terms_candidates, 'is_numeric' ); if ( count( $taxonomy_terms_candidates ) ) { $filtered_terms = array(); if ( isset( $view_settings['taxonomy_type'][0] ) ) { $adjusted_term_ids = array(); foreach ( $taxonomy_terms_candidates as $candidate_term_id ) { // WordPress 4.2 compatibility - split terms $candidate_term_id_splitted = wpv_compat_get_split_term( $candidate_term_id, $view_settings['taxonomy_type'][0] ); if ( $candidate_term_id_splitted ) { $candidate_term_id = $candidate_term_id_splitted; } // WPML support $candidate_term_id = apply_filters( 'translate_object_id', $candidate_term_id, $view_settings['taxonomy_type'][0], true, null ); $adjusted_term_ids[] = $candidate_term_id; } $taxonomy_terms_candidates = $adjusted_term_ids; } foreach ( $items as $item ) { if ( in_array( $item->term_id, $taxonomy_terms_candidates ) ) { // only add the terms in the 'taxonomy_terms' array. $filtered_terms[] = $item; } } $items = $filtered_terms; } } } break; } } if ( isset( $wpdb->queries ) && ! empty( $wpdb->queries ) ) { $WPVDebug->add_log( 'mysql_query', $wpdb->queries , 'taxonomy' ); } $WPVDebug->add_log( 'info', print_r( $items, true ), 'query_results', '', true ); $items = array_values( $items ); /** * Filter wpv_filter_taxonomy_post_query * * Filter applied to the results of the get_terms() call * * @param $items array of terms returned by the get_terms() call * @param $tax_query_settings the relevant elements of the View settings in an array to be used as arguments in a get_terms() call * @param $view_settings the View settings * @param $view_id the ID of the View being displayed * * @return $items * * @since unknown */ $items = apply_filters( 'wpv_filter_taxonomy_post_query', $items, $tax_query_settings, $view_settings, $view_id ); $WPVDebug->add_log( 'filters', "wpv_filter_taxonomy_post_query\n" . print_r( $items, true ), 'filters', 'Filter the returned query using <strong>wpv_filter_taxonomy_post_query</strong>' ); return $items; }
function _wpv_get_adjusted_terms( $term_ids, $category_name ) { if ( ! empty( $term_ids ) ) { $adjusted_term_ids = array(); foreach ( $term_ids as $candidate_term_id ) { // WordPress 4.2 compatibility - split terms $candidate_term_id_splitted = wpv_compat_get_split_term( $candidate_term_id, $category_name ); if ( $candidate_term_id_splitted ) { $candidate_term_id = $candidate_term_id_splitted; } // WPML support $candidate_term_id = apply_filters( 'translate_object_id', $candidate_term_id, $category_name, true, null ); $adjusted_term_ids[] = $candidate_term_id; } $term_ids = $adjusted_term_ids; } return $term_ids; }
/** * wpv_get_list_item_ui_taxonomy_term * * Render taxonomy term filter item content in the filters list * * @param $view_settings * * @since unknown */ static function wpv_get_list_item_ui_taxonomy_term( $view_settings = array() ) { if ( isset( $view_settings['taxonomy_type'] ) && is_array( $view_settings['taxonomy_type'] ) ) { $view_settings['taxonomy_type'] = $view_settings['taxonomy_type'][0]; } if ( ! isset( $view_settings['taxonomy_terms_mode'] ) ) { $view_settings['taxonomy_terms_mode'] = 'THESE'; } if ( ! isset( $view_settings['taxonomy_terms'] ) ) { $view_settings['taxonomy_terms'] = array(); } if ( ! empty( $view_settings['taxonomy_terms'] ) ) { $adjusted_term_ids = array(); foreach ( $view_settings['taxonomy_terms'] as $candidate_term_id ) { // WordPress 4.2 compatibility - split terms $candidate_term_id_splitted = wpv_compat_get_split_term( $candidate_term_id, $view_settings['taxonomy_type'] ); if ( $candidate_term_id_splitted ) { $candidate_term_id = $candidate_term_id_splitted; } // WPML support $candidate_term_id = apply_filters( 'translate_object_id', $candidate_term_id, $view_settings['taxonomy_type'], true, null ); $adjusted_term_ids[] = $candidate_term_id; } $view_settings['taxonomy_terms'] = $adjusted_term_ids; } ob_start() ?> <p class='wpv-filter-taxonomy-term-summary js-wpv-filter-summary js-wpv-filter-taxonomy-term-summary'> <?php echo wpv_get_filter_taxonomy_term_summary_txt( $view_settings ); ?> </p> <?php WPV_Filter_Item::simple_filter_list_item_buttons( 'taxonomy-term', 'wpv_filter_taxonomy_term_update', wp_create_nonce( 'wpv_view_filter_taxonomy_term_nonce' ), 'wpv_filter_taxonomy_term_delete', wp_create_nonce( 'wpv_view_filter_taxonomy_term_delete_nonce' ) ); ?> <div id="wpv-filter-taxonomy-term-edit" class="wpv-filter-edit js-wpv-filter-edit"> <div id="wpv-filter-taxonomy-term" class="js-wpv-filter-options js-wpv-filter-taxonomy-term-options"> <?php WPV_Taxonomy_Term_Filter::wpv_render_taxonomy_term_options( $view_settings ); ?> </div> <div class="js-wpv-filter-toolset-messages"></div> </div> <?php $res = ob_get_clean(); return $res; }