PluginProbe
Insert Pages / 2.6
Insert Pages v2.6
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.6, at insert-pages.php

447 lines 15.4 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.6
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( '/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( '/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 // Convert slugs to page IDs to standardize query_posts() lookup below.
143 if ( ! is_numeric( $page ) ) {
144 $page_object = get_page_by_path( $page, OBJECT, get_post_types() );
145 $page = $page_object ? $page_object->ID : $page;
146 }
147
148 if ( is_numeric( $page ) ) {
149 $args = array(
150 'p' => intval( $page ),
151 'post_type' => get_post_types(),
152 );
153 } else {
154 $args = array(
155 'name' => esc_attr( $page ),
156 'post_type' => get_post_types(),
157 );
158 }
159
160 query_posts( $args );
161
162 $should_apply_the_content_filter = true;
163 /**
164 * Filter the flag indicating whether to apply the_content filter to post
165 * contents and excerpts that are being inserted.
166 *
167 * @param bool $apply_the_content_filter Indicates whether to apply the_content filter.
168 */
169 $should_apply_the_content_filter = apply_filters( 'insert_pages_apply_the_content_filter', $should_apply_the_content_filter );
170
171 // Start our new Loop (only iterate once).
172 if ( have_posts() ) {
173 ob_start(); // Start output buffering so we can save the output to string
174
175 // Show either the title, link, content, everything, or everything via a custom template
176 // Note: if the sharing_display filter exists, it means Jetpack is installed and Sharing is enabled;
177 // This plugin conflicts with Sharing, because Sharing assumes the_content and the_excerpt filters
178 // are only getting called once. The fix here is to disable processing of filters on the_content in
179 // the inserted page. @see https://codex.wordpress.org/Function_Reference/the_content#Alternative_Usage
180 switch ( $display ) {
181 case "title":
182 the_post(); ?>
183 <h1><?php the_title(); ?></h1>
184 <?php break;
185 case "link":
186 the_post(); ?>
187 <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
188 <?php break;
189 case "excerpt":
190 the_post(); ?>
191 <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
192 <?php if ( $should_apply_the_content_filter ) the_excerpt(); else echo get_the_excerpt(); ?>
193 <?php break;
194 case "excerpt-only":
195 the_post(); ?>
196 <?php if ( $should_apply_the_content_filter ) the_excerpt(); else echo get_the_excerpt(); ?>
197 <?php break;
198 case "content":
199 the_post(); ?>
200 <?php if ( $should_apply_the_content_filter ) the_content(); else echo get_the_content(); ?>
201 <?php break;
202 case "all":
203 the_post(); ?>
204 <h1><?php the_title(); ?></h1>
205 <?php if ( $should_apply_the_content_filter ) the_content(); else echo get_the_content(); ?>
206 <?php the_meta(); ?>
207 <?php break;
208 default: // display is either invalid, or contains a template file to use
209 $template = locate_template( $display );
210 if ( strlen( $template ) > 0 ) {
211 include $template; // execute the template code
212 } else { // Couldn't find template, so fall back to printing a link to the page.
213 the_post(); ?>
214 <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><?php
215 }
216 break;
217 }
218
219 $content = ob_get_contents(); // Save off output buffer
220 ob_end_clean(); // End output buffering
221 }
222
223 wp_reset_query();
224
225 $content = "<div data-post-id='$page' class='insert-page insert-page-$page $class'>$content</div>";
226 return $content;
227 //return do_shortcode($content); // careful: watch for infinite loops with nested inserts
228 }
229
230
231 // Filter hook: Add a button to the TinyMCE toolbar for our insert page tool
232 function insertPages_handleFilter_mceButtons( $buttons ) {
233 array_push( $buttons, 'wpInsertPages_button' ); // add a separator and button to toolbar
234 return $buttons;
235 }
236
237 // Filter hook: Load the javascript for our custom toolbar button
238 function insertPages_handleFilter_mceExternalPlugins( $plugins ) {
239 $plugins['wpInsertPages'] = plugins_url( '/js/wpinsertpages_plugin.js', __FILE__ );
240 return $plugins;
241 }
242
243 /**
244 * Modified from /wp-admin/includes/internal-linking.php, function wp_link_dialog()
245 * Dialog for internal linking.
246 *
247 * @since 3.1.0
248 */
249 function insertPages_wp_tinymce_dialog() {
250 $options_panel_visible = '1' == get_user_setting( 'wplink', '0' ) ? ' options-panel-visible' : '';
251
252 // display: none is required here, see #WP27605
253 ?><div id="wp-insertpage-backdrop" style="display: none"></div>
254 <div id="wp-insertpage-wrap" class="wp-core-ui<?php echo $options_panel_visible; ?>" style="display: none">
255 <form id="wp-insertpage" tabindex="-1">
256 <?php wp_nonce_field( 'internal-inserting', '_ajax_inserting_nonce', false ); ?>
257 <input type="hidden" id="insertpage-parent-pageID" value="<?php echo $_GET['post'] ?>" />
258 <div id="insertpage-modal-title">
259 <?php _e( 'Insert page' ) ?>
260 <div id="wp-insertpage-close" tabindex="0"></div>
261 </div>
262 <div id="insertpage-selector">
263 <div id="insertpage-search-panel">
264 <div class="insertpage-search-wrapper">
265 <label>
266 <span class="search-label"><?php _e( 'Search' ); ?></span>
267 <input type="search" id="insertpage-search-field" class="insertpage-search-field" autocomplete="off" />
268 <span class="spinner"></span>
269 </label>
270 </div>
271 <div id="insertpage-search-results" class="query-results">
272 <ul></ul>
273 <div class="river-waiting">
274 <span class="spinner"></span>
275 </div>
276 </div>
277 <div id="insertpage-most-recent-results" class="query-results">
278 <div class="query-notice"><em><?php _e( 'No search term specified. Showing recent items.' ); ?></em></div>
279 <ul></ul>
280 <div class="river-waiting">
281 <span class="spinner"></span>
282 </div>
283 </div>
284 </div>
285 <p class="howto" id="insertpage-options-toggle"><?php _e( 'Options' ); ?></p>
286 <div id="insertpage-options-panel">
287 <div class="insertpage-options-wrapper">
288 <label for="insertpage-slug-field">
289 <span><?php _e( 'Slug or ID' ); ?></span>
290 <input id="insertpage-slug-field" type="text" tabindex="10" autocomplete="off" />
291 <input id="insertpage-pageID" type="hidden" />
292 </label>
293 </div>
294 <div class="insertpage-format">
295 <label for="insertpage-format-select">
296 <?php _e( 'Display' ); ?>
297 <select name="insertpage-format-select" id="insertpage-format-select">
298 <option value='title'>Title</option>
299 <option value='link'>Link</option>
300 <option value='excerpt'>Excerpt</option>
301 <option value='excerpt-only'>Excerpt only (no title)</option>
302 <option value='content'>Content</option>
303 <option value='all'>All (includes custom fields)</option>
304 <option value='template'>Use a custom template &raquo;</option>
305 </select>
306 <select name="insertpage-template-select" id="insertpage-template-select" disabled="true">
307 <option value='all'><?php _e( 'Default Template' ); ?></option>
308 <?php page_template_dropdown(); ?>
309 </select>
310 </label>
311 </div>
312 </div>
313 </div>
314 <div class="submitbox">
315 <div id="wp-insertpage-update">
316 <input type="submit" value="<?php esc_attr_e( 'Insert Page' ); ?>" class="button button-primary" id="wp-insertpage-submit" name="wp-insertpage-submit">
317 </div>
318 <div id="wp-insertpage-cancel">
319 <a class="submitdelete deletion" href="#"><?php _e( 'Cancel' ); ?></a>
320 </div>
321 </div>
322 </form>
323 </div>
324 <?php
325 }
326
327 /** Modified from:
328 * Internal linking functions.
329 *
330 * @package WordPress
331 * @subpackage Administration
332 * @since 3.1.0
333 */
334 function insertPages_insert_page_callback() {
335 check_ajax_referer( 'internal-inserting', '_ajax_inserting_nonce' );
336 $args = array();
337 if ( isset( $_POST['search'] ) ) {
338 $args['s'] = stripslashes( $_POST['search'] );
339 }
340 $args['pagenum'] = !empty( $_POST['page'] ) ? absint( $_POST['page'] ) : 1;
341 $args['pageID'] = !empty( $_POST['pageID'] ) ? absint( $_POST['pageID'] ) : 0;
342
343 $results = $this->insertPages_wp_query( $args );
344
345 if ( !isset( $results ) ) {
346 die( '0' );
347 }
348 echo json_encode( $results );
349 echo "\n";
350 die();
351 }
352
353 /** Modified from:
354 * Performs post queries for internal linking.
355 *
356 * @since 3.1.0
357 * @param array $args Optional. Accepts 'pagenum' and 's' (search) arguments.
358 * @return array Results.
359 */
360 function insertPages_wp_query( $args = array() ) {
361 $pts = get_post_types( array( 'public' => true ), 'objects' );
362 $post_types = array_keys( $pts );
363
364 /**
365 * Filter the post types that appear in the list of pages to insert.
366 *
367 * By default, all post types will apear.
368 *
369 * @since 2.0
370 *
371 * @param array $post_types Array of post type names to include.
372 */
373 $post_types = apply_filters( 'insert_pages_available_post_types', $post_types );
374
375 $query = array(
376 'post_type' => $post_types,
377 'suppress_filters' => true,
378 'update_post_term_cache' => false,
379 'update_post_meta_cache' => false,
380 'post_status' => 'publish',
381 'order' => 'DESC',
382 'orderby' => 'post_date',
383 'posts_per_page' => 20,
384 'post__not_in' => array( $args['pageID'] ),
385 );
386
387 $args['pagenum'] = isset( $args['pagenum'] ) ? absint( $args['pagenum'] ) : 1;
388
389 if ( isset( $args['s'] ) ) {
390 $query['s'] = $args['s'];
391 }
392
393 $query['offset'] = $args['pagenum'] > 1 ? $query['posts_per_page'] * ( $args['pagenum'] - 1 ) : 0;
394
395 // Do main query.
396 $get_posts = new WP_Query;
397 $posts = $get_posts->query( $query );
398 // Check if any posts were found.
399 if ( ! $get_posts->post_count ) {
400 return false;
401 }
402
403 // Build results.
404 $results = array();
405 foreach ( $posts as $post ) {
406 if ( 'post' == $post->post_type ) {
407 $info = mysql2date( __( 'Y/m/d' ), $post->post_date );
408 } else {
409 $info = $pts[ $post->post_type ]->labels->singular_name;
410 }
411 $results[] = array(
412 'ID' => $post->ID,
413 'title' => trim( esc_html( strip_tags( get_the_title( $post ) ) ) ),
414 'permalink' => get_permalink( $post->ID ),
415 'slug' => $post->post_name,
416 'info' => $info,
417 );
418 }
419 return $results;
420 }
421
422 function insertPages_add_quicktags() {
423 if ( wp_script_is( 'quicktags' ) ) : ?>
424 <script type="text/javascript">
425 QTags.addButton( 'ed_insert_page', '[insert page]', "[insert page='your-page-slug' display='title|link|excerpt|excerpt-only|content|all']\n", '', '', 'Insert Page', 999 );
426 </script>
427 <?php endif;
428 }
429
430 }
431 }
432
433 // Initialize InsertPagesPlugin object
434 if ( class_exists( 'InsertPagesPlugin' ) ) {
435 $insertPages_plugin = new InsertPagesPlugin();
436 }
437
438 // Actions and Filters handled by InsertPagesPlugin class
439 if ( isset( $insertPages_plugin ) ) {
440 // Actions
441 add_action( 'init', array( $insertPages_plugin, 'insertPages_init' ), 1 ); // Register Shortcodes here
442 add_action( 'admin_head', array( $insertPages_plugin, 'insertPages_admin_init' ), 1 ); // Add TinyMCE buttons here
443 add_action( 'before_wp_tiny_mce', array( $insertPages_plugin, 'insertPages_wp_tinymce_dialog' ), 1 ); // Preload TinyMCE popup
444 add_action( 'wp_ajax_insertpage', array( $insertPages_plugin, 'insertPages_insert_page_callback' ) ); // Populate page search in TinyMCE button popup in this ajax call
445 add_action( 'admin_print_footer_scripts', array( $insertPages_plugin, 'insertPages_add_quicktags' ) );
446 }
447