Insert Pages - Version 2.8

Version Description

  • Feature: Add options page with option to insert page IDs instead of page slugs (users of WPML will need this feature if translated pages all share the same page slug).
  • Feature: Inserted pages with Beaver Builder enabled now embed correctly.
  • Fix: TinyMCE toolbar button states (active, disabled) have been fixed.
  • Fix: TinyMCE cursor detection inside an existing shortcode has been fixed.
  • Fix: Expanded options in Insert Pages popup now correctly remembers last choice.
  • Fix: Restore missing spinners in search dialog.
  • Fix: prevent PHP warning when rendering wp_editor() outside of edit context. Props Jerry Benton.
Download this release

Release Info

Developer figureone
Plugin Icon wp plugin Insert Pages
Version 2.8
Comparing to
See all releases

Code changes from version 2.7.2 to 2.8

css/wpinsertpages.css CHANGED
@@ -168,6 +168,7 @@
168
  }
169
 
170
  #wp-insertpage .insertpage-search-wrapper .spinner {
 
171
  display: none;
172
  vertical-align: text-bottom;
173
  }
@@ -255,6 +256,7 @@
255
  #wp-insertpage .river-waiting .spinner {
256
  margin: 0 auto;
257
  display: block;
 
258
  float: none;
259
  }
260
 
168
  }
169
 
170
  #wp-insertpage .insertpage-search-wrapper .spinner {
171
+ visibility: visible;
172
  display: none;
173
  vertical-align: text-bottom;
174
  }
256
  #wp-insertpage .river-waiting .spinner {
257
  margin: 0 auto;
258
  display: block;
259
+ visibility: visible;
260
  float: none;
261
  }
262
 
insert-pages.php CHANGED
@@ -5,7 +5,7 @@ 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.7.2
9
  Author URI: http://www.linkedin.com/in/paulrryan
10
  License: GPL2
11
  */
@@ -38,7 +38,8 @@ if ( !class_exists( 'InsertPagesPlugin' ) ) {
38
 
39
  // Constructor
40
  public function InsertPagesPlugin() {
41
- //$this->pageID = '1'; echo $_GET['post'];
 
42
  }
43
 
44
  // Getter/Setter for pageID
@@ -56,6 +57,12 @@ if ( !class_exists( 'InsertPagesPlugin' ) ) {
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
 
@@ -64,7 +71,7 @@ if ( !class_exists( 'InsertPagesPlugin' ) ) {
64
  'wpinsertpages',
65
  plugins_url( '/js/wpinsertpages.js', __FILE__ ),
66
  array( 'wpdialogs' ),
67
- '20140819'
68
  );
69
  wp_localize_script(
70
  'wpinsertpages',
@@ -75,6 +82,7 @@ if ( !class_exists( 'InsertPagesPlugin' ) ) {
75
  'noTitle' => __( '(no title)' ),
76
  'noMatchesFound' => __( 'No matches found.' ),
77
  'l10n_print_after' => 'try{convertEntities(wpLinkL10n);}catch(e){};',
 
78
  )
79
  );
80
 
@@ -83,7 +91,7 @@ if ( !class_exists( 'InsertPagesPlugin' ) ) {
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' ) );
@@ -105,6 +113,12 @@ if ( !class_exists( 'InsertPagesPlugin' ) ) {
105
  'inline' => false,
106
  ), $atts ) );
107
 
 
 
 
 
 
 
108
  // Validation checks.
109
  if ( $page === '0' ) {
110
  return $content;
@@ -169,7 +183,7 @@ if ( !class_exists( 'InsertPagesPlugin' ) ) {
169
  */
170
  $should_apply_the_content_filter = apply_filters( 'insert_pages_apply_the_content_filter', $should_apply_the_content_filter );
171
 
172
- $should_use_inline_wrapper = ( $inline !== false && $inline !== 'false' ) || array_search( 'inline', $atts ) === 0;
173
  /**
174
  * Filter the flag indicating whether to wrap the inserted content in inline tags (span).
175
  *
@@ -187,6 +201,16 @@ if ( !class_exists( 'InsertPagesPlugin' ) ) {
187
  if ( have_posts() ) {
188
  ob_start(); // Start output buffering so we can save the output to string
189
 
 
 
 
 
 
 
 
 
 
 
190
  // Show either the title, link, content, everything, or everything via a custom template
191
  // Note: if the sharing_display filter exists, it means Jetpack is installed and Sharing is enabled;
192
  // This plugin conflicts with Sharing, because Sharing assumes the_content and the_excerpt filters
@@ -285,14 +309,17 @@ if ( !class_exists( 'InsertPagesPlugin' ) ) {
285
  return;
286
  }
287
 
288
- $options_panel_visible = '1' == get_user_setting( 'wplink', '0' ) ? ' options-panel-visible' : '';
 
 
 
289
 
290
  // display: none is required here, see #WP27605
291
  ?><div id="wp-insertpage-backdrop" style="display: none"></div>
292
  <div id="wp-insertpage-wrap" class="wp-core-ui<?php echo $options_panel_visible; ?>" style="display: none">
293
  <form id="wp-insertpage" tabindex="-1">
294
  <?php wp_nonce_field( 'internal-inserting', '_ajax_inserting_nonce', false ); ?>
295
- <input type="hidden" id="insertpage-parent-pageID" value="<?php echo $_GET['post'] ?>" />
296
  <div id="insertpage-modal-title">
297
  <?php _e( 'Insert page' ) ?>
298
  <div id="wp-insertpage-close" tabindex="0"></div>
@@ -378,11 +405,24 @@ if ( !class_exists( 'InsertPagesPlugin' ) ) {
378
  $args['pagenum'] = !empty( $_POST['page'] ) ? absint( $_POST['page'] ) : 1;
379
  $args['pageID'] = !empty( $_POST['pageID'] ) ? absint( $_POST['pageID'] ) : 0;
380
 
 
 
 
 
 
 
 
 
 
 
 
381
  $results = $this->insertPages_wp_query( $args );
382
 
383
- if ( !isset( $results ) ) {
 
384
  die( '0' );
385
  }
 
386
  echo json_encode( $results );
387
  echo "\n";
388
  die();
@@ -423,12 +463,22 @@ if ( !class_exists( 'InsertPagesPlugin' ) ) {
423
  );
424
 
425
  $args['pagenum'] = isset( $args['pagenum'] ) ? absint( $args['pagenum'] ) : 1;
 
426
 
 
427
  if ( isset( $args['s'] ) ) {
428
  $query['s'] = $args['s'];
429
  }
430
 
431
- $query['offset'] = $args['pagenum'] > 1 ? $query['posts_per_page'] * ( $args['pagenum'] - 1 ) : 0;
 
 
 
 
 
 
 
 
432
 
433
  // Do main query.
434
  $get_posts = new WP_Query;
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.8
9
  Author URI: http://www.linkedin.com/in/paulrryan
10
  License: GPL2
11
  */
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
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 ) {
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
 
71
  'wpinsertpages',
72
  plugins_url( '/js/wpinsertpages.js', __FILE__ ),
73
  array( 'wpdialogs' ),
74
+ '20151022'
75
  );
76
  wp_localize_script(
77
  'wpinsertpages',
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
 
91
  'wpinsertpagescss',
92
  plugins_url( '/css/wpinsertpages.css', __FILE__ ),
93
  array( 'wp-jquery-ui-dialog' ),
94
+ '20151022'
95
  );
96
 
97
  add_filter( 'mce_external_plugins', array( $this, 'insertPages_handleFilter_mceExternalPlugins' ) );
113
  'inline' => false,
114
  ), $atts ) );
115
 
116
+ // Get options set in WordPress dashboard (Settings > Insert Pages).
117
+ $options = get_option( 'wpip_settings' );
118
+ if ( $options === FALSE ) {
119
+ $options = wpip_set_defaults();
120
+ }
121
+
122
  // Validation checks.
123
  if ( $page === '0' ) {
124
  return $content;
183
  */
184
  $should_apply_the_content_filter = apply_filters( 'insert_pages_apply_the_content_filter', $should_apply_the_content_filter );
185
 
186
+ $should_use_inline_wrapper = ( $inline !== false && $inline !== 'false' ) || array_search( 'inline', $atts ) === 0 || ( array_key_exists( 'wpip_wrapper', $options ) && $options['wpip_wrapper'] === 'inline' );
187
  /**
188
  * Filter the flag indicating whether to wrap the inserted content in inline tags (span).
189
  *
201
  if ( have_posts() ) {
202
  ob_start(); // Start output buffering so we can save the output to string
203
 
204
+ // If Beaver Builder plugin is enabled, load any cached styles associated with the inserted page.
205
+ // Note: Temporarily set the global $post->ID to the inserted page ID,
206
+ // since Beaver Builder relies on it to load the appropraite styles.
207
+ if ( class_exists( 'FLBuilder' ) ) {
208
+ $old_post_id = $post->ID;
209
+ $post->ID = $page;
210
+ FLBuilder::enqueue_layout_styles_scripts( $page );
211
+ $post->ID = $old_post_id;
212
+ }
213
+
214
  // Show either the title, link, content, everything, or everything via a custom template
215
  // Note: if the sharing_display filter exists, it means Jetpack is installed and Sharing is enabled;
216
  // This plugin conflicts with Sharing, because Sharing assumes the_content and the_excerpt filters
309
  return;
310
  }
311
 
312
+ $options_panel_visible = '1' == get_user_setting( 'wpinsertpage', '0' ) ? ' options-panel-visible' : '';
313
+
314
+ // Get ID of post currently being edited.
315
+ $post_id = array_key_exists( 'post', $_REQUEST ) && intval( $_REQUEST['post'] ) > 0 ? intval( $_REQUEST['post'] ) : '';
316
 
317
  // display: none is required here, see #WP27605
318
  ?><div id="wp-insertpage-backdrop" style="display: none"></div>
319
  <div id="wp-insertpage-wrap" class="wp-core-ui<?php echo $options_panel_visible; ?>" style="display: none">
320
  <form id="wp-insertpage" tabindex="-1">
321
  <?php wp_nonce_field( 'internal-inserting', '_ajax_inserting_nonce', false ); ?>
322
+ <input type="hidden" id="insertpage-parent-pageID" value="<?php echo $post_id; ?>" />
323
  <div id="insertpage-modal-title">
324
  <?php _e( 'Insert page' ) ?>
325
  <div id="wp-insertpage-close" tabindex="0"></div>
405
  $args['pagenum'] = !empty( $_POST['page'] ) ? absint( $_POST['page'] ) : 1;
406
  $args['pageID'] = !empty( $_POST['pageID'] ) ? absint( $_POST['pageID'] ) : 0;
407
 
408
+ // Change search to slug or post ID if we're not doing a plaintext
409
+ // search (e.g., if we're editing an existing shortcode and the
410
+ // search field is populated with the post's slug or ID).
411
+ if ( array_key_exists( 'type', $_POST ) && $_POST['type'] === 'slug' ) {
412
+ $args['name'] = $args['s'];
413
+ unset( $args['s'] );
414
+ } else if ( array_key_exists( 'type', $_POST ) && $_POST['type'] === 'post_id' ) {
415
+ $args['p'] = $args['s'];
416
+ unset( $args['s'] );
417
+ }
418
+
419
  $results = $this->insertPages_wp_query( $args );
420
 
421
+ // Fail if our query didn't work.
422
+ if ( ! isset( $results ) ) {
423
  die( '0' );
424
  }
425
+
426
  echo json_encode( $results );
427
  echo "\n";
428
  die();
463
  );
464
 
465
  $args['pagenum'] = isset( $args['pagenum'] ) ? absint( $args['pagenum'] ) : 1;
466
+ $query['offset'] = $args['pagenum'] > 1 ? $query['posts_per_page'] * ( $args['pagenum'] - 1 ) : 0;
467
 
468
+ // Search post content and post title.
469
  if ( isset( $args['s'] ) ) {
470
  $query['s'] = $args['s'];
471
  }
472
 
473
+ // Search post_name (post slugs).
474
+ if ( isset( $args['name'] ) ) {
475
+ $query['name'] = $args['name'];
476
+ }
477
+
478
+ // Search post ids.
479
+ if ( isset( $args['p'] ) ) {
480
+ $query['p'] = $args['p'];
481
+ }
482
 
483
  // Do main query.
484
  $get_posts = new WP_Query;
js/wpinsertpages.js CHANGED
@@ -58,6 +58,12 @@ var wpInsertPages;
58
  }
59
  });
60
 
 
 
 
 
 
 
61
  inputs.search.keyup( function() {
62
  var self = this;
63
 
@@ -80,7 +86,7 @@ var wpInsertPages;
80
 
81
  open: function( editorId ) {
82
  var ed, node, bookmark, cursorPosition = -1;
83
-
84
  wpInsertPages.range = null;
85
 
86
  if ( editorId ) {
@@ -150,7 +156,7 @@ var wpInsertPages;
150
  },
151
 
152
  mceRefresh: function( cursorPosition ) {
153
- var shortcode, bookmark, regexp, match, matches;
154
 
155
  // Get the existing shortcode the cursor is in (or get the entire node if cursor not in one)
156
  shortcode = '';
@@ -162,7 +168,7 @@ var wpInsertPages;
162
  while ( ( match = regexp.exec( content ) ) != null ) {
163
  startPos = match.index;
164
  endPos = startPos + match[0].length;
165
- if ( cursorPosition > startPos && cursorPosition < endPos ) {
166
  shortcode = match[0];
167
  break;
168
  }
@@ -179,28 +185,38 @@ var wpInsertPages;
179
  for ( i = 0; i < node.childNodes.length; i++ ) {
180
  selectedChild = node.childNodes[i];
181
  length = ( selectedChild.outerHTML ) ? selectedChild.outerHTML.length : selectedChild.textContent.length;
182
- if ( cursorPosition < offset + length ) {
183
  break;
184
  }
185
  offset += length;
186
  }
187
- range.setStart( selectedChild, startPos - offset );
188
- range.setEnd( selectedChild, endPos - offset );
189
- editor.selection.setRng( range );
 
 
190
 
191
  // Set slug/id (also set the slug as the search term)
192
  regexp = /page=['"]([^['"]*)['"]/;
193
  matches = regexp.exec( shortcode );
194
  if ( matches.length > 1 ) {
 
 
 
 
 
 
 
195
  inputs.slug.val( matches[1] );
196
  inputs.search.val( matches[1] );
197
  inputs.search.keyup();
198
  }
199
 
 
200
  regexp = /display=['"]([^['"]*)['"]/;
201
  matches = regexp.exec( shortcode );
202
  if ( matches.length > 1 ) {
203
- if ( matches[1] == 'title' || matches[1] == 'link' || matches[1] == 'content' || matches[1] == 'all' ) {
204
  inputs.format.val( matches[1] );
205
  inputs.template.val( 'all' );
206
  } else {
@@ -228,6 +244,7 @@ var wpInsertPages;
228
  inputs.format.change();
229
  inputs.template.val('all');
230
  inputs.search.val( '' );
 
231
  inputs.search.keyup();
232
  },
233
 
@@ -286,7 +303,11 @@ var wpInsertPages;
286
  },
287
 
288
  updateFields : function( e, li, originalEvent ) {
289
- inputs.slug.val( li.children('.item-slug').val() );
 
 
 
 
290
  inputs.pageID.val( li.children('.item-id').val() );
291
  if ( originalEvent && originalEvent.type == "click" )
292
  inputs.slug.focus();
@@ -294,9 +315,10 @@ var wpInsertPages;
294
 
295
  searchInternalLinks : function() {
296
  var t = $(this), waiting,
297
- search = t.val();
 
298
 
299
- if ( search.length > 2 ) {
300
  rivers.recent.hide();
301
  rivers.search.show();
302
 
@@ -307,7 +329,7 @@ var wpInsertPages;
307
  wpInsertPages.lastSearch = search;
308
  waiting = t.parent().find( '.spinner' ).show();
309
 
310
- rivers.search.change( search );
311
  rivers.search.ajax( function() {
312
  waiting.hide();
313
  });
@@ -394,12 +416,13 @@ var wpInsertPages;
394
 
395
  RiverInsertPages = function( element, search ) {
396
  var self = this;
 
397
  this.element = element;
398
  this.ul = element.children( 'ul' );
399
  this.contentHeight = element.children( '#link-selector-height' );
400
  this.waiting = element.find('.river-waiting');
401
 
402
- this.change( search );
403
  this.refresh();
404
 
405
  $( '#wp-insertpage .query-results, #wp-insertpage #link-selector' ).scroll( function() {
@@ -441,10 +464,11 @@ var wpInsertPages;
441
  liTop = li.position().top;
442
  elTop = this.element.scrollTop();
443
 
444
- if ( liTop < 0 ) // Make first visible element
445
  this.element.scrollTop( elTop + liTop );
446
- else if ( liTop + liHeight > elHeight ) // Make last visible element
447
  this.element.scrollTop( elTop + liTop - elHeight + liHeight );
 
448
 
449
  // Trigger the river-select event
450
  this.element.trigger('river-select', [ li, event, this ]);
@@ -484,12 +508,12 @@ var wpInsertPages;
484
 
485
  this.query.ajax( response );
486
  },
487
- change: function( search ) {
488
  if ( this.query && this._search == search )
489
  return;
490
 
491
  this._search = search;
492
- this.query = new QueryInsertPages( search );
493
  this.element.scrollTop(0);
494
  },
495
  process: function( results, params ) {
@@ -505,14 +529,14 @@ var wpInsertPages;
505
  } else {
506
  $.each( results, function() {
507
  classes = alt ? 'alternate' : '';
508
- classes += this['title'] ? '' : ' no-title';
509
  list += classes ? '<li class="' + classes + '">' : '<li>';
510
- list += '<input type="hidden" class="item-permalink" value="' + this['permalink'] + '" />';
511
- list += '<input type="hidden" class="item-slug" value="' + this['slug'] + '" />';
512
- list += '<input type="hidden" class="item-id" value="' + this['ID'] + '" />';
513
  list += '<span class="item-title">';
514
- list += this['title'] ? this['title'] : wpInsertPagesL10n.noTitle;
515
- list += '</span><span class="item-info">' + this['info'] + '</span></li>';
516
  alt = ! alt;
517
  });
518
  }
@@ -544,11 +568,12 @@ var wpInsertPages;
544
  }
545
  });
546
 
547
- QueryInsertPages = function( search ) {
548
  this.page = 1;
549
  this.allLoaded = false;
550
  this.querying = false;
551
  this.search = search;
 
552
  };
553
 
554
  $.extend( QueryInsertPages.prototype, {
@@ -558,16 +583,16 @@ var wpInsertPages;
558
  ajax: function( callback ) {
559
  var self = this,
560
  query = {
561
- //action : 'wp-insertpage-ajax',
562
  action : 'insertpage',
563
  page : this.page,
 
564
  '_ajax_inserting_nonce' : $('#_ajax_inserting_nonce').val()
565
  };
566
 
567
  if ( this.search )
568
  query.search = this.search;
569
-
570
- query.pageID = inputs.pageID.val();
571
 
572
  this.querying = true;
573
  $.post( ajaxurl, query, function(r) {
58
  }
59
  });
60
 
61
+ // Set search type to plaintext if someone types in the search field.
62
+ // (Might have been set to 'slug' or 'id' if editing a current shortcode.)
63
+ inputs.search.keydown( function () {
64
+ inputs.search.data( 'type', 'text' );
65
+ });
66
+
67
  inputs.search.keyup( function() {
68
  var self = this;
69
 
86
 
87
  open: function( editorId ) {
88
  var ed, node, bookmark, cursorPosition = -1;
89
+
90
  wpInsertPages.range = null;
91
 
92
  if ( editorId ) {
156
  },
157
 
158
  mceRefresh: function( cursorPosition ) {
159
+ var shortcode, bookmark, regexp, match, matches, offset;
160
 
161
  // Get the existing shortcode the cursor is in (or get the entire node if cursor not in one)
162
  shortcode = '';
168
  while ( ( match = regexp.exec( content ) ) != null ) {
169
  startPos = match.index;
170
  endPos = startPos + match[0].length;
171
+ if ( cursorPosition >= startPos && cursorPosition <= endPos ) {
172
  shortcode = match[0];
173
  break;
174
  }
185
  for ( i = 0; i < node.childNodes.length; i++ ) {
186
  selectedChild = node.childNodes[i];
187
  length = ( selectedChild.outerHTML ) ? selectedChild.outerHTML.length : selectedChild.textContent.length;
188
+ if ( cursorPosition <= offset + length ) {
189
  break;
190
  }
191
  offset += length;
192
  }
193
+ if ( selectedChild.length >= offset ) {
194
+ range.setStart( selectedChild, startPos - offset );
195
+ range.setEnd( selectedChild, endPos - offset );
196
+ editor.selection.setRng( range );
197
+ }
198
 
199
  // Set slug/id (also set the slug as the search term)
200
  regexp = /page=['"]([^['"]*)['"]/;
201
  matches = regexp.exec( shortcode );
202
  if ( matches.length > 1 ) {
203
+ // Indicate that this search term is a slug or id.
204
+ if ( isNaN( parseInt( matches[1] ) ) ) {
205
+ inputs.search.data( 'type', 'slug' );
206
+ } else {
207
+ inputs.search.data( 'type', 'post_id' );
208
+ }
209
+
210
  inputs.slug.val( matches[1] );
211
  inputs.search.val( matches[1] );
212
  inputs.search.keyup();
213
  }
214
 
215
+ // Update display dropdown to match the selected shortcode.
216
  regexp = /display=['"]([^['"]*)['"]/;
217
  matches = regexp.exec( shortcode );
218
  if ( matches.length > 1 ) {
219
+ if ( ['title', 'link', 'excerpt', 'excerpt-only', 'content', 'all', ].indexOf( matches[1] ) >= 0 ) {
220
  inputs.format.val( matches[1] );
221
  inputs.template.val( 'all' );
222
  } else {
244
  inputs.format.change();
245
  inputs.template.val('all');
246
  inputs.search.val( '' );
247
+ inputs.search.data( 'type', 'text' );
248
  inputs.search.keyup();
249
  },
250
 
303
  },
304
 
305
  updateFields : function( e, li, originalEvent ) {
306
+ if ( wpInsertPagesL10n.format === 'post_id' ) {
307
+ inputs.slug.val( li.children('.item-id').val() );
308
+ } else {
309
+ inputs.slug.val( li.children('.item-slug').val() );
310
+ }
311
  inputs.pageID.val( li.children('.item-id').val() );
312
  if ( originalEvent && originalEvent.type == "click" )
313
  inputs.slug.focus();
315
 
316
  searchInternalLinks : function() {
317
  var t = $(this), waiting,
318
+ search = t.val(),
319
+ type = t.data( 'type' );
320
 
321
+ if ( search.length > 2 || ( type === 'post_id' && search.length > 0 ) ) {
322
  rivers.recent.hide();
323
  rivers.search.show();
324
 
329
  wpInsertPages.lastSearch = search;
330
  waiting = t.parent().find( '.spinner' ).show();
331
 
332
+ rivers.search.change( search, type );
333
  rivers.search.ajax( function() {
334
  waiting.hide();
335
  });
416
 
417
  RiverInsertPages = function( element, search ) {
418
  var self = this;
419
+ var type = 'text';
420
  this.element = element;
421
  this.ul = element.children( 'ul' );
422
  this.contentHeight = element.children( '#link-selector-height' );
423
  this.waiting = element.find('.river-waiting');
424
 
425
+ this.change( search, type );
426
  this.refresh();
427
 
428
  $( '#wp-insertpage .query-results, #wp-insertpage #link-selector' ).scroll( function() {
464
  liTop = li.position().top;
465
  elTop = this.element.scrollTop();
466
 
467
+ if ( liTop < 0 ) { // Make first visible element
468
  this.element.scrollTop( elTop + liTop );
469
+ } else if ( liTop + liHeight > elHeight ) { // Make last visible element
470
  this.element.scrollTop( elTop + liTop - elHeight + liHeight );
471
+ }
472
 
473
  // Trigger the river-select event
474
  this.element.trigger('river-select', [ li, event, this ]);
508
 
509
  this.query.ajax( response );
510
  },
511
+ change: function( search, type ) {
512
  if ( this.query && this._search == search )
513
  return;
514
 
515
  this._search = search;
516
+ this.query = new QueryInsertPages( search, type );
517
  this.element.scrollTop(0);
518
  },
519
  process: function( results, params ) {
529
  } else {
530
  $.each( results, function() {
531
  classes = alt ? 'alternate' : '';
532
+ classes += this.title ? '' : ' no-title';
533
  list += classes ? '<li class="' + classes + '">' : '<li>';
534
+ list += '<input type="hidden" class="item-permalink" value="' + this.permalink + '" />';
535
+ list += '<input type="hidden" class="item-slug" value="' + this.slug + '" />';
536
+ list += '<input type="hidden" class="item-id" value="' + this.ID + '" />';
537
  list += '<span class="item-title">';
538
+ list += this.title ? this.title : wpInsertPagesL10n.noTitle;
539
+ list += '</span><span class="item-info">' + this.info + '</span></li>';
540
  alt = ! alt;
541
  });
542
  }
568
  }
569
  });
570
 
571
+ QueryInsertPages = function( search, type ) {
572
  this.page = 1;
573
  this.allLoaded = false;
574
  this.querying = false;
575
  this.search = search;
576
+ this.type = type;
577
  };
578
 
579
  $.extend( QueryInsertPages.prototype, {
583
  ajax: function( callback ) {
584
  var self = this,
585
  query = {
 
586
  action : 'insertpage',
587
  page : this.page,
588
+ type : this.type,
589
  '_ajax_inserting_nonce' : $('#_ajax_inserting_nonce').val()
590
  };
591
 
592
  if ( this.search )
593
  query.search = this.search;
594
+
595
+ query.pageID = inputs.pageID.val();
596
 
597
  this.querying = true;
598
  $.post( ajaxurl, query, function(r) {
js/wpinsertpages_plugin.js CHANGED
@@ -23,32 +23,19 @@
23
 
24
  function setState( button, node ) {
25
  var parentIsShortcode = false,
26
- bookmark, cursorPosition, regexp, match, startPos, endPos,
27
- parentIsAnchor = editor.dom.getParent( node, 'a' ),
28
- parentIsImg = editor.dom.getParent( node, 'img' );
29
-
30
- // Get whether cursor is in an existing shortcode
31
- content = node.innerHTML;
32
- if ( content.indexOf( '[insert page=' ) >= 0 ) {
33
- // Find the cursor position in the current node.
34
- bookmark = editor.selection.getBookmark( 0 );
35
- cursorPosition = node.innerHTML.indexOf( '<span data-mce-type="bookmark"' );
36
- editor.selection.moveToBookmark( bookmark );
37
-
38
- // Find occurrences of shortcode in current node and see if the cursor
39
- // position is inside one of them.
40
- regexp = /\[insert page=[^\]]*]/g;
41
- while ( ( match = regexp.exec( content ) ) != null ) {
42
- startPos = match.index;
43
- endPos = startPos + match[0].length;
44
- if ( cursorPosition > startPos && cursorPosition < endPos ) {
45
- parentIsShortcode = true;
46
- break;
47
- }
48
- }
49
  }
50
 
51
- button.disabled( parentIsAnchor || parentIsImg );
52
  button.active( parentIsShortcode );
53
  }
54
 
23
 
24
  function setState( button, node ) {
25
  var parentIsShortcode = false,
26
+ elementContainingCursor = editor.selection.getNode(),
27
+ cursorOffsetWithinElement = editor.selection.getRng(),
28
+ indexOfShortcodeStart = elementContainingCursor.innerHTML.indexOf( '[insert page=' ),
29
+ indexOfShortcodeEnd = elementContainingCursor.innerHTML.indexOf( ']', indexOfShortcodeStart ),
30
+ parentAnchor = editor.dom.getParent( node, 'a' ),
31
+ parentImg = editor.dom.getParent( node, 'img' );
32
+
33
+ // Determine if cursor is in an existing shortcode.
34
+ if ( indexOfShortcodeStart >= 0 && indexOfShortcodeStart <= cursorOffsetWithinElement.startOffset && indexOfShortcodeEnd + 1 >= cursorOffsetWithinElement.endOffset ) {
35
+ parentIsShortcode = true;
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  }
37
 
38
+ button.disabled( parentAnchor !== null || parentImg !== null );
39
  button.active( parentIsShortcode );
40
  }
41
 
options.php ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ function wpip_add_admin_menu() {
4
+ add_options_page( 'Insert Pages', 'Insert Pages', 'manage_options', 'insert_pages', 'wpip_options_page' );
5
+ }
6
+ add_action( 'admin_menu', 'wpip_add_admin_menu' );
7
+
8
+
9
+ function wpip_settings_init() {
10
+ register_setting( 'wpipSettings', 'wpip_settings' );
11
+ add_settings_section(
12
+ 'wpip_section',
13
+ __( 'Insert Pages', 'wordpress' ),
14
+ 'wpip_settings_section_callback',
15
+ 'wpipSettings'
16
+ );
17
+ add_settings_field(
18
+ 'wpip_format',
19
+ __( 'Shortcode format', 'wordpress' ),
20
+ 'wpip_format_render',
21
+ 'wpipSettings',
22
+ 'wpip_section'
23
+ );
24
+ add_settings_field(
25
+ 'wpip_wrapper',
26
+ __( 'Wrapper for inserts', 'wordpress' ),
27
+ 'wpip_wrapper_render',
28
+ 'wpipSettings',
29
+ 'wpip_section'
30
+ );
31
+ }
32
+ add_action( 'admin_init', 'wpip_settings_init' );
33
+
34
+
35
+ function wpip_set_defaults() {
36
+ $options = get_option( 'wpip_settings' );
37
+ if ( $options === FALSE ) {
38
+ $options = array();
39
+ }
40
+
41
+ if ( ! array_key_exists( 'wpip_format', $options ) ) {
42
+ $options['wpip_format'] = 'slug';
43
+ }
44
+
45
+ if ( ! array_key_exists( 'wpip_wrapper', $options ) ) {
46
+ $options['wpip_wrapper'] = 'block';
47
+ }
48
+
49
+ update_option( 'wpip_settings', $options );
50
+
51
+ return $options;
52
+ }
53
+ register_activation_hook( __FILE__, 'wpip_set_defaults' );
54
+
55
+
56
+ function wpip_settings_section_callback() {
57
+ echo __( 'You may override some default settings here.', 'wordpress' );
58
+ }
59
+
60
+
61
+ function wpip_options_page() {
62
+ ?>
63
+ <form action='options.php' method='post'>
64
+ <?php
65
+ settings_fields( 'wpipSettings' );
66
+ do_settings_sections( 'wpipSettings' );
67
+ submit_button();
68
+ ?>
69
+ </form>
70
+ <?php
71
+ }
72
+
73
+
74
+ function wpip_format_render() {
75
+ $options = get_option( 'wpip_settings' );
76
+ if ( $options === FALSE ) {
77
+ $options = wpip_set_defaults();
78
+ }
79
+ ?>
80
+ <input type='radio' name='wpip_settings[wpip_format]' <?php checked( $options['wpip_format'], 'slug' ); ?> id="wpip_format_slug" value='slug'><label for="wpip_format_slug">Use page slugs (more readable). Example: <code>[insert&nbsp;page='hello&#8209;world&#8209;post'&nbsp;display='all']</code></label><br />
81
+ <input type='radio' name='wpip_settings[wpip_format]' <?php checked( $options['wpip_format'], 'post_id' ); ?> id="wpip_format_id" value='post_id'><label for="wpip_format_id">Use page IDs (more compatible). Example: <code>[insert&nbsp;page='1'&nbsp;display='all']</code></label><br />
82
+ <small><em>If your site reuses page slugs (for example, WPML sites often use the same page slug for each translation of the page in a different language), you should use page IDs.</em></small>
83
+ <?php
84
+ }
85
+
86
+
87
+ function wpip_wrapper_render() {
88
+ $options = get_option( 'wpip_settings' );
89
+ if ( $options === FALSE ) {
90
+ $options = wpip_set_defaults();
91
+ }
92
+ ?>
93
+ <input type='radio' name='wpip_settings[wpip_wrapper]' <?php checked( $options['wpip_wrapper'], 'block' ); ?> id="wpip_wrapper_block" value='block'><label for="wpip_wrapper_block">Use block wrapper (div). Example: <code>&lt;div data-post-id="1" class="insert-page">...&lt;/div></code></label><br />
94
+ <input type='radio' name='wpip_settings[wpip_wrapper]' <?php checked( $options['wpip_wrapper'], 'inline' ); ?> id="wpip_wrapper_inline" value='inline'><label for="wpip_wrapper_inline">Use inline wrapper (span). Example: <code>&lt;span data-post-id="1" class="insert-page">...&lt;/span></code></label><br />
95
+ <small><em>If you want to embed pages inline (for example, you can insert a link to a page in the flow of a normal paragraph), you should use inline tags. Note that the HTML spec does not allow block level elements within inline elements, so the inline wrapper has limited use.</em></small>
96
+ <?php
97
+ }
readme.txt CHANGED
@@ -86,12 +86,21 @@ Just one! The plugin prevents you from embedding a page in itself, but you can t
86
 
87
  == Changelog ==
88
 
 
 
 
 
 
 
 
 
 
89
  = 2.7.2 =
90
  * Add shortcode attribute to wrap inserted content in an inline element (span) instead of a block level element (div). Example usage:
91
  `Lorem ipsum [insert page='my-page' display='content' inline] dolor sit amet.`
92
  * Add filter to wrap inserted content in an inline element (span) instead of a block level element (div). Example usage:
93
  `function theme_init() {
94
- // Show a message in place of an inserted page if that page cannot be found.
95
  add_filter( 'insert_pages_use_inline_wrapper', function ( $should_use_inline_wrapper ) { return true; } );
96
  }
97
  add_action( 'init', 'theme_init' );`
86
 
87
  == Changelog ==
88
 
89
+ = 2.8 =
90
+ * Feature: Add options page with option to insert page IDs instead of page slugs (users of WPML will need this feature if translated pages all share the same page slug).
91
+ * Feature: Inserted pages with Beaver Builder enabled now embed correctly.
92
+ * Fix: TinyMCE toolbar button states (active, disabled) have been fixed.
93
+ * Fix: TinyMCE cursor detection inside an existing shortcode has been fixed.
94
+ * Fix: Expanded options in Insert Pages popup now correctly remembers last choice.
95
+ * Fix: Restore missing spinners in search dialog.
96
+ * Fix: prevent PHP warning when rendering wp_editor() outside of edit context. Props Jerry Benton.
97
+
98
  = 2.7.2 =
99
  * Add shortcode attribute to wrap inserted content in an inline element (span) instead of a block level element (div). Example usage:
100
  `Lorem ipsum [insert page='my-page' display='content' inline] dolor sit amet.`
101
  * Add filter to wrap inserted content in an inline element (span) instead of a block level element (div). Example usage:
102
  `function theme_init() {
103
+ // Wrap all inserted content in inline elements (span).
104
  add_filter( 'insert_pages_use_inline_wrapper', function ( $should_use_inline_wrapper ) { return true; } );
105
  }
106
  add_action( 'init', 'theme_init' );`