-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
161 lines (147 loc) · 4.49 KB
/
plugin.php
File metadata and controls
161 lines (147 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php defined( 'App' ) or die( 'BoidCMS' );
/**
*
* Order By – Effortlessly Organize Your Post Order
*
* @package Plugin_Order_By
* @author Shuaib Yusuf Shuaib
* @version 0.1.0
*/
if ( 'order-by' !== basename( __DIR__ ) ) return;
global $App;
$App->set_action( 'install', 'order_by_install' );
$App->set_action( 'uninstall', 'order_by_uninstall' );
$App->set_action( 'recent_posts', 'order_by_apply', 11 );
$App->set_action( 'admin', 'order_by_admin' );
/**
* Initialize Order By, first time install
* @param string $plugin
* @return void
*/
function order_by_install( string $plugin ): void {
global $App;
if ( 'order-by' === $plugin ) {
$config = array();
$config[ 'sort' ] = 'date';
$config[ 'order' ] = 'desc';
$App->set( $config, 'order-by' );
}
}
/**
* Free database space, while uninstalled
* @param string $plugin
* @return void
*/
function order_by_uninstall( string $plugin ): void {
global $App;
if ( 'order-by' === $plugin ) {
$App->unset( 'order-by' );
}
}
/**
* Apply order
* @param array $articles
* @return array
*/
function order_by_apply( array $articles ): array {
$callback = order_by_callback();
uasort( $articles, $callback );
return $articles;
}
/**
* Admin settings
* @return void
*/
function order_by_admin(): void {
global $App, $layout, $page;
switch ( $page ) {
case 'order-by':
$config = $App->get( 'order-by' );
$layout[ 'title' ] = 'Order By';
$layout[ 'content' ] = '
<form action="' . $App->admin_url( '?page=order-by', true ) . '" method="post">
<label for="sort" class="ss-label">Field to Sort by</label>
<select id="sort" name="sort" class="ss-select ss-mobile ss-w-6 ss-mx-auto">
<option value="date"' . ( 'date' === $config[ 'sort' ] ? ' selected' : '' ) . '>Date</option>
<option value="title"' . ( 'title' === $config[ 'sort' ] ? ' selected' : '' ) . '>Title</option>
</select>
<label for="order" class="ss-label">Sorting Order</label>
<select id="order" name="order" class="ss-select ss-mobile ss-w-6 ss-mx-auto">
<option value="asc"' . ( 'asc' === $config[ 'order' ] ? ' selected' : '' ) . '>Ascending (ASC)</option>
<option value="desc"' . ( 'desc' === $config[ 'order' ] ? ' selected' : '' ) . '>Descending (DESC)</option>
</select>
<input type="hidden" name="token" value="' . $App->token() . '">
<input type="submit" name="save" value="Save" class="ss-btn ss-mobile ss-w-5">
</form>';
if ( isset( $_POST[ 'save' ] ) ) {
$App->auth();
$config[ 'sort' ] = $App->esc( $_POST[ 'sort' ] ?? $config[ 'sort' ] );
$config[ 'order' ] = $App->esc( $_POST[ 'order' ] ?? $config[ 'order' ] );
if ( $App->set( $config, 'order-by' ) ) {
$App->alert( 'Settings saved successfully.', 'success' );
$App->go( $App->admin_url( '?page=order-by' ) );
}
$App->alert( 'Failed to save settings, please try again.', 'error' );
$App->go( $App->admin_url( '?page=order-by' ) );
}
require_once $App->root( 'app/layout.php' );
break;
}
}
/**
* Order by date in ASC
* @param array $first
* @param array $second
* @return int
*/
function order_by_date_asc( array $first, array $second ): int {
return ( strtotime( $first[ 'date' ] ) <=> strtotime( $second[ 'date' ] ) );
}
/**
* Order by date in DESC
* @param array $first
* @param array $second
* @return int
*/
function order_by_date_desc( array $first, array $second ): int {
return ( strtotime( $second[ 'date' ] ) <=> strtotime( $first[ 'date' ] ) );
}
/**
* Order by title in ASC
* @param array $first
* @param array $second
* @return int
*/
function order_by_title_asc( array $first, array $second ): int {
return strcmp( $first[ 'title' ], $second[ 'title' ] );
}
/**
* Order by title in DESC
* @param array $first
* @param array $second
* @return int
*/
function order_by_title_desc( array $first, array $second ): int {
return strcmp( $second[ 'title' ], $first[ 'title' ] );
}
/**
* Order callback
* @return callable
*/
function order_by_callback(): callable {
global $App;
$config = $App->get( 'order-by' );
if ( 'date' === $config[ 'sort' ] ) {
$callback = 'order_by_date_asc';
if ( 'asc' !== $config[ 'order' ] ) {
$callback = 'order_by_date_desc';
}
} else {
$callback = 'order_by_title_asc';
if ( 'asc' !== $config[ 'order' ] ) {
$callback = 'order_by_title_desc';
}
}
return $callback;
}
?>