Skip to content

Commit

Permalink
Fixed PHP Notice on Edit Post Page
Browse files Browse the repository at this point in the history
  • Loading branch information
samiahmedsiddiqui committed Sep 26, 2017
1 parent ca6d777 commit fa0414e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 35 deletions.
27 changes: 16 additions & 11 deletions admin/class-custom-permalinks-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,19 @@ public function custom_permalinks_save_post( $id ) {
*/
public function custom_permalinks_delete_permalink( $id ) {
global $wpdb;
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key = 'custom_permalink' AND `post_id` = %d", $id ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key = 'custom_permalink' AND post_id = %d", $id ) );
}

/**
* Per-post/page options (Wordpress > 2.9)
*/
public function custom_permalinks_get_sample_permalink_html( $html, $id, $new_title, $new_slug ) {
public function custom_permalinks_get_sample_permalink_html( $html, $id, $new_title, $new_slug ) {
$permalink = get_post_meta( $id, 'custom_permalink', true );
$post = get_post( $id );

if ( $post->post_type == 'attachment' || $post->ID == get_option( 'page_on_front' ) ) {
return $html;
}

$permalink = get_post_meta( $id, 'custom_permalink', true );
$post = get_post( $id );

ob_start();

require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-frontend.php' );
Expand Down Expand Up @@ -244,8 +243,9 @@ public function custom_permalinks_save_tag( $id ) {

require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-frontend.php' );
$custom_permalinks_frontend = new Custom_Permalinks_Frontend();
if ( $newPermalink == $custom_permalinks_frontend->custom_permalinks_original_tag_link( $id ) )
if ( $newPermalink == $custom_permalinks_frontend->custom_permalinks_original_tag_link( $id ) ) {
$newPermalink = '';
}

$term = get_term( $id, 'post_tag' );
$this->custom_permalinks_save_term( $term, str_replace( '%2F', '/', urlencode( $newPermalink ) ) );
Expand All @@ -263,8 +263,9 @@ public function custom_permalinks_save_category( $id ) {

require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-frontend.php' );
$custom_permalinks_frontend = new Custom_Permalinks_Frontend();
if ( $newPermalink == $custom_permalinks_frontend->custom_permalinks_original_category_link( $id ) )
if ( $newPermalink == $custom_permalinks_frontend->custom_permalinks_original_category_link( $id ) ) {
$newPermalink = '';
}

$term = get_term( $id, 'category' );
$this->custom_permalinks_save_term( $term, str_replace( '%2F', '/', urlencode( $newPermalink ) ) );
Expand Down Expand Up @@ -308,19 +309,23 @@ public function custom_permalinks_delete_term( $id ) {
* Check Conflicts and resolve it (e.g: Polylang)
*/
public function custom_permalinks_check_conflicts( $requested_url = '' ) {
if ( $requested_url == '' ) return;
if ( $requested_url == '' ) {
return;
}

// Check if the Polylang Plugin is installed so, make changes in the URL
if ( defined( 'POLYLANG_VERSION' ) ) {
$polylang_config = get_option( 'polylang' );
if ( $polylang_config['force_lang'] == 1 ) {

if ( strpos( $requested_url, 'language/' ) !== false )
if ( strpos( $requested_url, 'language/' ) !== false ) {
$requested_url = str_replace( "language/", "", $requested_url );
}

$remove_lang = ltrim( strstr( $requested_url, '/' ), '/' );
if ( $remove_lang != '' )
if ( $remove_lang != '' ) {
return $remove_lang;
}
}
}
return $requested_url;
Expand Down
2 changes: 1 addition & 1 deletion custom-permalinks-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
exit();
}

define( 'CUSTOM_PERMALINKS_PLUGIN_VERSION', '1.2.2' );
define( 'CUSTOM_PERMALINKS_PLUGIN_VERSION', '1.2.3' );

if ( ! defined( 'CUSTOM_PERMALINKS_PATH' ) ) {
define( 'CUSTOM_PERMALINKS_PATH', plugin_dir_path( __FILE__ ) );
Expand Down
2 changes: 1 addition & 1 deletion custom-permalinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin Name: Custom Permalinks
* Plugin URI: https://wordpress.org/plugins/custom-permalinks/
* Description: Set custom permalinks on a per-post basis
* Version: 1.2.2
* Version: 1.2.3
* Author: Sami Ahmed Siddiqui
* Author URI: https://www.yasglobal.com/web-design-development/wordpress/custom-permalinks/
* Donate link: https://www.paypal.me/yasglobal
Expand Down
57 changes: 37 additions & 20 deletions frontend/class-custom-permalinks-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,20 @@ public function custom_permalinks_request( $query ) {
$original_url = NULL;

// Get request URI, strip parameters and /'s
$url = parse_url( get_bloginfo( 'url' ) );
$url = isset( $url['path'] ) ? $url['path'] : '';
$url = parse_url( get_bloginfo( 'url' ) );
$url = isset( $url['path'] ) ? $url['path'] : '';
$request = ltrim( substr( $_SERVER['REQUEST_URI'], strlen( $url ) ), '/' );
$request = ( ( $pos = strpos( $request, '?' ) ) ? substr( $request, 0, $pos ) : $request );

if ( ! $request ) return $query;
if ( ! $request ) {
return $query;
}

$ignore = apply_filters( 'custom_permalinks_request_ignore', $request );

if ( $ignore === '__true' ) return $query;
if ( '__true' === $ignore ) {
return $query;
}

if ( defined( 'POLYLANG_VERSION' ) ) {
require_once( CUSTOM_PERMALINKS_PATH . 'admin/class-custom-permalinks-form.php' );
Expand All @@ -71,8 +75,9 @@ public function custom_permalinks_request( $query ) {
// A post matches our request

// Preserve this url for later if it's the same as the permalink (no extra stuff)
if ( $request_noslash == trim( $posts[0]->meta_value, '/' ) )
if ( $request_noslash == trim( $posts[0]->meta_value, '/' ) ) {
$_CPRegisteredURL = $request;
}

if ( $posts[0]->post_status == 'draft' ) {
if ( $posts[0]->post_type == 'page' ) {
Expand All @@ -99,16 +104,19 @@ public function custom_permalinks_request( $query ) {
if ( $original_url === NULL ) {
// See if any terms have a matching permalink
$table = get_option( 'custom_permalink_table' );
if ( ! $table ) return $query;
if ( ! $table ) {
return $query;
}

foreach ( array_keys( $table ) as $permalink ) {
if ( $permalink == substr( $request_noslash, 0, strlen( $permalink ) )
|| $permalink == substr( $request_noslash . "/", 0, strlen( $permalink ) ) ) {
$term = $table[$permalink];

// Preserve this url for later if it's the same as the permalink (no extra stuff)
if ( $request_noslash == trim( $permalink, '/' ) )
if ( $request_noslash == trim( $permalink, '/' ) ) {
$_CPRegisteredURL = $request;
}

if ( $term['kind'] == 'category' ) {
$category_link = $this->custom_permalinks_original_category_link( $term['id'] );
Expand Down Expand Up @@ -136,10 +144,11 @@ public function custom_permalinks_request( $query ) {
$_SERVER['QUERY_STRING'] = ( ( $pos = strpos( $original_url, '?' ) ) !== false ? substr( $original_url, $pos + 1 ) : '' );
parse_str( $_SERVER['QUERY_STRING'], $query_array );
$old_values = array();
if ( is_array( $query_array ) )
foreach ( $query_array as $key => $value ) {
$old_values[$key] = $_REQUEST[$key];
$_REQUEST[$key] = $_GET[$key] = $value;
if ( is_array( $query_array ) ) {
foreach ( $query_array as $key => $value ) {
$old_values[$key] = $_REQUEST[$key];
$_REQUEST[$key] = $_GET[$key] = $value;
}
}

// Re-run the filter, now with original environment in place
Expand All @@ -164,10 +173,13 @@ public function custom_permalinks_request( $query ) {
*/
public function custom_permalinks_redirect() {
// Get request URI, strip parameters
$url = parse_url( get_bloginfo( 'url' ) );
$url = isset( $url['path'] ) ? $url['path'] : '';
$url = parse_url( get_bloginfo( 'url' ) );
$url = isset( $url['path'] ) ? $url['path'] : '';
$request = ltrim( substr( $_SERVER['REQUEST_URI'], strlen( $url ) ), '/' );
if ( ( $pos = strpos( $request, "?" ) ) ) $request = substr( $request, 0, $pos );
$pos = strpos( $request, "?" );
if ( $pos ) {
$request = substr( $request, 0, $pos );
}

if ( defined( 'POLYLANG_VERSION' ) ) {
require_once( CUSTOM_PERMALINKS_PATH . 'admin/class-custom-permalinks-form.php' );
Expand Down Expand Up @@ -259,7 +271,9 @@ public function custom_permalinks_page_link( $permalink, $page ) {
*/
public function custom_permalinks_term_link( $permalink, $term ) {
$table = get_option( 'custom_permalink_table' );
if ( is_object( $term ) ) $term = $term->term_id;
if ( is_object( $term ) ) {
$term = $term->term_id;
}

$custom_permalink = $this->custom_permalinks_permalink_for_term( $term );
if ( $custom_permalink ) {
Expand Down Expand Up @@ -346,7 +360,9 @@ public function custom_permalinks_trailingslash( $string, $type ) {
$request = ltrim( isset( $url['path'] ) ? substr( $string, strlen( $url['path'] ) ) : $string, '/' );
add_filter( 'user_trailingslashit', array( $this, 'custom_permalinks_trailingslash' ), 10, 2 );

if ( ! trim( $request ) ) return $string;
if ( ! trim( $request ) ) {
return $string;
}

if ( trim( $_CPRegisteredURL, '/' ) == trim( $request, '/' ) ) {
if ( isset( $url['path'] ) ) {
Expand All @@ -364,10 +380,11 @@ public function custom_permalinks_trailingslash( $string, $type ) {
*/
public function custom_permalinks_permalink_for_term( $id ) {
$table = get_option( 'custom_permalink_table' );
if ( $table )
foreach ( $table as $link => $info ) {
if ( $info['id'] == $id ) {
return $link;
if ( $table ) {
foreach ( $table as $link => $info ) {
if ( $info['id'] == $id ) {
return $link;
}
}
}
return false;
Expand Down
8 changes: 6 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Donate link: https://www.paypal.me/yasglobal
Tags: permalink, url, link, address, custom, redirect, custom post type
Requires at least: 2.6
Tested up to: 4.8
Stable tag: 1.2.2
Stable tag: 1.2.3
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand All @@ -31,7 +31,7 @@ within that category.
If you want to exclude some Permalink to processed with the plugin so, just add the filter looks like this:
`
function check_xml_sitemap_url( $permalink ) {
if ( strpos( $permalink, 'sitemap.xml' ) !== false ) {
if ( false !== strpos( $permalink, 'sitemap.xml' )) {
return '__true';
}
return;
Expand All @@ -47,6 +47,10 @@ add_filter( 'custom_permalinks_request_ignore', 'check_xml_sitemap_url' );

== Changelog ==

= 1.2.3 =

* Fixed PHP Notice on Edit Post Page

= 1.2.2 =

* Fixed Typo of Class Object for term on Admin Page
Expand Down

0 comments on commit fa0414e

Please sign in to comment.