Created
April 17, 2025 19:43
-
-
Save rafaehlers/508fe8fca12b2e2f7b043975d3a809e9 to your computer and use it in GitHub Desktop.
Duplicate an entry on approval if conditions match
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // DO NOT COPY THIS LINE | |
function custom_duplicate_gf_entry( $entry_id ) { | |
if ( ! class_exists( 'GravityView_Duplicate_Entry' ) ) { | |
return new WP_Error( 'missing_class', 'GravityView_Duplicate_Entry class not found.' ); | |
} | |
$entry = GFAPI::get_entry( $entry_id ); | |
if ( is_wp_error( $entry ) ) { | |
return $entry; | |
} | |
$duplicator = new GravityView_Duplicate_Entry(); | |
// Use reflection to access the private method `duplicate_entry` | |
try { | |
$reflection = new ReflectionClass( $duplicator ); | |
$method = $reflection->getMethod( 'duplicate_entry' ); | |
$method->setAccessible( true ); | |
$result = $method->invoke( $duplicator, $entry ); | |
return $result; | |
} catch ( ReflectionException $e ) { | |
return new WP_Error( 'reflection_error', $e->getMessage() ); | |
} | |
} | |
add_action( 'gravityview/approve_entries/approved', function( $entry_id ) { | |
$entry = GFAPI::get_entry( $entry_id ); | |
if ( is_wp_error( $entry ) ) { | |
error_log( 'Could not load entry.' ); | |
return; | |
} | |
// Condition checks: | |
$form_id = rgar( $entry, 'form_id' ); | |
$field_261_1 = rgar( $entry, '261.1' ); | |
$field_218 = rgar( $entry, '218' ); | |
if ( intval( $form_id ) !== 35 ) { | |
return; // Not the correct form | |
} | |
if ( empty( $field_261_1 ) ) { | |
return; // Field 261.1 is empty | |
} | |
if ( $field_218 !== '1' ) { | |
return; // Field 218 does not equal "1" | |
} | |
// All conditions passed — duplicate the entry | |
$result = custom_duplicate_gf_entry( $entry_id ); | |
if ( is_wp_error( $result ) ) { | |
error_log( 'Duplication failed: ' . $result->get_error_message() ); | |
} else { | |
error_log( 'Entry duplicated successfully. New entry ID: ' . $result ); | |
} | |
}, 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment