Skip to content

Commit

Permalink
Fix Static Homepage redirect issue
Browse files Browse the repository at this point in the history
  • Loading branch information
samiahmedsiddiqui committed Aug 6, 2020
1 parent dd932e1 commit 3ec5eec
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 49 deletions.
2 changes: 1 addition & 1 deletion custom-permalinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Custom Permalinks
* Plugin URI: https://wordpress.org/plugins/custom-permalinks/
* Description: Set custom permalinks on a per-post basis
* Version: 1.6.0-beta
* Version: 1.6.0-beta3
* Author: Sami Ahmed Siddiqui
* Author URI: https://www.custompermalinks.com/
* License: GPLv3
Expand Down
107 changes: 61 additions & 46 deletions frontend/class-custom-permalinks-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,53 @@ public function init()
add_action( 'edited_term', array( $this, 'save_term' ), 10, 3 );
add_action( 'delete_term', array( $this, 'delete_term_permalink' ), 10, 3 );
add_action( 'rest_api_init', array( $this, 'rest_edit_form' ) );
add_action( 'update_option_page_on_front',
array( $this, 'static_homepage' ), 10, 2
);

add_filter( 'get_sample_permalink_html',
array( $this, 'sample_permalink_html' ), 10, 2
);
add_filter( 'is_protected_meta', array( $this, 'protect_meta' ), 10, 2 );
}

/**
* Initialize WordPress Hooks.
*
* @since 1.6.0
* @access private
*
* @param object $post WP Post Object.
*
* return bool false Whether to show Custom Permalink form or not.
*/
private function exclude_custom_permalinks( $post )
{
$args = array(
'public' => true,
);
$excluded_post_types = apply_filters( 'custom_permalinks_exclude_post_type',
$post->post_type
);
$public_post_types = get_post_types( $args, 'objects' );

if ( isset( $this->permalink_metabox ) && 1 === $this->permalink_metabox ) {
$check_availability = true;
} elseif ( 'attachment' === $post->post_type ) {
$check_availability = true;
} elseif ( $post->ID === intval( get_option( 'page_on_front' ) ) ) {
$check_availability = true;
} elseif ( ! isset( $public_post_types[$post->post_type] ) ) {
$check_availability = true;
} elseif ( '__true' === $excluded_post_types ) {
$check_availability = true;
} else {
$check_availability = false;
}

return $check_availability;
}

/**
* Register meta box(es).
*
Expand Down Expand Up @@ -87,7 +127,7 @@ public function save_post( $post_id )
return;
}

delete_post_meta( $post_id, 'custom_permalink' );
$this->delete_permalink( $post_id );

$cp_frontend = new Custom_Permalinks_Frontend();
$original_link = $cp_frontend->original_post_link( $post_id );
Expand All @@ -111,11 +151,7 @@ public function save_post( $post_id )
*/
public function delete_permalink( $post_id )
{
global $wpdb;
$wpdb->query( $wpdb->prepare(
"DELETE FROM $wpdb->postmeta WHERE meta_key = 'custom_permalink' AND post_id = %d",
$post_id
) );
delete_post_meta( $post_id, 'custom_permalink' );
}

/**
Expand Down Expand Up @@ -213,19 +249,10 @@ private function get_permalink_html( $post, $meta_box = false )
public function sample_permalink_html( $html, $post_id )
{
$post = get_post( $post_id );
$this->permalink_metabox = 1;

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

$exclude_post_types = $post->post_type;
$excluded = apply_filters( 'custom_permalinks_exclude_post_type',
$exclude_post_types
);
if ( '__true' === $excluded ) {
$disable_custom_permalink = $this->exclude_custom_permalinks( $post );
$this->permalink_metabox = 1;
if ( $disable_custom_permalink ) {
return $html;
}

Expand All @@ -244,38 +271,13 @@ public function sample_permalink_html( $html, $post_id )
*/
public function meta_edit_form( $post )
{
$form_return = 0;
if ( isset( $this->permalink_metabox ) && 1 === $this->permalink_metabox ) {
$form_return = 1;
}

if ( 'attachment' === $post->post_type ) {
$form_return = 1;
} elseif ( $post->ID === get_option( 'page_on_front' ) ) {
$form_return = 1;
}

$args = array(
'public' => true,
);
$post_types = get_post_types( $args, 'objects' );
if ( ! isset( $post_types[$post->post_type] ) ) {
$form_return = 1;
}

$exclude_post_types = $post->post_type;
$excluded = apply_filters( 'custom_permalinks_exclude_post_type',
$exclude_post_types
);
if ( '__true' === $excluded ) {
$form_return = 1;
}

if ( 1 === $form_return ) {
$disable_custom_permalink = $this->exclude_custom_permalinks( $post );
if ( $disable_custom_permalink ) {
wp_enqueue_script( 'custom-permalinks-form',
plugins_url( '/js/script-form.min.js', __FILE__ ), array(),
false, true
);

return;
}

Expand Down Expand Up @@ -652,4 +654,17 @@ public function rest_edit_form()
)
);
}

/**
* Delete the Permalink for the Page selected as the Static Homepage.
*
* @since 1.6.0
* @access public
*
* @param int $prev_homepage_id Page ID of previously set Front Page.
* @param int $new_homepage_id Page ID of current Front Page.
*/
public function static_homepage( $prev_homepage_id, $new_homepage_id ) {
$this->delete_permalink( $new_homepage_id );
}
}
5 changes: 3 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: sasiddiqui, michaeltyson
Tags: permalink, url, link, address, custom, redirect, custom post type, GDPR, GDPR Compliant
Requires at least: 2.6
Tested up to: 5.4
Stable tag: 1.6.0-beta
Stable tag: 1.6.0-beta3
License: GPLv3
License URI: https://www.gnu.org/licenses/gpl.html

Expand Down Expand Up @@ -100,7 +100,7 @@ This process defines you the steps to follow either you are installing through W

== Changelog ==

= 1.6.0-beta - Aug 04, 2020 =
= 1.6.0-beta3 - Aug 06, 2020 =

* Bugs
* [Undefined index and undefined variable error](https://github.com/samiahmedsiddiqui/custom-permalinks/issues/28)
Expand All @@ -111,6 +111,7 @@ This process defines you the steps to follow either you are installing through W
* Fix Yoast Canonical double slash issue
* [Replacing category_link with term_link](https://github.com/samiahmedsiddiqui/custom-permalinks/issues/34)
* [Bug with WPML and Use directory for default language](https://github.com/samiahmedsiddiqui/custom-permalinks/issues/36)
* Fix Static Homepage redirect issue
* Enhancements
* Improved Gutenberg Support
* Added compatibility for WPML language switcher
Expand Down

0 comments on commit 3ec5eec

Please sign in to comment.