Skip to content

Commit

Permalink
Fix bug with numeric custom entry slugs
Browse files Browse the repository at this point in the history
As per valid comment here:

a8c6b12#commitcomment-23020287
  • Loading branch information
soulseekah committed Jul 11, 2017
1 parent e7f53b5 commit 72c5545
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions future/includes/class-gv-entry-gravityforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public static function from_entry( $entry ) {
$self->entry = $entry;

$self->ID = $self->entry['id'];
$self->slug = \GravityView_API::get_entry_slug( $self->ID, $self->as_entry() );

return $self;
}
Expand Down
10 changes: 5 additions & 5 deletions future/includes/class-gv-shortcode-gravityview.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ public function callback( $atts, $content = null ) {
/**
* Editing a single entry.
*/
} else if ( $entry = $request->is_edit_entry() ) {
} else if ( $entry = $request->is_edit_entry() ) {
if ( $entry['status'] != 'active' ) {
gravityview()->log->notice( 'Entry ID #{entry_id} is not active', array( 'entry_id' => $entry->ID ) );
return __( 'You are not allowed to view this content.', 'gravityview' );
}

if ( is_numeric( get_query_var( \GV\Entry::get_endpoint_name() ) ) && apply_filters( 'gravityview_custom_entry_slug', false ) ) {
gravityview()->log->error( 'Entry ID #{entry_id} can only be accessed via slug', array( 'entry_id' => $entry->ID ) );
if ( apply_filters( 'gravityview_custom_entry_slug', false ) && $entry->slug != get_query_var( \GV\Entry::get_endpoint_name() ) ) {
gravityview()->log->error( 'Entry ID #{entry_id} was accessed by a bad slug', array( 'entry_id' => $entry->ID ) );
return __( 'You are not allowed to view this content.', 'gravityview' );
}

Expand All @@ -110,8 +110,8 @@ public function callback( $atts, $content = null ) {
return __( 'You are not allowed to view this content.', 'gravityview' );
}

if ( is_numeric( get_query_var( \GV\Entry::get_endpoint_name() ) ) && apply_filters( 'gravityview_custom_entry_slug', false ) ) {
gravityview()->log->error( 'Entry ID #{entry_id} can only be accessed via slug', array( 'entry_id' => $entry->ID ) );
if ( apply_filters( 'gravityview_custom_entry_slug', false ) && $entry->slug != get_query_var( \GV\Entry::get_endpoint_name() ) ) {
gravityview()->log->error( 'Entry ID #{entry_id} was accessed by a bad slug', array( 'entry_id' => $entry->ID ) );
return __( 'You are not allowed to view this content.', 'gravityview' );
}

Expand Down
8 changes: 4 additions & 4 deletions future/includes/class-gv-view.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ public static function content( $content ) {
return __( 'You are not allowed to view this content.', 'gravityview' );
}

if ( is_numeric( get_query_var( \GV\Entry::get_endpoint_name() ) ) && apply_filters( 'gravityview_custom_entry_slug', false ) ) {
gravityview()->log->error( 'Entry ID #{entry_id} can only be accessed via slug', array( 'entry_id' => $entry->ID ) );
if ( apply_filters( 'gravityview_custom_entry_slug', false ) && $entry->slug != get_query_var( \GV\Entry::get_endpoint_name() ) ) {
gravityview()->log->error( 'Entry ID #{entry_id} was accessed by a bad slug', array( 'entry_id' => $entry->ID ) );
return __( 'You are not allowed to view this content.', 'gravityview' );
}

Expand All @@ -289,8 +289,8 @@ public static function content( $content ) {
return __( 'You are not allowed to view this content.', 'gravityview' );
}

if ( is_numeric( get_query_var( \GV\Entry::get_endpoint_name() ) ) && apply_filters( 'gravityview_custom_entry_slug', false ) ) {
gravityview()->log->error( 'Entry ID #{entry_id} can only be accessed via slug', array( 'entry_id' => $entry->ID ) );
if ( apply_filters( 'gravityview_custom_entry_slug', false ) && $entry->slug != get_query_var( \GV\Entry::get_endpoint_name() ) ) {
gravityview()->log->error( 'Entry ID #{entry_id} was accessed by a bad slug', array( 'entry_id' => $entry->ID ) );
return __( 'You are not allowed to view this content.', 'gravityview' );
}

Expand Down
5 changes: 4 additions & 1 deletion tests/unit-tests/GravityView_Future_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,12 @@ public function test_entry_by_slug() {
$this->assertNull( $entry );

$entry = \GV\GF_Entry::by_id( $entry_id );
$this->assertEquals( $entry->slug, $entry_id );
$this->assertEquals( $entry_id, $entry->ID );

add_filter( 'gravityview_custom_entry_slug', '__return_true' );

$random = wp_generate_password( 8, false );
$random = strtolower( wp_generate_password( 8, false ) );
add_filter( 'gravityview_entry_slug', function( $slug ) use ( $random ) {
return "sentinel-$random";
}, 10 );
Expand All @@ -168,9 +169,11 @@ public function test_entry_by_slug() {
$this->assertNull( $entry );

$entry = \GV\GF_Entry::by_id( "sentinel-$random" );
$this->assertEquals( $entry->slug, "sentinel-$random" );
$this->assertEquals( $entry_id, $entry->ID );

$entry = \GV\GF_Entry::by_slug( "sentinel-$random" );
$this->assertEquals( $entry->slug, "sentinel-$random" );
$this->assertEquals( $entry_id, $entry->ID );

$entry = \GV\GF_Entry::by_slug( $entry_id );
Expand Down

0 comments on commit 72c5545

Please sign in to comment.