Custom Permalinks - Version 1.4.0

Version Description

  • Nov 08, 2018 =

    • Enhancement
    • Added Support for Gutenberg
    • Set meta_keys to be protected to stop duplication in Custom Fields
Download this release

Release Info

Developer sasiddiqui
Plugin Icon Custom Permalinks
Version 1.4.0
Comparing to
See all releases

Code changes from version 1.3.0 to 1.4.0

custom-permalinks.php CHANGED
@@ -3,7 +3,7 @@
3
  * Plugin Name: Custom Permalinks
4
  * Plugin URI: https://wordpress.org/plugins/custom-permalinks/
5
  * Description: Set custom permalinks on a per-post basis
6
- * Version: 1.3.0
7
  * Author: Sami Ahmed Siddiqui
8
  * Author URI: https://www.custompermalinks.com/
9
  * Donate link: https://www.paypal.me/yasglobal
@@ -64,7 +64,7 @@ class Custom_Permalinks {
64
  }
65
 
66
  if ( ! defined( 'CUSTOM_PERMALINKS_PLUGIN_VERSION' ) ) {
67
- define( 'CUSTOM_PERMALINKS_PLUGIN_VERSION', '1.3.0' );
68
  }
69
 
70
  if ( ! defined( 'CUSTOM_PERMALINKS_PATH' ) ) {
3
  * Plugin Name: Custom Permalinks
4
  * Plugin URI: https://wordpress.org/plugins/custom-permalinks/
5
  * Description: Set custom permalinks on a per-post basis
6
+ * Version: 1.4.0
7
  * Author: Sami Ahmed Siddiqui
8
  * Author URI: https://www.custompermalinks.com/
9
  * Donate link: https://www.paypal.me/yasglobal
64
  }
65
 
66
  if ( ! defined( 'CUSTOM_PERMALINKS_PLUGIN_VERSION' ) ) {
67
+ define( 'CUSTOM_PERMALINKS_PLUGIN_VERSION', '1.4.0' );
68
  }
69
 
70
  if ( ! defined( 'CUSTOM_PERMALINKS_PATH' ) ) {
frontend/class-custom-permalinks-form.php CHANGED
@@ -5,6 +5,8 @@
5
 
6
  class Custom_Permalinks_Form {
7
 
 
 
8
  /**
9
  * Initialize WordPress Hooks
10
  *
@@ -18,6 +20,14 @@ class Custom_Permalinks_Form {
18
  array( $this, 'custom_permalinks_get_sample_permalink_html' ), 10, 4
19
  );
20
 
 
 
 
 
 
 
 
 
21
  add_action( 'save_post',
22
  array( $this, 'custom_permalinks_save_post' ), 10, 3
23
  );
@@ -55,6 +65,47 @@ class Custom_Permalinks_Form {
55
  );
56
  }
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  /**
59
  * Save per-post options
60
  *
@@ -99,8 +150,8 @@ class Custom_Permalinks_Form {
99
  * @return string
100
  */
101
  public function custom_permalinks_get_sample_permalink_html( $html, $id, $new_title, $new_slug ) {
102
- $permalink = get_post_meta( $id, 'custom_permalink', true );
103
- $post = get_post( $id );
104
 
105
  if ( 'attachment' == $post->post_type || $post->ID == get_option( 'page_on_front' ) ) {
106
  return $html;
@@ -111,25 +162,23 @@ class Custom_Permalinks_Form {
111
  if ( '__true' === $excluded ) {
112
  return $html;
113
  }
 
114
 
115
  ob_start();
116
 
117
  $cp_frontend = new Custom_Permalinks_Frontend();
118
  if ( 'page' == $post->post_type ) {
119
- $original_page_url = $cp_frontend->custom_permalinks_original_page_link( $id );
120
- $this->custom_permalinks_get_form( $permalink, $original_page_url, false, $post->post_name );
121
  } else {
122
- $original_post_url = $cp_frontend->custom_permalinks_original_post_link( $id );
123
- $this->custom_permalinks_get_form( $permalink, $original_post_url, false, $post->post_name );
124
  }
 
125
 
126
  $content = ob_get_contents();
127
  ob_end_clean();
128
 
129
- if ( 'publish' == $post->post_status ) {
130
- $view_post = 'page' == $post->post_type ? __( 'View Page', 'custom-permalinks' ) : __( 'View Post', 'custom-permalinks' );
131
- }
132
-
133
  if ( preg_match( "@view-post-btn.*?href='([^']+)'@s", $html, $matches ) ) {
134
  $permalink = $matches[1];
135
  } else {
@@ -144,6 +193,78 @@ class Custom_Permalinks_Form {
144
  ( isset( $view_post ) ? "<span id='view-post-btn'><a href='$permalink' class='button button-small' target='_blank'>$view_post</a></span>\n" : "" );
145
  }
146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  /**
148
  * Per-post options (Wordpress < 2.9)
149
  *
5
 
6
  class Custom_Permalinks_Form {
7
 
8
+ private $permalink_metabox = 0;
9
+
10
  /**
11
  * Initialize WordPress Hooks
12
  *
20
  array( $this, 'custom_permalinks_get_sample_permalink_html' ), 10, 4
21
  );
22
 
23
+ add_action( 'add_meta_boxes',
24
+ array( $this, 'permalink_edit_box' )
25
+ );
26
+
27
+ add_filter( 'is_protected_meta',
28
+ array( $this, 'make_meta_protected' ), 10, 3
29
+ );
30
+
31
  add_action( 'save_post',
32
  array( $this, 'custom_permalinks_save_post' ), 10, 3
33
  );
65
  );
66
  }
67
 
68
+ /**
69
+ * Register meta box(es).
70
+ *
71
+ * @access public
72
+ * @since 1.4.0
73
+ *
74
+ * @return void
75
+ */
76
+ public function permalink_edit_box() {
77
+ add_meta_box( 'custom-permalinks-edit-box',
78
+ __( 'Permalink', 'custom-permalinks' ),
79
+ array( $this, 'meta_edit_form' ), null, 'normal', 'high',
80
+ array(
81
+ '__back_compat_meta_box' => false,
82
+ )
83
+ );
84
+ }
85
+
86
+ /**
87
+ * Set the meta_keys to protected which is created by the plugin.
88
+ *
89
+ * @access public
90
+ * @since 1.4.0
91
+ *
92
+ * @param boolean $protected
93
+ * Whether the key is protected or not
94
+ * @param string $meta_key
95
+ * Meta key
96
+ * @param string $meta_type
97
+ * Meta type
98
+ *
99
+ * @return boolean
100
+ * return `true` for the custom_permalink key
101
+ */
102
+ public function make_meta_protected( $protected, $meta_key, $meta_type ) {
103
+ if ( 'custom_permalink' === $meta_key ) {
104
+ $protected = true;
105
+ }
106
+ return $protected;
107
+ }
108
+
109
  /**
110
  * Save per-post options
111
  *
150
  * @return string
151
  */
152
  public function custom_permalinks_get_sample_permalink_html( $html, $id, $new_title, $new_slug ) {
153
+ $post = get_post( $id );
154
+ $this->permalink_metabox = 1;
155
 
156
  if ( 'attachment' == $post->post_type || $post->ID == get_option( 'page_on_front' ) ) {
157
  return $html;
162
  if ( '__true' === $excluded ) {
163
  return $html;
164
  }
165
+ $permalink = get_post_meta( $id, 'custom_permalink', true );
166
 
167
  ob_start();
168
 
169
  $cp_frontend = new Custom_Permalinks_Frontend();
170
  if ( 'page' == $post->post_type ) {
171
+ $original_permalink = $cp_frontend->custom_permalinks_original_page_link( $id );
172
+ $view_post = __( 'View Page', 'custom-permalinks' );
173
  } else {
174
+ $original_permalink = $cp_frontend->custom_permalinks_original_post_link( $id );
175
+ $view_post = __( 'View ' . ucfirst( $post->post_type ), 'custom-permalinks' );
176
  }
177
+ $this->custom_permalinks_get_form( $permalink, $original_permalink, false, $post->post_name );
178
 
179
  $content = ob_get_contents();
180
  ob_end_clean();
181
 
 
 
 
 
182
  if ( preg_match( "@view-post-btn.*?href='([^']+)'@s", $html, $matches ) ) {
183
  $permalink = $matches[1];
184
  } else {
193
  ( isset( $view_post ) ? "<span id='view-post-btn'><a href='$permalink' class='button button-small' target='_blank'>$view_post</a></span>\n" : "" );
194
  }
195
 
196
+ /**
197
+ * Per-post/page options (Wordpress > 2.9)
198
+ *
199
+ * @access public
200
+ * @return string
201
+ */
202
+ public function meta_edit_form( $post ) {
203
+ if ( isset( $this->permalink_metabox ) && 1 === $this->permalink_metabox ) {
204
+ wp_enqueue_script( 'custom-permalinks-form',
205
+ plugins_url( '/js/script-form.min.js', __FILE__ ), array(), false, true
206
+ );
207
+ return;
208
+ }
209
+
210
+ $screen = get_current_screen();
211
+ if ( 'add' === $screen->action ) {
212
+ wp_enqueue_script( 'custom-permalinks-form',
213
+ plugins_url( '/js/script-form.min.js', __FILE__ ), array(), false, true
214
+ );
215
+ return;
216
+ }
217
+
218
+ if ( 'attachment' == $post->post_type || $post->ID == get_option( 'page_on_front' ) ) {
219
+ return;
220
+ }
221
+
222
+ $exclude_post_types = $post->post_type;
223
+ $excluded = apply_filters( 'custom_permalinks_exclude_post_type', $exclude_post_types );
224
+ if ( '__true' === $excluded ) {
225
+ wp_enqueue_script( 'custom-permalinks-form',
226
+ plugins_url( '/js/script-form.min.js', __FILE__ ), array(), false, true
227
+ );
228
+ return;
229
+ }
230
+ $permalink = get_post_meta( $post->ID, 'custom_permalink', true );
231
+
232
+ ob_start();
233
+
234
+ $cp_frontend = new Custom_Permalinks_Frontend();
235
+ if ( 'page' == $post->post_type ) {
236
+ $original_permalink = $cp_frontend->custom_permalinks_original_page_link( $post->ID );
237
+ $view_post = __( 'View Page', 'custom-permalinks' );
238
+ } else {
239
+ $original_permalink = $cp_frontend->custom_permalinks_original_post_link( $post->ID );
240
+ $view_post = __( 'View ' . ucfirst( $post->post_type ), 'custom-permalinks' );
241
+ }
242
+ $this->custom_permalinks_get_form( $permalink, $original_permalink, false, $post->post_name );
243
+
244
+ $content = ob_get_contents();
245
+ ob_end_clean();
246
+
247
+ if ( 'trash' != $post->post_status ) {
248
+ wp_enqueue_script( 'custom-permalinks-form',
249
+ plugins_url( '/js/script-form.min.js', __FILE__ ), array(), false, true
250
+ );
251
+ if ( isset( $permalink ) && ! empty( $permalink ) ) {
252
+ if ( false !== strpos( $permalink, '%postname%' )
253
+ || false !== strpos( $permalink, '%pagename%' ) ) {
254
+ $permalink = str_replace( array( '%pagename%','%postname%' ), $post_name, $permalink );
255
+ }
256
+ $content .= ' <span id="view-post-btn">' .
257
+ '<a href="/' . $permalink . '" class="button button-small" target="_blank">' . $view_post . '</a>' .
258
+ '</span><br>';
259
+ } else {
260
+ $content .= ' <span id="view-post-btn">' .
261
+ '<a href="/' . $original_permalink . '" class="button button-small" target="_blank">' . $view_post .' </a>' .
262
+ '</span><br>';
263
+ }
264
+ }
265
+ echo $content;
266
+ }
267
+
268
  /**
269
  * Per-post options (Wordpress < 2.9)
270
  *
frontend/class-custom-permalinks-frontend.php CHANGED
@@ -88,7 +88,7 @@ class Custom_Permalinks_Frontend {
88
  " WHERE pm.meta_key = 'custom_permalink' " .
89
  " AND (pm.meta_value = '%s' OR pm.meta_value = '%s') " .
90
  " AND p.post_status != 'trash' AND p.post_type != 'nav_menu_item' " .
91
- " ORDER BY FIELD(post_status,'publish','private','draft','auto-draft','inherit')," .
92
  " FIELD(post_type,'post','page') LIMIT 1", $request_noslash, $request_noslash . "/" );
93
 
94
  $posts = $wpdb->get_results( $sql );
@@ -102,7 +102,7 @@ class Custom_Permalinks_Frontend {
102
  " LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) ) " .
103
  " AND post_status != 'trash' AND post_type != 'nav_menu_item'" .
104
  " ORDER BY LENGTH(meta_value) DESC, " .
105
- " FIELD(post_status,'publish','private','draft','auto-draft','inherit')," .
106
  " FIELD(post_type,'post','page'), p.ID ASC LIMIT 1",
107
  $request_noslash, $request_noslash . "/" );
108
 
88
  " WHERE pm.meta_key = 'custom_permalink' " .
89
  " AND (pm.meta_value = '%s' OR pm.meta_value = '%s') " .
90
  " AND p.post_status != 'trash' AND p.post_type != 'nav_menu_item' " .
91
+ " ORDER BY FIELD(post_status,'publish','private','pending','draft','auto-draft','inherit')," .
92
  " FIELD(post_type,'post','page') LIMIT 1", $request_noslash, $request_noslash . "/" );
93
 
94
  $posts = $wpdb->get_results( $sql );
102
  " LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) ) " .
103
  " AND post_status != 'trash' AND post_type != 'nav_menu_item'" .
104
  " ORDER BY LENGTH(meta_value) DESC, " .
105
+ " FIELD(post_status,'publish','private','pending','draft','auto-draft','inherit')," .
106
  " FIELD(post_type,'post','page'), p.ID ASC LIMIT 1",
107
  $request_noslash, $request_noslash . "/" );
108
 
frontend/js/script-form.js CHANGED
@@ -49,3 +49,6 @@ function changeSEOLink () {
49
  if ( checkYoastSEO ) {
50
  window.addEventListener("load", changeSEOLink, false);
51
  }
 
 
 
49
  if ( checkYoastSEO ) {
50
  window.addEventListener("load", changeSEOLink, false);
51
  }
52
+ if ( document.querySelector("#custom-permalinks-edit-box .inside").innerHTML.trim() === "" ) {
53
+ document.getElementById("custom-permalinks-edit-box").style.display = "none";
54
+ }
frontend/js/script-form.min.js CHANGED
@@ -1 +1 @@
1
- function changeSEOLinkOnBlur(){var e=document.getElementById("snippet_citeBase");if(e&&getHomeURL&&""!=getHomeURL.value&&getPermalink&&getPermalink.value)var t=0,n=setInterval(function(){t++,e.innerHTML=getHomeURL.value+"/"+getPermalink.value,5===t&&clearInterval(n)},1e3)}function changeSEOLink(){var e=document.getElementById("snippet_citeBase");if(e&&getHomeURL&&""!=getHomeURL.value&&getPermalink&&getPermalink.value){var t=0,n=setInterval(function(){t++,e.innerHTML=getHomeURL.value+"/"+getPermalink.value,5===t&&clearInterval(n)},1e3),a=document.getElementById("snippet-editor-title"),l=document.getElementById("snippet-editor-slug"),i=document.getElementById("snippet-editor-meta-description"),m=document.getElementById("snippet_cite");a&&a.addEventListener("blur",changeSEOLinkOnBlur,!1),l&&l.addEventListener("blur",changeSEOLinkOnBlur,!1),i&&i.addEventListener("blur",changeSEOLinkOnBlur,!1),m&&(m.style.display="none")}}var getHomeURL=document.getElementById("custom_permalinks_home_url"),getPermalink=document.getElementById("custom_permalink"),checkYoastSEO=document.getElementById("wpseo_meta");checkYoastSEO&&window.addEventListener("load",changeSEOLink,!1);
1
+ var getHomeURL=document.getElementById("custom_permalinks_home_url"),getPermalink=document.getElementById("custom_permalink"),checkYoastSEO=document.getElementById("wpseo_meta");function changeSEOLinkOnBlur(){var e=document.getElementById("snippet_citeBase");if(e&&getHomeURL&&""!=getHomeURL.value&&getPermalink&&getPermalink.value)var t=0,n=setInterval(function(){t++,e.innerHTML=getHomeURL.value+"/"+getPermalink.value,5===t&&clearInterval(n)},1e3)}function changeSEOLink(){var e=document.getElementById("snippet_citeBase");if(e&&getHomeURL&&""!=getHomeURL.value&&getPermalink&&getPermalink.value){var t=0,n=setInterval(function(){t++,e.innerHTML=getHomeURL.value+"/"+getPermalink.value,5===t&&clearInterval(n)},1e3),i=document.getElementById("snippet-editor-title"),l=document.getElementById("snippet-editor-slug"),a=document.getElementById("snippet-editor-meta-description"),m=document.getElementById("snippet_cite");i&&i.addEventListener("blur",changeSEOLinkOnBlur,!1),l&&l.addEventListener("blur",changeSEOLinkOnBlur,!1),a&&a.addEventListener("blur",changeSEOLinkOnBlur,!1),m&&(m.style.display="none")}}checkYoastSEO&&window.addEventListener("load",changeSEOLink,!1),""===document.querySelector("#custom-permalinks-edit-box .inside").innerHTML.trim()&&(document.getElementById("custom-permalinks-edit-box").style.display="none");
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: sasiddiqui, michaeltyson
3
  Donate link: https://www.paypal.me/yasglobal
4
  Tags: permalink, url, link, address, custom, redirect, custom post type, GDPR, GDPR Compliant
5
  Requires at least: 2.6
6
- Tested up to: 4.9
7
- Stable tag: 1.3.0
8
  License: GPLv3
9
  License URI: https://www.gnu.org/licenses/gpl.html
10
 
@@ -97,6 +97,12 @@ This process defines you the steps to follow either you are installing through W
97
 
98
  == Changelog ==
99
 
 
 
 
 
 
 
100
  = 1.3.0 - June 07, 2018 =
101
 
102
  * Enhancement
3
  Donate link: https://www.paypal.me/yasglobal
4
  Tags: permalink, url, link, address, custom, redirect, custom post type, GDPR, GDPR Compliant
5
  Requires at least: 2.6
6
+ Tested up to: 5.0
7
+ Stable tag: 1.4.0
8
  License: GPLv3
9
  License URI: https://www.gnu.org/licenses/gpl.html
10
 
97
 
98
  == Changelog ==
99
 
100
+ = 1.4.0 - Nov 08, 2018 =
101
+
102
+ * Enhancement
103
+ * Added Support for Gutenberg
104
+ * Set meta_keys to be protected to stop duplication in Custom Fields
105
+
106
  = 1.3.0 - June 07, 2018 =
107
 
108
  * Enhancement