GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

PPP_Schedule_Table::prepare_items()   F
last analyzed

Complexity

Conditions 12
Paths 578

Size

Total Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 578
nop 0
dl 0
loc 78
rs 3.1888
c 0
b 0
f 0

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
// Exit if accessed directly
4
if ( ! defined( 'ABSPATH' ) ) {
5
	exit;
6
}
7
8
if ( ! class_exists( 'WP_List_Table' ) ) {
9
	require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
10
}
11
12
/**
13
 * Creates the Schedule List table for Post Promoter Pro
14
 */
15
class PPP_Schedule_Table extends WP_List_Table {
16
17
	/**
18
	 * Generate the Class from it's parent
19
	 */
20
	public function __construct() {
21
		global $status, $page;
22
23
		parent::__construct( array(
24
				'singular'  => __( 'Scheduled Share', 'ppp-txt' ),
25
				'plural'    => __( 'Scheduled Shares', 'ppp-txt' ),
26
				'ajax'      => false,
27
			) );
28
	}
29
30
	/**
31
	 * What to show if no items are found
32
	 * @return void
33
	 */
34
	public function no_items() {
35
		printf( __( 'No shares scheduled. Go <a href="%s">write something</a>!', 'ppp-txt' ), admin_url( 'post-new.php' ) );
36
	}
37
38
	/**
39
	 * The Default columns
40
	 * @param  array $item        The Item being displayed
41
	 * @param  string $column_name The column we're currently in
42
	 * @return string              The Content to display
43
	 */
44
	public function column_default( $item, $column_name ) {
45
		switch ( $column_name ) {
46
		case 'date':
47
		case 'post_title':
48
			return $item[ $column_name ];
49
		case 'content':
50
			$content = $item[ $column_name ];
51
			return $content;
52
		case 'image':
53
			if ( ! empty( $item['image_url'] ) ) {
54
				$content = '<img src="' . $item['image_url'] . '" />';
55
			} else {
56
				$content = __( 'None', 'ppp-txt' );
57
			}
58
			return $content;
59
		default:
60
			return;
61
		}
62
	}
63
64
	/**
65
	 * The columns for our list view
66
	 * @return array Columns shown on the Schedule page
67
	 */
68
	public function get_columns() {
69
		$columns = array(
70
			'post_title'     => __( 'Post Title', 'ppp-txt' ),
71
			'date'           => __( 'Scheduled Date', 'ppp-txt' ),
72
			'content'        => __( 'Share Message', 'ppp-txt' ),
73
			'image'          => __( 'Image', 'ppp-txt' )
74
		);
75
76
		return $columns;
77
	}
78
79
	/**
80
	 * Specifies how to display the post_id column on the schedule page
81
	 * @param  array $item The Item being displayed
82
	 * @return string       The HTML to display for this column.
83
	 */
84
	public function column_post_title( $item ) {
85
		$item_name = $item['name'] . '_' . $item['service'];
86
		$actions   = array(
87
			'edit'          => sprintf( __( '<a href="%s">Edit</a>', 'ppp-txt' ), admin_url( 'post.php?post=' . $item['post_id'] . '&action=edit#ppp_schedule_metabox' ) ),
88
			'delete'        => sprintf( __( '<a href="%s">Delete</a>', 'ppp-txt' ), admin_url( 'admin.php?page=ppp-schedule-info&action=delete_item&post_id=' . $item['post_id'] . '&name=' . $item_name . '&index=' . $item['index'] ) ),
89
			'share'         => sprintf( __( '<a href="%s">Share Now</a>', 'ppp-txt' ), admin_url( 'admin.php?page=ppp-schedule-info&action=share_now&post_id=' . $item['post_id'] . '&name=' . $item_name ) ),
90
			'share_delete'  => sprintf( __( '<a href="%s">Share Now & Delete</a>', 'ppp-txt' ), admin_url( 'admin.php?page=ppp-schedule-info&action=share_now&post_id=' . $item['post_id'] . '&name=' . $item_name . '&index=' . $item['index'] . '&delete_too=true' ) ),
91
		);
92
93
		return sprintf( '<span class="dashicons icon-ppp-' . $item['service'] . '"></span>&nbsp;%1$s %2$s', $item['post_title'], $this->row_actions( $actions ) );
94
	}
95
96
	/**
97
	 * Specifies how to display the date columon on the schedule page
98
	 * @param  array $item The Item being displayed
99
	 * @return string       The HTML to display the date
100
	 */
101
	public function column_date( $item ) {
102
		$date = date_i18n( get_option('date_format') . ' @ ' . get_option('time_format'), $item['date'] );
103
		if ( $item['conflict'] ) {
104
			$date .= '<br /><small style="color: red">' . __( 'Warning: Multiple items scheduled at this time.', 'ppp-txt' ) . '</small>';
105
		}
106
107
		return $date;
108
	}
109
110
	/**
111
	 * Prepare the data for the WP List Table
112
	 * @return void
113
	 */
114
	public function prepare_items() {
115
		$columns               = $this->get_columns();
116
		$hidden                = array();
117
		$data                  = array();
118
		$sortable              = false;
119
		$this->_column_headers = array( $columns, $hidden, $sortable );
120
121
		$per_page = 25;
122
123
		$crons = ppp_get_shceduled_crons();
124
125
		$cron_tally = array();
126
		foreach ( $crons as $key => $cron ) {
127
			$ppp_data = $cron;
128
			$timestamp = $ppp_data['timestamp'];
129
130
			$cron_tally[$timestamp] = isset( $cron_tally[$timestamp] ) ? $cron_tally[$timestamp] + 1 : 1;
131
132
			$name_parts = explode( '_', $ppp_data['args'][1] );
133
			$post_id    = $ppp_data['args'][0];
134
			$index      = $name_parts[1];
135
			$service    = isset( $name_parts[3] ) ? $name_parts[3] : 'tw';
136
			$builder    = 'ppp_' . $service . '_build_share_message';
137
			$post_meta  = apply_filters( 'ppp_get_scheduled_items_' . $service, array(), $post_id );
138
			$image_url  = '';
139
140
			if ( ! empty( $post_meta[$index]['attachment_id'] ) ) {
141
				$image_url = ppp_post_has_media( $post_id, $service, true, $post_meta[ $index ]['attachment_id'] );
142
			} elseif ( ! empty( $post_meta[ $index ]['image'] ) ) {
143
				$image_url = $post_meta[ $index ]['image'];
144
			}
145
146
			$conflict   = $cron_tally[ $timestamp ] > 1 ? true : false;
147
148
			$data[ $key ] = array(
149
				'post_id'      => $post_id,
150
				'post_title'   => get_the_title( $post_id ),
151
				'service'      => $service,
152
				'index'        => $index,
153
				'date'         => $timestamp + ( get_option( 'gmt_offset' ) * 3600 ),
154
				'content'      => function_exists( $builder ) ? $builder( $post_id, $ppp_data['args'][1], false ) : '',
155
				'name'         => 'sharedate_' . $index . '_' . $post_id,
156
				'conflict'     => $conflict,
157
			);
158
159
			if ( ! empty( $image_url ) ) {
160
				$data[ $key ]['image_url'] = $image_url;
161
			}
162
163
			$description_function = 'ppp_' . $service . '_get_share_description';
164
			if ( function_exists( $description_function ) ) {
165
				$description = $description_function( $post_id, $index );
166
				if ( ! empty( $description ) ) {
167
					$data[ $key ]['content'] .= '<br />' . $description;
168
				}
169
			}
170
171
		}
172
173
		$total_items = count( $data );
174
175
		$offset = isset( $_GET['paged'] ) ? $_GET['paged'] : 1;
176
177
		$data = array_slice( $data, ( $offset - 1 ) * $per_page, $per_page, true );
178
		$this->items = $data;
179
180
181
		$this->set_pagination_args( array(
182
				'total_items' => $total_items,
183
				'per_page'    => $per_page,
184
				'total_pages' => ceil( $total_items / $per_page ),
185
			) );
186
187
		$this->items = $data;
188
189
		return $this->items;
190
191
	}
192
}
193
194
$ppp_schedule_table = new PPP_Schedule_Table();
195