function wpv_before_display_post_post_relationship($post, $view_id)
{
    static $related = array();
    global $WP_Views;
    if (function_exists('wpcf_pr_get_belongs')) {
        if (!isset($related[$post->post_type])) {
            $related[$post->post_type] = wpcf_pr_get_belongs($post->post_type);
        }
        if (is_array($related[$post->post_type])) {
            foreach ($related[$post->post_type] as $post_type => $data) {
                $related_id = wpcf_pr_post_get_belongs($post->ID, $post_type);
                if ($related_id) {
                    $WP_Views->set_variable($post_type . '_id', $related_id);
                }
            }
        }
    }
}
コード例 #2
0
 /**
  * Generates checksums for defined content types.
  * 
  * @param type $type
  * @param type $item_id
  * @return type
  */
 function generate_checksum($type, $item_id = null)
 {
     switch ($type) {
         case 'group':
             $checksum = wpcf_admin_fields_get_group($item_id);
             $checksum['meta'] = $this->get_group_meta($item_id);
             ksort($checksum['meta']);
             break;
         case 'field':
             $checksum = wpcf_admin_fields_get_field($item_id);
             break;
         case 'custom_post_type':
             $checksum = wpcf_get_custom_post_type_settings($item_id);
             $checksum['relationship_settings']['has'] = wpcf_pr_get_has($item_id);
             if (is_array($checksum['relationship_settings']['has'])) {
                 ksort($checksum['relationship_settings']['has']);
             }
             $checksum['relationship_settings']['belongs'] = wpcf_pr_get_belongs($item_id);
             if (is_array($checksum['relationship_settings']['belongs'])) {
                 ksort($checksum['relationship_settings']['belongs']);
             }
             break;
         case 'custom_taxonomy':
             $checksum = wpcf_get_custom_taxonomy_settings($item_id);
             break;
         default:
             /*
              * Enable $this->generate_checksum('test');
              */
             $checksum = $type;
             break;
     }
     // Unset various not wanted data
     foreach ($this->_remove_data_keys as $key) {
         if (isset($checksum[$key])) {
             unset($checksum[$key]);
         }
     }
     if (is_array($checksum)) {
         ksort($checksum);
     }
     //        debug( $checksum, false );
     return md5(maybe_serialize($checksum));
 }
コード例 #3
0
 function record_post_relationship_belongs($content)
 {
     global $post;
     $this->post_relationship_depth = $this->post_relationship_depth + 1;
     if (!empty($post->ID) && function_exists('wpcf_pr_get_belongs')) {
         if (!isset($this->relations[$post->post_type])) {
             $this->relations[$post->post_type] = wpcf_pr_get_belongs($post->post_type);
         }
         if (is_array($this->relations[$post->post_type])) {
             foreach ($this->relations[$post->post_type] as $post_type => $data) {
                 $related_id = wpcf_pr_post_get_belongs($post->ID, $post_type);
                 if ($related_id) {
                     $this->post_relationship['$' . $post_type . '_id'] = $related_id;
                 } else {
                     $this->post_relationship['$' . $post_type . '_id'] = 0;
                 }
             }
         }
     }
     $this->post_relationship_track[$this->post_relationship_depth] = $this->post_relationship;
     return $content;
 }
コード例 #4
0
function wpv_shortcode_wpv_control_item( $atts, $value ) {
	global $sitepress;
	// First control checks
	if ( !function_exists( 'wpcf_pr_get_belongs' ) ) {
		return __( 'You need the Types plugin to render this parametric search control', 'wpv-views' );
	}
	if ( !isset( $atts['url_param'] ) || empty( $atts['url_param'] ) ) {
		return __('The url_param argument is missing from the wpv-control-set shortcode.', 'wpv-views');
	}
	if ( !isset( $atts['type'] ) || empty( $atts['type'] ) ) {
		return __('The type argument needs to be set in the wpv-control-item shortcode.', 'wpv-views');
	}
	if ( !isset( $atts['ancestor_type'] ) || empty( $atts['ancestor_type'] ) ) {
		return __('The ancestor_type argument is missing from the wpv-control-item shortcode.', 'wpv-views');
	}
	if ( !isset( $atts['ancestor_tree'] ) || empty( $atts['ancestor_tree'] ) ) {
		return __('The ancestors argument is missing from the wpv-control-set shortcode.', 'wpv-views');
	}
	if ( !isset( $atts['returned_pt_parents'] ) || empty( $atts['returned_pt_parents'] ) ) {
		return __('The post types listed in this View do not have ancestors.', 'wpv-views');
	}
	extract(
		shortcode_atts( array(
				'type' => '', // select, multi-select, checbox, checkboxes, radio/radios, date/datepicker, textfield
				'url_param' => '', // URL parameter to be used
				'ancestor_type' => '',
				'ancestor_tree' => '',
				'default_label' => '',
				'returned_pt' => '',
				'returned_pt_parents' => '',
				'format' => false,
				'orderby' => 'title', // can be any key of $allowed_orderby_values
				'order' => 'ASC', // ASC or DESC
                'style' => '', // inline styles for input
                'class' => '', // input classes
                'label_style' => '', // inline styles for input label
                'label_class' => '' // classes for input label
			), $atts)
	);
    
    $style = esc_attr( $style );
    $class = esc_attr( $class );
    $label_style = esc_attr( $label_style );
    $label_class = esc_attr( $label_class );   
    
	$ancestor_tree_array = explode( '>', $ancestor_tree ); // NOTE this makes it useful for just one-branch scenarios, might extend this
	if ( !in_array( $ancestor_type, $ancestor_tree_array ) ) {
		return __( 'The ancestor_type argument refers to a post type that is not included in the ancestors tree.', 'wpv-views' );
	}
	global $wpdb, $WP_Views;
	$return = '';
	$this_type_parent_classes = array();
	$returned_post_types = explode( ',', $returned_pt );
	$returned_post_type_parents = explode( ',', $returned_pt_parents );
	
	$filter_full_list = false;
	
	$this_tree_ground = end( $ancestor_tree_array );
	$this_tree_roof = reset( $ancestor_tree_array );
	if ( !in_array( $this_tree_ground, $returned_post_type_parents ) ) {
		return __( 'The ancestors argument does not end with a valid parent for the returned post types on this View.', 'wpv-views' );
	}
	
	if ( !empty( $default_label ) ) {
		$aux_array = $WP_Views->view_used_ids;
		$view_name = get_post_field( 'post_name', end($aux_array));
		$default_label = wpv_translate( $ancestor_type . '_default_label', $default_label, false, 'View ' . $view_name );
	}

	// Validate order and orderby arguments for SQL query (ignore invalid values).

	// Allowed values and their translation into names of wp_posts columns.
	$allowed_orderby_values = array(
			'id' => 'ID',
			'title' => 'post_title',
			'date' => 'post_date',
			'date_modified' => 'post_modified',
			'comment_count' => 'comment_count' );
	if( ! isset( $allowed_orderby_values[ $orderby ] ) ) {
		$orderby = 'title';
	}
	// Now $orderby contains a valid column name at all times.
	$orderby = $allowed_orderby_values[ $orderby ];
	// Default to ASC on invalid $order value.
	$order = ( 'DESC' == strtoupper( $order ) ) ? 'DESC' : 'ASC';
	
	$view_settings = $WP_Views->get_view_settings();
	$dependant = false;
	$counters = false;
	$empty_action = array();
	if ( isset( $view_settings['dps'] ) && is_array( $view_settings['dps'] ) && isset( $view_settings['dps']['enable_dependency'] ) && $view_settings['dps']['enable_dependency'] == 'enable' ) {
		$dependant = true;
		$force_disable_dependant = $WP_Views->get_force_disable_dependant_parametric_search();
		if ( $force_disable_dependant ) {
			$dependant = false;
		}
	}
	if ( $format && strpos( $format, '%%COUNT%%' ) !== false ) {
		$counters = true;
	}
	
	if ( ( $dependant || $counters ) && $this_tree_ground == $ancestor_type ) {
		$wpv_data_cache = array();
		// Construct $wpv_data_cache
		if ( !isset( $_GET[$url_param] ) || empty( $_GET[$url_param] ) || $_GET[$url_param] === 0 || ( is_array( $_GET[$url_param] ) && in_array( (string) 0, $_GET[$url_param] ) ) ) {
			// This is when there is no value selected
			global $wp_object_cache;
			$wpv_data_cache = isset( $wp_object_cache->cache ) ? $wp_object_cache->cache : array();
		} else {
			// When there is a selected value, create a pseudo-cache based on all the other filters
			$query = wpv_get_dependant_view_query_args();
			$aux_cache_query = null;
			$filter_full_list = true;
			if ( isset( $query['post__in'] ) && is_array( $query['post__in'] ) && isset( $query['pr_filter_post__in'] ) && is_array( $query['pr_filter_post__in'] ) ) {
				$diff = array_diff( $query['post__in'], $query['pr_filter_post__in'] );
				if ( empty( $diff ) ) {// TODO maybe we can skip the query here
					unset( $query['post__in'] );
				} else {
					$query['post__in'] = $diff;
				}
			}
			$aux_cache_query = new WP_Query($query);
			if ( is_array( $aux_cache_query->posts ) && !empty( $aux_cache_query->posts ) ) {
				$f_fields = array( '_wpcf_belongs_' . $ancestor_type . '_id' );
				$wpv_data_cache = wpv_custom_cache_metadata( $aux_cache_query->posts, array( 'cf' => $f_fields ) );
			}
		}
		if ( !isset( $wpv_data_cache['post_meta'] ) ) {
			$wpv_data_cache['post_meta'] = array();
		}
		$empty_default = 'hide';
		$empty_alt = 'disable';
		$empty_options = array( 'select', 'radios', 'checkboxes' ); // multi-select is a special case because of dashes and underscores
		foreach ( $empty_options as $empty_opt ) {
			if ( isset( $view_settings['dps']['empty_' . $empty_opt] ) && $view_settings['dps']['empty_' . $empty_opt] == $empty_alt ) {
				$empty_action[$empty_opt] = $empty_alt;
			} else {
				$empty_action[$empty_opt] = $empty_default;
			}
		}
		if ( isset( $view_settings['dps']['empty_multi_select'] ) && $view_settings['dps']['empty_multi_select'] == $empty_alt ) {
			$empty_action['multi-select'] = $empty_alt;
		} else {
			$empty_action['multi-select'] = $empty_default;
		}
	}
	
	if ( $this_tree_ground == $ancestor_type ) {
	//	$this_type_parent_classes[] = 'js-wpv-post-relationship-real-parent';
		$this_type_parent_classes[] = 'js-wpv-filter-trigger';
	} else {
		$this_type_parent_classes[] = 'js-wpv-post-relationship-update';
	}
	
	if ( $this_tree_roof == $ancestor_type ) {
		$values_to_prepare = array();
		// Adjust query for WPML support
		$wpml_join = $wpml_where = "";
		if (
			isset( $sitepress ) 
			&& function_exists( 'icl_object_id' )
		) {
			$current_pt_translatable = $sitepress->is_translated_post_type( $ancestor_type );
			if ( $current_pt_translatable ) {
				$wpml_current_language = $sitepress->get_current_language();
				$wpml_join = " JOIN {$wpdb->prefix}icl_translations t ";
				$wpml_where = " AND p.ID = t.element_id AND t.language_code = %s ";
				$values_to_prepare[] = $wpml_current_language;
			}
		}
		$values_to_prepare[] = $ancestor_type;
		$pa_results = $wpdb->get_results(
			$wpdb->prepare(
				"SELECT p.ID, p.post_title
				FROM {$wpdb->posts} p {$wpml_join}
				WHERE p.post_status = 'publish' 
				{$wpml_where} 
				AND p.post_type = %s 
				ORDER BY p.{$orderby} {$order}",
				$values_to_prepare
			)
		);
	} else {
		$aux_position_array = array_keys( $ancestor_tree_array, $ancestor_type );
		if ( count( $aux_position_array ) > 1 ) {
			return __( 'There seems to be some kind of infinite loop happening.', 'wpv-views' );
		}
		$this_type_parents = array_slice( $ancestor_tree_array, 0, $aux_position_array[0] );
		foreach ( $this_type_parents as $ttp_item )  {
			$this_type_parent_classes[] = 'js-wpv-' . $ttp_item . '-watch';
		}
		
		$this_parent_parents = array();
		$this_parent_parents = wpcf_pr_get_belongs( $ancestor_type );
		if ( $this_parent_parents != false && is_array( $this_parent_parents ) ) {
			$this_parent_parents_array = array_merge( array_values( array_keys( $this_parent_parents ) ) );
		}
		
		$real_influencer_array = array_intersect( $this_parent_parents_array, $this_type_parents );
		$query_here = array();
		$query_here['posts_per_page'] = -1;
		$query_here['paged'] = 1;
		$query_here['offset'] = 0;
		$query_here['fields'] = 'ids';
		$query_here['post_type'] = $ancestor_type;
		foreach ( $real_influencer_array as $real_influencer ) {
			if ( isset( $_GET[$url_param . '-' . $real_influencer] ) && !empty( $_GET[$url_param . '-' . $real_influencer] ) && $_GET[$url_param . '-' . $real_influencer] != array( 0 ) ) {
				$query_here['meta_query'][] = array(
					'key' => '_wpcf_belongs_' . $real_influencer . '_id',
					'value' => $_GET[$url_param . '-' . $real_influencer]
				);
			}
		}
		if ( isset( $query_here['meta_query'] ) ) {
			$query_here['meta_query']['relation'] = 'AND';
			$aux_relationship_query = new WP_Query( $query_here );
			if ( 
				is_array( $aux_relationship_query->posts ) 
				&& count( $aux_relationship_query->posts ) 
			) {
				// If there are posts with those requirements, get their ID and post_title
				// We do not really need sanitization here, as $aux_relationship_query->posts only contains IDs come from the database, but still
				$values_to_prepare = array();
				$aux_rel_count = count( $aux_relationship_query->posts );
				$aux_rel_placeholders = array_fill( 0, $aux_rel_count, '%d' );
				foreach ( $aux_relationship_query->posts as $aux_rel_id ) {
					$values_to_prepare[] = $aux_rel_id;
				}
				$pa_results = $wpdb->get_results(
					$wpdb->prepare(
						"SELECT ID, post_title
						FROM {$wpdb->posts}
						WHERE post_status = 'publish' AND ID IN (" . implode( ",", $aux_rel_placeholders ) . ")
						ORDER BY {$orderby} {$order}",
						$values_to_prepare
					)
				);
			} else {
				//If there are no posts with those requeriments, render no posts
				$pa_results = array();
			}
		} else {
			$pa_results = array();
		}
	}
	//$pa_results = $wpdb->get_results( "SELECT ID, post_title FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type = '{$ancestor_type}'" );
	// Render different controls based on the $type attribute
	switch ( $type ) {
		case 'select':
		case 'multi-select':
			// Add the default value to the top of the options if $type is select, with a 0 value
			$options = array();
			if ( $type == 'select' ) {
				if ( empty( $default_label ) ) {
					$options[''] = 0;
				} else {
					$options[$default_label] = 0;
				}
			}
			// Create the basic $element that will hold the wpv_form_control attributes
			$element = array( 'field' => array(
							'#type' => 'select',
							'#attributes' => array( 'style' => $style, 'class' => $class . ' ', 'data-currentposttype' => $ancestor_type ),
							'#inline' => true
					)
			);
			// Build the name, id and default values depending whether we are dealing with a real parent or not
			$element['field']['#default_value'] = array( 0 );
			if ( $ancestor_type == $this_tree_ground ) {
				$element['field']['#name'] = $url_param . '[]';
				$element['field']['#id'] = 'wpv_control_' . $type . '_' . $url_param;
				if ( isset( $_GET[$url_param] ) ) {
					$element['field']['#default_value'] = $_GET[$url_param];
				}
			} else {
				$element['field']['#name'] = $url_param . '-' . $ancestor_type . '[]';
				$element['field']['#id'] = 'wpv_control_' . $type . '_' . $url_param . '_' . $ancestor_type;
				if ( isset( $_GET[$url_param . '-' . $ancestor_type] ) ) {
					$element['field']['#default_value'] = $_GET[$url_param . '-' . $ancestor_type];
				}
			}
			// Security check: this must always be an array!
			if ( ! is_array( $element['field']['#default_value'] ) ) {
				$element['field']['#default_value'] = array( $element['field']['#default_value'] );
			}
			// Loop through the posts and add them as options like post_title => ID
			foreach ( $pa_results as $pa_item ) {
				
				$options[$pa_item->post_title] = array(
					'#title' => $pa_item->post_title,
					'#value' => $pa_item->ID,
					'#inline' => true,
					'#after' => '<br />'
				);
				// Dependant stuf
				if ( $this_tree_ground == $ancestor_type ) {
					if ( $format ) {
						$display_value_formatted_name = str_replace( '%%NAME%%', $options[$pa_item->post_title]['#title'], $format );
						$options[$pa_item->post_title]['#title'] = $display_value_formatted_name;
					}
					if ( $dependant || $counters  ) {
						$val = $pa_item->ID;
						$this_query = $WP_Views->get_query();
						if ( isset( $_GET[$url_param] ) && is_array( $_GET[$url_param] ) && in_array( $val, $_GET[$url_param] ) && is_object( $this_query ) && !$counters ) {
							$this_checker = $this_query->found_posts;
						} else {
							$meta_criteria_to_filter = array( '_wpcf_belongs_' . $ancestor_type . '_id' => array( $val ) );
							$data = array();
							$data['list'] = $wpv_data_cache['post_meta'];
							$data['args'] = $meta_criteria_to_filter;
							$data['kind'] = '';
							$data['comparator'] = 'equal';
							$data['filter_full_list'] = $filter_full_list;
							if ( $counters ) {
								$data['count_matches'] = true;
							}
							$this_checker = wpv_list_filter_checker( $data );
						}
						if ( $counters ) {
							$display_value_formatted_counter = $options[$pa_item->post_title]['#title'];
							$display_value_formatted_counter = str_replace( '%%COUNT%%', $this_checker, $display_value_formatted_counter );
							$options[$pa_item->post_title]['#title'] = $display_value_formatted_counter;
						}
						if ( !$this_checker && !in_array( $pa_item->ID, $element['field']['#default_value'] ) && $dependant ) {
							$options[$pa_item->post_title]['#disable'] = 'true';
							$options[$pa_item->post_title]['#labelclass'] = 'wpv-parametric-disabled';
							if ( isset( $empty_action[$type] ) && $empty_action[$type] == 'hide' ) {
								unset( $options[$pa_item->post_title] );
							}
						}
					}
				}
			}
			$element['field']['#options'] = $options;
			// If there are no options and is multi-select, break NOTE this break is for hide, maybe disable, we will see
			if ( $type == 'multi-select' && count( $options ) == 0 ) {
			//	break;
			}
			// Add classnames js-wpv-{slug}-watch for each post type slug in any tree that is ancestor of the current one, to be able to act on their changes
			if ( count( $this_type_parent_classes ) ) {
				$element['field']['#attributes']['class'] .= implode( ' ', $this_type_parent_classes );
			}
			// If there is only one option for select or none for multi-select, disable this form control NOTE review this
			if ( count( $options ) == 1 && $type == 'select' ) {
				$element['field']['#attributes']['disabled'] = 'disabled';
			}
			// If $type is multi-select, use it
			if ( $type == 'multi-select' ) {
				$element['field']['#multiple'] = 'multiple';
			}
			// Create the form control and add it to the $returned_value
			$return .= wpv_form_control( $element );
			break;
		case 'checkboxes':
			// If there are no options, break
			if ( count( $pa_results ) == 0 ) {
				$options = array();
			}
			// Create the basic $element that will hold the wpv_form_control attributes
			$element = array( 'field' => array(
						'#type' => $type,
						'#attributes' => array( 'style' => $style, 'class' => $class ),
						'#inline' => true,
						'#before' => '<div class="wpcf-checkboxes-grou">', //we need to wrap them for js purposes
						'#after' => '</div>'
			) );
			// Build the name, id and default values depending whether we are dealing with a real parent or not
			$element['field']['#default_value'] = array( -1 );
			if ( $ancestor_type == $this_tree_ground ) {
				$checkbox_url_param = $url_param . '[]';
				if ( isset( $_GET[$url_param] ) ) {
					$element['field']['#default_value'] = $_GET[$url_param];
				}
				$element['field']['#name'] = $url_param . '[]';
				$element['field']['#id'] = 'wpv_control_' . $type . '_' . $url_param;
			} else {
				$checkbox_url_param = $url_param . '-' . $ancestor_type . '[]';
				if ( isset( $_GET[$url_param . '-' . $ancestor_type] ) ) {
					$element['field']['#default_value'] = $_GET[$url_param . '-' . $ancestor_type];
				}
				$element['field']['#name'] = $url_param . '-' . $ancestor_type . '[]';
				$element['field']['#id'] = 'wpv_control_' . $type . '_' . $url_param . '_' . $ancestor_type;
			}
			// Security check: this must always be an array!
			if ( ! is_array( $element['field']['#default_value'] ) ) {
				$element['field']['#default_value'] = array( $element['field']['#default_value'] );
			}
			// Add classnames js-wpv-{slug}-watch for each post type slug in any tree that is ancestor of the current one, to be able to act on their changes
			$checkboxes_classes = '';
			if ( count( $this_type_parent_classes ) ) {
				$checkboxes_classes = implode( ' ', $this_type_parent_classes );
			}
			// Loop through the posts and add them as options
			foreach ( $pa_results as $pa_item ) {
				$options[$pa_item->ID]['#name'] = $checkbox_url_param;
				$options[$pa_item->ID]['#title'] = $pa_item->post_title;
				$options[$pa_item->ID]['#value'] = $pa_item->ID;
				$options[$pa_item->ID]['#default_value'] = in_array( $pa_item->ID, $element['field']['#default_value'] ); // set default using option titles too
				$options[$pa_item->ID]['#inline'] = true;
				$options[$pa_item->ID]['#after'] = '&nbsp;&nbsp;';
				$options[$pa_item->ID]['#attributes']['data-currentposttype'] = $ancestor_type;
				$options[$pa_item->ID]['#attributes']['data-triggerer'] = 'rel-relationship';
                $options[$pa_item->ID]['#attributes']['style'] = $style;
                $options[$pa_item->ID]['#attributes']['class'] = $class;
				if ( !empty( $checkboxes_classes ) ) {
					$options[$pa_item->ID]['#attributes']['class'] .= ' ' . $checkboxes_classes;
				}
                $options[$pa_item->ID]['#labelclass'] = $label_class;
                $options[$pa_item->ID]['#labelstyle'] = $label_style;
				// Dependant stuff
				if ( $this_tree_ground == $ancestor_type ) {
					if ( $format ) {
						$display_value_formatted_name = str_replace( '%%NAME%%', $options[$pa_item->ID]['#title'], $format );
						$options[$pa_item->ID]['#title'] = $display_value_formatted_name;
					}
					if ( $dependant || $counters  ) {
						$val = $pa_item->ID;
						$this_query = $WP_Views->get_query();
						if ( isset( $_GET[$url_param] ) && is_array( $_GET[$url_param] ) && in_array( $val, $_GET[$url_param] ) && is_object( $this_query ) && !$counters ) {
							$this_checker = $this_query->found_posts;
						} else {
							$meta_criteria_to_filter = array( '_wpcf_belongs_' . $ancestor_type . '_id' => array( $val ) );
							$data = array();
							$data['list'] = $wpv_data_cache['post_meta'];
							$data['args'] = $meta_criteria_to_filter;
							$data['kind'] = '';
							$data['comparator'] = 'equal';
							if ( $counters ) {
								$data['count_matches'] = true;
							}
							$data['filter_full_list'] = $filter_full_list;
							$this_checker = wpv_list_filter_checker( $data );
						}
						if ( $counters ) {
							$display_value_formatted_counter = $options[$pa_item->ID]['#title'];
							$display_value_formatted_counter = str_replace( '%%COUNT%%', $this_checker, $display_value_formatted_counter );
							$options[$pa_item->ID]['#title'] = $display_value_formatted_counter;
						}
						if ( !$this_checker && !in_array( $pa_item->ID, $element['field']['#default_value'] ) && $dependant ) {
							$options[$pa_item->ID]['#attributes']['#disabled'] = 'true';
							$options[$pa_item->ID]['#labelclass'] .= ' wpv-parametric-disabled';
							if ( isset( $empty_action['checkboxes'] ) && $empty_action['checkboxes'] == 'hide' ) {
								unset( $options[$pa_item->ID] );
							}
						}
					}
				}
			}
			$element['field']['#options'] = $options;
			// Calculate the control
			$return .= wpv_form_control( $element );
			break;
		case 'radio':
		case 'radios':
			// Create the basic $element that will hold the wpv_form_control attributes
			$element = array( 'field' => array(
							'#type' => 'radios',
							'#attributes' => array( 'style' => $style, 'class' => $class, 'data-currentposttype' => $ancestor_type, 'data-triggerer' => 'rel-relationship' ),
							'#inline' => true
						)
			);
			// If there are no options, break
			if ( count( $pa_results ) == 0 ) {
				$options = array();
			}
			if ( !empty( $default_label ) ) {
				$options[$default_label] = array(
					'#title' => $default_label,
					'#value' => 0,
					'#inline' => true,
					'#after' => '<br />'
				);
				if ( 
					$this_tree_ground == $ancestor_type 
					&& $dependant 
					&& count( $pa_results ) == 0 
					&& (
						! isset( $_GET[$url_param] ) 
						|| $_GET[$url_param] != '' 
					)
				) {
					$options[$default_label]['#disable'] = 'true';
					$options[$default_label]['#labelclass'] = ' wpv-parametric-disabled';
				}
			}
			// Build the name, id and default values depending whether we are dealing with a real parent or not
			$element['field']['#default_value'] = 0;
			if ( $ancestor_type == $this_tree_ground ) {
				$element['field']['#name'] = $url_param;
				$element['field']['#id'] = 'wpv_control_' . $type . '_' . $url_param;
				if ( 
					isset( $_GET[$url_param] ) 
					&& $_GET[$url_param] != 0 
				) {
					$element['field']['#default_value'] = $_GET[$url_param];
				}
			} else {
				$element['field']['#name'] = $url_param . '-' . $ancestor_type;
				$element['field']['#id'] = 'wpv_control_' . $type . '_' . $url_param . '_' . $ancestor_type;
				if ( 
					isset( $_GET[$url_param . '-' . $ancestor_type] ) 
					&& $_GET[$url_param . '-' . $ancestor_type] != 0 
				) {
					$element['field']['#default_value'] = $_GET[$url_param . '-' . $ancestor_type];
				}
			}
			// Security check: this must always be a string!
			if ( is_array( $element['field']['#default_value'] ) ) {
				$element['field']['#default_value'] = reset( $element['field']['#default_value'] );
			}
			// Loop through the posts and add them as options like post_title => ID
			foreach ( $pa_results as $pa_item ) {
				
				$options[$pa_item->post_title] = array(
					'#title' => $pa_item->post_title,
					'#value' => $pa_item->ID,
					'#inline' => true,
					'#after' => '<br />'
				);
                $options[$pa_item->post_title]['#labelclass'] = $label_class;
                $options[$pa_item->post_title]['#labelstyle'] = $label_style;
				// Dependant stuf
				if ( $this_tree_ground == $ancestor_type ) {
					if ( $format ) {
						$display_value_formatted_name = str_replace( '%%NAME%%', $options[$pa_item->post_title]['#title'], $format );
						$options[$pa_item->post_title]['#title'] = $display_value_formatted_name;
					}
					if ( $dependant || $counters ) {
						$val = $pa_item->ID;
						$this_query = $WP_Views->get_query();
						if ( isset( $_GET[$url_param] ) && !empty( $_GET[$url_param] ) && $val == esc_attr( $_GET[$url_param] ) && is_object( $this_query ) && !$counters ) {
							$this_checker = $this_query->found_posts;
						} else {
							$meta_criteria_to_filter = array( '_wpcf_belongs_' . $ancestor_type . '_id' => array( $val ) );
							$data = array();
							$data['list'] = $wpv_data_cache['post_meta'];
							$data['args'] = $meta_criteria_to_filter;
							$data['kind'] = '';
							$data['comparator'] = 'equal';
							if ( $counters ) {
								$data['count_matches'] = true;
							}
							$data['filter_full_list'] = $filter_full_list;
							$this_checker = wpv_list_filter_checker( $data );
						}
						if ( $counters ) {
							$display_value_formatted_counter = $options[$pa_item->post_title]['#title'];
							$display_value_formatted_counter = str_replace( '%%COUNT%%', $this_checker, $display_value_formatted_counter );
							$options[$pa_item->post_title]['#title'] = $display_value_formatted_counter;
						}
						if ( !$this_checker && $pa_item->ID != $element['field']['#default_value'] && $dependant ) {
							$options[$pa_item->post_title]['#disable'] = 'true';
							$options[$pa_item->post_title]['#labelclass'] .= ' wpv-parametric-disabled';
							if ( isset( $empty_action['radios'] ) && $empty_action['radios'] == 'hide' ) {
								unset( $options[$pa_item->post_title] );
							}
						}
					}
				}
			}
			$element['field']['#options'] = $options;
			// Add classnames js-wpv-{slug}-watch for each post type slug in any tree that is ancestor of the current one, to be able to act on their changes
			if ( count( $this_type_parent_classes ) ) {
				$element['field']['#attributes']['class'] .= ' '. implode( ' ', $this_type_parent_classes );
			}
			// If there is only one option, disable this form control
			//This is not really needed,asin this case we are breaking above TODO review this
			if ( count( $options ) == 0 ) {
				$element['field']['#attributes']['disabled'] = 'disabled';
			}
			// Calculate the control
			$return .= wpv_form_control( $element );
			break;
		default:
			break;
	}
	return $return;
}
コード例 #5
0
ファイル: functions.php プロジェクト: aarongillett/B22-151217
function WPV_wpcf_record_post_relationship_belongs($content)
{
    global $post, $WPV_wpcf_post_relationship, $WPV_wpcf_post_relationship_depth, $WPV_wpcf_post_relationship_track;
    static $related = array();
    $WPV_wpcf_post_relationship_depth++;
    if (!empty($post->ID) && function_exists('wpcf_pr_get_belongs')) {
        if (!isset($related[$post->post_type])) {
            $related[$post->post_type] = wpcf_pr_get_belongs($post->post_type);
        }
        if (is_array($related[$post->post_type])) {
            foreach ($related[$post->post_type] as $post_type => $data) {
                $related_id = wpcf_pr_post_get_belongs($post->ID, $post_type);
                if ($related_id) {
                    $WPV_wpcf_post_relationship['$' . $post_type . '_id'] = $related_id;
                } else {
                    $WPV_wpcf_post_relationship['$' . $post_type . '_id'] = 0;
                }
            }
        }
    }
    $WPV_wpcf_post_relationship_track[$WPV_wpcf_post_relationship_depth] = $WPV_wpcf_post_relationship;
    return $content;
}
コード例 #6
0
ファイル: functions.php プロジェクト: Netsoro/gdnlteamgroup
function WPV_wpcf_record_post_relationship_belongs($content)
{
    global $post, $WPV_wpcf_post_relationship;
    static $related = array();
    if (function_exists('wpcf_pr_get_belongs')) {
        if (!isset($related[$post->post_type])) {
            $related[$post->post_type] = wpcf_pr_get_belongs($post->post_type);
        }
        if (is_array($related[$post->post_type])) {
            foreach ($related[$post->post_type] as $post_type => $data) {
                $related_id = wpcf_pr_post_get_belongs($post->ID, $post_type);
                if ($related_id) {
                    $WPV_wpcf_post_relationship['$' . $post_type . '_id'] = $related_id;
                }
            }
        }
    }
    return $content;
}
コード例 #7
0
 function add_view_template_parent_groups($items)
 {
     global $post;
     // get current View ID
     $view_template_id = $post->ID;
     // get all view templates attached in the Settings page for single view
     $view_template_relations = $this->get_view_template_settings();
     // find view template groups and get their parents
     $current_types = array();
     $parent_types = array();
     foreach ($view_template_relations as $relation => $value) {
         if ($value == $view_template_id) {
             $current_types[] = $relation;
             if (function_exists('wpcf_pr_get_belongs')) {
                 $parent_types[] = wpcf_pr_get_belongs($relation);
             }
         }
     }
     // get parent groups
     $all_parent_groups = array();
     foreach ($parent_types as $type) {
         foreach ($type as $typename => $typeval) {
             $parent_groups = wpcf_admin_get_groups_by_post_type($typename);
         }
     }
 }
コード例 #8
0
function wpv_filter_extend_query_for_parametric_and_counters($post_query, $view_settings, $id)
{
    $dps_enabled = false;
    $counters_enabled = false;
    if (!isset($view_settings['dps']) || !is_array($view_settings['dps'])) {
        $view_settings['dps'] = array();
    }
    if (isset($view_settings['dps']['enable_dependency']) && $view_settings['dps']['enable_dependency'] == 'enable') {
        $dps_enabled = true;
        $controls_per_kind = wpv_count_filter_controls($view_settings);
        $controls_count = 0;
        $no_intersection = array();
        if (!isset($controls_per_kind['error'])) {
            //	$controls_count = array_sum( $controls_per_kind );
            $controls_count = $controls_per_kind['cf'] + $controls_per_kind['tax'] + $controls_per_kind['pr'] + $controls_per_kind['search'];
            if ($controls_per_kind['cf'] > 1 && (!isset($view_settings['custom_fields_relationship']) || $view_settings['custom_fields_relationship'] != 'AND')) {
                $no_intersection[] = __('custom field', 'wpv-views');
            }
            if ($controls_per_kind['tax'] > 1 && (!isset($view_settings['taxonomy_relationship']) || $view_settings['taxonomy_relationship'] != 'AND')) {
                $no_intersection[] = __('taxonomy', 'wpv-views');
            }
        } else {
            $dps_enabled = false;
        }
        if ($controls_count > 0) {
            if (count($no_intersection) > 0) {
                $dps_enabled = false;
            }
        } else {
            $dps_enabled = false;
        }
    }
    if (!isset($view_settings['filter_meta_html'])) {
        $view_settings['filter_meta_html'] = '';
    }
    if (strpos($view_settings['filter_meta_html'], '%%COUNT%%') !== false) {
        $counters_enabled = true;
    }
    global $WP_Views;
    if (!$dps_enabled && !$counters_enabled) {
        // Set the force value
        $WP_Views->set_force_disable_dependant_parametric_search(true);
        return $post_query;
    }
    // If after all we have dps, we need to populate the $wp_object_cache
    // Check if we need to create all the cache or just for the missing ones
    global $wp_object_cache;
    $cache_exclude_queried_posts = false;
    // we will only exclude already queried posts if there are any queried posts and the native cache exists (so they are already cached)
    $cache_use_native = true;
    if (is_object($wp_object_cache)) {
        if (isset($wp_object_cache->cache)) {
            // existing queried posts were already cached
            if (empty($post_query)) {
                $cache_exclude_queried_posts = false;
                // we passed an empty $post_query so there are no queried posts at all (surely coming from a form View shortcode)
            } else {
                $cache_exclude_queried_posts = true;
            }
        } else {
            $wp_object_cache->cache = array();
            $cache_exclude_queried_posts = false;
            // already queried posts were not cached where we need them
            $cache_use_native = false;
            // the native $wp_object_cache->cache property is not set, so we will recreate it instead of naturally caching metadata
        }
    }
    // In any case, we need to mimic the process that we used to generate the $query
    $view_settings_defaults = array('post_type' => 'any', 'orderby' => 'post-date', 'order' => 'DESC', 'paged' => '1', 'posts_per_page' => -1);
    extract($view_settings_defaults);
    $view_settings['view_id'] = $id;
    extract($view_settings, EXTR_OVERWRITE);
    $query = array('posts_per_page' => $posts_per_page, 'paged' => $paged, 'post_type' => $post_type, 'order' => $order, 'suppress_filters' => false, 'ignore_sticky_posts' => true);
    // Add special check for media (attachments) as their default status in not usually published
    if (sizeof($post_type) == 1 && $post_type[0] == 'attachment') {
        $query['post_status'] = 'any';
        // Note this can be overriden by adding a status filter.
    }
    $query = apply_filters('wpv_filter_query', $query, $view_settings, $id);
    // Now we have the $query as in the original one
    // We now need to overwrite the limit, offset, paged and pagination options
    // Also, we set it to just return the IDs
    $query['posts_per_page'] = -1;
    $query['ĺimit'] = -1;
    $query['paged'] = 1;
    $query['offset'] = 0;
    $query['fields'] = 'ids';
    if ($cache_exclude_queried_posts) {
        // do not query again already queried and cached posts
        $already = array();
        if (isset($post_query->posts) && !empty($post_query->posts)) {
            foreach ((array) $post_query->posts as $post_object) {
                $already[] = $post_object->ID;
            }
        }
        $WP_Views->returned_ids_for_parametric_search = $already;
        if (isset($query['pr_filter_post__in'])) {
            $query['post__in'] = $query['pr_filter_post__in'];
        } else {
            // If just for the missing ones, generate the post__not_in argument
            if (isset($query['post__not_in'])) {
                $query['post__not_in'] = array_merge((array) $query['post__not_in'], (array) $already);
            } else {
                $query['post__not_in'] = (array) $already;
            }
            // And adjust on the post__in argument
            if (isset($query['post__in'])) {
                $query['post__in'] = array_diff((array) $query['post__in'], (array) $query['post__not_in']);
                //unset( $query['post__in'] );
            }
        }
    }
    // Perform the query
    $aux_cache_query = new WP_Query($query);
    // In case we need to recreate our own cache object, we do not need to load there all the postmeta and taxonomy data, just for the elements involved in parametric search controls
    $filter_c_mode = isset($view_settings['filter_controls_mode']) && is_array($view_settings['filter_controls_mode']) ? $view_settings['filter_controls_mode'] : array();
    $filter_c_name = isset($view_settings['filter_controls_field_name']) && is_array($view_settings['filter_controls_field_name']) ? $view_settings['filter_controls_field_name'] : array();
    $f_taxes = array();
    $f_fields = array();
    foreach ($filter_c_mode as $f_index => $f_mode) {
        if (isset($filter_c_name[$f_index])) {
            switch ($f_mode) {
                case 'slug':
                    $f_taxes[] = $filter_c_name[$f_index];
                    break;
                case 'cf':
                    $f_fields[] = $filter_c_name[$f_index];
                    break;
                case 'rel':
                    if (function_exists('wpcf_pr_get_belongs')) {
                        $returned_post_types = $view_settings['post_type'];
                        $returned_post_type_parents = array();
                        if (empty($returned_post_types)) {
                            $returned_post_types = array('any');
                        }
                        foreach ($returned_post_types as $returned_post_type_slug) {
                            $parent_parents_array = wpcf_pr_get_belongs($returned_post_type_slug);
                            if ($parent_parents_array != false && is_array($parent_parents_array)) {
                                $returned_post_type_parents = array_merge($returned_post_type_parents, array_values(array_keys($parent_parents_array)));
                            }
                        }
                        foreach ($returned_post_type_parents as $parent_to_cache) {
                            $f_fields[] = '_wpcf_belongs_' . $parent_to_cache . '_id';
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }
    // If we are using the native caching, update the cache for the posts returned by the aux query
    if (is_array($aux_cache_query->posts) && !empty($aux_cache_query->posts)) {
        $WP_Views->returned_ids_for_parametric_search = array_merge($WP_Views->returned_ids_for_parametric_search, $aux_cache_query->posts);
        $WP_Views->returned_ids_for_parametric_search = array_unique($WP_Views->returned_ids_for_parametric_search);
        if ($cache_use_native) {
            // If we are using the native caching, update the cache for the posts returned by the aux query
            update_postmeta_cache($aux_cache_query->posts);
            update_object_term_cache($aux_cache_query->posts, $view_settings['post_type']);
        } else {
            // Else, we need to fake an $wp_object_cache->cache
            $f_data = array('cf' => $f_fields, 'tax' => $f_taxes);
            $cache_combined = wpv_custom_cache_metadata($aux_cache_query->posts, $f_data);
            $wp_object_cache->cache = $cache_combined;
        }
    }
    return $post_query;
}
コード例 #9
0
function wpv_ajax_wpv_get_post_relationship_info()
{
    if (wp_verify_nonce($_POST['wpv_nonce'], 'wpv_get_posts_select_nonce')) {
        if (function_exists('wpcf_pr_get_belongs') && isset($_POST['post_types'])) {
            $post_types = get_post_types('', 'objects');
            $output_done = false;
            foreach ($_POST['post_types'] as $post_type) {
                $related = wpcf_pr_get_belongs($post_type);
                if ($related === false) {
                    echo sprintf(__('Post type <strong>%s</strong> doesn\'t belong to any other post type', 'wpv-views'), $post_types[$post_type]->labels->singular_name, $related);
                    echo '<br />';
                    $output_done = true;
                }
                if (is_array($related) && count($related)) {
                    $keys = array_keys($related);
                    $related = array();
                    foreach ($keys as $key) {
                        $related[] = $post_types[$key]->labels->singular_name;
                    }
                }
                if (is_array($related) && count($related) == 1) {
                    $related = implode(', ', $related);
                    echo sprintf(__('Post type <strong>%s</strong> is a child of <strong>%s</strong> post type', 'wpv-views'), $post_types[$post_type]->labels->singular_name, $related);
                    echo '<br />';
                    $output_done = true;
                }
                if (is_array($related) && count($related) > 1) {
                    $last = array_pop($related);
                    $related = implode(', ', $related);
                    $related .= __(' and ') . $last;
                    echo sprintf(__('Post type <strong>%s</strong> is a child of <strong>%s</strong> post types', 'wpv-views'), $post_types[$post_type]->labels->singular_name, $related);
                    echo '<br />';
                    $output_done = true;
                }
            }
            if ($output_done) {
                echo '<br />';
            }
        }
    }
    die;
}