PluginProbe
Insert Pages / 2.4
Insert Pages v2.4
3.11.5 trunk 1.4 2.4 2.5 2.6 2.7 2.7.1 2.7.2 2.8 2.9 2.9.1 3.0 3.0.1 3.0.2 3.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 All 79 releases
insert-pages / insert-pages.php

insert-pages.php in Insert Pages 2.4, at insert-pages.php

452 lines 15.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 Plugin Name: Insert Pages
5 Plugin URI: https://bitbucket.org/figureone/insert-pages
6 Description: Insert Pages lets you embed any WordPress content (e.g., pages, posts, custom post types) into other WordPress content using the Shortcode API.
7 Author: Paul Ryan
8 Version: 2.4
9 Author URI: http://www.linkedin.com/in/paulrryan
10 License: GPL2
11 */
12
13 /* Copyright 2011 Paul Ryan (email: prar@hawaii.edu)
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License, version 2, as
17 published by the Free Software Foundation.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29 /* Shortcode Format:
30 [insert page='{slug}|{id}' display='title|link|excerpt|excerpt-only|content|all|{custom-template.php}' class='any-classes']
31 */
32
33 // Define the InsertPagesPlugin class (variables and functions)
34 if ( !class_exists( 'InsertPagesPlugin' ) ) {
35 class InsertPagesPlugin {
36 // Save the id of the page being edited
37 protected $pageID;
38
39 // Constructor
40 public function InsertPagesPlugin() {
41 //$this->pageID = '1'; echo $_GET['post'];
42 }
43
44 // Getter/Setter for pageID
45 function getPageID() {
46 return $this->pageID;
47 }
48 function setPageID( $id ) {
49 return $this->pageID = $id;
50 }
51
52 // Action hook: Wordpress 'init'
53 function insertPages_init() {
54 add_shortcode( 'insert', array( $this, 'insertPages_handleShortcode_insert' ) );
55 }
56
57 // Action hook: Wordpress 'admin_init'
58 function insertPages_admin_init() {
59 // Add TinyMCE toolbar button filters only if current user has permissions
60 if ( current_user_can( 'edit_posts' ) && current_user_can( 'edit_pages' ) && get_user_option( 'rich_editing' )=='true' ) {
61
62 // Register the TinyMCE toolbar button script
63 wp_enqueue_script(
64 'wpinsertpages',
65 plugins_url( '/assets/js/wpinsertpages.js', __FILE__ ),
66 array( 'wpdialogs' ),
67 '20140819'
68 );
69 wp_localize_script(
70 'wpinsertpages',
71 'wpInsertPagesL10n',
72 array(
73 'update' => __( 'Update' ),
74 'save' => __( 'Insert Page' ),
75 'noTitle' => __( '(no title)' ),
76 'noMatchesFound' => __( 'No matches found.' ),
77 'l10n_print_after' => 'try{convertEntities(wpLinkL10n);}catch(e){};',
78 )
79 );
80
81 // Register the TinyMCE toolbar button styles
82 wp_enqueue_style(
83 'wpinsertpagescss',
84 plugins_url( '/assets/css/wpinsertpages.css', __FILE__ ),
85 array( 'wp-jquery-ui-dialog' ),
86 '20140819'
87 );
88
89 add_filter( 'mce_external_plugins', array( $this, 'insertPages_handleFilter_mceExternalPlugins' ) );
90 add_filter( 'mce_buttons', array( $this, 'insertPages_handleFilter_mceButtons' ) );
91
92 //load_plugin_textdomain('insert-pages', false, dirname(plugin_basename(__FILE__)).'/languages/');
93 }
94
95 }
96
97
98 // Shortcode hook: Replace the [insert ...] shortcode with the inserted page's content
99 function insertPages_handleShortcode_insert( $atts, $content = null ) {
100 global $wp_query, $post, $wp_current_filter;
101 extract( shortcode_atts( array(
102 'page' => '0',
103 'display' => 'all',
104 'class' => '',
105 ), $atts ) );
106
107 // Validation checks.
108 if ( $page === '0' ) {
109 return $content;
110 }
111
112 // Trying to embed same page in itself.
113 if ( $page == $post->ID || $page == $post->post_name ) {
114 return $content;
115 }
116
117 $should_apply_nesting_check = true;
118 /**
119 * Filter the flag indicating whether to apply deep nesting check
120 * that can prevent circular loops. Note that some use cases rely
121 * on inserting pages that themselves have inserted pages, so this
122 * check should be disabled for those individuals.
123 *
124 * @param bool $apply_the_content_filter Indicates whether to apply the_content filter.
125 */
126 $should_apply_nesting_check = apply_filters( 'insert_pages_apply_nesting_check', $should_apply_nesting_check );
127
128 // Don't allow inserted pages to be added to the_content more than once (prevent infinite loops).
129 if ( $should_apply_nesting_check ) {
130 $done = false;
131 foreach ( $wp_current_filter as $filter ) {
132 if ( 'the_content' == $filter ) {
133 if ( $done ) {
134 return $content;
135 } else {
136 $done = true;
137 }
138 }
139 }
140 }
141
142 // Get page object from slug or id
143 $temp_query = clone $wp_query; // we're starting a new loop within the main loop, so save the main query
144 $temp_post = $wp_query->get_queried_object(); // see: http://codex.wordpress.org/The_Loop#Multiple_Loops_Example_2
145
146 // Convert slugs to page IDs to standardize query_posts() lookup below.
147 if ( ! is_numeric( $page ) ) {
148 $page_object = get_page_by_path( $page, OBJECT, get_post_types() );
149 $page = $page_object ? $page_object->ID : $page;
150 }
151
152 if ( is_numeric( $page ) ) {
153 $args = array(
154 'p' => intval( $page ),
155 'post_type' => get_post_types(),
156 );
157 } else {
158 $args = array(
159 'name' => esc_attr( $page ),
160 'post_type' => get_post_types(),
161 );
162 }
163
164 query_posts( $args );
165
166 $should_apply_the_content_filter = true;
167 /**
168 * Filter the flag indicating whether to apply the_content filter to post
169 * contents and excerpts that are being inserted.
170 *
171 * @param bool $apply_the_content_filter Indicates whether to apply the_content filter.
172 */
173 $should_apply_the_content_filter = apply_filters( 'insert_pages_apply_the_content_filter', $should_apply_the_content_filter );
174
175 // Start our new Loop
176 while ( have_posts() ) {
177 ob_start(); // Start output buffering so we can save the output to string
178
179 // Show either the title, link, content, everything, or everything via a custom template
180 // Note: if the sharing_display filter exists, it means Jetpack is installed and Sharing is enabled;
181 // This plugin conflicts with Sharing, because Sharing assumes the_content and the_excerpt filters
182 // are only getting called once. The fix here is to disable processing of filters on the_content in
183 // the inserted page. @see https://codex.wordpress.org/Function_Reference/the_content#Alternative_Usage
184 switch ( $display ) {
185 case "title":
186 the_post(); ?>
187 <h1><?php the_title(); ?></h1>
188 <?php break;
189 case "link":
190 the_post(); ?>
191 <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
192 <?php break;
193 case "excerpt":
194 the_post(); ?>
195 <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
196 <?php if ( $should_apply_the_content_filter ) the_excerpt(); else echo get_the_excerpt(); ?>
197 <?php break;
198 case "excerpt-only":
199 the_post(); ?>
200 <?php if ( $should_apply_the_content_filter ) the_excerpt(); else echo get_the_excerpt(); ?>
201 <?php break;
202 case "content":
203 the_post(); ?>
204 <?php if ( $should_apply_the_content_filter ) the_content(); else echo get_the_content(); ?>
205 <?php break;
206 case "all":
207 the_post(); ?>
208 <h1><?php the_title(); ?></h1>
209 <?php if ( $should_apply_the_content_filter ) the_content(); else echo get_the_content(); ?>
210 <?php the_meta(); ?>
211 <?php break;
212 default: // display is either invalid, or contains a template file to use
213 $template = locate_template( $display );
214 if ( strlen( $template ) > 0 ) {
215 include $template; // execute the template code
216 } else { // Couldn't find template, so fall back to printing a link to the page.
217 the_post(); ?>
218 <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><?php
219 }
220 break;
221 }
222
223 $content = ob_get_contents(); // Save off output buffer
224 ob_end_clean(); // End output buffering
225 }
226 wp_reset_postdata();
227 $wp_query = clone $temp_query; // Restore main Loop's wp_query
228 $post = $temp_post;
229
230 $content = "<div data-post-id='$page' class='insert-page insert-page-$page $class'>$content</div>";
231 return $content;
232 //return do_shortcode($content); // careful: watch for infinite loops with nested inserts
233 }
234
235
236 // Filter hook: Add a button to the TinyMCE toolbar for our insert page tool
237 function insertPages_handleFilter_mceButtons( $buttons ) {
238 array_push( $buttons, 'wpInsertPages_button' ); // add a separator and button to toolbar
239 return $buttons;
240 }
241
242 // Filter hook: Load the javascript for our custom toolbar button
243 function insertPages_handleFilter_mceExternalPlugins( $plugins ) {
244 $plugins['wpInsertPages'] = plugins_url( '/assets/js/wpinsertpages_plugin.js', __FILE__ );
245 return $plugins;
246 }
247
248 /**
249 * Modified from /wp-admin/includes/internal-linking.php, function wp_link_dialog()
250 * Dialog for internal linking.
251 *
252 * @since 3.1.0
253 */
254 function insertPages_wp_tinymce_dialog() {
255 $options_panel_visible = '1' == get_user_setting( 'wplink', '0' ) ? ' options-panel-visible' : '';
256
257 // display: none is required here, see #WP27605
258 ?><div id="wp-insertpage-backdrop" style="display: none"></div>
259 <div id="wp-insertpage-wrap" class="wp-core-ui<?php echo $options_panel_visible; ?>" style="display: none">
260 <form id="wp-insertpage" tabindex="-1">
261 <?php wp_nonce_field( 'internal-inserting', '_ajax_inserting_nonce', false ); ?>
262 <input type="hidden" id="insertpage-parent-pageID" value="<?php echo $_GET['post'] ?>" />
263 <div id="insertpage-modal-title">
264 <?php _e( 'Insert page' ) ?>
265 <div id="wp-insertpage-close" tabindex="0"></div>
266 </div>
267 <div id="insertpage-selector">
268 <div id="insertpage-search-panel">
269 <div class="insertpage-search-wrapper">
270 <label>
271 <span class="search-label"><?php _e( 'Search' ); ?></span>
272 <input type="search" id="insertpage-search-field" class="insertpage-search-field" autocomplete="off" />
273 <span class="spinner"></span>
274 </label>
275 </div>
276 <div id="insertpage-search-results" class="query-results">
277 <ul></ul>
278 <div class="river-waiting">
279 <span class="spinner"></span>
280 </div>
281 </div>
282 <div id="insertpage-most-recent-results" class="query-results">
283 <div class="query-notice"><em><?php _e( 'No search term specified. Showing recent items.' ); ?></em></div>
284 <ul></ul>
285 <div class="river-waiting">
286 <span class="spinner"></span>
287 </div>
288 </div>
289 </div>
290 <p class="howto" id="insertpage-options-toggle"><?php _e( 'Options' ); ?></p>
291 <div id="insertpage-options-panel">
292 <div class="insertpage-options-wrapper">
293 <label for="insertpage-slug-field">
294 <span><?php _e( 'Slug or ID' ); ?></span>
295 <input id="insertpage-slug-field" type="text" tabindex="10" autocomplete="off" />
296 <input id="insertpage-pageID" type="hidden" />
297 </label>
298 </div>
299 <div class="insertpage-format">
300 <label for="insertpage-format-select">
301 <?php _e( 'Display' ); ?>
302 <select name="insertpage-format-select" id="insertpage-format-select">
303 <option value='title'>Title</option>
304 <option value='link'>Link</option>
305 <option value='excerpt'>Excerpt</option>
306 <option value='excerpt-only'>Excerpt only (no title)</option>
307 <option value='content'>Content</option>
308 <option value='all'>All (includes custom fields)</option>
309 <option value='template'>Use a custom template &raquo;</option>
310 </select>
311 <select name="insertpage-template-select" id="insertpage-template-select" disabled="true">
312 <option value='all'><?php _e( 'Default Template' ); ?></option>
313 <?php page_template_dropdown(); ?>
314 </select>
315 </label>
316 </div>
317 </div>
318 </div>
319 <div class="submitbox">
320 <div id="wp-insertpage-update">
321 <input type="submit" value="<?php esc_attr_e( 'Insert Page' ); ?>" class="button button-primary" id="wp-insertpage-submit" name="wp-insertpage-submit">
322 </div>
323 <div id="wp-insertpage-cancel">
324 <a class="submitdelete deletion" href="#"><?php _e( 'Cancel' ); ?></a>
325 </div>
326 </div>
327 </form>
328 </div>
329 <?php
330 }
331
332 /** Modified from:
333 * Internal linking functions.
334 *
335 * @package WordPress
336 * @subpackage Administration
337 * @since 3.1.0
338 */
339 function insertPages_insert_page_callback() {
340 check_ajax_referer( 'internal-inserting', '_ajax_inserting_nonce' );
341 $args = array();
342 if ( isset( $_POST['search'] ) ) {
343 $args['s'] = stripslashes( $_POST['search'] );
344 }
345 $args['pagenum'] = !empty( $_POST['page'] ) ? absint( $_POST['page'] ) : 1;
346 $args['pageID'] = !empty( $_POST['pageID'] ) ? absint( $_POST['pageID'] ) : 0;
347
348 $results = $this->insertPages_wp_query( $args );
349
350 if ( !isset( $results ) ) {
351 die( '0' );
352 }
353 echo json_encode( $results );
354 echo "\n";
355 die();
356 }
357
358 /** Modified from:
359 * Performs post queries for internal linking.
360 *
361 * @since 3.1.0
362 * @param array $args Optional. Accepts 'pagenum' and 's' (search) arguments.
363 * @return array Results.
364 */
365 function insertPages_wp_query( $args = array() ) {
366 $pts = get_post_types( array( 'public' => true ), 'objects' );
367 $post_types = array_keys( $pts );
368
369 /**
370 * Filter the post types that appear in the list of pages to insert.
371 *
372 * By default, all post types will apear.
373 *
374 * @since 2.0
375 *
376 * @param array $post_types Array of post type names to include.
377 */
378 $post_types = apply_filters( 'insert_pages_available_post_types', $post_types );
379
380 $query = array(
381 'post_type' => $post_types,
382 'suppress_filters' => true,
383 'update_post_term_cache' => false,
384 'update_post_meta_cache' => false,
385 'post_status' => 'publish',
386 'order' => 'DESC',
387 'orderby' => 'post_date',
388 'posts_per_page' => 20,
389 'post__not_in' => array( $args['pageID'] ),
390 );
391
392 $args['pagenum'] = isset( $args['pagenum'] ) ? absint( $args['pagenum'] ) : 1;
393
394 if ( isset( $args['s'] ) ) {
395 $query['s'] = $args['s'];
396 }
397
398 $query['offset'] = $args['pagenum'] > 1 ? $query['posts_per_page'] * ( $args['pagenum'] - 1 ) : 0;
399
400 // Do main query.
401 $get_posts = new WP_Query;
402 $posts = $get_posts->query( $query );
403 // Check if any posts were found.
404 if ( ! $get_posts->post_count ) {
405 return false;
406 }
407
408 // Build results.
409 $results = array();
410 foreach ( $posts as $post ) {
411 if ( 'post' == $post->post_type ) {
412 $info = mysql2date( __( 'Y/m/d' ), $post->post_date );
413 } else {
414 $info = $pts[ $post->post_type ]->labels->singular_name;
415 }
416 $results[] = array(
417 'ID' => $post->ID,
418 'title' => trim( esc_html( strip_tags( get_the_title( $post ) ) ) ),
419 'permalink' => get_permalink( $post->ID ),
420 'slug' => $post->post_name,
421 'info' => $info,
422 );
423 }
424 return $results;
425 }
426
427 function insertPages_add_quicktags() {
428 if ( wp_script_is( 'quicktags' ) ) : ?>
429 <script type="text/javascript">
430 QTags.addButton( 'ed_insert_page', '[insert page]', "[insert page='your-page-slug' display='title|link|excerpt|excerpt-only|content|all']\n", '', '', 'Insert Page', 999 );
431 </script>
432 <?php endif;
433 }
434
435 }
436 }
437
438 // Initialize InsertPagesPlugin object
439 if ( class_exists( 'InsertPagesPlugin' ) ) {
440 $insertPages_plugin = new InsertPagesPlugin();
441 }
442
443 // Actions and Filters handled by InsertPagesPlugin class
444 if ( isset( $insertPages_plugin ) ) {
445 // Actions
446 add_action( 'init', array( $insertPages_plugin, 'insertPages_init' ), 1 ); // Register Shortcodes here
447 add_action( 'admin_head', array( $insertPages_plugin, 'insertPages_admin_init' ), 1 ); // Add TinyMCE buttons here
448 add_action( 'before_wp_tiny_mce', array( $insertPages_plugin, 'insertPages_wp_tinymce_dialog' ), 1 ); // Preload TinyMCE popup
449 add_action( 'wp_ajax_insertpage', array( $insertPages_plugin, 'insertPages_insert_page_callback' ) ); // Populate page search in TinyMCE button popup in this ajax call
450 add_action( 'admin_print_footer_scripts', array( $insertPages_plugin, 'insertPages_add_quicktags' ) );
451 }
452