-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathascendingposts.php
176 lines (154 loc) · 4.97 KB
/
ascendingposts.php
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/*
Plugin Name: Ascending Posts Plugin
Plugin URI: http://www.flyplugins.com
Description: This plugin adds a feature to a post category to allow the posts in that particular category to be displayed in ascending or descending order by date.
Version: 1.6
Author: Fly Plugins
Author URI: http://www.flyplugins.com
License: GPL3
*/
require_once('fly_plugins_tools.php');
/**
* Show the plugin settings page.
*/
function AP_plugin_settingsPage()
{
?>
<body>
<div class="wrap"><a href="http://www.flyplugins.com">
<div id="fly-icon" style="background: url(<?php echo plugins_url('',__FILE__); ?>/images/fly32x32.png) no-repeat;" class="icon32"><br /></div></a>
<h2>Ascending Post Configuration</h2> <br />
<div class="postbox-container" style="width:70%;">
<div class="metabox-holder">
<div class="meta-box-sortables">
In order to change the order of a category from descending to ascending by date, follow these simple instructions:
<ol>
<li>Create a new category or if you want to modify a currently existing category skip to step #2.</li>
<li><strong>Click edit</strong> under the newly created category, or <strong>click edit</strong> under the already existing category.</li>
<li>Next to the "Sort Order" option, select <strong>"ascending"</strong> or <strong>"descending"</strong> from the drop down menu based upon your choice for that particular category. By default, "descending" is selected which is the default order for WordPress.</li>
</div>
</div>
</div>
<div class="postbox-container" style="width:20%;">
<div class="metabox-holder">
<div class="meta-box-sortables">
<?php
$flydisplay = new Fly_Plugin_Admin();
$flydisplay->donate();
$flydisplay->fly_news();
?>
</div>
</div>
</div>
</div>
</body>
<?php
}
/**
* Add admin post ascending options.
*/
function AP_category_addExtraCategoryFields( $tag )
{
global $theme_css, $cat_meta;
$t_id = $tag->term_id;
$curr_meta = $cat_meta[$t_id];
$direction=( $curr_meta && isset( $curr_meta['order'] ) && $curr_meta['order'] ) ? $curr_meta['order'] : "DESC";
$sort_order = array(
'DESC' => array(
'value' => 'DESC',
'label' => 'Descending (Newest Posts First)'
),
'ASC' => array(
'value' => 'ASC',
'label' => 'Ascending (Oldest Posts First)'
),
);
?>
<tr>
<th scope="row" valign="top"><label for="cat_meta[order]"><?php _e('Sort Order'); ?></label></th>
<td>
<select id="cat_meta[order]" name="cat_meta[order]">
<?php
foreach ( $sort_order as $option ) :
$label = $option['label'];
$selected = '';
if ( $direction && $direction == $option['value'] ) $selected = 'selected="selected"';
echo '<option style="padding-right: 10px;" value="' . esc_attr( $option['value'] ) . '" ' . $selected . '>' . $label . '</option>';
endforeach;
?>
</select>
</td>
</tr>
<?php
}
/**
* Does the actual saving of the option for this category.
*/
function AP_category_saveExtraCategoryFields($term_id)
{
global $cat_meta;
if ( isset( $_POST['cat_meta'] ) )
{
$t_id = $term_id;
$cat_keys = array_keys($_POST['cat_meta']);
$curr_meta = array();
foreach ($cat_keys as $key)
{
if (isset($_POST['cat_meta'][$key])){
$curr_meta[$key] = $_POST['cat_meta'][$key];
}
}
$cat_meta[$t_id] = $curr_meta;
update_option( "theme_cat_meta", $cat_meta );
}
}
$cat_meta = get_option("theme_cat_meta");
/* End - Add Admin Post Ascending Option */
/**
* Function that changes the sort order of the categories by modifying the
* query string.
*/
function AP_categories_sortPostsByAscending($wp_query)
{
global $cat, $cat_meta, $query_string;
if (is_archive() || is_category())
{
if (isset($cat_meta[$cat]['order']) && $cat_meta[$cat]['order'] == 'ASC')
{
query_posts($query_string . "&cat=".$cat."&order=ASC");
}
}
}
/**
* Plugin initialisation.
*/
function AP_plugin_init()
{
// Menu
add_action('admin_menu', 'AP_plugin_menu');
// Change category sorting
add_action('wp_head', 'AP_categories_sortPostsByAscending');
add_action('edit_category_form_fields', 'AP_category_addExtraCategoryFields');
add_action('edited_category', 'AP_category_saveExtraCategoryFields');
// Add settings link to plugins page
$plugin = plugin_basename(__FILE__);
add_filter("plugin_action_links_$plugin", 'AP_plugin_addSettingsLink');
}
add_action('init', 'AP_plugin_init');
/**
* Adds the plugin menu.
*/
function AP_plugin_menu() {
add_options_page('Ascending Post Configuration', 'Ascending Post' , 'manage_options', __FILE__, 'AP_plugin_settingsPage');
}
/**
* Adds a link to the plugin page to click through straight to the plugin page.
*/
function AP_plugin_addSettingsLink($links)
{
$settings_link = sprintf('<a href="%s">Settings</a>', admin_url('options-general.php?page=ascending-posts/ascendingposts.php'));
array_unshift($links, $settings_link);
return $links;
}
?>