@@ -26,7 +26,7 @@ function __construct() {
2626
2727 function add_custom_merge_tag ( $ merge_tags , $ form_id , $ fields , $ element_id ) {
2828
29- if ( ! GFCommon:: has_post_field ( $ fields ) ) {
29+ if ( ! $ this -> is_post_generating_form ( $ form_id , $ fields ) ) {
3030 return $ merge_tags ;
3131 }
3232
@@ -41,16 +41,45 @@ function add_custom_merge_tag( $merge_tags, $form_id, $fields, $element_id ) {
4141 function replace_merge_tag ( $ text , $ form , $ entry ) {
4242
4343 $ custom_merge_tag = '{post_permalink} ' ;
44- if ( strpos ( $ text , $ custom_merge_tag ) === false || ! rgar ( $ entry , ' post_id ' ) ) {
44+ if ( strpos ( $ text , $ custom_merge_tag ) === false ) {
4545 return $ text ;
4646 }
4747
48- $ post_permalink = get_permalink ( rgar ( $ entry , 'post_id ' ) );
48+ $ post_id = $ this ->get_post_id_by_entry ( $ entry );
49+ if ( ! $ post_id ) {
50+ return $ text ;
51+ }
52+
53+ $ post_permalink = get_permalink ( $ post_id );
4954 $ text = str_replace ( $ custom_merge_tag , $ post_permalink , $ text );
5055
5156 return $ text ;
5257 }
5358
59+ function get_post_id_by_entry ( $ entry ) {
60+ $ post_id = rgar ( $ entry , 'post_id ' );
61+ if ( ! $ post_id && function_exists ( 'gf_advancedpostcreation ' ) ) {
62+ $ entry_post_ids = gform_get_meta ( $ entry ['id ' ], gf_advancedpostcreation ()->get_slug () . '_post_id ' );
63+ if ( ! empty ( $ entry_post_ids ) ) {
64+ $ post_id = $ entry_post_ids [0 ]['post_id ' ];
65+ }
66+ }
67+ return $ post_id ;
68+ }
69+
70+ function is_post_generating_form ( $ form_id , $ fields ) {
71+
72+ if ( GFCommon::has_post_field ( $ fields ) ) {
73+ return true ;
74+ }
75+
76+ if ( function_exists ( 'gf_advancedpostcreation ' ) && gf_advancedpostcreation ()->has_feed ( $ form_id ) ) {
77+ return true ;
78+ }
79+
80+ return false ;
81+ }
82+
5483}
5584
5685new GWPostPermalink ();
0 commit comments