Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support advanced post creation add on #1982

Merged
merged 12 commits into from
Feb 14, 2024
2 changes: 1 addition & 1 deletion assets/js/admin-views.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@

var show_warning = ! dismissed_warning && value.configured === 0;

$( '#' + index + '-fields' ).find( '.notice-warning' ).toggle( show_warning );
$( '#' + index + '-fields' ).find( '.notice-no-link' ).toggle( show_warning );
$( 'li[aria-controls="' + index + '-view"]' )
.toggleClass( 'tab-not-configured', show_warning )
.find( '.tab-icon' )
Expand Down
4 changes: 2 additions & 2 deletions includes/admin/metaboxes/views/view-configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

<div id="single-fields" class="gv-section">

<div class="notice notice-warning inline is-dismissible">
<div class="notice notice-warning notice-no-link inline is-dismissible">
<h3><?php printf( esc_html__( 'Note: %s', 'gk-gravityview' ), sprintf( esc_html__( 'No fields link to the %s layout.', 'gk-gravityview' ), esc_html__( 'Single Entry', 'gk-gravityview' ) ) ); ?></h3>
<p><a data-beacon-article-modal="54c67bbae4b0512429885516" href="https://docs.gravitykit.com/article/70-linking-to-a-single-entry"><?php printf( esc_html__( 'Learn how to link to %s', 'gk-gravityview' ), esc_html__( 'Single Entry', 'gk-gravityview' ) ); ?></a></p>
</div>
Expand All @@ -87,7 +87,7 @@

<div id="edit-fields" class="gv-section">

<div class="notice notice-warning inline is-dismissible">
<div class="notice notice-warning notice-no-link inline is-dismissible">
<h3><?php printf( esc_html__( 'Note: %s', 'gk-gravityview' ), sprintf( esc_html__( 'No fields link to the %s layout.', 'gk-gravityview' ), esc_html__( 'Edit Entry', 'gk-gravityview' ) ) ); ?></h3>
<p><a data-beacon-article-modal="54c67bb9e4b0512429885513" href="https://docs.gravitykit.com/article/67-configuring-the-edit-entry-screen"><?php printf( esc_html__( 'Learn how to link to %s', 'gk-gravityview' ), esc_html__( 'Edit Entry', 'gk-gravityview' ) ); ?></a></p>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php
/**
* Add Advanced Post Creation customizations.
*
* @file class-gravityview-plugin-hooks-code-snippets.php
* @package GravityView
* @license GPL2+
* @author GravityKit <[email protected]>
* @link http://www.gravitykit.com
* @copyright Copyright 2024, Katz Web Services, Inc.
*
* @since $ver$
*/

use GV\View;

/**
* Add Gravity Forms Advanced Post Creation compatibility.
* @since $ver$
* @link https://www.gravityforms.com/add-ons/advanced-post-creation/
*/
final class GravityView_Plugin_Hooks_Gravity_Forms_Advanced_Post_Creation extends GravityView_Plugin_and_Theme_Hooks {
/**
* @inheritDoc
* @since $ver$
*/
protected $class_name = 'GF_Advanced_Post_Creation';

/**
* Updates the connected post.
* @since $ver$
*
* @param array $form Gravity Forms form array.
* @param string $entry_id Numeric ID of the entry that was updated.
* @param GravityView_Edit_Entry_Render $render The entry renderer.
*/
public function update_post_on_entry_edit( array $form, string $entry_id, GravityView_Edit_Entry_Render $render ): void {
if ( ! $form || ! $entry_id ) {
return;
}

$apc = GF_Advanced_Post_Creation::get_instance();

$created_posts = gform_get_meta( $entry_id, $apc->get_slug() . '_post_id' );

if ( ! $created_posts ) {
return;
}

$feeds = $apc->get_active_feeds( rgar( $form, 'id' ) );

if ( ! $feeds ) {
return;
}

// Map feeds on their id for easy access.
$feeds = array_column( $feeds, null, 'id' );

foreach ( $created_posts as $created_post ) {
$feed_id = rgar( $created_post, 'feed_id' );
$feed = rgar( $feeds, $feed_id );
if ( ! $feed ) {
continue;
}

$apc->update_post( $created_post['post_id'], $feed, $render->entry, $form );
}
}

/**
* Adds a notice if the form contains a feed for Advanced Post Creation.
* @since $ver$
*
* @param mixed $_ unused template name.
* @param string $context The context.
* @param int|string $view_id The view post ID.
* @param bool $echo Whether to print the HTML directly instead of returning.
*
* @return string|null The output.
*/
public function add_view_notification( $_, string $context, $view_id, bool $echo = false ): ?string {
if ( 'edit' !== $context ) {
return null;
}

$view = View::by_id( $view_id );
if ( ! $view ) {
return null;
}

$form = $view->form;
$apc = GF_Advanced_Post_Creation::get_instance();
$feeds = $apc->get_active_feeds( $form->ID );

if ( ! $feeds ) {
return null;
}

$notification_html = <<<HTML
<div class="gv-grid-col-1-1">
<div class="notice notice-warning inline">
<p><strong>%s</strong></p>
<p>%s</p>
</div>
</div>
HTML;

$notification = sprintf(
$notification_html,
// translators: Do not translate [link] and [/link]; they are replaced with an anchor tag.
esc_html__( 'Caution: [link]Advanced Post Creation[/link] is active for this form.', 'gk-gravityview' ),
esc_html__( 'Editing entries in GravityView may also update a connected post.', 'gk-gravityview' )
);

$apc_feed_link = admin_url( sprintf( 'admin.php?page=gf_edit_forms&amp;view=settings&amp;subview=%s&amp;id=%d', $apc->get_slug(), $form->ID ) );

$notification = strtr( $notification, [
'[link]' => '<a style="font-size: inherit;" href="' . esc_url( $apc_feed_link ) . '" target="_blank">',
'[/link]' => '<span class="screen-reader-text"> ' . esc_html__( '(This link opens in a new window.)', 'gk-gravityview' ) . '</span></a>',
] );

if ( $echo ) {
echo $notification;
}

return $notification;
}

/**
* @inheritDoc
* @since $ver$
*/
protected function add_hooks(): void {
parent::add_hooks();

add_action( 'gravityview/edit_entry/after_update', [ $this, 'update_post_on_entry_edit' ], 10, 3 );
add_action( 'gravityview_render_directory_active_areas', [ $this, 'add_view_notification' ], 5, 4 );
}
}

new GravityView_Plugin_Hooks_Gravity_Forms_Advanced_Post_Creation;
5 changes: 5 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ Beautifully display your Gravity Forms entries. Learn more on [gravitykit.com](h

== Changelog ==

= develop =

* Added: Support for the [Advanced Post Creation Add-On](https://www.gravityforms.com/add-ons/advanced-post-creation/) when editing entries in GravityView's Edit Entry mode


= 2.19.6 on February 7, 2024 =

This update introduces the ability to send notifications using Gravity Forms when an entry is deleted, improves sorting and survey field ratings, and updates key components for better performance and compatibility.
Expand Down