diff --git a/admin/post-types/player.php b/admin/post-types/player.php index 0cec6658..ceee3972 100644 --- a/admin/post-types/player.php +++ b/admin/post-types/player.php @@ -146,7 +146,11 @@ function sp_player_stats_meta( $post ) { else: // Calculate value - $placeholders[ $team_id ][ $div_id ][ $key ] = $eos->solveIF( str_replace( ' ', '', $value ), $totals ); + if ( sizeof( $events ) > 0 ): + $placeholders[ $team_id ][ $div_id ][ $key ] = sp_solve( $value, $totals ); + else: + $placeholders[ $team_id ][ $div_id ][ $key ] = 0; + endif; endif; diff --git a/admin/post-types/team.php b/admin/post-types/team.php index 33caa3e6..459ab8f3 100644 --- a/admin/post-types/team.php +++ b/admin/post-types/team.php @@ -134,7 +134,11 @@ function sp_team_columns_meta( $post ) { // Generate array of placeholder values for each league $placeholders[ $div_id ] = array(); foreach ( $equations as $key => $value ): - $placeholders[ $div_id ][ $key ] = $eos->solveIF( str_replace( ' ', '', $value ), $totals ); + if ( sizeof( $events ) > 0 ): + $placeholders[ $div_id ][ $key ] = sp_solve( $value, $totals ); + else: + $placeholders[ $div_id ][ $key ] = 0; + endif; endforeach; endforeach; diff --git a/sportspress-functions.php b/sportspress-functions.php index b38a1e69..ce9cfbfb 100644 --- a/sportspress-functions.php +++ b/sportspress-functions.php @@ -779,15 +779,41 @@ if ( !function_exists( 'sportspress_render_option_field' ) ) { } } +if ( !function_exists( 'sp_solve' ) ) { + function sp_solve( $equation, $vars ) { + + // Clearance to begin calculating remains true if all equation variables are in vars + $clearance = true; + + // Check if each variable part is in vars + $parts = explode( ' ', $equation ); + foreach( $parts as $key => $value ): + if ( substr( $value, 0, 1 ) == '$' ): + if ( ! array_key_exists( preg_replace( "/[^a-z]/", '', $value ), $vars ) ) + $clearance = false; + endif; + endforeach; + + if ( $clearance ): + // Equation Operating System + $eos = new eqEOS(); + + // Solve using EOS + return $eos->solveIF( str_replace( ' ', '', $equation ), $vars ); + else: + return 0; + endif; + + } + +} + if ( !function_exists( 'sp_get_table' ) ) { function sp_get_table( $post_id, $breakdown = false ) { $div_id = sp_get_the_term_id( $post_id, 'sp_league', 0 ); $team_ids = (array)get_post_meta( $post_id, 'sp_team', false ); $table_stats = (array)get_post_meta( $post_id, 'sp_teams', true ); - // Equation Operating System - $eos = new eqEOS(); - // Get labels from result variables $result_labels = (array)sp_get_var_labels( 'sp_result' ); @@ -915,7 +941,11 @@ if ( !function_exists( 'sp_get_table' ) ) { foreach ( $stats as $stat ): if ( sp_array_value( $placeholders[ $team_id ], $stat->post_name, '' ) == '' ): - $placeholders[ $team_id ][ $stat->post_name ] = $eos->solveIF( str_replace( ' ', '', $stat->equation ), sp_array_value( $totals, $team_id, array() ) ); + if ( sizeof( $events ) > 0 ): + $placeholders[ $team_id ][ $stat->post_name ] = sp_solve( $stat->equation, sp_array_value( $totals, $team_id, array() ) ); + else: + $placeholders[ $team_id ][ $stat->post_name ] = 0; + endif; endif; endforeach; endforeach; @@ -1126,8 +1156,11 @@ if ( !function_exists( 'sp_get_list' ) ) { else: // Calculate value - $placeholders[ $player_id ][ $statistic->post_name ] = $eos->solveIF( str_replace( ' ', '', $statistic->equation ), sp_array_value( $totals, $player_id, array() ) ); - + if ( sizeof( $events ) > 0 ): + $placeholders[ $player_id ][ $statistic->post_name ] = sp_solve( $statistic->equation, sp_array_value( $totals, $player_id, array() ) ); + else: + $placeholders[ $player_id ][ $statistic->post_name ] = 0; + endif; endif; endif; endforeach; diff --git a/sportspress-globals.php b/sportspress-globals.php index 90bd8298..03661ab7 100644 --- a/sportspress-globals.php +++ b/sportspress-globals.php @@ -67,4 +67,6 @@ $sportspress_thumbnail_texts = array( 'Remove featured image' => sprintf( __( 'Remove %s', 'sportspress' ), __( 'Photo', 'sportspress' ) ) ) ); + +add_option( 'sp_event_team_count', 2 ); // TODO: dynamic team appending ?> \ No newline at end of file