-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposts-per-page.php
207 lines (187 loc) · 7.17 KB
/
posts-per-page.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* Plugin Name: Posts Per Page
* Plugin URI: https://github.com/benchmark/posts-per-page
* Description: Control posts per page for all your custom post types.
* Version: 1.0
* Author: Benchmark
* Author URI: http://benchmark.co.uk/
* Text Domain: posts-per-page
* License: GPL2
* Domain Path: /languages/
*
* Copyright 2016 - 2017 Benchmark Studios Ltd.
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package posts-per-page
* @author Benchmark Studios Ltd., Lukas Juhas
* @version 1.0
*
*/
// exit if accessed directly
if (! defined('ABSPATH')) {
exit;
}
// define stuff
define('BPPP_VERSION', '1.0');
define('BPPP_PLUGIN_URL', plugin_dir_url(__FILE__));
define('BPPP_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('BPPP_PLUGIN_BASENAME', plugin_basename(__FILE__));
define('BPPP_PLUGIN_DOMAIN', 'posts-per-page');
define('BPPP_PLUGIN_PREFIX', 'bppp' . '_');
class Benchmark_Posts_Per_Page
{
/**
* constructor
* @since 1.0
*/
public function __construct()
{
add_action('admin_init', array($this, 'register_settings'));
add_action('admin_menu', array($this, 'manage_menus'));
add_action('init', array($this, 'update_options'));
add_filter('plugin_action_links_' . BPPP_PLUGIN_BASENAME, array($this, 'action_links'));
// make sure we modify queries on front end only
if (!is_admin()) {
add_filter('pre_get_posts', array($this, 'posts_per_page'));
}
}
/**
* register settings
* @since 1.0
*/
public function register_settings()
{
foreach ($this->get_posttypes() as $post_type) {
register_setting(BPPP_PLUGIN_DOMAIN, BPPP_PLUGIN_PREFIX . $post_type);
}
}
/**
* get cuomst post types
* @since 1.0
*/
public function get_posttypes()
{
return $post_types = get_post_types([
'public' => true,
'_builtin' => false,
]);
}
/**
* settings page markup
* @since 1.0
*/
public function settings_page()
{
?>
<div class="wrap">
<h2><?php _e('Posts Per Page', BPPP_PLUGIN_DOMAIN); ?></h2>
<form method="post" action="options.php">
<?php settings_fields(BPPP_PLUGIN_DOMAIN); ?>
<?php do_settings_sections(BPPP_PLUGIN_DOMAIN); ?>
<table class="form-table">
<tr>
<th scope="row"><label for="posts_per_page"><?php _e('Blog pages show at most', BPPP_PLUGIN_DOMAIN); ?></label></th>
<td>
<input name="posts_per_page" type="number" step="1" min="1" id="posts_per_page" value="<?php echo get_option('posts_per_page') ?>" class="small-text"> <?php _e('posts', BPPP_PLUGIN_DOMAIN); ?>
<p class="description"><?php _e('Please note this option is in sync with one in the <a href="/wp-admin/options-reading.php">reading options</a>', BPPP_PLUGIN_DOMAIN); ?>.</p>
</td>
</tr>
<?php if ($this->get_posttypes()) : ?>
<tr>
<th><h4><?php _e('Custom Post Types', BPPP_PLUGIN_DOMAIN); ?>:</h4></th>
</tr>
<?php foreach ($this->get_posttypes() as $post_type) : ?>
<tr>
<th scope="row">
<label for="<?php echo BPPP_PLUGIN_PREFIX . $post_type; ?>">
<?php _e(ucwords(str_replace('_', ' ', $post_type)), BPPP_PLUGIN_DOMAIN); ?>
</label>
</th>
<td>
<input name="<?php echo BPPP_PLUGIN_PREFIX ?><?php echo $post_type; ?>" type="number" step="1" min="1" id="<?php echo BPPP_PLUGIN_PREFIX; ?><?php echo $post_type; ?>" value="<?php echo get_option(BPPP_PLUGIN_PREFIX . $post_type); ?>" class="small-text"> <?php _e('posts', BPPP_PLUGIN_DOMAIN); ?>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
/**
* remove unwanted menus
* @since 1.0
*/
public function manage_menus()
{
// add menu pages
add_submenu_page('options-general.php', __('Posts Per Page', BPPP_PLUGIN_DOMAIN), __('Posts Per Page', BPPP_PLUGIN_DOMAIN), 'manage_options', 'ppp-settings', array($this, 'settings_page'));
}
/**
* plugin action links
*
* @since 1.0
*/
public function action_links($links)
{
$links[] = '<a href="'. get_admin_url(null, 'options-general.php?page=ppp-settings') .'">'._x('Settings', 'Plugin Settings link', BPPP_PLUGIN_DOMAIN).'</a>';
$links[] = '<a target="_blank" href="http://benchmark.co.uk/contact-us/">'._x('Support', 'Plugin Support link', BPPP_PLUGIN_DOMAIN).'</a>';
return $links;
}
/**
* custom posts per page
* @since 1.0
*/
public function posts_per_page($query)
{
// validate query
if (is_admin() || ! $query->is_main_query()) {
return;
}
// make sure it's one of our posts types
if (isset($query->query_vars['post_type']) && in_array($query->query_vars['post_type'], $this->get_posttypes())) {
// adjust posts per page
$query->query_vars['posts_per_page'] = get_option(BPPP_PLUGIN_PREFIX . $query->query_vars['post_type']);
}
// return query
return $query;
}
/**
* update options
* @since 1.0
*/
public function update_options()
{
// validate nonce field
if (! isset($_POST['_wpnonce']) || ! wp_verify_nonce($_POST['_wpnonce'], BPPP_PLUGIN_DOMAIN . '-options')) {
return false;
}
extract($_POST);
// We need to "manually" update posts_per_page option as this will not be
// automatically updated by submiting form to options.php:
//
// "For existing options `$autoload` can only be updated using
// `update_option()` if `$value` is also changed. Accepts
// 'yes' or true to enable, 'no' or false to disable.
// For non-existent options, the default value is 'yes'.
if (isset($posts_per_page)) {
update_option('posts_per_page', $posts_per_page);
}
// don't do anything, let the other options update.
}
}
// init
$Benchmark_Posts_Per_Page = new Benchmark_Posts_Per_Page();