| Conditions | 16 |
| Paths | 70 |
| Total Lines | 110 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 2 |
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 |
||
| 61 | function wpbo_relationships_column_content( $column, $post_id ) { |
||
| 62 | |||
| 63 | switch ( $column ) { |
||
| 64 | |||
| 65 | case 'relationship': |
||
| 66 | |||
| 67 | /** |
||
| 68 | * First we check if it is "display everywhere". |
||
| 69 | */ |
||
| 70 | if ( 'yes' == get_post_meta( $post_id, '_wpbo_display_all', true ) ) { |
||
| 71 | esc_html_e( 'Everywhere', 'betteroptin' ); |
||
| 72 | |||
| 73 | return; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Second we check if it displays everywhere for a specific post type. |
||
| 78 | */ |
||
| 79 | $post_types = get_post_types( array( 'public' => true ) ); |
||
| 80 | $except = array( 'attachment', 'wpbo-popup' ); |
||
| 81 | $pts = array(); |
||
| 82 | |||
| 83 | foreach ( $post_types as $key => $pt ) { |
||
| 84 | |||
| 85 | if ( in_array( $key, $except ) ) { |
||
| 86 | continue; |
||
| 87 | } |
||
| 88 | |||
| 89 | if ( 'all' == get_post_meta( $post_id, '_wpbo_display_' . $pt, true ) ) { |
||
| 90 | array_push( $pts, sprintf( esc_html__( 'All %s', 'betteroptin' ), ucwords( $pt ) ) ); |
||
| 91 | } |
||
| 92 | |||
| 93 | } |
||
| 94 | |||
| 95 | if ( count( $pts ) > 0 ) { |
||
| 96 | echo implode( ', ', $pts ); |
||
| 97 | |||
| 98 | return; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Third we check the individual relationships. |
||
| 103 | */ |
||
| 104 | $relationships = get_option( 'wpbo_popup_relationships', array() ); |
||
| 105 | $reverse = array(); |
||
| 106 | $list = array(); |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Switch keys and values without erasing duplicate values |
||
| 110 | * (which is why array_flip() would not work). |
||
| 111 | */ |
||
| 112 | foreach ( $relationships as $page => $popup ) { |
||
| 113 | |||
| 114 | if ( ! isset( $reverse[ $popup ] ) ) { |
||
| 115 | $reverse[ $popup ] = array(); |
||
| 116 | } |
||
| 117 | |||
| 118 | array_push( $reverse[ $popup ], $page ); |
||
| 119 | |||
| 120 | } |
||
| 121 | |||
| 122 | /* No relationships at all */ |
||
| 123 | if ( ! array_key_exists( $post_id, $reverse ) ) { |
||
| 124 | echo '-'; |
||
| 125 | |||
| 126 | return; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Print all the relationships in a table. |
||
| 131 | */ |
||
| 132 | foreach ( $reverse[ $post_id ] as $key => $page ) { |
||
| 133 | |||
| 134 | $page = get_post( $page ); |
||
| 135 | $link = add_query_arg( array( 'post' => $page->ID, 'action' => 'edit' ), admin_url( 'post.php' ) ); |
||
| 136 | $title = $page->post_title; |
||
| 137 | |||
| 138 | array_push( $list, "<a href='$link' class='wpbo-tag'>$title</a>" ); |
||
| 139 | |||
| 140 | } |
||
| 141 | |||
| 142 | if ( count( $list ) > 0 ) { |
||
| 143 | echo implode( ' ', $list ); |
||
| 144 | } |
||
| 145 | |||
| 146 | break; |
||
| 147 | |||
| 148 | case 'template': |
||
| 149 | |||
| 150 | $template = get_post_meta( $post_id, 'wpbo_template', true ); |
||
| 151 | |||
| 152 | if ( ! empty( $template ) ) { |
||
| 153 | printf( '<code>%s</code>', $template ); |
||
| 154 | } |
||
| 155 | |||
| 156 | break; |
||
| 157 | |||
| 158 | case 'returl': |
||
| 159 | |||
| 160 | $returl = wpbo_get_return_url( $post_id ); |
||
| 161 | |||
| 162 | if ( ! empty( $returl ) ) { |
||
| 163 | printf( '<a href="%1$s" target="_blank">%1$s</a>', esc_url( $returl ) ); |
||
| 164 | } |
||
| 165 | |||
| 166 | break; |
||
| 167 | |||
| 168 | } |
||
| 169 | |||
| 170 | } |