Completed
Push — develop ( c0c6f0...50e840 )
by Gennady
18:15
created

GravityView_Plugin_Hooks_Gravity_PDF::add_hooks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Add GravityView compatibility to Gravity PDF
4
 *
5
 * @file      class-gravityview-plugin-hooks-gravity-pdf.php
6
 * @package   GravityView
7
 * @license   GPL2+
8
 * @author    Katz Web Services, Inc.
9
 * @link      http://gravityview.co
10
 * @copyright Copyright 2015, Katz Web Services, Inc.
11
 *
12
 * @since develop
13
 */
14
15
/**
16
 * @inheritDoc
17
 * @since develop
18
 */
19
class GravityView_Plugin_Hooks_Gravity_PDF extends GravityView_Plugin_and_Theme_Hooks {
20
21
	/**
22
	 * @inheritDoc
23
	 * @since 1.15.2
24
	 */
25
	protected $constant_name = 'PDF_EXTENDED_VERSION';
26
27
	public function add_hooks() {
28
		parent::add_hooks();
29
30
		add_filter( 'gravityview/fields/custom/content_before', array( $this, 'fix_entry_id_for_custom_content_shortcode' ), 11, 2 );
31
	}
32
33
	/**
34
	 * @see https://github.com/gravityview/Multiple-Forms/issues/41
35
	 */
36
	public function fix_entry_id_for_custom_content_shortcode( $content, $context ) {
37
		if ( ! $context->entry->is_multi() ) {
38
			return $content;
39
		}
40
41
		if ( ! $shortcodes = GPDFAPI::get_mvc_class( 'Model_Shortcodes' ) ) {
42
			return;
43
		}
44
45
		global $wpdb;
46
		$table = GFFormsModel::get_meta_table_name();
47
48
		foreach ( $shortcodes->get_shortcode_information( 'gravitypdf', $content ) as $shortcode ) {
49
			// Let's make sure this entry ID is correct for the supplied form
50
			$form_id = $wpdb->get_var( $wpdb->prepare( "SELECT form_id FROM $table WHERE display_meta LIKE %s", '%"' . $wpdb->esc_like( $shortcode['attr']['id'] ) . '"%' ) );
51
52
			// Inject the needed entry ID
53
			$replace = str_replace(
54
				sprintf( 'entry="%d"', $shortcode['attr']['entry'] ),
55
				sprintf( 'entry="%d"', $context->entry[ $form_id ]['id'] ),
56
				$shortcode['shortcode']
57
			);
58
59
			$content = str_replace( $shortcode['shortcode'], $replace, $content );
60
		}
61
62
		return $content;
63
	}
64
}
65
66
new GravityView_Plugin_Hooks_Gravity_PDF;
67