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

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