Skip to content

Commit a371da3

Browse files
committed
Merge pull request #288 from Automattic/fix/exceptions
Avoid blindly throwing exceptions
2 parents defe6e0 + 8af1e3e commit a371da3

File tree

5 files changed

+64
-134
lines changed

5 files changed

+64
-134
lines changed

api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function bbl_get_current_lang_code() {
6666
* Given a lang object or lang code, this checks whether the
6767
* language is public or not.
6868
*
69-
* @param string $lang_code A language code
69+
* @param string|object $lang_code A language code or a language object
7070
* @return boolean True if public
7171
* @access public
7272
**/

class-languages.php

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,37 @@ public function options() {
153153
$langs = $this->merge_lang_sets( $this->available_langs, $this->lang_prefs );
154154
// Merge in any POSTed field values
155155
foreach ( $langs as $code => & $lang ) {
156-
$lang->url_prefix = ( @ isset( $_POST[ 'url_prefix_' . $code ] ) ) ? $_POST[ "url_prefix_$code" ] : @ $lang->url_prefix;
157-
if ( ! $lang->url_prefix )
158-
$lang->url_prefix = $lang->url_prefix;
159-
$lang->text_direction = $lang->text_direction;
156+
if ( ! empty( $_POST[ "url_prefix_$code" ] ) ) {
157+
$lang->url_prefix = $_POST[ "url_prefix_$code" ];
158+
} else if ( empty( $lang->url_prefix ) ) {
159+
$parts = explode( '_', $lang->code );
160+
$lang->url_prefix = $parts[0];
161+
}
162+
if ( ! in_array( $lang->text_direction, array( 'ltr', 'rtl' ) ) ) {
163+
// @TODO is this needed?
164+
$lang->text_direction = 'ltr';
165+
}
160166
// This line must come after the text direction value is set
161167
$lang->input_lang_class = ( 'rtl' == $lang->text_direction ) ? 'lang-rtl' : 'lang-ltr' ;
162-
$lang->display_name = ( @ isset( $_POST[ "display_name_$code" ] ) ) ? $_POST[ "display_name_$code" ] : @ $lang->display_name;
163-
if ( ! $lang->display_name )
168+
169+
if ( ! empty( $_POST[ "display_name_$code" ] ) ) {
170+
$lang->display_name = $_POST[ "display_name_$code" ];
171+
} else if ( empty( $lang->display_name ) ) {
164172
$lang->display_name = $lang->name;
173+
}
174+
165175
// Note any url_prefix errors
166-
$lang->url_prefix_error = ( @ $this->errors[ "url_prefix_$code" ] ) ? 'babble-error' : '0' ;
176+
if ( ! empty( $this->errors[ "url_prefix_$code" ] ) ) {
177+
$lang->url_prefix_error = 'babble-error';
178+
} else {
179+
$lang->url_prefix_error = '0';
180+
}
181+
167182
// Flag the active languages
168183
$lang->active = false;
169-
if ( in_array( $code, $this->active_langs ) )
184+
if ( in_array( $code, $this->active_langs ) ) {
170185
$lang->active = true;
186+
}
171187

172188
}
173189
$vars = array();
@@ -204,12 +220,13 @@ public function get_active_langs() {
204220
* Given a lang object or lang code, this checks whether the
205221
* language is public or not.
206222
*
207-
* @param string $lang_code A language code
223+
* @param string|object $lang_code A language code or a language object
208224
* @return boolean True if public
209225
**/
210226
public function is_public_lang( $lang_code ) {
211-
if ( ! is_string( $lang_code ) )
212-
throw new exception( 'Please provide a lang_code for the is_public_lang method.' );
227+
if ( is_object( $lang_code ) and ! empty( $lang_code->lang ) ) {
228+
$lang_code = $lang_code->lang;
229+
}
213230
return in_array( $lang_code, $this->public_langs );
214231
}
215232

@@ -330,8 +347,19 @@ protected function maybe_process_languages() {
330347
$url_prefixes = array();
331348
foreach ( $this->available_langs as $code => $lang ) {
332349
$lang_pref = new stdClass;
333-
$lang_pref->display_name = @ $_POST[ 'display_name_' . $code ];
334-
$lang_pref->url_prefix = @ $_POST[ 'url_prefix_' . $code ];
350+
351+
if ( ! empty( $_POST[ 'display_name_' . $code ] ) ) {
352+
$lang_pref->display_name = $_POST[ 'display_name_' . $code ];
353+
} else {
354+
$lang_pref->display_name = $lang->name;
355+
}
356+
357+
if ( ! empty( $_POST[ 'url_prefix_' . $code ] ) ) {
358+
$lang_pref->url_prefix = $_POST[ 'url_prefix_' . $code ];
359+
} else {
360+
$lang_pref->url_prefix = $lang->url_prefix;
361+
}
362+
335363
// Check we don't have more than one language using the same url prefix
336364
if ( array_key_exists( $lang_pref->url_prefix, $url_prefixes ) ) {
337365
$lang_1 = $this->format_code_lang( $code );
@@ -351,8 +379,11 @@ protected function maybe_process_languages() {
351379
if ( ! $this->errors ) {
352380
$langs = $this->merge_lang_sets( $this->available_langs, $this->lang_prefs );
353381
$active_langs = array();
354-
foreach ( (array) @ $_POST[ 'active_langs' ] as $code )
355-
$active_langs[ $langs[ $code ]->url_prefix ] = $code;
382+
if ( ! empty( $_POST[ 'active_langs' ] ) && is_array( $_POST[ 'active_langs' ] ) ) {
383+
foreach ( $_POST[ 'active_langs' ] as $code ) {
384+
$active_langs[ $langs[ $code ]->url_prefix ] = $code;
385+
}
386+
}
356387
if ( count( $active_langs ) < 2 ) {
357388
$this->set_admin_error( __( 'You must set at least two languages as active.', 'babble' ) );
358389
} else {
@@ -365,8 +396,11 @@ protected function maybe_process_languages() {
365396
$this->set_admin_error( __( 'You must set at least your default language as public.', 'babble' ) );
366397
} else {
367398
$public_langs = (array) $_POST[ 'public_langs' ];
368-
if ( ! in_array( @ $_POST[ 'default_lang' ], $public_langs ) )
399+
if ( empty( $_POST[ 'default_lang' ] ) ) {
400+
$this->set_admin_error( __( 'You must choose a default language.', 'babble' ) );
401+
} else if ( ! in_array( $_POST[ 'default_lang' ], $public_langs ) ) {
369402
$this->set_admin_error( __( 'You must set your default language as public.', 'babble' ) );
403+
}
370404
}
371405
}
372406
// Finish up, redirecting if we're all OK
@@ -375,7 +409,7 @@ protected function maybe_process_languages() {
375409
$this->update_option( 'public_langs', $public_langs );
376410

377411
// First the default language
378-
$default_lang = @ $_POST[ 'default_lang' ];
412+
$default_lang = $_POST[ 'default_lang' ];
379413
$this->update_option( 'default_lang', $default_lang );
380414
// Now the prefs
381415
$this->update_option( 'lang_prefs', $lang_prefs );

class-plugin.php

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -95,35 +95,21 @@ class Babble_Plugin {
9595
**/
9696
protected $type;
9797

98-
/**
99-
* Note the name of the function to call when the theme is activated.
100-
*
101-
* @var string
102-
**/
103-
protected $theme_activation_function;
104-
10598
/**
10699
* Initiate!
107100
*
108101
* @return void
109102
* @author Simon Wheatley
110103
**/
111-
public function setup( $name = '', $type = null ) {
112-
if ( ! $name )
113-
throw new exception( "Please pass the name parameter into the setup method." );
104+
public function setup( $name, $type = null ) {
114105
$this->name = $name;
115106

116107
// Attempt to handle a Windows
117108
$ds = ( defined( 'DIRECTORY_SEPARATOR' ) ) ? DIRECTORY_SEPARATOR : '\\';
118109
$file = str_replace( $ds, '/', __FILE__ );
119110
$plugins_dir = str_replace( $ds, '/', dirname( __FILE__ ) );
120-
// Setup the dir and url for this plugin/theme
121-
if ( 'theme' == $type ) {
122-
// This is a theme
123-
$this->type = 'theme';
124-
$this->dir = get_stylesheet_directory();
125-
$this->url = get_stylesheet_directory_uri();
126-
} elseif ( stripos( $file, $plugins_dir ) !== false || 'plugin' == $type ) {
111+
// Setup the dir and url for this plugin
112+
if ( stripos( $file, $plugins_dir ) !== false || 'plugin' == $type ) {
127113
// This is a plugin
128114
$this->type = 'plugin';
129115

@@ -228,29 +214,9 @@ function remove_filter ($filter, $function = '', $priority = 10, $accepted_args
228214
function register_activation ( $pluginfile = __FILE__, $function = '' ) {
229215
if ( $this->type == 'plugin' ) {
230216
add_action ('activate_'.basename (dirname ($pluginfile)).'/'.basename ($pluginfile), array ($this, $function == '' ? 'activate' : $function));
231-
} elseif ( $this->type == 'theme' ) {
232-
$this->theme_activation_function = ( $function ) ? $function : 'activate';
233-
add_action ('load-themes.php', array ( $this, 'theme_activation' ) );
234217
}
235218
}
236219

237-
/**
238-
* Hack to catch theme activation. We hook the load-themes.php action, look for the
239-
* "activated" GET param and make a big fat assumption if we find it.
240-
*
241-
* @return void
242-
* @author Simon Wheatley
243-
**/
244-
public function theme_activation() {
245-
$activated = (bool) @ $_GET[ 'activated' ];
246-
if ( ! $activated )
247-
return;
248-
if ( ! $this->theme_activation_function )
249-
return;
250-
// Looks like the theme might just have been activated, call the registered function
251-
$this->{$this->theme_activation_function}();
252-
}
253-
254220
/**
255221
* Special deactivation function that takes into account the plugin directory
256222
*

class-switcher-content.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ protected function populate_links() {
9696
$this->jobs = bbl_get_incomplete_post_jobs( get_option( 'page_for_posts' ) );
9797
} else if ( ( !is_admin() and ( is_tax() || is_category() ) ) || $editing_term ) {
9898
if ( isset( $_REQUEST[ 'tag_ID' ] ) )
99-
$term = get_term( (int) @ $_REQUEST[ 'tag_ID' ], $this->screen->taxonomy );
99+
$term = get_term( absint( $_REQUEST[ 'tag_ID' ] ), $this->screen->taxonomy );
100100
else
101101
$term = get_queried_object();
102102
$this->translations = bbl_get_term_translations( $term->term_id, $term->taxonomy );

class-taxonomy.php

Lines changed: 8 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public function __construct() {
4949
$this->add_action( 'init', 'init_early', 0 );
5050
$this->add_action( 'parse_request' );
5151
$this->add_action( 'registered_taxonomy', null, null, 3 );
52-
$this->add_action( 'save_post', null, null, 2 );
5352
$this->add_action( 'set_object_terms', null, null, 5 );
5453
$this->add_filter( 'get_terms' );
5554
$this->add_filter( 'term_link', null, null, 3 );
@@ -263,18 +262,6 @@ public function created_new_shadow_post( $new_post_id, $origin_post_id ) {
263262
$this->no_recursion = false;
264263
}
265264

266-
/**
267-
* Hooks the WP save_post action to resync data
268-
* when requested.
269-
*
270-
* @param int $post_id The ID of the WP post
271-
* @param object $post The WP Post object
272-
* @return void
273-
**/
274-
public function save_post( $post_id, $post ) {
275-
$this->maybe_resync_terms( $post_id, $post );
276-
}
277-
278265
/**
279266
* Hooks the WordPress term_link filter to provide functions to provide
280267
* appropriate links for the shadow taxonomies.
@@ -373,9 +360,7 @@ public function get_terms( $terms ) {
373360
continue;
374361
}
375362
if ( isset( $this->taxonomies[ $term->taxonomy ] ) ) {
376-
if ( ! $this->get_transid( $term->term_id ) ) {
377-
throw new exception( "ERROR: Translated term ID $term->term_id does not have a transid" );
378-
} else {
363+
if ( $this->get_transid( $term->term_id ) ) {
379364
continue;
380365
}
381366
}
@@ -922,15 +907,15 @@ function update_post_term_count( $terms, $taxonomy ) {
922907
* belongs to.
923908
*
924909
* @param int $target_term_id The term ID to find the translation group for
925-
* @return int The transID the target term belongs to
910+
* @return int|false The transID the target term belongs to, boolean false on failure
926911
**/
927912
public function get_transid( $target_term_id ) {
928-
if ( $transid = wp_cache_get( $target_term_id, 'bbl_term_transids' ) ) {
929-
return $transid;
913+
if ( ! $target_term_id ) {
914+
return false;
930915
}
931916

932-
if ( ! $target_term_id ) {
933-
throw new exception( "Please specify a target term_id" );
917+
if ( $transid = wp_cache_get( $target_term_id, 'bbl_term_transids' ) ) {
918+
return $transid;
934919
}
935920

936921
$transids = wp_get_object_terms( $target_term_id, 'term_translation', array( 'fields' => 'ids' ) );
@@ -952,11 +937,11 @@ public function get_transid( $target_term_id ) {
952937
*
953938
* @param int $target_term_id The term ID to set the translation group for
954939
* @param int $translation_group_id The ID of the translation group to add this
955-
* @return int The transID the target term belongs to
940+
* @return int|false The transID the target term belongs to, false on failure
956941
**/
957942
public function set_transid( $target_term_id, $transid = null ) {
958943
if ( ! $target_term_id ) {
959-
throw new exception( "Please specify a target term_id" );
944+
return false;
960945
}
961946

962947
if ( ! $transid ) {
@@ -979,61 +964,6 @@ public function set_transid( $target_term_id, $transid = null ) {
979964
return $transid;
980965
}
981966

982-
/**
983-
* Checks for the relevant POSTed field, then
984-
* resyncs the terms.
985-
*
986-
* @param int $post_id The ID of the WP post
987-
* @param object $post The WP Post object
988-
* @return void
989-
**/
990-
protected function maybe_resync_terms( $post_id, $post ) {
991-
// Check that the fields were included on the screen, we
992-
// can do this by checking for the presence of the nonce.
993-
$nonce = isset( $_POST[ '_bbl_metabox_resync' ] ) ? $_POST[ '_bbl_metabox_resync' ] : false;
994-
995-
996-
if ( ! in_array( $post->post_status, array( 'draft', 'publish' ) ) ) {
997-
return;
998-
}
999-
1000-
if ( ! $nonce ) {
1001-
return;
1002-
}
1003-
1004-
$posted_id = isset( $_POST[ 'post_ID' ] ) ? $_POST[ 'post_ID' ] : 0;
1005-
if ( $posted_id != $post_id ) {
1006-
return;
1007-
}
1008-
// While we're at it, let's check the nonce
1009-
check_admin_referer( "bbl_resync_translation-$post_id", '_bbl_metabox_resync' );
1010-
1011-
if ( $this->no_recursion ) {
1012-
return;
1013-
}
1014-
$this->no_recursion = true;
1015-
1016-
$taxonomies = get_object_taxonomies( $post->post_type );
1017-
$origin_post = bbl_get_post_in_lang( $post_id, bbl_get_default_lang_code() );
1018-
1019-
// First dissociate all the terms from synced taxonomies from this post
1020-
wp_delete_object_term_relationships( $post_id, $taxonomies );
1021-
1022-
// Now associate terms from synced taxonomies in from the origin post
1023-
foreach ( $taxonomies as $taxonomy ) {
1024-
$origin_taxonomy = $taxonomy;
1025-
if ( $this->is_taxonomy_translated( $taxonomy ) ) {
1026-
$origin_taxonomy = bbl_get_taxonomy_in_lang( $taxonomy, bbl_get_default_lang_code() );
1027-
}
1028-
$term_ids = wp_get_object_terms( $origin_post->ID, $origin_taxonomy, array( 'fields' => 'ids' ) );
1029-
$term_ids = array_map( 'absint', $term_ids );
1030-
$result = wp_set_object_terms( $post_id, $term_ids, $taxonomy );
1031-
if ( is_wp_error( $result, true ) ) {
1032-
throw new exception( "Problem syncing terms: " . print_r( $terms, true ), " Error: " . print_r( $result, true ) );
1033-
}
1034-
}
1035-
}
1036-
1037967
}
1038968

1039969
global $bbl_taxonomies;

0 commit comments

Comments
 (0)