PluginProbe
Insert Pages / 3.1
Insert Pages v3.1
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 3.1, at insert-pages.php

771 lines 28.6 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://github.com/uhm-coe/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: 3.1
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 // Include the code that generates the options page.
42 require_once( dirname( __FILE__ ) . '/options.php' );
43 }
44
45 // Getter/Setter for pageID
46 function getPageID() {
47 return $this->pageID;
48 }
49 function setPageID( $id ) {
50 return $this->pageID = $id;
51 }
52
53 // Action hook: Wordpress 'init'
54 function insertPages_init() {
55 add_shortcode( 'insert', array( $this, 'insertPages_handleShortcode_insert' ) );
56 }
57
58 // Action hook: Wordpress 'admin_init'
59 function insertPages_admin_init() {
60 // Get options set in WordPress dashboard (Settings > Insert Pages).
61 $options = get_option( 'wpip_settings' );
62 if ( $options === FALSE || ! is_array( $options ) || ! array_key_exists( 'wpip_format', $options ) || ! array_key_exists( 'wpip_wrapper', $options ) || ! array_key_exists( 'wpip_insert_method', $options ) ) {
63 $options = wpip_set_defaults();
64 }
65
66 // Add TinyMCE toolbar button filters only if current user has permissions
67 if ( current_user_can( 'edit_posts' ) && current_user_can( 'edit_pages' ) && get_user_option( 'rich_editing' )=='true' ) {
68
69 // Register the TinyMCE toolbar button script
70 wp_enqueue_script(
71 'wpinsertpages',
72 plugins_url( '/js/wpinsertpages.js', __FILE__ ),
73 array( 'wpdialogs' ),
74 '20151230'
75 );
76 wp_localize_script(
77 'wpinsertpages',
78 'wpInsertPagesL10n',
79 array(
80 'update' => __( 'Update' ),
81 'save' => __( 'Insert Page' ),
82 'noTitle' => __( '(no title)' ),
83 'noMatchesFound' => __( 'No matches found.' ),
84 'l10n_print_after' => 'try{convertEntities(wpLinkL10n);}catch(e){};',
85 'format' => $options['wpip_format'],
86 )
87 );
88
89 // Register the TinyMCE toolbar button styles
90 wp_enqueue_style(
91 'wpinsertpagescss',
92 plugins_url( '/css/wpinsertpages.css', __FILE__ ),
93 array( 'wp-jquery-ui-dialog' ),
94 '20151230'
95 );
96
97 add_filter( 'mce_external_plugins', array( $this, 'insertPages_handleFilter_mceExternalPlugins' ) );
98 add_filter( 'mce_buttons', array( $this, 'insertPages_handleFilter_mceButtons' ) );
99
100 //load_plugin_textdomain('insert-pages', false, dirname(plugin_basename(__FILE__)).'/languages/');
101 }
102
103 }
104
105
106 // Shortcode hook: Replace the [insert ...] shortcode with the inserted page's content
107 function insertPages_handleShortcode_insert( $atts, $content = null ) {
108 global $wp_query, $post, $wp_current_filter;
109
110 // Shortcode attributes.
111 $attributes = shortcode_atts( array(
112 'page' => '0',
113 'display' => 'all',
114 'class' => '',
115 'inline' => false,
116 ), $atts );
117
118 // Validation checks.
119 if ( $attributes['page'] === '0' ) {
120 return $content;
121 }
122
123 // Trying to embed same page in itself.
124 if ( $attributes['page'] == $post->ID || $attributes['page'] == $post->post_name ) {
125 return $content;
126 }
127
128 // Get options set in WordPress dashboard (Settings > Insert Pages).
129 $options = get_option( 'wpip_settings' );
130 if ( $options === FALSE || ! is_array( $options ) || ! array_key_exists( 'wpip_format', $options ) || ! array_key_exists( 'wpip_wrapper', $options ) || ! array_key_exists( 'wpip_insert_method', $options ) ) {
131 $options = wpip_set_defaults();
132 }
133
134 $attributes['inline'] = ( $attributes['inline'] !== false && $attributes['inline'] !== 'false' ) || array_search( 'inline', $atts ) === 0 || ( array_key_exists( 'wpip_wrapper', $options ) && $options['wpip_wrapper'] === 'inline' );
135 /**
136 * Filter the flag indicating whether to wrap the inserted content in inline tags (span).
137 *
138 * @param bool $use_inline_wrapper Indicates whether to wrap the content in span tags.
139 */
140 $attributes['inline'] = apply_filters( 'insert_pages_use_inline_wrapper', $attributes['inline'] );
141 $attributes['wrapper_tag'] = $attributes['inline'] ? 'span' : 'div';
142
143 $attributes['should_apply_the_content_filter'] = true;
144 /**
145 * Filter the flag indicating whether to apply the_content filter to post
146 * contents and excerpts that are being inserted.
147 *
148 * @param bool $apply_the_content_filter Indicates whether to apply the_content filter.
149 */
150 $attributes['should_apply_the_content_filter'] = apply_filters( 'insert_pages_apply_the_content_filter', $attributes['should_apply_the_content_filter'] );
151
152 // Disable the_content filter if using inline tags, since wpautop
153 // inserts p tags and we can't have any inside inline elements.
154 if ( $attributes['inline'] ) {
155 $attributes['should_apply_the_content_filter'] = false;
156 }
157
158 $attributes['should_apply_nesting_check'] = true;
159 /**
160 * Filter the flag indicating whether to apply deep nesting check
161 * that can prevent circular loops. Note that some use cases rely
162 * on inserting pages that themselves have inserted pages, so this
163 * check should be disabled for those individuals.
164 *
165 * @param bool $apply_the_content_filter Indicates whether to apply the_content filter.
166 */
167 $attributes['should_apply_nesting_check'] = apply_filters( 'insert_pages_apply_nesting_check', $attributes['should_apply_nesting_check'] );
168
169 // Don't allow inserted pages to be added to the_content more than once (prevent infinite loops).
170 if ( $attributes['should_apply_nesting_check'] ) {
171 $done = false;
172 foreach ( $wp_current_filter as $filter ) {
173 if ( 'the_content' == $filter ) {
174 if ( $done ) {
175 return $content;
176 } else {
177 $done = true;
178 }
179 }
180 }
181 }
182
183 // Get the WP_Post object from the provided slug or ID.
184 if ( ! is_numeric( $attributes['page'] ) ) {
185 $inserted_page = get_page_by_path( $attributes['page'], OBJECT, get_post_types() );
186 $attributes['page'] = $inserted_page ? $inserted_page->ID : $attributes['page'];
187 } else {
188 $inserted_page = get_post( intval( $attributes['page'] ) );
189 }
190
191 // Use "Normal" insert method (get_post()).
192 if ( $options['wpip_insert_method'] !== 'legacy' ) {
193
194 // If we couldn't retrieve the page, fire the filter hook showing a not-found message.
195 if ( $inserted_page === null ) {
196 /**
197 * Filter the html that should be displayed if an inserted page was not found.
198 *
199 * @param string $content html to be displayed. Defaults to an empty string.
200 */
201 $content = apply_filters( 'insert_pages_not_found_message', $content );
202
203 // Short-circuit since we didn't find the page.
204 return $content;
205 }
206
207 // Start output buffering so we can save the output to a string.
208 ob_start();
209
210 // If Beaver Builder plugin is enabled, load any cached styles associated with the inserted page.
211 // Note: Temporarily set the global $post->ID to the inserted page ID,
212 // since Beaver Builder relies on it to load the appropriate styles.
213 if ( class_exists( 'FLBuilder' ) ) {
214 $old_post_id = $post->ID;
215 $post->ID = $inserted_page->ID;
216 FLBuilder::enqueue_layout_styles_scripts( $inserted_page->ID );
217 $post->ID = $old_post_id;
218 }
219
220 // Show either the title, link, content, everything, or everything via a custom template
221 // Note: if the sharing_display filter exists, it means Jetpack is installed and Sharing is enabled;
222 // This plugin conflicts with Sharing, because Sharing assumes the_content and the_excerpt filters
223 // are only getting called once. The fix here is to disable processing of filters on the_content in
224 // the inserted page. @see https://codex.wordpress.org/Function_Reference/the_content#Alternative_Usage
225 switch ( $attributes['display'] ) {
226
227 case "title":
228 $title_tag = $attributes['inline'] ? 'span' : 'h1';
229 echo "<$title_tag class='insert-page-title'>";
230 echo get_the_title( $inserted_page->ID );
231 echo "</$title_tag>";
232 break;
233
234 case "link":
235 ?><a href="<?php echo esc_url( get_permalink( $inserted_page->ID ) ); ?>"><?php echo get_the_title( $inserted_page->ID ); ?></a><?php
236 break;
237
238 case "excerpt":
239 ?><h1><a href="<?php echo esc_url( get_permalink( $inserted_page->ID ) ); ?>"><?php echo get_the_title( $inserted_page->ID ); ?></a></h1><?php
240 echo $this->insertPages_trim_excerpt( get_post_field( 'post_excerpt', $inserted_page->ID ), $inserted_page->ID, $attributes['should_apply_the_content_filter'] );
241 break;
242
243 case "excerpt-only":
244 echo $this->insertPages_trim_excerpt( get_post_field( 'post_excerpt', $inserted_page->ID ), $inserted_page->ID, $attributes['should_apply_the_content_filter'] );
245 break;
246
247 case "content":
248 $content = get_post_field( 'post_content', $inserted_page->ID );
249 if ( $attributes['should_apply_the_content_filter'] ) {
250 $content = apply_filters( 'the_content', $content );
251 }
252 echo $content;
253 break;
254
255 case "all":
256 // Title.
257 $title_tag = $attributes['inline'] ? 'span' : 'h1';
258 echo "<$title_tag class='insert-page-title'>";
259 echo get_the_title( $inserted_page->ID );
260 echo "</$title_tag>";
261 // Content.
262 $content = get_post_field( 'post_content', $inserted_page->ID );
263 if ( $attributes['should_apply_the_content_filter'] ) {
264 $content = apply_filters( 'the_content', $content );
265 }
266 echo $content;
267 // Meta.
268 // @ref https://core.trac.wordpress.org/browser/tags/4.4/src/wp-includes/post-template.php#L968
269 if ( $keys = get_post_custom_keys( $inserted_page->ID ) ) {
270 echo "<ul class='post-meta'>\n";
271 foreach ( (array) $keys as $key ) {
272 $keyt = trim( $key );
273 if ( is_protected_meta( $keyt, 'post' ) ) {
274 continue;
275 }
276 $values = array_map( 'trim', get_post_custom_values( $key ) );
277 $value = implode( $values, ', ' );
278
279 /**
280 * Filter the HTML output of the li element in the post custom fields list.
281 *
282 * @since 2.2.0
283 *
284 * @param string $html The HTML output for the li element.
285 * @param string $key Meta key.
286 * @param string $value Meta value.
287 */
288 echo apply_filters( 'the_meta_key', "<li><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value );
289 }
290 echo "</ul>\n";
291 }
292 break;
293
294 default: // display is either invalid, or contains a template file to use
295 // Legacy/compatibility code: In order to use custom templates,
296 // we use query_posts() to provide the template with the global
297 // state it requires for the inserted page (in other words, all
298 // template tags will work with respect to the inserted page
299 // instead of the parent page / main loop). Note that this may
300 // cause some compatibility issues with other plugins.
301 // @ref https://codex.wordpress.org/Function_Reference/query_posts
302 if ( is_numeric( $attributes['page'] ) ) {
303 $args = array(
304 'p' => intval( $attributes['page'] ),
305 'post_type' => get_post_types(),
306 );
307 } else {
308 $args = array(
309 'name' => esc_attr( $attributes['page'] ),
310 'post_type' => get_post_types(),
311 );
312 }
313 $inserted_page = query_posts( $args );
314 if ( have_posts() ) {
315 $template = locate_template( $attributes['display'] );
316 if ( strlen( $template ) > 0 ) {
317 include $template; // execute the template code
318 } else { // Couldn't find template, so fall back to printing a link to the page.
319 the_post();
320 ?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><?php
321 }
322 }
323 wp_reset_query();
324
325 }
326
327 // Save output buffer contents.
328 $content = ob_get_clean();
329
330 // Use "Legacy" insert method (query_posts()).
331 } else {
332
333 // Construct query_posts arguments.
334 if ( is_numeric( $attributes['page'] ) ) {
335 $args = array(
336 'p' => intval( $attributes['page'] ),
337 'post_type' => get_post_types(),
338 );
339 } else {
340 $args = array(
341 'name' => esc_attr( $attributes['page'] ),
342 'post_type' => get_post_types(),
343 );
344 }
345 $posts = query_posts( $args );
346 if ( have_posts() ) {
347 // Start output buffering so we can save the output to string
348 ob_start();
349
350 // If Beaver Builder plugin is enabled, load any cached styles associated with the inserted page.
351 // Note: Temporarily set the global $post->ID to the inserted page ID,
352 // since Beaver Builder relies on it to load the appropriate styles.
353 if ( class_exists( 'FLBuilder' ) ) {
354 $old_post_id = $post->ID;
355 $post->ID = $inserted_page->ID;
356 FLBuilder::enqueue_layout_styles_scripts( $inserted_page->ID );
357 $post->ID = $old_post_id;
358 }
359
360 // Show either the title, link, content, everything, or everything via a custom template
361 // Note: if the sharing_display filter exists, it means Jetpack is installed and Sharing is enabled;
362 // This plugin conflicts with Sharing, because Sharing assumes the_content and the_excerpt filters
363 // are only getting called once. The fix here is to disable processing of filters on the_content in
364 // the inserted page. @see https://codex.wordpress.org/Function_Reference/the_content#Alternative_Usage
365 switch ( $attributes['display'] ) {
366 case "title":
367 the_post();
368 $title_tag = $attributes['inline'] ? 'span' : 'h1';
369 echo "<$title_tag class='insert-page-title'>";
370 the_title();
371 echo "</$title_tag>";
372 break;
373 case "link":
374 the_post();
375 ?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><?php
376 break;
377 case "excerpt":
378 the_post();
379 ?><h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1><?php
380 if ( $attributes['should_apply_the_content_filter'] ) the_excerpt(); else echo get_the_excerpt();
381 break;
382 case "excerpt-only":
383 the_post();
384 if ( $attributes['should_apply_the_content_filter'] ) the_excerpt(); else echo get_the_excerpt();
385 break;
386 case "content":
387 the_post();
388 if ( $attributes['should_apply_the_content_filter'] ) the_content(); else echo get_the_content();
389 break;
390 case "all":
391 the_post();
392 $title_tag = $attributes['inline'] ? 'span' : 'h1';
393 echo "<$title_tag class='insert-page-title'>";
394 the_title();
395 echo "</$title_tag>";
396 if ( $attributes['should_apply_the_content_filter'] ) the_content(); else echo get_the_content();
397 the_meta();
398 break;
399 default: // display is either invalid, or contains a template file to use
400 $template = locate_template( $attributes['display'] );
401 if ( strlen( $template ) > 0 ) {
402 include $template; // execute the template code
403 } else { // Couldn't find template, so fall back to printing a link to the page.
404 the_post();
405 ?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><?php
406 }
407 break;
408 }
409 // Save output buffer contents.
410 $content = ob_get_clean();
411 } else {
412 /**
413 * Filter the html that should be displayed if an inserted page was not found.
414 *
415 * @param string $content html to be displayed. Defaults to an empty string.
416 */
417 $content = apply_filters( 'insert_pages_not_found_message', $content );
418 }
419 wp_reset_query();
420 }
421
422 /**
423 * Filter the markup generated for the inserted page.
424 *
425 * @param string $content The post content of the inserted page.
426 * @param object $inserted_page The post object returned from querying the inserted page.
427 * @param array $attributes Extra parameters modifying the inserted page.
428 * page: Page ID or slug of page to be inserted.
429 * display: Content to display from inserted page.
430 * class: Extra classes to add to inserted page wrapper element.
431 * inline: Boolean indicating wrapper element should be a span.
432 * should_apply_nesting_check: Whether to disable nested inserted pages.
433 * should_apply_the_content_filter: Whether to apply the_content filter to post contents and excerpts.
434 * wrapper_tag: Tag to use for the wrapper element (e.g., div, span).
435 */
436 $content = apply_filters( 'insert_pages_wrap_content', $content, $inserted_page, $attributes );
437
438 return $content;
439 }
440
441 // Default filter for insert_pages_wrap_content.
442 function insertPages_wrap_content( $content, $posts, $attributes ) {
443 return "<{$attributes['wrapper_tag']} data-post-id='{$attributes['page']}' class='insert-page insert-page-{$attributes['page']} {$attributes['class']}'>{$content}</{$attributes['wrapper_tag']}>";
444 }
445
446 // Filter hook: Add a button to the TinyMCE toolbar for our insert page tool
447 function insertPages_handleFilter_mceButtons( $buttons ) {
448 array_push( $buttons, 'wpInsertPages_button' ); // add a separator and button to toolbar
449 return $buttons;
450 }
451
452 // Filter hook: Load the javascript for our custom toolbar button
453 function insertPages_handleFilter_mceExternalPlugins( $plugins ) {
454 $plugins['wpInsertPages'] = plugins_url( '/js/wpinsertpages_plugin.js', __FILE__ );
455 return $plugins;
456 }
457
458 // Helper function to generate an excerpt (outside of the Loop) for a given ID.
459 // @ref wp_trim_excerpt()
460 function insertPages_trim_excerpt( $text = '', $post_id = 0, $apply_the_content_filter = true ) {
461 $post_id = intval( $post_id );
462 if ( $post_id < 1 ) {
463 return '';
464 }
465
466 $raw_excerpt = $text;
467 if ( '' == $text ) {
468 $text = get_post_field( 'post_content', $post_id );
469
470 $text = strip_shortcodes( $text );
471
472 /** This filter is documented in wp-includes/post-template.php */
473 if ( $apply_the_content_filter ) {
474 $text = apply_filters( 'the_content', $text );
475 }
476 $text = str_replace( ']]>', ']]&gt;', $text );
477
478 /**
479 * Filter the number of words in an excerpt.
480 *
481 * @since 2.7.0
482 *
483 * @param int $number The number of words. Default 55.
484 */
485 $excerpt_length = apply_filters( 'excerpt_length', 55 );
486 /**
487 * Filter the string in the "more" link displayed after a trimmed excerpt.
488 *
489 * @since 2.9.0
490 *
491 * @param string $more_string The string shown within the more link.
492 */
493 $excerpt_more = apply_filters( 'excerpt_more', ' ' . '[&hellip;]' );
494 $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
495 }
496 /**
497 * Filter the trimmed excerpt string.
498 *
499 * @since 2.8.0
500 *
501 * @param string $text The trimmed text.
502 * @param string $raw_excerpt The text prior to trimming.
503 */
504 return apply_filters( 'wp_trim_excerpt', $text, $raw_excerpt );
505 }
506
507 /**
508 * Modified from /wp-admin/includes/internal-linking.php, function wp_link_dialog()
509 * Dialog for internal linking.
510 *
511 * @since 3.1.0
512 */
513 function insertPages_wp_tinymce_dialog() {
514 // If wp_editor() is being called outside of an admin context,
515 // required dependencies for Insert Pages will be missing (e.g.,
516 // wp-admin/includes/template.php will not be loaded, admin_head
517 // action will not be fired). If that's the case, just skip loading
518 // the Insert Pages tinymce button.
519 if ( ! is_admin() || ! function_exists( 'page_template_dropdown' ) ) {
520 return;
521 }
522
523 $options_panel_visible = '1' == get_user_setting( 'wpinsertpage', '0' ) ? ' options-panel-visible' : '';
524
525 // Get ID of post currently being edited.
526 $post_id = array_key_exists( 'post', $_REQUEST ) && intval( $_REQUEST['post'] ) > 0 ? intval( $_REQUEST['post'] ) : '';
527
528 // display: none is required here, see #WP27605
529 ?><div id="wp-insertpage-backdrop" style="display: none"></div>
530 <div id="wp-insertpage-wrap" class="wp-core-ui<?php echo $options_panel_visible; ?>" style="display: none">
531 <form id="wp-insertpage" tabindex="-1">
532 <?php wp_nonce_field( 'internal-inserting', '_ajax_inserting_nonce', false ); ?>
533 <input type="hidden" id="insertpage-parent-pageID" value="<?php echo $post_id; ?>" />
534 <div id="insertpage-modal-title">
535 <?php _e( 'Insert page' ) ?>
536 <div id="wp-insertpage-close" tabindex="0"></div>
537 </div>
538 <div id="insertpage-selector">
539 <div id="insertpage-search-panel">
540 <div class="insertpage-search-wrapper">
541 <label>
542 <span class="search-label"><?php _e( 'Search' ); ?></span>
543 <input type="search" id="insertpage-search-field" class="insertpage-search-field" autocomplete="off" />
544 <span class="spinner"></span>
545 </label>
546 </div>
547 <div id="insertpage-search-results" class="query-results">
548 <ul></ul>
549 <div class="river-waiting">
550 <span class="spinner"></span>
551 </div>
552 </div>
553 <div id="insertpage-most-recent-results" class="query-results">
554 <div class="query-notice"><em><?php _e( 'No search term specified. Showing recent items.' ); ?></em></div>
555 <ul></ul>
556 <div class="river-waiting">
557 <span class="spinner"></span>
558 </div>
559 </div>
560 </div>
561 <p class="howto" id="insertpage-options-toggle"><?php _e( 'Options' ); ?></p>
562 <div id="insertpage-options-panel">
563 <div class="insertpage-options-wrapper">
564 <label for="insertpage-slug-field">
565 <span><?php _e( 'Slug or ID' ); ?></span>
566 <input id="insertpage-slug-field" type="text" autocomplete="off" />
567 <input id="insertpage-pageID" type="hidden" />
568 </label>
569 </div>
570 <div class="insertpage-format">
571 <label for="insertpage-format-select">
572 <?php _e( 'Display' ); ?>
573 <select name="insertpage-format-select" id="insertpage-format-select">
574 <option value='title'>Title</option>
575 <option value='link'>Link</option>
576 <option value='excerpt'>Excerpt with title</option>
577 <option value='excerpt-only'>Excerpt only (no title)</option>
578 <option value='content'>Content</option>
579 <option value='all'>All (includes custom fields)</option>
580 <option value='template'>Use a custom template &raquo;</option>
581 </select>
582 <select name="insertpage-template-select" id="insertpage-template-select" disabled="true">
583 <option value='all'><?php _e( 'Default Template' ); ?></option>
584 <?php page_template_dropdown(); ?>
585 </select>
586 </label>
587 </div>
588 <div class="insertpage-extra">
589 <label for="insertpage-extra-classes">
590 <?php _e( 'Extra Classes' ); ?>
591 <input id="insertpage-extra-classes" type="text" autocomplete="off" />
592 </label>
593 <label for="insertpage-extra-inline">
594 <?php _e( 'Inline?' ); ?>
595 <input id="insertpage-extra-inline" type="checkbox" />
596 </label>
597 </div>
598 </div>
599 </div>
600 <div class="submitbox">
601 <div id="wp-insertpage-update">
602 <input type="submit" value="<?php esc_attr_e( 'Insert Page' ); ?>" class="button button-primary" id="wp-insertpage-submit" name="wp-insertpage-submit">
603 </div>
604 <div id="wp-insertpage-cancel">
605 <a class="submitdelete deletion" href="#"><?php _e( 'Cancel' ); ?></a>
606 </div>
607 </div>
608 </form>
609 </div>
610 <?php
611 }
612
613 /** Modified from:
614 * Internal linking functions.
615 *
616 * @package WordPress
617 * @subpackage Administration
618 * @since 3.1.0
619 */
620 function insertPages_insert_page_callback() {
621 check_ajax_referer( 'internal-inserting', '_ajax_inserting_nonce' );
622 $args = array();
623 if ( isset( $_POST['search'] ) ) {
624 $args['s'] = stripslashes( $_POST['search'] );
625 }
626 $args['pagenum'] = !empty( $_POST['page'] ) ? absint( $_POST['page'] ) : 1;
627 $args['pageID'] = !empty( $_POST['pageID'] ) ? absint( $_POST['pageID'] ) : 0;
628
629 // Change search to slug or post ID if we're not doing a plaintext
630 // search (e.g., if we're editing an existing shortcode and the
631 // search field is populated with the post's slug or ID).
632 if ( array_key_exists( 'type', $_POST ) && $_POST['type'] === 'slug' ) {
633 $args['name'] = $args['s'];
634 unset( $args['s'] );
635 } else if ( array_key_exists( 'type', $_POST ) && $_POST['type'] === 'post_id' ) {
636 $args['p'] = $args['s'];
637 unset( $args['s'] );
638 }
639
640 $results = $this->insertPages_wp_query( $args );
641
642 // Fail if our query didn't work.
643 if ( ! isset( $results ) ) {
644 die( '0' );
645 }
646
647 echo json_encode( $results );
648 echo "\n";
649 die();
650 }
651
652 /** Modified from:
653 * Performs post queries for internal linking.
654 *
655 * @since 3.1.0
656 * @param array $args Optional. Accepts 'pagenum' and 's' (search) arguments.
657 * @return array Results.
658 */
659 function insertPages_wp_query( $args = array() ) {
660 $pts = get_post_types( array( 'public' => true ), 'objects' );
661 $post_types = array_keys( $pts );
662
663 /**
664 * Filter the post types that appear in the list of pages to insert.
665 *
666 * By default, all post types will apear.
667 *
668 * @since 2.0
669 *
670 * @param array $post_types Array of post type names to include.
671 */
672 $post_types = apply_filters( 'insert_pages_available_post_types', $post_types );
673
674 $query = array(
675 'post_type' => $post_types,
676 'suppress_filters' => true,
677 'update_post_term_cache' => false,
678 'update_post_meta_cache' => false,
679 'post_status' => 'publish',
680 'order' => 'DESC',
681 'orderby' => 'post_date',
682 'posts_per_page' => 20,
683 'post__not_in' => array( $args['pageID'] ),
684 );
685
686 $args['pagenum'] = isset( $args['pagenum'] ) ? absint( $args['pagenum'] ) : 1;
687 $query['offset'] = $args['pagenum'] > 1 ? $query['posts_per_page'] * ( $args['pagenum'] - 1 ) : 0;
688
689 // Search post content and post title.
690 if ( isset( $args['s'] ) ) {
691 $query['s'] = $args['s'];
692 }
693
694 // Search post_name (post slugs).
695 if ( isset( $args['name'] ) ) {
696 $query['name'] = $args['name'];
697 }
698
699 // Search post ids.
700 if ( isset( $args['p'] ) ) {
701 $query['p'] = $args['p'];
702 }
703
704 // Do main query.
705 $get_posts = new WP_Query;
706 $posts = $get_posts->query( $query );
707 // Check if any posts were found.
708 if ( ! $get_posts->post_count ) {
709 return false;
710 }
711
712 // Build results.
713 $results = array();
714 foreach ( $posts as $post ) {
715 if ( 'post' == $post->post_type ) {
716 $info = mysql2date( __( 'Y/m/d' ), $post->post_date );
717 } else {
718 $info = $pts[ $post->post_type ]->labels->singular_name;
719 }
720 $results[] = array(
721 'ID' => $post->ID,
722 'title' => trim( esc_html( strip_tags( get_the_title( $post ) ) ) ),
723 'permalink' => get_permalink( $post->ID ),
724 'slug' => $post->post_name,
725 'info' => $info,
726 );
727 }
728 return $results;
729 }
730
731 function insertPages_add_quicktags() {
732 if ( wp_script_is( 'quicktags' ) ) : ?>
733 <script type="text/javascript">
734 QTags.addButton( 'ed_insert_page', '[insert page]', "[insert page='your-page-slug' display='title|link|excerpt|excerpt-only|content|all']\n", '', '', 'Insert Page', 999 );
735 </script>
736 <?php endif;
737 }
738
739 }
740 }
741
742 // Initialize InsertPagesPlugin object
743 if ( class_exists( 'InsertPagesPlugin' ) ) {
744 $insertPages_plugin = new InsertPagesPlugin();
745 }
746
747 // Actions and Filters handled by InsertPagesPlugin class
748 if ( isset( $insertPages_plugin ) ) {
749 // Register shortcode [insert ...].
750 add_action( 'init', array( $insertPages_plugin, 'insertPages_init' ), 1 );
751
752 // Add TinyMCE button for shortcode.
753 add_action( 'admin_head', array( $insertPages_plugin, 'insertPages_admin_init' ), 1 );
754
755 // Add quicktags button for shortcode.
756 add_action( 'admin_print_footer_scripts', array( $insertPages_plugin, 'insertPages_add_quicktags' ) );
757
758 // Preload TinyMCE popup.
759 add_action( 'before_wp_tiny_mce', array( $insertPages_plugin, 'insertPages_wp_tinymce_dialog' ), 1 );
760
761 // Ajax: Populate page search in TinyMCE button popup.
762 add_action( 'wp_ajax_insertpage', array( $insertPages_plugin, 'insertPages_insert_page_callback' ) );
763
764 // Use internal filter to wrap inserted content in a div or span.
765 add_filter( 'insert_pages_wrap_content', array( $insertPages_plugin, 'insertPages_wrap_content' ), 10, 3 );
766
767 // Register Insert Pages shortcode widget.
768 require_once( dirname( __FILE__ ) . '/widget.php' );
769 add_action( 'widgets_init', function () { return register_widget( 'InsertPagesWidget' ); } );
770 }
771