Skip to content

Commit

Permalink
Merge pull request #1494 from GravityPDF/gravity-flow-inbox-pdf-meta
Browse files Browse the repository at this point in the history
Add Gravity PDF Metabox to Gravity Flow Inbox
  • Loading branch information
jakejackson1 authored Feb 19, 2024
2 parents 924f210 + a351d25 commit c7c23ef
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 54 deletions.
1 change: 1 addition & 0 deletions src/Controller/Controller_PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public function add_actions() {

/* Display PDF links in Gravity Forms Admin Area */
add_action( 'gform_entries_first_column_actions', [ $this->model, 'view_pdf_entry_list' ], 10, 4 );
add_action( 'gravityflow_workflow_detail_sidebar', [ $this->model, 'view_pdf_gravityflow_inbox' ], 10, 4 );

/* Add save PDF actions */
add_action( 'gform_after_submission', [ $this->model, 'maybe_save_pdf' ], 10, 2 );
Expand Down
19 changes: 19 additions & 0 deletions src/Helper/Helper_Templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ public function get_template_url() {
* @since 4.1
*/
public function get_all_templates() {
$options = GPDFAPI::get_options_class();
$debug = $options->get_option( 'debug_mode', 'No' );

if ( $debug === 'No' ) {
$cache_name = $this->data->template_transient_cache . '-template-list';
$cache = get_transient( $cache_name );

/* There may be no transient and we got a non-array. If that occurs reset $cache */
if ( is_array( $cache ) && ! empty( $cache ) ) {
return $cache;
}
}

$template_list = [];
$matched_templates_basename_list = [];
Expand All @@ -132,6 +144,12 @@ function( $file ) {
);
}

if ( $debug === 'No' ) {
$cache = $template_list;

set_transient( $cache_name, $cache, 604800 );
}

return $template_list;
}

Expand Down Expand Up @@ -427,6 +445,7 @@ public function get_template_info_by_path( $template_path, $cache_name = '', $ca
*/
public function flush_template_transient_cache() {
delete_transient( $this->data->template_transient_cache );
delete_transient( $this->data->template_transient_cache . '-template-list' );
}

/**
Expand Down
160 changes: 124 additions & 36 deletions src/Model/Model_PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,26 +638,7 @@ public function middle_user_capability( $action, $entry, $settings ) {
if ( is_user_logged_in() &&
( ( $this->options->get_option( 'limit_to_admin', 'No' ) === 'Yes' ) || ( $this->is_current_pdf_owner( $entry, 'logged_in' ) === false ) )
) {

/* Handle permissions checks */
$admin_permissions = $this->options->get_option( 'admin_capabilities', [ 'gravityforms_view_entries' ] );

/* loop through permissions and check if the current user has any of those capabilities */
$access = false;
foreach ( $admin_permissions as $permission ) {
if ( $this->gform->has_capability( $permission ) ) {
$access = true;

$this->log->notice(
'Current logged-in user has appropriate WordPress capability to view PDF',
[
'permission' => $permission,
]
);

break;
}
}
$access = $this->can_user_view_pdf_with_capabilities();

/* throw error if no access granted */
if ( ! $access ) {
Expand All @@ -669,6 +650,30 @@ public function middle_user_capability( $action, $entry, $settings ) {
return $action;
}

/**
* Check if the logged in user has permission to view the PDF
*
* @param int|null $user_id
*
* @return bool
*
* @since 6.8
*/
public function can_user_view_pdf_with_capabilities( $user_id = null ) {
$admin_permissions = $this->options->get_option( 'admin_capabilities', [ 'gravityforms_view_entries' ] );

/* loop through permissions and check if the current user has any of those capabilities */
$can_user_view_pdf = false;
foreach ( $admin_permissions as $permission ) {
if ( $this->gform->has_capability( $permission, $user_id ) ) {
$can_user_view_pdf = true;
break;
}
}

return $can_user_view_pdf;
}

/**
* Display PDF on Gravity Form entry list page
*
Expand All @@ -683,27 +688,33 @@ public function middle_user_capability( $action, $entry, $settings ) {
*/
public function view_pdf_entry_list( $form_id, $field_id, $value, $entry ) {

/* Only show the PDF metabox if a user has permission to view the documents */
if ( ! $this->can_user_view_pdf_with_capabilities() ) {
return;
}

$controller = $this->getController();
$pdf_list = $this->get_pdf_display_list( $entry );

if ( ! empty( $pdf_list ) ) {
if ( empty( $pdf_list ) ) {
return;
}

if ( count( $pdf_list ) > 1 ) {
$args = [
'pdfs' => $pdf_list,
'view' => strtolower( $this->options->get_option( 'default_action' ) ),
];
if ( count( $pdf_list ) > 1 ) {
$args = [
'pdfs' => $pdf_list,
'view' => strtolower( $this->options->get_option( 'default_action' ) ),
];

$controller->view->entry_list_pdf_multiple( $args );
} else {
/* Only one PDF for this form so display a simple 'View PDF' link */
$args = [
'pdf' => array_shift( $pdf_list ),
'view' => strtolower( $this->options->get_option( 'default_action' ) ),
];
$controller->view->entry_list_pdf_multiple( $args );
} else {
/* Only one PDF for this form so display a simple 'View PDF' link */
$args = [
'pdf' => array_shift( $pdf_list ),
'view' => strtolower( $this->options->get_option( 'default_action' ) ),
];

$controller->view->entry_list_pdf_single( $args );
}
$controller->view->entry_list_pdf_single( $args );
}
}

Expand Down Expand Up @@ -896,6 +907,77 @@ public function view_pdf_entry_detail( $args ) {
$controller->view->entry_detailed_pdf( $pdfs );
}

/**
* Display the PDF metabox in the Gravity Flow inbox
*
* @param array $form
* @param array $entry
* @param $current_step
* @param $args
*
* @return void
*
* @since 6.8
*/
public function view_pdf_gravityflow_inbox( $form, $entry, $current_step, $args ) {
/* Only show the PDF metabox if a user has permission to view the documents */
if ( ! $this->can_user_view_pdf_with_capabilities() ) {
return;
}

$active_pdfs = array_filter(
$form['gfpdf_form_settings'] ?? [],
function( $pdf ) {
return $pdf['active'] === true;
}
);

/* Only show the metabox if there's an active PDF */
if ( count( $active_pdfs ) === 0 ) {
return;
}

?>
<style type="text/css">
div.gf_entry_wrap #poststuff #gravitypdf-pdf-box-container .inside {
margin: 0;
padding: 0;
max-height: 18rem;
overflow-y: auto;
line-height: 1.4;
font-size: 13px;
}

#gravitypdf-pdf-box-container ul {
margin: 0;
padding: 0;
}

#gravitypdf-pdf-box-container li {
margin-bottom: 0.25rem;
border-bottom: 1px solid #EBEBF2;
padding: 0.5rem 0.75rem;
}

#gravitypdf-pdf-box-container li:last-of-type {
border-bottom: none;
margin-bottom: 0;
}
</style>

<div id="gravitypdf-pdf-box-container" class="postbox">

<h3 class="hndle" style="cursor:default;">
<span><?php esc_html_e( 'Gravity PDF', 'gravity-forms-pdf-extended' ); ?></span>
</h3>

<div class="inside">
<?php $this->view_pdf_entry_detail( [ 'entry' => $entry ] ); ?>
</div>
</div>
<?php
}

/**
* Add the pdf meta box to the entry detail page.
*
Expand Down Expand Up @@ -924,7 +1006,13 @@ function( $pdf ) {
$meta = [
'gfpdf-entry-details-list' => [
'title' => esc_html__( 'PDFs', 'gravity-forms-pdf-extended' ),
'callback' => [ $this, 'view_pdf_entry_detail' ],
'callback' => function( $args ) {
/* Only show the PDF metabox if a user has permission to view the documents */
if ( ! $this->can_user_view_pdf_with_capabilities() ) {
return;
}
$this->view_pdf_entry_detail( $args );
},
'context' => 'side',
'callback_args' => [
'form' => $form,
Expand Down
6 changes: 4 additions & 2 deletions src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,10 @@ private function register_scripts() {
wp_register_script( 'gfpdf_js_entries', PDF_PLUGIN_URL . 'dist/assets/js/gfpdf-entries.min.js', [ 'jquery' ], $version, true );

/* Localise admin script */
wp_localize_script( 'gfpdf_js_entrypoint', 'GFPDF', $this->data->get_localised_script_data( $this->options, $this->gform ) );
wp_localize_script( 'gfpdf_js_settings', 'GFPDF', $this->data->get_localised_script_data( $this->options, $this->gform ) );
$data = $this->data->get_localised_script_data( $this->options, $this->gform );

wp_localize_script( 'gfpdf_js_entrypoint', 'GFPDF', $data );
wp_localize_script( 'gfpdf_js_settings', 'GFPDF', $data );
}

/**
Expand Down
26 changes: 19 additions & 7 deletions tests/phpunit/unit-tests/test-helper-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,39 +105,51 @@ public function test_get_all_templates() {
$templates = $this->templates->get_all_templates();

/* Test the standard templates */
$this->assertEquals( 4, count( $templates ) );
$this->assertCount( 4, $templates );

/* Test for additional templates in PDF working directory */
touch( $gfpdf->data->template_location . 'test.php' );
touch( $gfpdf->data->template_location . 'test2.php' );

delete_transient( $gfpdf->data->template_transient_cache . '-template-list' );

$templates = $this->templates->get_all_templates();
$this->assertEquals( 6, count( $templates ) );
$this->assertCount( 6, $templates );

/* Test for override */
$templates = $this->templates->get_all_templates();
touch( $gfpdf->data->template_location . 'zadani.php' );

$this->assertEquals( 6, count( $templates ) );
delete_transient( $gfpdf->data->template_transient_cache . '-template-list' );

$templates = $this->templates->get_all_templates();

$this->assertCount( 6, $templates );

/* Check that a configuration.php or configuration.archive.php file don't count */
touch( $gfpdf->data->template_location . 'configuration.php' );
touch( $gfpdf->data->template_location . 'configuration.archive.php' );

delete_transient( $gfpdf->data->template_transient_cache . '-template-list' );

$templates = $this->templates->get_all_templates();
$this->assertEquals( 6, count( $templates ) );
$this->assertCount( 6, $templates );

/* Test for multisite templates */
if ( is_multisite() ) {
touch( $gfpdf->data->multisite_template_location . 'test3.php' );

delete_transient( $gfpdf->data->template_transient_cache . '-template-list' );

$templates = $this->templates->get_all_templates();
$this->assertEquals( 7, count( $templates ) );
$this->assertCount( 7, $templates );

/* Check for override */
touch( $gfpdf->data->multisite_template_location . 'zadani.php' );

delete_transient( $gfpdf->data->template_transient_cache . '-template-list' );

$templates = $this->templates->get_all_templates();
$this->assertEquals( 7, count( $templates ) );
$this->assertCount( 7, $templates );
}
}

Expand Down
Loading

0 comments on commit c7c23ef

Please sign in to comment.