From 5ba33fd6296dacead247023f06cad2e61cfd5ec6 Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Fri, 13 Jun 2014 23:21:59 +1000 Subject: [PATCH] Use gettext filter to modify fronted text --- .../admin/settings/class-sp-settings-text.php | 22 +++++-- includes/class-sp-league-table.php | 2 +- includes/class-sp-player-list.php | 2 +- includes/class-sp-player.php | 2 +- includes/class-sp-team.php | 2 +- includes/class-sp-text.php | 49 ---------------- includes/sp-core-functions.php | 58 +++++++++---------- includes/sp-template-hooks.php | 6 +- sportspress.php | 9 +-- templates/event-blocks.php | 2 +- templates/event-calendar.php | 2 +- templates/event-details.php | 8 +-- templates/event-list.php | 18 +++--- templates/event-performance.php | 4 +- templates/event-results.php | 6 +- templates/event-venue.php | 2 +- templates/league-table.php | 4 +- templates/player-details.php | 4 +- templates/player-gallery.php | 2 +- templates/player-list.php | 4 +- templates/staff-details.php | 6 +- 21 files changed, 90 insertions(+), 124 deletions(-) delete mode 100644 includes/class-sp-text.php diff --git a/includes/admin/settings/class-sp-settings-text.php b/includes/admin/settings/class-sp-settings-text.php index 78a22016..ceb1d2b3 100644 --- a/includes/admin/settings/class-sp-settings-text.php +++ b/includes/admin/settings/class-sp-settings-text.php @@ -43,14 +43,16 @@ class SP_Settings_Text extends SP_Settings_Page { ); $strings = sp_get_text_options(); + $options = get_option( 'sportspress_text' ); - foreach ( $strings as $key => $value ): + foreach ( $strings as $string ): $settings[] = array( - 'title' => $value, - 'id' => 'sportspress_' . $key . '_text', - 'default' => '', - 'placeholder' => $value, - 'type' => 'text', + 'title' => $string, + 'id' => 'sportspress_text[' . $string . ']', + 'default' => '', + 'placeholder' => $string, + 'value' => sp_array_value( $options, $string, null ), + 'type' => 'text', ); endforeach; @@ -58,6 +60,14 @@ class SP_Settings_Text extends SP_Settings_Page { return apply_filters( 'sportspress_text_settings', $settings ); // End event settings } + + /** + * Save settings + */ + public function save() { + if ( isset( $_POST['sportspress_text'] ) ) + update_option( 'sportspress_text', $_POST['sportspress_text'] ); + } } endif; diff --git a/includes/class-sp-league-table.php b/includes/class-sp-league-table.php index 15cafcf4..ff0c7beb 100644 --- a/includes/class-sp-league-table.php +++ b/includes/class-sp-league-table.php @@ -316,7 +316,7 @@ class SP_League_Table extends SP_Custom_Post{ unset( $columns[ $key ] ); endif; endforeach; - $labels = array_merge( array( 'name' => SP()->text->string('Team') ), $columns ); + $labels = array_merge( array( 'name' => __( 'Team', 'sportspress' ) ), $columns ); $merged[0] = $labels; return $merged; endif; diff --git a/includes/class-sp-player-list.php b/includes/class-sp-player-list.php index fe92d7ca..b69c94dd 100644 --- a/includes/class-sp-player-list.php +++ b/includes/class-sp-player-list.php @@ -358,7 +358,7 @@ class SP_Player_List extends SP_Custom_Post { endif; endforeach; - $labels = array( 'name' => SP()->text->string('Player') ); + $labels = array( 'name' => __( 'Player', 'sportspress' ) ); if ( in_array( 'team', $this->columns ) ) $labels['team'] = __( 'Team', 'sportspress' ); diff --git a/includes/class-sp-player.php b/includes/class-sp-player.php index 74861d24..61e6e57b 100644 --- a/includes/class-sp-player.php +++ b/includes/class-sp-player.php @@ -316,7 +316,7 @@ class SP_Player extends SP_Custom_Post { unset( $columns[ $key ] ); endif; endforeach; - $labels = array_merge( array( 'name' => SP()->text->string('Season'), 'team' => SP()->text->string('Team') ), $columns ); + $labels = array_merge( array( 'name' => __( 'Season', 'sportspress' ), 'team' => __( 'Team', 'sportspress' ) ), $columns ); $merged[0] = $labels; return $merged; endif; diff --git a/includes/class-sp-team.php b/includes/class-sp-team.php index 2ebd96c4..7a665047 100644 --- a/includes/class-sp-team.php +++ b/includes/class-sp-team.php @@ -252,7 +252,7 @@ class SP_Team extends SP_Custom_Post { if ( $admin ): return array( $columns, $data, $placeholders, $merged, $leagues_seasons ); else: - $labels = array_merge( array( 'name' => SP()->text->string('Season') ), $columns ); + $labels = array_merge( array( 'name' => __( 'Season', 'sportspress' ) ), $columns ); $merged[0] = $labels; return $merged; endif; diff --git a/includes/class-sp-text.php b/includes/class-sp-text.php deleted file mode 100644 index a03039da..00000000 --- a/includes/class-sp-text.php +++ /dev/null @@ -1,49 +0,0 @@ -data = sp_get_text_options(); - } - - public function __get( $key ) { - return ( array_key_exists( $key, $this->data ) ? $this->data[ $key ] : null ); - } - - public function __set( $key, $value ){ - $this->data[ $key ] = $value; - } - - public function string( $string ){ - if ( is_admin() ) - return $string; - - $key = str_replace( '-', '_', sanitize_title( $string ) ); - - if ( array_key_exists( $key, $this->data ) ): - $string = get_option( 'sportspress_' . $key . '_text' ); - return ( empty( $string ) ? $this->data[ $key ] : $string ); - else: - return $string; - endif; - } -} diff --git a/includes/sp-core-functions.php b/includes/sp-core-functions.php index 059f4871..b921f125 100644 --- a/includes/sp-core-functions.php +++ b/includes/sp-core-functions.php @@ -2387,35 +2387,35 @@ function sp_get_sport_presets() { */ function sp_get_text_options() { $strings = apply_filters( 'sportspress_text', array( - 'article' => __( 'Article', 'sportspress' ), - 'current_team' => __( 'Current Team', 'sportspress' ), - 'date' => __( 'Date', 'sportspress' ), - 'details' => __( 'Details', 'sportspress' ), - 'event' => __( 'Event', 'sportspress' ), - 'league' => __( 'League', 'sportspress' ), - 'nationality' => __( 'Nationality', 'sportspress' ), - 'outcome' => __( 'Outcome', 'sportspress' ), - 'past_teams' => __( 'Past Teams', 'sportspress' ), - 'played' => __( 'Played', 'sportspress' ), - 'player' => __( 'Player', 'sportspress' ), - 'pos' => __( 'Pos', 'sportspress' ), - 'position' => __( 'Position', 'sportspress' ), - 'preview' => __( 'Preview', 'sportspress' ), - 'rank' => __( 'Rank', 'sportspress' ), - 'recap' => __( 'Recap', 'sportspress' ), - 'team_results' => __( 'Team Results', 'sportspress' ), - 'season' => __( 'Season', 'sportspress' ), - 'staff' => __( 'Staff', 'sportspress' ), - 'substitutes' => __( 'Substitutes', 'sportspress' ), - 'team' => __( 'Team', 'sportspress' ), - 'teams' => __( 'Teams', 'sportspress' ), - 'time' => __( 'Time', 'sportspress' ), - 'timeresults' => __( 'Time/Results', 'sportspress' ), - 'total' => __( 'Total', 'sportspress' ), - 'venue' => __( 'Venue', 'sportspress' ), - 'view_all_events' => __( 'View all events', 'sportspress' ), - 'view_all_players' => __( 'View all players', 'sportspress' ), - 'view_full_table' => __( 'View full table', 'sportspress' ), + __( 'Article', 'sportspress' ), + __( 'Current Team', 'sportspress' ), + __( 'Date', 'sportspress' ), + __( 'Details', 'sportspress' ), + __( 'Event', 'sportspress' ), + __( 'League', 'sportspress' ), + __( 'Nationality', 'sportspress' ), + __( 'Outcome', 'sportspress' ), + __( 'Past Teams', 'sportspress' ), + __( 'Played', 'sportspress' ), + __( 'Player', 'sportspress' ), + __( 'Pos', 'sportspress' ), + __( 'Position', 'sportspress' ), + __( 'Preview', 'sportspress' ), + __( 'Rank', 'sportspress' ), + __( 'Recap', 'sportspress' ), + __( 'Team Results', 'sportspress' ), + __( 'Season', 'sportspress' ), + __( 'Staff', 'sportspress' ), + __( 'Substitutes', 'sportspress' ), + __( 'Team', 'sportspress' ), + __( 'Teams', 'sportspress' ), + __( 'Time', 'sportspress' ), + __( 'Time/Results', 'sportspress' ), + __( 'Total', 'sportspress' ), + __( 'Venue', 'sportspress' ), + __( 'View all events', 'sportspress' ), + __( 'View all players', 'sportspress' ), + __( 'View full table', 'sportspress' ), )); asort( $strings ); return $strings; diff --git a/includes/sp-template-hooks.php b/includes/sp-template-hooks.php index de6ff053..a70e373e 100644 --- a/includes/sp-template-hooks.php +++ b/includes/sp-template-hooks.php @@ -190,7 +190,11 @@ function sportspress_gettext( $translated_text, $untranslated_text, $domain ) { endswitch; endif; else: - if ( ! current_theme_supports( 'sportspress' ) && $untranslated_text == 'Archives' && is_tax( 'sp_venue' ) ): + if ( $domain == 'sportspress' ): + if ( ! empty( SP()->text[ $untranslated_text ] ) ): + $translated_text = SP()->text[ $untranslated_text ]; + endif; + elseif ( ! current_theme_supports( 'sportspress' ) && $untranslated_text == 'Archives' && is_tax( 'sp_venue' ) ): $slug = get_query_var( 'sp_venue' ); if ( $slug ): $venue = get_term_by( 'slug', $slug, 'sp_venue' ); diff --git a/sportspress.php b/sportspress.php index 443703b5..49427c23 100644 --- a/sportspress.php +++ b/sportspress.php @@ -52,9 +52,9 @@ final class SportsPress { public $formats = null; /** - * @var SP_Text $text + * @var array */ - public $text = null; + public $text = array(); /** * Main SportsPress Instance @@ -211,7 +211,6 @@ final class SportsPress { // Classes (used on all pages) include_once( 'includes/class-sp-countries.php' ); // Defines continents and countries include_once( 'includes/class-sp-formats.php' ); // Defines custom post type formats - include_once( 'includes/class-sp-text.php' ); // Defines editable strings // Include template hooks in time for themes to remove/modify them include_once( 'includes/sp-template-hooks.php' ); @@ -261,7 +260,9 @@ final class SportsPress { // Load class instances $this->countries = new SP_Countries(); // Countries class $this->formats = new SP_Formats(); // Formats class - $this->text = new SP_Text(); // Text class + + // Load string options + $this->text = get_option( 'sportspress_text', array() ); // Init action do_action( 'sportspress_init' ); diff --git a/templates/event-blocks.php b/templates/event-blocks.php index e23cb968..4ab5c63a 100644 --- a/templates/event-blocks.php +++ b/templates/event-blocks.php @@ -102,5 +102,5 @@ if ( isset( $columns ) ) ' . SP()->text->string('View all events') . ''; + echo '' . __( 'View all events', 'sportspress' ) . ''; ?> \ No newline at end of file diff --git a/templates/event-calendar.php b/templates/event-calendar.php index 6331cda9..4952ee23 100644 --- a/templates/event-calendar.php +++ b/templates/event-calendar.php @@ -209,6 +209,6 @@ if ( $pad != 0 && $pad != 7 ) $calendar_output .= "\n\t\n\t\n\t\n\t"; if ( $id && $show_all_events_link ) - $calendar_output .= '' . SP()->text->string('View all events') . ''; + $calendar_output .= '' . __( 'View all events', 'sportspress' ) . ''; echo apply_filters( 'sportspress_event_calendar', $calendar_output ); diff --git a/templates/event-details.php b/templates/event-details.php index 7519deb9..3efe4783 100644 --- a/templates/event-details.php +++ b/templates/event-details.php @@ -17,19 +17,19 @@ $time = get_the_time( get_option('time_format'), $id ); $leagues = get_the_terms( $id, 'sp_league' ); $seasons = get_the_terms( $id, 'sp_season' ); -$data = array( SP()->text->string('Date') => $date, SP()->text->string('Time') => $time ); +$data = array( __( 'Date', 'sportspress' ) => $date, __( 'Time', 'sportspress' ) => $time ); if ( $leagues ): $league = array_pop( $leagues ); - $data[ SP()->text->string('League') ] = $league->name; + $data[ __( 'League', 'sportspress' ) ] = $league->name; endif; if ( $seasons ): $season = array_pop( $seasons ); - $data[ SP()->text->string('Season') ] = $season->name; + $data[ __( 'Season', 'sportspress' ) ] = $season->name; endif; ?> -

text->string('Details'); ?>

+

diff --git a/templates/event-list.php b/templates/event-list.php index 6018314b..6ab4f5f3 100644 --- a/templates/event-list.php +++ b/templates/event-list.php @@ -42,22 +42,22 @@ if ( isset( $columns ) ) ' . SP()->text->string('Date') . ''; + echo ''; if ( $usecolumns == null || in_array( 'event', $usecolumns ) ) - echo ''; + echo ''; if ( $usecolumns == null || in_array( 'teams', $usecolumns ) ) - echo ''; + echo ''; if ( $usecolumns == null || in_array( 'time', $usecolumns ) ) - echo ''; + echo ''; if ( $usecolumns == null || in_array( 'venue', $usecolumns ) ) - echo ''; + echo ''; if ( $usecolumns == null || in_array( 'article', $usecolumns ) ) - echo ''; + echo ''; ?> @@ -160,9 +160,9 @@ if ( isset( $columns ) ) endif; if ( $event->post_content !== null ): if ( $event->post_status == 'publish' ): - echo SP()->text->string('Recap'); + echo __( 'Recap', 'sportspress' ); else: - echo SP()->text->string('Preview'); + echo __( 'Preview', 'sportspress' ); endif; endif; @@ -180,5 +180,5 @@ if ( isset( $columns ) ) ' . SP()->text->string('View all events') . ''; + echo '' . __( 'View all events', 'sportspress' ) . ''; ?> \ No newline at end of file diff --git a/templates/event-performance.php b/templates/event-performance.php index 8c58755e..e2448324 100644 --- a/templates/event-performance.php +++ b/templates/event-performance.php @@ -47,7 +47,7 @@ foreach( $teams as $key => $team_id ): - + $label ): ?> @@ -130,7 +130,7 @@ foreach( $teams as $key => $team_id ):  '; - echo ''; + echo ''; endif; $row = $data[0]; diff --git a/templates/event-results.php b/templates/event-results.php index d7f4f2b0..06879a57 100644 --- a/templates/event-results.php +++ b/templates/event-results.php @@ -83,16 +83,16 @@ if ( empty( $table_rows ) ): else: - $output .= '

' . SP()->text->string('Team Results') . '

'; + $output .= '

' . __( 'Team Results', 'sportspress' ) . '

'; $output .= '
' . '
' . __( 'Date', 'sportspress' ) . '' . SP()->text->string('Event') . '' . __( 'Event', 'sportspress' ) . '' . SP()->text->string('Teams') . '' . __( 'Teams', 'sportspress' ) . '' . SP()->text->string('Time/Results') . '' . __( 'Time/Results', 'sportspress' ) . '' . SP()->text->string('Venue') . '' . __( 'Venue', 'sportspress' ) . '' . SP()->text->string('Article') . '' . __( 'Article', 'sportspress' ) . '
#text->string('Player'); ?> ' . SP()->text->string('Total') . '' . __( 'Total', 'sportspress' ) . '
' . - ''; + ''; foreach( $result_labels as $key => $label ): $output .= ''; endforeach; if ( $show_outcomes ): - $output .= ''; + $output .= ''; endif; $output .= '' . '' . ''; $output .= $table_rows; diff --git a/templates/event-venue.php b/templates/event-venue.php index da29792f..b04df126 100644 --- a/templates/event-venue.php +++ b/templates/event-venue.php @@ -32,7 +32,7 @@ foreach( $venues as $venue ): $latitude = sp_array_value( $meta, 'sp_latitude', 0 ); $longitude = sp_array_value( $meta, 'sp_longitude', 0 ); ?> -

text->string('Venue'); ?>

+

' . SP()->text->string('Team') . '' . __( 'Team', 'sportspress' ) . '' . $label . '' . SP()->text->string('Outcome') . '' . __( 'Outcome', 'sportspress' ) . '
diff --git a/templates/league-table.php b/templates/league-table.php index 17d2c9ce..75eba2b2 100644 --- a/templates/league-table.php +++ b/templates/league-table.php @@ -46,7 +46,7 @@ if ( ! $columns ) if ( ! is_array( $columns ) ) $columns = explode( ',', $columns ); -$output .= ''; +$output .= ''; foreach( $labels as $key => $label ): if ( ! is_array( $columns ) || $key == 'name' || in_array( $key, $columns ) ) @@ -107,6 +107,6 @@ $output .= '' . '
' . SP()->text->string('Pos') . '' . __( 'Pos', 'sportspress' ) . '
'; $output .= '
'; if ( $show_full_table_link ) - $output .= '' . SP()->text->string('View full table') . ''; + $output .= '' . __( 'View full table', 'sportspress' ) . ''; echo $output; diff --git a/templates/player-details.php b/templates/player-details.php index a4761a7c..080e3d97 100644 --- a/templates/player-details.php +++ b/templates/player-details.php @@ -31,7 +31,7 @@ $metrics_after = $player->metrics( false ); $common = array(); if ( $nationality ): $country_name = sp_array_value( $countries, $nationality, null ); - $common[ SP()->text->string('Nationality') ] = $country_name ? ( $show_nationality_flags ? '' . $nationality . ' ' : '' ) . $country_name : '—'; + $common[ __( 'Nationality', 'sportspress' ) ] = $country_name ? ( $show_nationality_flags ? '' . $nationality . ' ' : '' ) . $country_name : '—'; endif; $data = array_merge( $metrics_before, $common, $metrics_after ); @@ -50,7 +50,7 @@ if ( $past_teams ): foreach ( $past_teams as $team ): $teams[] = '' . get_the_title( $team ) . ''; endforeach; - $data[ SP()->text->string('Past Teams') ] = implode( ', ', $teams ); + $data[ __( 'Past Teams', 'sportspress' ) ] = implode( ', ', $teams ); endif; $output = '
' . diff --git a/templates/player-gallery.php b/templates/player-gallery.php index 7ff6b208..e2810f06 100644 --- a/templates/player-gallery.php +++ b/templates/player-gallery.php @@ -142,4 +142,4 @@ endforeach; echo "
\n"; if ( $show_all_players_link ) - echo '' . SP()->text->string('View all players') . ''; + echo '' . __( 'View all players', 'sportspress' ) . ''; diff --git a/templates/player-list.php b/templates/player-list.php index b3d527e5..5bac3202 100644 --- a/templates/player-list.php +++ b/templates/player-list.php @@ -85,7 +85,7 @@ foreach ( $groups as $group ): if ( in_array( $orderby, array( 'number', 'name' ) ) ): $output .= '#'; else: - $output .= '' . SP()->text->string('Rank') . ''; + $output .= '' . __( 'Rank', 'sportspress' ) . ''; endif; foreach( $labels as $key => $label ): @@ -153,6 +153,6 @@ foreach ( $groups as $group ): endforeach; if ( $show_all_players_link ) - $output .= '' . SP()->text->string('View all players') . ''; + $output .= '' . __( 'View all players', 'sportspress' ) . ''; echo apply_filters( 'sportspress_player_list', $output ); diff --git a/templates/staff-details.php b/templates/staff-details.php index 55e30c3a..c2de25d5 100644 --- a/templates/staff-details.php +++ b/templates/staff-details.php @@ -29,18 +29,18 @@ $past_teams = $staff->past_teams(); $data = array(); if ( $nationality ): $country_name = sp_array_value( $countries, $nationality, null ); - $data[ SP()->text->string('Nationality') ] = $country_name ? ( $show_nationality_flags ? '' . $nationality . ' ' : '' ) . $country_name : '—'; + $data[ __( 'Nationality', 'sportspress' ) ] = $country_name ? ( $show_nationality_flags ? '' . $nationality . ' ' : '' ) . $country_name : '—'; endif; if ( $current_team ) - $data[ SP()->text->string('Current Team') ] = '' . get_the_title( $current_team ) . ''; + $data[ __( 'Current Team', 'sportspress' ) ] = '' . get_the_title( $current_team ) . ''; if ( $past_teams ): $teams = array(); foreach ( $past_teams as $team ): $teams[] = '' . get_the_title( $team ) . ''; endforeach; - $data[ SP()->text->string('Past Teams') ] = implode( ', ', $teams ); + $data[ __( 'Past Teams', 'sportspress' ) ] = implode( ', ', $teams ); endif; $output = '
' .