Add filters to convert statistic slugs to alphabetic strings

This commit is contained in:
ThemeBoy
2013-11-27 03:10:49 +11:00
parent 639f4b86da
commit 68fa7f30d4
10 changed files with 204 additions and 59 deletions

View File

@@ -7,18 +7,29 @@ function sp_metric_cpt_init() {
$args = array(
'label' => $name,
'labels' => $labels,
'public' => true,
'public' => false,
'show_ui' => true,
'show_in_nav_menus' => false,
'hierarchical' => false,
'supports' => array( 'title', 'page-attributes' ),
'register_meta_box_cb' => 'sp_metric_meta_init',
'rewrite' => array( 'slug' => 'metric' ),
'show_in_menu' => 'edit.php?post_type=sp_player'
);
register_post_type( 'sp_metric', $args );
}
add_action( 'init', 'sp_metric_cpt_init' );
function sp_metric_edit_columns() {
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Label', 'sportspress' ),
'sp_sport' => __( 'Sports', 'sportspress' ),
'sp_equation' => __( 'Equation', 'sportspress' ),
);
return $columns;
}
add_filter( 'manage_edit-sp_metric_columns', 'sp_metric_edit_columns' );
function sp_metric_meta_init() {
}
?>

View File

@@ -0,0 +1,35 @@
<?php
function sp_outcome_cpt_init() {
$name = __( 'Outcomes', 'sportspress' );
$singular_name = __( 'Outcome', 'sportspress' );
$lowercase_name = __( 'outcome', 'sportspress' );
$labels = sp_cpt_labels( $name, $singular_name, $lowercase_name, true );
$args = array(
'label' => $name,
'labels' => $labels,
'public' => false,
'show_ui' => true,
'show_in_nav_menus' => false,
'hierarchical' => false,
'supports' => array( 'title', 'page-attributes' ),
'register_meta_box_cb' => 'sp_outcome_meta_init',
'show_in_menu' => 'edit.php?post_type=sp_event'
);
register_post_type( 'sp_outcome', $args );
}
add_action( 'init', 'sp_outcome_cpt_init' );
function sp_outcome_edit_columns() {
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Label', 'sportspress' ),
'sp_sport' => __( 'Sports', 'sportspress' ),
'sp_equation' => __( 'Conditions', 'sportspress' ),
);
return $columns;
}
add_filter( 'manage_edit-sp_outcome_columns', 'sp_outcome_edit_columns' );
function sp_outcome_meta_init() {
}
?>

View File

@@ -0,0 +1,35 @@
<?php
function sp_result_cpt_init() {
$name = __( 'Results', 'sportspress' );
$singular_name = __( 'Result', 'sportspress' );
$lowercase_name = __( 'result', 'sportspress' );
$labels = sp_cpt_labels( $name, $singular_name, $lowercase_name, true );
$args = array(
'label' => $name,
'labels' => $labels,
'public' => true,
'show_ui' => true,
'show_in_nav_menus' => false,
'hierarchical' => false,
'supports' => array( 'title', 'page-attributes' ),
'register_meta_box_cb' => 'sp_result_meta_init',
'show_in_menu' => 'edit.php?post_type=sp_event'
);
register_post_type( 'sp_result', $args );
}
add_action( 'init', 'sp_result_cpt_init' );
function sp_result_edit_columns() {
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Label', 'sportspress' ),
'sp_sport' => __( 'Sports', 'sportspress' ),
'sp_equation' => __( 'Equation', 'sportspress' ),
);
return $columns;
}
add_filter( 'manage_edit-sp_result_columns', 'sp_result_edit_columns' );
function sp_result_meta_init() {
}
?>

View File

@@ -7,7 +7,8 @@ function sp_stat_cpt_init() {
$args = array(
'label' => $name,
'labels' => $labels,
'public' => true,
'public' => false,
'show_ui' => true,
'show_in_nav_menus' => false,
'hierarchical' => false,
'supports' => array( 'title', 'page-attributes' ),
@@ -23,7 +24,7 @@ function sp_stat_edit_columns() {
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Label', 'sportspress' ),
'sp_sport' => __( 'Sport', 'sportspress' ),
'sp_sport' => __( 'Sports', 'sportspress' ),
'sp_equation' => __( 'Equation', 'sportspress' ),
);
return $columns;
@@ -41,7 +42,7 @@ function sp_stat_equation_meta( $post ) {
<p class="sp-equation-selector">
<?php
foreach ( $equation as $piece ):
sp_get_equation_selector( 'stat', $piece );
sp_get_equation_selector( $post->ID, 'stat', $piece );
endforeach;
?>
</p>

View File

@@ -10,7 +10,7 @@ function sp_div_term_init() {
'labels' => $labels,
'public' => true,
'hierarchical' => true,
'rewrite' => array( 'slug' => 'division' ),
'rewrite' => array( 'slug' => 'division' )
);
register_taxonomy( 'sp_div', $object_type, $args );
}

View File

@@ -3,12 +3,14 @@ function sp_sport_term_init() {
$name = __( 'Sports', 'sportspress' );
$singular_name = __( 'Sport', 'sportspress' );
$lowercase_name = __( 'sport', 'sportspress' );
$object_type = array( 'sp_stat', 'sp_metric' );
$object_type = array( 'sp_result', 'sp_outcome', 'sp_stat', 'sp_metric' );
$labels = sp_tax_labels( $name, $singular_name, $lowercase_name );
$args = array(
'label' => $name,
'labels' => $labels,
'public' => true,
'public' => false,
'show_ui' => true,
'show_in_nav_menus' => false,
'hierarchical' => true,
'rewrite' => array( 'slug' => 'sport' )
);

View File

@@ -10,13 +10,28 @@ function sp_after_theme_setup() {
}
add_action( 'after_theme_setup', 'sp_after_theme_setup' );
function sp_admin_menu_separator() {
function sp_admin_menu() {
if ( ! current_user_can( 'manage_options' ) )
return;
global $menu;
global $menu, $submenu;
// Add separator
$menu[41] = array( '', 'read', 'separator-sportspress', '', 'wp-menu-separator sportspress' );
// Remove "Add Event" link under Events
unset( $submenu['edit.php?post_type=sp_event'][10] );
// Remove "Divisions" link under Events
unset( $submenu['edit.php?post_type=sp_event'][15] );
// Remove "Divisions" link under Players
unset( $submenu['edit.php?post_type=sp_player'][15] );
// Remove "Divisions" link under Staff
unset( $submenu['edit.php?post_type=sp_staff'][15] );
}
add_action( 'admin_menu', 'sp_admin_menu_separator' );
add_action( 'admin_menu', 'sp_admin_menu' );
function sp_manage_posts_custom_column( $column, $post_id ) {
global $post;

View File

@@ -42,4 +42,26 @@ function sportspress_the_content( $content ) {
return $content;
}
add_filter('the_content', 'sportspress_the_content');
function sp_sanitize_title( $title ) {
if ( in_array( $_POST['post_type'], array( 'sp_result', 'sp_outcome', 'sp_stat', 'sp_metric' ) ) ):
// Get post title
$title = $_POST['post_title'];
// String to lowercase
$title = strtolower( $title );
// Replace all numbers with words
$title = sp_numbers_to_words( $title );
// Remove all other non-alphabet characters
$title = preg_replace( "/[^a-z]/", '', $title );
endif;
return $title;
}
add_filter( 'sanitize_title', 'sp_sanitize_title' );
?>

View File

@@ -55,13 +55,11 @@ if ( !function_exists( 'sp_array_combine' ) ) {
}
}
if ( !function_exists( 'sp_num_to_letter' ) ) {
function sp_num_to_letter( $num, $uppercase = false ) {
$num -= 0;
$letter = chr( ( $num % 26 ) + 97 );
$letter .= ( floor( $num / 26 ) > 0 ) ? str_repeat( $letter, floor( $num / 26 ) ) : '';
return ( $uppercase ? strtoupper( $letter ) : $letter );
}
if ( !function_exists( 'sp_numbers_to_words' ) ) {
function sp_numbers_to_words( $str ) {
$output = str_replace( array( '1st', '2nd', '3rd', '5th', '8th', '9th', '10', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ), array( 'first', 'second', 'third', 'fifth', 'eight', 'ninth', 'ten', 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine' ), $str );
return $output;
}
}
if ( !function_exists( 'sp_cpt_labels' ) ) {
@@ -71,6 +69,7 @@ if ( !function_exists( 'sp_cpt_labels' ) ) {
'name' => $name,
'singular_name' => $singular_name,
'all_items' => $name,
'add_new' => sprintf( __( 'Add %s', 'sportspress' ), $singular_name ),
'add_new_item' => sprintf( __( 'Add New %s', 'sportspress' ), $singular_name ),
'edit_item' => sprintf( __( 'Edit %s', 'sportspress' ), $singular_name ),
'new_item' => sprintf( __( 'New %s', 'sportspress' ), $singular_name ),
@@ -257,9 +256,62 @@ if ( !function_exists( 'sp_post_checklist' ) ) {
}
}
if ( !function_exists( 'sp_get_equation_optgroup_array' ) ) {
function sp_get_equation_optgroup_array( $postid, $type = null, $variations = null, $defaults = null ) {
$arr = array();
// Get stats within the sports that the current stat is in ### TODO: should be for sport selected
$args = array(
'post_type' => $type,
'numberposts' => -1,
'posts_per_page' => -1,
'exclude' => $postid
);
$sports = get_the_terms( $postid, 'sp_sport' );
if ( ! empty( $sports ) ):
$terms = array();
foreach ( $sports as $sport ):
$terms[] = $sport->slug;
endforeach;
$args['tax_query'] = array(
array(
'taxonomy' => 'sp_sport',
'field' => 'slug',
'terms' => $terms
)
);
endif;
$vars = get_posts( $args );
// Add extra vars to the array
if ( isset( $defaults ) && is_array( $defaults ) ):
foreach ( $defaults as $key => $value ):
$arr[ $key ] = $value;
endforeach;
endif;
// Add vars to the array
if ( isset( $variations ) && is_array( $variations ) ):
foreach ( $vars as $var ):
foreach ( $variations as $key => $value ):
$arr[ $var->post_name . '_' . $key ] = $var->post_title . ' ' . $value;
endforeach;
endforeach;
else:
foreach ( $vars as $var ):
$arr[ $var->post_name ] = $var->post_title;
endforeach;
endif;
return (array) $arr;
}
}
if ( !function_exists( 'sp_get_equation_selector' ) ) {
function sp_get_equation_selector( $type = null, $selected = null ) {
function sp_get_equation_selector( $postid, $type = null, $selected = null ) {
if ( ! isset( $postid ) )
return;
// Initialize options array
$options = array();
@@ -268,57 +320,27 @@ if ( !function_exists( 'sp_get_equation_selector' ) ) {
if ( $type == 'stat' ):
// Create array of events ## TODO: should be a custom post under events called outcomes
$events = array( 'wins' => __( 'Wins', 'sportspress' ), 'draws' => __( 'Draws', 'sportspress' ), 'ties' => __( 'Ties', 'sportspres' ), 'losses' => __( 'Losses', 'sportspress' ) );
// Add events to options
$options[ __( 'Results', 'sportspress' ) ] = sp_get_equation_optgroup_array( $postid, 'sp_result', array( 'for' => '&rarr;', 'against' => '&larr;' ) );
// Add events to options
$options[ __( 'Events', 'sportspress' ) ] = (array) $events;
$vars = array();
// Get stats within the sports that the current stat is in ### TODO: should be for sport selected
$args = array(
'post_type' => 'sp_stat',
'numberposts' => -1,
'posts_per_page' => -1,
'exclude' => $post->ID
);
$sports = get_the_terms( $post->ID, 'sp_sport' );
if ( ! empty( $sports ) ):
$terms = array();
foreach ( $sports as $sport ):
$terms[] = $sport->slug;
endforeach;
$args['tax_query'] = array(
array(
'taxonomy' => 'sp_sport',
'field' => 'slug',
'terms' => $terms
)
);
endif;
$stats = get_posts( $args );
// Add vars to the array
foreach ( $stats as $stat ):
$vars[ $stat->post_name ] = $stat->post_title;
endforeach;
$options[ __( 'Outcomes', 'sportspress' ) ] = sp_get_equation_optgroup_array( $postid, 'sp_outcome', array( '' => '', 'max' => '&uarr;', 'min' => '&darr;' ), array( 'played' => __( 'Played', 'sportspress' ) ) );
// Add stats to options
$options[ __( 'Statistics', 'sportspress' ) ] = (array) $vars;
$options[ __( 'Statistics', 'sportspress' ) ] = sp_get_equation_optgroup_array( $postid, 'sp_stat' );
elseif ( $type == 'metric' ):
$vars = array();
// Get metrics within the sports that the current metric is in ### TODO: should be for sport selected
// Get metrics within the sports that the current metric is in
$args = array(
'post_type' => 'sp_metric',
'numberposts' => -1,
'posts_per_page' => -1,
'exclude' => $post->ID
'exclude' => $postid
);
$sports = get_the_terms( $post->ID, 'sp_sport' );
$sports = get_the_terms( $postid, 'sp_sport' );
if ( ! empty( $sports ) ):
$terms = array();
foreach ( $sports as $sport ):
@@ -362,7 +384,7 @@ if ( !function_exists( 'sp_get_equation_selector' ) ) {
?>
<select name="sp_equation[]" data-remove-text="<?php _e( 'Remove', 'sportspress' ); ?>">
<option value=""><?php _e( 'Select', 'sportspress' ); ?></option>
<option value="">(<?php _e( 'Select', 'sportspress' ); ?>)</option>
<?php
foreach ( $options as $label => $option ):

View File

@@ -35,12 +35,14 @@ include dirname( __FILE__ ) . '/sportspress-settings.php' ;
// Custom Post Types
require_once dirname( __FILE__ ) . '/admin/post-types/event.php';
require_once dirname( __FILE__ ) . '/admin/post-types/result.php';
require_once dirname( __FILE__ ) . '/admin/post-types/outcome.php';
require_once dirname( __FILE__ ) . '/admin/post-types/team.php';
require_once dirname( __FILE__ ) . '/admin/post-types/stat.php';
require_once dirname( __FILE__ ) . '/admin/post-types/table.php';
require_once dirname( __FILE__ ) . '/admin/post-types/stat.php';
require_once dirname( __FILE__ ) . '/admin/post-types/player.php';
require_once dirname( __FILE__ ) . '/admin/post-types/metric.php';
require_once dirname( __FILE__ ) . '/admin/post-types/list.php';
require_once dirname( __FILE__ ) . '/admin/post-types/metric.php';
require_once dirname( __FILE__ ) . '/admin/post-types/staff.php';
// Terms