| 1 |
<?php |
| 2 |
|
| 3 |
namespace Barn2\Plugin\Posts_Table_Search_Sort\Admin\Wizard\Steps; |
| 4 |
|
| 5 |
use Barn2\Plugin\Posts_Table_Search_Sort\Dependencies\Setup_Wizard\Api; |
| 6 |
use Barn2\Plugin\Posts_Table_Search_Sort\Dependencies\Setup_Wizard\Step; |
| 7 |
use Barn2\Plugin\Posts_Table_Search_Sort\Settings; |
| 8 |
use Barn2\Plugin\Posts_Table_Search_Sort\Simple_Posts_Table; |
| 9 |
|
| 10 |
/** |
| 11 |
* Search settings step. |
| 12 |
* |
| 13 |
* @package Barn2\posts-data-table |
| 14 |
* @author Barn2 Plugins <info@barn2.com> |
| 15 |
* @license GPL-3.0 |
| 16 |
* @copyright Barn2 Media Ltd |
| 17 |
*/ |
| 18 |
class Search extends Step { |
| 19 |
/** |
| 20 |
* {@inheritdoc} |
| 21 |
*/ |
| 22 |
public function init() { |
| 23 |
$this->set_id( 'search' ); |
| 24 |
$this->set_name( esc_html__( 'Search and Sort', 'posts-data-table' ) ); |
| 25 |
$this->set_description( esc_html__( 'Next, make it quick and easy for people to find your posts.', 'posts-data-table' ) ); |
| 26 |
$this->set_title( esc_html__( 'Search and Sort', 'posts-data-table' ) ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* {@inheritdoc} |
| 31 |
*/ |
| 32 |
public function setup_fields() { |
| 33 |
|
| 34 |
$values = Settings::get_table_args(); |
| 35 |
|
| 36 |
$fields = [ |
| 37 |
'sort_by' => [ |
| 38 |
'label' => __( 'Sort by', 'posts-data-table' ), |
| 39 |
'description' => __( 'The initial sort order applied to the table.', 'posts-data-table' ), |
| 40 |
'type' => 'select', |
| 41 |
'options' => $this->get_sort_by(), |
| 42 |
'value' => $values['sort_by'] ?? 'date', |
| 43 |
], |
| 44 |
'sort_order' => [ |
| 45 |
'label' => __( 'Sort direction', 'posts-data-table' ), |
| 46 |
'type' => 'select', |
| 47 |
'options' => [ |
| 48 |
[ |
| 49 |
'value' => '', |
| 50 |
'label' => __( 'Automatic', 'posts-data-table' ), |
| 51 |
], |
| 52 |
[ |
| 53 |
'value' => 'asc', |
| 54 |
'label' => __( 'Ascending (A to Z, 1 to 99)', 'posts-data-table' ), |
| 55 |
], |
| 56 |
[ |
| 57 |
'value' => 'desc', |
| 58 |
'label' => __( 'Descending (Z to A, 99 to 1)', 'posts-data-table' ), |
| 59 |
], |
| 60 |
], |
| 61 |
'value' => $values['sort_order'] ?? '', |
| 62 |
], |
| 63 |
'search' => [ |
| 64 |
'label' => __( 'Search filters', 'posts-data-table' ), |
| 65 |
'type' => 'select', |
| 66 |
'description' => __( 'Add filter dropdowns above the table to quickly filter the posts by category, tag and more.', 'posts-data-table' ), |
| 67 |
'options' => [ |
| 68 |
[ |
| 69 |
'value' => '', |
| 70 |
'label' => __( 'Disabled', 'posts-data-table' ), |
| 71 |
], |
| 72 |
], |
| 73 |
'value' => '', |
| 74 |
'premium' => true, |
| 75 |
], |
| 76 |
]; |
| 77 |
|
| 78 |
return $fields; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Get formatted list of sort options. |
| 83 |
* |
| 84 |
* @return array |
| 85 |
*/ |
| 86 |
private function get_sort_by() { |
| 87 |
|
| 88 |
$available_columns = wp_list_pluck( Simple_Posts_Table::get_column_defaults(), 'heading' ); |
| 89 |
$sort_by = []; |
| 90 |
|
| 91 |
foreach ( $available_columns as $key => $heading ) { |
| 92 |
$sort_by[] = [ |
| 93 |
'value' => $key, |
| 94 |
'label' => $heading, |
| 95 |
]; |
| 96 |
} |
| 97 |
|
| 98 |
return $sort_by; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* {@inheritdoc} |
| 103 |
*/ |
| 104 |
public function submit( array $values ) { |
| 105 |
|
| 106 |
$sort_by = $values['sort_by'] ?? 'id'; |
| 107 |
$sort_order = $values['sort_order'] ?? ''; |
| 108 |
$options = Settings::get_table_args(); |
| 109 |
|
| 110 |
$options['sort_by'] = $sort_by; |
| 111 |
$options['sort_order'] = $sort_order; |
| 112 |
|
| 113 |
$options = Settings::sanitize_table_args( $options ); |
| 114 |
|
| 115 |
update_option( Settings::TABLE_ARGS_SETTING, $options ); |
| 116 |
|
| 117 |
return Api::send_success_response(); |
| 118 |
} |
| 119 |
} |
| 120 |
|