Conditions | 8 |
Paths | 13 |
Total Lines | 56 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
38 | public function import( array $data ) { |
||
39 | $source_blog_id = $this->import_coordinates->source_blog_id; |
||
40 | $source_post_id = $this->import_coordinates->source_post_id; |
||
41 | $dest_post_id = $this->import_coordinates->dest_post_id; |
||
42 | $dest_lang = $this->import_coordinates->dest_lang; |
||
43 | |||
44 | switch_to_blog( $source_blog_id ); |
||
|
|||
45 | |||
46 | $source_terms = wp_get_post_terms( $source_post_id, get_taxonomies() ); |
||
47 | $source_terms_ids = wp_list_pluck( $source_terms, 'term_id' ); |
||
48 | $msls_terms = array_combine( |
||
49 | $source_terms_ids, |
||
50 | array_map( array( MslsOptionsTaxTerm::class, 'create' ), $source_terms_ids ) |
||
51 | ); |
||
52 | |||
53 | switch_to_blog( $this->import_coordinates->dest_blog_id ); |
||
54 | |||
55 | /** @var \WP_Term $term */ |
||
56 | foreach ( $source_terms as $term ) { |
||
57 | // is there a translation for the term in this blog? |
||
58 | $msls_term = $msls_terms[ $term->term_id ]; |
||
59 | $dest_term_id = $msls_term->{$dest_lang}; |
||
60 | |||
61 | if ( null === $dest_term_id ) { |
||
62 | $dest_term_id = $this->create_local_term( $term, $msls_term, $dest_lang ); |
||
63 | } |
||
64 | |||
65 | if ( false === $dest_term_id ) { |
||
66 | continue; |
||
67 | } |
||
68 | |||
69 | $added = $this->update_object_terms( $dest_post_id, $dest_term_id, $term->taxonomy ); |
||
70 | |||
71 | if ( is_array( $added ) && ! count( array_filter( $added ) ) ) { |
||
72 | // while we think the term translation exists it might not, let's create it |
||
73 | $dest_term_id = $this->create_local_term( $term, $msls_term, $dest_lang ); |
||
74 | |||
75 | if ( false === $dest_term_id ) { |
||
76 | continue; |
||
77 | } |
||
78 | |||
79 | // and try again |
||
80 | $added = $this->update_object_terms( $dest_post_id, $dest_term_id, $term->taxonomy ); |
||
81 | } |
||
82 | |||
83 | if ( $added instanceof \WP_Error ) { |
||
84 | $this->logger->log_error( "term/added/{$term->taxonomy}", array( $term->name => $term->term_id ) ); |
||
85 | } else { |
||
86 | $this->logger->log_success( "term/added/{$term->taxonomy}", array( $term->name => $term->term_id ) ); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | restore_current_blog(); |
||
91 | |||
92 | return $data; |
||
93 | } |
||
94 | |||
141 |