Completed
Push — master ( 42ba23...5fbbab )
by Julien
03:55
created

functions-list-table.php ➔ wpbo_relationships_column_content()   D

Complexity

Conditions 16
Paths 70

Size

Total Lines 110
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
cc 16
eloc 45
c 4
b 0
f 2
nc 70
nop 2
dl 0
loc 110
rs 4.8736

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * BetterOptin Popup
4
 *
5
 * @package   BetterOptin/Popup
6
 * @author    ThemeAvenue <[email protected]>
7
 * @license   GPL-2.0+
8
 * @link      http://themeavenue.net
9
 * @copyright 2015 ThemeAvenue
10
 */
11
12
// If this file is called directly, abort.
13
if ( ! defined( 'WPINC' ) ) {
14
	die;
15
}
16
17
add_filter( 'manage_wpbo-popup_posts_columns', 'wpbo_relationships_column', 10, 2 );
18
/**
19
 * Add relationship column.
20
 *
21
 * Add a relationships column in the popup
22
 * list screen.
23
 *
24
 * @param  array $columns Currently available columns
25
 *
26
 * @return array          Columns containing the relationships
27
 */
28
function wpbo_relationships_column( $columns ) {
29
30
	$new = array();
31
32
	foreach ( $columns as $key => $label ) {
33
34
		$new[ $key ] = $label;
35
36
		if ( 'title' == $key ) {
37
			$new['relationship'] = esc_html__( 'Appears On', 'betteroptin' );
38
			$new['template']     = esc_html__( 'Template', 'betteroptin' );
39
			$new['returl']       = esc_html__( 'Return URL', 'betteroptin' );
40
		}
41
42
	}
43
44
	return $new;
45
46
}
47
48
49
add_action( 'manage_wpbo-popup_posts_custom_column', 'wpbo_relationships_column_content', 10, 2 );
50
/**
51
 * Relationship content.
52
 *
53
 * Get the relationships for all popups and display it
54
 * in the relationships custom column.
55
 *
56
 * @since  1.0.0
57
 *
58
 * @param  array   $column  Current column ID
59
 * @param  integer $post_id Current post ID
60
 */
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
}