-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-post.php
286 lines (266 loc) · 9.73 KB
/
get-post.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/*
Plugin Name: Get post
Plugin URI: http://wordpress.org/extend/plugins/get-post/
Description: Get the content of post(s) matching criteria and use them in
other pages or posts via the [get-post] tag. Full documentation is available
on the plugin site.
Author: James Tatum
Version: 2.0.0
Author URI: http://thelightness.blogspot.com
*/
/* Copyright 2008, 2011 James Tatum (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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
*/
/**
* I tried to separate out reusable components into classes.
* The non-reusable components specific to this plugin are in PluginGetPost.
* Remember to set DEBUG in wp-config.php if making changes, particularly to
* the parser.
*/
include_once dirname( __FILE__ ) . '/class-get-post-parser.php';
include_once dirname( __FILE__ ) . '/class-get-post-getter.php';
if ( !class_exists('PluginGetPostError') )
{
/**
* Exception class. This will be raised if there's an error to be displayed
* to the user rather than a successful invocation
*/
class PluginGetPostError extends Exception { }
}
if (!class_exists('PluginGetPost'))
{
/**
* Plugin class. This class contains all plugin related members.
* The actual work is done in PluginGetPostGetter.
*/
class PluginGetPost
{
/**
* Try to figure out what the user wants. This should either return
* a configured PluginGetPostGetter object or raise an exception.
*/
private function read_params($params)
{
$parser = new PluginGetPostParser();
$options = $parser->parse_params($params);
// Create an object and pass it junk
$post_getter = new PluginGetPostGetter();
$global_options = get_option('get_post_options');
$post_getter->set_template($global_options['template']);
$errors = '';
// if ( count($options) == 0 )
// {
// throw new PluginGetPostError(__('No options specified'));
// }
foreach ( $options as $key => $value )
{
switch ( strtolower($key) )
{
case 'tag':
$post_getter->set_tag($value);
break;
case 'category':
$post_getter->set_category($value);
break;
case 'show':
$post_getter->set_show_posts($value);
break;
case 'random':
$post_getter->set_random(1);
break;
case 'template':
$post_getter->set_template($value);
break;
default:
$errors .= __('Unknown option:') . " $key ";
break;
}
}
if ( $errors != '' )
{
throw new PluginGetPostError($errors);
}
return $post_getter;
}
/**
* Scan post content. The plugin registers to see all post content, so
* here's where we scan it and replace [get-post] tags with something
* like the post they requested. Hopefully.
*/
public function scan_content($content = '')
{
$re = '/(\[get\-post(.*)\])/';
if ( preg_match_all($re, $content, $matches, PREG_SET_ORDER) )
{
foreach ($matches as $match)
{
$params = $match[2];
$replace = $match[1];
unset($post_getter);
try {
$post_getter = $this->read_params($params);
} catch (PluginGetPostError $e)
{
$error_string = __('Get-post encountered an error:');
$message = $e->getMessage();
$result = "<span style=\"color:red;\">$error_string
$message</span>";
}
if ( isset($post_getter) )
{
$result = $post_getter->get();
}
// Replace the tag with the result of the call
$content = str_replace($replace, $result, $content);
}
}
return $content;
}
/**
* Setup default options
*/
public function setup_defaults()
{
$options = get_option('get_post_options');
$updated = 0;
if ( !isset($options) || !is_array($options) )
{
$options = array();
}
if ( !isset($options['template']) )
{
$options['template'] = <<<EOT
<div class="post" id="post-{id}">
<h2 class="entry-title"><a href="{permalink}" rel="bookmark">{title}</a></h2>
<div class="entry-meta">Posted on <a href="{permalink}" title="{time}"
rel="bookmark">{date}</a> by <a href="{authorlink}">{author}</a></div>
<div class="entry">{content}</div>
</div>
EOT;
$updated = 1;
}
if ( $updated == 1 )
{
update_option('get_post_options', $options);
}
}
/**
* Register the admin page
*/
public function register_admin_page()
{
add_options_page('Get-post plugin options', 'Get-post',
'manage_options', 'get-post',
array($this, 'display_admin_page'));
add_filter('plugin_action_links', array($this,
'plugin_actions'), 10, 2);
}
/**
* Initialize admin page, defining settings
*/
public function init_admin_page()
{
register_setting('get_post_options', 'get_post_options',
array($this, 'options_validate'));
add_settings_section('get_post_main', 'General Settings',
array($this, 'display_main_section'), 'get_post_options');
add_settings_field('template', 'Template',
array($this, 'display_template_string'), 'get_post_options',
'get_post_main');
}
/**
* Display the admin page
*/
public function display_admin_page()
{
?>
<div>
<h2>Get-post</h2>
<form action="options.php" method="post">
<?php settings_fields('get_post_options'); ?>
<?php do_settings_sections('get_post_options'); ?>
<input name="Submit" type="submit"
value="<?php esc_attr_e('Save Changes'); ?>" />
</form></div>
<?php
}
/**
* Display main section text
*/
public function display_main_section()
{
echo '<p>General settings controlling text rendering</p>';
}
/**
* Display the template input field
*/
public function display_template_string()
{
$options = get_option('get_post_options');
?>
<textarea id='template' name='get_post_options[template]'
cols='70' rows='6'><?php echo
htmlEntities($options['template'], ENT_QUOTES);
?></textarea>
<?php
}
/**
* Validate the supplied options
*/
public function options_validate($values)
{
// Echo, var_dump, nothing really works here
// add_settings_error('get_post_options', 'value',
// var_export($values, 1), 'error');
$validated_options = array();
$validated_options['template'] =
$values['template'];
// add_settings_error('get_post_options', 'value',
// var_export($validated_options, 1), 'error');
return $validated_options;
}
/**
* Add a settings link to the plugins page
*/
public function plugin_actions($links, $file)
{
static $this_plugin;
if( ! $this_plugin )
{
$this_plugin = plugin_basename(__FILE__);
}
if( $file == $this_plugin )
{
$settings_link = '<a href="options-general.php?page=get-post">'
. __('Settings') . '</a>';
array_unshift( $links, $settings_link );
}
return $links;
}
} // Class PluginGetPost
}
if ( class_exists('PluginGetPost') )
{
$get_post_plugin = new PluginGetPost();
}
if ( isset($get_post_plugin) )
{
// Register a new filter with WordPress to receive all post content
add_filter('the_content', array($get_post_plugin, 'scan_content'));
// Add settings page to admin menu
add_action('admin_menu', array($get_post_plugin, 'register_admin_page'));
add_action('admin_init', array($get_post_plugin, 'init_admin_page'));
// Configure plugin defaults
add_action('wp_loaded', array($get_post_plugin, 'setup_defaults'));
}