Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor cleanup #28

Merged
merged 1 commit into from
Sep 20, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions plugin/class-facet-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,41 @@
class Facet_Post_Type implements Facet
{

var $_post_to_label = array( // pretty names
public $_post_to_label = array( // pretty names
'post_author' => 'Author',
'post_status' => 'Status',
'post_parent' => 'Parent',
'post_type' => 'Post Type',
'post_mime_type' => 'MIME Type',
);

var $_post_to_queryvar = array(
public $_post_to_queryvar = array(
'post_author' => 'author_name',
'post_status' => 'post_status',
'post_parent' => 'post_parent',
'post_type' => 'post_type',
'post_mime_type' => 'post_mime_type',
);

var $_queryvar_to_post = array(
public $_queryvar_to_post = array(
'author_name' => 'post_author',
'post_status' => 'post_status',
'post_parent' => 'post_parent',
'post_type' => 'post_type',
'post_mime_type' => 'post_mime_type',
);

var $_permastructs = array(
public $_permastructs = array(
'post_author' => FALSE,
'post_status' => FALSE,
'post_parent' => FALSE,
'post_type' => TRUE,
'post_mime_type' => FALSE,
);

var $ttl = 600; // 10 minutes
public $ttl = 600; // 10 minutes

function __construct( $name , $args , $facets_object )
public function __construct( $name , $args , $facets_object )
{
$post_type = get_post_type_object( 'post' );

Expand All @@ -50,7 +50,7 @@ function __construct( $name , $args , $facets_object )
$this->query_var = $this->_post_to_queryvar[ $name ];
}

function register_query_var()
public function register_query_var()
{
if ( TRUE === $this->query_var )
{
Expand All @@ -62,13 +62,14 @@ function register_query_var()
return $this->query_var;
}

function parse_query( $query_terms , $wp_query )
public function parse_query( $query_terms, $wp_query )
{
// identify the terms in this query
foreach( array_filter( array_map( 'trim' , (array) preg_split( '/[,\+\|]/' , $query_terms ) ) ) as $val )
$terms = array_filter( array_map( 'trim', (array) preg_split( '/[,\+\|]/', $query_terms ) ) );
foreach( $terms as $val )
{
if( $post_type = get_post_type_object( $val ) )
{
{
$this->selected_terms[$post_type->name] = (object) array(
'facet' => $this->name,
'slug' => $post_type->name,
Expand All @@ -82,7 +83,7 @@ function parse_query( $query_terms , $wp_query )
return $this->selected_terms;
}

function get_terms_in_corpus()
public function get_terms_in_corpus()
{
if( isset( $this->terms_in_corpus ) )
{
Expand All @@ -92,14 +93,14 @@ function get_terms_in_corpus()
if( ! $this->terms_in_corpus = wp_cache_get( 'terms-in-corpus' , 'scrib-facet-post-type' ) )
{
global $wpdb;

$terms = $wpdb->get_results( 'SELECT post_type, COUNT(*) AS hits FROM ' . $wpdb->posts . ' WHERE post_status = "publish" GROUP BY post_type LIMIT 1000 /* generated in Facet_Post_Type::get_terms_in_corpus() */' );

$this->terms_in_corpus = array();
foreach( $terms as $term )
{
$post_type = get_post_type_object( $term->post_type );

if( empty( $post_type ) )
{
continue;
Expand All @@ -121,7 +122,7 @@ function get_terms_in_corpus()
return $this->terms_in_corpus;
}

function get_terms_in_found_set()
public function get_terms_in_found_set()
{
if( isset( $this->terms_in_found_set ) )
{
Expand All @@ -145,16 +146,16 @@ function get_terms_in_found_set()
$terms = $wpdb->get_results( 'SELECT post_type , COUNT(*) AS hits FROM ' . $wpdb->posts . ' WHERE ID IN (' . implode( ',' , $matching_post_ids ) . ') GROUP BY post_type LIMIT 1000 /* generated in Facet_Post_Type::get_terms_in_found_set() */' );

$this->terms_in_found_set = array();

foreach( $terms as $term )
{
$post_type = get_post_type_object( $term->post_type );

if( empty( $post_type ))
{
continue;
}

$this->terms_in_found_set[] = (object) array(
'facet' => $this->name,
'slug' => $post_type->name,
Expand All @@ -171,7 +172,7 @@ function get_terms_in_found_set()
return $this->terms_in_found_set;
}

function get_terms_in_post( $post_id = FALSE )
public function get_terms_in_post( $post_id = FALSE )
{
if( ! $post_id )
{
Expand All @@ -184,7 +185,7 @@ function get_terms_in_post( $post_id = FALSE )
}

$post_type = get_post_type_object( get_post( $post_id )->post_type );

$count = wp_count_posts( $post_type );

$this->terms_in_post[] = (object) array(
Expand All @@ -199,31 +200,31 @@ function get_terms_in_post( $post_id = FALSE )
return $this->terms_in_post;
}

function selected( $term )
public function selected( $term )
{
return( isset( $this->selected_terms[ ( is_object( $term ) ? $term->slug : $term ) ] ) );
}

function queryterm_add( $term , $current )
public function queryterm_add( $term, $current )
{
$current[ $term->slug ] = $term;
return $current;
}

function queryterm_remove( $term , $current )
public function queryterm_remove( $term, $current )
{
unset( $current[ $term->slug ] );
return $current;
}

function permalink( $terms )
public function permalink( $terms )
{
if( empty( $terms ))
{
return;
}
// This only works for the first post_type

// This only works for the first post_type
return get_post_type_archive_link( current( $terms )->term_id );
}
}
11 changes: 8 additions & 3 deletions plugin/class-facets.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ public function parse_query( $query )
$this->selected_facets = (object) array();
foreach( $searched as $k => $v )
{
$this->selected_facets->{$this->_query_vars[ $k ]} = $this->facets->{$this->_query_vars[ $k ]}->parse_query( $v , $query );
if ( is_array( $v ) )
{
continue;
}// end if

$this->selected_facets->{$this->_query_vars[ $k ]} = $this->facets->{$this->_query_vars[ $k ]}->parse_query( $v, $query );
$this->selected_facets_counts->{$this->_query_vars[ $k ]} = count( (array) $this->selected_facets->{$this->_query_vars[ $k ]} );
}

Expand Down Expand Up @@ -253,13 +258,13 @@ public function get_queryterms( $facet , $term , $additive = -1 )
{
case 1: // TRUE add this facet to the other facets in the previous query
$vars = clone $this->selected_facets;
$vars->{$facet} = $this->facets->$facet->queryterm_add( $term , $vars->{$facet} );
$vars->{$facet} = $this->facets->$facet->queryterm_add( $term, isset( $vars->{$facet} ) ? $vars->{$facet} : '' );
return $vars;
break;

case 0: // FALSE remove this term from the current query vars
$vars = clone $this->selected_facets;
$vars->$facet = $this->facets->$facet->queryterm_remove( $term , $vars->{$facet} );
$vars->$facet = $this->facets->$facet->queryterm_remove( $term, isset( $vars->{$facet} ) ? $vars->{$facet} : '' );
if( ! count( (array) $vars->$facet ))
unset( $vars->$facet );
return $vars;
Expand Down
2 changes: 2 additions & 0 deletions plugin/class-scriblio.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class Scriblio
),
);

public $facets;

public function __construct()
{
add_action( 'wp_loaded', array( $this, 'wp_loaded' ), 1 );
Expand Down