PluginProbe
Posts Table with Search & Sort / 1.3.4
Posts Table with Search & Sort v1.3.4
1.4.13 trunk 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2 1.3 1.3.1 1.3.1-v2 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 All 40 releases
posts-data-table / src / Admin / Settings_Page.php

Settings_Page.php in Posts Table with Search & Sort 1.3.4, at src/Admin/Settings_Page.php

399 lines 18.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Barn2\Plugin\Posts_Table_Search_Sort\Admin;
3
4 use Barn2\PTS_Lib\Util,
5 Barn2\PTS_Lib\Registerable,
6 Barn2\PTS_Lib\Plugin\Plugin,
7 Barn2\PTS_Lib\Admin\Settings_API_Helper,
8 Barn2\PTS_Lib\Admin\Plugin_Promo,
9 Barn2\Plugin\Posts_Table_Search_Sort\Simple_Posts_Table,
10 Barn2\Plugin\Posts_Table_Search_Sort\Settings;
11
12 /**
13 * This class handles our plugin settings page in the admin.
14 *
15 * @package Barn2\posts-table-search-sort
16 * @author Barn2 Plugins <support@barn2.co.uk>
17 * @license GPL-3.0
18 * @copyright Barn2 Media Ltd
19 */
20 class Settings_Page implements Registerable {
21
22 const MENU_SLUG = 'posts_table_search_sort';
23 const OPTION_GROUP = 'posts_table_search_sort_main';
24
25 /**
26 * @var Plugin $plugin The plugin that we're building settings for.
27 */
28 private $plugin;
29
30 /**
31 * @var array The list of readonly settings.
32 */
33 private static $readonly_settings = [
34 'post_type',
35 'image_size',
36 'lightbox',
37 'shortcodes',
38 'excerpt_length',
39 'links',
40 'lazy_load',
41 'post_limit',
42 'cache',
43 'filters',
44 'page_length',
45 'search_box',
46 'totals',
47 'pagination',
48 'paging_type',
49 'reset_button',
50 ];
51
52 public function __construct( Plugin $plugin ) {
53 $this->plugin = $plugin;
54 }
55
56 public function register() {
57 add_action( 'admin_menu', [ $this, 'add_settings_page' ] );
58 add_action( 'admin_init', [ $this, 'register_settings' ] );
59
60 $plugin_promo = new Plugin_Promo( $this->plugin );
61 $plugin_promo->register();
62 }
63
64 public function add_settings_page() {
65 add_options_page(
66 __( 'Posts Table With Search &amp; Sort', 'posts-data-table' ),
67 __( 'Posts Table With Search &amp; Sort', 'posts-data-table' ),
68 'manage_options',
69 self::MENU_SLUG,
70 array( $this, 'render_settings_page' )
71 );
72 }
73
74 public function render_settings_page() {
75 ?>
76 <div class="wrap barn2-settings barn2-settings-flex">
77 <?php do_action( 'barn2_before_plugin_settings', $this->plugin->get_id() ); ?>
78 <div class="barn2-settings-inner">
79 <h1><?php _e( 'Posts Table with Search and Sort', 'posts-data-table' ); ?></h1>
80 <form action="options.php" method="post">
81 <?php
82 // Output the hidden form fields (_wpnonce, etc)
83 settings_fields( self::OPTION_GROUP );
84
85 // Output the sections and their settings
86 do_settings_sections( self::MENU_SLUG );
87 ?>
88 <p class="submit">
89 <input name="Submit" type="submit" name="submit" class="button button-primary" value="<?php esc_attr_e( 'Save Changes', 'posts-data-table' ); ?>" />
90 </p>
91 </form>
92 </div>
93 <?php do_action( 'barn2_after_plugin_settings', $this->plugin->get_id() ); ?>
94 </div>
95 <?php
96 }
97
98 public function register_settings() {
99 // Register the settings
100 register_setting( self::OPTION_GROUP, Settings::TABLE_ARGS_SETTING, [
101 'type' => 'string', // array type not supported, so just use string
102 'description' => 'Posts Table with Search and Sort - table defaults',
103 'sanitize_callback' => '\Barn2\Plugin\Posts_Table_Search_Sort\Settings::sanitize_table_args'
104 ] );
105
106 $default_args = Simple_Posts_Table::get_defaults();
107 $args_setting = Settings::TABLE_ARGS_SETTING;
108
109 // Selecting posts
110 Settings_API_Helper::add_settings_section(
111 'ptss_post_selection', self::MENU_SLUG, __( 'Posts selection', 'posts-data-table' ), [ $this, 'section_description_selecting_posts' ],
112 $this->mark_readonly_settings( [
113 [
114 'id' => $args_setting . '[post_type]',
115 'title' => __( 'Post type', 'posts-data-table' ),
116 'type' => 'select',
117 'desc' => __( 'The default post type for your tables.', 'posts-data-table' ),
118 'options' => [ 'post' ],
119 'default' => 'post'
120 ],
121 ] )
122 );
123
124 // Table content
125 Settings_API_Helper::add_settings_section(
126 'ptss_content', self::MENU_SLUG, __( 'Table content', 'posts-data-table' ), [ $this, 'section_description_table_content' ],
127 $this->mark_readonly_settings( [
128 [
129 'id' => $args_setting . '[columns]',
130 'title' => __( 'Columns', 'posts-data-table' ),
131 'type' => 'text',
132 'desc' => __( 'The columns displayed in your table. Enter a comma-separated list. ', 'posts-data-table' ),
133 'default' => $default_args['columns'],
134 ],
135 [
136 'id' => $args_setting . '[image_size]',
137 'title' => __( 'Image size', 'posts-data-table' ),
138 'type' => 'text',
139 'desc' => __( "W x H in pixels. Sets the image size when using the 'image' column. ", 'posts-data-table' ) . Util::barn2_link( 'kb/ptp-column-widths/#image-size' ),
140 'default' => 'thumbnail',
141 ],
142 [
143 'id' => $args_setting . '[lightbox]',
144 'title' => __( 'Image lightbox', 'posts-data-table' ),
145 'type' => 'checkbox',
146 'label' => __( 'Display featured images in a lightbox when opened', 'posts-data-table' ),
147 'default' => true,
148 ],
149 [
150 'title' => __( 'Shortcodes', 'posts-data-table' ),
151 'type' => 'checkbox',
152 'id' => $args_setting . '[shortcodes]',
153 'label' => __( 'Display shortcodes, HTML and other formatting inside the table content', 'posts-data-table' ),
154 'default' => true
155 ],
156 [
157 'id' => $args_setting . '[content_length]',
158 'title' => __( 'Content length', 'posts-data-table' ),
159 'type' => 'number',
160 'class' => 'small-text',
161 'suffix' => __( 'words', 'posts-data-table' ),
162 'desc' => __( 'Enter -1 to show the full post content.', 'posts-data-table' ),
163 'default' => $default_args['content_length'],
164 'custom_attributes' => [
165 'min' => -1
166 ]
167 ],
168 [
169 'id' => $args_setting . '[excerpt_length]',
170 'title' => __( 'Excerpt length', 'posts-data-table' ),
171 'type' => 'number',
172 'class' => 'small-text',
173 'suffix' => __( 'words', 'posts-data-table' ),
174 'desc' => __( 'Enter -1 to show the full excerpt.', 'posts-data-table' ),
175 'default' => 15,
176 'custom_attributes' => [
177 'min' => -1
178 ]
179 ],
180 [
181 'id' => $args_setting . '[links]',
182 'title' => __( 'Links', 'posts-data-table' ),
183 'type' => 'text',
184 'desc' => __( 'Display links to the single post page, category, tag, or term archive. ', 'posts-data-table' ) . Util::barn2_link( 'kb/links-posts-table/' ),
185 'default' => 'all',
186 ]
187 ] )
188 );
189
190 // Loading posts
191 Settings_API_Helper::add_settings_section(
192 'ptss_loading', self::MENU_SLUG, __( 'Table loading', 'posts-data-table' ), '__return_false',
193 $this->mark_readonly_settings( [
194 [
195 'title' => __( 'Lazy load', 'posts-data-table' ),
196 'type' => 'checkbox',
197 'id' => $args_setting . '[lazy_load]',
198 'label' => __( 'Load the posts one page at a time', 'posts-data-table' ),
199 'desc' => sprintf( __( 'Enable this if you have many posts or experience slow page load times. ', 'posts-data-table' ) ) . Util::barn2_link( 'kb/posts-table-lazy-load/' ),
200 'default' => false,
201 ],
202 [
203 'title' => __( 'Post limit', 'posts-data-table' ),
204 'type' => 'number',
205 'id' => $args_setting . '[post_limit]',
206 'desc' => __( 'The maximum (total) number of posts to display in one table.', 'posts-data-table' ),
207 'default' => 500,
208 'class' => 'small-text',
209 'custom_attributes' => [
210 'min' => -1
211 ]
212 ],
213 [
214 'title' => __( 'Posts per page', 'posts-data-table' ),
215 'type' => 'number',
216 'id' => $args_setting . '[rows_per_page]',
217 'desc' => __( 'The number of posts per page of results. Enter -1 to display all posts on one page.', 'posts-data-table' ),
218 'default' => $default_args['rows_per_page'],
219 'custom_attributes' => [
220 'min' => -1
221 ]
222 ],
223 [
224 'title' => __( 'Caching', 'posts-data-table' ),
225 'type' => 'checkbox',
226 'id' => $args_setting . '[cache]',
227 'label' => __( 'Cache table contents to improve load time', 'posts-data-table' ),
228 'default' => false
229 ]
230 ] )
231 );
232
233 // Sorting
234 $sort_columns = wp_list_pluck( Simple_Posts_Table::get_column_defaults(), 'heading' );
235
236 Settings_API_Helper::add_settings_section(
237 'ptss_sorting', self::MENU_SLUG, __( 'Sorting', 'posts-data-table' ), '__return_false',
238 $this->mark_readonly_settings( [
239 [
240 'title' => __( 'Sort by', 'posts-data-table' ),
241 'type' => 'select',
242 'id' => $args_setting . '[sort_by]',
243 'options' => $sort_columns,
244 'desc' => __( 'The initial sort order applied to the table. ', 'posts-data-table' ),
245 'default' => $default_args['sort_by']
246 ],
247 [
248 'title' => __( 'Sort direction', 'posts-data-table' ),
249 'type' => 'select',
250 'id' => $args_setting . '[sort_order]',
251 'options' => [
252 '' => __( 'Automatic', 'posts-data-table' ),
253 'asc' => __( 'Ascending (A to Z, 1 to 99)', 'posts-data-table' ),
254 'desc' => __( 'Descending (Z to A, 99 to 1)', 'posts-data-table' )
255 ],
256 'default' => $default_args['sort_order']
257 ]
258 ] )
259 );
260
261 // Table controls
262 Settings_API_Helper::add_settings_section(
263 'ptss_controls', self::MENU_SLUG, __( 'Table controls', 'posts-data-table' ), '__return_false',
264 $this->mark_readonly_settings( [
265 [
266 'title' => __( 'Search filters', 'posts-data-table' ),
267 'type' => 'select',
268 'id' => $args_setting . '[filters]',
269 'options' => [
270 'false' => __( 'Disabled', 'posts-data-table' ),
271 'true' => __( 'Show based on columns in table', 'posts-data-table' ),
272 'custom' => __( 'Custom', 'posts-data-table' )
273 ],
274 'desc' => __( 'Dropdown lists to filter the table by category, tag, attribute, or custom taxonomy. ', 'posts-data-table' ) . Util::barn2_link( 'kb/posts-table-filters/' ),
275 'default' => true,
276 ],
277 [
278 'title' => __( 'Page length', 'posts-data-table' ),
279 'type' => 'select',
280 'id' => $args_setting . '[page_length]',
281 'options' => [
282 'top' => __( 'Above table', 'posts-data-table' ),
283 'bottom' => __( 'Below table', 'posts-data-table' ),
284 'both' => __( 'Above and below table', 'posts-data-table' ),
285 'false' => __( 'Hidden', 'posts-data-table' )
286 ],
287 'desc' => __( "The position of the 'Show [x] entries' dropdown list.", 'posts-data-table' ),
288 'default' => 'top'
289 ],
290 [
291 'title' => __( 'Search box', 'posts-data-table' ),
292 'type' => 'select',
293 'id' => $args_setting . '[search_box]',
294 'options' => [
295 'top' => __( 'Above table', 'posts-data-table' ),
296 'bottom' => __( 'Below table', 'posts-data-table' ),
297 'both' => __( 'Above and below table', 'posts-data-table' ),
298 'false' => __( 'Hidden', 'posts-data-table' )
299 ],
300 'default' => 'top'
301 ],
302 [
303 'title' => __( 'Totals', 'posts-data-table' ),
304 'type' => 'select',
305 'id' => $args_setting . '[totals]',
306 'options' => [
307 'top' => __( 'Above table', 'posts-data-table' ),
308 'bottom' => __( 'Below table', 'posts-data-table' ),
309 'both' => __( 'Above and below table', 'posts-data-table' ),
310 'false' => __( 'Hidden', 'posts-data-table' )
311 ],
312 'desc' => __( "The position of the post totals, e.g. 'Showing 1 to 5 of 10 entries'.", 'posts-data-table' ),
313 'default' => 'bottom'
314 ],
315 [
316 'title' => __( 'Pagination buttons', 'posts-data-table' ),
317 'type' => 'select',
318 'id' => $args_setting . '[pagination]',
319 'options' => [
320 'top' => __( 'Above table', 'posts-data-table' ),
321 'bottom' => __( 'Below table', 'posts-data-table' ),
322 'both' => __( 'Above and below table', 'posts-data-table' ),
323 'false' => __( 'Hidden', 'posts-data-table' )
324 ],
325 'desc' => __( 'The position of the paging buttons which scroll between results.', 'posts-data-table' ),
326 'default' => 'bottom'
327 ],
328 [
329 'title' => __( 'Pagination type', 'posts-data-table' ),
330 'type' => 'select',
331 'id' => $args_setting . '[paging_type]',
332 'options' => [
333 'numbers' => __( 'Numbers only', 'posts-data-table' ),
334 'simple' => __( 'Prev|Next', 'posts-data-table' ),
335 'simple_numbers' => __( 'Prev|Next + Numbers', 'posts-data-table' ),
336 'full' => __( 'Prev|Next|First|Last', 'posts-data-table' ),
337 'full_numbers' => __( 'Prev|Next|First|Last + Numbers', 'posts-data-table' )
338 ],
339 'default' => 'simple_numbers'
340 ],
341 [
342 'title' => __( 'Reset button', 'posts-data-table' ),
343 'type' => 'checkbox',
344 'id' => $args_setting . '[reset_button]',
345 'label' => __( 'Show a reset button above the table', 'posts-data-table' ),
346 'default' => false
347 ]
348 ] )
349 );
350 }
351
352 public function mark_readonly_settings( $settings ) {
353 foreach ( $settings as &$setting ) {
354 $subkey = preg_filter( '/^[\w\[\]]+\[(\w+)\]$/', '$1', $setting['id'] );
355
356 if ( $subkey && false !== array_search( $subkey, self::$readonly_settings ) ) {
357 $setting['field_class'] = 'readonly';
358 $setting['custom_attributes'] = [
359 'disabled' => 'disabled'
360 ];
361
362 $setting['title'] = $setting['title'] .
363 sprintf( '<span class="pro-version">%s</span>', Util::barn2_link( 'wordpress-plugins/posts-table-pro/', __( 'Pro version only', 'posts-data-table' ), true ) );
364 }
365 }
366
367 return $settings;
368 }
369
370 public function section_description_selecting_posts() {
371 ?>
372 <p>
373 <?php
374 printf(
375 __( 'Post tables list all published posts by default. To restrict posts by category, tag, author, etc. add the %1$scorresponding option%2$s to the [posts_table] shortcode.', 'posts-data-table' ),
376 Util::format_link_open( Util::barn2_url( 'kb-categories/posts-table-search-sort-free-kb/' ), true ),
377 '</a>'
378 );
379 ?>
380 </p>
381 <?php
382 }
383
384 public function section_description_table_content() {
385 ?>
386 <p>
387 <?php
388 printf(
389 __( 'You can override these settings for individual tables by adding options to the [posts_table] shortcode. See the %1$sKnowledge Base%2$s for details.', 'posts-data-table' ),
390 Util::format_link_open( Util::barn2_url( 'kb-categories/posts-table-search-sort-free-kb/' ), true ),
391 '</a>'
392 );
393 ?>
394 </p>
395 <?php
396 }
397
398 }
399