PluginProbe
Posts Table with Search & Sort / 1.3.8
Posts Table with Search & Sort v1.3.8
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.8, at src/Admin/Settings_Page.php

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