Add drag-and-drop template layout options

This commit is contained in:
Brian Miyaji
2016-04-02 18:39:17 +11:00
parent bd565b16bb
commit e611b00a92
10 changed files with 568 additions and 129 deletions

View File

@@ -23,11 +23,15 @@ class SP_Settings_Events extends SP_Settings_Page {
public function __construct() {
$this->id = 'events';
$this->label = __( 'Events', 'sportspress' );
$this->template = 'event';
$this->templates = SP()->templates->event;
add_filter( 'sportspress_settings_tabs_array', array( $this, 'add_settings_page' ), 20 );
add_action( 'sportspress_settings_' . $this->id, array( $this, 'output' ) );
add_action( 'sportspress_admin_field_current_mode', array( $this, 'current_mode_setting' ) );
add_action( 'sportspress_admin_field_delimiter', array( $this, 'delimiter_setting' ) );
add_action( 'sportspress_admin_field_event_layout', array( $this, 'layout_setting' ) );
add_action( 'sportspress_settings_save_' . $this->id, array( $this, 'save' ) );
}
@@ -55,20 +59,15 @@ class SP_Settings_Events extends SP_Settings_Page {
),
apply_filters( 'sportspress_event_template_options', array(
array( 'type' => 'event_layout' ),
array(
'title' => __( 'Display', 'sportspress' ),
'desc' => __( 'Logos', 'sportspress' ),
'id' => 'sportspress_event_show_logos',
'default' => 'yes',
'type' => 'checkbox',
'checkboxgroup' => 'start',
),
array(
'desc' => __( 'Date', 'sportspress' ),
'id' => 'sportspress_event_show_date',
'default' => 'yes',
'type' => 'checkbox',
'checkboxgroup' => '',
'checkboxgroup' => 'start',
),
array(
'desc' => __( 'Time', 'sportspress' ),
@@ -77,34 +76,6 @@ class SP_Settings_Events extends SP_Settings_Page {
'type' => 'checkbox',
'checkboxgroup' => '',
),
array(
'desc' => __( 'Results', 'sportspress' ),
'id' => 'sportspress_event_show_results',
'default' => 'yes',
'type' => 'checkbox',
'checkboxgroup' => '',
),
array(
'desc' => __( 'Details', 'sportspress' ),
'id' => 'sportspress_event_show_details',
'default' => 'yes',
'type' => 'checkbox',
'checkboxgroup' => '',
),
array(
'desc' => __( 'Venue', 'sportspress' ),
'id' => 'sportspress_event_show_venue',
'default' => 'yes',
'type' => 'checkbox',
'checkboxgroup' => '',
),
array(
'desc' => __( 'Scorecard', 'sportspress' ),
'id' => 'sportspress_event_show_performance',
'default' => 'yes',
'type' => 'checkbox',
'checkboxgroup' => 'end',
),
) ),
array(
@@ -421,8 +392,7 @@ class SP_Settings_Events extends SP_Settings_Page {
* Save settings
*/
public function save() {
$settings = $this->get_settings();
SP_Admin_Settings::save_fields( $settings );
parent::save();
if ( isset( $_POST['sportspress_event_teams_delimiter'] ) )
update_option( 'sportspress_event_teams_delimiter', $_POST['sportspress_event_teams_delimiter'] );

View File

@@ -19,7 +19,8 @@ class SP_Settings_Page {
protected $id = '';
protected $label = '';
protected $template = '';
public $templates = array();
/**
* Add this page to settings
*/
@@ -56,8 +57,69 @@ class SP_Settings_Page {
$settings = $this->get_settings();
SP_Admin_Settings::save_fields( $settings );
if ( $current_section )
do_action( 'sportspress_update_options_' . $this->id . '_' . $current_section );
if ( $current_section )
do_action( 'sportspress_update_options_' . $this->template . '_' . $current_section );
if ( ! empty( $this->templates ) )
update_option( 'sportspress_' . $this->template . '_template_order', sp_array_value( $_POST, 'sportspress_' . $this->template . '_template_order', false ) );
if ( isset( $_POST['sportspress_template_visibility'] ) && is_array( $_POST['sportspress_template_visibility'] ) ) {
foreach ( $_POST['sportspress_template_visibility'] as $option => $toggled ) {
if ( $toggled ) {
update_option( $option, 'yes' );
} else {
update_option( $option, 'no' );
}
}
}
}
/**
* Layout settings
*
* @access public
* @return void
*/
public function layout_setting() {
$templates = apply_filters( 'sportspress_' . $this->template . '_templates', $this->templates );
$layout = get_option( 'sportspress_' . $this->template . '_template_order' );
if ( false === $layout ) {
$layout = array_keys( $templates );
}
$templates = array_merge( array_flip( $layout ), $templates );
?>
<tr valign="top">
<th>
<?php _e( 'Layout', 'sportspress' ); ?>
</th>
<td class="sp-sortable-list-container">
<p class="description"><?php _e( 'Drag each item into the order you prefer.', 'sportspress' ); ?></p>
<ul class="sp-layout sp-sortable-list ui-sortable">
<?php foreach ( $templates as $template => $details ) {
$option = sp_array_value( $details, 'option', 'sportspress_' . $this->template . '_show_' . $template );
$visibility = get_option( $option, sp_array_value( $details, 'default', 'yes' ) );
?>
<li>
<div class="sp-item-bar sp-layout-item-bar">
<div class="sp-item-handle sp-layout-item-handle ui-sortable-handle">
<span class="sp-item-title item-title"><?php echo sp_array_value( $details, 'title', ucfirst( $template ) ); ?></span>
<input type="hidden" name="sportspress_<?php echo $this->template; ?>_template_order[]" value="<?php echo $template; ?>">
</div>
<input type="hidden" name="sportspress_template_visibility[<?php echo $option; ?>]" value="0">
<input class="sp-toggle-switch" type="checkbox" name="sportspress_template_visibility[<?php echo $option; ?>]" id="<?php echo $option; ?>" value="1" <?php checked( $visibility, 'yes' ); ?>>
<label for="sportspress_<?php echo $this->template; ?>_show_<?php echo $template; ?>"></label>
</div>
</li>
<?php } ?>
</ul>
</td>
</tr>
<?php
}
}

View File

@@ -23,9 +23,13 @@ class SP_Settings_Players extends SP_Settings_Page {
public function __construct() {
$this->id = 'players';
$this->label = __( 'Players', 'sportspress' );
$this->template = 'player';
$this->templates = SP()->templates->player;
add_filter( 'sportspress_settings_tabs_array', array( $this, 'add_settings_page' ), 20 );
add_action( 'sportspress_settings_' . $this->id, array( $this, 'output' ) );
add_action( 'sportspress_admin_field_player_layout', array( $this, 'layout_setting' ) );
add_action( 'sportspress_settings_save_' . $this->id, array( $this, 'save' ) );
}
@@ -37,12 +41,13 @@ class SP_Settings_Players extends SP_Settings_Page {
public function get_settings() {
$settings = array_merge(
array(
array( 'title' => __( 'Player Options', 'sportspress' ), 'type' => 'title','desc' => '', 'id' => 'player_options' ),
),
apply_filters( 'sportspress_player_options', array(
array( 'type' => 'player_layout' ),
array(
'title' => __( 'Link', 'sportspress' ),
'desc' => __( 'Link players', 'sportspress' ),
@@ -51,39 +56,6 @@ class SP_Settings_Players extends SP_Settings_Page {
'type' => 'checkbox',
),
array(
'title' => __( 'Display', 'sportspress' ),
'desc' => __( 'Photo', 'sportspress' ),
'id' => 'sportspress_player_show_photo',
'default' => 'yes',
'type' => 'checkbox',
'checkboxgroup' => 'start',
),
array(
'desc' => __( 'Details', 'sportspress' ),
'id' => 'sportspress_player_show_details',
'default' => 'yes',
'type' => 'checkbox',
'checkboxgroup' => '',
),
array(
'desc' => __( 'Statistics', 'sportspress' ),
'id' => 'sportspress_player_show_statistics',
'default' => 'yes',
'type' => 'checkbox',
'checkboxgroup' => '',
),
array(
'desc' => __( 'Total', 'sportspress' ),
'id' => 'sportspress_player_show_total',
'default' => 'no',
'type' => 'checkbox',
'checkboxgroup' => 'end',
),
array(
'title' => __( 'Details', 'sportspress' ),
'desc' => __( 'Nationality', 'sportspress' ),
@@ -156,6 +128,14 @@ class SP_Settings_Players extends SP_Settings_Page {
),
),
array(
'title' => __( 'Total', 'sportspress' ),
'desc' => __( 'Display total', 'sportspress' ),
'id' => 'sportspress_player_show_total',
'default' => 'no',
'type' => 'checkbox',
),
array(
'title' => __( 'Nationality', 'sportspress' ),
'desc' => __( 'Display national flags', 'sportspress' ),

View File

@@ -23,9 +23,13 @@ class SP_Settings_Teams extends SP_Settings_Page {
public function __construct() {
$this->id = 'teams';
$this->label = __( 'Teams', 'sportspress' );
$this->template = 'team';
$this->templates = SP()->templates->team;
add_filter( 'sportspress_settings_tabs_array', array( $this, 'add_settings_page' ), 20 );
add_action( 'sportspress_settings_' . $this->id, array( $this, 'output' ) );
add_action( 'sportspress_admin_field_team_layout', array( $this, 'layout_setting' ) );
add_action( 'sportspress_settings_save_' . $this->id, array( $this, 'save' ) );
}
@@ -43,6 +47,8 @@ class SP_Settings_Teams extends SP_Settings_Page {
),
apply_filters( 'sportspress_team_options', array(
array( 'type' => 'team_layout' ),
array(
'title' => __( 'Link', 'sportspress' ),
'desc' => __( 'Link teams', 'sportspress' ),
@@ -51,31 +57,6 @@ class SP_Settings_Teams extends SP_Settings_Page {
'type' => 'checkbox',
),
array(
'title' => __( 'Display', 'sportspress' ),
'desc' => __( 'Logo', 'sportspress' ),
'id' => 'sportspress_team_show_logo',
'default' => 'yes',
'type' => 'checkbox',
'checkboxgroup' => 'start',
),
array(
'desc' => __( 'Details', 'sportspress' ),
'id' => 'sportspress_team_show_details',
'default' => 'no',
'type' => 'checkbox',
'checkboxgroup' => '',
),
array(
'desc' => __( 'Visit Site', 'sportspress' ),
'id' => 'sportspress_team_show_link',
'default' => 'yes',
'type' => 'checkbox',
'checkboxgroup' => 'end',
),
array(
'title' => __( 'Venue', 'sportspress' ),
'desc' => __( 'Link venues', 'sportspress' ),

View File

@@ -24,10 +24,17 @@ class SP_Template_Loader {
add_filter( 'the_content', array( $this, 'staff_content' ) );
}
public function add_content( $content, $template, $position = 10, $caption = null ) {
public function add_content( $content, $type, $position = 10, $caption = null ) {
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! in_the_loop() ) return; // Return if not in main loop
// Return password form if required
if ( post_password_required() ) {
echo get_the_password_form();
return;
}
// Prepend caption to content if given
if ( $content ) {
if ( $caption ) {
$content = '<h3 class="sp-post-caption">' . $caption . '</h3>' . $content;
@@ -36,30 +43,51 @@ class SP_Template_Loader {
$content = '<div class="sp-post-content">' . $content . '</div>';
}
ob_start();
if ( post_password_required() ) {
echo get_the_password_form();
return;
global $wp_filter;
// Array of hooks associated with this post type
$hooks = array(
'sportspress_before_single_' . $type,
'sportspress_single_' . $type . '_content',
'sportspress_after_single_' . $type,
);
$actions = array();
// Find all actions associated with those hooks
foreach ( $hooks as $hook ) {
$priorities = sp_array_value( $wp_filter, $hook, array() );
foreach ( $priorities as $priority => $action ) {
$a = reset( $action );
$function = sp_array_value( $a, 'function', false );
remove_action( $hook, $function, $priority );
$actions[] = $function;
}
}
// Get layout setting
$layout = (array) get_option( 'sportspress_' . $type . '_template_order', array() );
// Combine layout setting with available templates
$templates = array_merge( array_flip( $layout ), SP()->templates->$type );
if ( $position <= 0 )
echo $content;
do_action( 'sportspress_before_single_' . $template );
if ( $position > 0 && $position <= 10 )
echo $content;
do_action( 'sportspress_single_' . $template . '_content' );
if ( $position > 10 && $position <= 20 )
echo $content;
do_action( 'sportspress_after_single_' . $template );
if ( $position > 20 )
echo $content;
ob_start();
// Loop through templates
foreach ( $templates as $key => $template ) {
// Ignore templates that are unavailable or that have been turned off
if ( ! is_array( $template ) ) continue;
if ( ! isset( $template['option'] ) ) continue;
if ( 'yes' !== get_option( $template['option'], sp_array_value( $template, 'default', 'yes' ) ) ) continue;
// Render the template
if ( 'content' === $key ) {
echo $content;
} else {
call_user_func( $template['action'] );
}
}
return ob_get_clean();
}

View File

@@ -0,0 +1,288 @@
<?php
/**
* SportsPress templates
*
* The SportsPress templates class stores template layout data.
*
* @class SP_Templates
* @version 2.0
* @package SportsPress/Classes
* @category Class
* @author ThemeBoy
*/
class SP_Templates {
/** @var array Array of templates */
private $data = array();
/**
* Constructor for the templates class - defines all templates.
*
* @access public
* @return void
*/
public function __construct() {
$this->data = array(
'event' => array_merge(
apply_filters( 'sportspress_before_event_template', array(
'logos' => array(
'title' => __( 'Logos', 'sportspress' ),
'option' => 'sportspress_event_show_logos',
'action' => 'sportspress_output_event_logos',
'default' => 'yes',
),
'excerpt' => array(
'title' => __( 'Excerpt', 'sportspress' ),
'option' => 'sportspress_event_show_excerpt',
'action' => 'sportspress_output_post_excerpt',
'default' => 'yes',
),
) ),
array(
'content' => array(
'title' => __( 'Article', 'sportspress' ),
'option' => 'sportspress_event_show_content',
'action' => 'sportspress_output_event_content',
'default' => 'yes',
),
),
apply_filters( 'sportspress_after_event_template', array(
'video' => array(
'title' => __( 'Video', 'sportspress' ),
'option' => 'sportspress_event_show_video',
'action' => 'sportspress_output_event_video',
'default' => 'yes',
),
'details' => array(
'title' => __( 'Details', 'sportspress' ),
'option' => 'sportspress_event_show_details',
'action' => 'sportspress_output_event_details',
'default' => 'yes',
),
'venue' => array(
'title' => __( 'Venue', 'sportspress' ),
'option' => 'sportspress_event_show_venue',
'action' => 'sportspress_output_event_venue',
'default' => 'yes',
),
'results' => array(
'title' => __( 'Results', 'sportspress' ),
'option' => 'sportspress_event_show_results',
'action' => 'sportspress_output_event_results',
'default' => 'yes',
),
'performance' => array(
'title' => __( 'Scorecard', 'sportspress' ),
'option' => 'sportspress_event_show_performance',
'action' => 'sportspress_output_event_performance',
'default' => 'yes',
),
) )
),
'calendar' => array_merge(
apply_filters( 'sportspress_before_calendar_template', array() ),
array(
'content' => array(
'title' => __( 'Description', 'sportspress' ),
'option' => 'sportspress_calendar_show_content',
'action' => 'sportspress_output_calendar_content',
'default' => 'yes',
),
),
apply_filters( 'sportspress_after_calendar_template', array(
'data' => array(
'title' => __( 'Calendar', 'sportspress' ),
'option' => 'sportspress_calendar_show_data',
'action' => 'sportspress_output_calendar',
'default' => 'yes',
),
) )
),
'team' => array_merge(
apply_filters( 'sportspress_before_team_template', array(
'logo' => array(
'title' => __( 'Logo', 'sportspress' ),
'option' => 'sportspress_team_show_logo',
'action' => 'sportspress_output_team_logo',
'default' => 'yes',
),
'excerpt' => array(
'title' => __( 'Excerpt', 'sportspress' ),
'option' => 'sportspress_team_show_excerpt',
'action' => 'sportspress_output_post_excerpt',
'default' => 'yes',
),
) ),
array(
'content' => array(
'title' => __( 'Profile', 'sportspress' ),
'option' => 'sportspress_team_show_content',
'action' => 'sportspress_output_team_content',
'default' => 'yes',
),
),
apply_filters( 'sportspress_after_team_template', array(
'link' => array(
'title' => __( 'Visit Site', 'sportspress' ),
'option' => 'sportspress_team_show_link',
'action' => 'sportspress_output_team_link',
'default' => 'no',
),
'details' => array(
'title' => __( 'Details', 'sportspress' ),
'option' => 'sportspress_team_show_details',
'action' => 'sportspress_output_team_details',
'default' => 'no',
),
'staff' => array(
'title' => __( 'Staff', 'sportspress' ),
'option' => 'sportspress_team_show_staff',
'action' => 'sportspress_output_team_staff',
'default' => 'yes',
),
'lists' => array(
'title' => __( 'Player Lists', 'sportspress' ),
'option' => 'sportspress_team_show_lists',
'action' => 'sportspress_output_team_lists',
'default' => 'yes',
),
'tables' => array(
'title' => __( 'League Tables', 'sportspress' ),
'option' => 'sportspress_team_show_tables',
'action' => 'sportspress_output_team_tables',
'default' => 'yes',
),
) )
),
'table' => array_merge(
apply_filters( 'sportspress_before_table_template', array() ),
array(
'content' => array(
'title' => __( 'Description', 'sportspress' ),
'option' => 'sportspress_table_show_content',
'action' => 'sportspress_output_table_content',
'default' => 'yes',
),
),
apply_filters( 'sportspress_after_table_template', array(
'data' => array(
'title' => __( 'League Table', 'sportspress' ),
'option' => 'sportspress_table_show_data',
'action' => 'sportspress_output_league_table',
'default' => 'yes',
),
) )
),
'player' => array_merge(
apply_filters( 'sportspress_before_player_template', array(
'photo' => array(
'title' => __( 'Photo', 'sportspress' ),
'option' => 'sportspress_player_show_photo',
'action' => 'sportspress_output_player_photo',
'default' => 'yes',
),
'details' => array(
'title' => __( 'Details', 'sportspress' ),
'option' => 'sportspress_player_show_details',
'action' => 'sportspress_output_player_details',
'default' => 'yes',
),
'excerpt' => array(
'title' => __( 'Excerpt', 'sportspress' ),
'option' => 'sportspress_player_show_excerpt',
'action' => 'sportspress_output_post_excerpt',
'default' => 'yes',
),
) ),
array(
'content' => array(
'title' => __( 'Profile', 'sportspress' ),
'option' => 'sportspress_player_show_content',
'action' => 'sportspress_output_player_content',
'default' => 'yes',
),
),
apply_filters( 'sportspress_after_player_template', array(
'statistics' => array(
'title' => __( 'Statistics', 'sportspress' ),
'option' => 'sportspress_player_show_statistics',
'action' => 'sportspress_output_player_statistics',
'default' => 'yes',
),
) )
),
'list' => array_merge(
apply_filters( 'sportspress_before_list_template', array() ),
array(
'content' => array(
'title' => __( 'Description', 'sportspress' ),
'option' => 'sportspress_list_show_content',
'action' => 'sportspress_output_list_content',
'default' => 'yes',
),
),
apply_filters( 'sportspress_after_list_template', array(
'data' => array(
'title' => __( 'Player List', 'sportspress' ),
'option' => 'sportspress_list_show_data',
'action' => 'sportspress_output_player_list',
'default' => 'yes',
),
) )
),
'staff' => array_merge(
apply_filters( 'sportspress_before_staff_template', array(
'photo' => array(
'title' => __( 'Photo', 'sportspress' ),
'option' => 'sportspress_staff_show_photo',
'action' => 'sportspress_output_staff_photo',
'default' => 'yes',
),
'details' => array(
'title' => __( 'Details', 'sportspress' ),
'option' => 'sportspress_staff_show_details',
'action' => 'sportspress_output_staff_details',
'default' => 'yes',
),
'excerpt' => array(
'title' => __( 'Excerpt', 'sportspress' ),
'option' => 'sportspress_staff_show_excerpt',
'action' => 'sportspress_output_post_excerpt',
'default' => 'yes',
),
) ),
array(
'content' => array(
'title' => __( 'Profile', 'sportspress' ),
'option' => 'sportspress_staff_show_content',
'action' => 'sportspress_output_staff_content',
'default' => 'yes',
),
),
apply_filters( 'sportspress_after_staff_template', array() )
),
);
}
public function __get( $key ) {
return ( array_key_exists( $key, $this->data ) ? $this->data[ $key ] : array() );
}
public function __set( $key, $value ){
$this->data[ $key ] = $value;
}
}

View File

@@ -82,12 +82,6 @@ add_action( 'sportspress_single_team_content', 'sportspress_output_team_staff',
add_action( 'sportspress_single_team_content', 'sportspress_output_team_lists', 20 );
add_action( 'sportspress_single_team_content', 'sportspress_output_team_tables', 30 );
/**
* Before Single Table
* @see sportspress_output_post_excerpt()
*/
add_action( 'sportspress_before_single_table', 'sportspress_output_post_excerpt', 20 );
/**
* Single Table Content
*