Add option to insert sample data during install

This commit is contained in:
Brian Miyaji
2014-10-19 01:06:30 +11:00
parent da2a778343
commit 779c84054f
6 changed files with 87 additions and 278 deletions

View File

@@ -1,251 +1,16 @@
<?php
/**
* SportsPress Admin Sports Class.
* SportsPress Admin Sample Data Class.
*
* The SportsPress admin sports class stores preset sport data.
* The SportsPress admin sample data class stores demo content.
*
* @class SP_Admin_Sports
* @class SP_Admin_Sample_Data
* @version 1.4
* @package SportsPress/Admin
* @category Class
* @author ThemeBoy
*/
class SP_Admin_Sports {
public static $presets = array();
public static $options = array();
/**
* Include the preset classes
*/
public static function get_presets() {
if ( empty( self::$presets ) ) {
$presets = array();
self::$options = array(
__( 'Traditional Sports', 'sportspress' ) => array(),
__( 'Esports', 'sportspress' ) => array(),
__( 'Other', 'sportspress' ) => array( 'custom' => __( 'Custom', 'sportspress' ) ),
);
$dir = scandir( SP()->plugin_path() . '/presets' );
$files = array();
if ( $dir ) {
foreach ( $dir as $key => $value ) {
if ( substr( $value, 0, 1 ) !== '.' && strpos( $value, '.' ) !== false ) {
$files[] = $value;
}
}
}
foreach( $files as $file ) {
$json_data = file_get_contents( SP()->plugin_path() . '/presets/' . $file );
$data = json_decode( $json_data, true );
if ( ! is_array( $data ) ) continue;
$id = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file );
$presets[ $id ] = $data;
$name = array_key_exists( 'name', $data ) ? __( $data['name'], 'sportspress' ) : $id;
self::$options[ __( 'Traditional Sports', 'sportspress' ) ][ $id ] = $name;
}
asort( self::$options[ __( 'Traditional Sports', 'sportspress' ) ] );
$dir = scandir( SP()->plugin_path() . '/presets/esports' );
$files = array();
if ( $dir ) {
foreach ( $dir as $key => $value ) {
if ( substr( $value, 0, 1 ) !== '.' && strpos( $value, '.' ) !== false ) {
$files[] = $value;
}
}
}
foreach( $files as $file ) {
$json_data = file_get_contents( SP()->plugin_path() . '/presets/esports/' . $file );
$data = json_decode( $json_data, true );
if ( ! is_array( $data ) ) continue;
$id = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file );
$presets[ $id ] = $data;
$name = array_key_exists( 'name', $data ) ? __( $data['name'], 'sportspress' ) : $id;
self::$options[ __( 'Esports', 'sportspress' ) ][ $id ] = $name;
}
asort( self::$options[ __( 'Esports', 'sportspress' ) ] );
self::$presets = apply_filters( 'sportspress_get_presets', $presets );
}
return self::$presets;
}
public static function get_preset( $id ) {
$json_data = @file_get_contents( SP()->plugin_path() . '/presets/' . $id . '.json', true );
if ( $json_data ) return json_decode( $json_data, true );
$dir = scandir( SP()->plugin_path() . '/presets' );
if ( $dir ) {
foreach ( $dir as $key => $value ) {
if ( substr( $value, 0, 1 ) !== '.' && strpos( $value, '.' ) === false ) {
$json_data = @file_get_contents( SP()->plugin_path() . '/presets/' . $value . '/' . $id . '.json', true );
if ( $json_data ) return json_decode( $json_data, true );
}
}
}
}
public static function get_preset_options() {
$presets = self::get_presets();
return self::$options;
}
/**
* Apply preset
*
* @access public
* @return void
*/
public static function apply_preset( $id ) {
if ( 'custom' == $id ) {
$preset = array();
} else {
$preset = self::get_preset( $id );
}
// Positions
$positions = sp_array_value( $preset, 'positions', array() );
foreach ( $positions as $index => $term ) {
$slug = $index . '-' . sanitize_title( $term );
wp_insert_term( $term, 'sp_position', array( 'description' => $term, 'slug' => $slug ) );
}
// Outcomes
$post_type = 'sp_outcome';
$outcomes = sp_array_value( $preset, 'outcomes', array() );
self::delete_preset_posts( $post_type );
foreach ( $outcomes as $index => $outcome ) {
$post = self::get_post_array( $outcome, $post_type );
if ( empty( $post ) ) continue;
$id = self::insert_preset_post( $post, $index );
update_post_meta( $id, 'sp_abbreviation', sp_array_value( $outcome, 'abbreviation', null ) );
}
// Results
$post_type = 'sp_result';
$results = sp_array_value( $preset, 'results', array() );
self::delete_preset_posts( $post_type );
$primary_result = 0;
foreach ( $results as $index => $result ) {
$post = self::get_post_array( $result, $post_type );
if ( empty( $post ) ) continue;
$id = self::insert_preset_post( $post, $index );
if ( is_array( $result ) && array_key_exists( 'primary', $result ) ) $primary_result = $post['post_name'];
}
// Make sure statistics and metrics have greater menu order than performance
$i = 0;
// Performance
$post_type = 'sp_performance';
$performances = sp_array_value( $preset, 'performance', array() );
self::delete_preset_posts( $post_type );
foreach ( $performances as $index => $performance ) {
$post = self::get_post_array( $performance, $post_type );
if ( empty( $post ) ) continue;
$id = self::insert_preset_post( $post, $index );
$i ++;
}
// Columns
$post_type = 'sp_column';
$columns = sp_array_value( $preset, 'columns', array() );
self::delete_preset_posts( $post_type );
foreach ( $columns as $index => $column ) {
$post = self::get_post_array( $column, $post_type );
if ( empty( $post ) ) continue;
$id = self::insert_preset_post( $post, $index );
update_post_meta( $id, 'sp_equation', sp_array_value( $column, 'equation', null ) );
update_post_meta( $id, 'sp_precision', sp_array_value( $column, 'precision', 0 ) );
update_post_meta( $id, 'sp_priority', sp_array_value( $column, 'priority', null ) );
update_post_meta( $id, 'sp_order', sp_array_value( $column, 'order', 'DESC' ) );
}
// Metrics
$post_type = 'sp_metric';
$metrics = sp_array_value( $preset, 'metrics', array() );
self::delete_preset_posts( $post_type );
foreach ( $metrics as $index => $metric ) {
$post = self::get_post_array( $metric, $post_type );
if ( empty( $post ) ) continue;
$id = self::insert_preset_post( $post, $i + $index );
$i ++;
}
// Statistics
$post_type = 'sp_statistic';
$statistics = sp_array_value( $preset, 'statistics', array() );
self::delete_preset_posts( $post_type );
foreach ( $statistics as $index => $statistic ) {
$post = self::get_post_array( $statistic, $post_type );
if ( empty( $post ) ) continue;
$id = self::insert_preset_post( $post, $i + $index );
update_post_meta( $id, 'sp_equation', sp_array_value( $statistic, 'equation', null ) );
update_post_meta( $id, 'sp_precision', sp_array_value( $statistic, 'precision', 0 ) );
}
update_option( 'sportspress_primary_result', $primary_result );
self::delete_sample_data();
self::add_sample_data();
}
public static function delete_preset_posts( $post_type = null ) {
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1,
'post_status' => array( 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash' ),
'meta_query' => array(
array(
'key' => '_sp_preset',
'value' => 1
)
)
);
// Delete posts
$old_posts = get_posts( $args );
foreach( $old_posts as $post ):
wp_delete_post( $post->ID, true );
endforeach;
}
public static function get_post_array( $post = array(), $post_type = null ) {
$post_array = array();
if ( is_string( $post ) ) {
$post_array['post_title'] = $post;
$post_array['post_name'] = sp_get_eos_safe_slug( $post_array['post_title'] );
} elseif ( is_array( $post ) ) {
if ( ! array_key_exists( 'name', $post ) ) $post_array = array();
$post_array['post_title'] = $post['name'];
$post_array['post_name'] = sp_array_value( $post, 'id', sp_get_eos_safe_slug( $post_array['post_title'] ) );
}
// Return empty array if post with same slug already exists
if ( get_page_by_path( $post_array['post_name'], OBJECT, $post_type ) ) return array();
// Set post type
$post_array['post_type'] = $post_type;
// Add post excerpt
$post_array['post_excerpt'] = sp_array_value( $post, 'description', $post_array['post_title'] );
return $post_array;
}
public static function insert_preset_post( $post, $index = 0 ) {
// Increment menu order by 10 and publish post
$post['menu_order'] = $index * 10 + 10;
$post['post_status'] = 'publish';
$id = wp_insert_post( $post );
// Flag as preset
update_post_meta( $id, '_sp_preset', 1 );
return $id;
}
class SP_Admin_Sample_Data {
/**
* Sample data
@@ -254,7 +19,7 @@ class SP_Admin_Sports {
*
* @access public
*/
public static function add_sample_data() {
public static function insert_posts() {
// Initialize inserted ids array
$inserted_ids = array(
@@ -414,6 +179,13 @@ class SP_Admin_Sports {
'Bobby Brown',
);
// Define event videos
$event_videos = array(
'https://www.youtube.com/watch?v=t_aQRQGoXRk',
'https://www.youtube.com/watch?v=sIrjQyuwteM',
'https://www.youtube.com/watch?v=PJuqJefZwuA',
);
// Get countries
$countries = new SP_Countries();
@@ -687,6 +459,7 @@ class SP_Admin_Sports {
sp_update_post_meta_recursive( $id, 'sp_player', $event_players );
update_post_meta( $id, 'sp_columns', $columns );
update_post_meta( $id, 'sp_format', 'league' );
update_post_meta( $id, 'sp_video', $event_videos[ $i ] );
}
/*
@@ -843,7 +616,7 @@ class SP_Admin_Sports {
*
* @access public
*/
public static function delete_sample_data() {
public static function delete_posts() {
$post_types = sp_post_types();
$args = array(
'post_type' => $post_types,
@@ -863,31 +636,4 @@ class SP_Admin_Sports {
wp_delete_post( $post->ID, true );
endforeach;
}
/**
* Sport preset names for localization
* @return null
*/
public static function sport_preset_names() {
__( 'Baseball', 'sportspress' );
__( 'Basketball', 'sportspress' );
__( 'Cricket', 'sportspress' );
__( 'Darts', 'sportspress' );
__( 'American Football', 'sportspress' );
__( 'Australian Rules Football', 'sportspress' );
__( 'Handball', 'sportspress' );
__( 'Ice Hockey', 'sportspress' );
__( 'Netball', 'sportspress' );
__( 'Rugby League', 'sportspress' );
__( 'Rugby Union', 'sportspress' );
__( 'Snooker', 'sportspress' );
__( 'Soccer (Association Football)', 'sportspress' );
__( 'Squash', 'sportspress' );
__( 'Table Tennis', 'sportspress' );
__( 'Tennis', 'sportspress' );
__( 'Volleyball', 'sportspress' );
__( 'Water Polo', 'sportspress' );
__( 'Dota 2', 'sportspress' );
__( 'League of Legends', 'sportspress' );
}
}

View File

@@ -411,6 +411,58 @@ class SP_Admin_Settings {
</tr><?php
break;
// Select sport
case 'sport' :
$option_value = self::get_option( $value['id'], $value['default'] );
?><tr valign="top">
<th scope="row" class="titledesc">
<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
<?php echo $tip; ?>
</th>
<td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
<select
name="<?php echo esc_attr( $value['id'] ); ?><?php if ( $value['type'] == 'multiselect' ) echo '[]'; ?>"
id="<?php echo esc_attr( $value['id'] ); ?>"
style="<?php echo esc_attr( $value['css'] ); ?>"
class="chosen-select<?php if ( is_rtl() ): ?> chosen-rtl<?php endif; ?> <?php echo esc_attr( $value['class'] ); ?>"
<?php echo implode( ' ', $custom_attributes ); ?>
<?php if ( $value['type'] == 'multiselect' ) echo 'multiple="multiple"'; ?>
>
<?php
foreach ( $value['options'] as $group => $options ) {
?>
<optgroup label="<?php _e( $group, 'sportspress' ); ?>">
<?php
foreach ( $options as $key => $val ) {
?>
<option value="<?php echo esc_attr( $key ); ?>" <?php
if ( is_array( $option_value ) )
selected( in_array( $key, $option_value ), true );
else
selected( $option_value, $key );
?>><?php echo $val ?></option>
<?php
}
?>
</optgroup>
<?php
}
?>
</select> <?php echo $description; ?>
<p>
<label>
<input type="checkbox" name="add_sample_data" id="add_sample_data" <?php checked( sp_array_value( $value, 'welcome' ) ); ?>>
<?php _e( 'Install demo content', 'sportspress' ); ?>
</label>
</p>
</td>
</tr><?php
break;
// Radio inputs
case 'radio' :
@@ -578,6 +630,7 @@ class SP_Admin_Settings {
case 'number':
case 'select' :
case 'groupselect' :
case 'sport' :
case 'color' :
case 'password' :
case 'radio' :

View File

@@ -240,7 +240,12 @@ class SP_Admin_Welcome {
<div id="message" class="updated sportspress-message">
<p><strong><?php _e( 'Your settings have been saved.', 'sportspress' ); ?></strong></p>
</div>
<?php endif; ?>
<?php
endif;
if ( isset( $_POST['add_sample_data'] ) ):
SP_Admin_Sample_Data::insert_posts();
endif;
?>
<div class="sp-feature feature-section col two-col">
<div>
<?php if ( get_option( 'sportspress_basic_setup' ) ) { ?>
@@ -262,16 +267,16 @@ class SP_Admin_Welcome {
<h4><?php _e( 'Next Steps', 'sportspress' ); ?></h4>
<p><?php _e( 'We&#8217;ve assembled some links to get you started:', 'sportspress' ); ?></p>
<div class="sportspress-steps">
<ul>
<li><a href="<?php echo esc_url( admin_url( add_query_arg( array( 'page' => 'sp-overview' ), 'admin.php' ) ) ); ?>" class="welcome-icon dashicons-networking"><?php _e( 'SportsPress Overview', 'sportspress' ); ?></a></li>
<li><a href="<?php echo esc_url( admin_url( add_query_arg( array( 'page' => 'sp-config' ), 'admin.php' ) ) ); ?>" class="welcome-icon dashicons-performance"><?php _e( 'Configure SportsPress', 'sportspress' ); ?></a></li>
<li><a href="<?php echo esc_url( admin_url( 'widgets.php' ) ); ?>" class="welcome-icon dashicons-welcome-widgets-menus"><?php _e( 'Widgets', 'sportspress' ); ?></a></li>
</ul>
<ul>
<li><a href="<?php echo esc_url( admin_url( add_query_arg( array( 'post_type' => 'sp_team' ), 'post-new.php' ) ) ); ?>" class="welcome-icon sp-welcome-icon dashicons-shield-alt"><?php _e( 'Add New Team', 'sportspress' ); ?></a></li>
<li><a href="<?php echo esc_url( admin_url( add_query_arg( array( 'post_type' => 'sp_player' ), 'post-new.php' ) ) ); ?>" class="welcome-icon sp-welcome-icon dashicons-groups"><?php _e( 'Add New Player', 'sportspress' ); ?></a></li>
<li><a href="<?php echo esc_url( admin_url( add_query_arg( array( 'post_type' => 'sp_event' ), 'post-new.php' ) ) ); ?>" class="welcome-icon sp-welcome-icon dashicons-calendar"><?php _e( 'Add New Event', 'sportspress' ); ?></a></li>
</ul>
<ul>
<li><a href="<?php echo esc_url( admin_url( add_query_arg( array( 'page' => 'sp-overview' ), 'admin.php' ) ) ); ?>" class="welcome-icon dashicons-networking"><?php _e( 'Overview', 'sportspress' ); ?></a></li>
<li><a href="<?php echo esc_url( admin_url( add_query_arg( array( 'page' => 'sp-config' ), 'admin.php' ) ) ); ?>" class="welcome-icon dashicons-performance"><?php _e( 'Configure', 'sportspress' ); ?></a></li>
<li><a href="<?php echo esc_url( admin_url( 'widgets.php' ) ); ?>" class="welcome-icon dashicons-welcome-widgets-menus"><?php _e( 'Widgets', 'sportspress' ); ?></a></li>
</ul>
</div>
<div class="return-to-dashboard">
<a href="<?php echo esc_url( admin_url( add_query_arg( array( 'page' => 'sportspress' ), 'admin.php' ) ) ); ?>"><?php _e( 'Go to SportsPress Settings', 'sportspress' ); ?></a>
@@ -317,8 +322,9 @@ class SP_Admin_Welcome {
$settings = array( array(
'id' => 'sportspress_sport',
'default' => 'custom',
'type' => 'groupselect',
'type' => 'sport',
'title' => __( 'Sport', 'sportspress' ),
'welcome' => true,
'class' => $class,
'options' => $sport_options,
));

View File

@@ -49,7 +49,7 @@ class SP_Settings_General extends SP_Settings_Page {
'title' => __( 'Sport', 'sportspress' ),
'id' => 'sportspress_sport',
'default' => 'custom',
'type' => 'groupselect',
'type' => 'sport',
'options' => $presets,
),
@@ -184,6 +184,10 @@ class SP_Settings_General extends SP_Settings_Page {
update_option( '_sp_needs_welcome', 0 );
endif;
if ( isset( $_POST['add_sample_data'] ) ):
SP_Admin_Sample_Data::insert_posts();
endif;
$settings = $this->get_settings();
SP_Admin_Settings::save_fields( $settings );

View File

@@ -1,6 +1,6 @@
<div class="wrap sportspress sp-config-wrap">
<h2>
<?php _e( 'Configure', 'sportspress' ); ?>
<?php _e( 'Configure SportsPress', 'sportspress' ); ?>
</h2>
<table class="form-table">
<tbody>

View File

@@ -1,6 +1,6 @@
<div class="wrap sportspress sp-overview-wrap">
<h2>
<?php _e( 'Overview', 'sportspress' ); ?>
<?php _e( 'SportsPress Overview', 'sportspress' ); ?>
</h2>
<div class="sp-sitemap">