-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostinpost.php
334 lines (234 loc) · 7.45 KB
/
postinpost.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php if ( ! defined( 'ABSPATH' ) ) exit;
/*
Plugin Name: Post In Post
Plugin URI: https://github.com/doginthehat/postinpost
Description: Import posts in other posts.
Author: Doginthehat
Version: 0.1.4
Author URI: http://doginthehat.com.au/
*/
/*
Post In Post
Copyright (c) 2012 Dog in the hat
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
Acknowledgements
* This plugin structure is inspired by the clean plugin structure from Mute Screamer by Ampt and re-uses its view loading mechanism (released under MIT license).
(http://ampt.github.com/mute-screamer)
Thanks for showing the way to clean plugins!
* The TinyMCE editor icon is adapted from the famous FamFamFam Silk icon library by Mark James (http://www.famfamfam.com/lab/icons/silk/) released under the Creative Commons Attribution 2.5 License
* Thanks to Gozer Studio (http://gozer.com.au) for the opportunity to develop this plugin.
* Thanks also to Johan Steen (http://johansteen.se/) for some inspiration taken from his Post Snippets plugin (http://wpstorm.net/wordpress-plugins/post-snippets/).
*/
if ( ! class_exists( 'PostInPost' ) ) :
define( 'PIP_PATH', dirname( __FILE__ ) );
set_include_path( get_include_path() . PATH_SEPARATOR . PIP_PATH . '/libraries' );
require_once 'Utils.php';
class PostInPost {
/**
* Main instance of this class
*
* @var object
*/
private static $instance = null;
public static function get() { return self::$instance; }
/**
* Base url for plugin resources
*
* @var string
*/
public $base_url = '';
/**
* Plugin options
*
* @var array
*/
public $options = null;
/**
* Allowed post types
*
* @var array
*/
public $post_types = null;
/**
* Behaviour for inception (infinite nesting)
*
* @var string
*/
public $inception_behaviour = null;
/**
* Where to add the tinymce extension
*
* @var string
*/
public $show_in = null;
public function __construct() {
$success = true;
// Require PHP 5.2.
if (version_compare(PHP_VERSION, '5.2', '<'))
{
add_action( 'admin_notices', array($this, 'php_version_error') );
$success = false;
}
// Require WP 3.3 (could work on old version but who wants to do that kind of testing, really).
if (version_compare(get_bloginfo('version'), '3.3', '<'))
{
add_action( 'admin_notices', array($this, 'wp_version_error') );
$success = false;
}
if (!$success)
return;;
self::$instance = $this;
$this->init();
$this->base_url = plugins_url( '/', __FILE__ );
}
function php_version_error() {
echo '<div class="error"><p><strong>';
printf('Error: Post In Post requires PHP version 5.2 or greater.<br/>Your installed PHP version: %1$s', PHP_VERSION);
echo '</strong></p></div>';
}
function wp_version_error() {
echo '<div class="error"><p><strong>';
printf('Error: Post In Post requires WordPress version 3.3 or greater.<br/>Your current Wordpress version: %1$s', get_bloginfo('version') );
echo '</strong></p></div>';
}
/**
* Initialise Post In Post
*
* @return void
*/
private function init() {
$this->init_options();
// Load textdomain
load_plugin_textdomain( 'postinpost', false, PIP_PATH.'/languages' );
add_shortcode( 'postinpost', array($this,'shortcode_postinpost') );
// WP Admin?
if ( is_admin() ) {
require_once 'postinpost-admin.php';
new PostInPost_Admin($this);
}
}
/**
* Initialise options
*
* @return void
*/
private function init_options() {
$options = get_option( 'postinpost_options' );
if ($options === false || $options == '')
$options = array();
$default_options = self::default_options();
// Fallback to default options if the options don't exist in
// the database (kind of like a soft upgrade).
// Automatic plugin updates don't call register_activation_hook.
$this->options = array_merge($default_options, $options);
if (!isset($this->options['post_types']) || !is_array($this->options['post_types']))
{
$this->options['post_types'] = array();
}
$this->post_types = apply_filters('postinpost_post_types', $this->options['post_types']);
if (!isset($this->options['show_in']) || !is_array($this->options['show_in']))
{
$this->options['show_in'] = array();
}
$this->show_in = apply_filters('postinpost_show_in', $this->options['show_in']);
$this->inception_behaviour = apply_filters('postinpost_inception_behaviour', $this->options['inception_behaviour']);
}
/**
* Default options
*
* @return array
*/
public static function default_options() {
return array(
'post_types' => array('page','post'),
'show_in' => array('page','post'),
'inception_behaviour' => 'link'
);
}
/**
* url(), shortcut for plugin resource urls
*
* @return string
*/
public function url($path = '')
{
return plugins_url( $path , __FILE__ );
}
public function shortcode_postinpost($attributes)
{
global $post;
static $inception = array();
if (count($inception)==0)
{
$inception[] = $post->ID;
}
$current_post = $post;
ob_start();
$id = isset($attributes['id']) ? $attributes['id'] : false;
$length = isset($attributes['length']) ? $attributes['length'] : 'full';
if ($id && ($tmp = get_post($id)))
{
$post = $tmp;
// thanks LG
setup_postdata($tmp);
if (in_array($id,$inception))
{
//Oh oh, we have inception
if (defined('WP_DEBUG') && WP_DEBUG == true )
{
$top = $inception[count($inception)-1];
echo "<p><strong>Warning: inception while loading post entry {$id} in entry {$top}</strong></p>";
}
if ($this->inception_behaviour=='link')
{
?>
<p><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', get_template() ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></p>
<?php
}
elseif ($this->inception_behaviour=='ignore')
{
//duh
}
}
else
{
$inception[] = $id;
self::render_the_post($post, $length);
array_pop($inception);
}
}
else if (defined('WP_DEBUG') && WP_DEBUG == true ){
echo "<p><strong>Warning: missing post in post entry ({$id})</strong></p>";
}
$content = ob_get_contents();
ob_end_clean();
array_pop($inception);
$post = $current_post;
setup_postdata($current_post);
return $content;
}
public static function render_the_post($post, $length)
{
// cheap cache
static $override_template = null;
if ($override_template===null)
$override_template = locate_template('postinpost-item.php', false );
if ($override_template)
include($override_template);
else
PIP_Utils::view('postinpost-render',array('post'=>$post, 'length'=>$length ));
}
}
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
add_action( 'init', create_function( '','new PostInPost();' ) );
}
endif;