Completed
Push — master ( 6cab57...287bd7 )
by Sam
02:34
created

ShortcodeController::record_format()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 8.5906
cc 6
eloc 14
nc 12
nop 3
1
<?php
2
/**
3
 * This file contains only one class.
4
 *
5
 * @package Tabulate
6
 * @file
7
 */
8
9
namespace WordPress\Tabulate\Controllers;
10
11
use WordPress\Tabulate\DB\Database;
12
use WordPress\Tabulate\DB\Table;
13
use WordPress\Tabulate\DB\Grants;
14
use WordPress\Tabulate\Template;
15
16
/**
17
 * The shortcode controller.
18
 */
19
class ShortcodeController extends ControllerBase {
20
21
	/**
22
	 * The Database object.
23
	 *
24
	 * @var \WordPress\Tabulate\DB\Database
25
	 */
26
	private $db;
27
28
	/**
29
	 * Create the controller and dequeue scripts.
30
	 *
31
	 * @param \wpdb $wpdb The global wpdb object.
32
	 */
33
	public function __construct( $wpdb ) {
34
		parent::__construct( $wpdb );
35
		$this->db = new Database( $wpdb );
36
		// Dequeue scripts, but they'll be requeued in self::run() if required.
37
		add_action( 'wp_enqueue_scripts', function() {
38
			wp_dequeue_script( 'tabulate-scripts' );
39
		} );
40
	}
41
42
	/**
43
	 * Substitute the Shortcode with the relevant formatted output.
44
	 *
45
	 * @param string[] $raw_attrs The shortcode attributes.
46
	 * @return string
47
	 */
48
	public function run( $raw_attrs ) {
49
		$defaults = array(
50
			'format' => 'table',
51
			'table' => null,
52
			'ident' => null,
53
			'search' => null,
54
		);
55
		$attrs = shortcode_atts( $defaults, $raw_attrs );
56
		if ( ! isset( $attrs['table'] ) ) {
57
			$msg = "The 'table' attribute must be set. Attributes found: [";
58
			foreach ( $raw_attrs as $k => $v ) {
59
				$msg .= ' ' . htmlentities2( $k ) . ' = "' . htmlentities2( $v ) . '" ';
60
			}
61
			$msg .= "]";
62
			return $this->error( $msg );
63
		}
64
		$table = $this->db->get_table( $attrs['table'] );
65
		if ( ! $table ) {
66
			if ( ! is_user_logged_in() ) {
67
				return $this->error( "You are not logged in. " . wp_loginout( get_the_permalink(), false ) );
68
			}
69
			return $this->error();
70
		}
71
		$format_method = $attrs['format'] . '_format';
72
		if ( is_callable( array( $this, $format_method ) ) ) {
73
			wp_enqueue_script( 'tabulate-scripts' );
74
			return $this->$format_method( $table, $attrs, $_REQUEST );
75
		} else {
76
			return $this->error( "Format '{$attrs['format']}' not available." );
77
		}
78
	}
79
80
	/**
81
	 * Get a formatted error message.
82
	 *
83
	 * @param string $message The error message to display.
84
	 * @return string The error HTML.
85
	 */
86
	protected function error( $message = '' ) {
87
		$url = "http://tabulate.readthedocs.io/en/latest/shortcode.html";
88
		return "<div class='tabulate shortcode-error'>"
89
			. "<h3>Tabulate shortcode error:</h3> "
90
			. "<p class='message'>$message</p>"
91
			. "<p>For more information, please "
92
			. "<a href='$url' target='_blank' title='Opens in new tab'>read the docs</a>."
93
			. "</p></div>";
94
	}
95
96
	/**
97
	 * The 'record' format.
98
	 *
99
	 * @param Table    $table The table to display.
100
	 * @param string[] $attrs The shortcode attributes.
101
	 * @param string   $query The query parameters.
102
	 * @return string
103
	 */
104
	protected function record_format( Table $table, $attrs, $query = null ) {
105
		// Check for the ident shortcode parameter...
106
		if ( isset( $attrs['ident'] ) ) {
107
			$ident = $attrs['ident'];
108
		}
109
		// ...or the tablename=ident URL parameter.
110
		if ( isset( $query[ $table->get_name() ] ) && is_scalar( $query[ $table->get_name() ] ) ) {
111
			$ident = $query[ $table->get_name() ];
112
		}
113
		if ( ! isset( $ident ) ) {
114
			return $this->error( __( 'No record identifier could be determined.', 'tabulate' ) );
115
		}
116
117
		// Get the record.
118
		$record = $table->get_record( $ident );
119
		if ( false === $record ) {
120
			return $this->error( __( 'No record found.', 'tabulate' ) );
121
		}
122
		$template = new Template( 'record/view.html' );
123
		$template->table = $table;
124
		$template->record = $record;
125
		return $template->render();
126
	}
127
128
	/**
129
	 * The 'form' format.
130
	 *
131
	 * @param Table    $table The table to display.
132
	 * @param string[] $attrs The shortcode attributes.
133
	 * @return string
134
	 */
135
	protected function form_format( Table $table, $attrs ) {
136
		if ( ! Grants::current_user_can( Grants::CREATE, $table ) ) {
137
			return 'You do not have permission to create ' . $table->get_title() . ' records.';
138
		}
139
		$template = new Template( 'record/shortcode.html' );
140
		$template->table = $table;
141
		$template->record = $table->get_default_record();
142
		$template->return_to = ( isset( $attrs['return_to'] ) ) ? $attrs['return_to'] : get_the_permalink();
143
		return $template->render();
144
	}
145
146
	/**
147
	 * The 'count' format.
148
	 *
149
	 * @param Table $table The table to display.
150
	 * @return string
151
	 */
152
	protected function count_format( Table $table ) {
153
		$count = number_format( $table->count_records() );
154
		return '<span class="tabulate count-format">' . $count . '</span>';
155
	}
156
157
	/**
158
	 * The 'list' format.
159
	 *
160
	 * @param Table    $table The table to display.
161
	 * @param string[] $attrs The shortcode attributes.
162
	 * @return string
163
	 */
164
	protected function list_format( Table $table, $attrs ) {
165
		$titles = array();
166
		foreach ( $table->get_records() as $rec ) {
167
			$titles[] = $rec->get_title();
168
		}
169
		$glue = ( ! empty( $attrs['glue'] ) ) ? $attrs['glue'] : ', ';
170
		return '<span class="tabulate list-format">' . join( $glue, $titles ) . '</span>';
171
	}
172
173
	/**
174
	 * The 'table' format.
175
	 *
176
	 * @param Table    $table The table to display.
177
	 * @param string[] $attrs The shortcode attributes.
178
	 * @param string   $query The query parameters.
179
	 * @return string
180
	 */
181
	protected function table_format( Table $table, $attrs, $query = null ) {
182
		// Filters.
183
		// Apply filters from the URL query parameters.
184
		if ( isset( $query['table'] ) && $query['table'] === $table->get_name() ) {
185
			$query_filters = (isset( $query['filter'] )) ? $query['filter'] : array();
186
			$table->add_filters( $query_filters );
0 ignored issues
show
Documentation introduced by
$query_filters is of type string|array, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
187
		}
188
189
		// Pagination.
190
		$page_num = 1;
191
		if ( isset( $query['tabulate_p'] ) && is_numeric( $query['tabulate_p'] ) ) {
192
			$page_num = abs( $query['tabulate_p'] );
193
		}
194
		$table->set_current_page_num( $page_num );
195
		if ( isset( $query['tabulate_psize'] ) ) {
196
			$table->set_records_per_page( $query['tabulate_psize'] );
197
		}
198
199
		// Construct the HTML.
200
		$template = new Template( 'table/shortcode.html' );
201
		$template->table = $table;
202
		$template->record = $table->get_default_record();
203
		$template->records = $table->get_records();
204
205
		// Add the search form if required.
206
		$template->search = ! empty( $attrs['search'] );
207
		$template->form_action = get_the_permalink();
208
209
		// Return completed HTML output.
210
		return $template->render();
211
	}
212
}
213